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

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

Added optional -p printscreen arg

  • 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:frpo:h", ["directory=", "csmlfeaturetype=", "timedimension=", "filemapping=", "recursive", "printscreen", "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    printscreen=0
116    print printscreen
117    individual = False
118    outputfile = 'csmloutput.xml'
119    for o, v in opts:
120            if o in ("-d", "--directory"):
121                    directory = v
122            elif o in ("-c", "--csmlfeaturetype"):
123                    csmlfeaturetype = v
124            elif o in ("-t", "--timedimension"):
125                    timedimension = v
126            elif o in ("-f", "--filemapping"):
127                    if v == '11':
128                        mapping = 'onetoone'
129                    elif v == '1n':
130                        mapping = 'onetomany'
131            elif o in ("-p", "--printscreen"):
132                    printscreen = 1
133            elif o in ("-o", "--outputfile"):
134                    outputfile = v
135            elif o in ("-h", "--help"):
136                    print usage
137                    sys.exit()                         
138   
139           
140    #create CSML:
141    if csmlfeaturetype == "PointFeature":
142        #NOT YET IMPLEMENTED#
143        if mapping is None:
144            mapping = 'onetoone' #default filemapping for PointFeature (?)
145           
146    elif csmlfeaturetype == "ProfileFeature":
147        #NOT YET IMPLEMENTED#
148        if mapping is None:
149            mapping = 'onetoone' #default filemapping for ProfileFeature (?)
150           
151    elif csmlfeaturetype == "GridFeature":
152            #NOT YET IMPLEMENTED#
153        if mapping is None:
154            mapping = 'onetoone' #default filemapping for GridFeature
155   
156    elif csmlfeaturetype == "PointSeriesFeature":
157        if mapping is None:
158            mapping = 'onetomany'
159   
160    elif csmlfeaturetype == "ProfileSeriesFeature":
161        if mapping is None:     #NOT YET IMPLEMENTED#
162            mapping = 'onetomany' #default filemapping for ProfileSeriesFeature (?)
163           
164    elif csmlfeaturetype == "GridSeriesFeature":
165        if mapping is None:
166            mapping = 'onetomany' #default filemapping for GridSeriesFeature
167
168    csmldataset=csmllibs.csmlbuilder.csmlBuilder(directory,csmlfeaturetype,mapping,timedimension, outputfile, printscreen)
169    print "building csml..."
170    csmldataset.build()
171   
172   
173    print ''
174    print '********************************************************************'
175    print 'CSML file is at: ' + outputfile
176    print '********************************************************************'               
177    print ''
178
179
180if __name__=='__main__':
181    main()
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
Note: See TracBrowser for help on using the repository browser.