source: nappy/branches/nappy-eggable/nappy/script/nc2na.py @ 5069

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/branches/nappy-eggable/nappy/script/nc2na.py@5069
Revision 5069, 4.0 KB checked in by spascoe, 12 years ago (diff)

Renamed unit_tests directory so it's auto-discovered with "nosetests
nappy".

Copied scripts into package heirarchy.

  • Property svn:executable set to *
RevLine 
[3314]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
[3394]6"""
[3314]7
8nc2na.py
9========
10
[3394]11Converts a NetCDF file into one or more NASA Ames file.
[3314]12
13Usage
14=====
15
[3394]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>]
[3314]21Where
22-----
23
[3394]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).
[3314]36   
37"""
38
39# Imports from python standard library
[3394]40import sys
[3314]41
[3394]42# Import from nappy package
43import nappy.nc_convertor.nc_to_na
44import nappy.utils.common_utils
[3314]45
46
[3394]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"])
[3314]68   
[3394]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!")
[3314]92
[3394]93    if not a["nc_file"]:
94        raise Exception("Please provide argument '-i <nc_file>'")
[3314]95
[3394]96    return a
[3314]97
[3394]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__":
118
119    args=sys.argv[1:]
120    nc2na(args)
Note: See TracBrowser for help on using the repository browser.