@@ -265,7 +265,7 @@ def loadsetclone(name):
265
265
return map_out
266
266
267
267
268
- def compressArray (map , pcr = True , name = None ):
268
+ def compressArray (map , pcr = True , name = None , force_load_with_nans = False ):
269
269
maskinfo = MaskInfo .instance ()
270
270
if pcr :
271
271
mapnp = pcr2numpy (map ,np .nan )
@@ -278,11 +278,13 @@ def compressArray(map, pcr=True, name=None):
278
278
mapnp1 = np .ma .masked_array (map , maskinfo .info .mask )
279
279
mapC = np .ma .compressed (mapnp1 )
280
280
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)
286
288
return mapC .astype (float )
287
289
288
290
@@ -342,7 +344,7 @@ def loadmap_cached(*args, **kwargs):
342
344
return loadmap_base (* args , ** kwargs )
343
345
344
346
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 ):
346
348
""" Load a static map either value or pcraster map or netcdf (single or stack)
347
349
348
350
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
357
359
:param lddflag: flag for local drain direction map (CM??)
358
360
:param timestampflag: look for exact time stamp in netcdf file ('exact') or for the closest (left) time stamp available ('closest')
359
361
: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)
360
365
:return: map or mapC
361
366
:except: pcr: maps must have the same size of clone.map
362
367
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
398
403
# if failed before try reading from netCDF map format
399
404
if not load :
400
405
# 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'
402
408
# get mapextend of netcdf map and calculate the cutting
403
409
cut0 , cut1 , cut2 , cut3 = mapattrNetCDF (filename )
404
410
# load netcdf map but only the rectangle needed
@@ -519,7 +525,7 @@ def loadmap_base(name, pcr=False, lddflag=False, timestampflag='exact', averagey
519
525
if lddflag :
520
526
map = pcraster .ldd (pcraster .nominal (map ))
521
527
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 )
523
529
flagmap = True
524
530
525
531
# pcraster map but it has to be an array
0 commit comments