Changeset 3348


Ignore:
Timestamp:
09/02/08 20:43:53 (12 years ago)
Author:
astephen
Message:
 
Location:
nappy/trunk/nappy/nc_interface
Files:
2 edited

Legend:

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

    r3347 r3348  
    4444 
    4545 
    46 def cdms2na(ncfile, na_file_names, naVars={}, variables=None, nFilesOnly="no",  
     46def cdms2na(ncfile, na_file_names, na_vars={}, variables=None, only_return_file_names="no",  
    4747            ffi="automatic", spacer="    ", floatFormat="%g", size_limit=None): 
    4848    """ 
     
    5050    to write a NASA Ames file. 
    5151    """ 
    52     #print infilename, outfilenames, nFilesOnly, naVars, variables 
    5352    if type(na_file_names) == type("string"):  
    5453        na_file_names = [na_file_names] 
     
    6867    if not variables: 
    6968        variables = cdms_file.listvariables() 
    70         #for var in cdms_file.listvariables(): 
    71             #vars.append(cdms_file(var))     
    72              
     69 
    7370    for variable in variables: 
    74         varObj = cdms_file(variable) 
     71        var_obj = cdms_file(variable) 
     72 
    7573        # Deal with singleton variables 
    76         if not hasattr(varObj, "rank"): 
     74        if not hasattr(var_obj, "rank"): 
    7775                varMetadata = cdms_file[variable].attributes 
    78                 varValue = varObj 
    79                 #print varMetadata, varValue, varMetadata.keys(), varMetadata._obj_.id 
    80                 varObj = cdms.createVariable(Numeric.array(varObj), id=nappy.cdms_utils.var_utils.getBestName(varMetadata).replace(" ", "_"), attributes=varMetadata) 
    81                 #print varObj, dir(varObj); sys.exit() 
    82                 varObj.value = varObj._data[0] 
    83                 #varObj.rank = 0 
    84                  
    85         #print varObj, varObj.attributes                          
    86         vars.append(varObj) 
     76                varValue = var_obj 
     77                var_obj = cdms.createVariable(Numeric.array(var_obj), id=nappy.cdms_utils.var_utils.getBestName(varMetadata).replace(" ", "_"), attributes=varMetadata) 
     78                var_obj.value = var_obj._data[0]                  
     79        vars.append(var_obj) 
    8780         
    8881    # Re-order variables if they have the attribute 'nasa_ames_var_number' 
     
    10598     
    10699    builder = NAContentCollector(vars, globals, rule=rule, cdms_file=cdms_file) 
    107     #print builder.naDict["X"] 
    108     builtNADicts = [[builder.naDict, builder.varIDs]] 
    109     if builder.varIDs == None: 
     100    built_na_dicts = [[builder.naDict, builder.var_ids]] 
     101 
     102    if builder.var_ids == None: 
    110103        msg = "\nNo files created after variables parsed." 
    111104        print msg 
     
    116109        builder = NAContentCollector(builder.varBin, globals, rule=rule, cdms_file=cdms_file) 
    117110        output_message = output_message + builder.output_message 
    118         if builder.varIDs != None:  builtNADicts.append([builder.naDict, builder.varIDs]) 
    119  
    120     # Return only filenames if only want to know them now. 
     111        if builder.var_ids != None:  built_na_dicts.append([builder.naDict, builder.var_ids]) 
     112 
     113    # Return only file_names if only want to know them now. 
    121114    ncount = 1 
    122     fileNames = [] 
    123     if nFilesOnly == "yes":  
    124         for i in builtNADicts: 
    125             if len(builtNADicts) == 1: 
     115    file_names = [] 
     116    if only_return_file_names == "yes":  
     117        for i in built_na_dicts: 
     118            if len(built_na_dicts) == 1: 
    126119                suffix = "" 
    127120            else: 
    128121                suffix = "_%s" % ncount 
    129             nameparts = outfilenames[0].split(".")     
    130             newname = (".".join(nameparts[:-1])) + suffix + "." + nameparts[-1] 
    131             fileNames.append(newname) 
     122            name_parts = output_file_names[0].split(".")     
     123            new_name = (".".join(name_parts[:-1])) + suffix + "." + name_parts[-1] 
     124            file_names.append(new_name) 
    132125        ncount = ncount + 1 
    133126             
    134         return fileNames 
     127        return file_names 
    135128                 
    136     msg = "\n%s files to write" % len(builtNADicts) 
     129    msg = "\n%s files to write" % len(built_na_dicts) 
    137130    print msg 
    138131    output_message.append(msg) 
     
    140133    count = 1 
    141134    ncount = 1 
    142     for i in builtNADicts: 
    143         if len(outfilenames) == 1: 
    144             if len(builtNADicts) == 1: 
     135    for i in built_na_dicts: 
     136        if len(output_file_names) == 1: 
     137            if len(built_na_dicts) == 1: 
    145138                suffix = "" 
    146139            else: 
    147140                suffix = "_%s" % ncount 
    148             nameparts = outfilenames[0].split(".")     
    149             newname = (".".join(nameparts[:-1])) + suffix + "." + nameparts[-1] 
     141            name_parts = output_file_names[0].split(".")     
     142            new_name = (".".join(name_parts[:-1])) + suffix + "." + name_parts[-1] 
    150143        else: 
    151             newname = outfilenames[count - 1] 
     144            new_name = output_file_names[count - 1] 
    152145  
    153         msg = "\nWriting output NASA Ames file: %s" % newname 
     146        msg = "\nWriting output NASA Ames file: %s" % new_name 
    154147        print msg 
    155148        output_message.append(msg) 
    156149         
    157150        builtNADict = i[0] 
    158         for key in naVars.keys(): 
     151        for key in na_vars.keys(): 
    159152            if key in allowed_overwrite_metadata: 
    160153             
    161154                if key in array_args: 
    162                     newItem = naVars[key].split()                   
     155                    newItem = na_vars[key].split()                  
    163156                else: 
    164                     newItem = naVars[key] 
     157                    newItem = na_vars[key] 
    165158                                     
    166159                if newItem != builtNADict[key]: 
     
    195188                 
    196189                # Write data to output file 
    197                 newnamePlusLetter = "%s-%.3d.na" % (newname[:-3], ivol) 
    198                 fileList.append(newnamePlusLetter) 
    199                 general.openNAFile(newnamePlusLetter, 'w', NADictCopy, spacer=spacer, floatFormat=floatFormat) 
    200                 msg = "\nOutput files split on size limit: %s\nFilename used: %s" % (size_limit, newnamePlusLetter) 
     190                new_namePlusLetter = "%s-%.3d.na" % (new_name[:-3], ivol) 
     191                fileList.append(new_namePlusLetter) 
     192                general.openNAFile(new_namePlusLetter, 'w', NADictCopy, spacer=spacer, floatFormat=floatFormat) 
     193                msg = "\nOutput files split on size limit: %s\nFilename used: %s" % (size_limit, new_namePlusLetter) 
    201194                print msg 
    202195                output_message.append(msg) 
     
    206199 
    207200        else:            
    208             general.openNAFile(newname, 'w', builtNADict, spacer=spacer, floatFormat=floatFormat) 
     201            general.openNAFile(new_name, 'w', builtNADict, spacer=spacer, floatFormat=floatFormat) 
    209202 
    210203        msg = "\nWrote the following variables:" + "\n\t" + ("\n\t".join(i[1][0])) 
     
    224217            count = count + len(fileList) 
    225218        else: 
    226             msg = msg + "\n\nNASA Ames file written successfully: %s" % newname 
     219            msg = msg + "\n\nNASA Ames file written successfully: %s" % new_name 
    227220            count = count + 1 
    228221        ncount = ncount + 1 
  • nappy/trunk/nappy/nc_interface/na_content_collector.py

    r3347 r3348  
    3838cdms.setAutoBounds("off")  
    3939 
    40 class CdmsToNABuilder --> NAContentCollector: (naDict, varIDs, varBin) 
     40class CdmsToNABuilder --> NAContentCollector: (naDict, var_ids, varBin) 
    4141__init__ --> sets everything up and runs it move some to --> analyse() 
    4242analyseVariables 
Note: See TracChangeset for help on using the changeset viewer.