Example #1
0
 def opt_fun(par):
     try:
         # parameters
         klb=float(par[-2])
         kub=float(par[-1])
         par=par[:-2]
                     
         par_dist=SpatialVarFun(par,*SpatialVarArgs,kub=kub,klb=klb)    
         
         #run the model
         _, q_out, q_uz_routed, q_lz_trans=Wrapper.Dist_model(ConceptualModel,
                                                              acc, fd, prec, evap,
                                                              temp, par_dist, p2,
                                                              snow , init_st)
         
         # calculate performance of the model
         try:
             error=OF(Q_obs,q_out,q_uz_routed,q_lz_trans,*OF_args)
         except TypeError: # if no of inputs less than what the function needs
             assert 1==5, "the objective function you have entered needs more inputs please enter then in a list as *args"
             
         # print error
         if printError != 0:
             print(error)
             print(par)
         
         fail = 0
     except:
         error = np.nan
         fail = 1
         
     return error, [], fail 
Example #2
0
def run_Distributed_model():
    performance.text = str("<h2>processing...<h2>")
    # read values of parameters

    # data validation

    #    _area = float(w_area.value)
    #    pars222222 = [_k1, _k2, _k3, _k4, _d1, _d2, _s1, _s2]
    #    extra_pars22222 = [_dt, _area]

    performance_Q = {}
    calc_Q = pd.DataFrame(index=index)

    calc_Q['Q'], _, _, _, _, _ = Wrapper.Dist_model(
        lake_data_A,
        extra_pars,
        curve,
        lakecell,
        DEM,
        flow_acc_table,
        flow_acc,
        sp_prec_c,
        sp_et_c,
        sp_temp_c,
        pars,
        kub,
        klb,
        jiboa_initial=jiboa_initial,
        lake_initial=lake_initial,
        ll_temp=None,
        q_0=None)

    # Calculate model performance
    performance.text = str("<h2>calculating model performance..<h2>")

    WS = {
    }  #------------------------------------------------------------------------
    WS['type'] = 1  #------------------------------------------------------------------------
    WS['N'] = 3  #------------------------------------------------------------------------

    performance_Q['c_error_hf'] = Performance_criteria.rmseHF(
        lake_data['Q'], calc_Q['Q'], WS['type'], WS['N'], 0.75
    )  #------------------------------------------------------------------------
    performance_Q['c_error_lf'] = Performance_criteria.rmseLF(
        lake_data['Q'], calc_Q['Q'], WS['type'], WS['N'], 0.75
    )  #------------------------------------------------------------------------
    performance_Q['c_nsehf'] = Performance_criteria.nse(
        lake_data_A[:, -1], calc_Q['Q']
    )  #------------------------------------------------------------------------
    performance_Q['c_rmse'] = Performance_criteria.rmse(
        lake_data_A[:, -1], calc_Q['Q']
    )  #------------------------------------------------------------------------
    performance_Q['c_nself'] = Performance_criteria.nse(
        np.log(lake_data_A[:, -1]), np.log(calc_Q['Q'])
    )  #------------------------------------------------------------------------
    performance_Q['c_KGE'] = Performance_criteria.KGE(
        lake_data_A[:, -1], calc_Q['Q']
    )  #------------------------------------------------------------------------
    performance_Q['c_wb'] = Performance_criteria.WB(
        lake_data_A[:, -1], calc_Q['Q']
    )  #------------------------------------------------------------------------

    # update data source
    ds_sim.data = (dict(q_sim=calc_Q['Q'].tolist(), ds_time=index))

    performance.text = str("<h2>Model perfomance(RMSE) is %s<h2>" %
                           round(performance_Q['c_rmse'], 3))
Example #3
0
def RunModel(ConceptualModel, Paths, ParPath, p2, init_st, snow):
    """
    =======================================================================
        RunModel(PrecPath, Evap_Path, TempPath, DemPath, FlowAccPath, FlowDPath, ParPath, p2)
    =======================================================================
    this function runs the conceptual distributed hydrological model
    
    Inputs:
        1-Paths:
            1-PrecPath:
                [String] path to the Folder contains precipitation rasters
            2-Evap_Path:
                [String] path to the Folder contains Evapotranspiration rasters
            3-TempPath:
                [String] path to the Folder contains Temperature rasters
            4-FlowAccPath:
                [String] path to the Flow Accumulation raster of the catchment (it should
                include the raster name and extension)
            5-FlowDPath:
                [String] path to the Flow Direction raster of the catchment (it should
                include the raster name and extension)
        7-ParPath:
            [String] path to the Folder contains parameters rasters of the catchment 
        8-p2:
            [List] list of unoptimized parameters
            p2[0] = tfac, 1 for hourly, 0.25 for 15 min time step and 24 for daily time step
            p2[1] = catchment area in km2
    Outputs:
        1- st:
            [4D array] state variables
        2- q_out:
            [1D array] calculated Discharge at the outlet of the catchment
        3- q_uz:
            [3D array] Distributed discharge for each cell
    Example:
        PrecPath = prec_path="meteodata/4000/calib/prec"
        Evap_Path = evap_path="meteodata/4000/calib/evap"
        TempPath = temp_path="meteodata/4000/calib/temp"
        DemPath = "GIS/4000/dem4000.tif"
        FlowAccPath = "GIS/4000/acc4000.tif"
        FlowDPath = "GIS/4000/fd4000.tif"
        ParPath = "meteodata/4000/parameters"
        p2=[1, 227.31]
        st, q_out, q_uz_routed = RunModel(PrecPath,Evap_Path,TempPath,DemPath,
                                          FlowAccPath,FlowDPath,ParPath,p2)
    """
    # input data validation
    assert len(Paths) == 5, "Paths should include 5 folder pathes " +str(len(Paths))+" paths are only provided"
    
    PrecPath=Paths[0]
    Evap_Path=Paths[1]
    TempPath=Paths[2]
#    DemPath=Paths[3]
    FlowAccPath=Paths[3]
    FlowDPath=Paths[4]
    
    # data type
    assert type(PrecPath)== str, "PrecPath input should be string type"
    assert type(Evap_Path)== str, "Evap_Path input should be string type"
    assert type(TempPath)== str, "TempPath input should be string type"
#    assert type(DemPath)== str, "DemPath input should be string type"
    assert type(FlowAccPath)== str, "FlowAccPath input should be string type"
    assert type(FlowDPath)== str, "FlowDPath input should be string type"
    assert type(ParPath)== str, "ParPath input should be string type"
    
    
    # input values
#    dem_ext=DemPath[-4:]
#    assert dem_ext == ".tif", "please add the extension at the end of the DEM raster path input"
    acc_ext=FlowAccPath[-4:]
    assert acc_ext == ".tif", "please add the extension at the end of the Flow accumulation raster path input"
    fd_ext=FlowDPath[-4:]
    assert fd_ext == ".tif", "please add the extension at the end of the Flow Direction path input"
    # check wether the path exists or not
    assert os.path.exists(PrecPath), PrecPath + " you have provided does not exist"
    assert os.path.exists(Evap_Path), Evap_Path+" path you have provided does not exist"
    assert os.path.exists(TempPath), TempPath+" path you have provided does not exist"
#    assert os.path.exists(DemPath), DemPath+ " you have provided does not exist"
    assert os.path.exists(FlowAccPath), FlowAccPath + " you have provided does not exist"
    assert os.path.exists(FlowDPath), FlowDPath+ " you have provided does not exist"
    # check wether the folder has the rasters or not 
    assert len(os.listdir(PrecPath)) > 0, PrecPath+" folder you have provided is empty"
    assert len(os.listdir(Evap_Path)) > 0, Evap_Path+" folder you have provided is empty"
    assert len(os.listdir(TempPath)) > 0, TempPath+" folder you have provided is empty"
    
    # read data
    ### meteorological data
    prec=GIS.ReadRastersFolder(PrecPath)
    evap=GIS.ReadRastersFolder(Evap_Path)
    temp=GIS.ReadRastersFolder(TempPath)
    print("meteorological data are read successfully")
    
    #### GIS data
#    dem= gdal.Open(DemPath) 
    acc=gdal.Open(FlowAccPath)
    fd=gdal.Open(FlowDPath)
    print("GIS data are read successfully")
    
    # parameters
    parameters=GIS.ReadRastersFolder(ParPath)
    print("Parameters are read successfully")
    
    #run the model
    st, q_out, q_uz, q_lz = Wrapper.Dist_model(ConceptualModel, acc, fd, prec, evap,
                                               temp, parameters, p2, snow, init_st)
    
    return st, q_out, q_uz, q_lz