Changeset 3324 for nappy/trunk/nappy/bin


Ignore:
Timestamp:
08/02/08 09:31:46 (12 years ago)
Author:
astephen
Message:
 
Location:
nappy/trunk/nappy/bin
Files:
2 moved

Legend:

Unmodified
Added
Removed
  • nappy/trunk/nappy/bin/na2nc.py

    r3321 r3324  
    55 
    66""" 
    7 na2nc 
    8 ===== 
     7na2nc.py 
     8======== 
    99 
    1010Converts a NASA Ames file to a NetCDF file. 
     
    1313===== 
    1414 
    15    na2nc.py [<options>] -i <infilename> -o <outfilename>  
     15   na2nc.py [-m <mode>] [-v <var_list>] [-a <aux_var_list>] [-g <global_atts_list>] 
     16            [-r <rename_vars_list>] [-t <time_units>] [-n] -i <na_file> [-o <nc_file>]  
    1617 
    1718Where 
    1819----- 
    1920 
    20     infilename  - name of input file (NASA Ames). 
    21     outfilename - name of output file (NetCDF). 
    22     options     - list of options: 
    23                     -t <time_units_string>  (such as "hours since 2003-04-30 10:00:00") 
     21    <mode>                              is the file mode, either "w" for write or "a" for append 
     22    <var_list>                  is a comma-separated list of variable names to include 
     23    <aux_var_list>              is a comma-separated list of auxiliary variables names to include 
     24    <global_atts_list>  is a comma-separated list of global attributes to add 
     25    <rename_vars_list>  is a comma-separated list of <old_name>,<new_name> pairs to rename variables 
     26    <time_units>                is a valid time units string such as "hours since 2003-04-30 10:00:00" 
     27    -n                                  suppresses the time units warning if invalid 
     28    <na_file>                   is the input NASA Ames file path 
     29    <nc_file>                   is the output NetCDF file path 
    2430     
    2531""" 
     
    2733# Imports from python standard library 
    2834import sys 
    29 sys.path.append(r"..") 
    30 import os 
    3135import re 
    32 import time 
    3336 
    3437# Import from nappy package 
    35 import general 
    36 import cdmsMap 
     38import nappy.nc_convertor.na_to_nc 
    3739 
    38 def na2cdms(nafile, ncfile, time_units=None, time_warning="yes", rename_variables={}, rules=None): 
    39     print "Reading data from: %s" % nafile 
    40     file=general.openNAFile(nafile) 
    41     if file.FFI in (2110, 2160, 2310):  
    42         print """\nERROR: Cannot convert NASA Ames File Format Index (FFI) %s to NetCDF.  
    43 No mapping implemented yet.""" % file.FFI 
    44         return 0 
     40def makeDictFromCommaSepString(s): 
     41    """ 
     42    Reads in comma-separated list and converts to dictionary of successive 
     43    keyword,value pairs. 
     44    """ 
     45    if s.count(",") % 2 == 0: 
     46        raise Exception("Must provide even number of items in argument of commas-separated pairs of values: " + s) 
    4547 
    46     print "Writing output NetCDF file: %s\n" % ncfile 
    47     file.toCdmsFile(ncfile, time_units=time_units, time_warning=time_warning, rename_variables=rename_variables) 
     48    d = {} 
     49    items = value.split(",") 
     50    while len(items) > 0: 
     51        d[items[0]] = items[1] 
     52        items = items[2:]  
     53    return d 
     54 
     55def parseArgs(args): 
     56    """ 
     57    Parses arguments and returns dictionary. 
     58    """ 
     59    arg_dict = {} 
     60    a = arg_dict 
     61 
     62    # Set up defaults 
     63    a["na_file"] = None 
     64    a["mode"] = "w" 
     65    a["variables"] = "all" 
     66    a["aux_variables"]  = "all" 
     67    a["global_attributes"] = {} 
     68    a["rename_variables"] = {} 
     69    a["time_units"] = None 
     70    a["time_warning"] = True 
     71    a["nc_file"] = None 
     72 
     73    (arg_list, dummy) = getopt.getopt("i:o:m:v:a:g:t:nr:", args) 
    4874     
    49     print "\nNetCDF file written successfully: %s" % ncfile 
     75    for arg, value in arg_list: 
     76        if arg == "-i": 
     77            a["na_file"] = value 
     78        elif arg == "-o": 
     79            a["nc_file"] = value 
     80        elif arg == "-m": 
     81            a["mode"] = value 
     82        elif arg == "-v": 
     83            a["variables"] = value.split(",") 
     84        elif arg == "-a": 
     85            a["aux_variables"] = value.split(",") 
     86        elif arg == "-g": 
     87            a["global_attributes"] = makeDictFromCommaSepString(value) 
     88        elif arg == "-t": 
     89            a["time_units"] = value 
     90        elif arg == "-n": 
     91            a["time_warning"] = False 
     92        elif arg == "-r": 
     93            a["rename_variables"] = makeDictFromCommaSepString(value) 
    5094 
    51     return 1 
     95    if not a["na_file"]: 
     96        raise Exception("Please provide argument '-i <na_file>'") 
    5297 
    53 if __name__=="__main__": 
     98    if not a["nc_file"]: 
     99        fn = a["na_file"] 
     100        if fn[-3:] == ".na": fn = fn[:-3] 
     101        nc_file = fn + ".nc" 
     102        print "Auto-generating output file name:", nc_file 
     103        a["nc_file"] = nc_file 
     104 
     105    return a 
     106 
     107 
     108def na2nc(args): 
     109    """ 
     110    Controller for conversion of NASA Ames file to NetCDF file. 
     111    """ 
     112    arg_dict = parseArgs(args) 
     113    nc_file = arg_dict["nc_file"] 
     114    del arg_dict["nc_file"] 
     115    mode = arg_dict["mode"] 
     116    del arg_dict["mode"] 
     117 
     118    convertor = apply(nappy.nc_convertor.na_to_nc.NAToNC, [], arg_dict) 
     119    convertor.convert() 
     120    convertor.writeFile(nc_file, mode)     
     121 
     122 
     123if __name__ == "__main__": 
    54124 
    55125    args=sys.argv[1:] 
    56     if len(args)<4: 
    57         print helpMessage 
    58         print "Incorrect number of arguments used." 
    59         sys.exit() 
    60          
    61     time_units=None 
    62     time_warning="yes" 
    63     rename_variables={} 
    64      
    65     for arg in args: 
    66         if arg=="-i": 
    67             infile=args[args.index(arg)+1] 
    68         elif arg=="-o": 
    69             outfile=args[args.index(arg)+1] 
    70         elif arg=="-t": 
    71             time_units=args[args.index(arg)+1] 
    72         elif arg=="-n": 
    73             time_warning="no" 
    74         elif arg=="-r": 
    75             renamer=args[args.index(arg)+1].split("=")       
    76             rename_variables[renamer[0]]=renamer[1] 
    77  
    78     na2cdms(infile, outfile, time_units=time_units, time_warning=time_warning, rename_variables=rename_variables)  
    79  
     126    na2nc(args) 
Note: See TracChangeset for help on using the changeset viewer.