Skip to content

Commit 6e656a4

Browse files
committed
added flag for additional check of maskinfo availability and correct usage in loadmap
1 parent d97ff0f commit 6e656a4

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/lisflood/global_modules/add1.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ def loadsetclone(name):
249249
_ = MaskAreaInfo(maskchkarea, map_out) # MaskAreaInfo init here
250250
# put in the ldd map
251251
# if there is no ldd at a cell, this cell should be excluded from modelling
252-
ldd = loadmap('Ldd', pcr=True)
252+
# set "skip_mask_info" to True, as MaskInfo is initialized only later in the code
253+
ldd = loadmap('Ldd', pcr=True, skip_mask_info = True)
253254
# convert ldd to numpy
254255
maskldd = pcr2numpy(ldd, np.nan)
255256

@@ -344,7 +345,7 @@ def loadmap_cached(*args, **kwargs):
344345
return loadmap_base(*args, **kwargs)
345346

346347

347-
def loadmap_base(name, pcr=False, lddflag=False, timestampflag='exact', averageyearflag=False, value=None, force_load_with_nans=False):
348+
def loadmap_base(name, pcr=False, lddflag=False, timestampflag='exact', averageyearflag=False, value=None, force_load_with_nans=False, skip_mask_info=False):
348349
""" Load a static map either value or pcraster map or netcdf (single or stack)
349350
350351
Load a static map either value or pcraster map or netcdf (single or stack)
@@ -362,6 +363,8 @@ def loadmap_base(name, pcr=False, lddflag=False, timestampflag='exact', averagey
362363
:param force_load_with_nans: if True, loads the map without checking for nan values inside area Map.
363364
Warning: this flag should be used ONLY when managing and manipulating incomplete maps
364365
(maps should be completed before using into actual simulations, otherwise Lisflood will fail)
366+
:param skip_mask_info: if True do not use MaskInfo instance to mask values
367+
(this is only used in loadsetclone when MaskInfo is not available yet)
365368
:return: map or mapC
366369
:except: pcr: maps must have the same size of clone.map
367370
netCDF: time step timestepInit must be included into the stack
@@ -498,12 +501,13 @@ def loadmap_base(name, pcr=False, lddflag=False, timestampflag='exact', averagey
498501

499502
# masking
500503
try:
501-
maskinfo = MaskInfo.instance()
502-
if mapnp.mask is not np.bool_(0):
503-
if (mapnp.mask[maskinfo.info.mask==False].any()==True):
504-
## warning: fill values masking is different from the area mask map, and some values in the area mask map contains invalid fill values
505-
warnings.warn(LisfloodWarning("Warning: map {} (binding: '{}') has fill values inside the area mask map!".format(filename, name)))
506-
mapnp.mask = maskinfo.info.mask
504+
if skip_mask_info is False:
505+
maskinfo = MaskInfo.instance()
506+
if mapnp.mask is not np.bool_(0):
507+
if (mapnp.mask[maskinfo.info.mask==False].any()==True):
508+
## warning: fill values masking is different from the area mask map, and some values in the area mask map contains invalid fill values
509+
warnings.warn(LisfloodWarning("Warning: map {} (binding: '{}') has fill values inside the area mask map!".format(filename, name)))
510+
mapnp.mask = maskinfo.info.mask
507511
except (KeyError, AttributeError):
508512
pass
509513
nf1.close()
@@ -525,11 +529,13 @@ def loadmap_base(name, pcr=False, lddflag=False, timestampflag='exact', averagey
525529
if lddflag:
526530
map = pcraster.ldd(pcraster.nominal(map))
527531
else:
532+
assert(not skip_mask_info)
528533
mapC = compressArray(mapnp, pcr=False, name=filename, force_load_with_nans = force_load_with_nans)
529534
flagmap = True
530535

531536
# pcraster map but it has to be an array
532537
if pcrmap and not pcr:
538+
assert(not skip_mask_info)
533539
mapC = compressArray(map, name=filename)
534540

535541
if flags['checkfiles']:
@@ -541,6 +547,7 @@ def loadmap_base(name, pcr=False, lddflag=False, timestampflag='exact', averagey
541547
else:
542548
#print(name, mapC.size)
543549
if mapC.size > 0:
550+
assert(not skip_mask_info)
544551
map= decompress(mapC)
545552
checkmap(name, filename, map, flagmap, 0)
546553
if pcr:

0 commit comments

Comments
 (0)