Changeset 3991 for nappy


Ignore:
Timestamp:
04/06/08 17:46:03 (11 years ago)
Author:
astephen
Message:

Converted all global_atts to lists of key,value pairs so order can be maintained.

Location:
nappy/trunk/nappy
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • nappy/trunk/nappy/nappy_api.py

    r3987 r3991  
    5656 
    5757# Let's convert a NASA Ames file into a NetCDF file, and add some of our own global attributes 
    58 glob_atts = {"Project": "Really important scientific project involving worms", 
    59              "Errata": "I meant worm holes!"} 
     58glob_atts = [("Project", "Really important scientific project involving worms"), 
     59             ("Errata": "I meant worm holes!")] 
    6060na_file = "data_files/1020.na" 
    6161nc_file = "test_outputs/try_1020.nc" 
     
    7676 
    7777# Let's take some in-memory CDMS objects and write them to one, or more, NASA Ames file(s). 
    78 # We need to give it a list of cdms variables and a global attributes dictionary. 
     78# We need to give it a list of cdms variables and a global attributes list of tuples/lists. 
    7979# We also want to instruct nappy to overwrite the content of its  
    8080# MNAME (Mission Name) header line with our specific mission name. 
    8181# Also, tell nappy to write the output to NASA Ames file format index (FFI) 2310 
    8282# because we know it is compatible. 
    83 nappy.convertCDMSObjectsToNA([cdms_var_1, cdms_var_2], {"Institute": "British Atmospheric Data Centre"},  
     83nappy.convertCDMSObjectsToNA([cdms_var_1, cdms_var_2], [("Institute", "British Atmospheric Data Centre")],  
    8484              na_file="test_outputs/cdms_to_na.na",  
    8585              na_items_to_override={"MNAME": "Atlantic Divergence Mission 2009"},  
    8686              requested_ffi=2310) 
    8787 
    88 # Let's take a list of cdms variables and a global attributes dictionary and write 
     88# Let's take a list of cdms variables and a global attributes list and write 
    8989# them to a CSV file. 
    90 nappy.convertCDMSObjectsToCSV(cdms_vars, global_atts_dict, csv_file) 
     90nappy.convertCDMSObjectsToCSV(cdms_vars, global_atttributes, csv_file) 
    9191 
    9292# Let's take a NASA Ames dictionary object, and write it to a NetCDF file 
     
    171171 
    172172def convertNAToNC(na_file, nc_file=None, mode="w", variables=None, aux_variables=None, 
    173                  global_attributes={"Conventions":"CF-1.0"}, 
     173                 global_attributes=[("Conventions", "CF-1.0")], 
    174174                 time_units=None, time_warning=True, 
    175175                 rename_variables={}): 
     
    186186              If not set then nappy will use any compatible variables it finds as  
    187187              auxiliary variables. 
    188     global_attributes - is a dictionary of global attributes to add to the output file. 
     188    global_attributes - is a list of global attributes to add to the output file. 
    189189    rename_variables - is a dictionary of {old_name: new_name} variable ID pairs that nappy  
    190190              should use to rename variables before it writes them to file.   
     
    296296     
    297297 
    298 def convertCDMSObjectsToNA(cdms_vars, global_atts_dict, na_file,  
     298def convertCDMSObjectsToNA(cdms_vars, global_attributes, na_file,  
    299299              na_items_to_override={}, requested_ffi=None, delimiter=default_delimiter,  
    300300              float_format=default_float_format, size_limit=None, annotation=False, no_header=False): 
    301301    """ 
    302     Takes a list of cdms variables and a global attributes dictionary and 
     302    Takes a list of cdms variables and a list of global attributes and 
    303303    writes them to one or more NASA Ames files. Arguments are: 
    304304  
    305305    cdms_vars - is a list of CDMS variables 
    306     global_atts_dict - is a dictionary of {key: value} pairs for header 
     306    global_attributes - is a list of (key, value) pairs for header 
    307307    na_items_to_override - is a dictionary of {key: value} pairs to overwrite in  
    308308                output files. Typically the keys are in:   
    309                 ("DATE", "RDATE", "ANAME", "MNAME","ONAME", "ORG", "SNAME", "VNAME".) 
     309                ("DATE", "RDATE", "ANAME", "MNAME","ONAME", "ORG", "SNAME", "VNAME", "SCOM", "NCOM".) 
    310310    requested_ffi - is the NASA Ames File Format Index (FFI) you wish to write to.  
    311311                Note that there are only limited options available depending on the data  
     
    321321    """ 
    322322    import nappy.nc_interface.cdms_objs_to_na_file 
    323     convertor = nappy.nc_interface.cdms_objs_to_na_file.CDMSObjectsToNAFile(cdms_vars, global_atts=global_atts_dict,  
     323    convertor = nappy.nc_interface.cdms_objs_to_na_file.CDMSObjectsToNAFile(cdms_vars, global_attributes=global_attributes,  
    324324                        na_items_to_override=na_items_to_override, requested_ffi=requested_ffi) 
    325325    convertor.convert() 
     
    329329 
    330330 
    331 def convertCDMSObjectsToCSV(cdms_vars, global_atts_dict, csv_file, **arg_dict): 
    332     """ 
    333     Takes a list of cdms variables and a global attributes dictionary and 
     331def convertCDMSObjectsToCSV(cdms_vars, global_attributes, csv_file, **arg_dict): 
     332    """ 
     333    Takes a list of cdms variables and a global attributes list and 
    334334    writes them to one or more CSV files. 
    335335    """ 
    336336    arg_dict["delimiter"] = "," 
    337     return apply(convertCDMSObjectsToNA, [cdms_vars, global_atts_dict, csv_file], arg_dict) 
     337    return apply(convertCDMSObjectsToNA, [cdms_vars, global_attributes, csv_file], arg_dict) 
    338338 
    339339 
     
    361361 
    362362    # Write global attributes 
    363     for (att, value) in global_attributes.items(): 
     363    for (att, value) in global_attributes: 
    364364        setattr(fout, att, value) 
    365365         
     
    375375      * a list of primary NASA Ames variables as CDMS variables 
    376376      * a list of auxiliary NASA Ames variables as CDMS variables, 
    377       * a dictionary of global attributes 
     377      * a list of global attributes 
    378378    """ 
    379379    cdms_var_list = [] 
  • nappy/trunk/nappy/nc_interface/cdms_objs_to_na_file.py

    r3987 r3991  
    4646    """ 
    4747 
    48     def __init__(self, cdms_variables, global_atts={}, na_items_to_override={}, 
     48    def __init__(self, cdms_variables, global_attributes=[], na_items_to_override={}, 
    4949                 only_return_file_names=False, 
    5050                 requested_ffi=None): 
     
    5858        """ 
    5959        self.cdms_variables = cdms_variables 
    60         self.global_atts = global_atts 
     60        self.global_attributes = global_attributes 
    6161        self.na_items_to_override = na_items_to_override 
    6262        self.only_return_file_names = only_return_file_names 
  • nappy/trunk/nappy/nc_interface/cdms_to_na.py

    r3987 r3991  
    4848    """ 
    4949 
    50     def __init__(self, cdms_variables, global_atts={}, na_items_to_override={},  
     50    def __init__(self, cdms_variables, global_attributes=[], na_items_to_override={},  
    5151                 only_return_file_names=False, requested_ffi=None): 
    5252        """ 
     
    5454        """ 
    5555        self.cdms_variables = cdms_variables 
    56         self.global_atts = global_atts 
     56        self.global_attributes = global_attributes 
    5757        self.na_items_to_override = na_items_to_override 
    5858        self.only_return_file_names = only_return_file_names 
     
    8686        variables = self._reorderVars(variables) 
    8787 
    88         # Make first call to collector class that creates NA dict from CDMS variables and global atts dicts 
     88        # Make first call to collector class that creates NA dict from CDMS variables and global atts list  
    8989        collector = nappy.nc_interface.na_content_collector.NAContentCollector(variables,  
    90                                         self.global_atts, requested_ffi=self.requested_ffi) 
     90                                        self.global_attributes, requested_ffi=self.requested_ffi) 
    9191        collector.collectNAContent() 
    9292 
     
    108108        while len(collector.unused_vars) > 0: 
    109109            collector = nappy.nc_interface.na_content_collector.NAContentCollector(collector.unused_vars,  
    110                                        self.global_atts, requested_ffi=self.requested_ffi) 
     110                                       self.global_attributes, requested_ffi=self.requested_ffi) 
    111111            collector.collectNAContent()            
    112112            self.output_message += collector.output_message 
  • nappy/trunk/nappy/nc_interface/na_content_collector.py

    r3988 r3991  
    5151    """ 
    5252     
    53     def __init__(self, variables, global_attributes={}, requested_ffi=None): 
     53    def __init__(self, variables, global_attributes=[], requested_ffi=None): 
    5454        """ 
    5555        Sets up instance variables and calls appropriate methods to 
     
    5858        Input arguments are: 
    5959          * variables - list/tuple of actual CDMS variables 
    60           * global_attributes - dictionary of user-defined globals to include. 
     60          * global_attributes - list of user-defined global (key,value) attributes to include. 
    6161 
    6262        Typical usage: 
     
    7373        #    [ordered_vars,  auxiliary_vars,   rank_zero_vars] 
    7474        self.var_ids = None 
    75         self.globals = global_attributes         
     75        self.globals = dict(global_attributes)   
    7676        self.requested_ffi = requested_ffi 
    7777        self.rank_zero_vars = [] 
  • nappy/trunk/nappy/nc_interface/na_to_cdms.py

    r3966 r3991  
    7575     
    7676    def __init__(self, na_file_obj, variables="all", aux_variables="all", 
    77                  global_attributes={"Conventions":"CF-1.0"}, 
     77                 global_attributes=[("Conventions":"CF-1.0")], 
    7878                 time_units=None, time_warning=True,  
    7979                 rename_variables={}): 
     
    9999        """ 
    100100        Reads the NASA Ames file object and converts to CDMS objects. 
    101         Returns (variable_list, aux_variable_list, global_attribute_dict). 
     101        Returns (variable_list, aux_variable_list, global_attributes_list). 
    102102        All these can be readily written to a CDMS File object. 
    103103        """ 
     
    131131        """ 
    132132        Maps the NASA Ames comments section to global attributes and append them to the  
    133         self.global_attributes dictionary. 
    134         """ 
     133        self.global_attributes list. 
     134        """ 
     135        glob_atts = dict(self.global_attributes) 
    135136         
    136137        for key in na_to_nc_map.keys(): 
     
    165166                    # Tidy comment line then write to global atts dict 
    166167                    comment_line = comment_line.replace("\n\n", "\n") 
    167                     #self.cdms_file.comment = comment_line 
    168                     self.global_attributes["comment"] = comment_line 
     168 
     169                    #self.global_attributes["comment"] = comment_line 
     170                    glob_atts["comment"] = comment_line 
    169171 
    170172                elif key == ("ONAME", "ORG"): 
     
    172174                    institution = "%s (ONAME from NASA Ames file); %s (ORG from NASA Ames file)." % \ 
    173175                                         (self.na_file_obj.ONAME, self.na_file_obj.ORG) 
    174                     self.global_attributes["institution"] = institution 
     176                    #self.global_attributes["institution"] = institution 
     177                    glob_atts["institution"] = institution 
    175178                    #self.cdms_file.institution = "%s (ONAME from NASA Ames file); %s (ORG from NASA Ames file)." % (self.na_file_obj.ONAME, self.na_file_obj.ORG) 
    176179 
     
    178181                    # Any other strange tuple just gets merged into a string 
    179182                    item = (getattr(self.na_file_obj, key[0])) + "\n" + (getattr(self.na_file_obj, key[1])) 
     183 
    180184                    # setattr(self.cdms_file, cdms_map.fromNA[key], item) 
    181                     self.global_attributes[na_to_nc_map[key]] = item 
     185                    #self.global_attributes[na_to_nc_map[key]] = item 
     186                    glob_atts[na_to_nc_map[key]] = item 
    182187 
    183188            elif key == "RDATE": 
     
    191196                # self.cdms_file.history = hist 
    192197                print "DIDNT HAVE MAPPING SO PUT HISTORY IN LINE 160 IN na_to_cdms.py" 
    193                 self.global_attributes["history"] = hist 
     198                #self.global_attributes["history"] = hist 
     199                glob_atts["history"] = hist            
    194200            else: 
    195201                # Anything else just needs to be stored as a global attribute 
    196202                # setattr(self.cdms_file, cdms_map.fromNA[key], getattr(self, key)) 
    197                 self.global_attributes[na_to_nc_map[key]] = getattr(self.na_file_obj, key) 
    198  
     203                #self.global_attributes[na_to_nc_map[key]] = getattr(self.na_file_obj, key) 
     204                glob_atts[na_to_nc_map[key]] = getattr(self.na_file_obj, key) 
     205 
     206        # Now remake global atts list 
     207        new_atts = [] 
     208 
     209        for key, value in self.global_attributes: 
     210            new_atts.append( (key, glob_atts[key]) ) 
     211        used_keys = [i[0] for i in new_atts] 
     212 
     213        for key in glob_atts.keys(): 
     214            if key not in used_keys: 
     215                new_atts.append( (key, glob_atts[key]) ) 
     216 
     217        self.global_attributes = new_atts[:] 
    199218 
    200219    def _convertCdmsVariables(self): 
  • nappy/trunk/nappy/nc_interface/na_to_nc.py

    r3966 r3991  
    3434     
    3535    def __init__(self, na_file, variables=None, aux_variables=None, 
    36                  global_attributes={"Conventions":"CF-1.0"}, 
     36                 global_attributes=[("Conventions","CF-1.0")], 
    3737                 time_units=None, time_warning=True,  
    3838                 rename_variables={}): 
     
    7979 
    8080        # Write global attributes 
    81         for (att, value) in self.global_attributes.items(): 
     81        for (att, value) in self.global_attributes: 
    8282            setattr(fout, att, value) 
    8383         
  • nappy/trunk/nappy/nc_interface/nc_to_na.py

    r3988 r3991  
    7272 
    7373        # Now need to read CDMS file so parent class methods are compatible 
    74         (cdms_variables, global_atts) = self._readCDMSFile(var_ids, exclude_vars) 
    75         nappy.nc_interface.cdms_to_na.CDMSToNA.__init__(self, cdms_variables, global_atts=global_atts,  
     74        (cdms_variables, global_attributes) = self._readCDMSFile(var_ids, exclude_vars) 
     75        nappy.nc_interface.cdms_to_na.CDMSToNA.__init__(self, cdms_variables, global_attributes=global_attributes,  
    7676                                                        na_items_to_override=na_items_to_override,  
    7777                                                        only_return_file_names=only_return_file_names, 
     
    8282        """ 
    8383        Reads the file and returns all the CDMS variables in a list as well 
    84         as the global attributes: (cdms_variable_list, global_atts_dict) 
     84        as the global attributes: (cdms_variable_list, global_atts_list) 
    8585        If var_ids is defined then only get those. 
    8686        """ 
     
    9797                    cdms_variables.append(fin(var_id)) 
    9898 
    99         globals = fin.attributes 
     99        globals = fin.attributes.items() 
    100100        return (cdms_variables, globals)  
    101101 
Note: See TracChangeset for help on using the changeset viewer.