Skip to content

Commit 86547aa

Browse files
author
Nikolaos Mastrantonas
committed
seperate 0 and nans on mct mask
1 parent c6d37b3 commit 86547aa

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/lisfloodutilities/mctrivers/mctrivers.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def mct_mask(channels_slope_file, ldd_file, uparea_file, mask_file='',
9696
# proprocess CH dataset for having correct format
9797
CH = xr.open_dataset(channels_slope_file)
9898
old_name = [i for i in list(CH.data_vars) if sorted(CH[i].dims)==sorted([x_proj, y_proj])]
99-
CH = CH.rename({old_name[0]: "changrad"}) # only 1 variable complies with above check
99+
CH = CH.rename({old_name[0]: "changrad"}) # only 1 variable complies with the above check
100100
CH['changrad'] = CH['changrad'].transpose(y_proj, x_proj) # make sure dims order is as pcraster needs
101101

102102
# ---------------- Set clone map for pcraster
@@ -147,7 +147,7 @@ def mct_mask(channels_slope_file, ldd_file, uparea_file, mask_file='',
147147
# ---------------- Read upstream area
148148
UA = xr.open_dataset(uparea_file)
149149
old_name = [i for i in list(UA.data_vars) if sorted(UA[i].dims)==sorted([x_proj, y_proj])]
150-
UA = UA.rename({old_name[0]: "domain"})['domain'] # only 1 variable complies with above if
150+
UA = UA.rename({old_name[0]: "domain"})['domain'] # only 1 variable complies with the above if statement
151151

152152
# convert the xarray to pcraster
153153
UA = UA>=minuparea # check that the area is over the minimum
@@ -158,11 +158,10 @@ def mct_mask(channels_slope_file, ldd_file, uparea_file, mask_file='',
158158
try:
159159
MX = xr.open_dataset(mask_file)
160160
old_name = [i for i in list(MX.data_vars) if sorted(MX[i].dims)==sorted([x_proj, y_proj])]
161-
MX = MX.rename({old_name[0]: "domain"})['domain'] # only 1 variable complies with above if
161+
MX = MX.rename({old_name[0]: "domain"})['domain'] # only 1 variable complies with the above if statement
162162
except:
163163
print(f'The given mask path {mask_file} is not a valid path. All domain read from LDD file {ldd_file} is considered vaid.')
164164
MX = LD.copy(deep=True)
165-
MX = MX.fillna(0)*0+1
166165

167166
# use the exact same coords from channel slope file, just in case there are precision differences
168167
MX = MX.assign_coords({x_proj: x_all, y_proj: y_all})
@@ -213,14 +212,14 @@ def mct_mask(channels_slope_file, ldd_file, uparea_file, mask_file='',
213212
MCT.name = 'mct_mask'
214213

215214
# mask final data with the mask_file
216-
MCT = MCT.where(MX==1)
215+
MCT = MCT.where(MX.notnull())
217216

218217
return MCT
219218

220219

221220
def main():
222221
'function for running from command line'
223-
# ---------------- Read settings
222+
# ---------------- Read settingss
224223
args = getarg()
225224
channels_slope_file_arg = args.changradfile
226225
ldd_file_arg = args.LDDfile
@@ -234,8 +233,8 @@ def main():
234233

235234
mct_final = mct_mask(channels_slope_file=channels_slope_file_arg, ldd_file=ldd_file_arg, uparea_file=uparea_file_arg, mask_file=mask_file_arg,
236235
slp_threshold=slp_threshold_arg, nloops=nloops_arg, minuparea=minuparea_arg, coords_names=coords_names_arg)
237-
# lisflood does not read NaNs so the data are saved as boolean 0-1, with 0 being flagged as NaN for python reading
238-
mct_final.to_netcdf(outputfile_arg, encoding={"mct_mask": {'_FillValue': 0, 'dtype': 'int8'}})
236+
# lisflood does not read NaNs so the data are saved as boolean 0-1, with -1 being flagged as NaN for python reading
237+
mct_final.to_netcdf(outputfile_arg, encoding={"mct_mask": {'_FillValue': -1, 'dtype': 'int8'}})
239238

240239

241240
if __name__ == "__main__":

0 commit comments

Comments
 (0)