Skip to content

Commit f326711

Browse files
improved definition coordinate grids
1 parent 55248a1 commit f326711

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

Diff for: src/lisfloodutilities/readers/nc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def mv(self):
1919
for variable in self.ds.variables.values():
2020
if len(variable.dims) < 2:
2121
continue
22-
return variable.attrs['_FillValue'] if '_FillValue' in variable.attrs else variable.attrs['missing_value']
22+
return variable.attrs['_FillValue'] if '_FillValue' in variable.attrs else -9999
2323

2424
@property
2525
def data(self):

Diff for: src/lisfloodutilities/waterregions/define_waterregions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def define_waterregions(calib_points=None, countries_id=None, ldd=None, waterreg
129129
os.remove(ldd_pcr)
130130
except:
131131
pass
132-
convnc2pcr(ldd,ldd_pcr,is_ldd=True)
132+
convnc2pcr(ldd,ldd_pcr,is_ldd=True)
133133
else:
134134
ldd_pcr=ldd
135135
if countries_id[-3:]=='.nc':

Diff for: src/lisfloodutilities/waterregions/verify_waterregions.py

+29-4
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,44 @@ def verify_waterregions(calib_catchments=None, waterregions=None):
5252
waterregions_map = Dataset(waterregions,'r','format=NETCDF4_classic')
5353

5454
for v in waterregions_map.variables:
55+
if (v == 'y' or v == 'lat'):
56+
ywr = waterregions_map.variables[v][:]
57+
wr_yresolution = np.abs((ywr[-1] - ywr[0])/(len(ywr[:])-1.0))
58+
if (v == 'x' or v == 'lon'):
59+
xwr = waterregions_map.variables[v][:]
60+
wr_xresolution = np.abs((xwr[-1] - xwr[0])/(len(xwr[:])-1.0))
5561
if len(waterregions_map.variables[v].dimensions) == 2:
56-
wr = waterregions_map.variables[v][:]
62+
wr = waterregions_map.variables[v][:]
5763

64+
flipud = 0
5865
calibration_catchments_map = Dataset(calib_catchments,'r','format=NETCDF4_classic')
5966
for v in calibration_catchments_map.variables:
67+
if (v == 'y' or v == 'lat'):
68+
ycc = calibration_catchments_map.variables[v][:]
69+
cc_resolution =np.abs((ycc[-1] - ywr[0])/(len(ycc[:])-1.0))
70+
if round(ycc[0],1) != round(ywr[0],1):
71+
flipud = 1
72+
check_grid_y = np.amax(np.abs(np.flip(ycc)-ywr))
73+
if (check_grid_y > wr_yresolution):
74+
print('Error: the two maps do not have the same coordinates. Please, check the input maps.')
75+
exit()
76+
if (v == 'x' or v == 'lon'):
77+
xcc = calibration_catchments_map.variables[v][:]
78+
check_grid_x = np.amax(np.abs(xcc-xwr))
79+
if (check_grid_x > wr_xresolution):
80+
print('Error: the two maps do not have the same coordinates. Please, check the input maps.')
81+
exit()
6082
if len(calibration_catchments_map.variables[v].dimensions) == 2:
6183
cal_catch = calibration_catchments_map.variables[v][:]
84+
if flipud == 1:
85+
print('Warning: one of the maps has inverted y-axis, is this an intended feature?')
86+
cal_catch = np.flipud(cal_catch)
87+
6288

63-
64-
cal_catch[cal_catch<1] = -9999
89+
cal_catch[cal_catch < 1] = -9999
6590
cal_catch[np.isnan(cal_catch) == 1] = -9999
6691
wr_id = np.unique(wr)
67-
92+
6893
id_error_wr = []
6994
cal_catch_error_wr = []
7095
output_message = []

Diff for: src/lisfloodutilities/writers/pcr.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,17 @@ def _get_geo_transform_from_coords(self):
139139
# 4 - 0.0
140140
# 5 - n-s pixel resolution (negative value)
141141
#
142-
w_e_resolution = np.round(self._coordinates['x'][1] - self._coordinates['x'][0], 3)
143-
n_s_resolution = np.round(self._coordinates['y'][1] - self._coordinates['y'][0], 3)
142+
n_s_resolution =(self._coordinates['y'][-1] - self._coordinates['y'][0])/(len(self._coordinates['y'][:])-1.0)
143+
w_e_resolution =(self._coordinates['x'][-1] - self._coordinates['x'][0])/(len(self._coordinates['x'][:])-1.0)
144144
# considering coordinates at center of the cell, we must subtract half step for top left points
145145
if (self._coordinates['y'][0]<self._coordinates['y'][-1]):
146146
top=self._coordinates['y'][-1]
147147
ns=-1
148148
else:
149149
top=self._coordinates['y'][0]
150150
ns=1
151-
top_left_x = np.round(self._coordinates['x'][0], 2) - w_e_resolution / 2
152-
top_left_y = np.round(top, 2) - ns*n_s_resolution / 2
153-
return top_left_x, w_e_resolution, 0.0, top_left_y, 0.0, n_s_resolution
151+
top_left_x = self._coordinates['x'][0] - w_e_resolution / 2
152+
top_left_y = top - ns*n_s_resolution / 2
153+
return top_left_x, w_e_resolution, 0.0, top_left_y, 0.0, n_s_resolution
154+
155+

0 commit comments

Comments
 (0)