source: DPPP/kml/csml2kml/python/csml2kml/csml2kml/Station.py @ 3633

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/DPPP/kml/csml2kml/python/csml2kml/csml2kml/Station.py@3633
Revision 3633, 5.4 KB checked in by mkochan, 11 years ago (diff)

Finished listing of features according to collection times (only those features are listed which are available during the whole interval. Also, add more detailed error messages.

Line 
1'''
2Classes for work with I{<np:Station>}'s.
3'''
4
5from cElementTree import XML
6from pylab import dates     # a very good date/time module from matplotviz -- allows years < 1970
7
8npXmlNamespace = 'http://ndg.nerc.ac.uk/np'  # the namespace in which the Station element is defined
9
10class NPStation:
11    '''
12    Represents a ground meteorological station, identified by C{id}, with description C{desc},
13    located at (C{lon},C{lat}), and containing CSML features with identifiers C{csmlFeatureIds}.
14    @ivar id: Station identifier (e.g. "midas.station.1")
15    @type id: str
16    @ivar desc: Station description (e.g. "FAIR ISLE")
17    @type desc: str
18    @ivar lon: Station longitude
19    @type lon: float
20    @ivar lat: Station latitude
21    @type lat: float
22    @type features: C{NPStationFeature} list
23
24    '''
25
26    def __init__(self, id = None, desc = None, lon = None, lat = None, stationFeatures = None):
27        '''
28        The initialiser (use empty call C{NPStation()} and later call the C{parseString} or C{parseXML} method).
29        '''
30        self.id = id
31        self.desc = desc
32        self.lon = lon; self.lat = lat
33        self.stationFeatures = stationFeatures
34
35    def parseString(self, str):
36        '''
37        Parse in the station from a string C{str} containing XML.
38        '''
39        stationElement = XML(str)
40        self.parseXML(stationElement)
41
42    def parseXML(self, stationElement):
43        '''
44        Parse in the station directly from an C{cElementTree.Element} object.
45        @param stationElement: An element containing station in XML form.
46        @type stationElement: C{cElementTree.Element}
47        '''
48
49        # Extract the identifier, short description, and longitude and latitute
50        (stationNameElement, stationIDElement, locationElement, stationFeaturesElement) = stationElement.getchildren()
51        pointElement = locationElement.getchildren()[0]
52        posElement = pointElement.getchildren()[0]
53        (self.lon,self.lat) = map(float, posElement.text.split())
54        self.id = stationElement.get('{http://www.opengis.net/gml}id')
55        self.desc = stationNameElement.text
56
57        self.stationFeatures = []
58        for stationFeatureElement in stationFeaturesElement.getchildren():
59            npStationFeature = NPStationFeature()
60            npStationFeature.parseXML(stationFeatureElement)
61            self.stationFeatures.append(npStationFeature)
62
63    def __repr__(self):
64        return '<np:Station> %s ("%s") located at (%f, %f) with %d features (%s).' % (
65            self.id, self.desc, self.lon, self.lat, len(self.stationFeatures), str(self.stationFeatures)
66            )
67
68class NPStationFeature:
69    '''
70    Represents a I{<np:StationFeature>} element.
71    @ivar featureId: Unique ID of the feature
72    @type featureId: C{str}
73    @ivar collectBeginDate: Data when feature collection started.
74    @type collectBeginDate: C{pylab.dates.datetime.datetime}
75    @ivar collectEndDate: Data when feature collection started.
76    @type collectEndDate: C{pylab.dates.datetime.datetime}
77    '''
78
79    def __init__(self, featureId = None, featureType = None, collectBeginDate = None, collectEndDate = None):
80        '''
81        The initialiser (use empty call C{NPStationFeature()} and later call the C{parseXML} method).
82        '''
83        self.featureId = featureId
84        self.featureType = featureType
85        self.collectBeginDate = collectBeginDate
86        self.collectEndDate = collectEndDate
87
88    def parseXML(self, stationFeatureElement):
89        '''
90        Parse in the station directly from an C{cElementTree.Element} object.
91        @param stationFeatureElement: An element containing a station feature in XML form.
92        @type stationElement: C{cElementTree.Element}
93        '''
94        (featureTypeElement, featureIdElement, 
95         collectBeginDateElement, collectEndDateElement) = stationFeatureElement.getchildren()
96        if featureTypeElement.text != 'csml:PointSeriesFeature':
97            raise NotImplementedError('No other than "csml:PointSeriesFeature" feature type is supported')
98        self.featureType = featureTypeElement.text
99        self.featureId = featureIdElement.text
100        self.collectBeginDate = dates.dateutil.parser.parse(collectBeginDateElement.text)
101        self.collectEndDate = dates.dateutil.parser.parse(collectEndDateElement.text)
102
103    def __repr__(self):
104        return str(vars(self))
105
106class WFSStationCollection:
107    '''
108    Represents a <wfs:FeatureCollection> element that contains *specifically* <np:Station> elements!
109    @ivar stations: A list of C{NPStation}'s contained in the collection.
110    '''
111
112    def __init__(self):
113        self.stations = []
114
115    def parseString(self, str):
116        '''
117        Parse in the <wfs:FeatureCollection> element returned from GeoServer, and save individual stations.
118        @param str: A string containing the the <wfs:FeatureCollection> element.
119
120        '''
121        featureCollectionElement = XML(str)
122        self.parseXML(featureCollectionElement)
123
124    def parseXML(self, featureCollectionElement):
125        '''
126        Parse in the <wfs:FeatureCollection> element returned from GeoServer, save individual stations.
127        Use if your WFS subset of XML is stored in an C{cElementTree.Element} object.
128        '''
129        featureMembersElement = featureCollectionElement.getchildren()[0]
130        for npStationElement in featureMembersElement.getchildren():
131            npStation = NPStation()
132            npStation.parseXML(npStationElement)
133            self.stations.append(npStation)
Note: See TracBrowser for help on using the repository browser.