source: TI02-CSML/trunk/csml/csmlscan.py @ 3286

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

overhaul of scanning command args

  • 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 ConfigParser
14import getopt
15import os
16#CSML modules
17import csmllibs
18import parser
19import parser_extra
20
21
22
23usage = """Usage:
24    Coapec2CSML [options] <files>
25
26    Scan a list of files (netCDF/PP/GRIB) producing a CSML file for those files
27    (as a single CSML Dataset)
28    NOTE: these arguments are out of date.
29
30Arguments:
31
32    <files> is a list of file paths to scan. The files can be listed in any order,
33    and may be in multiple directories. Currently only full path names are supported.
34    These files will be marked up as a single CSML Dataset.
35       
36Options:
37   
38    c:d:i:a:f:xt:m:po:s:v:T:h", ["configfile=","directory=","id=","minaxes=", "csmlfeaturetype=", "exitafterconfig","timedimension=", "filemapping=",  "printscreen", "outputfile=", "spatialstorage=","valuestorage=","timestorage=","help"])
39
40    -c configfile: A config file as described on the CSML wiki here: http://proj.badc.rl.ac.uk/ndg/wiki/T02_CSML/Csmlscan#Examplecontentsofconfigfile
41   
42    -d directory: Root directory. Scanner will scan all files in this directory and below.
43   
44    -i id: Dataset ID. This will be written to the CSML file as the Dataset ID attribute. Otherwise a random ID is generated.
45   
46    -a minaxes: A dimension threshold below which variables are not considered feature. e.g. If this is 3 only 3-Dimensional and above variables will be considered for scanning. Useful for case where auxiliary variables may confuse the scanner.
47   
48    -f csmlfeaturetype: The csmlFeatureType of the data. Defaults to GridSeries.
49   
50    -x:   Print out the config options that have been set, but don't do the actual scan. Useful for trying out config options.
51                                               
52     -t timedimension: The default time dimension is 'time' as per the CF recomendations. If your files use a
53                different variable name you may supply it with this option.
54
55    -m filemapping: One of the file map patterns, see here for more info: http://proj.badc.rl.ac.uk/ndg/wiki/T02_CSML/Csmlscan#filessection:
56
57    -o outputfile:      The name of the output csml file, default is csmloutput.xml
58                                which can be found in the directory from which the program is run
59   
60    -p: If this flag is included, the CSML output is printed to std out.
61   
62    -s spatialstorage: How to store spatial domain info in the csml file: inline or fileextract
63   
64    -v valuestorage: How to store rangeset info in the csml file: inline or fileextract
65   
66    -T timestorage: How to store temporal info in the csml file: inline or fileextract
67   
68    -h help: Shows this help information.
69       
70       
71Examples:
72        csmlscan -o testout3.xml -t t /home/me/files/xaaqda@pxr99c1.nc /home/me/files/xaaqda@pxs09c1.nc
73               - processes 2 netcdf files supplied, which have a time dimension called 't'
74               - output csmlfile is called testout3.xml
75               
76        csmlscan -c myconfigfile.cfg
77                - reads settings from config file called myconfigfile.cfg
78               
79        TODO: more examples....
80       
81"""
82#this should go in ndg.utils
83def getConfigNoError(self, section, option):
84    # get the config option, returning None if not present
85    #value = self.get(section, option)
86    try:
87        value = self.get(section, option)
88    except ConfigParser.NoOptionError:
89        #config value is not set - option could not be found
90        value =None 
91    except ConfigParser.NoSectionError:
92        #config value is not set - section could not be found
93        value =None
94    return value
95   
96def main(optargs=None):
97    #assign extra method to configparser
98    ConfigParser.ConfigParser.getConfigNoError=getConfigNoError
99    config = ConfigParser.ConfigParser()
100    #Get command line arguments
101    print sys.argv
102    if optargs:
103        #if called as main(args) from another python module use these args, else use
104        #sys.argv if called from command line.
105        sys.argv =optargs
106    try:
107            opts, args = getopt.getopt(sys.argv[1:], "c:d:i:a:f:xt:m:po:s:v:T:h", ["configfile=","directory=","id=","minaxes=", "csmlfeaturetype=", "exitafterconfig","timedimension=", "filemapping=",  "printscreen", "outputfile=", "spatialstorage=","valuestorage=","timestorage=","help"])
108    except getopt.error, msg:
109            print "Invalid options, use --help for help"
110            sys.exit()
111    hasConfig=False
112    configTest=False
113    for o, v in opts:
114            if o in ("-c", "--configfile"):
115                configfile = v
116                hasConfig=True
117            elif o in ("-x", "--exitafterconfig"):
118                configTest=True
119           
120    #set default values for required configs
121    DATASETID=None
122    MINAXES = 2
123    PRINTSCREEN = 0
124    MAPPING=None
125    OUTPUTFILE='csmloutput.xml'
126    TIMEDIMENSION='time'
127    TIMESTORAGE='inline'
128    VALUESTORAGE='fileextract'
129    SPATIALSTORAGE='fileextract'
130    FEATURETYPE='GridSeries'
131    SELECTION=None
132    ROOTDIRECTORY=os.getcwd()
133   
134    #...
135    if hasConfig == True:
136        config.read(configfile)
137        conf=config.getConfigNoError('dataset', 'dsID')
138        if conf:
139            DATASETID = conf 
140        conf=config.getConfigNoError('features', 'type') 
141        if conf:
142            FEATURETYPE = conf
143        conf=config.getConfigNoError('features','minaxes')
144        if conf:
145            MINAXES=conf
146        conf=config.getConfigNoError('files', 'root')
147        if conf:
148            ROOTDIRECTORY = conf
149        conf=config.getConfigNoError('files', 'selection')
150        if conf:
151            SELECTION=conf
152        conf=config.getConfigNoError('files', 'mapping')
153        if conf:
154            MAPPING = conf
155        conf=config.getConfigNoError('files', 'output')
156        if conf:
157            OUTPUTFILE = conf
158        conf=config.getConfigNoError('files', 'printscreen')
159        if conf:
160            PRINTSCREEN = conf
161        conf=config.getConfigNoError('time','timedimension')
162        if conf:
163            TIMEDIMENSION=conf
164        conf=config.getConfigNoError('time','timestorage')
165        if conf:
166            TIMESTORAGE=conf
167        conf=config.getConfigNoError('values','valuestorage')
168        if conf:
169            VALUESTORAGE=conf
170        conf=config.getConfigNoError('spatialaxes','spatialstorage')
171        if conf:
172            SPATIALSTORAGE=conf
173   
174    else: #no config file, get command line args
175        for o, v in opts:
176            print o,v
177        for o, v in opts:
178            if o in ("-d", "--directory"):
179                ROOTDIRECTORY = v
180            elif o in ('-i','id'):
181                DATASETID=v
182            elif o in ('-a','minaxes'):
183                MINAXES=v
184            elif o in ("-f", "--csmlfeaturetype"):
185                FEATURETYPE = v
186            elif o in ("-t", "--timedimension"):
187                TIMEDIMENSION = v
188            elif o in ("-m", "--filemapping"):
189                MAPPING=v
190            elif o in ("-p", "--printscreen"):
191                PRINTSCREEN = 1
192            elif o in ("-o", "--outputfile"):
193                OUTPUTFILE = v
194            elif o in ("-T", "--timestorage"):
195                TIMESTORAGE = v
196            elif o in ("-v", "--valuestorage"):
197                VALUESTORAGE = v
198            elif o in ("-s", "--spatialstorage"):
199                SPATIALSTORAGE = v
200            elif o in ("-h", "--help"):
201                print usage
202                sys.exit()     
203        if args is not []:
204            SELECTION=args
205
206    # Handle defaults in case of no file mapping:
207    if MAPPING is None:
208        if FEATURETYPE == "Point":
209            MAPPING = 'onetoone' #default filemapping for PointFeature (?)
210        elif FEATURETYPE == "Profile":           
211            MAPPING = 'onetoone' #default filemapping for ProfileFeature (?)
212        elif FEATURETYPE == "Grid":
213            MAPPING = 'onetoone' #default filemapping for GridFeature
214        elif FEATURETYPE == "PointSeries":
215            MAPPING = 'onetomany'
216        elif FEATURETYPE == "ProfileSeries":
217            MAPPING = 'onetomany' #default filemapping for ProfileSeriesFeature (?)
218        elif FEATURETYPE == "GridSeries":
219            MAPPING = 'onetomany' #default filemapping for GridSeriesFeature     
220             
221        print '\n Starting scan...'
222        print '\n'
223        print 'The following config options have been set:'
224        print '\n dataset:dsID = %s'%DATASETID
225        print '\n features:type = %s'%FEATURETYPE
226        print '\n features:minaxes = %s'%MINAXES
227        print '\n files:root = %s'%ROOTDIRECTORY
228        print '\n files.selection = %s'%SELECTION
229        print '\n files:mapping = %s'%MAPPING
230        print '\n files:output = %s'%OUTPUTFILE
231        print '\n files:printscreen = %s'%PRINTSCREEN
232        print '\n time:timedimension = %s'%TIMEDIMENSION
233        print '\n time:timestorage = %s'%TIMESTORAGE
234        print '\n spatialaxes:spatialstorage = %s'%SPATIALSTORAGE
235        print '\n values:valuestorage = %s'%VALUESTORAGE
236        print '\n For more information on config options run the command "csmlscan --help"'
237       
238       
239        if configTest==True:
240            #don't actually start the scan
241            sys.exit()
242           
243           
244       
245    #build CSML document
246    csmldataset=csmllibs.csmlbuilder.csmlBuilder(DATASETID,ROOTDIRECTORY,SELECTION,FEATURETYPE,MAPPING,TIMEDIMENSION, OUTPUTFILE, PRINTSCREEN,TIMESTORAGE,SPATIALSTORAGE,VALUESTORAGE, MINAXES)
247    csmldataset.build()
248
249    print ''
250    print '********************************************************************'
251    print 'CSML file is at: ' + OUTPUTFILE
252    print '********************************************************************'               
253    print ''
254
255
256if __name__=='__main__':   
257    main()
258
259
260
261
262
263
264
265
266
267
268
269
270
271
Note: See TracBrowser for help on using the repository browser.