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

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

VirtualDirectoryTree added to enable arbitrary collections of files to be scanned

  • 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.
34    These files will be marked up as a single CSML Dataset.
35    Please note that use of the 'directory' option will override any file arguments here.
36       
37Options:
38
39    -d directory: All netcdf files in the directory are marked up in a single CSML file
40                       
41               
42    -f csmlfeaturetype: The csmlFeatureType of the data. Only GridSeriesProfile is
43                currently implemented. Other feature types are PointFeature
44                PointSeriesFeature ProfileFeature ProfileSeriesFeature and GridFeature.
45                                               
46     -t timedimension:  The default time dimension is 't'. If your files use a
47                different variable name you may supply it with this option.
48
49    -o outputfile:      Provide the name of the output file, default is csmloutput.xml
50                                which can be found in the directory from which the program is run
51        -h help:                Shows this help information.
52       
53       
54Example:
55       
56        python Coapec2csml.py -d /home/users/me/mynetcdffiles/ -o myoutput.xml
57                - processes all netcdf files in the mynetcdffiles directory, outputs to
58                  file called myoutput.xml
59       
60       
61        python Coapec2csml.py myfile1.nc myfile2.nc myfile3.nc -t timevar
62                - processes the three named files, aggregating with the time dimension
63                  'timevar'. Output to default file csmloutput.xml
64       
65        python Coapec2csml.py -d /home/users/me/mynetcdffiles/
66                                 -o myoutput.xml myfile1.nc myfile2.nc myfile3.nc
67       
68                - same as the first example. The -d option overrides the file list so files
69                  myfile1.nc, myfile2.nc and myfile3.nc are all ignored. Outputs to
70                  file called myoutput.xml.
71       
72        TODO: more examples     
73       
74"""
75#this should go in ndg.utils
76def getConfigNoError(self, section, option):
77    # get the config option, returning None if not present
78    #value = self.get(section, option)
79    try:
80        value = self.get(section, option)
81    except ConfigParser.NoOptionError:
82        #config value is not set - option could not be found
83        value =None 
84    except ConfigParser.NoSectionError:
85        #config value is not set - section could not be found
86        value =None
87    return value
88   
89def main(optargs=None):
90    #assign extra method to configparser
91    ConfigParser.ConfigParser.getConfigNoError=getConfigNoError
92    config = ConfigParser.ConfigParser()
93    #Get command line arguments
94    if optargs:
95        #if called as main(args) from another python module use these args, else use
96        #sys.argv if called from command line.
97        sys.argv =optargs
98    try:
99            opts, args = getopt.getopt(sys.argv[1:], "c:d:f:xt:m:rpo:h", ["configfile=,directory=", "csmlfeaturetype=", "exitafterconfig","timedimension=", "filemapping=", "recursive", "printscreen", "outputfile=", "help"])
100    except getopt.error, msg:
101            print "Invalid options, use --help for help"
102            sys.exit()
103    hasConfig=False
104    for o, v in opts:
105            if o in ("-c", "--configfile"):
106                configfile = v
107                hasConfig=True
108                configTest=False
109                for o, v in opts:
110                    if o in ("-x", "--exitafterconfig"):
111                        configTest=True
112           
113    #set default values for required configs
114    PRINTSCREEN = 0
115    MAPPING=None
116    TIMEDIMENSION=None
117    TIMESTORAGE='inline'
118    VALUESTORAGE='fileextract'
119    SPATIALSTORAGE='fileextract'
120    FEATURETYPE=None
121   
122    #...
123    if hasConfig == True:
124        config.read(configfile)
125        DATASETID = config.getConfigNoError('dataset', 'dsID')
126        FEATURETYPE = config.getConfigNoError('features', 'type')
127        FEATURENUMBER = config.getConfigNoError('features', 'number')       
128
129        try:
130            MINAXES=eval(config.getConfigNoError('features','minaxes'))
131        except:
132            MINAXES=0
133        ROOTDIRECTORY = config.getConfigNoError('files', 'root')
134        SELECTION=config.getConfigNoError('files', 'selection')
135        MAPPING = config.getConfigNoError('files', 'mapping')
136        OUTPUTFILE = config.getConfigNoError('files', 'output')
137        PRINTSCREEN = config.getConfigNoError('files', 'printscreen')
138        SRSNAME = config.getConfigNoError('SpatialReference', 'srs')
139        TIMEDIMENSION=config.getConfigNoError('time','timedimension')
140        TIMESTORAGE=config.getConfigNoError('time','timestorage')
141        VALUESTORAGE=config.getConfigNoError('values','valuestorage')
142        SPATIALSTORAGE=config.getConfigNoError('spatialaxes','spatialstorage')
143        SEC_ALLOW=config.getConfigNoError('security', 'allow')
144        SEC_DENY=config.getConfigNoError('security', 'deny')
145       
146        if configTest==True:
147            print 'Testing config file:'
148            print 'Enables you to check your config file has been correctly interpreted.'
149            print '\n'
150            print 'The following config options have been set:'
151            print '\n dataset:dsID =%s'%DATASETID
152            print '\n features:type =%s'%FEATURETYPE
153            print '\n features:number =%s'%FEATURENUMBER
154            print '\n features:minaxes =%s'%MINAXES
155            print '\n files:root =%s'%ROOTDIRECTORY
156            print '\n files:mapping =%s'%MAPPING
157            print '\n files:output =%s'%OUTPUTFILE
158            print '\n files:printscreen =%s'%PRINTSCREEN
159            print '\n SpatialReference:srs =%s'%SRSNAME
160            print '\n time:timedimension =%s'%TIMEDIMENSION
161            print '\n time:timestorage =%s'%TIMESTORAGE
162            print '\n spatialaxes:spatialstorage =%s'%SPATIALSTORAGE
163            print '\n values:valuestorage =%s'%VALUESTORAGE
164            print '\n security:allow =%s'%SEC_ALLOW
165            print '\n security:deny =%s'%SEC_DENY
166            sys.exit()
167   
168    else: #no config file, get command line args
169        for o, v in opts:
170            print o,v
171        for o, v in opts:
172            if o in ("-d", "--directory"):
173                    ROOTDIRECTORY = v
174            elif o in ("-f", "--csmlfeaturetype"):
175                    FEATURETYPE = v
176            elif o in ("-t", "--timedimension"):
177                    TIMEDIMENSION = v
178            elif o in ("-f", "--filemapping"):
179                    MAPPING=v
180            elif o in ("-p", "--printscreen"):
181                    PRINTSCREEN = 1
182            elif o in ("-o", "--outputfile"):
183                    OUTPUTFILE = v
184            elif o in ("-h", "--help"):
185                    print usage
186                    sys.exit() 
187   
188    if FEATURETYPE is None:
189        raise Exception('No featuretype declared in config file (or no config file)')
190     
191   
192    # Handle defaults in case of no file mapping:
193    if MAPPING is None:
194        if FEATURETYPE == "Point":
195            MAPPING = 'onetoone' #default filemapping for PointFeature (?)
196        elif FEATURETYPE == "Profile":           
197            MAPPING = 'onetoone' #default filemapping for ProfileFeature (?)
198        elif FEATURETYPE == "Grid":
199            MAPPING = 'onetoone' #default filemapping for GridFeature
200        elif FEATURETYPE == "PointSeries":
201            MAPPING = 'onetomany'
202        elif FEATURETYPE == "ProfileSeries":
203            MAPPING = 'onetomany' #default filemapping for ProfileSeriesFeature (?)
204        elif FEATURETYPE == "GridSeries":
205            MAPPING = 'onetomany' #default filemapping for GridSeriesFeature
206   
207    #build CSML document
208    print ROOTDIRECTORY
209    csmldataset=csmllibs.csmlbuilder.csmlBuilder(DATASETID,ROOTDIRECTORY,SELECTION,FEATURETYPE,MAPPING,TIMEDIMENSION, OUTPUTFILE, PRINTSCREEN,TIMESTORAGE,SPATIALSTORAGE,VALUESTORAGE, MINAXES, SEC_ALLOW, SEC_DENY)
210    csmldataset.build()
211
212    print ''
213    print '********************************************************************'
214    print 'CSML file is at: ' + OUTPUTFILE
215    print '********************************************************************'               
216    print ''
217
218
219if __name__=='__main__':   
220    main()
221
222
223
224
225
226
227
228
229
230
231
232
233
234
Note: See TracBrowser for help on using the repository browser.