-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcalculate_ET_Maps.py
209 lines (156 loc) · 7.52 KB
/
calculate_ET_Maps.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# -*- coding: utf-8 -*-
#!/usr/bin/python
"""
Created on Mon Sep 10 14:37:54 2018
@author: gag
Function that opens a set of satellite images, performs the corresponding transformations
to unify resolutions, projections and sizes, to then apply a specific methodology.
Then, get the maps of the physical variables and contrast them with other sources.
"""
import numpy.ma as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.axes_grid1 import make_axes_locatable
from skimage.measure import compare_ssim as ssim
from sklearn.metrics import mean_squared_error
import functions
def calculateETMaps():
dir = "..."
path = "/.../"+dir+"/.../"
fechas= []
fechas.append("2016_05_15")
fechas.append("2016_05_07")
fechas.append("2016_06_16")
fechas.append("2016_07_10")
pathOut = "/.../"+dir+"/.../CreatedMaps/"
Ta = []
HR = []
PP = []
sigma0 = []
for i in range(0,len(fechas)):
#for i in range(0,1):
print(fechas[i])
### ET modis
fileETModis = "/media/"+dir+"/TOURO Mobile/ET/"+fechas[i]+"/MYD16A2/MYD16A2_reprojected.data/ET_500m.img"
src_ds_ETModis, bandETModis, GeoTETModis, ProjectETModis = functions.openFileHDF(fileETModis, 1)
### SMAP resolucion 36km
fileSM = "/media/"+dir+"/TOURO Mobile/ET/SM_36km/"+fechas[i]+"/SM.dat"
src_ds_SM, bandSM, GeoTSM, ProjectSM = functions.openFileHDF(fileSM, 1)
print("tamanio SM SMAP:" + str(bandSM.shape))
### observed variables
fileRn = "/media/"+dir+"/TOURO Mobile/ET/"+fechas[i]+"/RN/mapa_RN.asc"
src_ds_Rn, bandRn, GeoTRn, ProjectRn = functions.openFileHDF(fileRn, 1)
fileG = "/media/"+dir+"/TOURO Mobile/ET/"+fechas[i]+"/G/mapa_G.asc"
src_ds_G, bandG, GeoTG, ProjectG = functions.openFileHDF(fileG, 1)
fileDelta = "/media/"+dir+"/TOURO Mobile/ET/"+fechas[i]+"/Delta/mapa_delta.asc"
src_ds_Delta, bandDelta, GeoTDelta, ProjectDelta = functions.openFileHDF(fileDelta, 1)
#### real ET observed
fileETObs = "/media/"+dir+"/TOURO Mobile/ET/"+fechas[i]+"/ETobs/mapa_ETobs.asc"
src_ds_ETObs, bandETObs, GeoTETObs, ProjectETObs = functions.openFileHDF(fileDelta, 1)
nameFileET = "mapa_ET_"+str(fechas[i])
### se cambian las resoluciones de todas las imagenes a la de la sar
type = "Nearest"
# type = "Bilinear"
nRow, nCol = bandSM.shape
# fig, ax = plt.subplots()
# ax.imshow(bandSM, interpolation='None',cmap=cm.gray)
data_src = src_ds_ETObs
data_match = src_ds_SM
match = functions.matchData(data_src, data_match, type, nRow, nCol)
band_matchETObs = match.ReadAsArray()
# fig, ax = plt.subplots()
# ax.imshow(bandETObs*1000, interpolation='None',cmap=cm.gray)
data_src = src_ds_ETModis
data_match = src_ds_SM
match = functions.matchData(data_src, data_match, type, nRow, nCol)
band_matchET = match.ReadAsArray()
# fig, ax = plt.subplots()
# ax.imshow(band_matchET, interpolation='None',cmap=cm.gray)
data_src = src_ds_Rn
data_match = src_ds_SM
match = functions.matchData(data_src, data_match, type, nRow, nCol)
band_matchRn = match.ReadAsArray()
# fig, ax = plt.subplots()
# ax.imshow(band_matchRn, interpolation='None',cmap=cm.gray)
data_src = src_ds_G
data_match = src_ds_SM
match = functions.matchData(data_src, data_match, type, nRow, nCol)
band_matchG = match.ReadAsArray()
# fig, ax = plt.subplots()
# ax.imshow(band_matchG, interpolation='None',cmap=cm.gray)
data_src = src_ds_Delta
data_match = src_ds_SM
match = functions.matchData(data_src, data_match, type, nRow, nCol)
band_matchDelta = match.ReadAsArray()
# fig, ax = plt.subplots()
# ax.imshow(band_matchDelta, interpolation='None',cmap=cm.gray)
################################################################################
######## here goes the equation to calculate the ET
################################################################################
# fig, ax = plt.subplots()
# ax.imshow(mapET, interpolation='None',cmap=cm.gray)
# plt.show()
##my_cmap = cm.Blues
##my_cmap.set_under('k', alpha=0)
##my_cmap1 = cm.Greens
##my_cmap1.set_under('k', alpha=0)
##my_cmap2 = cm.OrRd
##my_cmap2.set_under('k', alpha=0)
#my_cmap3 = cm.Oranges
my_cmap3 = cm.terrain
my_cmap3.set_under('k', alpha=0)
transform = GeoTSM
xmin,xmax,ymin,ymax=transform[0],transform[0]+transform[1]*src_ds_SM.RasterXSize,transform[3]+transform[5]*src_ds_SM.RasterYSize,transform[3]
#print xmin
#print xmax
path = "/.../ET_modelado_36km/"
nameFileET = "ET_modelado_"+str(fechas[i])
### mapas ET modelados
fig, ax = plt.subplots()
mapET = (mapET-np.min(mapET)) /(np.max(mapET)-np.min(mapET))
### guarda mapa
functions.createHDFfile(path, nameFileET, 'ENVI', mapET, nCol, nRow, GeoTSM, ProjectSM)
im1 = ax.imshow(mapET, interpolation='none', cmap=plt.get_cmap('gray'), extent=[xmin,xmax,ymin,ymax], clim=(0, 1))
ax.xaxis.tick_top()
divider = make_axes_locatable(ax)
cax = divider.append_axes('bottom', size="5%", pad=0.05)
cb = plt.colorbar(im1, cax=cax, orientation="horizontal")
cb.set_label('Evapotranspiration (W/m^2)')
print ("ET modelado:")
print ("Max:" + str(np.max(mapET)))
print ("Min:" + str(np.min(mapET)))
print ("Std:" + str(np.std(mapET)))
### mapas ET observado interpolado
fig, ax = plt.subplots()
band_matchETObs = band_matchETObs*1000
band_matchETObs = (band_matchETObs- np.min(band_matchETObs)) /(np.max(band_matchETObs)-np.min(band_matchETObs))
im0 = ax.imshow(band_matchETObs, cmap=plt.get_cmap('gray'), extent=[xmin,xmax,ymin,ymax], interpolation='none', clim=(0, 1))
ax.xaxis.tick_top()
divider = make_axes_locatable(ax)
cax = divider.append_axes('bottom', size="5%", pad=0.05)
cb = plt.colorbar(im0, cax=cax, orientation="horizontal")
cb.set_label('Evapotranspiration (W/m^2)')
#cb.set_clim(vmin=5, vmax=50)
print ("ET observado:")
print ("Max:" + str(np.max(band_matchETObs)))
print ("Min:" + str(np.min(band_matchETObs)))
print ("Std:" + str(np.std(band_matchETObs)))
print("Error entre ET modelado y observado")
mse_noise= mean_squared_error(y_true = band_matchETObs , y_pred = mapET)
mse_noise = np.sqrt(mse_noise)
#mse_noise= compare_mse(bandET_modis, bandET_modeled)
ssim_noise = ssim(band_matchETObs.flatten(), mapET.flatten())
print("SSIM:" +str(ssim_noise))
print("RMSE:" + str(mse_noise))
fig, ax = plt.subplots()
errorModelado = np.sqrt((mapET - band_matchETObs)**2)
im0 = ax.imshow(errorModelado, cmap=plt.cm.jet, extent=[xmin,xmax,ymin,ymax], interpolation='none', clim=(0, 1))
ax.xaxis.tick_top()
divider = make_axes_locatable(ax)
cax = divider.append_axes('bottom', size="5%", pad=0.05)
cb = plt.colorbar(im0, cax=cax, orientation="horizontal")
cb.set_label('Error')
#cb.set_clim(vmin=5, vmax=50)
plt.show()
if __name__ == '__main__':
calculateETMaps()