1 | import csml.parser |
---|
2 | import csml.parser_extra |
---|
3 | |
---|
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='testbed270401' |
---|
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 | #create a PointSeriesCoverage |
---|
25 | ptscvg=csml.parser.PointSeriesCoverage() |
---|
26 | |
---|
27 | #create a TimeSeriesDomain (appropriate domain for a PointSeriesFeature |
---|
28 | ptsd=csml.parser.TimeSeries() |
---|
29 | #create (and populate) a TimePositionList. Using keyword arguements for conciseness. |
---|
30 | ptsd.time=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') |
---|
31 | |
---|
32 | #create a RangeSet |
---|
33 | rs=csml.parser.RangeSet() |
---|
34 | #set the quantityList attribute of the RangeSet to be a MeasureOrNullList containing the values for the rangeSet |
---|
35 | 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') |
---|
36 | |
---|
37 | |
---|
38 | #Add the domain and rangeSet as attributes of the coverage |
---|
39 | ptscvg.pointSeriesDomain=ptsd |
---|
40 | ptscvg.rangeSet=rs |
---|
41 | |
---|
42 | #set the coverage to be the 'value' of the feature |
---|
43 | ptsf.value=ptscvg |
---|
44 | |
---|
45 | |
---|
46 | #the parameter of the feature is of type Phenomenon, here href creates "xlink:href=..." |
---|
47 | param=csml.parser.Phenomenon(href='CFStandardNames.xml#temperature') |
---|
48 | |
---|
49 | #set the parameter of the feature to be the parameter just defined |
---|
50 | ptsf.parameter=param |
---|
51 | |
---|
52 | #append the feature to the list of featureMembers |
---|
53 | fms.append(ptsf) |
---|
54 | |
---|
55 | |
---|
56 | |
---|
57 | #create a profile series feature |
---|
58 | psf=csml.parser.ProfileSeriesFeature() |
---|
59 | psf.id='profilef' |
---|
60 | psf.description=csml.parser.csString('sample profile series feature') |
---|
61 | cvg = csml.parser.ProfileSeriesCoverage() |
---|
62 | psd=csml.parser.ProfileSeriesDomain() |
---|
63 | rs=csml.parser.RangeSet() |
---|
64 | gcT=csml.parser.GridCoordinatesTable() |
---|
65 | psd.coordTransformTable=gcT |
---|
66 | cvg.profileSeriesDomain=psd |
---|
67 | psf.value=cvg |
---|
68 | fms.append(psf) |
---|
69 | |
---|
70 | |
---|
71 | #instantiate a FeatureCollection object (and add all featureMembers) |
---|
72 | fc=csml.parser.CSMLFeatureCollection(featureMembers=fms) |
---|
73 | |
---|
74 | |
---|
75 | ########### The Dataset ############## |
---|
76 | #Create an Empty Dataset |
---|
77 | ds = csml.parser.Dataset() |
---|
78 | #set the xml attribute 'id' |
---|
79 | ds.id='Test001' |
---|
80 | |
---|
81 | #set the description element |
---|
82 | ds.description=csml.parser.csString('This is a test Water level.Period 26/04/06 06h -> 29/04/06 12h') |
---|
83 | |
---|
84 | #set the metaDataProperty(s) |
---|
85 | #if there is only one metaDataProperty you can just do: |
---|
86 | #ds.metaDataProperty=csml.parser.csString('Data (c) 2007 CCLRC // www.cclrc.ac.uk') |
---|
87 | |
---|
88 | #but if there are several you need to put them in a list: |
---|
89 | mdplist=[] |
---|
90 | mdp1=csml.parser.csString('Data (c) 2007 CCLRC // www.cclrc.ac.uk') |
---|
91 | mdp2=csml.parser.csString('Another CCLRC MetaDataProperty') |
---|
92 | mdplist.append(mdp1) |
---|
93 | mdplist.append(mdp2) |
---|
94 | ds.metaDataProperty=mdplist |
---|
95 | |
---|
96 | #now add the featureCollection to the dataset |
---|
97 | ds.featureCollection=fc |
---|
98 | |
---|
99 | #finished creating all the python objects! |
---|
100 | ######################################## |
---|
101 | |
---|
102 | ####### Generating XML ################# |
---|
103 | |
---|
104 | #call the toXML method of the Dataset object: |
---|
105 | csmldoc = ds.toXML() |
---|
106 | #parse and pretty print the result |
---|
107 | strCSML=csml.parser_extra.PrettyPrint(csmldoc) |
---|
108 | strCSML=csml.parser_extra.removeInlineNS(strCSML) |
---|
109 | print strCSML |
---|
110 | |
---|
111 | #(you could save strCSML to a file at this point) |
---|