source: TI02-CSML/trunk/csmlscan.py @ 1909

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

Moving CSML v2 branch to trunk

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