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

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

First major re-factor.

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