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

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

fixed multiple glitches in scanner

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