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

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

Added new FeatureFileMap? option to Scanner. The option is called oneonly and means that the structure of just one file represents a feature spannign mulptiple directories.

  • 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 xml.dom.ext
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    -c 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    -r recursive:       if this option is provided along with the -d option, all
47                netcdf files in subdirectories of the -d directory are also processed.
48                If it is not provided, then only the netcdf files in the named directory
49                are processed.
50
51    -o outputfile:      Provide the name of the output file, default is csmloutput.xml
52                                which can be found in the directory from which the program is run
53        -h help:                Shows this help information.
54       
55       
56Example:
57       
58        python Coapec2csml.py -d /home/users/me/mynetcdffiles/ -o myoutput.xml
59                - processes all netcdf files in the mynetcdffiles directory, outputs to
60                  file called myoutput.xml
61       
62       
63        python Coapec2csml.py -d /home/users/me/mynetcdffiles/ -r
64                - processes all netcdf files in the mynetcdffiles directory and
65                  any subdirectories, output to default file csmloutput.xml
66                 
67       
68        python Coapec2csml.py myfile1.nc myfile2.nc myfile3.nc
69                - processes the three named files, output to default file csmloutput.xml
70       
71       
72        python Coapec2csml.py myfile1.nc myfile2.nc myfile3.nc -t timevar
73                - processes the three named files, aggregating with the time dimension
74                  'timevar'. Output to default file csmloutput.xml
75       
76        python Coapec2csml.py -d /home/users/me/mynetcdffiles/
77                                 -o myoutput.xml myfile1.nc myfile2.nc myfile3.nc
78       
79                - same as the first example. The -d option overrides the file list so files
80                  myfile1.nc, myfile2.nc and myfile3.nc are all ignored. Outputs to
81                  file called myoutput.xml.
82               
83   
84Notes:
85
86        (1) The files must be CF compliant.
87       
88        (2) Currently only works for GridSeriesFeatures - ie. typical atmospheric model
89         output.
90       
91       
92"""
93
94
95def main(optargs=None):
96    #Main program:
97    #***************
98    #Get command line arguments
99    if optargs:
100        #if called as main(args) from another python module use these args, else use
101        #sys.argv if called from command line.
102        sys.argv =optargs
103    try:
104            opts, args = getopt.getopt(sys.argv[1:], "d:c:t:f:rpo:h", ["directory=", "csmlfeaturetype=", "timedimension=", "filemapping=", "recursive", "printscreen", "outputfile=", "help"])
105    except getopt.error, msg:
106            print "Invalid options, use --help for help"
107            sys.exit()
108    print sys.argv
109   
110    # process options
111    directory = None
112    csmlfeaturetype = 'GridSeriesFeature'   #default feature type
113    timedimension  = 't'
114    mapping = None
115    recursive = 0
116    printscreen=0
117    individual = False
118    outputfile = 'csmloutput.xml'
119    for o, v in opts:
120            if o in ("-d", "--directory"):
121                    directory = v
122            elif o in ("-c", "--csmlfeaturetype"):
123                    csmlfeaturetype = v
124            elif o in ("-t", "--timedimension"):
125                    timedimension = v
126            elif o in ("-f", "--filemapping"):
127                    if v == '11':
128                        mapping = 'onetoone'
129                    elif v == '1n':
130                        mapping = 'onetomany'
131                    elif v == '1o':
132                        mapping = 'oneonly'
133            elif o in ("-p", "--printscreen"):
134                    printscreen = 1
135            elif o in ("-o", "--outputfile"):
136                    outputfile = v
137            elif o in ("-h", "--help"):
138                    print usage
139                    sys.exit()                         
140           
141    #create CSML:
142    if csmlfeaturetype == "PointFeature":
143        #NOT YET IMPLEMENTED#
144        if mapping is None:
145            mapping = 'onetoone' #default filemapping for PointFeature (?)
146           
147    elif csmlfeaturetype == "ProfileFeature":
148        #NOT YET IMPLEMENTED#
149        if mapping is None:
150            mapping = 'onetoone' #default filemapping for ProfileFeature (?)
151           
152    elif csmlfeaturetype == "GridFeature":
153            #NOT YET IMPLEMENTED#
154        if mapping is None:
155            mapping = 'onetoone' #default filemapping for GridFeature
156   
157    elif csmlfeaturetype == "PointSeriesFeature":
158        if mapping is None:
159            mapping = 'onetomany'
160   
161    elif csmlfeaturetype == "ProfileSeriesFeature":
162        if mapping is None:     #NOT YET IMPLEMENTED#
163            mapping = 'onetomany' #default filemapping for ProfileSeriesFeature (?)
164           
165    elif csmlfeaturetype == "GridSeriesFeature":
166        if mapping is None:
167            mapping = 'onetomany' #default filemapping for GridSeriesFeature
168
169    csmldataset=csmllibs.csmlbuilder.csmlBuilder(directory,csmlfeaturetype,mapping,timedimension, outputfile, printscreen)
170    print 'building csml... with %s mapping '%mapping
171    csmldataset.build()
172   
173   
174    print ''
175    print '********************************************************************'
176    print 'CSML file is at: ' + outputfile
177    print '********************************************************************'               
178    print ''
179
180
181if __name__=='__main__':
182    main()
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
Note: See TracBrowser for help on using the repository browser.