Changeset 5124 for nappy


Ignore:
Timestamp:
20/03/09 15:06:09 (10 years ago)
Author:
astephen
Message:

Fixed some of na2nc.py and added list of cmd_line tests for now. Some don't work

Location:
nappy/trunk/bin
Files:
1 added
1 edited

Legend:

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

    r3395 r5124  
    1919----- 
    2020 
    21     <mode>                              is the file mode, either "w" for write or "a" for append 
     21    <mode>                      is the file mode, either "w" for write or "a" for append 
    2222    <var_list>                  is a comma-separated list of variable names to include 
    2323    <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 
     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 
    2626    <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 
     27    -n                          suppresses the time units warning if invalid 
    2828    <na_file>                   is the input NASA Ames file path 
    29     <nc_file>                   is the output NetCDF file path 
    30      
     29    <nc_file>                   is the output NetCDF file path (default is to replace ".na" from NASA Ames 
     30                                 file with ".nc"). 
     31  
    3132""" 
    3233 
    3334# Imports from python standard library 
    3435import sys 
    35 import re 
     36import getopt 
    3637 
    3738# Import from nappy package 
    38 import nappy.nc_convertor.na_to_nc 
    39 import nappy.utils.common_utils 
     39import nappy 
     40from nappy.utils.common_utils import makeListFromCommaSepString, makeDictFromCommaSepString 
     41 
     42 
     43def exitNicely(msg=""): 
     44    "Exits nicely!" 
     45    print __doc__ 
     46    if msg != "": print "ERROR:", msg 
     47    sys.exit() 
     48 
    4049 
    4150def parseArgs(args): 
     
    5766    a["nc_file"] = None 
    5867 
    59     (arg_list, dummy) = getopt.getopt(args, "i:o:m:v:a:g:t:nr:") 
     68    try: 
     69        (arg_list, dummy) = getopt.getopt(args, "i:o:m:v:a:g:t:nr:") 
     70    except getopt.GetoptError, e: 
     71        exitNicely(str(e)) 
    6072     
    6173    for arg, value in arg_list: 
     
    7183            a["aux_variables"] = value.split(",") 
    7284        elif arg == "-g": 
    73             a["global_attributes"] = nappy.utils.makeDictFromCommaSepString(value) 
     85            a["global_attributes"] = makeListFromCommaSepString(value) 
    7486        elif arg == "-t": 
    7587            a["time_units"] = value 
     
    7991            a["rename_variables"] = makeDictFromCommaSepString(value) 
    8092        else: 
    81             raise Exception("Argument '" + arg + "' not recognised!") 
     93            exitNicely("Argument '" + arg + "' not recognised!") 
    8294 
    8395    if not a["na_file"]: 
    84         raise Exception("Please provide argument '-i <na_file>'") 
     96        exitNicely("Please provide argument '-i <na_file>'") 
    8597 
    8698    if not a["nc_file"]: 
     
    99111    """ 
    100112    arg_dict = parseArgs(args) 
    101     nc_file = arg_dict["nc_file"] 
    102     del arg_dict["nc_file"] 
    103     mode = arg_dict["mode"] 
    104     del arg_dict["mode"] 
    105  
    106     convertor = apply(nappy.nc_convertor.na_to_nc.NAToNC, [], arg_dict) 
    107     convertor.convert() 
    108     convertor.writeNCFile(nc_file, mode)     
     113    na_file = arg_dict["na_file"] 
     114    del arg_dict["na_file"] 
     115    nc_file = apply(nappy.convertNAToNC, [na_file], arg_dict) 
    109116 
    110117 
Note: See TracChangeset for help on using the changeset viewer.