source: TI02-CSML/trunk/Scanner/csmlscan.py @ 919

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/Scanner/csmlscan.py@919
Revision 919, 6.0 KB checked in by domlowe, 14 years ago (diff)

improvments to PointSeriesFeature? & NasaAmes? handling

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#!/usr/bin/env python
2
3#**************************************************************************************
4#cd2csml.py
5#read netcdf/pp files through cdunif/cdat and produce CSML documents
6#V0.56 Dominic Lowe, BADC 06th December 2005
7#V0.55 Dominic Lowe, BADC  08th November 2005
8#V0.5 Dominic Lowe, BADC  27th October 2005
9#
10#**************************************************************************************
11
12import sys, string
13import xml.dom.ext
14import getopt
15import os
16#CSML modules
17import csmllibs
18
19
20
21usage = """Usage:
22    Coapec2CSML [options] <files>
23
24    Scan a list of files (netCDF/PP/GRIB) producing a CSML file for those files
25    (as a single CSML Dataset)
26
27Arguments:
28
29    <files> is a list of file paths to scan. The files can be listed in any order,
30    and may be in multiple directories.
31    These files will be marked up as a single CSML Dataset.
32    Please note that use of the 'directory' option will override any file arguments here.
33       
34Options:
35
36    -d directory: All netcdf files in the directory are marked up in a single CSML file
37                       
38               
39    -c csmlfeaturetype: The csmlFeatureType of the data. Only GridSeriesProfile is
40                currently implemented. Other feature types are PointFeature
41                PointSeriesFeature ProfileFeature ProfileSeriesFeature and GridFeature.
42                                               
43     -t timedimension:  The default time dimension is 't'. If your files use a
44                different variable name you may supply it with this option.
45
46    -r recursive:       if this option is provided along with the -d option, all
47                netcdf files in subdirectories of the -d directory are also processed.
48                If it is not provided, then only the netcdf files in the named directory
49                are processed.
50
51    -o outputfile:      Provide the name of the output file, default is csmloutput.xml
52                                which can be found in the directory from which the program is run
53        -h help:                Shows this help information.
54       
55       
56Example:
57       
58        python Coapec2csml.py -d /home/users/me/mynetcdffiles/ -o myoutput.xml
59                - processes all netcdf files in the mynetcdffiles directory, outputs to
60                  file called myoutput.xml
61       
62       
63        python Coapec2csml.py -d /home/users/me/mynetcdffiles/ -r
64                - processes all netcdf files in the mynetcdffiles directory and
65                  any subdirectories, output to default file csmloutput.xml
66                 
67       
68        python Coapec2csml.py myfile1.nc myfile2.nc myfile3.nc
69                - processes the three named files, output to default file csmloutput.xml
70       
71       
72        python Coapec2csml.py myfile1.nc myfile2.nc myfile3.nc -t timevar
73                - processes the three named files, aggregating with the time dimension
74                  'timevar'. Output to default file csmloutput.xml
75       
76        python Coapec2csml.py -d /home/users/me/mynetcdffiles/
77                                 -o myoutput.xml myfile1.nc myfile2.nc myfile3.nc
78       
79                - same as the first example. The -d option overrides the file list so files
80                  myfile1.nc, myfile2.nc and myfile3.nc are all ignored. Outputs to
81                  file called myoutput.xml.
82               
83   
84Notes:
85
86        (1) The files must be CF compliant.
87       
88        (2) Currently only works for GridSeriesFeatures - ie. typical atmospheric model
89         output.
90       
91       
92"""
93
94
95def main(optargs=None):
96    #Main program:
97    #***************
98    #Get command line arguments
99    if optargs:
100        #if called as main(args) from another python module use these args, else use
101        #sys.argv if called from command line.
102        sys.argv =optargs
103    try:
104            opts, args = getopt.getopt(sys.argv[1:], "d:c:t:fro:h", ["directory=", "csmlfeaturetype=", "timedimension=", "filemapping=", "recursive", "outputfile=", "help"])
105    except getopt.error, msg:
106            print "Invalid options, use --help for help"
107            sys.exit()
108    print sys.argv
109    # process options
110    directory = None
111    csmlfeaturetype = 'GridSeriesFeature'   #default feature type
112    timedimension  = 't'
113    mapping = None
114    recursive = 0
115    individual = False
116    outputfile = 'csmloutput.xml'
117    for o, v in opts:
118            if o in ("-d", "--directory"):
119                    directory = v
120            elif o in ("-c", "--csmlfeaturetype"):
121                    csmlfeaturetype = v
122            elif o in ("-t", "--timedimension"):
123                    timedimension = v
124            elif o in ("-f", "--filemapping"):
125                    if v == '11':
126                        mapping = 'onetoone'
127                    elif v == '1n':
128                        mapping = 'onetomany'
129            elif o in ("-o", "--outputfile"):
130                    outputfile = v
131            elif o in ("-h", "--help"):
132                    print usage
133                    sys.exit()                         
134                   
135   
136           
137    #create CSML:
138    if csmlfeaturetype == "PointFeature":
139        #NOT YET IMPLEMENTED#
140        if mapping is None:
141            mapping = 'onetoone' #default filemapping for PointFeature (?)
142           
143    elif csmlfeaturetype == "ProfileFeature":
144        #NOT YET IMPLEMENTED#
145        if mapping is None:
146            mapping = 'onetoone' #default filemapping for ProfileFeature (?)
147           
148    elif csmlfeaturetype == "GridFeature":
149            #NOT YET IMPLEMENTED#
150        if mapping is None:
151            mapping = 'onetoone' #default filemapping for GridFeature
152   
153    elif csmlfeaturetype == "PointSeriesFeature":
154        if mapping is None:
155            mapping = 'onetomany'
156   
157    elif csmlfeaturetype == "ProfileSeriesFeature":
158        if mapping is None:     #NOT YET IMPLEMENTED#
159            mapping = 'onetomany' #default filemapping for ProfileSeriesFeature (?)
160           
161    elif csmlfeaturetype == "GridSeriesFeature":
162        if mapping is None:
163            mapping = 'onetomany' #default filemapping for GridSeriesFeature
164   
165    csmldataset=csmllibs.csmlbuilder.csmlBuilder(directory,csmlfeaturetype,mapping,timedimension, outputfile)
166    print "building csml..."
167    csmldataset.build()
168   
169   
170    print ''
171    print '********************************************************************'
172    print 'CSML file is at: ' + outputfile
173    print '********************************************************************'               
174    print ''
175
176
177if __name__=='__main__':
178    main()
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
Note: See TracBrowser for help on using the repository browser.