Skip to content

Commit 960cef9

Browse files
authored
Merge pull request #39 from DHI-GRAS/32bit
Use 32 bit floats for most calculations
2 parents 78df401 + 244a0c5 commit 960cef9

File tree

3 files changed

+33
-35
lines changed

3 files changed

+33
-35
lines changed

pyTSEB/PyTSEB.py

+18-19
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def process_local_image(self):
172172
elif field == "input_mask":
173173
if self.p['input_mask'] == '0':
174174
# Create mask from landcover array
175-
mask = np.ones(dims)
175+
mask = np.ones(dims, np.int32)
176176
mask[np.logical_or.reduce((in_data['landcover'] == res.WATER,
177177
in_data['landcover'] == res.URBAN,
178178
in_data['landcover'] == res.SNOW))] = 0
@@ -530,12 +530,12 @@ def run(self, in_data, mask=None):
530530
"resistance_form": [self.resistance_form, self.res_params]}
531531

532532
if mask is None:
533-
mask = np.ones(in_data['LAI'].shape)
533+
mask = np.ones(in_data['LAI'].shape, np.int32)
534534

535535
# Create the output dictionary
536536
out_data = dict()
537537
for field in self._get_output_structure():
538-
out_data[field] = np.zeros(in_data['LAI'].shape) + np.NaN
538+
out_data[field] = np.zeros(in_data['LAI'].shape, np.float32) + np.NaN
539539

540540
# Esimate diffuse and direct irradiance
541541
difvis, difnir, fvis, fnir = rad.calc_difuse_ratio(
@@ -591,10 +591,11 @@ def run(self, in_data, mask=None):
591591
f_c=in_data['f_c'][i])
592592

593593
# Net shortwave radiation for vegetation
594-
F = np.zeros(in_data['LAI'].shape)
594+
F = np.zeros(in_data['LAI'].shape, np.float32)
595595
F[i] = in_data['LAI'][i] / in_data['f_c'][i]
596596
# Clumping index
597-
omega0, Omega = np.zeros(in_data['LAI'].shape), np.zeros(in_data['LAI'].shape)
597+
omega0 = np.zeros(in_data['LAI'].shape, np.float32)
598+
Omega = np.zeros(in_data['LAI'].shape, np.float32)
598599
omega0[i] = CI.calc_omega0_Kustas(
599600
in_data['LAI'][i],
600601
in_data['f_c'][i],
@@ -637,7 +638,7 @@ def run(self, in_data, mask=None):
637638

638639
if self.water_stress:
639640
i = np.array(np.logical_and(~noVegPixels, mask == 1))
640-
[_, _, _, _, _, _, out_data['LE_0'][i], _,
641+
[_, _, _, _, _, _, out_data['LE_0'][i], _,
641642
out_data['LE_C_0'][i], _, _, _, _, _, _, _, _, _, _] = \
642643
pet.shuttleworth_wallace(
643644
in_data['T_A1'][i],
@@ -670,13 +671,11 @@ def run(self, in_data, mask=None):
670671

671672
out_data['CWSI'][i] = 1.0 - (out_data['LE_C1'][i] / out_data['LE_C_0'][i])
672673

673-
674674
if self.calc_daily_ET:
675-
out_data['ET_day'] = met.flux_2_evaporation(in_data['S_dn_24'] * out_data['LE1'] / in_data['S_dn'],
676-
t_k=20+273.15,
675+
out_data['ET_day'] = met.flux_2_evaporation(in_data['S_dn_24'] * out_data['LE1'] / in_data['S_dn'],
676+
t_k=20+273.15,
677677
time_domain=24)
678-
679-
678+
680679
print("Finished processing!")
681680
return out_data
682681

@@ -801,7 +800,7 @@ def _set_param_array(self, parameter, dims, band=1):
801800

802801
# See if the parameter is a number
803802
try:
804-
array = np.zeros(dims) + float(parameter)
803+
array = np.zeros(dims, np.float32) + float(parameter)
805804
return success, array
806805
except ValueError:
807806
pass
@@ -814,19 +813,19 @@ def _set_param_array(self, parameter, dims, band=1):
814813
return success, array
815814
# If it is then get the value of that parameter
816815
try:
817-
array = np.zeros(dims) + float(inputString)
816+
array = np.zeros(dims, np.float32) + float(inputString)
818817
except ValueError:
819818
try:
820819
fid = gdal.Open(inputString, gdal.GA_ReadOnly)
821820
if self.subset:
822821
array = fid.GetRasterBand(band).ReadAsArray(self.subset[0],
823822
self.subset[1],
824823
self.subset[2],
825-
self.subset[3])
824+
self.subset[3]).astype(np.float32)
826825
else:
827-
array = fid.GetRasterBand(band).ReadAsArray()
826+
array = fid.GetRasterBand(band).ReadAsArray().astype(np.float32)
828827
except AttributeError:
829-
print("%s image not present for parameter %s"%(inputString, parameter))
828+
print("%s image not present for parameter %s" % (inputString, parameter))
830829
success = False
831830
finally:
832831
fid = None
@@ -1065,7 +1064,7 @@ def _get_input_structure(self):
10651064
# Soil heat flux parameter
10661065
("G", "Soil Heat Flux Parameter"),
10671066
('S_dn_24', 'Daily shortwave irradiance')])
1068-
1067+
10691068
return input_fields
10701069

10711070
def _set_special_model_input(self, field, dims):
@@ -1577,9 +1576,9 @@ def _set_special_model_input(self, field, dims):
15771576
inputs[field] = fid.GetRasterBand(1).ReadAsArray(subset[0],
15781577
subset[1],
15791578
subset[2],
1580-
subset[3])
1579+
subset[3]).astype(np.float32)
15811580
else:
1582-
inputs[field] = fid.GetRasterBand(1).ReadAsArray()
1581+
inputs[field] = fid.GetRasterBand(1).ReadAsArray().astype(np.float32)
15831582
inputs['scale'] = [geo_LR, prj_LR, self.geo, self.prj]
15841583
success = True
15851584
else:

pyTSEB/TSEB.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ def TSEB_2T(T_C,
318318
# calcG_params[1] = None
319319
# Create the output variables
320320
[flag, Ln_S, Ln_C, LE_C, H_C, LE_S, H_S, G, R_S, R_x,
321-
R_A, iterations] = [np.zeros(T_S.shape)+np.NaN for i in range(12)]
321+
R_A, iterations] = [np.zeros(T_S.shape, np.float32)+np.NaN for i in range(12)]
322322
T_AC = T_A_K.copy()
323323

324324
# iteration of the Monin-Obukhov length
@@ -675,10 +675,10 @@ def TSEB_PT(Tr_K,
675675
# iteration of the Monin-Obukhov length
676676
if const_L is None:
677677
# Initially assume stable atmospheric conditions and set variables for
678-
L = np.asarray(np.zeros(Tr_K.shape, np.float32) + np.inf, dtype=np.float32)
678+
L = np.zeros(Tr_K.shape) + np.inf
679679
max_iterations = ITERATIONS
680680
else: # We force Monin-Obukhov lenght to the provided array/value
681-
L = np.asarray(np.ones(Tr_K.shape, np.float32) * const_L, dtype=np.float32)
681+
L = np.ones(Tr_K.shape) * const_L
682682
max_iterations = 1 # No iteration
683683
# Calculate the general parameters
684684
rho = met.calc_rho(p, ea, T_A_K) # Air density
@@ -696,7 +696,7 @@ def TSEB_PT(Tr_K,
696696
u_friction = MO.calc_u_star(u, z_u, L, d_0, z_0M)
697697
u_friction = np.asarray(np.maximum(U_FRICTION_MIN, u_friction), dtype=np.float32)
698698
L_queue = deque([np.array(L, np.float32)], 6)
699-
L_converged = np.asarray(np.zeros(Tr_K.shape), dtype=bool)
699+
L_converged = np.zeros(Tr_K.shape, bool)
700700
L_diff_max = np.inf
701701

702702
# First assume that canopy temperature equals the minumum of Air or
@@ -1133,7 +1133,7 @@ def DTD(Tr_K_0,
11331133
resistance_form = resistance_form[0]
11341134
# Create the output variables
11351135
[flag, T_S, T_C, T_AC, Ln_S, Ln_C, LE_C, H_C, LE_S, H_S, G, R_S, R_x,
1136-
R_A, H, iterations] = [np.zeros(Tr_K_1.shape) + np.NaN for i in range(16)]
1136+
R_A, H, iterations] = [np.zeros(Tr_K_1.shape, np.float32) + np.NaN for i in range(16)]
11371137

11381138
# Calculate the general parameters
11391139
rho = met.calc_rho(p, ea, T_A_K_1) # Air density
@@ -1480,7 +1480,7 @@ def OSEB(Tr_K,
14801480
calcG_params[1]],
14811481
[Tr_K] * 12)
14821482
# Create the output variables
1483-
[flag, Ln, LE, H, G, R_A] = [np.zeros(Tr_K.shape) + np.NaN for i in range(6)]
1483+
[flag, Ln, LE, H, G, R_A] = [np.zeros(Tr_K.shape, np.float32) + np.NaN for i in range(6)]
14841484

14851485
# iteration of the Monin-Obukhov length
14861486
if const_L is None:

pyTSEB/energy_combination_ET.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def penman_monteith(T_A_K,
154154
[T_A_K] * 14)
155155

156156
# Create the output variables
157-
[flag, Ln, LE, H, G, R_A, iterations] = [np.zeros(T_A_K.shape) + np.NaN for i in
157+
[flag, Ln, LE, H, G, R_A, iterations] = [np.zeros(T_A_K.shape, np.float32) + np.NaN for i in
158158
range(7)]
159159

160160
# Calculate the general parameters
@@ -175,10 +175,10 @@ def penman_monteith(T_A_K,
175175
# iteration of the Monin-Obukhov length
176176
if const_L is None:
177177
# Initially assume stable atmospheric conditions and set variables for
178-
L = np.asarray(np.zeros(T_A_K.shape) + np.inf)
178+
L = np.zeros(T_A_K.shape) + np.inf
179179
max_iterations = ITERATIONS
180180
else: # We force Monin-Obukhov lenght to the provided array/value
181-
L = np.asarray(np.ones(T_A_K.shape) * const_L)
181+
L = np.ones(T_A_K.shape) * const_L
182182
max_iterations = 1 # No iteration
183183
u_friction = TSEB.MO.calc_u_star(u, z_u, L, d_0, z_0M)
184184
u_friction = np.asarray(np.maximum(TSEB.U_FRICTION_MIN, u_friction))
@@ -484,8 +484,7 @@ def shuttleworth_wallace(T_A_K,
484484

485485
# Create the output variables
486486
[flag, vpd_0, LE, H, LE_C, H_C, LE_S, H_S, G, R_S, R_x, R_A,
487-
Rn, Rn_C, Rn_S, C_s, C_c, PM_C, PM_S, iterations] = [np.full(T_A_K.shape,
488-
np.NaN)
487+
Rn, Rn_C, Rn_S, C_s, C_c, PM_C, PM_S, iterations] = [np.full(T_A_K.shape, np.NaN, np.float32)
489488
for i in range(20)]
490489

491490
# Calculate the general parameters
@@ -511,10 +510,10 @@ def shuttleworth_wallace(T_A_K,
511510
# iteration of the Monin-Obukhov length
512511
if const_L is None:
513512
# Initially assume stable atmospheric conditions and set variables for
514-
L = np.asarray(np.zeros(T_A_K.shape) + np.inf)
513+
L = np.zeros(T_A_K.shape) + np.inf
515514
max_iterations = ITERATIONS
516515
else: # We force Monin-Obukhov lenght to the provided array/value
517-
L = np.asarray(np.ones(T_A_K.shape) * const_L)
516+
L = np.ones(T_A_K.shape) * const_L
518517
max_iterations = 1 # No iteration
519518
u_friction = TSEB.MO.calc_u_star(u, z_u, L, d_0, z_0M)
520519
u_friction = np.asarray(np.maximum(TSEB.U_FRICTION_MIN, u_friction))
@@ -871,7 +870,7 @@ def penman(T_A_K,
871870
[T_A_K] * 11)
872871

873872
# Create the output variables
874-
[flag, Ln, LE, H, G, R_A, iterations] = [np.zeros(T_A_K.shape) + np.NaN for i in
873+
[flag, Ln, LE, H, G, R_A, iterations] = [np.zeros(T_A_K.shape, np.float32) + np.NaN for i in
875874
range(7)]
876875

877876
# Calculate the general parameters
@@ -889,10 +888,10 @@ def penman(T_A_K,
889888
# iteration of the Monin-Obukhov length
890889
if const_L is None:
891890
# Initially assume stable atmospheric conditions and set variables for
892-
L = np.asarray(np.zeros(T_A_K.shape) + np.inf)
891+
L = np.zeros(T_A_K.shape) + np.inf
893892
max_iterations = ITERATIONS
894893
else: # We force Monin-Obukhov lenght to the provided array/value
895-
L = np.asarray(np.ones(T_A_K.shape) * const_L)
894+
L = np.ones(T_A_K.shape) * const_L
896895
max_iterations = 1 # No iteration
897896
u_friction = TSEB.MO.calc_u_star(u, z_u, L, d_0, z_0M)
898897
u_friction = np.asarray(np.maximum(TSEB.U_FRICTION_MIN, u_friction))

0 commit comments

Comments
 (0)