-
Notifications
You must be signed in to change notification settings - Fork 1
/
wps_multivar_indice.py
175 lines (128 loc) · 8.31 KB
/
wps_multivar_indice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from pywps.Process import WPSProcess
import icclim
import icclim.util.callback as callback
import dateutil.parser
from datetime import datetime
import os
from os.path import expanduser
from mkdir_p import *
transfer_limit_Mb = 100
class ProcessMultivarIndice(WPSProcess):
def __init__(self):
WPSProcess.__init__(self,
identifier = 'wps_multivar_indice', # only mandatary attribute = same file name
title = 'RangeIndices',
abstract = 'Computes temperature range indices: ETR, DTR, DTRv.',
version = "1.0",
storeSupported = True,
statusSupported = True,
grassLocation =False)
self.indiceNameIn = self.addLiteralInput(identifier = 'indiceName',
title = 'Indice name',
type="String",
default = 'ETR')
self.indiceNameIn.values = ['DTR', 'ETR', 'vDTR']
self.sliceModeIn = self.addLiteralInput(identifier = 'sliceMode',
title = 'Slice mode (temporal grouping to applay for calculations)',
type="String",
default = 'year')
self.sliceModeIn.values = ["year","month","ONDJFM","AMJJAS","DJF","MAM","JJA","SON"]
self.filesTasmaxIn = self.addLiteralInput(identifier = 'filesTasmax',
title = 'Input netCDF files list (daily max temperature)',
abstract="application/netcdf",
type=type("S"),
minOccurs=0,
maxOccurs=1024,
default = 'http://opendap.knmi.nl/knmi/thredds/dodsC/IS-ENES/TESTSETS/tasmax_day_EC-EARTH_rcp26_r8i1p1_20060101-20251231.nc,' +
'http://opendap.knmi.nl/knmi/thredds/dodsC/IS-ENES/TESTSETS/tasmax_day_EC-EARTH_rcp26_r8i1p1_20260101-20501231.nc,' +
'http://opendap.knmi.nl/knmi/thredds/dodsC/IS-ENES/TESTSETS/tasmax_day_EC-EARTH_rcp26_r8i1p1_20510101-20751231.nc,' +
'http://opendap.knmi.nl/knmi/thredds/dodsC/IS-ENES/TESTSETS/tasmax_day_EC-EARTH_rcp26_r8i1p1_20760101-21001231.nc')
self.varTasmaxIn = self.addLiteralInput(identifier = 'varTasmax',
title = 'Variable name to process (daily max temperature)',
type="String",
default = 'tasmax')
self.filesTasminIn = self.addLiteralInput(identifier = 'filesTasmin',
title = 'Input netCDF files list (daily min temperature)',
abstract="application/netcdf",
type=type("S"),
minOccurs=0,
maxOccurs=1024,
default = 'http://opendap.knmi.nl/knmi/thredds/dodsC/IS-ENES/TESTSETS/tasmin_day_EC-EARTH_rcp26_r8i1p1_20060101-20251231.nc,' +
'http://opendap.knmi.nl/knmi/thredds/dodsC/IS-ENES/TESTSETS/tasmin_day_EC-EARTH_rcp26_r8i1p1_20260101-20501231.nc,' +
'http://opendap.knmi.nl/knmi/thredds/dodsC/IS-ENES/TESTSETS/tasmin_day_EC-EARTH_rcp26_r8i1p1_20510101-20751231.nc,' +
'http://opendap.knmi.nl/knmi/thredds/dodsC/IS-ENES/TESTSETS/tasmin_day_EC-EARTH_rcp26_r8i1p1_20760101-21001231.nc')
self.varTasminIn = self.addLiteralInput(identifier = 'varTasmin',
title = 'Variable name to process (daily min temperature)',
type="String",
default = 'tasmin')
self.timeRangeIn = self.addLiteralInput(identifier = 'timeRange',
title = 'Time range, e.g. 2010-01-01/2012-12-31',
type="String",
default = None)
self.outputFileNameIn = self.addLiteralInput(identifier = 'outputFileName',
title = 'Name of output netCDF file',
type="String",
default = './out_icclim.nc')
self.NLevelIn = self.addLiteralInput(identifier = 'NLevel',
title = 'Number of level (if 4D variable)',
type="String",
default = None)
self.opendapURL = self.addLiteralOutput(identifier = "opendapURL",title = "opendapURL");
def callback(self,message,percentage):
self.status.set("%s" % str(message),str(percentage));
def execute(self):
# Very important: This allows the NetCDF library to find the users credentials (X509 cert)
homedir = os.environ['HOME']
os.chdir(homedir)
def callback(b):
self.callback("Processing",b)
files_tasmax = [];
files_tasmax.extend(self.filesTasmaxIn.getValue())
var_tasmax = self.varTasmaxIn.getValue()
files_tasmin = [];
files_tasmin.extend(self.filesTasminIn.getValue())
var_tasmin = self.varTasminIn.getValue()
indice_name = self.indiceNameIn.getValue()
slice_mode = self.sliceModeIn.getValue()
time_range = self.timeRangeIn.getValue()
out_file_name = self.outputFileNameIn.getValue()
level = self.NLevelIn.getValue()
if(level == "None"):
level = None
if(time_range == "None"):
time_range = None
else:
startdate = dateutil.parser.parse(time_range.split("/")[0])
stopdate = dateutil.parser.parse(time_range.split("/")[1])
time_range = [startdate,stopdate]
self.status.set("Preparing....", 0)
pathToAppendToOutputDirectory = "/WPS_"+self.identifier+"_" + datetime.now().strftime("%Y%m%dT%H%M%SZ")
""" URL output path """
fileOutURL = os.environ['POF_OUTPUT_URL'] + pathToAppendToOutputDirectory+"/"
""" Internal output path"""
fileOutPath = os.environ['POF_OUTPUT_PATH'] + pathToAppendToOutputDirectory +"/"
""" Create output directory """
mkdir_p(fileOutPath)
self.status.set("Processing input lists: " + str(files_tasmax) + " " + str(files_tasmin), 0)
icclim.indice(indice_name=indice_name,
in_files=[files_tasmax,files_tasmin],
var_name=[var_tasmax,var_tasmin],
slice_mode=slice_mode,
time_range=time_range,
out_file=fileOutPath+out_file_name,
N_lev=level,
transfer_limit_Mbytes=transfer_limit_Mb,
callback=callback,
callback_percentage_start_value=0,
callback_percentage_total=100,
base_period_time_range=None,
window_width=5,
only_leap_years=False,
ignore_Feb29th=True,
interpolation='hyndman_fan',
netcdf_version='NETCDF4_CLASSIC',
out_unit='days')
""" Set output """
url = fileOutURL+"/"+out_file_name;
self.opendapURL.setValue(url);
self.status.set("ready",100);