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

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

basic getData method working for NetCDF/PP/NasaAmes

Line 
1#csmlio.py
2#Contains Parser wrapper'functions to enable reading of data
3
4from  Parser import *
5import nappy
6import cdms
7import parser_extra
8from xml.dom.minidom import parseString
9import elementtree.ElementTree as etree
10
11#import the DataInterface module from the Scanner, assumes it is in a sibling directory to the one containing csmlio.py
12#TODO - how best to organise modules...
13import os
14currentPath=os.getcwd()
15parentPath=os.sep.join(currentPath.split(os.sep)[:-1])
16parserPath=parentPath + '/Scanner/csmllibs'
17sys.path.append(parserPath) #append the parser path to sys.path
18try:
19    import csmldataiface
20except:
21    print 'Could not import CSML Data Interface module. Make sure the Parser code is in ../newparser directory on the same level as ../Scanner directory.'
22    sys.exit()
23
24
25class CSMLinterface:
26    #Wrapper class containing methods to read/write from/to CSML
27    #Uses parser.
28    def __init__(self):
29        self.dataset=None
30        self.currentFeature=None
31        self.currentArrayDescriptor = None
32        #this is a fix to the  ElementTree namespace problem that namespaces are usually represented as ns0, ns1, ns2 etc.
33        etree._namespace_map.update({
34        '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'})
35   
36    def __setFeature(self,featureID):
37        #given a featureID, set the currentFeature if it is not already set
38        #this is used to check the correct feature is always set.
39        if self.currentFeature is None:
40            for member in self.dataset.featureCollection.members:
41                if member.id == featureID:
42                    self.currentFeature = member
43
44    def __setArrayDescriptor(self,fileExtractID):
45        #given a fileExtractID, set the currentArrayDescriptor if it is not already set
46        if self.currentArrayDescriptor is None:
47            for arrayDescriptor in self.dataset.arrayDescriptors:
48                if arrayDescriptor.id == fileExtractID:
49                    self.currentArrayDescriptor=arrayDescriptor
50        if self.currentArrayDescriptor.id != fileExtractID:
51            for arrayDescriptor in self.dataset.arrayDescriptors:
52                if arrayDescriptor.id == fileExtractID:
53                    self.currentArrayDescriptor=arrayDescriptor
54
55    def parse(self,csmldoc):
56        #takes incoming csml document and parses it.
57        tree = ElementTree(file=csmldoc)
58        self.dataset=Dataset()
59        self.dataset.fromXML(tree.getroot())
60        self.dataset =parser_extra.ParserPostProcessor(self.dataset).resolveReferences()
61
62        #self.currentFeature holds a feature object
63        self.currentFeature = None
64       
65    def getDatasetObj(self):
66        #if you want to bypass this wrapper layer, call this method and it will
67        #return a Parser.Dataset object, which you can interrogate...
68        return self.dataset
69
70    def getCSMLasString(self):
71        #returns csml document as a string
72        strCSML=self.dataset.toXML()
73        strCSML= parseString(tostring(strCSML)).toprettyxml()
74        strCSML= parser_extra.removeInlineNS(strCSML)
75        return strCSML
76
77    def getFeatureList(self):
78        #returns a list of feature ids
79        self.featureList = []
80        for member in self.dataset.featureCollection.members:
81             self.featureList.append(member.id)
82        return self.featureList
83       
84    def getFeatureType(self,featureID):
85        self.__setFeature(featureID)
86        print featureID
87        if isinstance(self.currentFeature, PointFeature):
88            featureType = 'PointFeature'
89        elif isinstance(self.currentFeature, PointSeriesFeature):
90            featureType = 'PointSeriesFeature'
91        elif isinstance(self.currentFeature, ProfileFeature):
92            featureType = 'ProfileFeature'
93        elif isinstance(self.currentFeature, ProfileSeriesFeature):
94            featureType = 'ProfileSeriesFeature'
95        elif isinstance(self.currentFeature, GridFeature):
96            featureType = 'GridFeature'
97        elif isinstance(self.currentFeature, GridSeriesFeature):
98            featureType = 'GridSeriesFeature'
99        elif isinstance(self.currentFeature, TrajectoryFeature):
100            featureType = 'TrajectoryFeature'
101        else:
102             featureType = 'Unknown feature'
103        return featureType
104   
105    def getFeatureDescription(self,featureID):
106        #returns gml:description (== long name) of feature
107        #returns empty string if not available
108        self.__setFeature(featureID)
109        if hasattr(self.currentFeature, 'description'):
110            desc = self.currentFeature.description
111        else:
112            desc =""
113        return desc
114       
115    def getFileExtractList(self):
116        fileExtractList=[]
117        print dir(self.dataset)
118        for arrayDescriptor in self.dataset.arrayDescriptors:
119            fileExtractList.append(arrayDescriptor.id)
120        return fileExtractList
121   
122    def getData(self, fileExtractID):
123        #getData, given the gml:id of a file extract, returns the (full) data for that extract
124        self.__setArrayDescriptor(fileExtractID)
125        file = self.currentArrayDescriptor.fileName
126        print self.currentArrayDescriptor.arraySize
127        variable = self.currentArrayDescriptor.variableName
128        DI = csmldataiface.DataInterface()
129        DI=DI.getUnknownInterfaceType(file)       
130        DI.openFile(file)
131        DI.setAxis(variable)
132        fulldata = DI.getDataForAxis()
133        return fulldata
Note: See TracBrowser for help on using the repository browser.