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

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

major rewrite of Scanner to employ Parser module. (not complete)

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