@@ -96,7 +96,7 @@ def mct_mask(channels_slope_file, ldd_file, uparea_file, mask_file='',
96
96
# proprocess CH dataset for having correct format
97
97
CH = xr .open_dataset (channels_slope_file )
98
98
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
100
100
CH ['changrad' ] = CH ['changrad' ].transpose (y_proj , x_proj ) # make sure dims order is as pcraster needs
101
101
102
102
# ---------------- Set clone map for pcraster
@@ -147,7 +147,7 @@ def mct_mask(channels_slope_file, ldd_file, uparea_file, mask_file='',
147
147
# ---------------- Read upstream area
148
148
UA = xr .open_dataset (uparea_file )
149
149
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
151
151
152
152
# convert the xarray to pcraster
153
153
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='',
158
158
try :
159
159
MX = xr .open_dataset (mask_file )
160
160
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
162
162
except :
163
163
print (f'The given mask path { mask_file } is not a valid path. All domain read from LDD file { ldd_file } is considered vaid.' )
164
164
MX = LD .copy (deep = True )
165
- MX = MX .fillna (0 )* 0 + 1
166
165
167
166
# use the exact same coords from channel slope file, just in case there are precision differences
168
167
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='',
213
212
MCT .name = 'mct_mask'
214
213
215
214
# mask final data with the mask_file
216
- MCT = MCT .where (MX == 1 )
215
+ MCT = MCT .where (MX . notnull () )
217
216
218
217
return MCT
219
218
220
219
221
220
def main ():
222
221
'function for running from command line'
223
- # ---------------- Read settings
222
+ # ---------------- Read settingss
224
223
args = getarg ()
225
224
channels_slope_file_arg = args .changradfile
226
225
ldd_file_arg = args .LDDfile
@@ -234,8 +233,8 @@ def main():
234
233
235
234
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 ,
236
235
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' }})
239
238
240
239
241
240
if __name__ == "__main__" :
0 commit comments