Distributed Hydrological Model

After preparing all the meteorological, GIS inputs required for the model, and Extracting the parameters for the catchment

import numpy as np
import datetime as dt
import gdal
from Hapi.rrm.calibration import Calibration
import Hapi.rrm.hbv_bergestrom92 as HBV

import Hapi.statistics.performancecriteria as PC

Path = Comp + "/data/distributed/coello"
PrecPath = Path + "/prec"
Evap_Path = Path + "/evap"
TempPath = Path + "/temp"
FlowAccPath = Path + "/GIS/acc4000.tif"
FlowDPath = Path + "/GIS/fd4000.tif"
CalibPath = Path + "/calibration"
SaveTo = Path + "/results"

AreaCoeff = 1530
#[sp,sm,uz,lz,wc]
InitialCond = [0,5,5,5,0]
Snow = 0

# Create the model object and read the input data

Sdate = '2009-01-01'
Edate = '2011-12-31'
name = "Coello"
Coello = Calibration(name, Sdate, Edate, SpatialResolution = "Distributed")

# Meteorological & GIS Data
Coello.readRainfall(PrecPath)
Coello.readTemperature(TempPath)
Coello.readET(Evap_Path)

Coello.readFlowAcc(FlowAccPath)
Coello.readFlowDir(FlowDPath)

# Lumped Model
Coello.readLumpedModel(HBV, AreaCoeff, InitialCond)

# Gauges Data
Coello.readGaugeTable(Path+"/stations/gauges.csv", FlowAccPath)
GaugesPath = Path+"/stations/"
Coello.readDischargeGauges(GaugesPath, column='id', fmt="%Y-%m-%d")

-Spatial Variability Object

from Hapi.rrm.distparameters import DistParameters as DP

  • The DistParameters distribute the parameter vector on the cells following some sptial logic (same set of parameters for all cells, different parameters for each cell, HRU, different parameters for each class in aditional map)

raster = gdal.Open(FlowAccPath)
#-------------
# for lumped catchment parameters
no_parameters = 12
klb = 0.5
kub = 1
#------------
no_lumped_par = 1
lumped_par_pos = [7]

SpatialVarFun = DP(raster, no_parameters, no_lumped_par=no_lumped_par,
                   lumped_par_pos=lumped_par_pos,Function=2, Klb=klb, Kub=kub)
# calculate no of parameters that optimization algorithm is going to generate
SpatialVarFun.ParametersNO
  • Define the objective function


 1coordinates = Coello.GaugesTable[['id','x','y','weight']][:]
 2
 3# define the objective function and its arguments
 4OF_args = [coordinates]
 5
 6def OF(Qobs, Qout, q_uz_routed, q_lz_trans, coordinates):
 7    Coello.extractDischarge()
 8    all_errors=[]
 9    # error for all internal stations
10    for i in range(len(coordinates)):
11        all_errors.append((PC.RMSE(Qobs.loc[:,Qobs.columns[0]],Coello.Qsim[:,i]))) #*coordinates.loc[coordinates.index[i],'weight']
12    print(all_errors)
13    error = sum(all_errors)
14    return error
15
16Coello.readObjectiveFn(OF, OF_args)

-Calibration algorithm Arguments

  • Create the options dictionary all the optimization parameters should be passed to the optimization object inside the option dictionary:

to see all options import Optimizer class and check the documentation of the method setOption

 1ApiObjArgs = dict(hms=50, hmcr=0.95, par=0.65, dbw=2000, fileout=1,
 2                  filename=SaveTo + "/Coello_"+str(dt.datetime.now())[0:10]+".txt")
 3
 4for i in range(len(ApiObjArgs)):
 5    print(list(ApiObjArgs.keys())[i], str(ApiObjArgs[list(ApiObjArgs.keys())[i]]))
 6
 7pll_type = 'POA'
 8pll_type = None
 9
10ApiSolveArgs = dict(store_sol=True, display_opts=True, store_hst=True,hot_start=False)
11
12OptimizationArgs=[ApiObjArgs, pll_type, ApiSolveArgs]
  • Run Calibration algorithm


cal_parameters = Coello.runCalibration(SpatialVarFun, OptimizationArgs,printError=0)
  • Save results


SpatialVarFun.Function(Coello.Parameters, kub=SpatialVarFun.Kub, klb=SpatialVarFun.Klb)
SpatialVarFun.saveParameters(SaveTo)