Unit 29 - PyWPS LST region statsΒΆ

A third version of the process will return LST statistics for region in Germany defined by input vector (polygon) data. Input area of interest is defined as region ComplexInput parameter (9,18-21) and imported by v.import (line 53), resultant statistics is computed similarly as in process verion 1 (Unit 27 - PyWPS intro) by t.rast.series (line 59) and r.univar (line 72). Before computation the computational region and mask is set by g.region (56) and r.mask (57). Statistics is provided in JSON structure similary to process version 2 (Unit 28 - PyWPS LST point stats) on line 83.

Note that also class name (30), process identifier (32), title (33), abstract (38) and version (39) 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, ComplexInput, ComplexOutput, Format


class ModisV3(Process):

    def __init__(self):
        inputs = list()
        outputs = list()

        inputs.append(ComplexInput('region', 'Input vector region',
                    supported_formats=[
                        Format('text/xml'),
                        Format('application/gml+xml')]))
        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(ModisV3, self).__init__(
            self._handler,
            identifier="modis-v3",
            title="Modis process (v3)",
            inputs=inputs,
            outputs=outputs,
            # here you could also specify the GRASS location, for example:
            # grass_location="EPSG:5514",
            abstract="Computes LST stats for given area and period (limited to Germany and 2023).",
            version="0.3",
            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)

        Module('v.import',
                input=request.inputs['region'][0].file,
                output='poly', overwrite=True)
        Module('g.region', vector='poly', align='c_001')
        Module('r.mask', vector='poly', overwrite=True)

        Module("t.rast.series",
               overwrite=True,
               input="modis_c@PERMANENT",
               method="average",
               order="start_time",
               nprocs=1,
               memory=300,
               where="start_time >= '{}' and start_time < '{}'".format(
                   request.inputs["start"][0].data,
                   request.inputs["end"][0].data),
               output="t_rast_series_out",
               file_limit=1000)

        m = Module("r.univar",
                   flags="g",
                   overwrite=True,
                   map="t_rast_series_out",
                   percentile=90,
                   nprocs=1,
                   separator="pipe",
                   stdout_=PIPE)

        stats = parse_key_val(m.outputs.stdout, val_type=float)

        response.outputs['output'].data = json.dumps(stats)

        return response


if __name__ == "__main__":
    from pywps.app.Service import Service

    processes = [ModisV3()]
    application = Service(processes)

Sample process to download: modis_v3.py

Important

Do not forget to import process as done in Unit 27 and restart demo PyWPS server.

Copy jena_boundary.gpkg to pywps-flask-master/static/data and execute the process.

http://localhost:5000/wps?request=Execute&service=WPS&identifier=modis-v3&version=1.0.0&datainputs=start=2023-03-01;end=2023-04-01;region=@xlink:href=http://localhost:5000/static/data/jena_boundary.gpkg

Possible response.

<wps:ComplexData mimeType="application/json" encoding="" schema="">
{"n": 115.0, "null_cells": 109.0, "cells": 224.0, "min": 1.02076923076927,...}
</wps:ComplexData>