pyTSEB 2.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,279 @@
1
+ # This file is part PyTSEB, consisting of of high level pyTSEB scripting
2
+ # Copyright 2016 Hector Nieto and contributors listed in the README.md file.
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU Lesser General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU Lesser General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU Lesser General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ from configparser import ConfigParser, NoOptionError
18
+ import itertools
19
+
20
+ from .PyTSEB import PyTSEB, PyTSEB2T, PyDTD, PydisTSEB
21
+
22
+
23
+ class ParserError(Exception):
24
+
25
+ def __init__(self, parameter, expected_type):
26
+ self.param = parameter
27
+ self.type = expected_type
28
+
29
+
30
+ class MyConfigParser(ConfigParser):
31
+
32
+ def __init__(self, top_section, *args, **kwargs):
33
+ super().__init__(*args, inline_comment_prefixes=('#',), **kwargs)
34
+ self.section = top_section
35
+
36
+ def myget(self, option, **kwargs):
37
+ return super().get(self.section, option, **kwargs)
38
+
39
+ def getint(self, option, **kwargs):
40
+ try:
41
+ val = super().getint(self.section, option, **kwargs)
42
+ except ValueError:
43
+ raise ParserError(option, 'int')
44
+
45
+ return val
46
+
47
+ def getfloat(self, option, **kwargs):
48
+ try:
49
+ val = super().getfloat(self.section, option, **kwargs)
50
+ except ValueError:
51
+ raise ParserError(option, 'float')
52
+
53
+ return val
54
+
55
+ def has_option(self, option):
56
+ return super().has_option(self.section, option)
57
+
58
+
59
+ class TSEBConfigFileInterface():
60
+
61
+ SITE_DESCRIPTION = [
62
+ 'landcover',
63
+ 'lat',
64
+ 'lon',
65
+ 'alt',
66
+ 'stdlon',
67
+ 'z_T',
68
+ 'z_u',
69
+ 'z0_soil'
70
+ ]
71
+
72
+ VEGETATION_PROPERTIES = [
73
+ 'leaf_width',
74
+ 'alpha_PT',
75
+ 'x_LAD'
76
+ ]
77
+
78
+ SPECTRAL_PROPERTIES = [
79
+ 'emis_C',
80
+ 'emis_S',
81
+ 'rho_vis_C',
82
+ 'tau_vis_C',
83
+ 'rho_nir_C',
84
+ 'tau_nir_C',
85
+ 'rho_vis_S',
86
+ 'rho_nir_S'
87
+ ]
88
+
89
+ MODEL_FORMULATION = [
90
+ 'model',
91
+ 'resistance_form',
92
+ 'KN_b',
93
+ 'KN_c',
94
+ 'KN_C_dash',
95
+ 'G_form',
96
+ 'G_constant',
97
+ 'G_ratio',
98
+ 'G_amp',
99
+ 'G_phase',
100
+ 'G_shape',
101
+ 'calc_row',
102
+ 'row_az',
103
+ 'output_file',
104
+ 'correct_LST',
105
+ 'flux_LR_method',
106
+ 'water_stress'
107
+ ]
108
+
109
+ IMAGE_VARS = [
110
+ 'T_R1',
111
+ 'T_R0',
112
+ 'VZA',
113
+ 'LAI',
114
+ 'f_c',
115
+ 'f_g',
116
+ 'h_C',
117
+ 'w_C',
118
+ 'input_mask',
119
+ 'subset',
120
+ 'time',
121
+ 'DOY',
122
+ 'T_A1',
123
+ 'T_A0',
124
+ 'u',
125
+ 'ea',
126
+ 'S_dn',
127
+ 'L_dn',
128
+ 'p',
129
+ 'flux_LR',
130
+ 'flux_LR_ancillary',
131
+ 'S_dn_24',
132
+ 'SZA',
133
+ 'SAA',
134
+ ]
135
+
136
+ POINT_VARS = [
137
+ 'f_c',
138
+ 'f_g',
139
+ 'w_C'
140
+ ]
141
+
142
+ def __init__(self):
143
+
144
+ self.params = {}
145
+ self.ready = False
146
+
147
+ @staticmethod
148
+ def parse_input_config(input_file, **kwargs):
149
+ ''' Parses the information contained in a configuration file into a dictionary'''
150
+
151
+ parser = MyConfigParser('top')
152
+ with open(input_file) as conf_file:
153
+ conf_file = itertools.chain(('[top]',), conf_file) # dummy section to please parser
154
+ parser.read_file(conf_file)
155
+
156
+ return parser
157
+
158
+ @staticmethod
159
+ def _parse_common_config(parser):
160
+ """Parse all the stuff that's the same for image and point"""
161
+
162
+ conf = {}
163
+
164
+ conf['model'] = parser.myget('model')
165
+ conf['output_file'] = parser.myget('output_file')
166
+
167
+ conf['resistance_form'] = parser.getint('resistance_form', fallback=None)
168
+
169
+ conf['water_stress'] = parser.getint('water_stress', fallback=False)
170
+ if conf['water_stress']:
171
+ conf['Rst_min'] = parser.getfloat('Rst_min', fallback=100)
172
+ conf['R_ss'] = parser.getfloat('R_ss', fallback=500)
173
+
174
+ conf['calc_row'] = parser.getint('calc_row', fallback=[0, 0])
175
+
176
+ if conf['calc_row'] != [0, 0]:
177
+ row_az = parser.getfloat('row_az')
178
+ conf['calc_row'] = [1, row_az]
179
+
180
+ g_form = parser.getint('G_form', fallback=1)
181
+ if g_form == 0:
182
+ g_constant = parser.getfloat('G_constant')
183
+ conf['G_form'] = [[0], g_constant]
184
+ elif g_form == 2:
185
+ g_params = [parser.getfloat(p) for p in ('G_amp', 'G_phase', 'G_shape')]
186
+ conf['G_form'] = [[2, *g_params], 12.0]
187
+ else:
188
+ g_ratio = parser.getfloat('G_ratio')
189
+ conf['G_form'] = [[1], g_ratio]
190
+
191
+ if conf['model'] == 'disTSEB':
192
+ conf['flux_LR_method'] = parser.myget('flux_LR_method')
193
+ conf['correct_LST'] = parser.getint('correct_LST')
194
+
195
+ return conf
196
+
197
+ @staticmethod
198
+ def _parse_image_config(parser, conf):
199
+ """Parse the image specific things"""
200
+
201
+ # remaining in MODEL_FORMULATION
202
+ conf.update({p: parser.myget(p) for p in ['KN_b', 'KN_c', 'KN_C_dash']})
203
+
204
+ conf.update({p: parser.myget(p) for p in TSEBConfigFileInterface.SITE_DESCRIPTION})
205
+ conf.update({p: parser.myget(p) for p in TSEBConfigFileInterface.VEGETATION_PROPERTIES})
206
+ conf.update({p: parser.myget(p) for p in TSEBConfigFileInterface.SPECTRAL_PROPERTIES})
207
+
208
+ img_vars = set(TSEBConfigFileInterface.IMAGE_VARS)
209
+ if conf['model'] != 'DTD':
210
+ img_vars -= set(['T_A0', 'T_R0'])
211
+ if conf['model'] != 'disTSEB':
212
+ img_vars -= set(['flux_LR', 'flux_LR_ancillary'])
213
+ if not parser.has_option('subset'):
214
+ img_vars.remove('subset')
215
+
216
+ for p in img_vars:
217
+ if p in ['S_dn_24', "SZA", "SAA"]:
218
+ conf.update({p: parser.myget(p, fallback='')})
219
+ else:
220
+ conf.update({p: parser.myget(p)})
221
+
222
+ return conf
223
+
224
+ @staticmethod
225
+ def _parse_point_config(parser, conf):
226
+ """Parse the point specific things"""
227
+
228
+ # remaining in MODEL_FORMULATION
229
+ conf.update({p: parser.getfloat(p) for p in ['KN_b', 'KN_c', 'KN_C_dash']})
230
+
231
+ conf.update({p: parser.getfloat(p) for p in TSEBConfigFileInterface.SITE_DESCRIPTION})
232
+ conf.update({p: parser.getfloat(p) for p in TSEBConfigFileInterface.VEGETATION_PROPERTIES})
233
+ conf.update({p: parser.getfloat(p) for p in TSEBConfigFileInterface.SPECTRAL_PROPERTIES})
234
+
235
+ conf['input_file'] = parser.myget('input_file')
236
+ conf.update({p: parser.getfloat(p) for p in TSEBConfigFileInterface.POINT_VARS})
237
+
238
+ return conf
239
+
240
+ def get_data(self, parser, is_image):
241
+ '''Parses the parameters in a configuration file directly to TSEB variables for running
242
+ TSEB'''
243
+
244
+ conf = self._parse_common_config(parser)
245
+
246
+ try:
247
+ if is_image:
248
+ conf = self._parse_image_config(parser, conf)
249
+ else:
250
+ conf = self._parse_point_config(parser, conf)
251
+ self.ready = True
252
+ except NoOptionError as e:
253
+ print(f'Error: missing parameter {e.option}')
254
+ except ParserError as e:
255
+ print(f'Error: could not parse parameter {e.param} as type {e.type}')
256
+
257
+ self.params = conf
258
+
259
+ def run(self, is_image):
260
+
261
+ if self.ready:
262
+ if self.params['model'] == "TSEB_PT":
263
+ model = PyTSEB(self.params)
264
+ elif self.params['model'] == "TSEB_2T":
265
+ model = PyTSEB2T(self.params)
266
+ elif self.params['model'] == "DTD":
267
+ model = PyDTD(self.params)
268
+ elif self.params['model'] == "disTSEB":
269
+ model = PydisTSEB(self.params)
270
+ else:
271
+ print("Unknown model: " + self.params['model'] + "!")
272
+ return None
273
+ if is_image:
274
+ model.process_local_image()
275
+ else:
276
+ in_data, out_data = model.process_point_series_array()
277
+ return in_data, out_data
278
+ else:
279
+ print("pyTSEB will not be run due to errors in the input data.")