source: TI02-CSML/trunk/newparser/parser_extra.py @ 839

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

basic getData method working for NetCDF/PP/NasaAmes

Line 
1#parser_extra.py
2# DL 12 april 2006
3# 'extra' functions to enable parsing.
4#contains:
5#. Namespace fix
6# Function to resolve references in CSML doc.
7
8import Parser
9import sys
10import elementtree.ElementTree as etree
11
12
13#string manipulation function. may as well go in this file for now.
14def removeInlineNS(csmlstring):
15        #  removeInlineNS: function removes "inline" namespaces and declares them as part of the Dataset element.
16        #TODO: This whole thing is hardcoded and needs reviewing.
17       
18        #first need to update the ElementTree namespace map:
19        etree._namespace_map.update({
20        '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'})
21   
22        # This is used when parsing a csml document#
23        #remove all
24        csmlstring=csmlstring.replace(' xmlns:csml="http://ndg.nerc.ac.uk/csml"','')
25        csmlstring=csmlstring.replace(' xmlns:xlink="http://www.w3.org/1999/xlink"','')
26        csmlstring=csmlstring.replace('xmlns:om="http://www.opengis.net/om"','')
27        csmlstring=csmlstring.replace('xsi:schemaLocation="http://ndg.nerc.ac.uk/csml/XMLSchemas/CSMLAppSchema.xsd"','')
28        csmlstring=csmlstring.replace('xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"','')
29
30        #remove cmsl: prefixes
31        csmlstring=csmlstring.replace('<csml:','<')
32        csmlstring=csmlstring.replace('</csml:','</')
33        #add namespace declarations at top of document, after gml namespace declaration
34        csmlstring=csmlstring.replace('xmlns:gml="http://www.opengis.net/gml">', '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">')
35       
36        #this is used when creating a new csml document
37        csmlstring=csmlstring.replace('ns0', 'gml')
38        csmlstring=csmlstring.replace('xmlns:ns1', 'xmlns:xlink')
39        csmlstring=csmlstring.replace('ns1:href', 'xlink:href')
40        csmlstring=csmlstring.replace('ns1:', '')  #the rest of the ns1s are CSML namespaces... due to the way it has been parsed.       
41        return csmlstring
42
43
44class ParserPostProcessor:
45    def __init__(self,dataset):
46        if isinstance(dataset,Parser.Dataset):
47            self.dataset=dataset
48        else: 
49            print 'Error: Could not post-process CSML document'
50            sys.exit()
51   
52    def resolveReferences(self):
53        #if there are any phenomenon defintions then
54         #for every <parameter> element, need to resolve any hrefs.
55        if hasattr(self.dataset, 'phenomenonDefinitions'):
56            for feature in self.dataset.featureCollection.members:
57                if hasattr(feature, 'parameter'):
58                    if hasattr(feature.parameter,'href'):
59                        #search phenomenon definitions for a match.
60                        for definition in self.dataset.phenomenonDefinitions.definitionMembers:
61                            if definition.id == feature.parameter.href[1:]:  #remove '#'
62                                #remove href and replace with object
63                                print dir(definition)
64                                feature.parameter.href =None
65                                feature.parameter=definition
66                                break
67       
68        #if there are embedded reference system definitions then
69        #for every DomainReference and DomainComplement need to resolve any srsNames
70        if hasattr(self.dataset, 'referenceSystemDefinitions'):
71            for feature in self.dataset.featureCollection.members:
72                if hasattr(feature, 'domain'):  #Trajectory features don't have domains..
73                    if hasattr(feature.domain.domainReference, 'srsName'):
74                        #search reference definitions for a match.
75                        for definition in self.dataset.referenceSystemDefinitions.definitionMembers:
76                            if definition.id == feature.domain.domainReference.srsName[1:]:
77                                #remove srsName attribute and replace withimport pickle Object.
78                                pass #Leave this for now.
79                    if hasattr(feature.domain, 'domainComplement'):
80                        if hasattr(feature.domain.domainComplement, 'srsName'):
81                            #search reference definitions for a match.
82                            for definition in self.dataset.referenceSystemDefinitions.definitionMembers:
83                                if definition.id == feature.domain.domainComplement.srsName[1:]:
84                                        #remove srsName attribute and replace with Object.
85                                    pass #Leave this for now.
86       
87       #NetCDFExtracts etc may be referenced e.g. as <axisvalues> element. Need to fix these too.
88        if hasattr(self.dataset, 'arrayDescriptors'):
89            for feature in self.dataset.featureCollection.members:
90                if hasattr (feature, 'domain'):
91                    if hasattr(feature.domain, 'domainComplement'):
92                        if isinstance(feature.domain.domainComplement, Parser.Grid):
93                                for ordinate in feature.domain.domainComplement.ordinates:
94                                    #search arrayDescriptors for a match.
95                                    for arrayDescriptor  in self.dataset.arrayDescriptors:
96                                        print arrayDescriptor.id
97                                        if arrayDescriptor.id == ordinate.axisValues[1:]:  #remove '#'
98                                            ordinate.axisValues  = arrayDescriptor
99                                            break
100                                       
101        #uom may reference UnitDefinitions. Replace these too
102        if hasattr(self.dataset,'arrayDescriptors'):
103            for feature in self.dataset.featureCollection.members:
104                if hasattr (feature, 'rangeSet'):
105                    if hasattr (feature.rangeSet, 'aggregatedArray'):
106                         if hasattr(feature.rangeSet.aggregatedArray, 'uom'):
107                             if hasattr(self.dataset, 'unitDefinitions'):
108                                for definition in self.dataset.unitDefinitions.definitionMembers:
109                                    if definition.id== feature.rangeSet.aggregatedArray.uom[1:]:
110                                        feature.rangeSet.aggregatedArray.uom=definition
111                                        break
112        #TODO need to also take into account the fact that UnitDefinitions could be externally defined.
113       
114        return self.dataset
Note: See TracBrowser for help on using the repository browser.