Unit 28 - PyWPS LST point statsΒΆ
A second version of the WPS process will return LST statistics for
given point in Germany (referenced in EPSG:3035). A new input
parameter (point
) is defined on 18-19. The
output is defined as JSON, the class is changed from LiteralOutput
to ComplexOutput
, see line 9,25-26.
The pixel extraction is done by t.rast.what on line
53. Statistics is computed based on command output manually
on lines 62-84. Resultant Python dictionary is converted to
JSON structure using json
Python package on line 86. The
json
package is imported on line 3.
Note that also class name (28), process identifier (30), title (31), abstract (36) and version (37) needs to be updated.
#!/usr/bin/env python3
import json
from subprocess import PIPE
from datetime import datetime
from grass.pygrass.modules import Module
from grass.script import parse_key_val
from pywps import Process, LiteralInput, ComplexOutput, Format
class ModisV2(Process):
def __init__(self):
inputs = list()
outputs = list()
inputs.append(LiteralInput('coordinates', 'ETRS-89 coordinates',
data_type='string'))
inputs.append(LiteralInput('start', 'Start date (eg. 2023-03-01)',
data_type='string'))
inputs.append(LiteralInput('end', 'End date (eg. 2023-03-01)',
data_type='string'))
outputs.append(ComplexOutput('output', 'Computed LST statistics',
supported_formats=[Format('application/json')]))
super(ModisV2, self).__init__(
self._handler,
identifier="modis-v2",
title="Modis process (v2)",
inputs=inputs,
outputs=outputs,
# here you could also specify the GRASS location, for example:
# grass_location="EPSG:5514",
abstract="Computes LST stats for given location and period (limited to Germany and 2023).",
version="0.2",
store_supported=True,
status_supported=True)
@staticmethod
def _handler(request, response):
def check_date(date_str):
d = datetime.strptime(date_str, '%Y-%m-%d')
if d.year != 2023:
raise Exception("Only year 2023 allowed")
check_date(request.inputs['start'][0].data)
check_date(request.inputs['end'][0].data)
x, y = request.inputs['coordinates'][0].data.split(',')
m = Module('t.rast.what',
strds='modis_c@PERMANENT',
coordinates=[x, y],
separator=',',
where="start_time >= '{}' and start_time < '{}'".format(
request.inputs["start"][0].data,
request.inputs["end"][0].data),
stdout_=PIPE)
stats = {
'min' : None,
'max' : None,
'mean' : None,
}
tsum = 0
count = 0
for line in m.outputs.stdout.splitlines():
items = line.split(',')
if items[-1] == '*': # no data
continue
val = float(items[-1])
if stats['min'] is None:
stats['min'] = stats['max'] = stats['mean'] = val
else:
if val < stats['min']:
stats['min'] = val
if val > stats['max']:
stats['max'] = val
tsum += val
count += 1
stats['mean'] = tsum / count
response.outputs['output'].data = json.dumps(stats)
return response
if __name__ == "__main__":
from pywps.app.Service import Service
processes = [ModisV2()]
application = Service(processes)
Sample process to download: modis_v2.py
Important
Do not forget to import process as done in Unit 27 and restart demo PyWPS server.
Example of execute request:
Possible response.
<wps:ComplexData mimeType="application/json" encoding="" schema="">
{"min": -3.54999999999995, "max": 15.33, "mean": 4.673076923076951}
</wps:ComplexData>
Task
Try to improve the process in order to validate coordinates. Only point located in Germany is allowed.