source: nappy/trunk/nappy/nappy_api.py @ 3604

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/nappy/nappy_api.py@3604
Revision 3604, 6.2 KB checked in by astephen, 14 years ago (diff)

Added start of functions for API.

Line 
1"""
2nappy_api.py
3============
4
5Top-level API module that allows user to access most of the useful stuff in
6nappy. Just imports all the good stuff so accessible from here.
7
8What do we want in the API?
9
10
11
12def convertNCToNA(nc_file, na_file=None, var_ids=None, na_items_to_override={},
13            only_return_file_names=False, exclude_vars=[],
14            requested_ffi=None, delimiter="    ", float_format="%g", size_limit=None):
15    """
16    Takes a NetCDF and converts to one or more NASA Ames files. Options are:
17    <nc_file>                           - name of input file (NetCDF).
18    <na_file>                           - name of output file (NASA Ames or CSV) - will be used as base name if multiple files.
19    <var_list>                  - a comma-separated list of variables (i.e. var ids) to include in the output file(s).
20    <ffi>                                       - NASA Ames File Format Index (FFI) to write to (normally automatic).
21    <float_format>          - a python formatting string such as %s, %g or %5.2f
22    <delimiter>                         - the delimiter you wish to use between data items in the output file such as "   " or "
23\t"
24    <limit_ffi_1001_rows>       - if format FFI is 1001 then chop files up into <limitFFI1001Rows> rows of data.
25    <exclude_vars>          - a comma-separated list of variables (i.e. var ids) to exclude in the output file(s).
26    <key1>,<value1>[,<key2>,<value2>[...]] - list of comma-separated key,value pairs to overwrite in output files:
27                                                                * Typically the keys are in:
28                                   * "DATE", "RDATE", "ANAME", "MNAME","ONAME", "ORG", "SNAME", "VNAME".
29        --names-only                    - only display a list of file names that would be written (i.e. don't convert actual fil
30es).
31    """
32    convertor = apply(nappy.nc_convertor.nc_to_na.NCToNA, [], arg_dict)
33    convertor.convert()
34
35    # If user only wants files then only give them that
36    if arg_dict["only_return_file_names"] == True:
37        print convertor.constructNAFileNames(na_file)
38    else:
39        convertor.writeNAFiles(na_file, mode)
40        print convertor.output_message
41
42    print "Should NCToNA return a list of the na file names produced?"
43    return True   
44   
45def convertNCToCSV(nc_file, csv_file=None, **kwargs):
46    """
47    Reads in a NetCDF file and writes the data out to a CSV file following the
48    NASA Ames standard.
49    """
50    if not csv_file:
51        nc_base = nc_file
52        if nc_file[-3:] == ".nc":
53            nc_base = nc_file[:-3]
54       
55        csv_file = nc_base + ".csv"
56        kwargs["na_file"] = csv_file
57        kwargs["delimiter"] = ","
58   
59    return apply(convertNCToNA, [nc_file], kwargs)
60   
61def convertNAToNC(na_file, nc_file=None, mode="w", variables=None, aux_variables=None,
62                 global_attributes={"Conventions":"CF-1.0"},
63                 time_units=None, time_warning=True,
64                 rename_variables={}):
65    """
66    Takes a NASA Ames file and converts to a NetCDF file. Options are:
67
68na2nc.py [-m <mode>] [-v <var_list>] [-a <aux_var_list>] [-g <global_atts_list>]
69            [-r <rename_vars_list>] [-t <time_units>] [-n] -i <na_file> [-o <nc_file>]
70    <mode>                      is the file mode, either "w" for write or "a" for append
71    <var_list>                  is a comma-separated list of variable names to include
72    <aux_var_list>              is a comma-separated list of auxiliary variables names to include
73    <global_atts_list>  is a comma-separated list of global attributes to add
74    <rename_vars_list>  is a comma-separated list of <old_name>,<new_name> pairs to rename variables
75    <time_units>                is a valid time units string such as "hours since 2003-04-30 10:00:00
76"
77    -n                                  suppresses the time units warning if invalid
78    <na_file>                   is the input NASA Ames file path
79    <nc_file>                   is the output NetCDF file path
80na_file, variables=None, aux_variables=None,
81                 global_attributes={"Conventions":"CF-1.0"},
82                 time_units=None, time_warning=True,
83                 rename_variables={}):
84    """
85    convertor = apply(nappy.nc_convertor.na_to_nc.NAToNC, [], arg_dict)
86    convertor.convert()
87    convertor.writeNCFile(nc_file, mode)
88    print "SHould this return nc file path?"
89    return True   
90
91
92def convertNAToCSV(...):
93    """
94    Reads in a NASA Ames file and writes it out again with commas as the delimiter.
95    """
96    fin = openNAFile....
97    na_dict = fin.na_dict...
98    fout = openNAFile(....
99    fout.write(delimiter=",")
100    fout.close()
101    return True
102
103
104compareNAFiles - do it
105  Check comparing 2001.na and 2001.csv and see why it falls over.
106
107convertCDMSObjectsToNA
108convertCDMSObjectsToCSV
109convertNADictToNC
110readCDMSObjectsFromNA - returns cdsms var list and global atts
111
112some things from utils...
113
114getMappings
115getVersion use __version__
116getFFI
117chooseFFI
118 have a look at what is needed
119
120
121Some docs here on usage...
122>>> import nappy  (via nappy_api.py module)
123>>> nappy.convertNAToNC(na_file, nc_file)
124>>> nappy.convertNCToNA(nc_file, na_file)
125
126
127"""
128
129# Import standard library modules
130
131
132# Import local modules
133from nappy.utils.common_utils import *
134
135def openNAFile(filename, mode="r", na_dict=None, delimiter="    ", float_format="%g"):
136    """
137    Function wrapper around the NASA Ames File classes. Any NASA Ames
138    file can be opened through this function and the appropriate read or
139    write NASA Ames File class instance is returned.
140    """
141    if mode == "r":
142        ffi = readFFI(filename)
143        return apply(getNAFileClass(ffi), (filename, mode))
144
145    elif mode == "w":
146        if na_dict.has_key('FFI') and type(na_dict['FFI']) == type(3):
147            ffi = na_dict['FFI']
148        else:
149            ffi = chooseFFI(na_dict)
150            na_dict['FFI'] = ffi
151            print "\nFormat identified as:", ffi   
152        return apply(getNAFileClass(ffi), (filename,), {"mode":mode, "na_dict":na_dict}) #, "delimiter":delimiter, "float_format":float_format})
153    else:
154        raise Exception("File mode not recognised '" + mode + "'.")
Note: See TracBrowser for help on using the repository browser.