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

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

aligned command line args with config options

  • 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    #...
111    if hasConfig == True:
112        config.read(configfile)
113        FEATURETYPE = config.getConfigNoError('features', 'type')
114        FEATURENUMBER = config.getConfigNoError('features', 'number')
115        ROOTDIRECTORY = config.getConfigNoError('files', 'root')
116        MAPPING = config.getConfigNoError('files', 'mapping')
117        OUTPUTFILE = config.getConfigNoError('files', 'output')
118        PRINTSCREEN = config.getConfigNoError('files', 'printscreen')
119        SRSNAME = config.getConfigNoError('SpatialReference', 'srs')
120        TIMEDIMENSION=config.getConfigNoError('time','timedimension')
121       
122        if configTest==True:
123            print 'Testing config file:'
124            print 'Enables you to check your config file has been correctly interpreted.'
125            print '\n'
126            print 'The following config options have been set:'
127            print '\n features:type =%s'%FEATURETYPE
128            print '\n features:number =%s'%FEATURENUMBER
129            print '\n files:root =%s'%ROOTDIRECTORY
130            print '\n files:mapping =%s'%MAPPING
131            print '\n files:output =%s'%OUTPUTFILE
132            print '\n files:printscreen =%s'%PRINTSCREEN
133            print '\n SpatialReference:srs =%s'%SRSNAME
134            print '\n time:timedimension =%s'%TIMEDIMENSION
135            sys.exit()
136   
137    else: #no config file, get command line args
138        for o, v in opts:
139            print o,v
140        for o, v in opts:
141            if o in ("-d", "--directory"):
142                    ROOTDIRECTORY = v
143            elif o in ("-f", "--csmlfeaturetype"):
144                    FEATURETYPE = v
145            elif o in ("-t", "--timedimension"):
146                    TIMEDIMENSION = v
147            elif o in ("-f", "--filemapping"):
148                    MAPPING=v
149            elif o in ("-p", "--printscreen"):
150                    PRINTSCREEN = 1
151            elif o in ("-o", "--outputfile"):
152                    OUTPUTFILE = v
153            elif o in ("-h", "--help"):
154                    print usage
155                    sys.exit() 
156                   
157    # Handle defaults in case of no file mapping:
158    if MAPPING is None:
159        if FEATURETYPE == "PointFeature":
160            MAPPING = 'onetoone' #default filemapping for PointFeature (?)
161        elif FEATURETYPE == "ProfileFeature":           
162            MAPPING = 'onetoone' #default filemapping for ProfileFeature (?)
163        elif FEATURETYPE == "GridFeature":
164            MAPPING = 'onetoone' #default filemapping for GridFeature
165        elif FEATURETYPE == "PointSeriesFeature":
166            MAPPING = 'onetomany'
167        elif FEATURETYPE == "ProfileSeriesFeature":
168            MAPPING = 'onetomany' #default filemapping for ProfileSeriesFeature (?)
169        elif FEATURETYPE == "GridSeriesFeature":
170            MAPPING = 'onetomany' #default filemapping for GridSeriesFeature
171   
172    #build CSML document
173    print ROOTDIRECTORY
174    csmldataset=csmllibs.csmlbuilder.csmlBuilder(ROOTDIRECTORY,FEATURETYPE,MAPPING,TIMEDIMENSION, OUTPUTFILE, PRINTSCREEN)
175    csmldataset.build()
176
177    print ''
178    print '********************************************************************'
179    print 'CSML file is at: ' + OUTPUTFILE
180    print '********************************************************************'               
181    print ''
182
183
184if __name__=='__main__':
185    main()
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
Note: See TracBrowser for help on using the repository browser.