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