source: DPPP/kml/csml2kml/python/csml2kmlpylon/csml2kmlpylon/controllers/csmlGrapher.py @ 3705

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

Added configurable formatting of URL queries to WFS for requesting CSML features.

Line 
1# Pylons-specific imports
2import logging
3from csml2kmlpylon.lib.base import *
4from paste.httpexceptions import HTTPBadRequest, HTTPNotFound
5
6# Other imports
7import Image
8from pylab import *
9from matplotlib import dates
10import re
11from cStringIO import StringIO
12from tempfile import NamedTemporaryFile
13import urllib
14
15# CSML and csml2kml imports
16import csml
17import csml2kml.Station
18from csml2kml.utils import wget, URLError
19from csml2kml.ET import Element, SubElement, ElementTree, XML
20
21log = logging.getLogger(__name__)
22
23class CsmlgrapherController(BaseController):
24
25    class Dataset:
26        '''
27        An auxiliary storage class describing a dataset of csml2kml.Station's;
28        this describes what URL the data is I{retrievable} from, as opposed to this class actually storing any data.
29        E.g. MIDAS and ECN are different datasets (each has a WFS URL from which it is being accessible).
30        '''
31        def __init__(self, id, name, geoServerUrl, datetimeQueryFormat):
32            self.id = id
33            self.name = name
34            self.geoServerUrl = geoServerUrl
35            self.datetimeQueryFormat = datetimeQueryFormat
36
37    def __call__(self, environ, start_response):
38        '''
39        Initialise the web service by overriding a method that always gets called upon controller construction.
40        More specifically, read in the config file.
41        '''
42
43        # The name of the config file is set in the server's "development.ini" file.
44        configFileName = config['app_conf']['csmlGrapher.configfile']
45       
46        # Load the configuration XML element (but only the part pertaining to the web service)
47        testConfig = ElementTree().parse(configFileName)
48        print testConfig
49        grapherConfig = (ElementTree().parse(configFileName))
50
51        # Initialise the datasets from the config file.
52        self.datasets = {}
53        datasetElements = grapherConfig.findall('Dataset')
54        for datasetElement in datasetElements:
55            id = datasetElement.get('id')
56            self.datasets[id] = self.Dataset(
57                id, 
58                datasetElement.get('name'),
59                datasetElement.find('GeoServerURL').text,
60                datasetElement.find('DatetimeQueryFormat').text
61                )
62
63        # Set other configurable variables
64        self.displayIntervalStart = dates.dateutil.parser.parse(grapherConfig.find('DisplayIntervalStart').text)
65        self.displayIntervalEnd = dates.dateutil.parser.parse(grapherConfig.find('DisplayIntervalEnd').text)
66        self.servedFromUrl = grapherConfig.find('ServedFromURL').text
67        print 'Config file parse finished.'
68
69        # Call the __call__ method of the parent class
70        return BaseController.__call__(self, environ, start_response)
71
72    def _datetimeToGeoServerDate(self, datetime, datetimeQueryFormat):
73        if datetimeQueryFormat == 'UTC_TIMESTAMP':
74            # Use the UTC timestamp format, generate timestamp.
75            # (would like to do this using the datetime.strftime() method but it only supplies years after 1900).
76            dt = datetime
77            return '%04d-%02d-%02dT%02d:%02d:%02d' % (dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second)
78        elif datetimeQueryFormat == '1-JAN-1970':
79            # Use format e.g. "1-JAN-1970" or "31-MAR-2008".
80            monthCodes = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
81            return repr(datetime.day) + '-' + monthCodes[datetime.month-1] + '-' + repr(datetime.year)
82        else:
83            raise ValueError('Wrong keyword in the <DatetimeQueryFormat> tag in the config file.')
84
85    def _retrieveCsmlPointSeriesFeature(self, dataset, station_name, feature_id):
86        '''
87        @return: A C{PointSeriesFeature} object representing the single CSML feature if the feature was found;
88        C{None} if a feature collection has been returned, but contains no CSML features (this happens when
89        there are no measured time points in the used time interval).
90        @throws: A C{LookupError} exception with a message if there is no response from the server.
91        '''
92     
93        # Prepare the request for the dataset's GeoServer
94        geoServerRequestUrl = urllib.quote(dataset.geoServerUrl + '?request=getfeature&service=wfs&version=1.1.0&typename=csml:PointSeriesFeature&filter=<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml"><ogc:And><ogc:PropertyIsEqualTo><ogc:PropertyName>gml:description</ogc:PropertyName><ogc:Literal>'+ station_name +'</ogc:Literal></ogc:PropertyIsEqualTo><ogc:PropertyIsEqualTo><ogc:PropertyName>csml:parameter/swe:Phenomenon/gml:name</ogc:PropertyName><ogc:Literal>'+ feature_id +'</ogc:Literal></ogc:PropertyIsEqualTo><ogc:PropertyIsBetween><ogc:PropertyName>csml:value/csml:PointSeriesCoverage/csml:pointSeriesDomain/csml:TimeSeries/csml:timePositionList</ogc:PropertyName><ogc:LowerBoundary><ogc:Literal>'+ self._datetimeToGeoServerDate(self.displayIntervalStart, dataset.datetimeQueryFormat) +'</ogc:Literal></ogc:LowerBoundary><ogc:UpperBoundary><ogc:Literal>'+ self._datetimeToGeoServerDate(self.displayIntervalEnd, dataset.datetimeQueryFormat) +'</ogc:Literal></ogc:UpperBoundary></ogc:PropertyIsBetween></ogc:And></ogc:Filter>', '/._:?&=')
95
96        # Acquire the GeoServer response and parse it into a CSMLFeatureCollection object
97        print geoServerRequestUrl
98        try:
99            geoServerResponse = wget(geoServerRequestUrl)
100        except URLError:
101            raise LookupError('Cannot acquire response from server (wrong URL or server down)')
102        csmlFeatureCollection = csml.parser.CSMLFeatureCollection()
103        csmlFeatureCollection.fromXML( XML(geoServerResponse) )
104
105        # If csmlFeatureCollection contains a single CSML feature, the isolate it. If it contains no feature,
106        # probably no data points are available for that interval.
107        try:
108            csmlFeature = csmlFeatureCollection.featureMembers
109        except AttributeError:
110            raise LookupError(
111                'No data points available for the given time interval. ' + 
112                'Perhaps the interval is shorter than the frequency of data points, ' +
113                'or collection of data for this feature was interrupted at the time.'
114                )
115
116        # Make sure that the feature is a PointSeriesFeature
117        if not isinstance(csmlFeature, csml.parser.PointSeriesFeature):
118            raise LookupError('CSML feature not a PointSeriesFeature')
119        csmlPointSeriesFeature = csmlFeature
120
121        return csmlPointSeriesFeature
122
123    def plot(self):
124        '''
125        Handler for plotting a specific PointSeries feature given by URL parameter "feature_id".
126        Retrieves the data from a web service, which should preferably run on the same machine.
127
128        Request parameters:
129                               dataset_id      Unique identifier of the dataset to use.
130                               feature_id      Unique identifier of the requested CSML PointSeries feature.
131                               station_name    Name of the requested station.
132                               
133        Response:              An image/png of the time series for the CSML PointSeries feature.
134        '''
135        def _plot_feature(feature, dataset):
136            '''
137            Plot a PointSeries feature using matplotlib, into a temporary file
138            '''
139           
140            # Make sure that time is considered to be UTC time. This is because all CSML data is supposed to be in UTC,
141            # but actually pylab is "time zone aware" and will interpret the time as local time zone time.
142            # Not quite sure if this would be a problem, but rather enforce it is UTC.
143            def _enforce_UTC_timezone(datestr):
144                if datestr[-1] == 'Z':
145                    return datestr
146                else:
147                    return datestr + 'Z'
148           
149            # Isolate time points
150            times=feature.value.pointSeriesDomain.timePositionList.CONTENT.split()
151            times = map(_enforce_UTC_timezone, times)
152
153            # Make a list of times as float numbers being days passed since start of epoch (here 01-01-0001).
154            # (We need to use the wrapping function csml2kml.utils.parseTimestamp() because unfortunately the matplotlib's
155            #  datetime parser cannot handle timestamps like "2004-02-17T24:00:00" directly.)
156            elapsed_times = map(date2num,
157                                map(csml2kml.utils.parseTimestamp,
158                                    times
159                                    )
160                                )
161
162            # Get values of the measured quantity
163            ql = feature.value.rangeSet.quantityList
164            vals = map(float, ql.CONTENT.split())
165
166            # Make sure the times are sorted in ascending order (and rearrange the values accordingly),
167            # this is because we get them potentially unsorted from GeoServer. Also have to handle the sad fact,
168            # that sometimes we are getting duplicate time-value samples from the MIDAS dataset!
169            sortTimesExplicitly = True
170            if sortTimesExplicitly:
171                reorder = {}
172                for i in range(len(elapsed_times)):
173                    reorder[elapsed_times[i]] = i
174                elapsed_times = reorder.keys()
175                elapsed_times.sort()
176                times2 = []
177                vals2 = []
178                for et in elapsed_times:
179                    times2.append(times[reorder[et]])
180                    vals2.append(vals[reorder[et]])
181                times = times2
182                vals = vals2
183
184            print '---times (' + str(len(times)) + ' of them):\n' + str(times)
185            print '---vals (' + str(len(times)) + ' of them):\n' + str(vals)
186
187            # Note the start and end date (they are actually date+time), and time span between them
188
189            start_date = dates.num2date(elapsed_times[0])
190            end_date = dates.num2date(elapsed_times[-1])
191            span = elapsed_times[-1] - elapsed_times[0]
192
193            # Generate tick locator and formatter; these determine the time axis: locator determines tick positions
194            # and formatter format of time
195            print '---start_date:' + str(start_date)
196            print '---end_date:' + str(end_date)
197            print '---span: ' + str(span)
198            (tickLocator, tickFormatter) = dates.date_ticker_factory(span, numticks=8)
199
200            # Determine the units of measurement
201            uom=ql.uom.title()
202            if ql.uom.islower():
203                uom = uom.lower()
204            if ql.uom.isupper():
205                uom = uom.upper()
206
207            # Prepare the plot the figure (actual plotting actions are postponed until save)
208            fig = figure()
209            plot_date(elapsed_times, vals, 'b-', xdate=True, lw=1)
210            plot_date(elapsed_times, vals, 'go', markeredgecolor = 'g', xdate=True, lw=2)
211            ax = gca()
212            ax.xaxis.set_major_locator(tickLocator)
213            ax.xaxis.set_major_formatter(tickFormatter)
214            fig.autofmt_xdate()  # show times without overlaps
215            time_format = '%d-%b-%Y %H:%M:%S UTC'
216            xlabel('Times between %s and %s ' % (start_date.strftime(time_format), end_date.strftime(time_format)))
217            ylabel('Values [%s]' % uom)
218            title('"%s:%s" (%s station %s)' % (dataset.id, feature.id, dataset.name, feature.description.CONTENT))
219            grid(True)
220
221            # Save the figure to a temporary file
222            tempFile = NamedTemporaryFile(suffix='.png')
223            savefig(tempFile.name)
224           
225            return tempFile
226
227        def _set_response(tempFile):
228            '''
229            Set the WSGI response to an image, containing image read from a temporary location.
230            '''
231            img = Image.open(tempFile.name)
232            buf = StringIO()
233            img.save(buf, 'PNG')
234            response.content_type = 'image/png'
235            response.content = buf.getvalue()
236           
237        #----------------------
238
239        # Get parameters from the request object
240        try:
241            dataset_id = str(request.params['dataset_id'])     # convert back from Unicode
242            feature_id = str(request.params['feature_id'])     # convert back from Unicode
243            station_name = str(request.params['station_name']) # convert back from Unicode
244        except KeyError:
245            raise HTTPBadRequest('Parameters "dataset_id", "feature_id" and "station_name" must be supplied.')
246
247        # Try to retrieve the csmlPointSeriesFeature object from the appropriate dataset
248        try:
249            dataset = self.datasets[dataset_id]
250            csmlPointSeriesFeature = self._retrieveCsmlPointSeriesFeature(dataset, station_name, feature_id)
251        except LookupError, e:
252            errorMessage = e.args[0]
253            httpNotFound = HTTPNotFound()
254            httpNotFound.explanation = ''
255            httpNotFound.detail = errorMessage
256            raise httpNotFound
257
258        # Try to plot the feature into a temporary file, and put the contents of that file into the response
259        try:
260            tempFile = _plot_feature(csmlPointSeriesFeature, dataset)    # plot the feature into a temporary file
261            _set_response(tempFile)                                      # set the response as an image containing the plot
262        finally:
263            try:
264                tempFile.close()
265            except NameError:
266                pass  # tempFile undefined -- no need to close
267
268    def list(self):
269        '''
270        Handler for generating a list of CSML features per station, with dynamic links to the plot service.
271        Request parameters:
272                               dataset_id      Unique identifier of the dataset to use.
273                               station_name    Name of the requested station.
274                               
275        Response:              A text/html with the page containing links to the plot service on the same server.
276        '''
277
278        # Get parameters from the request object
279        try:
280            dataset_id = str(request.params['dataset_id'])      # convert back from Unicode
281            station_name = str(request.params['station_name'])  # convert back from Unicode
282        except KeyError:
283            raise HTTPBadRequest('Parameters "dataset_id" and "station_name" must be supplied.')
284
285        try:
286            # Identify the appropriate dataset.
287            try:
288                dataset = self.datasets[dataset_id]
289            except KeyError:
290                raise LookupError('Wrong dataset identifier.')
291           
292            # Prepare the request for the GeoServer -- to return a collection of np:Station, containing a single np:Station,
293            # which contains a list of CSML features.
294            geoServerRequestUrl = urllib.quote(dataset.geoServerUrl + '?request=getFeature&service=wfs&version=1.1.0&typename=np:Station&filter=<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml"><ogc:PropertyIsEqualTo><ogc:PropertyName>stationName</ogc:PropertyName><ogc:Literal>' + station_name + '</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Filter>', '/._:?&=')
295
296            # Acquire the GeoServer response and parse it into a WFSStationCollection object
297            try:
298                geoServerResponse = wget(geoServerRequestUrl)
299            except URLError:
300                raise LookupError('Cannot access underlying WFS (server error, wrong network connection, or typo in the config file).')
301
302            wfsStationCollection = csml2kml.Station.WFSStationCollection()
303            wfsStationCollection.parseString(geoServerResponse)
304            if len(wfsStationCollection.stations) != 1:
305                raise LookupError('Requested station not found.')
306            wfsStation = wfsStationCollection.stations[0]
307        except LookupError, e:
308            errorMessage = e.args[0]
309            httpNotFound = HTTPNotFound()
310            httpNotFound.explanation = ''
311            httpNotFound.detail = errorMessage
312            raise httpNotFound
313
314        # Generate HTML with a list of CSML features contained in the station.
315        # Currently, this is done simply by directly generating HTML (i.e. no template was used).
316        htmlElement = Element('html')
317        SubElement(htmlElement, 'title').text = 'List of CSML features for station ' + station_name
318        bodyElement = SubElement(htmlElement, 'body')
319        SubElement(bodyElement, 'h2').text = 'List of CSML features for station ' + station_name
320        tableElement = SubElement(bodyElement, 'table')
321        tableElement.set('border', '1')
322        headingRowElement = SubElement(tableElement, 'tr')
323        SubElement(headingRowElement, 'th').text = 'Feature name'
324        SubElement(headingRowElement, 'th').text = 'Data collected since'
325        SubElement(headingRowElement, 'th').text = 'Data collected until '
326        for stationFeature in wfsStation.stationFeatures:
327            if self.displayIntervalStart >= stationFeature.collectBeginDate and self.displayIntervalEnd <= stationFeature.collectEndDate:
328                rowElement = SubElement(tableElement, 'tr')
329                featureNameElement = SubElement(rowElement, 'td')
330                anchorElement = SubElement(featureNameElement, 'a')
331                linkToGrapher = self.servedFromUrl + '/plot?dataset_id=' + dataset_id + '&station_name=' + station_name + '&feature_id=' + stationFeature.featureId
332                anchorElement.set('href', linkToGrapher)
333                anchorElement.text = stationFeature.featureId
334                SubElement(rowElement, 'td').text = self._datetimeToGeoServerDate(stationFeature.collectBeginDate)
335                SubElement(rowElement, 'td').text = self._datetimeToGeoServerDate(stationFeature.collectEndDate)
336
337        htmlStringIO = StringIO()
338        ElementTree(htmlElement).write(htmlStringIO)
339
340        response.content_type = 'text/html'
341        response.content = htmlStringIO.getvalue()
Note: See TracBrowser for help on using the repository browser.