-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
230 lines (207 loc) · 9.45 KB
/
tools.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""Created on Thu May 17 04:26:42 2018.
@author: Mostafa
"""
import numpy as np
# from statista.tools import Tools as tl
class Tools:
"""Tools.
Tools different statistical and interpolation tools
"""
def __init__(self):
pass
@staticmethod
def IDW(raster, coordinates, data, No_data_cells=False):
"""IDW.
this function generates distributred values from reading at stations
using inverse distance weighting method
Parameters
raster:
GDAL calss represent the GIS raster
coordinates:
dict {'x':[],'y':[]} with two columns contains x coordinates and y
coordinates of the stations
data:
numpy array contains values of the timeseries at each gauge in the
same order as the coordinates in the coordinates lists (x,y)
No_data_cells:
boolen value (True or False) if the user want to calculate the
values in the cells that has no data value (cropped) No_data_cells
equal True if not No_data_cells equals False (default is false )
Returns
-------
array:
array with the same dimension of the raster
"""
# get the shaoe of the raster
shape_base_dem = raster.ReadAsArray().shape
# get the coordinates of the top left corner and cell size [x,dx,y,dy]
geo_trans = raster.GetGeoTransform()
# get the no_value
no_val = np.float32(
raster.GetRasterBand(1).GetNoDataValue()
) # get the value stores in novalue cells
# read the raster
raster_array = raster.ReadAsArray()
# calculate the coordinates of the center of each cell
# X_coordinate= upperleft corner x+ index* cell size+celsize/2
coox = np.ones(shape_base_dem)
cooy = np.ones(shape_base_dem)
# calculate the coordinates of the cells
if (
not No_data_cells
): # if user decide not to calculate values in the o data cells
for i in range(shape_base_dem[0]): # iteration by row
for j in range(shape_base_dem[1]): # iteration by column
if raster_array[i, j] != no_val:
coox[i, j] = (
geo_trans[0] + geo_trans[1] / 2 + j * geo_trans[1]
) # calculate x
cooy[i, j] = (
geo_trans[3] + geo_trans[5] / 2 + i * geo_trans[5]
) # calculate y
else:
for i in range(shape_base_dem[0]): # iteration by row
for j in range(shape_base_dem[1]): # iteration by column
coox[i, j] = (
geo_trans[0] + geo_trans[1] / 2 + j * geo_trans[1]
) # calculate x
cooy[i, j] = (
geo_trans[3] + geo_trans[5] / 2 + i * geo_trans[5]
) # calculate y
# inverse the distance from the cell to each station
inverseDist = np.ones(
(shape_base_dem[0], shape_base_dem[1], len(coordinates["x"]))
)
# denominator of the equation (sum(1/d))
denominator = np.ones((shape_base_dem[0], shape_base_dem[1]))
for i in range(shape_base_dem[0]): # iteration by row
for j in range(shape_base_dem[1]): # iteration by column
if not np.isnan(coox[i, j]): # calculate only if the coox is not nan
for st in range(
len(coordinates["x"])
): # iteration by station [0]: #
# distance= sqrt((xstation-xcell)**2+ystation-ycell)**2)
inverseDist[i, j, st] = 1 / (
np.sqrt(
np.power((coordinates["x"][st] - coox[i, j]), 2)
+ np.power((coordinates["y"][st] - cooy[i, j]), 2)
)
)
denominator = np.sum(inverseDist, axis=2)
sp_dist = (
np.ones((len(raster[:, 0]), shape_base_dem[0], shape_base_dem[1])) * np.nan
)
for t in range(len(raster[:, 0])): # iteration by time step
for i in range(shape_base_dem[0]): # iteration by row
for j in range(shape_base_dem[1]): # iteration by column
if not np.isnan(
coox[i, j]
): # calculate only if the coox is not nan
sp_dist[i, j, t] = (
np.sum(inverseDist[i, j, :] * raster[t, :])
/ denominator[i, j]
)
# change the type to float 32
sp_dist = sp_dist.astype(np.float32)
return sp_dist
@staticmethod
def ISDW(raster, coordinates, data, No_data_cells=False):
"""ISDW.
this function generates distributred values from reading at stations using
inverse squared distance weighting method
Parameters
----------
raster: [dataset]
GDAL calss represent the GIS raster
coordinates: [dict]
dict {'x':[],'y':[]} with two columns contains x coordinates and y
coordinates of the stations
data: [array]
numpy array contains values of the timeseries at each gauge in the
same order as the coordinates in the coordinates lists (x,y)
No_data_cells: []
boolen value (True or False) if the user want to calculate the
values in the cells that has no data value (cropped) No_data_cells
equal True if not No_data_cells equals False
(default is false )
Returns
-------
sp_dist: [array]
array with the same dimension of the raster
"""
shape_base_dem = raster.ReadAsArray().shape
# get the coordinates of the top left corner and cell size [x,dx,y,dy]
geo_trans = raster.GetGeoTransform()
# get the no_value
no_val = np.float32(
raster.GetRasterBand(1).GetNoDataValue()
) # get the value stores in novalue cells
# read the raster
raster_array = raster.ReadAsArray()
# calculate the coordinates of the center of each cell
# X_coordinate= upperleft corner x+ index* cell size+celsize/2
coox = np.ones(shape_base_dem) * np.nan
cooy = np.ones(shape_base_dem) * np.nan
# calculate the coordinates of the cells
if (
not No_data_cells
): # if user decide not to calculate values in the o data cells
for i in range(shape_base_dem[0]): # iteration by row
for j in range(shape_base_dem[1]): # iteration by column
if raster_array[i, j] != no_val:
coox[i, j] = (
geo_trans[0] + geo_trans[1] / 2 + j * geo_trans[1]
) # calculate x
cooy[i, j] = (
geo_trans[3] + geo_trans[5] / 2 + i * geo_trans[5]
) # calculate y
else:
for i in range(shape_base_dem[0]): # iteration by row
for j in range(shape_base_dem[1]): # iteration by column
coox[i, j] = (
geo_trans[0] + geo_trans[1] / 2 + j * geo_trans[1]
) # calculate x
cooy[i, j] = (
geo_trans[3] + geo_trans[5] / 2 + i * geo_trans[5]
) # calculate y
print("step 1 finished")
# inverse the distance from the cell to each station
inverseDist = np.ones(
(shape_base_dem[0], shape_base_dem[1], len(coordinates["x"]))
)
# denominator of the equation (sum(1/d))
denominator = np.ones((shape_base_dem[0], shape_base_dem[1]))
for i in range(shape_base_dem[0]): # iteration by row
for j in range(shape_base_dem[1]): # iteration by column
if not np.isnan(coox[i, j]): # calculate only if the coox is not nan
for st in range(
len(coordinates["x"])
): # iteration by station [0]: #
inverseDist[i, j, st] = (
1
/ (
np.sqrt(
np.power((coordinates["x"][st] - coox[i, j]), 2)
+ np.power((coordinates["y"][st] - cooy[i, j]), 2)
)
)
** 2
)
print("step 2 finished")
denominator = np.sum(inverseDist, axis=2)
sp_dist = (
np.ones((shape_base_dem[0], shape_base_dem[1], len(data[:, 0]))) * np.nan
)
for t in range(len(data[:, 0])): # iteration by time step
for i in range(shape_base_dem[0]): # iteration by row
for j in range(shape_base_dem[1]): # iteration by column
if not np.isnan(
coox[i, j]
): # calculate only if the coox is not nan
sp_dist[i, j, t] = (
np.sum(inverseDist[i, j, :] * data[t, :])
/ denominator[i, j]
)
# change the type to float 32
sp_dist = sp_dist.astype(np.float32)
return sp_dist