Changeset 5149


Ignore:
Timestamp:
30/03/09 21:02:51 (10 years ago)
Author:
astephen
Message:

Fixing scripts.

Location:
nappy/trunk/nappy/script
Files:
2 edited

Legend:

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

    r5146 r5149  
    123123if __name__ == "__main__": 
    124124 
     125    print nappy.__file__ 
    125126    args=sys.argv[1:] 
    126127    na2nc(args) 
  • nappy/trunk/nappy/script/nc2na.py

    r5139 r5149  
    1717             [-s <delimiter>] [-l <limit_ffi_1001_rows>]  
    1818             [-e <exclude_vars>] [--overwrite-metadata=<key1>,<value1>[,<key2>,<value2>[...]]]  
    19              [--names-only] 
     19             [--names-only] [--no-header] [--annotated] 
    2020             -i <nc_file> [-o <na_file>]  
    2121Where 
    2222----- 
    2323 
    24     <nc_file>                           - name of input file (NetCDF). 
    25     <na_file>                           - name of output file (NASA Ames or CSV) - will be used as base name if multiple files. 
     24    <nc_file>                   - name of input file (NetCDF). 
     25    <na_file>                   - name of output file (NASA Ames or CSV) - will be used as base name if multiple files. 
    2626    <var_list>                  - a comma-separated list of variables (i.e. var ids) to include in the output file(s). 
    27     <ffi>                                       - NASA Ames File Format Index (FFI) to write to (normally automatic).   
    28     <float_format>          - a python formatting string such as %s, %g or %5.2f 
    29     <delimiter>                         - the delimiter you wish to use between data items in the output file such as "   " or "\t" 
     27    <ffi>                       - NASA Ames File Format Index (FFI) to write to (normally automatic).   
     28    <float_format>              - a python formatting string such as %s, %g or %5.2f 
     29    <delimiter>                 - the delimiter you wish to use between data items in the output file such as "   " or "\t" 
    3030    <limit_ffi_1001_rows>       - if format FFI is 1001 then chop files up into <limitFFI1001Rows> rows of data.   
    31     <exclude_vars>          - a comma-separated list of variables (i.e. var ids) to exclude in the output file(s). 
     31    <exclude_vars>              - a comma-separated list of variables (i.e. var ids) to exclude in the output file(s). 
    3232    <key1>,<value1>[,<key2>,<value2>[...]] - list of comma-separated key,value pairs to overwrite in output files: 
    3333                                                                * Typically the keys are in:  
    3434                                   * "DATE", "RDATE", "ANAME", "MNAME","ONAME", "ORG", "SNAME", "VNAME". 
    35         --names-only                    - only display a list of file names that would be written (i.e. don't convert actual files). 
     35    --names-only                - only display a list of file names that would be written (i.e. don't convert actual files). 
     36    --no-header                 - Do not write NASA Ames header 
     37    --annotated                 - add annotation column in first column 
    3638     
    3739""" 
     
    3941# Imports from python standard library 
    4042import sys 
     43import getopt 
    4144 
    4245# Import from nappy package 
    4346import nappy 
    44 import nappy.utils.common_utils 
     47from nappy.utils.common_utils import makeListFromCommaSepString, makeDictFromCommaSepString 
     48 
     49 
     50def exitNicely(msg=""): 
     51    "Exits nicely!" 
     52    print __doc__ 
     53    if msg != "": print "ERROR:", msg 
     54    sys.exit() 
    4555 
    4656 
     
    5464    # Set up defaults 
    5565    a["nc_file"] = None 
    56     a["var_list"] = None 
    57     a["ffi"] = None 
    58     a["float_format"] = None 
    59     a["delimiter"] = None 
     66    a["var_ids"] = None 
     67    a["requested_ffi"] = None 
     68    a["float_format"] = nappy.default_float_format 
     69    a["delimiter"] = nappy.default_delimiter 
    6070    a["size_limit"] = None 
    6171    a["exclude_vars"] = [] 
    62     a["na_items_to_override"] = None 
     72    a["na_items_to_override"] = {} 
    6373    a["only_return_file_names"] = False 
    6474    a["na_file"] = None 
     75    a["no_header"] = False 
     76    a["annotation"] = False 
    6577 
    66     (arg_list, dummy) = getopt.getopt(args, "i:o:v:f:s:l:e:",  
    67                               ["--ffi=", "--overwrite-metadata=", "--names-only"]) 
    68      
     78    try: 
     79        (arg_list, dummy) = getopt.getopt(args, "i:o:v:f:s:l:e:",  
     80                              ["ffi=", "overwrite-metadata=", "names-only", 
     81                               "no-header", "annotated"]) 
     82    except getopt.GetoptError, e: 
     83        exitNicely(str(e)) 
     84 
    6985    for arg, value in arg_list: 
    7086        if arg == "-i": 
     
    7389            a["na_file"] = value 
    7490        elif arg == "-v": 
    75             a["var_list"] = value.split(",") 
     91            a["var_ids"] = value.split(",") 
    7692        elif arg == "--ffi": 
    77             a["ffi"] = int(value) 
     93            a["requested_ffi"] = int(value) 
    7894        elif arg == "-f": 
    7995            a["float_format"] = value 
     
    88104        elif arg == "--names-only": 
    89105            a["only_return_file_names"] = True 
     106        elif arg == "--no-header": 
     107            a["no_header"] = True 
     108        elif arg == "--annotated": 
     109            a["annotation"] = True 
    90110        else: 
    91             raise Exception("Argument '" + arg + "' not recognised!") 
     111            exitNicely("Argument '" + arg + "' not recognised!") 
    92112 
    93113    if not a["nc_file"]: 
    94         raise Exception("Please provide argument '-i <nc_file>'") 
     114        exitNicely("Please provide argument '-i <nc_file>'") 
    95115 
    96116    return a 
     
    104124    if args is None: 
    105125        args = sys.argv[1:] 
    106      
     126 
    107127    arg_dict = parseArgs(args) 
    108     na_file = arg_dict.get("na_file", None) 
    109  
    110     convertor = apply(nappy.nc_convertor.nc_to_na.NCToNA, [], arg_dict) 
    111     convertor.convert() 
     128    nc_file = arg_dict["nc_file"] 
     129    del arg_dict["nc_file"] 
     130    print arg_dict 
     131    na_files = apply(nappy.convertNCToNA, [nc_file], arg_dict) 
    112132 
    113133    # If user only wants files then only give them that 
    114134    if arg_dict["only_return_file_names"] == True: 
    115         print convertor.constructNAFileNames(na_file) 
     135        print "\nExpected file names would be:" 
     136        for naf in na_files: 
     137            print "    ", naf 
     138 
    116139    else: 
    117         convertor.writeNAFiles(na_file, mode)     
    118         print convertor.output_message 
     140        print "\nSuccessfully wrote: "  
     141        for naf in na_files: 
     142            print "    ", naf 
    119143 
    120144 
Note: See TracChangeset for help on using the changeset viewer.