source: nappy/trunk/nappy/bin/na2nc @ 3314

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

Re-factored under a nappy directory for a start.

  • 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"""
7na2cdms.py
8==========
9
10Module holds the na2cdms function to convert a NASA Ames file to a CDMS
11file (NetCDF).
12
13"""
14
15helpMessage="""
16
17na2nc
18=====
19
20Converts a NASA Ames file to a NetCDF file.
21
22Usage
23=====
24
25   na2nc.py [<options>] -i <infilename> -o <outfilename>
26
27Where
28-----
29
30    infilename  - name of input file (NASA Ames).
31    outfilename - name of output file (NetCDF).
32    options     - list of options:
33                    -t <time_units_string>  (such as "hours since 2003-04-30 10:00:00")
34   
35"""
36
37# Imports from python standard library
38import sys
39sys.path.append(r"..")
40import os
41import re
42import time
43
44# Import from nappy package
45import general
46import cdmsMap
47
48def na2cdms(nafile, ncfile, time_units=None, time_warning="yes", rename_variables={}, rules=None):
49    print "Reading data from: %s" % nafile
50    file=general.openNAFile(nafile)
51    if file.FFI in (2110, 2160, 2310): 
52        print """\nERROR: Cannot convert NASA Ames File Format Index (FFI) %s to NetCDF.
53No mapping implemented yet.""" % file.FFI
54        return 0
55
56    print "Writing output NetCDF file: %s\n" % ncfile
57    file.toCdmsFile(ncfile, time_units=time_units, time_warning=time_warning, rename_variables=rename_variables)
58   
59    print "\nNetCDF file written successfully: %s" % ncfile
60
61    return 1
62
63if __name__=="__main__":
64
65    args=sys.argv[1:]
66    if len(args)<4:
67        print helpMessage
68        print "Incorrect number of arguments used."
69        sys.exit()
70       
71    time_units=None
72    time_warning="yes"
73    rename_variables={}
74   
75    for arg in args:
76        if arg=="-i":
77            infile=args[args.index(arg)+1]
78        elif arg=="-o":
79            outfile=args[args.index(arg)+1]
80        elif arg=="-t":
81            time_units=args[args.index(arg)+1]
82        elif arg=="-n":
83            time_warning="no"
84        elif arg=="-r":
85            renamer=args[args.index(arg)+1].split("=")     
86            rename_variables[renamer[0]]=renamer[1]
87
88    na2cdms(infile, outfile, time_units=time_units, time_warning=time_warning, rename_variables=rename_variables) 
89
Note: See TracBrowser for help on using the repository browser.