Skip to content

Commit a1855eb

Browse files
committed
Fixed issue with .end filenames. Added option to force loading maps having NaNs for external tools manipulations
1 parent 5317b2a commit a1855eb

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/lisflood/global_modules/add1.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ def loadsetclone(name):
265265
return map_out
266266

267267

268-
def compressArray(map, pcr=True, name=None):
268+
def compressArray(map, pcr=True, name=None, force_load_with_nans=False):
269269
maskinfo = MaskInfo.instance()
270270
if pcr:
271271
mapnp = pcr2numpy(map,np.nan)
@@ -278,11 +278,13 @@ def compressArray(map, pcr=True, name=None):
278278
mapnp1 = np.ma.masked_array(map, maskinfo.info.mask)
279279
mapC = np.ma.compressed(mapnp1)
280280

281-
if name is not None:
282-
if np.max(np.isnan(mapC)):
283-
msg = name + " has less valid pixels than area or ldd \n"
284-
raise LisfloodError(msg)
285-
# test if map has less valid pixel than area.map (or ldd)
281+
282+
if force_load_with_nans==False:
283+
if name is not None:
284+
if np.max(np.isnan(mapC)):
285+
msg = name + " has less valid pixels than area or ldd \n"
286+
raise LisfloodError(msg)
287+
# test if map has less valid pixel than area.map (or ldd)
286288
return mapC.astype(float)
287289

288290

@@ -342,7 +344,7 @@ def loadmap_cached(*args, **kwargs):
342344
return loadmap_base(*args, **kwargs)
343345

344346

345-
def loadmap_base(name, pcr=False, lddflag=False, timestampflag='exact', averageyearflag=False, value=None):
347+
def loadmap_base(name, pcr=False, lddflag=False, timestampflag='exact', averageyearflag=False, value=None, force_load_with_nans=False):
346348
""" Load a static map either value or pcraster map or netcdf (single or stack)
347349
348350
Load a static map either value or pcraster map or netcdf (single or stack)
@@ -357,6 +359,9 @@ def loadmap_base(name, pcr=False, lddflag=False, timestampflag='exact', averagey
357359
:param lddflag: flag for local drain direction map (CM??)
358360
:param timestampflag: look for exact time stamp in netcdf file ('exact') or for the closest (left) time stamp available ('closest')
359361
:param averageyearflag: if True, use "average year" netcdf file over the entire model simulation period
362+
:param force_load_with_nans: if True, loads the map without checking for nan values inside area Map.
363+
Warning: this flag should be used ONLY when managing and manipulating incomplete maps
364+
(maps should be completed before using into actual simulations, otherwise Lisflood will fail)
360365
:return: map or mapC
361366
:except: pcr: maps must have the same size of clone.map
362367
netCDF: time step timestepInit must be included into the stack
@@ -398,7 +403,8 @@ def loadmap_base(name, pcr=False, lddflag=False, timestampflag='exact', averagey
398403
# if failed before try reading from netCDF map format
399404
if not load:
400405
# read a netcdf (single one not a stack)
401-
filename = os.path.splitext(value)[0] + '.nc'
406+
# here we already tried to load the map as PCRaster and failed, thus try as NetCDF (with or without .nc extension)
407+
filename = value if value.lower().endswith('.nc') else value + '.nc'
402408
# get mapextend of netcdf map and calculate the cutting
403409
cut0, cut1, cut2, cut3 = mapattrNetCDF(filename)
404410
# load netcdf map but only the rectangle needed
@@ -519,7 +525,7 @@ def loadmap_base(name, pcr=False, lddflag=False, timestampflag='exact', averagey
519525
if lddflag:
520526
map = pcraster.ldd(pcraster.nominal(map))
521527
else:
522-
mapC = compressArray(mapnp, pcr=False, name=filename)
528+
mapC = compressArray(mapnp, pcr=False, name=filename, force_load_with_nans = force_load_with_nans)
523529
flagmap = True
524530

525531
# pcraster map but it has to be an array

0 commit comments

Comments
 (0)