Ignore:
Timestamp:
15/02/08 18:46:46 (12 years ago)
Author:
astephen
Message:

Fixes to make NC to NA route work.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • nappy/trunk/nappy/nc_interface/nc_to_na.py

    r3416 r3441  
    5858 
    5959        # Now need to read CDMS file so parent class methods are compatible 
    60         (cdms_variables, global_atts) = self._readCDMSFile(self.nc_file, var_ids, exclude_vars) 
     60        (cdms_variables, global_atts) = self._readCDMSFile(var_ids, exclude_vars) 
    6161        nappy.nc_interface.cdms_to_na.CDMSToNA.__init__(self, cdms_variables, global_atts=global_atts,  
    6262                    na_items_to_override=na_items_to_override, only_return_file_names=only_return_file_names) 
    6363  
    6464 
    65     def _readCDMSFile(self, nc_file, var_ids=None, exclude_vars=[]): 
     65    def _readCDMSFile(self, var_ids=None, exclude_vars=[]): 
    6666        """ 
    6767        Reads the file and returns all the CDMS variables in a list as well 
     
    6969        If var_ids is defined then only get those. 
    7070        """ 
    71         fin = cdms.open(nc_file) 
     71        fin = cdms.open(self.nc_file) 
    7272        cdms_variables = [] 
    7373 
     
    9090        """ 
    9191        self.convert() 
     92 
    9293        file_names = [] 
     94        # create file name if not given 
     95        if na_file == None: 
     96            base_name = self.nc_file 
     97            if base_name[-3:] == ".nc": 
     98                base_name = base_name[:-3] 
     99            na_file = base_name + ".na" 
    93100 
    94101        file_counter = 1 
     102        # Now, create some valid file names 
    95103        for this_na_dict in self.na_dict_list: 
    96             if len(na_dict_list) == 1: 
     104            if len(self.na_dict_list) == 1: 
    97105                suffix = "" 
    98106            else: 
     
    100108 
    101109            # Create file name 
    102             name_parts = self.output_file_names[0].split(".")     
     110            name_parts = na_file.split(".")     
    103111            new_name = (".".join(name_parts[:-1])) + suffix + "." + name_parts[-1] 
    104112            file_names.append(new_name) 
     
    127135 
    128136        # Now loop through writing the outputs 
    129         for na_dict_and_var_ids in built_na_dicts: 
     137        for na_dict_and_var_ids in self.na_dict_list: 
    130138            file_name = file_names[file_counter - 1] 
    131139            msg = "\nWriting output NASA Ames file: %s" % file_name 
     
    134142 
    135143            # Set up current na dict 
    136             this_na_dict = (na_dict_and_var_ids, vars_to_write) 
     144            (this_na_dict, vars_to_write) = na_dict_and_var_ids 
    137145 
    138146            # Override content of NASA Ames if they are permitted 
     
    153161            # Cope with size limits if specified and FFI is 1001 
    154162            # Seems to be writing different chunks of a too long array to different na_dicts to then write to separate files. 
    155             if self.size_limit and (this_na_dict["FFI"] == 1001 and len(this_na_dict["V"][0]) > self.size_limit): 
     163            if size_limit is not None and (this_na_dict["FFI"] == 1001 and len(this_na_dict["V"][0]) > size_limit): 
    156164                files_written = self._writeNAFileSubsetsWithinSizeLimit(this_na_dict, file_name, delimiter=delimiter, 
    157165                                                                        float_format=float_format, size_limit=size_limit) 
    158166                file_list.extend(files_written) 
     167 
    159168            # If not having to split file into multiple outputs (normal condition) 
    160169            else:                
    161                 nappy.openNAFile(file_name, 'w', this_na_dict, delimiter=delimiter, float_format=float_format) 
     170                x = nappy.openNAFile(file_name, 'w', this_na_dict) 
     171                x.write(delimiter=delimiter, float_format=float_format) 
     172                x.close() 
    162173                file_list.append(file_name) 
    163174 
     
    168179         
    169180            msg = "" 
    170             aux_var_count = this_na_dict[1][1] 
     181            aux_var_count = vars_to_write[1] 
    171182            if len(aux_var_count) > 0: 
    172183                msg = "\nWrote the following auxiliary variables:" + "\n\t" + ("\n\t".join(aux_var_count))       
    173184             
    174             singleton_var_count = this_na_dict[1][2] 
     185            singleton_var_count = vars_to_write[2] 
    175186            if len(singleton_var_count) > 0: 
    176187                msg = "\nWrote the following Singleton variables:" + "\n\t" + ("\n\t".join(singleton_var_count)) 
     
    179190                msg = msg + ("\n\nNASA Ames file(s) written successfully: \n%s" % "\n".join(file_list)) 
    180191 
    181             actual_file_counter += len(file_list) 
     192            full_file_counter += len(file_list) 
    182193            file_counter += 1 
    183194 
     
    185196            self.output_message.append(msg) 
    186197             
    187         full_file_count = (actual_file_counter - 1) 
     198        full_file_count = full_file_counter - 1 
    188199        if full_file_count == 1: 
    189200            plural = "" 
     
    236247 
    237248            # Write data to output file 
    238             nappy.openNAFile(file_name_plus_letter, 'w', na_dict_copy, delimiter=delimiter,  
    239                                       float_format=float_format) 
     249            x = nappy.openNAFile(file_name_plus_letter, 'w', na_dict_copy) 
     250            x.write(delimiter=delimiter, float_format=float_format) 
     251            x.close() 
    240252 
    241253            msg = "\nOutput files split on size limit: %s\nFilename used: %s" % (size_limit, file_name_plus_letter) 
Note: See TracChangeset for help on using the changeset viewer.