-
Notifications
You must be signed in to change notification settings - Fork 1
/
step_1_snap_locations.py
313 lines (257 loc) · 13 KB
/
step_1_snap_locations.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = "Hylke E. Beck"
__email__ = "[email protected]"
__date__ = "July 2021"
import os, sys, glob, time, h5py, scipy.io, pdb
import pandas as pd
import numpy as np
import pcraster as pcr
from netCDF4 import Dataset
import matplotlib.pyplot as plt
import geopandas as gpd
def latlon2rowcol(lat,lon,res,lat_upper,lon_left):
row = np.round((lat_upper-lat)/res-0.5).astype(int)
col = np.round((lon-lon_left)/res-0.5).astype(int)
return row.squeeze(),col.squeeze()
def rowcol2latlon(row,col,res,lat_upper,lon_left):
lat = lat_upper-row*res-res/2
lon = lon_left+col*res+res/2
return lat.squeeze(),lon.squeeze()
def readmatfile(filepath,var):
try:
f = h5py.File(filepath,'r')
data = f.get(var)[()]
data = data.transpose()
f.close()
except:
try:
mat = scipy.io.loadmat(filepath)
data = eval("mat['"+var.replace("/","'][0,0]['")+"'][:]")
except:
pass
return data
def onclick(event):
global ix, iy
ix, iy = event.xdata, event.ydata
global coords
coords = np.round([ix, iy]).astype(int)
print("Selected location "+str(coords))
plt.close()
# Load configuration file
config = pd.read_csv('config.cfg',header=None,index_col=False)
for ii in np.arange(len(config)):
string = config.iloc[ii,0]
string = string.replace(" ","")
string = string.replace("=","=r")
exec(string)
# Load LDD map
dset = Dataset(ldd_path)
ldd_np = np.array(dset.variables['ldd'][:])
ldd_np[np.isnan(ldd_np)] = -9999
ldd_np = ldd_np.astype(np.int16)
lat = np.array(dset.variables['lat'][:])
lon = np.array(dset.variables['lon'][:])
res = np.diff(lon)[0]
lat_upper = lat[0]+res/2
lon_left = lon[0]-res/2
# Load upstream map
dset = Dataset(ups_path)
upstreamarea_np = np.array(dset.variables['ups'][:])
# Set pcraster clone map
pcr.setclone(ldd_np.shape[0],ldd_np.shape[1],res,lon[0]-res/2,lat[0]-res/2)
# Convert LDD to pcraster format
ldd_pcr = pcr.numpy2pcr(pcr.Ldd,ldd_np,mv=-9999)
# Large rivers shapefile
# http://ihp-wins.unesco.org/layers/geonode:world_rivers
rivers_shp = gpd.read_file(r'e:\Temp\world_rivers\world_rivers.shp')
# Create folder with corrected station locations
if os.path.isdir(corrected_locations_dir)==False:
os.mkdir(corrected_locations_dir)
############################################################################
# Loop over catchments to automatically snap station locations to the
# 'correct' grid cell. If snapping doesn't work, a window is opened to
# manually select the correct location
############################################################################
catchment_dirs = glob.glob(os.path.join(database_dir,'Sudan_*',""))
catchment_dirs = sorted(catchment_dirs)
failcounter = 0
toosmallcounter = 0
successcounter = 0
for ii in np.arange(len(catchment_dirs)):
catchment_dir = catchment_dirs[ii]
ID = os.path.split(os.path.dirname(catchment_dir))[-1]
print("===============================================================================")
print(str(ii)+" "+ID)
t1 = time.time()
if os.path.isfile(os.path.join(corrected_locations_dir,ID+'.txt')):
print('Corrected station location already found, skipping')
continue
if 'Discharge' in globals(): del Discharge
if 'StatLat' in globals(): del StatLat
if 'StatLon' in globals(): del StatLon
if 'Area' in globals(): del Area
if 'CatchCentroidLat' in globals(): del CatchCentroidLat
if 'CatchCentroidLon' in globals(): del CatchCentroidLon
if 'coords' in globals(): del coords
print('Bla')
############################################################################
# Load discharge and catchment boundary data
############################################################################
# Load discharge data
try:
Discharge = readmatfile(os.path.join(catchment_dir,"DISCHARGE.mat"),'DISCHARGE/Discharge')
StatLat = readmatfile(os.path.join(catchment_dir,"DISCHARGE.mat"),'DISCHARGE/StationCoords/Lat')[0][0]
StatLon = readmatfile(os.path.join(catchment_dir,"DISCHARGE.mat"),'DISCHARGE/StationCoords/Lon')[0][0]
StatRow,StatCol = latlon2rowcol(StatLat,StatLon,res,lat_upper,lon_left)
Station = readmatfile(os.path.join(catchment_dir,"DISCHARGE.mat"),'DISCHARGE/Station')[0]
Station = ''.join(map(chr,Station))
except:
print("Unable to load DISCHARGE.mat, skipping")
continue
# Load catchment boundaries
try:
Area = float(readmatfile(os.path.join(catchment_dir,"BOUNDARIES.mat"),'BOUNDARIES/Area'))
CatchBoundsLat = readmatfile(os.path.join(catchment_dir,"BOUNDARIES.mat"),'BOUNDARIES/CatchBounds/Lat')[0]
CatchBoundsLon = readmatfile(os.path.join(catchment_dir,"BOUNDARIES.mat"),'BOUNDARIES/CatchBounds/Lon')[0]
CatchCentroidLat = (np.nanmin(CatchBoundsLat)+np.nanmax(CatchBoundsLat))/2
CatchCentroidLon = (np.nanmin(CatchBoundsLon)+np.nanmax(CatchBoundsLon))/2
except:
print("Unable to load BOUNDARIES.mat")
snap_success = False
############################################################################
# If we have area and catchment boundary estimates, we will automatically
# determine the most suitable grid cell
############################################################################
if ('Area' in globals()) & ('CatchCentroidLat' in globals()) & ('CatchCentroidLat' in globals()):
if Area<10000:
print("Catchment too small")
toosmallcounter = toosmallcounter+1
continue
# Snap settings
permissible_area_error = 20 # Maximum allowed catchment area error (percent)
permissible_centroid_error = 0.5 # Maximum allowed centroid location error (degrees)
margin = (np.ceil(np.sqrt(Area*0.1)/2)/(40000*0.08333/360)).astype(int) # Search box size (grid cells around station)
if margin<2: margin = 2
if margin>4: margin = 4
print('Station snap margin set to '+str(margin))
# Compute area error
area_error_map = 100*np.abs(upstreamarea_np-Area)/Area
mask = np.zeros(ldd_np.shape,dtype=bool)
mask[StatRow-margin:StatRow+margin+1,StatCol-margin:StatCol+margin+1] = True
area_error_map[~mask] = 9999
# Compute centroid error
centroid_error_map = np.zeros(ldd_np.shape,dtype=np.single)+9999
indices = zip(*np.where(mask))
for i, j in indices:
point_np = np.zeros(ldd_np.shape,dtype=bool)
point_np[i,j] = True
point_pcr = pcr.numpy2pcr(pcr.Boolean,point_np,mv=-9999)
catch_pcr = pcr.catchment(ldd_pcr, point_pcr)
catch_np = pcr.pcr2numpy(catch_pcr,mv=-9999)
catch_np = catch_np==1
if np.sum(catch_np)==0: continue
ind = np.where(catch_np)
centroid_row = (ind[0][0]+ind[0][-1])/2
centroid_col = (ind[1][0]+ind[1][-1])/2
centroid_lat,centroid_lon = rowcol2latlon(centroid_row,centroid_col,res,lat_upper,lon_left)
centroid_error_map[i,j] = np.sqrt((centroid_lat-CatchCentroidLat)**2+(centroid_lon-CatchCentroidLon)**2)
# Determine the most appropriate station grid cell
area_error = area_error_map[StatRow,StatCol]
centroid_error = centroid_error_map[StatRow,StatCol]
if (area_error<permissible_area_error) & (centroid_error<permissible_centroid_error):
StatRowCorr,StatColCorr = StatRow,StatCol
StatLatCorr,StatLonCorr = rowcol2latlon(StatRowCorr,StatColCorr,res,lat_upper,lon_left)
print('Automatic snapping not necessary')
else:
rank_area_error_map = area_error_map.ravel().argsort().argsort().reshape(area_error_map.shape)
rank_centroid_error_map = centroid_error_map.ravel().argsort().argsort().reshape(centroid_error_map.shape)
rank_cum_map = rank_area_error_map+rank_centroid_error_map
StatRowCorr,StatColCorr = np.where(rank_cum_map==np.min(rank_cum_map))
StatRowCorr,StatColCorr = StatRowCorr[0],StatColCorr[0]
StatLatCorr,StatLonCorr = rowcol2latlon(StatRowCorr,StatColCorr,res,lat_upper,lon_left)
print('Station automatically snapped')
area_error = area_error_map[StatRowCorr,StatColCorr]
centroid_error = centroid_error_map[StatRowCorr,StatColCorr]
print("Area error: "+str(area_error)+" %, centroid error: "+str(centroid_error)+" degrees")
if (area_error<permissible_area_error):
snap_success = True
print('Automatic snap successful')
else:
print('Automatic snap failed')
############################################################################
# If we couldn't automatically snap, or if the snapping was unsuccessful,
# we have to manually select the correct grid cell
############################################################################
plt.close('all')
if snap_success==False:
failcounter = failcounter+1
if 'Area' in globals(): print('Provider catchment area is '+str(np.round(Area))+' km2 (10^'+str(np.log10(Area))+' km2)')
extent = [lon_left,lon_left+len(lon)*res,lat_upper-len(lat)*res,lat_upper] # (left, right, bottom, top)
fig, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(2, 3)
fig.suptitle(str(ii)+' '+ID+' '+Station+'\nMean discharge '+str(np.nanmean(Discharge))+' m3/s')
ax1.imshow(np.log10(upstreamarea_np), extent=extent)
ax1.scatter(StatLon,StatLat,c='r')
ax1.set_xlim(StatLon-6,StatLon+6)
ax1.set_ylim(StatLat-6,StatLat+6)
rivers_shp.plot(ax=ax1,column=None,cmap=None,facecolor="none",edgecolor='r',linewidth=0.35)
ax2.imshow(np.log10(upstreamarea_np), extent=extent)
ax2.scatter(StatLon,StatLat,c='r')
ax2.set_xlim(StatLon-2,StatLon+2)
ax2.set_ylim(StatLat-2,StatLat+2)
rivers_shp.plot(ax=ax2,column=None,cmap=None,facecolor="none",edgecolor='r',linewidth=0.35)
ax3.imshow(np.log10(upstreamarea_np), extent=extent)
ax3.scatter(StatLon,StatLat,c='r')
ax3.set_xlim(StatLon-0.5,StatLon+0.5)
ax3.set_ylim(StatLat-0.5,StatLat+0.5)
rivers_shp.plot(ax=ax3,column=None,cmap=None,facecolor="none",edgecolor='r',linewidth=0.35)
margin = 6
im4 = ax4.imshow(np.log10(upstreamarea_np))
ax4.scatter(StatCol,StatRow,c='r')
ax4.set_xlim(StatCol-margin,StatCol+margin)
ax4.set_ylim(StatRow-margin,StatRow+margin)
ax4.invert_yaxis()
ax4.set_title('Click here...')
fig.colorbar(im4,ax=ax4)
margin = 36
im5 = ax5.imshow(np.log10(upstreamarea_np))
ax5.scatter(StatCol,StatRow,c='r')
ax5.set_xlim(StatCol-margin,StatCol+margin)
ax5.set_ylim(StatRow-margin,StatRow+margin)
ax5.invert_yaxis()
ax5.set_title('... or here')
fig.colorbar(im5,ax=ax5)
# Select correct grid cell manually
print('Click on correct grid cell, then close plot')
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
if 'coords' not in globals():
print('No location selected, skipping')
continue
StatColCorr,StatRowCorr = coords[0],coords[1]
StatLatCorr,StatLonCorr = rowcol2latlon(StatRowCorr,StatColCorr,res,lat_upper,lon_left)
############################################################################
# Show catchment of corrected location
############################################################################
point_np = np.zeros(ldd_np.shape,dtype=bool)
point_np[StatRowCorr,StatColCorr] = True
point_pcr = pcr.numpy2pcr(pcr.Boolean,point_np,mv=-9999)
catch_pcr = pcr.catchment(ldd_pcr, point_pcr)
catch_np = pcr.pcr2numpy(catch_pcr,mv=-9999)
catch_np = catch_np==1
extent = [lon_left,lon_left+len(lon)*res,lat_upper-len(lat)*res,lat_upper] # (left, right, bottom, top)
plt.imshow(np.single(catch_np)+np.log10(upstreamarea_np)/5, extent=extent,vmin=0,vmax=3)
#plt.plot(CatchBoundsLon,CatchBoundsLat)
plt.scatter(StatLon,StatLat,c='r')
plt.gca().set_xlim(StatLon-10,StatLon+10)
plt.gca().set_ylim(StatLat-6,StatLat+6)
plt.title(str(ii)+' '+ID)
plt.show(block=False)
plt.pause(0.01)
############################################################################
# Save corrected location
############################################################################
np.savetxt(os.path.join(corrected_locations_dir,ID+'.txt'), np.array([StatColCorr,StatRowCorr]), delimiter=',',fmt='%1.3f')
successcounter = successcounter+1
print('Time elapsed is ' + str(time.time() - t1) + ' sec')
pdb.set_trace()