source: TI02-CSML/trunk/newparser/demoCreateCSML.py @ 683

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

demoCreateCSML.py - example of creating CSML from scratch using parser

Line 
1"""Test for GML/CSML parsing code
2"""
3
4from cElementTree import *
5from Parser import *
6from xml.dom.minidom import parseString
7import elementtree.ElementTree as etree
8import sys
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
36
37  #########################################
38## Test construction of CSML objects from scratch ##
39  #########################################
40
41#MetaDataProperty Object
42md = 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 = UnitDefinition()
52ud.description=Description('Conventional practical salinity units')
53ud.name = Name(['practical salinity units'])
54ud.quantityType='sea water salinity'
55ud.catalogSymbol=CodeType( 'psu', 'http://ndg.nerc.ac.uk/units')
56dm.append(ud)
57
58uds = 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 =Phenomenon()
70ph.id='rainfall'
71dm.append(ph)
72pds = PhenomenonDefinitions(definitionMembers=dm)
73###############################################################
74
75#################### FeatureCollection ############################
76
77#empty list to hold featureMembers
78fms =[]
79pf=PointFeature()
80pf.id='feat01'
81pf.description=Description('Temperature reading from thermometer')
82pd=PointDomain()
83p=Position()
84p.srsName ='urn:EPSG:geographicCRS:4979'
85p.axisLabels='Lat Long h'
86p.uomLabels = 'degree degree m'
87p.location='0.1 1.5 25'
88p.time='2000-08-13T13:51:10'
89pd.domainReference=p
90pf.pointDomain=pd
91rs=RangeSet()
92#TODO populate rangeSet
93pf.rangeSet=rs
94
95fms.append(pf)
96
97
98fc=FeatureCollection(members=fms)
99etp = EnvelopeWithTimePeriod()
100etp.lowerCorner='-10 15'
101etp.upperCorner='30 65'
102etp.timePosition='1998-01-01'
103etp.timePosition2='2003-12-31'
104fc.boundedBy=etp
105
106
107
108####################ArrayDescriptors#############################
109#An empty list to hold a collection of ArrayDescriptors (eg NetCDFExtracts, AggregatedArrays)
110
111#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).
112
113adlist = []
114
115#An empty list to hold a collection of NASAAmesExtracts:
116nalist=[]
117
118#Create a NASAAmesExtract
119na1 = NASAAmesExtract()
120#set attributes:
121setattr(na1, 'id', 'naextract_one')
122setattr(na1, 'variableName', 'temp')
123setattr(na1, 'index', '1')
124setattr(na1, 'fileName', '/blah/testfile.na')
125setattr(na1, 'arraySize', [50])
126setattr(na1, 'uom', 'celsius')
127#nalist.append(na1)
128adlist.append(na1)
129
130#Create a second NASAAmesExtract
131na2 = NASAAmesExtract()
132#set attributes:
133setattr(na2, 'id', 'naextract_two')
134setattr(na2, 'variableName', 'pressure')
135setattr(na2, 'index', '1')
136setattr(na2, 'fileName', '/blah/testfile2.na')
137setattr(na2, 'arraySize', [50])
138setattr(na2, 'uom', 'mb')
139#nalist.append(na2)
140adlist.append(na2)
141
142################### NetCDFExtracts ##########################
143
144#An empty list to hold a collection of NetCDFExtracts:
145nclist=[]
146
147#Create a NetCDFExtract
148nc = NetCDFExtract()
149#set attributes:
150setattr(nc, 'id', 'feat04times')
151setattr(nc, 'variableName', 'time')
152setattr(nc, 'fileName', 'radar_data.nc')
153setattr(nc, 'arraySize', [10000])
154#nclist.append(nc)
155adlist.append(nc)
156
157#Create another NetCDFExtract
158nc = NetCDFExtract()
159#set attributes:
160setattr(nc, 'id', 'feat04azimuth')
161setattr(nc, 'variableName', 'feat04azimuth')
162setattr(nc, 'fileName', 'radar_data.nc')
163setattr(nc, 'arraySize', [10000])
164#nclist.append(nc)
165adlist.append(nc)
166
167#Create another NetCDFExtract
168nc = NetCDFExtract()
169#set attributes:
170setattr(nc, 'id', 'feat05times')
171setattr(nc, 'variableName', 'atime')
172setattr(nc, 'fileName', 'cruisetrack.nc')
173setattr(nc, 'arraySize', [50])
174#nclist.append(nc)
175adlist.append(nc)
176#############################################################
177
178
179################### AggregatedArrays ##########################
180
181#An empty list to hold a collection of AggregatedArrays:
182aalist=[]
183#Create an AggregatedArray
184aa = AggregatedArray()
185nc1 = NetCDFExtract()
186nc2 = NetCDFExtract()
187#set attributes for AggregatedArray
188setattr(aa, 'id', 'feat05cruisetrack')
189setattr(aa, 'arraySize', [2,50])
190setattr(aa, 'aggType', 'new')
191setattr(aa, 'aggIndex', 1)
192#set attributes for component extracts:
193setattr(nc1, 'variableName', 'alat')
194setattr(nc1, 'fileName', 'cruisetrack.nc')
195setattr(nc1, 'arraySize', [50])
196setattr(nc2, 'variableName', 'alon')
197setattr(nc2, 'fileName', 'cruisetrack.nc')
198setattr(nc2, 'arraySize', [50])
199
200aa.component=[]
201aa.component.append(nc1)
202aa.component.append(nc2)
203#aalist.append(aa)
204adlist.append(aa)
205
206#############################################################
207
208
209################### ArrayGenerator ############################
210#Create ArrayGenerator
211ag =ArrayGenerator()
212#set attributes:
213setattr(ag, 'id', 'feat05depths')
214setattr(ag, 'arraySize', [3001])
215setattr(ag, 'expression', '0:2:6000')
216#nclist.append(nc)
217adlist.append(ag)
218
219
220######################## The Dataset ##########################
221#Create an Empty Dataset
222ds = Dataset()
223#Set objects as attributes of dataset
224setattr(ds,'id','TestDataSet')
225setattr(ds, 'metaDataProperty', [md])
226setattr(ds, 'arrayDescriptors', adlist)
227setattr(ds, 'unitDefinitions', uds)
228setattr(ds, 'phenomenonDefinitions', pds)
229setattr(ds, 'featureCollection',fc)
230#############################################################
231
232csml = ds.toXML()
233strCSML= parseString(tostring(csml)).toprettyxml()
234strCSML=removeInlineNS(strCSML)
235print strCSML
Note: See TracBrowser for help on using the repository browser.