1 | import cElementTree as ET |
---|
2 | import elementtree.ElementTree as etree |
---|
3 | import parser_extra |
---|
4 | import sys |
---|
5 | '''CSML v2 Parser ''' |
---|
6 | |
---|
7 | #this map needs updating for V2 |
---|
8 | etree._namespace_map.update({'http://www.opengis.net/om': 'om', 'http://www.opengis.net/swe': 'swe', 'http://www.opengis.net/gml': 'gml','http://ndg.nerc.ac.uk/csml' : 'csml', 'http://www.w3.org/1999/xlink':'xlink'}) |
---|
9 | |
---|
10 | nsCSML = 'http://ndg.nerc.ac.uk/csml' |
---|
11 | nsGML = 'http://www.opengis.net/gml' |
---|
12 | nsOM = 'http://www.opengis.net/om' |
---|
13 | nsXLINK = 'http://www.w3.org/1999/xlink' |
---|
14 | nsXML = 'http://ndg.nerc.ac.uk/csml' |
---|
15 | nsMOLES='http://ndg.nerc.ac.uk/moles' |
---|
16 | nsSWE='http://www.opengis.net/swe' |
---|
17 | |
---|
18 | def myQName(uri,tag): |
---|
19 | return "{"+uri+"}"+tag |
---|
20 | |
---|
21 | def CSML(tag): |
---|
22 | return myQName(nsCSML,tag) |
---|
23 | |
---|
24 | def GML(tag): |
---|
25 | return myQName(nsGML,tag) |
---|
26 | |
---|
27 | def SWE(tag): |
---|
28 | return myQName(nsSWE,tag) |
---|
29 | |
---|
30 | def XLINK(tag): |
---|
31 | return myQName(nsXLINK,tag) |
---|
32 | |
---|
33 | def addchildren(obj, dict): |
---|
34 | ''' merges (adds) dictionary to existing self.CHILDREN dictionary ''' |
---|
35 | dict1 = dict |
---|
36 | if hasattr(obj, 'CHILDREN'): |
---|
37 | dict2=obj.CHILDREN |
---|
38 | else: |
---|
39 | dict2={} |
---|
40 | dict3={} |
---|
41 | for item in dict1: |
---|
42 | dict3[item] =dict1[item] |
---|
43 | for item in dict2: |
---|
44 | dict3[item] =dict2[item] |
---|
45 | obj.CHILDREN=dict3 |
---|
46 | |
---|
47 | |
---|
48 | def addatts(obj, atts): |
---|
49 | ''' merges self.ATTRIBUTES''' |
---|
50 | if hasattr(obj, 'ATTRIBUTES'): |
---|
51 | for att in atts: |
---|
52 | obj.ATTRIBUTES.append(att) |
---|
53 | else: |
---|
54 | obj.ATTRIBUTES=atts |
---|
55 | |
---|
56 | |
---|
57 | #Some variable definitions: these things are often repeated so store in variables. |
---|
58 | FILEFORMATS=[CSML('NetCDFExtract'),CSML('NASAAmesExtract'), CSML('GRIBExtract'),CSML('CDMLExtract'), CSML('RawFileExtract')] |
---|
59 | |
---|
60 | |
---|
61 | class csElement(object): |
---|
62 | ''' main csElement class - all other elements inherit from this baseclass |
---|
63 | all the from/to XML conversion is done by this class''' |
---|
64 | |
---|
65 | def __init__(self, **kwargs): |
---|
66 | if not hasattr(self, 'ATTRIBUTES'): |
---|
67 | self.__dict__['ATTRIBUTES']=[] |
---|
68 | |
---|
69 | #The __setattr__ and __getattribute__ special methods have been overridden. |
---|
70 | #This is so that attributes can be called by their fully qualified name internally |
---|
71 | #while being referenced by their short name externally. |
---|
72 | # so 'id' and {'http://www.opengis.net/gml'}id always have the same value |
---|
73 | #you can set either of them at any time and the other will be kept in sync. |
---|
74 | |
---|
75 | def __setattr__(self, name, value): |
---|
76 | if hasattr(self, CSML(name)): |
---|
77 | object.__setattr__(self, CSML(name), value) |
---|
78 | object.__setattr__(self, name, value) |
---|
79 | elif hasattr(self,GML(name)): |
---|
80 | object.__setattr__(self, GML(name), value) |
---|
81 | object.__setattr__(self, name, value) |
---|
82 | else: |
---|
83 | if hasattr(self, 'ATTRIBUTES'): |
---|
84 | if CSML(name) in self.__dict__['ATTRIBUTES']: |
---|
85 | object.__setattr__(self, CSML(name), value) |
---|
86 | object.__setattr__(self, name, value) |
---|
87 | elif GML(name) in self.__dict__['ATTRIBUTES']: |
---|
88 | object.__setattr__(self, GML(name), value) |
---|
89 | object.__setattr__(self, name, value) |
---|
90 | elif name in self.__dict__['ATTRIBUTES']: |
---|
91 | object.__setattr__(self, name, value) |
---|
92 | try: |
---|
93 | name=name.split('}')[1] |
---|
94 | except IndexError: |
---|
95 | pass |
---|
96 | object.__setattr__(self, name, value) |
---|
97 | else: |
---|
98 | object.__setattr__(self, name, value) |
---|
99 | else: |
---|
100 | object.__setattr__(self, name, value) |
---|
101 | |
---|
102 | def __getattribute__(self, name): |
---|
103 | if CSML(name) in object.__dict__: |
---|
104 | return object.__getattribute__(self,CSML(name)) |
---|
105 | elif GML(name) in object.__dict__: |
---|
106 | return object.__getattribute__(self,GML(name)) |
---|
107 | else: |
---|
108 | return object.__getattribute__(self,name) |
---|
109 | |
---|
110 | def __removeURI(self, qname): |
---|
111 | try: |
---|
112 | attname = qname.split('}')[1] |
---|
113 | except IndexError: |
---|
114 | attname = qname |
---|
115 | return attname |
---|
116 | |
---|
117 | def _getSubstitutionType(self,tag): |
---|
118 | return tag.split('}')[1] |
---|
119 | |
---|
120 | |
---|
121 | def _getReverseSubsType(self, typename): |
---|
122 | return typename |
---|
123 | |
---|
124 | def addChildElem(self, childname, childobj): |
---|
125 | #sometimes you want to add a child element but don't know if there is one already. In which case you want to create a list of child objects. |
---|
126 | if hasattr(self, childname): |
---|
127 | currentattribute=getattr(self,childname) |
---|
128 | if type(getattr(self,childname)) is list: |
---|
129 | currentattribute.append(childobj) |
---|
130 | else: |
---|
131 | newlist=[currentattribute] |
---|
132 | newlist.append(childobj) |
---|
133 | setattr(self,childname, newlist) |
---|
134 | else: |
---|
135 | setattr(self,childname, childobj) |
---|
136 | |
---|
137 | def toXML(self, csmlfrag): |
---|
138 | #process self and convert to XML |
---|
139 | if hasattr(self, 'CONTENT'): |
---|
140 | if self.CONTENT is not None: csmlfrag.text=self.CONTENT |
---|
141 | if hasattr(self, 'ATTRIBUTES'): |
---|
142 | for item in self.__dict__: |
---|
143 | if item in self.ATTRIBUTES: |
---|
144 | csmlfrag.set(item, self.__dict__[item]) |
---|
145 | for item in self.__dict__: |
---|
146 | if GML(item) in self.ATTRIBUTES: |
---|
147 | csmlfrag.set(GML(item), self.__dict__[item]) |
---|
148 | # self.CHILDREN (recursive - calls the toXML method of children |
---|
149 | for att in self.__dict__: |
---|
150 | if att not in ['ATTRIBUTES', 'CHILDREN', 'CONTENT']: |
---|
151 | for child in self.CHILDREN: |
---|
152 | if child == att: |
---|
153 | parserobjects=[] |
---|
154 | if type(self.__dict__[att]) is list: |
---|
155 | for a in self.__dict__[att]: |
---|
156 | parserobjects.append(a) |
---|
157 | else: |
---|
158 | parserobjects.append(self.__dict__[att]) |
---|
159 | parentfrag=None |
---|
160 | if len(self.CHILDREN[child])>=3: |
---|
161 | ename2=self.CHILDREN[child][2] |
---|
162 | parentfrag=ET.Element(ename2) |
---|
163 | for po in parserobjects: |
---|
164 | if type(self.CHILDREN[child][0]) is not list: |
---|
165 | ename=self.CHILDREN[child][0] |
---|
166 | else: |
---|
167 | ename = self._getReverseSubsType(type(po).__name__) |
---|
168 | if len(self.CHILDREN[child])==3: |
---|
169 | frag=ET.Element(ename) |
---|
170 | po.toXML(frag) |
---|
171 | parentfrag.append(frag) |
---|
172 | appendLater=True |
---|
173 | elif len(self.CHILDREN[child])>=4: |
---|
174 | if self.CHILDREN[child][3]==1: |
---|
175 | frag=ET.Element(ename) |
---|
176 | po.toXML(frag) |
---|
177 | parentfrag.append(frag) |
---|
178 | csmlfrag.append(parentfrag) |
---|
179 | parentfrag=ET.Element(parentfrag.tag) |
---|
180 | appendLater=False |
---|
181 | else: |
---|
182 | frag=ET.Element(ename) |
---|
183 | po.toXML(frag) |
---|
184 | csmlfrag.append(frag) |
---|
185 | appendLater=True |
---|
186 | if appendLater==True and parentfrag != None: |
---|
187 | csmlfrag.append(parentfrag) |
---|
188 | return csmlfrag |
---|
189 | |
---|
190 | def fromXML(self,csmlfrag): |
---|
191 | # deal with attributes, e.g. gml id's |
---|
192 | if csmlfrag.text is not None: |
---|
193 | self.CONTENT = csmlfrag.text |
---|
194 | for item in csmlfrag.items(): |
---|
195 | if item[0] in self.ATTRIBUTES: |
---|
196 | setattr(self, item[0], item[1]) |
---|
197 | # self.CHILDREN (recursive - calls the fromXML method of children |
---|
198 | for frag in csmlfrag[:]: |
---|
199 | #for each child element |
---|
200 | #iterate through expected CHILDREN |
---|
201 | for child in self.CHILDREN: |
---|
202 | #get element name |
---|
203 | if len(self.CHILDREN[child])>=3: |
---|
204 | ename = self.CHILDREN[child][2] |
---|
205 | ename2 = self.CHILDREN[child][0] |
---|
206 | |
---|
207 | else: |
---|
208 | ename = self.CHILDREN[child][0] |
---|
209 | ename2=None |
---|
210 | #if there are options, then find the name that matches the frag |
---|
211 | if ename2 is not None: |
---|
212 | if frag[:] != []: |
---|
213 | for subfrag in frag[:]: |
---|
214 | etype=None |
---|
215 | if type(ename2) is list: |
---|
216 | if subfrag.tag in ename2: |
---|
217 | etype=self._getSubstitutionType(subfrag.tag) |
---|
218 | if subfrag.tag==ename2: |
---|
219 | etype=self.CHILDREN[child][1] |
---|
220 | if etype: |
---|
221 | childobj=eval(etype)() |
---|
222 | if len(self.CHILDREN[child])>=3: |
---|
223 | if frag[:] != []: |
---|
224 | childobj.fromXML(subfrag) |
---|
225 | else: |
---|
226 | childobj.fromXML(frag) |
---|
227 | #set this object to be an attribute of the 'parent' (self) object |
---|
228 | if hasattr(self, child): |
---|
229 | if type(self.__dict__[child]) is not list: |
---|
230 | tmp=self.__dict__[child] |
---|
231 | setattr(self, child, [tmp]) #convert to list |
---|
232 | self.__dict__[child].append(childobj) |
---|
233 | else: |
---|
234 | setattr(self, child, childobj) |
---|
235 | else: |
---|
236 | etype=None |
---|
237 | if type(ename) is list: |
---|
238 | if frag.tag in ename: |
---|
239 | etype=self._getSubstitutionType(frag.tag) |
---|
240 | elif frag.tag==ename: |
---|
241 | etype=self.CHILDREN[child][1] |
---|
242 | if etype: |
---|
243 | childobj=eval(etype)() |
---|
244 | if len(self.CHILDREN[child])>=3: |
---|
245 | if frag[:] != []: |
---|
246 | childobj.fromXML(frag[0]) |
---|
247 | else: |
---|
248 | childobj.fromXML(frag) |
---|
249 | #set this object to be an attribute of the 'parent' (self) object |
---|
250 | if hasattr(self, child): |
---|
251 | if type(self.__dict__[child]) is not list: |
---|
252 | tmp=self.__dict__[child] |
---|
253 | setattr(self, child, [tmp]) #convert to list |
---|
254 | self.__dict__[child].append(childobj) |
---|
255 | else: |
---|
256 | setattr(self, child, childobj) |
---|
257 | |
---|
258 | |
---|
259 | class csString(csElement): |
---|
260 | def __init__(self, text=None,**kwargs): |
---|
261 | if text != None: |
---|
262 | if type(text) is not str: |
---|
263 | text=str(text) |
---|
264 | self.CONTENT=text |
---|
265 | children={} |
---|
266 | addchildren(self,children) |
---|
267 | |
---|
268 | class AbstractGML(csElement): |
---|
269 | def __init__(self, **kwargs): |
---|
270 | a=[GML('id'), GML('description'), GML('name'), GML('MetaDataProperty')] |
---|
271 | addatts(self,a) |
---|
272 | |
---|
273 | class AssociationAttributeGroup(csElement): |
---|
274 | def __init__(self, **kwargs): |
---|
275 | a =[XLINK('href'),XLINK('role'), XLINK('arcrole'),XLINK('title'), XLINK('show'), XLINK('actuate')] |
---|
276 | addatts(self,a) |
---|
277 | #Note the abbreviations href, role etc can be used to set these attributes rather than the full qualified name |
---|
278 | if 'href' in kwargs: |
---|
279 | self.__dict__[XLINK('href')]=kwargs['href'] |
---|
280 | if 'role' in kwargs: |
---|
281 | self.__dict__[XLINK('role')]=kwargs['role'] |
---|
282 | if 'arcrole' in kwargs: |
---|
283 | self.__dict__[XLINK('arcrole')]=kwargs['arcrole'] |
---|
284 | if 'title' in kwargs: |
---|
285 | self.__dict__[XLINK('title')]=kwargs['title'] |
---|
286 | if 'show' in kwargs: |
---|
287 | self.__dict__[XLINK('show')]=kwargs['show'] |
---|
288 | if 'actuate' in kwargs: |
---|
289 | self.__dict__[XLINK('actuate')]=kwargs['actuate'] |
---|
290 | |
---|
291 | class SRSReferenceGroup(csElement): |
---|
292 | def __init__(self, **kwargs): |
---|
293 | a =['srsName','srsDimension'] |
---|
294 | addatts(self,a) |
---|
295 | |
---|
296 | class SRSInformationGroup(csElement): |
---|
297 | def __init__(self, **kwargs): |
---|
298 | a =['uomLabels','axisLabels'] |
---|
299 | addatts(self,a) |
---|
300 | |
---|
301 | class ArrayDescriptor(AbstractGML, csElement): |
---|
302 | def __init__(self,**kwargs): |
---|
303 | AbstractGML.__init__(self,**kwargs) |
---|
304 | children={'arraySize':[CSML('arraySize'), 'csString'], 'uom':[CSML('uom'),'csString'], 'numericType':[CSML('numericType'),'csString'], 'regExpTransform':[CSML('regExpTransform'),'csString'], 'numericTransform':[CSML('numericTransform'),'csString']} |
---|
305 | addchildren(self,children) |
---|
306 | |
---|
307 | class DirectPosition(AbstractGML, SRSReferenceGroup,SRSInformationGroup,csElement): |
---|
308 | def __init__(self, **kwargs): |
---|
309 | AbstractGML.__init__(self,**kwargs) |
---|
310 | |
---|
311 | class GridEnvelope(AbstractGML,SRSReferenceGroup, csElement): |
---|
312 | def __init__(self, **kwargs): |
---|
313 | SRSReferenceGroup.__init__(self,**kwargs) |
---|
314 | AbstractGML.__init__(self,**kwargs) |
---|
315 | children={'low':[GML('low'), 'csString'],'high':[GML('high'), 'csString']} |
---|
316 | addchildren(self,children) |
---|
317 | |
---|
318 | class Envelope(AbstractGML,SRSReferenceGroup, csElement): |
---|
319 | def __init__(self, **kwargs): |
---|
320 | SRSReferenceGroup.__init__(self,**kwargs) |
---|
321 | AbstractGML.__init__(self,**kwargs) |
---|
322 | children={'lowerCorner':[GML('lowerCorner'), 'DirectPosition'],'upperCorner':[GML('upperCorner'), 'DirectPosition']} |
---|
323 | addchildren(self,children) |
---|
324 | |
---|
325 | |
---|
326 | class AbstractFeature(AbstractGML,csElement): |
---|
327 | def __init__(self, **kwargs): |
---|
328 | AbstractGML.__init__(self,**kwargs) |
---|
329 | children={'boundedBy':[GML('boundedBy'), 'Envelope']} |
---|
330 | addchildren(self,children) |
---|
331 | |
---|
332 | class AbstractFeatureCollection(AbstractFeature,csElement): |
---|
333 | def __init__(self, **kwargs): |
---|
334 | AbstractFeature.__init__(self,**kwargs) |
---|
335 | |
---|
336 | class DomainSet(AbstractGML,AssociationAttributeGroup,csElement): |
---|
337 | def __init__(self, **kwargs): |
---|
338 | AbstractGML.__init__(self,**kwargs) |
---|
339 | addchildren(self,{}) |
---|
340 | |
---|
341 | class AggregatedArray(ArrayDescriptor,csElement): |
---|
342 | def __init__(self, **kwargs): |
---|
343 | ArrayDescriptor.__init__(self,**kwargs) |
---|
344 | children={'aggType':[CSML('aggType'),'csString'], 'aggIndex':[CSML('aggIndex'),'csString'],'components':[FILEFORMATS, 'ArrayDescriptor',CSML('component')]} |
---|
345 | addchildren(self,children) |
---|
346 | #'component':[CSML:('component'), 'ArrayDescriptor', CSML('component')], |
---|
347 | class MeasureOrNullList(AbstractGML,csElement): |
---|
348 | def __init__(self, **kwargs): |
---|
349 | AbstractGML.__init__(self,**kwargs) |
---|
350 | children={} |
---|
351 | addchildren(self,children) |
---|
352 | |
---|
353 | class CompositeValue(AbstractGML,csElement): |
---|
354 | def __init__(self, **kwargs): |
---|
355 | AbstractGML.__init__(self,**kwargs) |
---|
356 | children={'measures':[GML('measure'),'csString',GML('valueComponents')]} |
---|
357 | addchildren(self,children) |
---|
358 | |
---|
359 | class DataBlock(AbstractGML,csElement): |
---|
360 | #THIS IS INCOMPLETE |
---|
361 | def __init__(self, **kwargs): |
---|
362 | AbstractGML.__init__(self,**kwargs) |
---|
363 | children={'doubleOrNullTupleList':[GML('doubleOrNullTupleList'),'csString'],'rangeParameters':[GML('CompositeValue'), 'CompositeValue', GML('rangeParameters')]} |
---|
364 | addchildren(self,children) |
---|
365 | |
---|
366 | class RangeSet(AbstractGML,AssociationAttributeGroup,csElement): |
---|
367 | def __init__(self, **kwargs): |
---|
368 | AbstractGML.__init__(self,**kwargs) |
---|
369 | children={'quantityList':[GML('QuantityList'), 'MeasureOrNullList'], 'dataBlock':[GML('DataBlock'),'DataBlock'],'arrayDescriptor':[FILEFORMATS, 'ArrayDescriptor'], 'aggregatedArray':[CSML('AggregatedArray'), 'AggregatedArray']} |
---|
370 | addchildren(self,children) |
---|
371 | |
---|
372 | class MultiPoint(AbstractGML, SRSReferenceGroup,csElement): |
---|
373 | def __init__(self, **kwargs): |
---|
374 | AbstractGML.__init__(self,**kwargs) |
---|
375 | SRSReferenceGroup.__init__(self,**kwargs) |
---|
376 | children={'pointMember':[GML('pointMember'), 'csString'],'pointMembers':[GML('pointMember'), 'csString']} |
---|
377 | addchildren(self,children) |
---|
378 | |
---|
379 | class Point(AbstractGML,SRSReferenceGroup,csElement): |
---|
380 | def __init__(self, **kwargs): |
---|
381 | AbstractGML.__init__(self,**kwargs) |
---|
382 | SRSReferenceGroup.__init__(self,**kwargs) |
---|
383 | children={'pos':[GML('pos'), 'csString'],'coordinates':[GML('coordinates'), 'csString']} |
---|
384 | addchildren(self,children) |
---|
385 | |
---|
386 | class PointDomain(DomainSet, MultiPoint,csElement): |
---|
387 | def __init__(self, **kwargs): |
---|
388 | DomainSet.__init__(self,**kwargs) |
---|
389 | MultiPoint.__init__(self,**kwargs) |
---|
390 | children={} |
---|
391 | addchildren(self,children) |
---|
392 | |
---|
393 | class ProfileDomain(DomainSet, MultiPoint,csElement): |
---|
394 | def __init__(self, **kwargs): |
---|
395 | DomainSet.__init__(self,**kwargs) |
---|
396 | MultiPoint.__init__(self,**kwargs) |
---|
397 | children={} |
---|
398 | addchildren(self,children) |
---|
399 | |
---|
400 | |
---|
401 | |
---|
402 | class AbstractCoverage(AbstractFeature, csElement): |
---|
403 | def __init__(self, **kwargs): |
---|
404 | AbstractFeature.__init__(self,**kwargs) |
---|
405 | |
---|
406 | class AbstractDiscreteCoverage(AbstractCoverage, csElement): |
---|
407 | def __init__(self, **kwargs): |
---|
408 | AbstractCoverage.__init__(self,**kwargs) |
---|
409 | addchildren(self,{}) |
---|
410 | |
---|
411 | class Definition(AbstractGML): |
---|
412 | def __init__(self, **kwargs): |
---|
413 | AbstractGML.__init__(self,**kwargs) |
---|
414 | addchildren(self,{}) |
---|
415 | |
---|
416 | class Phenomenon(Definition,AssociationAttributeGroup): |
---|
417 | def __init__(self, **kwargs): |
---|
418 | Definition.__init__(self,**kwargs) |
---|
419 | AssociationAttributeGroup.__init__(self,**kwargs) |
---|
420 | children = {'':[CSML(''), '']} |
---|
421 | addchildren(self,children) |
---|
422 | |
---|
423 | class SpatialOrTemporalPositionList(AbstractGML,csElement): |
---|
424 | def __init__(self, **kwargs): |
---|
425 | AbstractGML.__init__(self,**kwargs) |
---|
426 | children={'coordinateList':[CSML('coordinateList'),'csString'], 'timePositionList':[CSML('timePositionList'),'csString']} |
---|
427 | addchildren(self,children) |
---|
428 | a=['frame'] |
---|
429 | addatts(self,a) |
---|
430 | |
---|
431 | class GridOrdinateDescription(AbstractGML,csElement): |
---|
432 | def __init__(self, **kwargs): |
---|
433 | AbstractGML.__init__(self,**kwargs) |
---|
434 | children={'coordAxisLabel':[CSML('coordAxisLabel'), 'csString'], 'coordAxisValues':[CSML('SpatialOrTemporalPositionList'),'SpatialOrTemporalPositionList',CSML('coordAxisValues')], 'gridAxesSpanned':[CSML('gridAxesSpanned'), 'csString'], 'sequenceRule':[CSML('sequenceRule'),'SequenceRuleType']} |
---|
435 | addchildren(self,children) |
---|
436 | |
---|
437 | class SequenceRuleType(csElement): |
---|
438 | def __init__(self, **kwargs): |
---|
439 | a=['axisOrder'] |
---|
440 | addatts(self,a) |
---|
441 | children={} |
---|
442 | addchildren(self,children) |
---|
443 | |
---|
444 | class GridPointDescription(AbstractGML,csElement): |
---|
445 | def __init__(self, **kwargs): |
---|
446 | AbstractGML.__init__(self,**kwargs) |
---|
447 | children={'posList':[CSML('posList'),'csString'],'sequenceRule':[CSML('sequenceRule'),'SequenceRuleType']} |
---|
448 | addchildren(self,children) |
---|
449 | |
---|
450 | |
---|
451 | class TimePositionList(AbstractGML,csElement): |
---|
452 | def __init__(self,**kwargs): |
---|
453 | a=['frame', 'calendarEraName','indeterminatePosition'] |
---|
454 | addatts(self,a) |
---|
455 | |
---|
456 | |
---|
457 | class GridCoordinatesTable(AbstractGML,csElement): |
---|
458 | def __init__(self,**kwargs): |
---|
459 | '''the additional value '1' in the children dictionary is used to signify that the elements must be nested as: |
---|
460 | <gml:ordinate> |
---|
461 | <gml:GridOrdinateDescription> |
---|
462 | </gml:ordinate> |
---|
463 | <gml:ordinate> |
---|
464 | <gml:GridOrdinateDescription> |
---|
465 | </gml:ordinate> |
---|
466 | |
---|
467 | not as: |
---|
468 | <gml:ordinate> |
---|
469 | <gml:GridOrdinateDescription> |
---|
470 | <gml:GridOrdinateDescription> |
---|
471 | </gml:ordinate> ''' |
---|
472 | AbstractGML.__init__(self,**kwargs) |
---|
473 | children={'gridOrdinates':[CSML('GridOrdinateDescription'), 'GridOrdinateDescription',CSML('gridOrdinate'),1], 'gridPoints':[CSML('GridPointDescription'),'GridPointDescription',CSML('gridPoints')]} |
---|
474 | addchildren(self,children) |
---|
475 | |
---|
476 | class ReferenceableGrid(AbstractGML, AssociationAttributeGroup, csElement): |
---|
477 | def __init__(self, **kwargs): |
---|
478 | AbstractGML.__init__(self,**kwargs) |
---|
479 | AssociationAttributeGroup.__init__(self,**kwargs) |
---|
480 | children={'coordTransformTable':[CSML('GridCoordinatesTable'), 'GridCoordinatesTable', CSML('coordTransformTable')],'axisLabels':[CSML('axisLabels'),'csString'], 'limits':[GML('GridEnvelope'),'GridEnvelope',CSML('limits')]} |
---|
481 | addchildren(self,children) |
---|
482 | a=['dimension'] |
---|
483 | addatts(self,a) |
---|
484 | |
---|
485 | class ReferenceableGridCoverage(AbstractDiscreteCoverage, csElement): |
---|
486 | def __init__(self, **kwargs): |
---|
487 | AbstractDiscreteCoverage.__init__(self,**kwargs) |
---|
488 | children={'referenceableGridDomain':[CSML('ReferenceableGrid'),'ReferenceableGrid' ,CSML('referenceableGridDomain') ]} |
---|
489 | addchildren(self,children) |
---|
490 | |
---|
491 | class AlternatePointCoverage(AbstractDiscreteCoverage, csElement): |
---|
492 | def __init__(self, **kwargs): |
---|
493 | AbstractDiscreteCoverage.__init__(self,**kwargs) |
---|
494 | children={'alternatePointDomain':[GML('Point'),'Point', CSML('alternatePointDomain')], 'rangeSet':[GML('rangeSet'), 'RangeSet'],'coverageFunction':[GML('coverageFunction'),'csString']} |
---|
495 | addchildren(self,children) |
---|
496 | |
---|
497 | |
---|
498 | class ProfileCoverage(AbstractDiscreteCoverage, csElement): |
---|
499 | def __init__(self, **kwargs): |
---|
500 | AbstractDiscreteCoverage.__init__(self,**kwargs) |
---|
501 | children={'profileDomain':[CSML('ProfileDomain'),'ProfileDomain' ,CSML('profileDomain') ], 'rangeSet':[GML('rangeSet'), 'RangeSet'],'coverageFunction':[GML('coverageFunction'),'csString']} |
---|
502 | addchildren(self,children) |
---|
503 | |
---|
504 | class PointCoverage(AbstractDiscreteCoverage, csElement): |
---|
505 | def __init__(self, **kwargs): |
---|
506 | AbstractDiscreteCoverage.__init__(self,**kwargs) |
---|
507 | children={'pointDomain':[CSML('PointDomain'),'PointDomain' ,CSML('pointDomain') ], 'rangeSet':[GML('rangeSet'), 'RangeSet'],'coverageFunction':[GML('coverageFunction'),'csString']} |
---|
508 | addchildren(self,children) |
---|
509 | |
---|
510 | class TimeSeriesDomain(AbstractGML, csElement): |
---|
511 | def __init__(self, **kwargs): |
---|
512 | AbstractGML.__init__(self,**kwargs) |
---|
513 | children={'time':[CSML('time'), 'csString']} |
---|
514 | addchildren(self,children) |
---|
515 | |
---|
516 | |
---|
517 | class PointSeriesCoverage(AbstractDiscreteCoverage, csElement): |
---|
518 | def __init__(self, **kwargs): |
---|
519 | AbstractDiscreteCoverage.__init__(self,**kwargs) |
---|
520 | children={'pointSeriesDomain':[CSML('TimeSeriesDomain'),'TimeSeriesDomain' ,CSML('pointSeriesDomain') ], 'rangeSet':[GML('rangeSet'), 'RangeSet'],'coverageFunction':[GML('coverageFunction'),'csString']} |
---|
521 | addchildren(self,children) |
---|
522 | |
---|
523 | class ProfileSeriesDomain(ReferenceableGrid, DomainSet, csElement): |
---|
524 | def __init__(self, **kwargs): |
---|
525 | DomainSet.__init__(self,**kwargs) |
---|
526 | ReferenceableGrid.__init__(self,**kwargs) |
---|
527 | children={} |
---|
528 | addchildren(self,children) |
---|
529 | |
---|
530 | class ProfileSeriesCoverage(AbstractDiscreteCoverage,csElement): |
---|
531 | def __init__(self, **kwargs): |
---|
532 | AbstractDiscreteCoverage.__init__(self,**kwargs) |
---|
533 | children={'profileSeriesDomain':[CSML('ProfileSeriesDomain'),'ProfileSeriesDomain' ,CSML('profileSeriesDomain') ], 'rangeSet':[GML('rangeSet'), 'RangeSet'],'coverageFunction':[GML('coverageFunction'),'csString']} |
---|
534 | addchildren(self,children) |
---|
535 | |
---|
536 | class SectionDomain(ReferenceableGrid, DomainSet, csElement): |
---|
537 | def __init__(self, **kwargs): |
---|
538 | DomainSet.__init__(self,**kwargs) |
---|
539 | ReferenceableGrid.__init__(self,**kwargs) |
---|
540 | children={} |
---|
541 | addchildren(self,children) |
---|
542 | |
---|
543 | class SectionCoverage(AbstractDiscreteCoverage,csElement): |
---|
544 | def __init__(self, **kwargs): |
---|
545 | AbstractDiscreteCoverage.__init__(self,**kwargs) |
---|
546 | children={'sectionDomain':[CSML('SectionDomain'),'SectionDomain' ,CSML('sectionDomain') ], 'rangeSet':[GML('rangeSet'), 'RangeSet'],'coverageFunction':[GML('coverageFunction'),'csString']} |
---|
547 | addchildren(self,children) |
---|
548 | |
---|
549 | class TrajectoryDomain(ReferenceableGrid, DomainSet, csElement): |
---|
550 | def __init__(self, **kwargs): |
---|
551 | DomainSet.__init__(self,**kwargs) |
---|
552 | ReferenceableGrid.__init__(self,**kwargs) |
---|
553 | children={} |
---|
554 | addchildren(self,children) |
---|
555 | |
---|
556 | class TrajectoryCoverage(AbstractDiscreteCoverage,csElement): |
---|
557 | def __init__(self, **kwargs): |
---|
558 | AbstractDiscreteCoverage.__init__(self,**kwargs) |
---|
559 | children={'trajectoryDomain':[CSML('TrajectoryDomain'),'TrajectoryDomain' ,CSML('trajectoryDomain') ], 'rangeSet':[GML('rangeSet'), 'RangeSet'],'coverageFunction':[GML('coverageFunction'),'csString']} |
---|
560 | addchildren(self,children) |
---|
561 | |
---|
562 | |
---|
563 | class ScanningRadarDomain(ReferenceableGrid, DomainSet, csElement): |
---|
564 | def __init__(self, **kwargs): |
---|
565 | DomainSet.__init__(self,**kwargs) |
---|
566 | ReferenceableGrid.__init__(self,**kwargs) |
---|
567 | children={} |
---|
568 | addchildren(self,children) |
---|
569 | |
---|
570 | class ScanningRadarCoverage(AbstractDiscreteCoverage,csElement): |
---|
571 | def __init__(self, **kwargs): |
---|
572 | AbstractDiscreteCoverage.__init__(self,**kwargs) |
---|
573 | children={'scanningRadarDomain':[CSML('ScanningRadarDomain'),'ScanningRadarDomain' ,CSML('scanningRadarDomain') ], 'rangeSet':[GML('rangeSet'), 'RangeSet'],'coverageFunction':[GML('coverageFunction'),'csString']} |
---|
574 | addchildren(self,children) |
---|
575 | |
---|
576 | |
---|
577 | class GridSeriesDomain(ReferenceableGrid, DomainSet, csElement): |
---|
578 | def __init__(self, **kwargs): |
---|
579 | DomainSet.__init__(self,**kwargs) |
---|
580 | ReferenceableGrid.__init__(self,**kwargs) |
---|
581 | children={} |
---|
582 | addchildren(self,children) |
---|
583 | |
---|
584 | class GridSeriesCoverage(AbstractDiscreteCoverage,csElement): |
---|
585 | def __init__(self, **kwargs): |
---|
586 | AbstractDiscreteCoverage.__init__(self,**kwargs) |
---|
587 | children={'gridSeriesDomain':[CSML('GridSeriesDomain'),'GridSeriesDomain' ,CSML('gridSeriesDomain') ], 'rangeSet':[GML('rangeSet'), 'RangeSet'],'coverageFunction':[GML('coverageFunction'),'csString']} |
---|
588 | addchildren(self,children) |
---|
589 | |
---|
590 | class AlternatePointFeature(AbstractFeature, csElement): |
---|
591 | def __init__(self, **kwargs): |
---|
592 | AbstractFeature.__init__(self,**kwargs) |
---|
593 | children={'location':[CSML('location'), 'csString'],'time':[CSML('time'), 'csString'], 'value':[CSML('AlternatePointCoverage'), 'AlternatePointCoverage', CSML('value')], 'parameter':[CSML('parameter'), 'Phenomenon']} |
---|
594 | addchildren(self,children) |
---|
595 | |
---|
596 | class PointFeature(AbstractFeature, csElement): |
---|
597 | def __init__(self, **kwargs): |
---|
598 | AbstractFeature.__init__(self,**kwargs) |
---|
599 | children={'location':[CSML('location'), 'csString'],'time':[CSML('time'), 'csString'], 'value':[CSML('PointCoverage'), 'PointCoverage', CSML('value')], 'parameter':[CSML('parameter'), 'Phenomenon']} |
---|
600 | addchildren(self,children) |
---|
601 | |
---|
602 | class PointCollectionFeature(AbstractFeature, csElement): |
---|
603 | def __init__(self, **kwargs): |
---|
604 | AbstractFeature.__init__(self,**kwargs) |
---|
605 | children={'time':[CSML('time'), 'csString'], 'value':[CSML('MultiPointCoverage'), 'MultiPointCoverage', CSML('value')], 'parameter':[CSML('parameter'), 'Phenomenon']} |
---|
606 | addchildren(self,children) |
---|
607 | |
---|
608 | |
---|
609 | class PointSeriesFeature(AbstractFeature, csElement): |
---|
610 | def __init__(self, **kwargs): |
---|
611 | AbstractFeature.__init__(self,**kwargs) |
---|
612 | children={'location':[CSML('location'), 'csString'], 'value':[CSML('PointSeriesCoverage'), 'PointSeriesCoverage', CSML('value')], 'parameter':[CSML('parameter'), 'Phenomenon']} |
---|
613 | addchildren(self,children) |
---|
614 | |
---|
615 | |
---|
616 | class GridFeature(AbstractFeature, csElement): |
---|
617 | def __init__(self, **kwargs): |
---|
618 | AbstractFeature.__init__(self,**kwargs) |
---|
619 | children={'time':[CSML('time'), 'csString'], 'value':[CSML('ReferenceableGridCoverage'), 'ReferenceableGridCoverage', CSML('value')], 'parameter':[CSML('parameter'), 'Phenomenon']} |
---|
620 | addchildren(self,children) |
---|
621 | |
---|
622 | class GridSeriesFeature(AbstractFeature, csElement): |
---|
623 | def __init__(self, **kwargs): |
---|
624 | AbstractFeature.__init__(self,**kwargs) |
---|
625 | children={'value':[CSML('GridSeriesCoverage'), 'GridSeriesCoverage', CSML('value')], 'parameter':[CSML('parameter'), 'Phenomenon']} |
---|
626 | addchildren(self,children) |
---|
627 | |
---|
628 | class ProfileFeature(AbstractFeature, csElement): |
---|
629 | def __init__(self, **kwargs): |
---|
630 | AbstractFeature.__init__(self,**kwargs) |
---|
631 | children={'time':[CSML('time'), 'csString'],'location':[CSML('location'), 'csString'], 'value':[CSML('ProfileCoverage'), 'ProfileCoverage', CSML('value')], 'parameter':[CSML('parameter'), 'Phenomenon']} |
---|
632 | addchildren(self,children) |
---|
633 | |
---|
634 | class ProfileSeriesFeature(AbstractFeature, csElement): |
---|
635 | def __init__(self, **kwargs): |
---|
636 | AbstractFeature.__init__(self,**kwargs) |
---|
637 | children={'location':[CSML('location'), 'csString'], 'value':[CSML('ProfileSeriesCoverage'), 'ProfileSeriesCoverage', CSML('value')], 'parameter':[CSML('parameter'), 'Phenomenon']} |
---|
638 | addchildren(self,children) |
---|
639 | |
---|
640 | class RaggedProfileSeriesFeature(AbstractFeature, csElement): |
---|
641 | def __init__(self, **kwargs): |
---|
642 | AbstractFeature.__init__(self,**kwargs) |
---|
643 | children={'location':[CSML('location'), 'csString'], 'profileLength':[CSML('profileLength'), 'csString'],'value':[CSML('ProfileSeriesCoverage'), 'ProfileSeriesCoverage', CSML('value')], 'parameter':[CSML('parameter'), 'Phenomenon']} |
---|
644 | addchildren(self,children) |
---|
645 | |
---|
646 | class RaggedSectionFeature(AbstractFeature, csElement): |
---|
647 | def __init__(self, **kwargs): |
---|
648 | AbstractFeature.__init__(self,**kwargs) |
---|
649 | children={'stationLocations':[CSML('stationLocations'), 'csString'], 'stationTimes':[CSML('stationTimes'), 'csString'],'profileLength':[CSML('profileLength'),'csString'],'value':[CSML('SectionCoverage'), 'SectionCoverage', CSML('value')], 'parameter':[CSML('parameter'), 'Phenomenon']} |
---|
650 | addchildren(self,children) |
---|
651 | |
---|
652 | class SectionFeature(AbstractFeature, csElement): |
---|
653 | def __init__(self, **kwargs): |
---|
654 | AbstractFeature.__init__(self,**kwargs) |
---|
655 | children={'stationLocations':[CSML('stationLocations'), 'csString'], 'stationTimes':[CSML('stationTimes'), 'TimePositionList'],'value':[CSML('SectionCoverage'), 'SectionCoverage', CSML('value')], 'parameter':[CSML('parameter'), 'Phenomenon']} |
---|
656 | addchildren(self,children) |
---|
657 | |
---|
658 | |
---|
659 | |
---|
660 | |
---|
661 | class ScanningRadarFeature(AbstractFeature, csElement): |
---|
662 | def __init__(self, **kwargs): |
---|
663 | AbstractFeature.__init__(self,**kwargs) |
---|
664 | children={'elevation':[CSML('elevation'), 'csString'], 'value':[CSML('ScanningRadarCoverage'), 'ScanningRadarCoverage', CSML('value')], 'parameter':[CSML('parameter'), 'Phenomenon']} |
---|
665 | addchildren(self,children) |
---|
666 | |
---|
667 | class SwathFeature(AbstractFeature, csElement): |
---|
668 | def __init__(self, **kwargs): |
---|
669 | AbstractFeature.__init__(self,**kwargs) |
---|
670 | children={'eqCrossLon':[CSML('eqCrossLon'), 'csString'],'eqCrossTime':[CSML('eqCrossTime'), 'csString'], 'value':[CSML('ReferenceableGridCoverage'), 'ReferenceableGridCoverage', CSML('value')], 'parameter':[CSML('parameter'), 'Phenomenon']} |
---|
671 | addchildren(self,children) |
---|
672 | |
---|
673 | class TrajectoryFeature(AbstractFeature, csElement): |
---|
674 | def __init__(self, **kwargs): |
---|
675 | AbstractFeature.__init__(self,**kwargs) |
---|
676 | children={'value':[CSML('TrajectoryCoverage'), 'TrajectoryCoverage', CSML('value')], 'parameter':[CSML('parameter'), 'Phenomenon']} |
---|
677 | addchildren(self,children) |
---|
678 | |
---|
679 | class FileExtract(ArrayDescriptor, csElement): |
---|
680 | def __init__(self,**kwargs): |
---|
681 | ArrayDescriptor.__init__(self,**kwargs) |
---|
682 | children= {'fileName':[CSML('fileName'), 'csString']} |
---|
683 | addchildren(self,children) |
---|
684 | |
---|
685 | class NetCDFExtract(FileExtract, csElement): |
---|
686 | def __init__(self,**kwargs): |
---|
687 | FileExtract.__init__(self, **kwargs) |
---|
688 | children={'variableName':[CSML('variableName'), 'csString']} |
---|
689 | addchildren(self,children) |
---|
690 | |
---|
691 | class NASAAmesExtract(FileExtract, csElement): |
---|
692 | def __init__(self,**kwargs): |
---|
693 | FileExtract.__init__(self, **kwargs) |
---|
694 | children={'variableName':[CSML('variableName'), 'csString'], 'index':[CSML('index'),'csString']} |
---|
695 | addchildren(self,children) |
---|
696 | |
---|
697 | class FeatureCollection(AbstractFeatureCollection,csElement): |
---|
698 | def __init__(self,**kwargs): |
---|
699 | AbstractFeatureCollection.__init__(self,**kwargs) |
---|
700 | children={'members':[[CSML('GridFeature'), CSML('GridSeriesFeature'),CSML('PointFeature'),CSML('TrajectoryFeature'),CSML('ProfileFeature'),CSML('ProfileSeriesFeature'),CSML('RaggedProfileSeriesFeature'),CSML('RaggedSectionFeature'),CSML('SectionFeature'),CSML('ScanningRadarFeature'),CSML('PointSeriesFeature'),CSML('AlternatePointFeature')], 'AbstractFeature', GML('featureMembers')]} |
---|
701 | addchildren(self,children) |
---|
702 | def _getSubstitutionType(self,tag): |
---|
703 | if tag==CSML('GridFeature'): |
---|
704 | return 'GridFeature' |
---|
705 | elif tag==CSML('GridSeriesFeature'): |
---|
706 | return 'GridSeriesFeature' |
---|
707 | elif tag==CSML('PointFeature'): |
---|
708 | return 'PointFeature' |
---|
709 | elif tag==CSML('TrajectoryFeature'): |
---|
710 | return 'TrajectoryFeature' |
---|
711 | elif tag==CSML('PointSeriesFeature'): |
---|
712 | return 'PointSeriesFeature' |
---|
713 | elif tag==CSML('ProfileFeature'): |
---|
714 | return 'ProfileFeature' |
---|
715 | elif tag==CSML('ProfileSeriesFeature'): |
---|
716 | return 'ProfileSeriesFeature' |
---|
717 | elif tag==CSML('RaggedProfileSeriesFeature'): |
---|
718 | return 'RaggedProfileSeriesFeature' |
---|
719 | elif tag==CSML('RaggedSectionFeature'): |
---|
720 | return 'RaggedSectionFeature' |
---|
721 | elif tag==CSML('SectionFeature'): |
---|
722 | return 'SectionFeature' |
---|
723 | elif tag==CSML('ScanningRadarFeature'): |
---|
724 | return 'ScanningRadarFeature' |
---|
725 | elif tag==CSML('AlternatePointFeature'): |
---|
726 | return 'AlternatePointFeature' |
---|
727 | else: return 'AbstractFeature' |
---|
728 | def _getReverseSubsType(self, typename): |
---|
729 | if typename== 'GridFeature': |
---|
730 | return CSML('GridFeature') |
---|
731 | elif typename == 'GridSeriesFeature': |
---|
732 | return CSML('GridSeriesFeature') |
---|
733 | elif typename == 'PointSeriesFeature': |
---|
734 | return CSML('PointSeriesFeature') |
---|
735 | elif typename == 'ProfileFeature': |
---|
736 | return CSML('ProfileFeature') |
---|
737 | elif typename == 'ProfileSeriesFeature': |
---|
738 | return CSML('ProfileSeriesFeature') |
---|
739 | elif typename == 'SectionFeature': |
---|
740 | return CSML('SectionFeature') |
---|
741 | elif typename == 'ScanningRadarFeature': |
---|
742 | return CSML('ScanningRadarFeature') |
---|
743 | elif typename == 'RaggedSectionFeature': |
---|
744 | return CSML('RaggedSectionFeature') |
---|
745 | elif typename == 'RaggedProfileSeriesFeature': |
---|
746 | return CSML('RaggedProfileSeriesFeature') |
---|
747 | elif typename == 'PointFeature': |
---|
748 | return CSML('PointFeature') |
---|
749 | elif typename == 'TrajectoryFeature': |
---|
750 | return CSML('TrajectoryFeature') |
---|
751 | elif typename == 'AlternatePointFeature': |
---|
752 | return CSML('AlternatePointFeature') |
---|
753 | else: return CSML('AbstractFeature') |
---|
754 | |
---|
755 | |
---|
756 | class Dataset(AbstractGML, csElement): |
---|
757 | ''' Dataset class, needed as root of tree''' |
---|
758 | def __init__(self, **kwargs): |
---|
759 | AbstractGML.__init__(self,**kwargs) |
---|
760 | children = {'featureCollection':[GML('FeatureCollection') ,'FeatureCollection'],'fileExtracts':[FILEFORMATS, 'ArrayDescriptor']} |
---|
761 | addchildren(self,children) |
---|
762 | def toXML(self): |
---|
763 | csmlfrag=ET.Element(CSML('Dataset')) |
---|
764 | csElement.toXML(self, csmlfrag) |
---|
765 | return csmlfrag |
---|
766 | def _getSubstitutionType(self,tag): |
---|
767 | if tag==CSML('NetCDFExtract'): |
---|
768 | return 'NetCDFExtract' |
---|
769 | elif tag==CSML('NASAAmesExtract'): |
---|
770 | return 'NASAAmesExtract' |
---|
771 | else: return 'ArrayDescriptor' |
---|
772 | def _getReverseSubsType(self, typename): |
---|
773 | if typename== 'NetCDFExtract': |
---|
774 | return CSML('NetCDFExtract') |
---|
775 | elif typename == 'NASAAmesExtract': |
---|
776 | return CSML('NASAAmesExtract') |
---|
777 | else: return CSML('ArrayDescriptor') |
---|
778 | |
---|
779 | return typename |
---|
780 | |
---|
781 | |
---|
782 | def main(): |
---|
783 | '''round trip for testing purposes:''' |
---|
784 | print '\n' |
---|
785 | tree=ET.ElementTree(file='test.xml') |
---|
786 | ds=Dataset() |
---|
787 | ds.fromXML(tree.getroot()) |
---|
788 | csmltree=ds.toXML() |
---|
789 | #print csmltree |
---|
790 | |
---|
791 | csmlout=parser_extra.PrettyPrint(csmltree) |
---|
792 | csmlout=parser_extra.removeInlineNS(csmlout) |
---|
793 | print '\n %s'% csmlout |
---|
794 | #for member in ds.featureCollection.members: |
---|
795 | #print dir(member.value) |
---|
796 | |
---|
797 | |
---|
798 | if __name__=='__main__': |
---|
799 | main() |
---|