Changeset 5156


Ignore:
Timestamp:
31/03/09 20:35:58 (10 years ago)
Author:
astephen
Message:

More tests on command line scripts.

Location:
nappy/trunk
Files:
1 added
1 deleted
3 edited

Legend:

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

    r5139 r5156  
    66""" 
    77 
    8 nc2na.py 
    9 ======== 
     8nc2csv.py 
     9========= 
    1010 
    1111Converts a NetCDF file into one or more NASA Ames file. 
     
    1414===== 
    1515 
    16     nc2na.py [-v <var_list>] [--ffi=<ffi>] [-f <float_format>]  
    17              [-s <delimiter>] [-l <limit_ffi_1001_rows>]  
     16    nc2csv.py [-v <var_list>] [--ffi=<ffi>] [-f <float_format>]  
     17             [-l <limit_ffi_1001_rows>]  
    1818             [-e <exclude_vars>] [--overwrite-metadata=<key1>,<value1>[,<key2>,<value2>[...]]]  
    19              [--names-only] 
    20              -i <nc_file> [-o <na_file>]  
     19             [--names-only] [--no-header] [--annotated] 
     20             -i <nc_file> [-o <csv_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    <csv_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 
    3029    <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). 
     30    <exclude_vars>              - a comma-separated list of variables (i.e. var ids) to exclude in the output file(s). 
    3231    <key1>,<value1>[,<key2>,<value2>[...]] - list of comma-separated key,value pairs to overwrite in output files: 
    3332                                                                * Typically the keys are in:  
    3433                                   * "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). 
     34    --names-only                - only display a list of file names that would be written (i.e. don't convert actual files). 
     35    --no-header                 - Do not write NASA Ames header 
     36    --annotated                 - add annotation column in first column 
    3637     
    3738""" 
     
    4142 
    4243# Import from nappy package 
    43 import nappy 
    44 import nappy.utils.common_utils 
    45  
    46  
    47 def parseArgs(args): 
    48     """ 
    49     Parses arguments and returns dictionary. 
    50     """ 
    51     arg_dict = {} 
    52     a = arg_dict 
    53  
    54     # Set up defaults 
    55     a["nc_file"] = None 
    56     a["var_list"] = None 
    57     a["ffi"] = None 
    58     a["float_format"] = None 
    59     a["delimiter"] = None 
    60     a["size_limit"] = None 
    61     a["exclude_vars"] = [] 
    62     a["na_items_to_override"] = None 
    63     a["only_return_file_names"] = False 
    64     a["na_file"] = None 
    65  
    66     (arg_list, dummy) = getopt.getopt(args, "i:o:v:f:s:l:e:",  
    67                               ["--ffi=", "--overwrite-metadata=", "--names-only"]) 
    68      
    69     for arg, value in arg_list: 
    70         if arg == "-i": 
    71             a["nc_file"] = value 
    72         elif arg == "-o": 
    73             a["na_file"] = value 
    74         elif arg == "-v": 
    75             a["var_list"] = value.split(",") 
    76         elif arg == "--ffi": 
    77             a["ffi"] = int(value) 
    78         elif arg == "-f": 
    79             a["float_format"] = value 
    80         elif arg == "-s": 
    81             a["delimiter"] = value 
    82         elif arg == "--limit_ffi_1001_rows": 
    83             a["size_limit"] = long(value) 
    84         elif arg == "-e": 
    85             a["exclude_vars"] = value.split(",") 
    86         elif arg == "--overwrite-metadata": 
    87             a["na_items_to_override"] = makeDictFromCommaSepString(value) 
    88         elif arg == "--names-only": 
    89             a["only_return_file_names"] = True 
    90         else: 
    91             raise Exception("Argument '" + arg + "' not recognised!") 
    92  
    93     if not a["nc_file"]: 
    94         raise Exception("Please provide argument '-i <nc_file>'") 
    95  
    96     return a 
    97  
    98  
    99 def nc2na(args): 
    100     """ 
    101     Controller for conversion of NetCDF file to NASA Ames files. 
    102     """ 
    103     arg_dict = parseArgs(args) 
    104     na_file = arg_dict.get("na_file", None) 
    105  
    106     convertor = apply(nappy.nc_convertor.nc_to_na.NCToNA, [], arg_dict) 
    107     convertor.convert() 
    108  
    109     # If user only wants files then only give them that 
    110     if arg_dict["only_return_file_names"] == True: 
    111         print convertor.constructNAFileNames(na_file) 
    112     else: 
    113         convertor.writeNAFiles(na_file, mode)     
    114         print convertor.output_message 
     44from nc2na import * 
    11545 
    11646 
    11747if __name__ == "__main__": 
    11848 
    119     args=sys.argv[1:] 
     49    args = sys.argv[1:] 
     50    if "-d" in args: 
     51        exitNicely("nc2csv.py does not accept the -d argument as the delimiter is fixed as a comma, try using nc2na.py instead.") 
     52 
     53    args.extend(["-d", ","]) 
    12054    nc2na(args) 
  • nappy/trunk/nappy/script/nc2na.py

    r5149 r5156  
    1515 
    1616    nc2na.py [-v <var_list>] [--ffi=<ffi>] [-f <float_format>]  
    17              [-s <delimiter>] [-l <limit_ffi_1001_rows>]  
     17             [-d <delimiter>] [-l <limit_ffi_1001_rows>]  
    1818             [-e <exclude_vars>] [--overwrite-metadata=<key1>,<value1>[,<key2>,<value2>[...]]]  
    1919             [--names-only] [--no-header] [--annotated] 
     
    7777 
    7878    try: 
    79         (arg_list, dummy) = getopt.getopt(args, "i:o:v:f:s:l:e:",  
     79        (arg_list, dummy) = getopt.getopt(args, "i:o:v:f:d:l:e:",  
    8080                              ["ffi=", "overwrite-metadata=", "names-only", 
    8181                               "no-header", "annotated"]) 
     
    9494        elif arg == "-f": 
    9595            a["float_format"] = value 
    96         elif arg == "-s": 
     96        elif arg == "-d": 
    9797            a["delimiter"] = value 
    9898        elif arg == "--limit_ffi_1001_rows": 
     
    145145if __name__ == "__main__": 
    146146 
    147     args=sys.argv[1:] 
     147    args = sys.argv[1:] 
    148148    nc2na(args) 
  • nappy/trunk/tests/tests-nc2na.py.txt

    r5150 r5156  
    33nc2na.py -i ../../test_outputs/1010.nc -o ../../test_outputs/1010-from-nc.na 
    44nc2na.py -i ../../test_outputs/2010.nc -o ../../test_outputs/2010-from-nc.na 
     5nc2na.py -i ../../test_outputs/2110.nc -o ../../test_outputs/2110-from-nc.na 
    56nc2na.py -i ../../test_outputs/3010.nc -o ../../test_outputs/3010-from-nc.na 
    67nc2na.py -i ../../test_outputs/4010.nc -o ../../test_outputs/4010-from-nc.na 
    78 
     9nc2na.py -i ../../test_outputs/1001.nc -o ../../test_outputs/1001-from-nc-includes.na -v "ascent_rate,pressure" 
    810nc2na.py -i ../../test_outputs/1001.nc -o ../../test_outputs/1001-from-nc-excludes.na -e "Ascent Rate" 
    911nc2na.py -i ../../test_outputs/1001.nc -o ../../test_outputs/1001-from-nc-overwrite.na --overwrite-metadata=ONAME,"See line 2 - my favourite org." 
    1012nc2na.py -i ../../test_outputs/1001.nc -o ../../test_outputs/1001-from-nc-as-padded-ints.na -f "%03d" 
    11 nc2na.py -i ../../test_outputs/1001.nc -o ../../test_outputs/1001-from-nc-csv.na -s , 
     13nc2na.py -i ../../test_outputs/1001.nc -o ../../test_outputs/1001-from-nc-csv.na -d , 
    1214nc2na.py -i ../../test_outputs/1001.nc -o ../../test_outputs/1001-from-nc-no-header.na --no-header 
    1315nc2na.py -i ../../test_outputs/1001.nc -o ../../test_outputs/1001-from-nc-annotated.na --annotated 
    1416nc2na.py -i ../../test_outputs/1001.nc --names-only 
    1517 
    16 #    nc2na.py [-v <var_list>] [--ffi=<ffi>] [-f <float_format>] 
    17 #             [-s <delimiter>] [-l <limit_ffi_1001_rows>] 
    18 #             [-e <exclude_vars>] [--overwrite-metadata=<key1>,<value1>[,<key2>,<value2>[...]]] 
    19 #             [--names-only] 
    20 #             -i <nc_file> [-o <na_file>] 
     18nc2na.py -i ../../test_outputs/1001.nc -o ../../test_outputs/1001-from-nc-tab-delim-2dps.na -d "\t" -f "%.2f" 
     19nc2na.py -i ../../test_outputs/1001.nc -o ../../test_outputs/1001-from-nc-tab-delim-2dps-annot.na -d "\t" -f "%.2f" --annotated 
     20nc2na.py -i ../../test_outputs/1001.nc -o ../../test_outputs/1001-from-nc-tab-delim-2dps-annot-nohead.na -d "\t" -f "%.2f" --annotated --no-header 
     21nc2na.py -i ../../test_outputs/1001.nc -o ../../test_outputs/1001-from-nc-tab-delim-2dps-annot-nohead-overwrite.na -d "\t" -f "%.2f" --annotated --no-header --overwrite-metadata=ONAME,i_was_oname,ORG,i_was_org 
     22nc2na.py -i ../../test_outputs/1001.nc -o ../../test_outputs/1001-from-nc-tab-delim-2dps-annot-nohead-overwrite-exclude.na -d "\t" -f "%.2f" --annotated --no-header --overwrite-metadata=ONAME,i_was_oname,ORG,i_was_org -e "Ascent Rate" 
    2123 
     24nc2na.py -i ../../test_outputs/2010.nc -o ../../test_outputs/2110-from-nc-2010.na --ffi=2110 
     25nc2na.py -i ../../test_outputs/2010.nc -o ../../test_outputs/2310-from-nc-2010.na --ffi=2310 
     26nc2na.py -i ../../test_outputs/1010.nc -o ../../test_outputs/1020-from-nc-1010.na --ffi=1020 
     27 
     28echo "WHY DOES -e NOT WORK?? WHY DOES no-header not work, what about -v???" 
     29 
Note: See TracChangeset for help on using the changeset viewer.