source: nappy/trunk/bin/na2nc.py @ 5124

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/bin/na2nc.py@5124
Revision 5124, 3.4 KB checked in by astephen, 11 years ago (diff)

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

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2#   Copyright (C) 2004 CCLRC & NERC( Natural Environment Research Council ).
3#   This software may be distributed under the terms of the
4#   Q Public License, version 1.0 or later. http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
5
6"""
7na2nc.py
8========
9
10Converts a NASA Ames file to a NetCDF file.
11
12Usage
13=====
14
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>]
17
18Where
19-----
20
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 (default is to replace ".na" from NASA Ames
30                                 file with ".nc").
31 
32"""
33
34# Imports from python standard library
35import sys
36import getopt
37
38# Import from nappy package
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
49
50def parseArgs(args):
51    """
52    Parses arguments and returns dictionary.
53    """
54    arg_dict = {}
55    a = arg_dict
56
57    # Set up defaults
58    a["na_file"] = None
59    a["mode"] = "w"
60    a["variables"] = "all"
61    a["aux_variables"]  = "all"
62    a["global_attributes"] = {}
63    a["rename_variables"] = {}
64    a["time_units"] = None
65    a["time_warning"] = True
66    a["nc_file"] = None
67
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))
72   
73    for arg, value in arg_list:
74        if arg == "-i":
75            a["na_file"] = value
76        elif arg == "-o":
77            a["nc_file"] = value
78        elif arg == "-m":
79            a["mode"] = value
80        elif arg == "-v":
81            a["variables"] = value.split(",")
82        elif arg == "-a":
83            a["aux_variables"] = value.split(",")
84        elif arg == "-g":
85            a["global_attributes"] = makeListFromCommaSepString(value)
86        elif arg == "-t":
87            a["time_units"] = value
88        elif arg == "-n":
89            a["time_warning"] = False
90        elif arg == "-r":
91            a["rename_variables"] = makeDictFromCommaSepString(value)
92        else:
93            exitNicely("Argument '" + arg + "' not recognised!")
94
95    if not a["na_file"]:
96        exitNicely("Please provide argument '-i <na_file>'")
97
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    na_file = arg_dict["na_file"]
114    del arg_dict["na_file"]
115    nc_file = apply(nappy.convertNAToNC, [na_file], arg_dict)
116
117
118if __name__ == "__main__":
119
120    args=sys.argv[1:]
121    na2nc(args)
Note: See TracBrowser for help on using the repository browser.