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

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

nearest neighbour working for times, plus separation of subsetting into core functions and feature specific ones

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