source: TI02-CSML/trunk/services/3rdParty/pywps-1.0.0/pywps/processes/ndvi.py @ 2194

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/services/3rdParty/pywps-1.0.0/pywps/processes/ndvi.py@2194
Revision 2194, 4.6 KB checked in by lawrence, 13 years ago (diff)

Adding various specs and 3rd party code of interest for the CSML
services development.

Line 
1#!/usr/bin/python
2"""
3pywps process example:
4
5ndvi
6"""
7# Author:       Jachym Cepicky
8#               http://les-ejk.cz
9# Lince:
10#
11# Web Processing Service implementation
12# Copyright (C) 2006 Jachym Cepicky
13#
14# This program is free software; you can redistribute it and/or modify
15# it under the terms of the GNU General Public License as published by
16# the Free Software Foundation; either version 2 of the License.
17#
18# This program is distributed in the hope that it will be useful,
19# but WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21# GNU General Public License for more details.
22#
23# You should have received a copy of the GNU General Public License
24# along with this program; if not, write to the Free Software
25# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
26
27import os
28
29class Process:
30#####################################################################
31#
32# Configuration part of the process
33#
34#####################################################################
35    def __init__(self):
36        #
37        # Mandatory parameters
38        #
39        # Identifier - name of this process
40        self.Identifier = "ndvi"
41        # processVersion - version of this process
42        self.processVersion = "0.1"
43        # Title - title for this process
44        self.Title="NDVI"
45        self.Abstract = """
46        Calculates normalized vegetation index: NDVI =(LANDSAT.4 - LANDSAT.3) / (LANDSAT.4 + LANDSAT.3)" or
47        NDVI =(nir - red) / (nir + red)"
48
49        """
50       
51        #
52        # Inputs
53        #
54        self.Inputs = [
55       
56                # 0 input
57                    {
58                        'Identifier': 'landsat3',
59                        'Title': 'Landsat 3',
60                        'Abstract':'Channel 3 of LANDSAT TM (RED)',
61                        'ComplexValueReference': {
62                            'Formats':['image/tif','image/png'], # AllowedValues, AnyValue, ValuesReference
63                        },
64                        'dataType' : type(''), # you should set this for input params controll
65                        'value':None,
66                    },
67                # 1 input
68                    {
69                        'Identifier': 'landsat4',
70                        'Title': 'Landsat 4',
71                        'Abstract':'Channel 4 of LANDSAT TM (NIR)',
72                        'ComplexValueReference': {
73                            'Formats':['image/tif','image/png'], # AllowedValues, AnyValue, ValuesReference
74                        },
75                        'dataType' : type(''), # you should set this for input params controll
76                        'value':None,
77                    },
78
79                ]
80       
81        #
82        # Output
83        #
84        self.Outputs = [
85            {
86               'Identifier': 'ndvi',
87               'Title': 'NDVI',
88               'ComplexValueReference': {'Formats':["image/tif"]},
89               'value': 'ndvi.tif',
90           },
91        ]
92       
93        #
94        # Optional attributes
95        #
96        #
97        # storeSuport = "true" or "false" - should the resulting map be stored on our disk?
98        self.storeSupported = "true"
99       
100        #
101        # statusSupported = "true" or "false" - if statusLocation is set, the server
102        # will not wait for the end of the operation and will return the
103        # ExectuceResponce XML file immediately, without the ProcessOutput section
104        # but with the statusLocation parameter
105        self.statusSupported = "true"
106        # and many others
107
108    #####################################################################
109    #
110    # Execute part of the process
111    #
112    #####################################################################
113    def execute(self):
114        """
115        NDVI
116        """
117
118        # import
119        if os.system("r.in.gdal in=%s out=red >&2" % (self.Inputs[0]['value'])):
120            return "Could not import <%s>" % (self.Inputs[0]['value'])
121        if os.system("r.in.gdal in=%s out=nir >&2" % (self.Inputs[1]['value'])):
122            return "Could not import <%s>" % (self.Inputs[2]['value'])
123       
124        # ndvi
125        if os.system("""r.mapcalc ndvi"="float(nir-red)/float(nir+red)" >&2"""):
126            return   """Could not r.mapcalc ndvi"="float(nir-red)/float(nir+red)" """
127       
128        # export
129        if os.system("""r.out.gdal in=ndvi out=ndvi.tif type=Int32 >&2"""):
130            return   """Could r.out.gdal in=ndvi out=ndvi.tif type=Int32 """
131       
132if __name__ == "__main__":
133       
134    ps = Process()
135    ps.Inputs[0]['value'] = "red"
136    ps.Inputs[1]['value'] = "nir"
137
138    ps.execute()
139    print ps.Outputs[0]
Note: See TracBrowser for help on using the repository browser.