source: TI02-CSML/trunk/newparser/csmlio.py @ 831

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

added csmlio.py to provide easy wrapper to parser, also ioDemo.py to show how to make calls. Neither are complete

Line 
1#csmlio.py
2#Contains Parser wrapper'functions to enable reading of data
3
4from  Parser import *
5import nappy
6from cdms import Cdunif
7import parser_extra
8from xml.dom.minidom import parseString
9import elementtree.ElementTree as etree
10
11
12class CSMLinterface:
13    #Wrapper class containing methods to read/write from/to CSML
14    #Uses parser.
15    def __init__(self):
16        self.dataset=None
17        self.currentFeature=None
18        self.currentArrayDescriptor = None
19        #this is a fix to the  ElementTree namespace problem that namespaces are usually represented as ns0, ns1, ns2 etc.
20        etree._namespace_map.update({
21        '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'})
22   
23    def __setFeature(self,featureID):
24        #given a featureID, set the currentFeature if it is not already set
25        #this is used to check the correct feature is always set.
26        if self.currentFeature is None:
27            for member in self.dataset.featureCollection.members:
28                if member.id == featureID:
29                    self.currentFeature = member
30
31    def __setArrayDescriptor(self,fileExtractID):
32        #given a fileExtractID, set the currentArrayDescriptor if it is not already set
33        if self.currentArrayDescriptor is None:
34            for arrayDescriptor in self.dataset.arrayDescriptors:
35                if arrayDescriptor.id == fileExtractID:
36                    self.currentArrayDescriptor=arrayDescriptor
37        if self.currentArrayDescriptor.id != fileExtractID:
38            for arrayDescriptor in self.dataset.arrayDescriptors:
39                if arrayDescriptor.id == fileExtractID:
40                    self.currentArrayDescriptor=arrayDescriptor
41
42    def parse(self,csmldoc):
43        #takes incoming csml document and parses it.
44        tree = ElementTree(file=csmldoc)
45        self.dataset=Dataset()
46        self.dataset.fromXML(tree.getroot())
47        self.dataset =parser_extra.ParserPostProcessor(self.dataset).resolveReferences()
48
49        #self.currentFeature holds a feature object
50        self.currentFeature = None
51       
52
53    def getCSMLasString(self):
54        #returns csml document as a string
55        strCSML=self.dataset.toXML()
56        strCSML= parseString(tostring(strCSML)).toprettyxml()
57        strCSML= parser_extra.removeInlineNS(strCSML)
58        return strCSML
59
60    def getFeatureList(self):
61        #returns a list of feature ids
62        self.featureList = []
63        for member in self.dataset.featureCollection.members:
64             self.featureList.append(member.id)
65        return self.featureList
66       
67    def getFeatureType(self,featureID):
68        self.__setFeature(featureID)
69        print featureID
70        if isinstance(self.currentFeature, PointFeature):
71            featureType = 'PointFeature'
72        elif isinstance(self.currentFeature, PointSeriesFeature):
73            featureType = 'PointSeriesFeature'
74        elif isinstance(self.currentFeature, ProfileFeature):
75            featureType = 'ProfileFeature'
76        elif isinstance(self.currentFeature, ProfileSeriesFeature):
77            featureType = 'ProfileSeriesFeature'
78        elif isinstance(self.currentFeature, GridFeature):
79            featureType = 'GridFeature'
80        elif isinstance(self.currentFeature, GridSeriesFeature):
81            featureType = 'GridSeriesFeature'
82        elif isinstance(self.currentFeature, TrajectoryFeature):
83            featureType = 'TrajectoryFeature'
84        else:
85             featureType = 'Unknown feature'
86        return featureType
87   
88    def getFeatureDescription(self,featureID):
89        #returns gml:description (== long name) of feature
90        #returns empty string if not available
91        self.__setFeature(featureID)
92        if hasattr(self.currentFeature, 'description'):
93            desc = self.currentFeature.description
94        else:
95            desc =""
96        return desc
97       
98    def getFileExtractList(self):
99        fileExtractList=[]
100        for arrayDescriptor in self.dataset.arrayDescriptors:
101            fileExtractList.append(arrayDescriptor.id)
102        return fileExtractList
103   
104    def getData(self, fileExtractID):
105        #getData, given the gml:id of a file extract, calls the appropriate library to read the data
106        self.__setArrayDescriptor(fileExtractID)
107        file = self.currentArrayDescriptor.fileName
108        if isinstance(self.currentArrayDescriptor, NetCDFExtract():
109            pass
110        if isinstance(self.currentArrayDescriptor, NASAAmesExtract():
111            pass
112        if isinstance(self.currentArrayDescriptor, GribExtract():
113            pass
114        if isinstance(self.currentArrayDescriptor, PPExtract():
115            pass
116        if isinstance(self.currentArrayDescriptor, QXFExtract():
117            pass       
118        return file       
Note: See TracBrowser for help on using the repository browser.