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

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

added ability to filter out low dimension variables in scanner. Added some test crs definitions for testing rotated grids. Added axis identification by standard name

  • 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        try:
123            MINAXES=eval(config.getConfigNoError('features','minaxes'))
124        except ConfigParser.NoOptionError:
125            MINAXES=0
126        ROOTDIRECTORY = config.getConfigNoError('files', 'root')
127        MAPPING = config.getConfigNoError('files', 'mapping')
128        OUTPUTFILE = config.getConfigNoError('files', 'output')
129        PRINTSCREEN = config.getConfigNoError('files', 'printscreen')
130        SRSNAME = config.getConfigNoError('SpatialReference', 'srs')
131        TIMEDIMENSION=config.getConfigNoError('time','timedimension')
132        TIMESTORAGE=config.getConfigNoError('time','timestorage')
133        VALUESTORAGE=config.getConfigNoError('values','valuestorage')
134        SPATIALSTORAGE=config.getConfigNoError('spatialaxes','spatialstorage')
135       
136       
137        if configTest==True:
138            print 'Testing config file:'
139            print 'Enables you to check your config file has been correctly interpreted.'
140            print '\n'
141            print 'The following config options have been set:'
142            print '\n dataset:dsID =%s'%DATASETID
143            print '\n features:type =%s'%FEATURETYPE
144            print '\n features:number =%s'%FEATURENUMBER
145            print '\n features:minaxes =%s'%MINAXES
146            print '\n files:root =%s'%ROOTDIRECTORY
147            print '\n files:mapping =%s'%MAPPING
148            print '\n files:output =%s'%OUTPUTFILE
149            print '\n files:printscreen =%s'%PRINTSCREEN
150            print '\n SpatialReference:srs =%s'%SRSNAME
151            print '\n time:timedimension =%s'%TIMEDIMENSION
152            print '\n time:timestorage =%s'%TIMESTORAGE
153            print '\n spatialaxes:spatialstorage =%s'%SPATIALSTORAGE
154            print '\n values:valuestorage =%s'%VALUESTORAGE
155            sys.exit()
156   
157    else: #no config file, get command line args
158        for o, v in opts:
159            print o,v
160        for o, v in opts:
161            if o in ("-d", "--directory"):
162                    ROOTDIRECTORY = v
163            elif o in ("-f", "--csmlfeaturetype"):
164                    FEATURETYPE = v
165            elif o in ("-t", "--timedimension"):
166                    TIMEDIMENSION = v
167            elif o in ("-f", "--filemapping"):
168                    MAPPING=v
169            elif o in ("-p", "--printscreen"):
170                    PRINTSCREEN = 1
171            elif o in ("-o", "--outputfile"):
172                    OUTPUTFILE = v
173            elif o in ("-h", "--help"):
174                    print usage
175                    sys.exit() 
176                   
177    # Handle defaults in case of no file mapping:
178    if MAPPING is None:
179        if FEATURETYPE == "Point":
180            MAPPING = 'onetoone' #default filemapping for PointFeature (?)
181        elif FEATURETYPE == "Profile":           
182            MAPPING = 'onetoone' #default filemapping for ProfileFeature (?)
183        elif FEATURETYPE == "Grid":
184            MAPPING = 'onetoone' #default filemapping for GridFeature
185        elif FEATURETYPE == "PointSeries":
186            MAPPING = 'onetomany'
187        elif FEATURETYPE == "ProfileSeries":
188            MAPPING = 'onetomany' #default filemapping for ProfileSeriesFeature (?)
189        elif FEATURETYPE == "GridSeries":
190            MAPPING = 'onetomany' #default filemapping for GridSeriesFeature
191   
192    #build CSML document
193    print ROOTDIRECTORY
194    csmldataset=csmllibs.csmlbuilder.csmlBuilder(DATASETID,ROOTDIRECTORY,FEATURETYPE,MAPPING,TIMEDIMENSION, OUTPUTFILE, PRINTSCREEN,TIMESTORAGE,SPATIALSTORAGE,VALUESTORAGE, MINAXES)
195    csmldataset.build()
196
197    print ''
198    print '********************************************************************'
199    print 'CSML file is at: ' + OUTPUTFILE
200    print '********************************************************************'               
201    print ''
202
203
204if __name__=='__main__':   
205    main()
206
207
208
209
210
211
212
213
214
215
216
217
218
219
Note: See TracBrowser for help on using the repository browser.