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

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

nicer exception

  • 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
29Arguments:
30
31    <files> is a list of file paths to scan. The files can be listed in any order,
32    and may be in multiple directories.
33    These files will be marked up as a single CSML Dataset.
34    Please note that use of the 'directory' option will override any file arguments here.
35       
36Options:
37
38    -d directory: All netcdf files in the directory are marked up in a single CSML file
39                       
40               
41    -f csmlfeaturetype: The csmlFeatureType of the data. Only GridSeriesProfile is
42                currently implemented. Other feature types are PointFeature
43                PointSeriesFeature ProfileFeature ProfileSeriesFeature and GridFeature.
44                                               
45     -t timedimension:  The default time dimension is 't'. If your files use a
46                different variable name you may supply it with this option.
47
48    -o outputfile:      Provide the name of the output file, default is csmloutput.xml
49                                which can be found in the directory from which the program is run
50        -h help:                Shows this help information.
51       
52       
53Example:
54       
55        python Coapec2csml.py -d /home/users/me/mynetcdffiles/ -o myoutput.xml
56                - processes all netcdf files in the mynetcdffiles directory, outputs to
57                  file called myoutput.xml
58       
59       
60        python Coapec2csml.py myfile1.nc myfile2.nc myfile3.nc -t timevar
61                - processes the three named files, aggregating with the time dimension
62                  'timevar'. Output to default file csmloutput.xml
63       
64        python Coapec2csml.py -d /home/users/me/mynetcdffiles/
65                                 -o myoutput.xml myfile1.nc myfile2.nc myfile3.nc
66       
67                - same as the first example. The -d option overrides the file list so files
68                  myfile1.nc, myfile2.nc and myfile3.nc are all ignored. Outputs to
69                  file called myoutput.xml.
70       
71        TODO: more examples     
72       
73"""
74#this should go in ndg.utils
75def getConfigNoError(self, section, option):
76    # get the config option, returning None if not present
77    #value = self.get(section, option)
78    try:
79        value = self.get(section, option)
80    except ConfigParser.NoOptionError:
81        #config value is not set - option could not be found
82        value =None 
83    except ConfigParser.NoSectionError:
84        #config value is not set - section could not be found
85        value =None
86    return value
87   
88def main(optargs=None):
89    #assign extra method to configparser
90    ConfigParser.ConfigParser.getConfigNoError=getConfigNoError
91    config = ConfigParser.ConfigParser()
92    #Get command line arguments
93    if optargs:
94        #if called as main(args) from another python module use these args, else use
95        #sys.argv if called from command line.
96        sys.argv =optargs
97    try:
98            opts, args = getopt.getopt(sys.argv[1:], "c:d:f:xt:m:rpo:h", ["configfile=,directory=", "csmlfeaturetype=", "exitafterconfig","timedimension=", "filemapping=", "recursive", "printscreen", "outputfile=", "help"])
99    except getopt.error, msg:
100            print "Invalid options, use --help for help"
101            sys.exit()
102    hasConfig=False
103    for o, v in opts:
104            if o in ("-c", "--configfile"):
105                configfile = v
106                hasConfig=True
107                configTest=False
108                for o, v in opts:
109                    if o in ("-x", "--exitafterconfig"):
110                        configTest=True
111           
112    #set default values for required configs
113    PRINTSCREEN = 0
114    MAPPING=None
115    TIMEDIMENSION=None
116    TIMESTORAGE='inline'
117    VALUESTORAGE='fileextract'
118    SPATIALSTORAGE='fileextract'
119    FEATURETYPE=None
120   
121    #...
122    if hasConfig == True:
123        config.read(configfile)
124        DATASETID = config.getConfigNoError('dataset', 'dsID')
125        FEATURETYPE = config.getConfigNoError('features', 'type')
126        FEATURENUMBER = config.getConfigNoError('features', 'number')       
127
128        try:
129            MINAXES=eval(config.getConfigNoError('features','minaxes'))
130        except:
131            MINAXES=0
132        ROOTDIRECTORY = config.getConfigNoError('files', 'root')
133        MAPPING = config.getConfigNoError('files', 'mapping')
134        OUTPUTFILE = config.getConfigNoError('files', 'output')
135        PRINTSCREEN = config.getConfigNoError('files', 'printscreen')
136        SRSNAME = config.getConfigNoError('SpatialReference', 'srs')
137        TIMEDIMENSION=config.getConfigNoError('time','timedimension')
138        TIMESTORAGE=config.getConfigNoError('time','timestorage')
139        VALUESTORAGE=config.getConfigNoError('values','valuestorage')
140        SPATIALSTORAGE=config.getConfigNoError('spatialaxes','spatialstorage')
141        SEC_ALLOW=config.getConfigNoError('security', 'allow')
142        print SEC_ALLOW
143        SEC_DENY=config.getConfigNoError('security', 'deny')
144        print SEC_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,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.