Skip to content

Commit 3fb8c42

Browse files
committed
Added fixes and TODOs for EPIC module compatibility. Fixed issue on test_results in xml files
1 parent dbe5bdd commit 3fb8c42

File tree

11 files changed

+44
-34
lines changed

11 files changed

+44
-34
lines changed

pytest.ini

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,4 @@ env =
1414
D:NUMEXPR_MAX_THREADS=12
1515
; D:PCRASTER_NR_WORKER_THREADS=4 gives errors with pcraster.multicore - need changes in many hydro modules
1616
; run pytest tests/ -m "slow" before release !!!
17-
;addopts = -ra -x -l --cov=lisflood --cov-config=.coveragerc -m "not slow" -vv
18-
19-
addopts = --no-cov -m "slow or not slow"
17+
addopts = -ra -x -l --cov=lisflood --cov-config=.coveragerc -m "not slow" -vv

src/lisflood/Lisflood_dynamic.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,11 @@
2727

2828
from .global_modules.settings import CDFFlags, LisSettings, MaskInfo
2929

30-
#import time # CR: speed test
31-
3230
class LisfloodModel_dyn(DynamicModel):
3331

3432
# =========== DYNAMIC ====================================================
3533

3634
def dynamic(self):
37-
#t0tot=time.time() # CR: speed test
38-
3935
""" Dynamic part of LISFLOOD
4036
calls the dynamic part of the hydrological modules
4137
"""
@@ -260,6 +256,4 @@ def splitlanduse(array1, array2=None, array3=None):
260256
self.stateVar_module.dynamic()
261257
if option['wateruse'] and option['indicator'] and self.monthend:
262258
self.indicatorcalc_module.dynamic_setzero()
263-
# setting monthly and yearly dindicator to zero at the end of the month (year)
264-
265-
#print(' - total time for this step: ', (time.time() - t0tot)) # CR: speed test
259+
# setting monthly and yearly dindicator to zero at the end of the month (year)

src/lisflood/Lisflood_initial.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -365,16 +365,22 @@ def defsoil(self, name_1, name_2=None, name_3=None, coords=None):
365365
coords = self.coord_landuse
366366
data = self.allocateDataArray(coords)
367367
values_1 = readInputWithBackup(name_1)
368-
if (list(coords.keys())[0]=="landuse") or (list(coords.keys())[0]=="runoff"):
369-
data.values[0][:] = values_1
370-
data.values[1][:] = readInputWithBackup(name_2, values_1)
371-
data.values[2][:] = readInputWithBackup(name_3, values_1)
368+
option = self.settings.options
369+
if option.get('cropsEPIC'):
370+
labels = list(coords.values())[0]
371+
data.loc[labels[0],:] = values_1
372+
data.loc[labels[1],:] = readInputWithBackup(name_2, values_1)
373+
data.loc[labels[2],:] = readInputWithBackup(name_3, values_1)
372374
else:
373-
raise Exception("Coords key not found!")
375+
if (list(coords.keys())[0]=="landuse") or (list(coords.keys())[0]=="runoff"):
376+
data.values[0][:] = values_1
377+
data.values[1][:] = readInputWithBackup(name_2, values_1)
378+
data.values[2][:] = readInputWithBackup(name_3, values_1)
379+
else:
380+
raise Exception("Coords key not found!")
374381
return data
375382

376383
def deffraction(self, variable):
377-
"""Weighted sum over the soil fractions of each pixel"""
378-
#ax_veg = variable.dims.index("vegetation")
379-
ax_veg=0
380-
return _vegSum(ax_veg, variable.values, self.SoilFraction.values)
384+
"""Weighted sum over the soil fractions of each pixel"""
385+
ax_veg = variable.dims.index("vegetation")
386+
return _vegSum(ax_veg, variable.values, self.SoilFraction.values)

src/lisflood/global_modules/add1.py

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ def __new__(cls, input_array):
5353
@property
5454
def values(self):
5555
return self
56+
57+
@property
58+
def dims(self):
59+
return ['vegetation']
5660

5761

5862
def defsoil(name1, name2=None, name3=None):

src/lisflood/hydrological_modules/soil.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def splitlanduse(array1, array2=None, array3=None):
267267

268268
for veg, luse in self.var.VEGETATION_LANDUSE.items():
269269
iveg = self.var.vegetation.index(veg)
270-
iluse = self.var.epic_settings.soil_uses.index(luse)
270+
iluse = self.var.SOIL_USES.index(luse)
271271
ini_1a = np.where(ThetaInit1aValue[iveg] == -9999, self.var.WFC1a[iluse], ThetaInit1aValue[iveg] * self.var.SoilDepth1a[iluse])
272272
self.var.W1a[iveg] = np.where(self.var.PoreSpaceNotZero1a[iluse], ini_1a, 0)
273273
ini_1b = np.where(ThetaInit1bValue[iveg] == -9999, self.var.WFC1b[iluse], ThetaInit1bValue[iveg] * self.var.SoilDepth1b[iluse])
@@ -498,8 +498,9 @@ def dynamic_perpixel(self):
498498
for landuse, veg_list in self.var.LANDUSE_VEGETATION.items():
499499
iveg_list,iveg_list_pres,ilanduse = self.var.get_indexes_from_landuse_and_veg_list_GLOBAL(landuse, veg_list)
500500
self.var.Theta[iveg_list] = self.var.SoilFraction[iveg_list_pres] * tot_sm[iveg_list] / self.var.SoilDepthTotal[ilanduse]
501-
soil_fract_sum = np.sum(self.var.SoilFraction,0)
502-
self.var.ThetaAll = np.where(soil_fract_sum > 0, np.sum(self.var.Theta,0) / soil_fract_sum, 0)
501+
ax_veg=self.var.SoilFraction.dims.index("vegetation")
502+
soil_fract_sum = np.sum(self.var.SoilFraction,ax_veg)
503+
self.var.ThetaAll = np.where(soil_fract_sum > 0, np.sum(self.var.Theta,ax_veg) / soil_fract_sum, 0)
503504

504505
self.var.SeepTopToSubPixelA = self.var.deffraction(self.var.SeepTopToSubA)
505506
self.var.SeepTopToSubPixelB = self.var.deffraction(self.var.SeepTopToSubB)

src/lisflood/hydrological_modules/soilloop.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ def initial(self):
464464
self.is_paddy_irrig = np.zeros(len(self.var.vegetation), bool)
465465

466466
self.index_landuse_all = np.array([self.var.SOIL_USES.index(self.var.VEGETATION_LANDUSE[v]) for v in self.var.vegetation]) #######################
467-
self.index_landuse_prescr = np.array([self.var.SOIL_USES.index(self.var.VEGETATION_LANDUSE[v]) for v in self.var.prescribed_vegetation]) #######################
467+
self.index_landuse_prescr = np.array([self.var.SOIL_USES.index(self.var.VEGETATION_LANDUSE[v]) for v in self.var.PRESCRIBED_VEGETATION]) #######################
468468
self.is_irrigated = np.array([self.var.VEGETATION_LANDUSE[v] == 'Irrigated' for v in self.var.vegetation]) #######################
469469
if option["cropsEPIC"]: #######################
470470
self.is_paddy_irrig = np.array([v in self.var.crop_module.paddy_crops for v in self.var.vegetation]) #######################

src/lisflood/hydrological_modules/surface_routing.py

+9
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,16 @@ def dynamic(self):
122122

123123
self.var.SurfaceRunSoil = self.var.allocateDataArray([self.var.dim_landuse, self.var.dim_pixel])
124124
for landuse, veg_list in self.var.LANDUSE_VEGETATION.items():
125+
# TODO: CR: to ensure compatibility with EPIC, get ax_veg for the sum operation from the actual dimesions on the variable
126+
# self.var.SurfaceRunSoil.loc[landuse] = (self.var.SoilFraction.loc[veg_list] * \
127+
# np.maximum(self.var.AvailableWaterForInfiltration.loc[veg_list] - self.var.Infiltration.loc[veg_list], 0)).sum("vegetation")
125128
iveg_list,iveg_list_pres,ilanduse = self.var.get_indexes_from_landuse_and_veg_list_GLOBAL(landuse, veg_list)
126129
self.var.SurfaceRunSoil.values[ilanduse] = np.sum((self.var.SoilFraction.values[iveg_list_pres] * \
127130
np.maximum(self.var.AvailableWaterForInfiltration.values[iveg_list] - self.var.Infiltration.values[iveg_list],0)),0)
128131

132+
# TODO: CR: to ensure compatibility with EPIC, get ax_veg for the sum operation from the actual dimesions on the variable
133+
# (probably implementing two different NumpyModified classes for vegetation and for landuse, checking also for any drop of performances)
134+
# self.var.SurfaceRunoff = self.var.DirectRunoff + self.var.SurfaceRunSoil.sum("landuse").values
129135
self.var.SurfaceRunoff = self.var.DirectRunoff + np.sum(self.var.SurfaceRunSoil.values,0)
130136

131137
# Surface runoff for this time step (mm)
@@ -142,6 +148,9 @@ def dynamic(self):
142148
# Routing of overland flow to channel using kinematic wave
143149
# Note that all 'new' water is added as side-flow
144150
SideflowDirect = self.var.DirectRunoff * self.var.MMtoM3 * self.var.InvPixelLength * self.var.InvDtSec
151+
# TODO: CR: to ensure compatibility with EPIC, get ax_veg for the sum operation from the actual dimesions on the variable
152+
# SideflowOther = self.var.SurfaceRunSoil.loc[["Rainfed","Irrigated"]].sum("landuse").values * self.var.MMtoM3 * self.var.InvPixelLength * self.var.InvDtSec
153+
# SideflowForest = self.var.SurfaceRunSoil.loc["Forest"].values * self.var.MMtoM3 * self.var.InvPixelLength * self.var.InvDtSec
145154
ilusevalues = []
146155
for luse in ["Rainfed","Irrigated"]:
147156
ilusevalues.append(self.var.epic_settings.soil_uses.index(luse))

src/lisflood/hydrological_modules/waterabstraction.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,7 @@ def dynamic(self):
349349
self.var.abstraction_required_irrigation_M3 = maskinfo.in_zero()
350350
else: # irrigation demand = transpiration deficit multiplied by anti-salinity factor
351351
irr_pre = "Irrigated_prescribed" # It applies only to prescribed irrigation fraction (EPIC simulates specific irrigated crops, if any)
352-
landuse_irr_pre = self.var.VEGETATION_LANDUSE[irr_pre] # landuse corresponding to vegetation fraction
353-
ivegIrrigatedPrescribed = self.var.vegetation.index(irr_pre)
354-
iluseIrrigatedPrescribed = self.var.epic_settings.soil_uses.index(landuse_irr_pre)
352+
ivegIrrigatedPrescribed, iluseIrrigatedPrescribed, _ = self.var.get_landuse_and_indexes_from_vegetation_GLOBAL(irr_pre)
355353

356354
self.var.Ta.values[ivegIrrigatedPrescribed] = np.maximum(np.minimum(self.var.RWS.values[ivegIrrigatedPrescribed] * self.var.potential_transpiration.values[ivegIrrigatedPrescribed],
357355
self.var.W1.values[ivegIrrigatedPrescribed] - self.var.WWP1.values[iluseIrrigatedPrescribed]), maskinfo.in_zero())

tests/data/LF_ETRS89_UseCase/settings/cold.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@
11121112
</comment>
11131113
</textvar>
11141114

1115-
<textvar name="LeakageFraction" value="0.2">
1115+
<textvar name="LeakageFraction" value="$(PathRoot)/maps/ad_leakage">
11161116
<comment>
11171117
0.2
11181118
$(PathRoot)/maps/leakage.map
@@ -4407,7 +4407,7 @@ LFBINDING: MORE LOW-LEVEL CONTROL OVER MODEL IN- AND OUTPUT
44074407
</comment>
44084408
</textvar>
44094409

4410-
<textvar name="FractionGroundwaterUsed" value="$(PathMapsLanduse)/fracgwusedNew">
4410+
<textvar name="FractionGroundwaterUsed" value="$(PathMapsLanduse)/fracgwused">
44114411
<comment>
44124412
groundwater used fraction of a pixel (0-1)
44134413
</comment>

tests/data/LF_ETRS89_UseCase/settings/prerun.xml

+4-4
Original file line numberDiff line numberDiff line change
@@ -411,13 +411,13 @@ The option "numCPUs_parallelNumba" may take the following values:
411411
**************************************************************
412412
</comment>
413413

414-
<textvar name="PathOut" value="$(PathRoot)/out/">
414+
<textvar name="PathOut" value="$(PathRoot)/out">
415415
<comment>
416416
Output path
417417
</comment>
418418
</textvar>
419419

420-
<textvar name="PathInit" value="$(PathRoot)/out/">
420+
<textvar name="PathInit" value="$(PathRoot)/out">
421421
<comment>
422422
Path of the initial value maps e.g. lzavin.map
423423
</comment>
@@ -1071,7 +1071,7 @@ The option "numCPUs_parallelNumba" may take the following values:
10711071
</comment>
10721072
</textvar>
10731073

1074-
<textvar name="LeakageFraction" value="0.2">
1074+
<textvar name="LeakageFraction" value="$(PathRoot)/maps/ad_leakage">
10751075
<comment>
10761076
Fraction of leakage of public water supply (0=no leakage, 1=100% leakage)
10771077
</comment>
@@ -4322,7 +4322,7 @@ LFBINDING: MORE LOW-LEVEL CONTROL OVER MODEL IN- AND OUTPUT
43224322
</comment>
43234323
</textvar>
43244324

4325-
<textvar name="FractionGroundwaterUsed" value="$(PathMapsLanduse)/fracgwusedNew">
4325+
<textvar name="FractionGroundwaterUsed" value="$(PathMapsLanduse)/fracgwused">
43264326
<comment>
43274327
groundwater used fraction of a pixel (0-1)
43284328
</comment>

tests/data/LF_ETRS89_UseCase/settings/warm.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,7 @@
10881088
</comment>
10891089
</textvar>
10901090

1091-
<textvar name="LeakageFraction" value="0.2">
1091+
<textvar name="LeakageFraction" value="$(PathRoot)/maps/ad_leakage">
10921092
<comment>
10931093
0.2
10941094
$(PathRoot)/maps/leakage.map
@@ -4384,7 +4384,7 @@ LFBINDING: MORE LOW-LEVEL CONTROL OVER MODEL IN- AND OUTPUT
43844384
</comment>
43854385
</textvar>
43864386

4387-
<textvar name="FractionGroundwaterUsed" value="$(PathMapsLanduse)/fracgwusedNew">
4387+
<textvar name="FractionGroundwaterUsed" value="$(PathMapsLanduse)/fracgwused">
43884388
<comment>
43894389
groundwater used fraction of a pixel (0-1)
43904390
</comment>

0 commit comments

Comments
 (0)