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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/bin/na2nc.py@3395
Revision 3395, 3.3 KB checked in by astephen, 13 years ago (diff)
  • 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
30   
31"""
32
33# Imports from python standard library
34import sys
35import re
36
37# Import from nappy package
38import nappy.nc_convertor.na_to_nc
39import nappy.utils.common_utils
40
41def parseArgs(args):
42    """
43    Parses arguments and returns dictionary.
44    """
45    arg_dict = {}
46    a = arg_dict
47
48    # Set up defaults
49    a["na_file"] = None
50    a["mode"] = "w"
51    a["variables"] = "all"
52    a["aux_variables"]  = "all"
53    a["global_attributes"] = {}
54    a["rename_variables"] = {}
55    a["time_units"] = None
56    a["time_warning"] = True
57    a["nc_file"] = None
58
59    (arg_list, dummy) = getopt.getopt(args, "i:o:m:v:a:g:t:nr:")
60   
61    for arg, value in arg_list:
62        if arg == "-i":
63            a["na_file"] = value
64        elif arg == "-o":
65            a["nc_file"] = value
66        elif arg == "-m":
67            a["mode"] = value
68        elif arg == "-v":
69            a["variables"] = value.split(",")
70        elif arg == "-a":
71            a["aux_variables"] = value.split(",")
72        elif arg == "-g":
73            a["global_attributes"] = nappy.utils.makeDictFromCommaSepString(value)
74        elif arg == "-t":
75            a["time_units"] = value
76        elif arg == "-n":
77            a["time_warning"] = False
78        elif arg == "-r":
79            a["rename_variables"] = makeDictFromCommaSepString(value)
80        else:
81            raise Exception("Argument '" + arg + "' not recognised!")
82
83    if not a["na_file"]:
84        raise Exception("Please provide argument '-i <na_file>'")
85
86    if not a["nc_file"]:
87        fn = a["na_file"]
88        if fn[-3:] == ".na": fn = fn[:-3]
89        nc_file = fn + ".nc"
90        print "Auto-generating output file name:", nc_file
91        a["nc_file"] = nc_file
92
93    return a
94
95
96def na2nc(args):
97    """
98    Controller for conversion of NASA Ames file to NetCDF file.
99    """
100    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)   
109
110
111if __name__ == "__main__":
112
113    args=sys.argv[1:]
114    na2nc(args)
Note: See TracBrowser for help on using the repository browser.