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

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

added option to pass dataset ID to csml scanner; use config option of form: [dataset] dsID:myIDabc123

  • 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    try:
78        value = self.get(section, option)
79    except ConfigParser.NoSectionError,ConfigParser.NoOptionError:
80        #config value is not set
81        value =None 
82    return value
83   
84def main(optargs=None):
85    #assign extra method to configparser
86    ConfigParser.ConfigParser.getConfigNoError=getConfigNoError
87    config = ConfigParser.ConfigParser()
88    #Get command line arguments
89    if optargs:
90        #if called as main(args) from another python module use these args, else use
91        #sys.argv if called from command line.
92        sys.argv =optargs
93    try:
94            opts, args = getopt.getopt(sys.argv[1:], "c:d:f:xt:m:rpo:h", ["configfile=,directory=", "csmlfeaturetype=", "exitafterconfig","timedimension=", "filemapping=", "recursive", "printscreen", "outputfile=", "help"])
95    except getopt.error, msg:
96            print "Invalid options, use --help for help"
97            sys.exit()
98    hasConfig=False
99    for o, v in opts:
100            if o in ("-c", "--configfile"):
101                configfile = v
102                hasConfig=True
103                configTest=False
104                for o, v in opts:
105                    if o in ("-x", "--exitafterconfig"):
106                        configTest=True
107           
108    #set default values for required configs
109    PRINTSCREEN = 0
110    MAPPING=None
111    TIMEDIMENSION=None
112    TIMESTORAGE='inline'
113    VALUESTORAGE='fileextract'
114    SPATIALSTORAGE='fileextract'
115   
116    #...
117    if hasConfig == True:
118        config.read(configfile)
119        DATASETID = config.getConfigNoError('dataset', 'dsID')
120        FEATURETYPE = config.getConfigNoError('features', 'type')
121        FEATURENUMBER = config.getConfigNoError('features', 'number')
122        ROOTDIRECTORY = config.getConfigNoError('files', 'root')
123        MAPPING = config.getConfigNoError('files', 'mapping')
124        OUTPUTFILE = config.getConfigNoError('files', 'output')
125        PRINTSCREEN = config.getConfigNoError('files', 'printscreen')
126        SRSNAME = config.getConfigNoError('SpatialReference', 'srs')
127        TIMEDIMENSION=config.getConfigNoError('time','timedimension')
128        TIMESTORAGE=config.getConfigNoError('time','timestorage')
129        VALUESTORAGE=config.getConfigNoError('values','valuestorage')
130        SPATIALSTORAGE=config.getConfigNoError('spatialaxes','spatialstorage')
131       
132        if configTest==True:
133            print 'Testing config file:'
134            print 'Enables you to check your config file has been correctly interpreted.'
135            print '\n'
136            print 'The following config options have been set:'
137            print '\n dataset:dsID =%s'%DATASETID
138            print '\n features:type =%s'%FEATURETYPE
139            print '\n features:number =%s'%FEATURENUMBER
140            print '\n files:root =%s'%ROOTDIRECTORY
141            print '\n files:mapping =%s'%MAPPING
142            print '\n files:output =%s'%OUTPUTFILE
143            print '\n files:printscreen =%s'%PRINTSCREEN
144            print '\n SpatialReference:srs =%s'%SRSNAME
145            print '\n time:timedimension =%s'%TIMEDIMENSION
146            print '\n time:timestorage =%s'%TIMESTORAGE
147            print '\n spatialaxes:spatialstorage =%s'%SPATIALSTORAGE
148            print '\n values:valuestorage =%s'%VALUESTORAGE
149            sys.exit()
150   
151    else: #no config file, get command line args
152        for o, v in opts:
153            print o,v
154        for o, v in opts:
155            if o in ("-d", "--directory"):
156                    ROOTDIRECTORY = v
157            elif o in ("-f", "--csmlfeaturetype"):
158                    FEATURETYPE = v
159            elif o in ("-t", "--timedimension"):
160                    TIMEDIMENSION = v
161            elif o in ("-f", "--filemapping"):
162                    MAPPING=v
163            elif o in ("-p", "--printscreen"):
164                    PRINTSCREEN = 1
165            elif o in ("-o", "--outputfile"):
166                    OUTPUTFILE = v
167            elif o in ("-h", "--help"):
168                    print usage
169                    sys.exit() 
170                   
171    # Handle defaults in case of no file mapping:
172    if MAPPING is None:
173        if FEATURETYPE == "Point":
174            MAPPING = 'onetoone' #default filemapping for PointFeature (?)
175        elif FEATURETYPE == "Profile":           
176            MAPPING = 'onetoone' #default filemapping for ProfileFeature (?)
177        elif FEATURETYPE == "Grid":
178            MAPPING = 'onetoone' #default filemapping for GridFeature
179        elif FEATURETYPE == "PointSeries":
180            MAPPING = 'onetomany'
181        elif FEATURETYPE == "ProfileSeries":
182            MAPPING = 'onetomany' #default filemapping for ProfileSeriesFeature (?)
183        elif FEATURETYPE == "GridSeries":
184            MAPPING = 'onetomany' #default filemapping for GridSeriesFeature
185   
186    #build CSML document
187    print ROOTDIRECTORY
188    csmldataset=csmllibs.csmlbuilder.csmlBuilder(DATASETID,ROOTDIRECTORY,FEATURETYPE,MAPPING,TIMEDIMENSION, OUTPUTFILE, PRINTSCREEN,TIMESTORAGE,SPATIALSTORAGE,VALUESTORAGE)
189    csmldataset.build()
190
191    print ''
192    print '********************************************************************'
193    print 'CSML file is at: ' + OUTPUTFILE
194    print '********************************************************************'               
195    print ''
196
197
198if __name__=='__main__':   
199    main()
200
201
202
203
204
205
206
207
208
209
210
211
212
213
Note: See TracBrowser for help on using the repository browser.