source: TI02-CSML/trunk/csml/Examples/Parsing/demoCreateCSML.py @ 2049

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/csml/Examples/Parsing/demoCreateCSML.py@2049
Revision 2049, 10.7 KB checked in by domlowe, 14 years ago (diff)

adding sample code - not yet revised for V2

Line 
1"""Test for GML/CSML parsing code
2"""
3
4from cElementTree import *
5import csml.parser
6import csml.parser_extra
7import elementtree.ElementTree as etree
8
9
10
11#this is a fix to the  ElementTree namespace problem that namespaces are usually represented as ns0, ns1, ns2 etc.
12etree._namespace_map.update({
13    'http://www.opengis.net/om': 'om',  'http://www.opengis.net/gml': 'gml','http://ndg.nerc.ac.uk/csml' : 'csml', 'http://www.w3.org/1999/xlink':'xlink'})
14
15
16def removeInlineNS(csmlstring):
17    # removeInlineNS: function removes "inline" namespaces and declares them as part of the Dataset element.
18    #remove all
19
20    csmlstring=csmlstring.replace('xmlns:gml="http://www.opengis.net/gml"','')
21    csmlstring=csmlstring.replace(' xmlns:csml="http://ndg.nerc.ac.uk/csml"','')
22    csmlstring=csmlstring.replace(' xmlns:xlink="http://www.w3.org/1999/xlink"','')
23    csmlstring=csmlstring.replace('xmlns:om="http://www.opengis.net/om"','')
24    csmlstring=csmlstring.replace('xsi:schemaLocation="http://ndg.nerc.ac.uk/csml/XMLSchemas/CSMLAppSchema.xsd"','')
25    csmlstring=csmlstring.replace('xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"','')
26   
27    #remove cmsl: prefixes
28    csmlstring=csmlstring.replace('<csml:','<')
29    csmlstring=csmlstring.replace('</csml:','</')
30    #add namespace declarations at top of document, after gml namespace declaration
31    csmlstring=csmlstring.replace('<Dataset', '<Dataset xmlns:gml="http://www.opengis.net/gml" xmlns:om="http://www.opengis.net/om" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://ndg.nerc.ac.uk/csml/XMLSchemas/CSMLAppSchema.xsd">',1)
32    return csmlstring
33
34
35#Test starts here:
36
37  #########################################
38## Test construction of CSML objects from scratch ##
39  #########################################
40
41#MetaDataProperty Object
42md = csml.parser.MetaDataProperty()
43setattr(md, 'href', 'http://ndg.nerc.ac.uk/Metadata/home/users/domlowe/CSML/demodata')
44
45
46
47###########################Unit Definitions #######################
48#empty list to hold definitionMembers
49dm=[]
50#UnitDefinition (s)
51ud = csml.parser.UnitDefinition()
52ud.description=csml.parser.Description('Conventional practical salinity units')
53ud.name = csml.parser.Name(['practical salinity units'])
54ud.quantityType='sea water salinity'
55ud.catalogSymbol=csml.parser.CodeType( 'psu', 'http://ndg.nerc.ac.uk/units')
56dm.append(ud)
57
58uds = csml.parser.UnitDefinitions(definitionMembers=dm)
59###############################################################
60
61
62###############Reference System Definitions #######################
63#To Do:
64###############################################################
65
66##################Local Phenomenon Definitions ####################
67#empty list  to hold definitionMembers
68dm = []
69ph =csml.parser.Phenomenon()
70ph.id='rainfall'
71dm.append(ph)
72ph =csml.parser.Phenomenon()
73ph.id='another Phenomenon name'
74dm.append(ph)
75pds = csml.parser.PhenomenonDefinitions(definitionMembers=dm)
76###############################################################
77
78
79
80####################ArrayDescriptors#############################
81#An empty list to hold a collection of ArrayDescriptors (eg NetCDFExtracts, AggregatedArrays)
82
83#Note this code uses the setattr function to set attributes, but they could equally be set using the form: object.attributeToSet = attributeValue (as used above).
84
85adlist = []
86
87#An empty list to hold a collection of NASAAmesExtracts:
88nalist=[]
89
90#Create a NASAAmesExtract
91na1 = csml.parser.NASAAmesExtract()
92#set attributes:
93setattr(na1, 'id', 'naextract_one')
94setattr(na1, 'variableName', 'temp')
95setattr(na1, 'index', '1')
96setattr(na1, 'fileName', '/blah/testfile.na')
97setattr(na1, 'arraySize', [50])
98setattr(na1, 'uom', 'celsius')
99#nalist.append(na1)
100adlist.append(na1)
101
102#Create a second NASAAmesExtract
103na2 = csml.parser.NASAAmesExtract()
104#set attributes:
105setattr(na2, 'id', 'naextract_two')
106setattr(na2, 'variableName', 'pressure')
107setattr(na2, 'index', '1')
108setattr(na2, 'fileName', '/blah/testfile2.na')
109setattr(na2, 'arraySize', [50])
110setattr(na2, 'uom', 'mb')
111#nalist.append(na2)
112adlist.append(na2)
113
114################### NetCDFExtracts ##########################
115
116#An empty list to hold a collection of NetCDFExtracts:
117nclist=[]
118
119#Create a NetCDFExtract
120nc = csml.parser.NetCDFExtract()
121#set attributes:
122setattr(nc, 'id', 'feat04times')
123setattr(nc, 'variableName', 'time')
124setattr(nc, 'fileName', 'radar_data.nc')
125setattr(nc, 'arraySize', [10000])
126#nclist.append(nc)
127adlist.append(nc)
128
129#Create another NetCDFExtract
130nc = csml.parser.NetCDFExtract()
131#set attributes:
132setattr(nc, 'id', 'feat04azimuth')
133setattr(nc, 'variableName', 'feat04azimuth')
134setattr(nc, 'fileName', 'radar_data.nc')
135setattr(nc, 'arraySize', [10000])
136#nclist.append(nc)
137adlist.append(nc)
138
139#Create another NetCDFExtract
140nc = csml.parser.NetCDFExtract()
141#set attributes:
142setattr(nc, 'id', 'feat05times')
143setattr(nc, 'variableName', 'atime')
144setattr(nc, 'fileName', 'cruisetrack.nc')
145setattr(nc, 'arraySize', [50])
146#nclist.append(nc)
147adlist.append(nc)
148#############################################################
149
150
151################### AggregatedArrays ##########################
152
153#An empty list to hold a collection of AggregatedArrays:
154aalist=[]
155#Create an AggregatedArray
156aa = csml.parser.AggregatedArray()
157nc1 = csml.parser.NetCDFExtract()
158nc2 = csml.parser.NetCDFExtract()
159#set attributes for AggregatedArray
160setattr(aa, 'id', 'feat05cruisetrack')
161setattr(aa, 'arraySize', [2,50])
162setattr(aa, 'aggType', 'new')
163setattr(aa, 'aggIndex', 1)
164#set attributes for component extracts:
165setattr(nc1, 'variableName', 'alat')
166setattr(nc1, 'fileName', 'cruisetrack.nc')
167setattr(nc1, 'arraySize', [50])
168setattr(nc2, 'variableName', 'alon')
169setattr(nc2, 'fileName', 'cruisetrack.nc')
170setattr(nc2, 'arraySize', [50])
171
172aa.component=[]
173aa.component.append(nc1)
174aa.component.append(nc2)
175#aalist.append(aa)
176adlist.append(aa)
177
178#############################################################
179
180
181################### ArrayGenerator ############################
182#Create ArrayGenerator
183ag =csml.parser.ArrayGenerator()
184#set attributes:
185setattr(ag, 'id', 'feat05depths')
186setattr(ag, 'arraySize', [3001])
187setattr(ag, 'expression', '0:2:6000')
188#nclist.append(nc)
189adlist.append(ag)
190######################################################
191
192#################### FeatureCollection #######################
193
194#empty list to hold featureMembers
195fms =[]
196
197
198#### PointFeature: #####
199ptf=csml.parser.PointFeature()
200ptf.id='feat01'
201ptf.description=csml.parser.Description('Temperature reading from thermometer')
202ptd=csml.parser.PointDomain()
203p=csml.parser.Position()
204p.srsName ='urn:EPSG:geographicCRS:4979'
205p.axisLabels='Lat Long h'
206p.uomLabels = 'degree degree m'
207p.location='0.1 1.5 25'
208p.time='2000-08-13T13:51:10'
209ptd.domainReference=p
210ptf.domain=ptd
211rs=csml.parser.RangeSet()
212rs.quantityList=csml.parser.MeasureOrNullList(uom='udunits.xml#degreesC',val = '10')
213ptf.rangeSet=rs
214ptf.parameter=csml.parser.Phenomenon(href='CFStandardNames.xml#air_temperature')  #TO FIX
215fms.append(ptf)
216
217#### PointSeriesFeature: #####
218ptsf=csml.parser.PointSeriesFeature()
219ptsf.id='feat02'
220ptsf.description=csml.parser.Description('January timeseries of raingauge measurements')
221ptsd=csml.parser.PointSeriesDomain()
222t=csml.parser.Trajectory()
223t.srsName='urn:EPSG:geographicCRS:4979'
224t.locations=csml.parser.DirectPositionList(vals='0.1 1.5 25')
225t.times=csml.parser.TimePositionList('#RefSys01','0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2 26 27 28 29 30 31')
226ptsd.domainReference=t
227ptsf.domain=ptsd
228rs=csml.parser.RangeSet()
229rs.quantityList=csml.parser.MeasureOrNullList('udunits.xml#mm', '5 3 10 1 2 8 10 2 5 10 20 21 12 3 5 19 12 23 32 10 8 8 2 0 0 1 5 6 10 17 20')
230ptsf.rangeSet=rs
231fms.append(ptsf)
232
233#### ProfileFeature: #####
234prf=csml.parser.ProfileFeature()
235prf.id='feat03'
236prf.description=csml.parser.Description('Vertical wind profile')
237prd=csml.parser.ProfileDomain()
238op=csml.parser.OrientedPosition(srsName='urn:EPSG:geographicCRS:4326')
239op.location='-1 3'
240op.time='1999-07-21T10:00:00'
241horiz=csml.parser.AngleList('udunits.xml#degrees','0')
242vert=csml.parser.AngleList('udunits.xml#degrees','90')
243direct=csml.parser.DirectionVectorList(horiz,vert)
244op.direction=direct
245prd.domainReference=op
246dpl =csml.parser.DirectPositionList(srsName='#RefSys02',vals='10 20 30 40 50 60 70 80 90 100')
247prd.domainComplement=dpl
248prf.domain=prd
249rs=csml.parser.RangeSet()
250valueComps=[]
251vc1=csml.parser.Measure('udunits.xml#ms-1')
252valueComps.append(vc1)
253vc2 =csml.parser.Measure('udunits.xml#ms-1')
254valueComps.append(vc2)
255cv=csml.parser.CompositeValue(valueComponents=valueComps)
256db=csml.parser.DataBlock(rangeParameters=cv, doubleOrNullTupleList='2.0 1.1 2.4 0.8 3.3 0.1 2.6 -0.2 5.6 0.1 4.5 1.3 6.3 0.1 5.7 0.9 4.2 1.1 3.2 -0.1')
257rs.dataBlock=db
258prf.rangeSet=rs
259cp=csml.parser.CompositePhenomenon()
260cp.id='wind'
261cp.name = csml.parser.Name(['Vector Wind'])
262components=[]
263component=csml.parser.Phenomenon(href='CFStandardNames.xml#eastward_wind')
264components.append(component)
265component=csml.parser.Phenomenon(href='CFStandardNames.xml#northward_wind')
266components.append(component)
267cp.componentPhenomena=components
268prf.parameter=cp
269fms.append(prf)
270
271#### ProfileSeriesFeature: (not-complete)#####
272psf=csml.parser.ProfileSeriesFeature()
273psf.id='feat05'
274psf.description=csml.parser.Description('Cruise CTD salinity')
275psd =csml.parser.ProfileSeriesDomain()
276ot=csml.parser.OrientedTrajectory()
277ot.srsName='urn:EPSG:geographicCRS:4326'
278psd.domainReference=ot
279psf.domain=psd
280fms.append(psf)
281#### GridFeature: #####
282#### GridSeriesFeature: #####
283#### TrajectoryFeature ######
284
285#instantiate FeatureCollection object:
286fc=csml.parser.FeatureCollection(members=fms)
287etp = csml.parser.EnvelopeWithTimePeriod()
288etp.lowerCorner=csml.parser.DirectPosition(vals='-10 15')
289etp.upperCorner=csml.parser.DirectPosition(vals='30 65')
290etp.timePosition='1998-01-01'
291etp.timePosition2='2003-12-31'
292fc.boundedBy=etp
293
294
295
296########### The Dataset  ##############
297#Create an Empty Dataset
298ds = csml.parser.Dataset()
299#Set objects as attributes of dataset
300setattr(ds,'id','TestDataSet')
301setattr(ds, 'metaDataProperty', [md])
302setattr(ds, 'arrayDescriptors', adlist)
303setattr(ds, 'unitDefinitions', uds)
304setattr(ds, 'phenomenonDefinitions', pds)
305setattr(ds, 'featureCollection',fc)
306########################################
307
308#call the toXML method of the Dataset object:
309csmldoc = ds.toXML()
310#parse and pretty print the result
311strCSML=csml.parser_extra.PrettyPrint(csmldoc)
312#tidy up elementtree style namespaces
313strCSML=csml.parser_extra.removeInlineNS(strCSML)
314#strCSML contains the csml doc.
315print strCSML
Note: See TracBrowser for help on using the repository browser.