Changeset 3394 for nappy


Ignore:
Timestamp:
12/02/08 18:45:38 (12 years ago)
Author:
astephen
Message:
 
File:
1 edited

Legend:

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

    r3383 r3394  
    44#   Q Public License, version 1.0 or later. http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt 
    55 
    6 helpMessage=""" 
     6""" 
    77 
    88nc2na.py 
    99======== 
    1010 
    11 Converts a NetCDF file into one a NASA Ames file. 
     11Converts a NetCDF file into one or more NASA Ames file. 
    1212 
    1313Usage 
    1414===== 
    1515 
    16     nc2na [-v <var_list>] [-r <rulename>[,<ruletype>[,<ra1>[,<ra2>[,<ra3>]]]]]] [-j] [-f <float_format>]  
    17               [-s <delimiter>] [-d <DATE>] [-l <limitFFI1001Rows>]  
    18               [-e <ex_var_list>] [<overwrite_global_metadata>] -i <infilename> -o <outfilename> 
    19  
     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>]  
    2021Where 
    2122----- 
    2223 
    23     infilename  - name of input file (NetCDF). 
    24     outfilename - name of output file (NASA Ames). 
    25     varlist     - a comma separated list of variables to include in the output file. 
    26     -r          - provide rule arguments for calling additional routines held in nappy/rules sub-package. 
    27                   example call would be "-r aircraft,av,flag,1,0,1" telling nappy to call rules/aircraftData.py 
    28                   and interpret the rest of the arguments when called (in this case sub-sampling by averaging. 
    29                   Or "-r aircraft,ss,flag,1,0,1" to sub-sample by selection using the flag variable at 
    30                   a sampling rate of 1Hz using valid flags of 0 and 1 only. 
    31     limitFFI1001Rows - if format FFI is 1001 then chop files up into <limitFFI1001Rows> rows of data. 
    32     -j          - just display file names that would be produced - without creating files. 
    33     -s          - resample data to per-second for aircraft data.      
    34     floatformat - a python formatting string such as %s or %5.2f 
    35     delimiter   - the delimiter you wish to use between data items in the output file such as "   " or "\t" 
    36     DATE    - to replace first valid Date of data ("DATE") in the file.  
    37     overwrite_global_metadata           -  one of "DATE", "RDATE", "ANAME", "MNAME", 
    38            "ONAME", "ORG", "SNAME", "VNAME" followed by "=<overwrite_value>". 
     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. 
     26    <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" 
     30    <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). 
     32    <key1>,<value1>[,<key2>,<value2>[...]] - list of comma-separated key,value pairs to overwrite in output files: 
     33                                                                * Typically the keys are in:  
     34                                   * "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). 
    3936     
    4037""" 
    4138 
    4239# Imports from python standard library 
    43 import sys, os, time, string, fileinput, re 
    44 sys.path.append("..") 
    45 from cdms2na import * 
     40import sys 
    4641 
    47 if __name__=="__main__": 
     42# Import from nappy package 
     43import nappy.nc_convertor.nc_to_na 
     44import nappy.utils.common_utils 
     45 
     46 
     47def 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"] = nappy.utils.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 
     99def 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 
     115 
     116 
     117if __name__ == "__main__": 
    48118 
    49119    args=sys.argv[1:] 
    50  
    51     allowedOverwriteMetadata=["DATE",  "RDATE", "ANAME", "MNAME", 
    52            "ONAME", "ORG", "SNAME", "VNAME"]     
    53     variables=None 
    54      
    55     if len(args)<4: 
    56         print helpMessage 
    57         print "Incorrect number of arguments used." 
    58         sys.exit() 
    59  
    60     nFilesOnly="no" 
    61     rule=None 
    62     delimiter="    " 
    63     sizeLimit=None 
    64     float_format="%8.3f"         
    65     naVars={} 
    66  
    67     for arg in args: 
    68         if arg=="-i": 
    69             infile=args[args.index(arg)+1] 
    70         elif arg=="-o": 
    71             outfile=args[args.index(arg)+1] 
    72         elif arg=="-f": 
    73             float_format=args[args.index(arg)+1] 
    74         elif arg=="-j": 
    75             nFilesOnly="yes" 
    76         elif arg=="-r": 
    77             rule=args[args.index(arg)+1].split(",") 
    78         elif arg=="-s": 
    79             delimiter=args[args.index(arg)+1]        
    80         elif arg=="-d": 
    81             naVars["DATE"]=args[args.index(arg)+1]           
    82         elif arg=="-l": 
    83             sizeLimit=long(args[args.index(arg)+1]) 
    84         elif arg[0]=="-" and arg[1:] in allowedOverwriteMetadata: 
    85             naVars[arg[1:]]=args[args.index(arg)+1] 
    86         elif arg in ("-v", "-vars", "-var", "-variables"): 
    87             variables=args[args.index(arg)+1].split(",") 
    88  
    89     rtvalue=cdms2na(infile, outfile, naVars, variables, nFilesOnly, sizeLimit=sizeLimit, rule=rule, delimiter=delimiter,  
    90 float_format=float_format)  
    91     if nFilesOnly=="yes": 
    92         print "\n\nOutput files would be:" 
    93         for file in rtvalue: 
    94             print file 
     120    nc2na(args) 
Note: See TracChangeset for help on using the changeset viewer.