source: nappy/trunk/nappy/script/na2nc.py @ 5146

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

Removed -v and -a args as still buggy.

  • 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>] [-g <global_atts_list>]
16            [-r <rename_vars_list>] [-t <time_units>] [-n]
17            -i <na_file> [-o <nc_file>]
18
19Where
20-----
21
22    <mode>                      is the file mode, either "w" for write or "a" for append
23    <global_atts_list>          is a comma-separated list of global attributes to add
24    <rename_vars_list>          is a comma-separated list of <old_name>,<new_name> pairs to rename variables
25    <time_units>                is a valid time units string such as "hours since 2003-04-30 10:00:00"
26    -n                          suppresses the time units warning if invalid
27    <na_file>                   is the input NASA Ames file path
28    <nc_file>                   is the output NetCDF file path (default is to replace ".na" from NASA Ames
29                                 file with ".nc").
30 
31"""
32
33# Imports from python standard library
34import sys
35import getopt
36
37# Import from nappy package
38import nappy
39from nappy.utils.common_utils import makeListFromCommaSepString, makeDictFromCommaSepString
40
41
42def exitNicely(msg=""):
43    "Exits nicely!"
44    print __doc__
45    if msg != "": print "ERROR:", msg
46    sys.exit()
47
48
49def parseArgs(args):
50    """
51    Parses arguments and returns dictionary.
52    """
53    arg_dict = {}
54    a = arg_dict
55
56    # Set up defaults
57    a["na_file"] = None
58    a["mode"] = "w"
59    a["variables"] = "all"
60    a["aux_variables"]  = "all"
61    a["global_attributes"] = {}
62    a["rename_variables"] = {}
63    a["time_units"] = None
64    a["time_warning"] = True
65    a["nc_file"] = None
66
67    try:
68        (arg_list, dummy) = getopt.getopt(args, "i:o:m:v:a:g:t:nr:")
69    except getopt.GetoptError, e:
70        exitNicely(str(e))
71   
72    for arg, value in arg_list:
73        if arg == "-i":
74            a["na_file"] = value
75        elif arg == "-o":
76            a["nc_file"] = value
77        elif arg == "-m":
78            a["mode"] = value
79        elif arg == "-v":
80            a["variables"] = value.split(",")
81            exitNicely("OPTION NOT IMPLEMENTED: %s : Not yet fully functional. Sorry." % arg)
82        elif arg == "-a":
83            a["aux_variables"] = value.split(",")
84            exitNicely("OPTION NOT IMPLEMENTED: %s : Not yet fully functional. Sorry." % arg)
85        elif arg == "-g":
86            a["global_attributes"] = makeListFromCommaSepString(value)
87        elif arg == "-t":
88            a["time_units"] = value
89        elif arg == "-n":
90            a["time_warning"] = False
91        elif arg == "-r":
92            a["rename_variables"] = makeDictFromCommaSepString(value)
93        else:
94            exitNicely("Argument '" + arg + "' not recognised!")
95
96    if not a["na_file"]:
97        exitNicely("Please provide argument '-i <na_file>'")
98
99    if not a["nc_file"]:
100        fn = a["na_file"]
101        if fn[-3:] == ".na": fn = fn[:-3]
102        nc_file = fn + ".nc"
103        print "Auto-generating output file name:", nc_file
104        a["nc_file"] = nc_file
105
106    return a
107
108
109def na2nc(args=None):
110    """
111    Controller for conversion of NASA Ames file to NetCDF file.
112    """
113
114    if args is None:
115        args = sys.argv[1:]
116   
117    arg_dict = parseArgs(args)
118    na_file = arg_dict["na_file"]
119    del arg_dict["na_file"]
120    nc_file = apply(nappy.convertNAToNC, [na_file], arg_dict)
121
122
123if __name__ == "__main__":
124
125    args=sys.argv[1:]
126    na2nc(args)
Note: See TracBrowser for help on using the repository browser.