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

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

Extended csmlGrapher to allow multiple datasets (that is, data served from multiple WFSs).

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