source: TI02-CSML/trunk/csml/Examples/Parsing/demoCreateV2.py @ 2400

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

fixes to demoCreateV2.py and associated parser fixes

Line 
1import csml.parser
2import csml.parser_extra
3import csml.csmllibs.csmlextra
4
5  #########################################
6    ##  construction of CSML objects ##
7  #########################################
8
9
10# create an empty list to hold featureMembers
11fms =[]
12
13
14#### create a PointSeriesFeature: #####
15ptsf=csml.parser.PointSeriesFeature()
16
17#set the id attribute
18ptsf.id='pointseriesf'
19
20#set the description
21#note, as description is an xml element not an attribute it is of type csString:
22ptsf.description=csml.parser.csString('Station BLUEBIRD')
23
24#create a PointSeriesCoverage
25ptscvg=csml.parser.PointSeriesCoverage()
26ptscvg.id=csml.csmllibs.csmlextra.getRandomID()
27
28#create a TimeSeriesDomain (appropriate domain for a PointSeriesFeature
29ptsd=csml.parser.TimeSeries()
30ptsd.id=csml.csmllibs.csmlextra.getRandomID()
31#create (and populate) a TimePositionList. Using keyword arguements for conciseness.
32ptsd.timePositionList=csml.parser.TimePositionList(frame='#pred20060427001',CONTENT='-18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60')
33   
34#create a RangeSet
35rs=csml.parser.RangeSet()
36#set the quantityList attribute of the RangeSet to be a MeasureOrNullList containing the values for the rangeSet
37rs.quantityList=csml.parser.MeasureOrNullList(uom='MLUnits.xml#m',CONTENT='0.27 0.25 0.25 0.23 0.22 0.22 0.21 0.17 0.17 0.14 0.14 0.12 0.10 0.08 0.08 0.08 0.11 0.13 0.11 0.11 0.13 0.10 0.06 0.06 0.06 0.01 -0.03 -0.06 -0.09 -0.11 -0.11 -0.13 -0.16 -0.18 -0.17 -0.16 -0.18 -0.16 -0.12 -0.09 -0.08 -0.07 -0.06 -0.05 -0.04 -0.03 -0.03 -0.04 -0.02 -0.01 -0.02 -0.02 -0.02 -0.03 -0.03 -0.02 -0.01 -0.01 -0.02 -0.02 -0.03 -0.04 -0.04 -0.05 -0.04 -0.05 -0.08 -0.09 -0.11 -0.12 -0.12 -0.12 -0.12 -0.12 -0.13 -0.14 -0.15')
38
39
40#Add the domain and rangeSet as attributes of the coverage
41ptscvg.pointSeriesDomain=ptsd
42ptscvg.rangeSet=rs
43
44#set the coverage to be the 'value' of the feature
45ptsf.value=ptscvg
46
47
48#the parameter of the feature is of type Phenomenon, here href creates "xlink:href=..."
49param=csml.parser.Phenomenon(href='CFStandardNames.xml#temperature')
50
51#set the parameter of the feature to be the parameter just defined
52ptsf.parameter=param
53
54#append the feature to the list of featureMembers
55fms.append(ptsf)
56
57
58
59#create a profile series feature
60            #commented out, was just an approximate template
61#psf=csml.parser.ProfileSeriesFeature()
62#psf.id='profileseriesf'
63#psf.description=csml.parser.csString('sample profile series feature')
64#cvg = csml.parser.ProfileSeriesCoverage()
65#cvg.id=csml.csmllibs.csmlextra.getRandomID()           
66#psd=csml.parser.ProfileSeriesDomain()
67#psd.id=csml.csmllibs.csmlextra.getRandomID()
68#rs=csml.parser.RangeSet()
69#gcT=csml.parser.GridCoordinatesTable()
70#psd.coordTransformTable=gcT
71#cvg.rangeSet=rs
72#cvg.profileSeriesDomain=psd
73#psf.value=cvg
74#psf.parameter=param #defined earlier
75#fms.append(psf)
76           
77                       
78#create a profile feature
79pf=csml.parser.ProfileFeature()           
80pf.id='profilef'                       
81pf.description=csml.parser.csString('sample profile feature')           
82cvg = csml.parser.ProfileCoverage()
83cvg.id=csml.csmllibs.csmlextra.getRandomID()
84pd =csml.parser.ProfileDomain()
85pd.id=csml.csmllibs.csmlextra.getRandomID()
86pd.pointMembers=csml.parser.csString('1000, 950, 850, 700, 600, 500, 400, 300, 250, 200, 150, 100, 50, 30, 10')
87cvg.profileDomain=pd
88rs=csml.parser.RangeSet()
89rs.quantityList=csml.parser.MeasureOrNullList(uom='MLUnits.xml#m',CONTENT='0.27 0.25 0.25 0.23 0.22 0.22 0.21 0.17 0.17 0.14 0.14 0.12 0.10 0.08 0.08')
90cvg.rangeSet=rs
91pf.value=cvg
92pf.parameter=param
93pf.location=csml.parser.csString('75 35')
94pf.time=csml.parser.csString('2003-02-02T00:00:00.0')
95fms.append(pf)           
96                       
97                       
98#instantiate a FeatureCollection object (and add all featureMembers)
99fc=csml.parser.CSMLFeatureCollection(featureMembers=fms)
100fc.id=csml.csmllibs.csmlextra.getRandomID()
101
102########### The Dataset  ##############
103#Create an Empty Dataset
104ds = csml.parser.Dataset()
105#set the xml attribute 'id'
106ds.id='Test001'
107
108#set the description element
109ds.description=csml.parser.csString('This is a test Water level.Period 26/04/06 06h -> 29/04/06 12h')
110
111#set the metaDataProperty(s)
112#if there is only one metaDataProperty you can just do:
113#ds.metaDataProperty=csml.parser.csString('Data (c) 2007 CCLRC // www.cclrc.ac.uk')
114
115#but if there are several you need to put them in a list:
116mdplist=[]
117mdp1=csml.parser.csString('Data (c) 2007 CCLRC // www.cclrc.ac.uk')
118mdp2=csml.parser.csString('Another CCLRC MetaDataProperty')
119mdplist.append(mdp1)
120mdplist.append(mdp2)
121ds.metaDataProperty=mdplist
122
123#now add the featureCollection to the dataset
124ds.featureCollection=fc
125                   
126                                     
127#dummy storage descriptor
128sd=csml.parser.CSMLStorageDescriptor()
129sd.id = csml.csmllibs.csmlextra.getRandomID()
130ds.storageDescriptor=sd
131
132                                     
133
134#finished creating all the python objects!
135########################################
136
137####### Generating XML #################
138           
139#call the toXML method of the Dataset object:
140csmldoc = ds.toXML()
141#parse and pretty print the result
142strCSML=csml.parser_extra.PrettyPrint(csmldoc)
143strCSML=csml.parser_extra.removeInlineNS(strCSML)
144print strCSML
145
146#(you could save strCSML to a file at this point)
147fname='ptseries.xml'
148f=open(fname,'w')
149f.write(strCSML)
150print 'CSML doc written to %s'%fname
151                                     
Note: See TracBrowser for help on using the repository browser.