Ejemplo n.º 1
0
def run(ini_file='plot_soil_moisture_maps.ini'):

    config.read(ini_file)
    print 'Read the file ',ini_file

    file_global_param=config.get('files','file_global_param')
    file_cell_param=config.get('files','file_cell_param')
    file_sim=config.get('files','file_sim')

    path_out=config.get('paths','path_out')

    fac_L=config.getfloat('calib_params','fac_L')
    fac_Ks=config.getfloat('calib_params','fac_Ks')
    fac_n_o=config.getfloat('calib_params','fac_n_o')
    fac_n_c=config.getfloat('calib_params','fac_n_c')

    t1=config.getfloat('flags','t1')
    t2=config.getfloat('flags','t2')
    variable=config.getfloat('flags','variable')

    ##~~~~~~PROGRAM~~~~~##
    # Create the folder if it doesn't exist
    ut.check_folder_exist(path_out)

    #~~~~Read Global parameters file
    X,Dt,alpha_s,alpha_o,alpha_c,A_thres,W_min,W_max\
      =pm.read_global_parameters(file_global_param)
    #~~~~Read Cell parameters file
    ar_cell_label, ar_coorx, \
    ar_coory, ar_lambda, \
    ar_Xc, ar_dam, \
    ar_tan_beta, ar_tan_beta_channel, \
    ar_L0, ar_Ks0, \
    ar_theta_r, ar_theta_s, \
    ar_n_o0, ar_n_c0, \
    ar_cell_down, ar_pVs_t0, \
    ar_Vo_t0, ar_Qc_t0, \
    ar_kc, psi_b, lamda = pm.read_cell_parameters(file_cell_param)
    #~~~~Number of cell in the catchment
    nb_cell=len(ar_cell_label)
    #~~~~Computation of cell order
    ar_label_sort=pm.sort_cell(ar_cell_label,ar_cell_down)
    #~~~~Computation of upcells
    li_cell_up=pm.direct_up_cell(ar_cell_label,ar_cell_down,ar_label_sort)
    #~~~~Computation of drained area
    ar_A_drained=pm.drained_area(ar_label_sort,li_cell_up,X)
    #~~~~Modifies the values of the parameters
    ar_L=ar_L0*fac_L
    ar_Ks=ar_Ks0*fac_Ks
    ar_n_o=ar_n_o0*fac_n_o
    ar_n_c=ar_n_c0*fac_n_c
    #~~~~Computation of model parameters from physical parameters
    ar_Vsm, ar_b_s, ar_b_o, ar_W, ar_b_c\
      =pm.compute_cell_param(X,ar_Xc,Dt,alpha_s,alpha_o,alpha_c,nb_cell,\
                              A_thres,W_max,W_min,\
                              ar_lambda,ar_tan_beta,ar_tan_beta_channel,ar_L,\
                              ar_Ks,ar_theta_r,ar_theta_s,ar_n_o,ar_n_c,\
                              ar_A_drained)

    #Read of data from the outputs of TOPKAPI in hdf5 format
    ndar_Vs=np.array(ut.read_one_array_hdf(file_sim,'/Soil/','V_s'))

    #Assign the variables
    if variable==1:
        im_out=os.path.join(path_out, 'field_Vs_')
        tab=ndar_Vs
    elif variable==2:
        im_out=os.path.join(path_out, 'field_Vo_')
        tab=ndar_Vo
    elif variable==3:
        im_out=os.path.join(path_out, 'field_Vo_bin_')
        tab=ndar_Vo
        tab[tab>0]=1.
    elif variable==4:
        im_out=os.path.join(path_out, 'field_SSI_')
        tab=ndar_Vs/ar_Vsm*100.

    # Plot the maps
    for t in np.arange(int(t1),int(t2+1)):
        print 'Map time-step ', t
        image_out=im_out+ut.string(t,len(str(t2)))+'.png'
        field_map_ndar(tab,t,ar_coorx,ar_coory,X,image_out,variable)
Ejemplo n.º 2
0
def mean_simuVsi(ini_file='mean_simuVsi.ini'):
    """
    * Objective

    """

    ### READ THE INI FILE ###
    config.read(ini_file)
    print('Read the file ',ini_file)

    ##~~~~~~ file_in ~~~~~~##
    file_in=config.get('file_in','file_in')
    file_in_global=config.get('file_in','file_in_global')
    file_h5=config.get('file_in','file_h5')

    ##~~~~~~ file_out ~~~~~~##
    file_out=config.get('file_out','file_out')

    ##~~~~~~ variables ~~~~~~##
    mean_pVs_t0=config.getfloat('variables','mean_pVs_t0')
    fac_L_simu=config.getfloat('variables','fac_L_simu')
    fac_Ks_simu=config.getfloat('variables','fac_Ks_simu')
    fac_n_o_simu=config.getfloat('variables','fac_n_o_simu')
    fac_n_c_simu=config.getfloat('variables','fac_n_c_simu')

    ##~~~~~~ flags ~~~~~~##
    nb_param=config.getfloat('flags','nb_param')

    #--Read and compute the parameters to have the values of parameter and ar_Vsm
    #~~~~Read Global parameters file
    print('Pretreatment of input data')
    #~~~~Read Global parameters file
    X,Dt,alpha_s,alpha_o,alpha_c,A_thres,W_min,W_max\
      =pm.read_global_parameters(file_in_global)
    #~~~~Read Cell parameters file
    ar_cell_label,ar_coorx,ar_coory,ar_lambda,ar_Xc,ar_dam,ar_tan_beta,ar_tan_beta_channel,ar_L,ar_Ks,\
    ar_theta_r,ar_theta_s,ar_n_o,ar_n_c,\
    ar_cell_down,ar_pVs_t0,ar_Vo_t0,ar_Qc_t0,ar_kc\
        =pm.read_cell_parameters(file_in)
    #~~~~Number of cell in the catchment
    nb_cell=len(ar_cell_label)
    #~~~~Computation of cell order
    ar_label_sort=pm.sort_cell(ar_cell_label,ar_cell_down)
    #~~~~Computation of upcells
    li_cell_up=pm.direct_up_cell(ar_cell_label,ar_cell_down,ar_label_sort)
    #~~~~Computation of drained area
    ar_A_drained=pm.drained_area(ar_label_sort,li_cell_up,X)
    #~~~~Modifies the values of the parameters
    ar_L1=ar_L*fac_L_simu
    ar_Ks1=ar_Ks*fac_Ks_simu
    ar_n_o1=ar_n_o*fac_n_o_simu
    ar_n_c1=ar_n_c*fac_n_c_simu
    #~~~~Computation of model parameters from physical parameters
    ar_Vsm, ar_b_s, ar_b_o, ar_W, ar_b_c\
      =pm.compute_cell_param(X,ar_Xc,Dt,alpha_s,alpha_o,alpha_c,nb_cell,\
                              A_thres,W_max,W_min,\
                              ar_lambda,ar_tan_beta,ar_tan_beta_channel,ar_L1,\
                              ar_Ks1,ar_theta_r,ar_theta_s,ar_n_o1,ar_n_c1,\
                              ar_A_drained)

    #Read the soil volume file
    ndar_Vs=np.array(ut.read_one_array_hdf(file_h5,'Soil','V_s'))

    #Read the file of catchment saturation rates
    ndar_Vs_sat=ndar_Vs/ar_Vsm*100.
    tab_rate=np.average(ndar_Vs_sat,axis=1)
    tab_rate_sort=np.sort(tab_rate)
    indice_sort=np.argsort(tab_rate)

    #Look for the rate closest to the expected mean value
    if mean_pVs_t0<tab_rate_sort[0]:
        ind=0
        print('mean_pVs_t0 expected:', mean_pVs_t0,'effective:',tab_rate_sort[0])
    elif mean_pVs_t0>tab_rate_sort[-1]:
        ind=-1
        print('mean_pVs_t0 expected:', mean_pVs_t0,' effective',tab_rate_sort[-1])
    else:
        loop=True
        i=-1
        while loop:
            i=i+1
            if mean_pVs_t0>=tab_rate_sort[i] and mean_pVs_t0<tab_rate_sort[i+1]:
                ind=i
                loop=False
                print('mean_pVs_t0 expected:', mean_pVs_t0,' effective:',tab_rate_sort[i])

    ind_end=indice_sort[ind]
    print(ind,ind_end)
    ar_Vs=ndar_Vs[ind_end,:]
    ar_Vsi=ar_Vs/ar_Vsm*100.
    print(ar_Vsi)
    ar_pVs_t0=ar_Vsi

    #~~~~~~Write parameter file~~~~~~#
    tab_param=np.zeros((len(ar_cell_label),nb_param))

    tab_param[:,0]=ar_cell_label
    tab_param[:,1]=ar_coorx
    tab_param[:,2]=ar_coory
    tab_param[:,3]=ar_lambda
    tab_param[:,4]=ar_Xc
    tab_param[:,5]=ar_dam
    tab_param[:,6]=ar_tan_beta
    tab_param[:,7]=ar_tan_beta_channel
    tab_param[:,8]=ar_L
    tab_param[:,9]=ar_Ks
    tab_param[:,10]=ar_theta_r
    tab_param[:,11]=ar_theta_s
    tab_param[:,12]=ar_n_o
    tab_param[:,13]=ar_n_c
    tab_param[:,14]=ar_cell_down
    tab_param[:,15]=ar_pVs_t0
    tab_param[:,16]=ar_Vo_t0
    tab_param[:,17]=ar_Qc_t0
    tab_param[:,18]=ar_kc

    np.savetxt(file_out, tab_param)
Ejemplo n.º 3
0
def run(ini_file='plot_soil_moisture_maps.ini'):

    config.read(ini_file)
    print('Read the file ',ini_file)

    file_global_param=config.get('files','file_global_param')
    file_cell_param=config.get('files','file_cell_param')
    file_sim=config.get('files','file_sim')

    path_out=config.get('paths','path_out')

    fac_L=config.getfloat('calib_params','fac_L')
    fac_Ks=config.getfloat('calib_params','fac_Ks')
    fac_n_o=config.getfloat('calib_params','fac_n_o')
    fac_n_c=config.getfloat('calib_params','fac_n_c')

    t1=config.getfloat('flags','t1')
    t2=config.getfloat('flags','t2')
    variable=config.getfloat('flags','variable')

    ##~~~~~~PROGRAM~~~~~##
    # Create the folder if it doesn't exist
    ut.check_folder_exist(path_out)

    #~~~~Read Global parameters file
    X,Dt,alpha_s,alpha_o,alpha_c,A_thres,W_min,W_max\
      =pm.read_global_parameters(file_global_param)
    #~~~~Read Cell parameters file
    ar_cell_label, ar_coorx, \
    ar_coory, ar_lambda, \
    ar_Xc, ar_dam, \
    ar_tan_beta, ar_tan_beta_channel, \
    ar_L0, ar_Ks0, \
    ar_theta_r, ar_theta_s, \
    ar_n_o0, ar_n_c0, \
    ar_cell_down, ar_pVs_t0, \
    ar_Vo_t0, ar_Qc_t0, \
    ar_kc, psi_b, lamda = pm.read_cell_parameters(file_cell_param)
    #~~~~Number of cell in the catchment
    nb_cell=len(ar_cell_label)
    #~~~~Computation of cell order
    ar_label_sort=pm.sort_cell(ar_cell_label,ar_cell_down)
    #~~~~Computation of upcells
    li_cell_up=pm.direct_up_cell(ar_cell_label,ar_cell_down,ar_label_sort)
    #~~~~Computation of drained area
    ar_A_drained=pm.drained_area(ar_label_sort,li_cell_up,X)
    #~~~~Modifies the values of the parameters
    ar_L=ar_L0*fac_L
    ar_Ks=ar_Ks0*fac_Ks
    ar_n_o=ar_n_o0*fac_n_o
    ar_n_c=ar_n_c0*fac_n_c
    #~~~~Computation of model parameters from physical parameters
    ar_Vsm, ar_b_s, ar_b_o, ar_W, ar_b_c\
      =pm.compute_cell_param(X,ar_Xc,Dt,alpha_s,alpha_o,alpha_c,nb_cell,\
                              A_thres,W_max,W_min,\
                              ar_lambda,ar_tan_beta,ar_tan_beta_channel,ar_L,\
                              ar_Ks,ar_theta_r,ar_theta_s,ar_n_o,ar_n_c,\
                              ar_A_drained)

    #Read of data from the outputs of TOPKAPI in hdf5 format
    ndar_Vs=np.array(ut.read_one_array_hdf(file_sim,'Soil','V_s'))

    #Assign the variables
    if variable==1:
        im_out=os.path.join(path_out, 'field_Vs_')
        tab=ndar_Vs
    elif variable==2:
        im_out=os.path.join(path_out, 'field_Vo_')
        tab=ndar_Vo
    elif variable==3:
        im_out=os.path.join(path_out, 'field_Vo_bin_')
        tab=ndar_Vo
        tab[tab>0]=1.
    elif variable==4:
        im_out=os.path.join(path_out, 'field_SSI_')
        tab=ndar_Vs/ar_Vsm*100.

    # Plot the maps
    for t in np.arange(int(t1),int(t2+1)):
        print('Map time-step ', t)
        image_out=im_out+ut.string(t,len(str(t2)))+'.png'
        field_map_ndar(tab,t,ar_coorx,ar_coory,X,image_out,variable)
Ejemplo n.º 4
0
def initial_pVs_Vo_Qc_from_simu(ini_file='initial_pVs_Vo_Qc_from_simu.ini'):
    """
    * Objective

     """
    ### READ THE INI FILE ###
    config.read(ini_file)
    print('Read the file ',ini_file)

    ##~~~~~~ file_in ~~~~~~##
    file_in=config.get('file_in','file_in')
    file_in_global=config.get('file_in','file_in_global')
    file_h5=config.get('file_in','file_h5')

    ##~~~~~~ file_out ~~~~~~##
    file_out=config.get('file_out','file_out')

    ##~~~~~~ variables ~~~~~~##
    time_step=config.getint('variables','time_step')
    fac_L_simu=config.getfloat('variables','fac_L_simu')
    fac_Ks_simu=config.getfloat('variables','fac_Ks_simu')
    fac_n_o_simu=config.getfloat('variables','fac_n_o_simu')
    fac_n_c_simu=config.getfloat('variables','fac_n_c_simu')

    ##~~~~~~ flags ~~~~~~##
    nb_param=config.getfloat('flags','nb_param')

    #--Read and compute the parameters to have the values of parameter and ar_Vsm
    #~~~~Read Global parameters file
    #~~~~Read Global parameters file
    X,Dt,alpha_s,alpha_o,alpha_c,A_thres,W_min,W_max\
      =pm.read_global_parameters(file_in_global)
    #~~~~Read Cell parameters file
    ar_cell_label,ar_coorx,ar_coory,ar_lambda,ar_Xc,ar_dam,ar_tan_beta,ar_tan_beta_channel,ar_L,ar_Ks,\
    ar_theta_r,ar_theta_s,ar_n_o,ar_n_c,\
    ar_cell_down,ar_pVs_t0,ar_Vo_t0,ar_Qc_t0,ar_kc\
        =pm.read_cell_parameters(file_in)
    #~~~~Number of cell in the catchment
    nb_cell=len(ar_cell_label)
    #~~~~Computation of cell order
    ar_label_sort=pm.sort_cell(ar_cell_label,ar_cell_down)
    #~~~~Computation of upcells
    li_cell_up=pm.direct_up_cell(ar_cell_label,ar_cell_down,ar_label_sort)
    #~~~~Computation of drained area
    ar_A_drained=pm.drained_area(ar_label_sort,li_cell_up,X)
    #~~~~Modifies the values of the parameters
    ar_L1=ar_L*fac_L_simu
    ar_Ks1=ar_Ks*fac_Ks_simu
    ar_n_o1=ar_n_o*fac_n_o_simu
    ar_n_c1=ar_n_c*fac_n_c_simu
    #~~~~Computation of model parameters from physical parameters
    ar_Vsm, ar_b_s, ar_b_o, ar_W, ar_b_c\
      =pm.compute_cell_param(X,ar_Xc,Dt,alpha_s,alpha_o,alpha_c,nb_cell,\
                              A_thres,W_max,W_min,\
                              ar_lambda,ar_tan_beta,ar_tan_beta_channel,ar_L1,\
                              ar_Ks1,ar_theta_r,ar_theta_s,ar_n_o1,ar_n_c1,\
                              ar_A_drained)

    #Read the soil volume file
    ndar_Vs=np.array(ut.read_one_array_hdf(file_h5,'Soil','V_s'))
    #Read the overland volume file
    ndar_Vo=np.array(ut.read_one_array_hdf(file_h5,'Overland','V_o'))
    #Read the channel dischargefile
    ndar_Qc=np.array(ut.read_one_array_hdf(file_h5,'Channel','Qc_out'))

    ar_Vs=ndar_Vs[time_step,:]
    ar_pVs_t0=ar_Vs/ar_Vsm*100.
    ar_Vo_t0=ndar_Vo[time_step,:]
    ar_Qc_t0=ndar_Qc[time_step,:]


    #~~~~~~Write parameter file~~~~~~#
    tab_param=np.zeros((len(ar_cell_label),nb_param))

    tab_param[:,0]=ar_cell_label
    tab_param[:,1]=ar_coorx
    tab_param[:,2]=ar_coory
    tab_param[:,3]=ar_lambda
    tab_param[:,4]=ar_Xc
    tab_param[:,5]=ar_dam
    tab_param[:,6]=ar_tan_beta
    tab_param[:,7]=ar_tan_beta_channel
    tab_param[:,8]=ar_L
    tab_param[:,9]=ar_Ks
    tab_param[:,10]=ar_theta_r
    tab_param[:,11]=ar_theta_s
    tab_param[:,12]=ar_n_o
    tab_param[:,13]=ar_n_c
    tab_param[:,14]=ar_cell_down
    tab_param[:,15]=ar_pVs_t0
    tab_param[:,16]=ar_Vo_t0
    tab_param[:,17]=ar_Qc_t0
    tab_param[:,18]=ar_kc

    np.savetxt(file_out, tab_param)
Ejemplo n.º 5
0
def zero_slope_management(ini_file='zero_slope_management.ini'):
    """Fix zero slopes

    Correct the cell and channel slopes to avoid situations with zero
    slopes. Water cannot drain downstream in these cases and the ODE's
    fail.

    Parameters
    ----------
    ini_file : string
        The name and full path to an ini file containing the required
        configuration.

    Returns
    -------
    Nothing

    """

    ##================================##
    ##  Read the input file (*.ini)   ##
    ##================================##
    config.read(ini_file)
    print('Read the file ',ini_file)
    ##~~~~~~~~~~~ input files ~~~~~~~~~~~##
    #Param
    file_cell_param=config.get('input_files','file_cell_param')
    file_cell_param_out=config.get('output_files','file_cell_param_out')
    ##~~~~~~ numerical_values ~~~~~~##
    nb_param=config.getfloat('numerical_values','nb_param')
    X=config.getfloat('numerical_values','X')

    ##============================##
    ## Pretreatment of input data ##
    ##============================##
    print('Pretreatment of input data')
    #~~~~Read Cell parameters file
    ar_cell_label, ar_coorx, ar_coory, \
    ar_lambda, ar_Xc, ar_dam, ar_tan_beta, \
    ar_tan_beta_channel, ar_L, ar_Ks, ar_theta_r, \
    ar_theta_s, ar_n_o, ar_n_c, ar_cell_down, ar_pVs_t0, ar_Vo_t0, \
    ar_Qc_t0, ar_kc, psi_b, lamda = pm.read_cell_parameters(file_cell_param)

    #~~~~Number of cell in the catchment
    nb_cell=len(ar_cell_label)
    #~~~~Computation of cell order
    ar_label_sort=pm.sort_cell(ar_cell_label,ar_cell_down)

    print('Treatment of cell slopes')
    #Adjust the outlet slope if equal to zero
    if ar_tan_beta[ar_label_sort[-1]]==0:
        ar_tan_beta[ar_label_sort[-1]]=1./X

    for cell1 in ar_label_sort:
        print(cell1)
        cell=np.where(ar_cell_label==cell1)[0][0]

        li_label_path=[cell]
        li_slope_path=[ar_tan_beta[cell]]

        down_cell=ar_cell_down[cell]

        # Extract the arrays of (i) all cells in the path
        # (ar_label_path) (ii) all corresponding slopes
        # (ar_slope_path)
        while down_cell>-1:
            li_label_path.append(down_cell)
            li_slope_path.append(ar_tan_beta[down_cell])
            down_cell=ar_cell_down[down_cell]
        ar_label_path=np.array(li_label_path)
        ar_slope_path=np.array(li_slope_path)

        # Go into the slope vector to detect the series of zero slopes
        # The method first consists of creating an array containing
        # the index of begining and end of zero series

        # Example: ar_slope_path=np.array([1, 2, 0, 0, 1, 0, 1, 0, 0, 0, 0])
        #--> ar_ind_start=array([2,5,7])
        #--> ar_ind_end=array([4,6,-1])
        li_ind_start=[]
        li_ind_end=[]
        #print(len(ar_slope_path), np.where(ar_slope_path==0.))
        for i in np.arange(len(ar_slope_path)):
            if i==0:
                if ar_slope_path[i]==0.:
                    li_ind_start.append(i)
            elif i==np.arange(len(ar_slope_path))[-1]:
                if ar_slope_path[i]==0. and ar_slope_path[i-1]==0.:
                    li_ind_end.append(-1)
                if ar_slope_path[i]!=0. and ar_slope_path[i-1]==0.:
                    li_ind_end.append(i)
            else:
                if ar_slope_path[i]==0. and ar_slope_path[i-1]!=0:
                    li_ind_start.append(i)
                if ar_slope_path[i]!=0. and ar_slope_path[i-1]==0.:
                    li_ind_end.append(i)
        ar_ind_start=np.array(li_ind_start,int)
        ar_ind_end=np.array(li_ind_end,int)
        if len(ar_ind_start)-len(ar_ind_end)!=0:
            print('problem length')
            print('ar_ind_start:',len(ar_ind_start),'ar_ind_end:',len(ar_ind_end))
            print(ar_ind_start)
            print(ar_ind_end)
            stop
        a=ar_ind_end-ar_ind_start
        if len(np.where(a<0)[0])!=0:
            print('problem index')
            stop

        #Then the slope are changed according to the defined index arrays
        if len(ar_ind_start)>0:
            print('Number of sections with zero slopes:',len(ar_ind_start))
        for i in np.arange(len(ar_ind_start)):
            #Compute the length of the zero path
            if ar_ind_end[i]!=-1:
                length_path=ar_ind_end[i]-ar_ind_start[i]
            else:
                length_path=len(ar_slope_path)-ar_ind_start[i]
            #Compute the corresponding slope
            slope=1./(length_path*X)
            #Replace the values of slope in the initial ar_tan_beta
            #select the cell labels
            ar_label_cell_zero=ar_label_path[ar_ind_start[i]:ar_ind_end[i]]
            #select the cell index (if different from the label)
            ar_ind_cell_zero=np.array(ar_label_cell_zero,int)
            n=-1
            for label in ar_label_cell_zero:
                n=n+1
                ind=np.where(ar_cell_label==label)[0][0]
                ar_ind_cell_zero[n]=ind

            #Change the values
            ar_tan_beta[ar_ind_cell_zero]=slope

    print('Treatment of channel slopes')
    #Adjust the outlet slope if equal to zero
    if ar_tan_beta_channel[ar_label_sort[-1]]==0:
        ar_tan_beta_channel[ar_label_sort[-1]]=1./X

    for cell1 in ar_label_sort:
        print(cell1)
        cell=np.where(ar_cell_label==cell1)[0][0]

        li_label_path=[cell]
        li_slope_path=[ar_tan_beta_channel[cell]]

        down_cell=ar_cell_down[cell]

        # Extract the arrays of (i) all cells in the path
        # (ar_label_path) (ii) all corresponding slopes
        # (ar_slope_path)
        while down_cell>-1:
            li_label_path.append(down_cell)
            li_slope_path.append(ar_tan_beta_channel[down_cell])
            down_cell=ar_cell_down[down_cell]
        ar_label_path=np.array(li_label_path)
        ar_slope_path=np.array(li_slope_path)

        # Go into the slope vector to detect the series of zero slopes
        # The method first consists of creating an array containing
        # the index of begining and end of zero series

        # Example: ar_slope_path=np.array([1, 2, 0, 0, 1, 0, 1, 0, 0, 0, 0])
        #--> ar_ind_start=array([2,5,7])
        #--> ar_ind_end=array([4,6,-1])
        li_ind_start=[]
        li_ind_end=[]
        #print(len(ar_slope_path), np.where(ar_slope_path==0.))
        for i in np.arange(len(ar_slope_path)):
            if i==0:
                if ar_slope_path[i]==0.:
                    li_ind_start.append(i)
            elif i==np.arange(len(ar_slope_path))[-1]:
                if ar_slope_path[i]==0. and ar_slope_path[i-1]==0.:
                    li_ind_end.append(-1)
                if ar_slope_path[i]!=0. and ar_slope_path[i-1]==0.:
                    li_ind_end.append(i)
            else:
                if ar_slope_path[i]==0. and ar_slope_path[i-1]!=0:
                    li_ind_start.append(i)
                if ar_slope_path[i]!=0. and ar_slope_path[i-1]==0.:
                    li_ind_end.append(i)
        ar_ind_start=np.array(li_ind_start,int)
        ar_ind_end=np.array(li_ind_end,int)
        if len(ar_ind_start)-len(ar_ind_end)!=0:
            print('problem length')
            print('ar_ind_start:',len(ar_ind_start),'ar_ind_end:',len(ar_ind_end))
            print(ar_ind_start)
            print(ar_ind_end)
            stop
        a=ar_ind_end-ar_ind_start
        if len(np.where(a<0)[0])!=0:
            print('problem index')
            stop

        # Then the slope are changed according to the defined index
        # arrays
        if len(ar_ind_start)>0:
            print('Number of sections with zero slopes:',len(ar_ind_start))
        for i in np.arange(len(ar_ind_start)):
            # Compute the length of the zero path
            if ar_ind_end[i]!=-1:
                length_path=ar_ind_end[i]-ar_ind_start[i]
            else:
                length_path=len(ar_slope_path)-ar_ind_start[i]
            # Compute the corresponding slope
            slope=1./(length_path*X)
            # Replace the values of slope in the initial
            # ar_tan_beta_channel select the cell labels
            ar_label_cell_zero=ar_label_path[ar_ind_start[i]:ar_ind_end[i]]
            # Select the cell index (if different from the label)
            ar_ind_cell_zero=np.array(ar_label_cell_zero,int)
            n=-1
            for label in ar_label_cell_zero:
                n=n+1
                ind=np.where(ar_cell_label==label)[0][0]
                ar_ind_cell_zero[n]=ind

            # Change the values
            ar_tan_beta_channel[ar_ind_cell_zero]=slope

    # Write parameter file
    param_table = np.zeros((len(ar_cell_label),nb_param))
    param_table[:,0] = ar_cell_label
    param_table[:,1] = ar_coorx
    param_table[:,2] = ar_coory
    param_table[:,3] = ar_lambda
    param_table[:,4] = ar_Xc
    param_table[:,5] = ar_dam
    param_table[:,6] = ar_tan_beta
    param_table[:,7] = ar_tan_beta_channel
    param_table[:,8] = ar_L
    param_table[:,9] = ar_Ks
    param_table[:,10] = ar_theta_r
    param_table[:,11] = ar_theta_s
    param_table[:,12] = ar_n_o
    param_table[:,13] = ar_n_c
    param_table[:,14] = ar_cell_down
    param_table[:,15] = ar_pVs_t0
    param_table[:,16] = ar_Vo_t0
    param_table[:,17] = ar_Qc_t0
    param_table[:,18] = ar_kc
    param_table[:,19] = psi_b
    param_table[:,20] = lamda

    format = '%d %f %f %d %f %d %f %f %f %f %f %f %f %f %d %f %f %f %f %f %f'
    np.savetxt(file_cell_param_out, param_table, fmt=format)
Ejemplo n.º 6
0
def mean_simuVsi(ini_file='mean_simuVsi.ini'):
    """
    * Objective

    """

    ### READ THE INI FILE ###
    config.read(ini_file)
    print 'Read the file ', ini_file

    ##~~~~~~ file_in ~~~~~~##
    file_in = config.get('file_in', 'file_in')
    file_in_global = config.get('file_in', 'file_in_global')
    file_h5 = config.get('file_in', 'file_h5')

    ##~~~~~~ file_out ~~~~~~##
    file_out = config.get('file_out', 'file_out')

    ##~~~~~~ variables ~~~~~~##
    mean_pVs_t0 = config.getfloat('variables', 'mean_pVs_t0')
    fac_L_simu = config.getfloat('variables', 'fac_L_simu')
    fac_Ks_simu = config.getfloat('variables', 'fac_Ks_simu')
    fac_n_o_simu = config.getfloat('variables', 'fac_n_o_simu')
    fac_n_c_simu = config.getfloat('variables', 'fac_n_c_simu')

    ##~~~~~~ flags ~~~~~~##
    nb_param = config.getfloat('flags', 'nb_param')

    #--Read and compute the parameters to have the values of parameter and ar_Vsm
    #~~~~Read Global parameters file
    print 'Pretreatment of input data'
    #~~~~Read Global parameters file
    X,Dt,alpha_s,alpha_o,alpha_c,A_thres,W_min,W_max\
      =pm.read_global_parameters(file_in_global)
    #~~~~Read Cell parameters file
    ar_cell_label,ar_coorx,ar_coory,ar_lambda,ar_Xc,ar_dam,ar_tan_beta,ar_tan_beta_channel,ar_L,ar_Ks,\
    ar_theta_r,ar_theta_s,ar_n_o,ar_n_c,\
    ar_cell_down,ar_pVs_t0,ar_Vo_t0,ar_Qc_t0,ar_kc\
        =pm.read_cell_parameters(file_in)
    #~~~~Number of cell in the catchment
    nb_cell = len(ar_cell_label)
    #~~~~Computation of cell order
    ar_label_sort = pm.sort_cell(ar_cell_label, ar_cell_down)
    #~~~~Computation of upcells
    li_cell_up = pm.direct_up_cell(ar_cell_label, ar_cell_down, ar_label_sort)
    #~~~~Computation of drained area
    ar_A_drained = pm.drained_area(ar_label_sort, li_cell_up, X)
    #~~~~Modifies the values of the parameters
    ar_L1 = ar_L * fac_L_simu
    ar_Ks1 = ar_Ks * fac_Ks_simu
    ar_n_o1 = ar_n_o * fac_n_o_simu
    ar_n_c1 = ar_n_c * fac_n_c_simu
    #~~~~Computation of model parameters from physical parameters
    ar_Vsm, ar_b_s, ar_b_o, ar_W, ar_b_c\
      =pm.compute_cell_param(X,ar_Xc,Dt,alpha_s,alpha_o,alpha_c,nb_cell,\
                              A_thres,W_max,W_min,\
                              ar_lambda,ar_tan_beta,ar_tan_beta_channel,ar_L1,\
                              ar_Ks1,ar_theta_r,ar_theta_s,ar_n_o1,ar_n_c1,\
                              ar_A_drained)

    #Read the soil volume file
    ndar_Vs = np.array(ut.read_one_array_hdf(file_h5, '/Soil/', 'V_s'))

    #Read the file of catchment saturation rates
    ndar_Vs_sat = ndar_Vs / ar_Vsm * 100.
    tab_rate = np.average(ndar_Vs_sat, axis=1)
    tab_rate_sort = np.sort(tab_rate)
    indice_sort = np.argsort(tab_rate)

    #Look for the rate closest to the expected mean value
    if mean_pVs_t0 < tab_rate_sort[0]:
        ind = 0
        print 'mean_pVs_t0 expected:', mean_pVs_t0, 'effective:', tab_rate_sort[
            0]
    elif mean_pVs_t0 > tab_rate_sort[-1]:
        ind = -1
        print 'mean_pVs_t0 expected:', mean_pVs_t0, ' effective', tab_rate_sort[
            -1]
    else:
        loop = True
        i = -1
        while loop:
            i = i + 1
            if mean_pVs_t0 >= tab_rate_sort[i] and mean_pVs_t0 < tab_rate_sort[
                    i + 1]:
                ind = i
                loop = False
                print 'mean_pVs_t0 expected:', mean_pVs_t0, ' effective:', tab_rate_sort[
                    i]

    ind_end = indice_sort[ind]
    print ind, ind_end
    ar_Vs = ndar_Vs[ind_end, :]
    ar_Vsi = ar_Vs / ar_Vsm * 100.
    print ar_Vsi
    ar_pVs_t0 = ar_Vsi

    #~~~~~~Write parameter file~~~~~~#
    tab_param = np.zeros((len(ar_cell_label), nb_param))

    tab_param[:, 0] = ar_cell_label
    tab_param[:, 1] = ar_coorx
    tab_param[:, 2] = ar_coory
    tab_param[:, 3] = ar_lambda
    tab_param[:, 4] = ar_Xc
    tab_param[:, 5] = ar_dam
    tab_param[:, 6] = ar_tan_beta
    tab_param[:, 7] = ar_tan_beta_channel
    tab_param[:, 8] = ar_L
    tab_param[:, 9] = ar_Ks
    tab_param[:, 10] = ar_theta_r
    tab_param[:, 11] = ar_theta_s
    tab_param[:, 12] = ar_n_o
    tab_param[:, 13] = ar_n_c
    tab_param[:, 14] = ar_cell_down
    tab_param[:, 15] = ar_pVs_t0
    tab_param[:, 16] = ar_Vo_t0
    tab_param[:, 17] = ar_Qc_t0
    tab_param[:, 18] = ar_kc

    np.savetxt(file_out, tab_param)
Ejemplo n.º 7
0
def initial_pVs_Vo_Qc_from_simu(ini_file='initial_pVs_Vo_Qc_from_simu.ini'):
    """
    * Objective

     """
    ### READ THE INI FILE ###
    config.read(ini_file)
    print 'Read the file ', ini_file

    ##~~~~~~ file_in ~~~~~~##
    file_in = config.get('file_in', 'file_in')
    file_in_global = config.get('file_in', 'file_in_global')
    file_h5 = config.get('file_in', 'file_h5')

    ##~~~~~~ file_out ~~~~~~##
    file_out = config.get('file_out', 'file_out')

    ##~~~~~~ variables ~~~~~~##
    time_step = config.getint('variables', 'time_step')
    fac_L_simu = config.getfloat('variables', 'fac_L_simu')
    fac_Ks_simu = config.getfloat('variables', 'fac_Ks_simu')
    fac_n_o_simu = config.getfloat('variables', 'fac_n_o_simu')
    fac_n_c_simu = config.getfloat('variables', 'fac_n_c_simu')

    ##~~~~~~ flags ~~~~~~##
    nb_param = config.getfloat('flags', 'nb_param')

    #--Read and compute the parameters to have the values of parameter and ar_Vsm
    #~~~~Read Global parameters file
    #~~~~Read Global parameters file
    X,Dt,alpha_s,alpha_o,alpha_c,A_thres,W_min,W_max\
      =pm.read_global_parameters(file_in_global)
    #~~~~Read Cell parameters file
    ar_cell_label,ar_coorx,ar_coory,ar_lambda,ar_Xc,ar_dam,ar_tan_beta,ar_tan_beta_channel,ar_L,ar_Ks,\
    ar_theta_r,ar_theta_s,ar_n_o,ar_n_c,\
    ar_cell_down,ar_pVs_t0,ar_Vo_t0,ar_Qc_t0,ar_kc\
        =pm.read_cell_parameters(file_in)
    #~~~~Number of cell in the catchment
    nb_cell = len(ar_cell_label)
    #~~~~Computation of cell order
    ar_label_sort = pm.sort_cell(ar_cell_label, ar_cell_down)
    #~~~~Computation of upcells
    li_cell_up = pm.direct_up_cell(ar_cell_label, ar_cell_down, ar_label_sort)
    #~~~~Computation of drained area
    ar_A_drained = pm.drained_area(ar_label_sort, li_cell_up, X)
    #~~~~Modifies the values of the parameters
    ar_L1 = ar_L * fac_L_simu
    ar_Ks1 = ar_Ks * fac_Ks_simu
    ar_n_o1 = ar_n_o * fac_n_o_simu
    ar_n_c1 = ar_n_c * fac_n_c_simu
    #~~~~Computation of model parameters from physical parameters
    ar_Vsm, ar_b_s, ar_b_o, ar_W, ar_b_c\
      =pm.compute_cell_param(X,ar_Xc,Dt,alpha_s,alpha_o,alpha_c,nb_cell,\
                              A_thres,W_max,W_min,\
                              ar_lambda,ar_tan_beta,ar_tan_beta_channel,ar_L1,\
                              ar_Ks1,ar_theta_r,ar_theta_s,ar_n_o1,ar_n_c1,\
                              ar_A_drained)

    #Read the soil volume file
    ndar_Vs = np.array(ut.read_one_array_hdf(file_h5, '/Soil/', 'V_s'))
    #Read the overland volume file
    ndar_Vo = np.array(ut.read_one_array_hdf(file_h5, '/Overland/', 'V_o'))
    #Read the channel dischargefile
    ndar_Qc = np.array(ut.read_one_array_hdf(file_h5, '/Channel/', 'Qc_out'))

    ar_Vs = ndar_Vs[time_step, :]
    ar_pVs_t0 = ar_Vs / ar_Vsm * 100.
    ar_Vo_t0 = ndar_Vo[time_step, :]
    ar_Qc_t0 = ndar_Qc[time_step, :]

    #~~~~~~Write parameter file~~~~~~#
    tab_param = np.zeros((len(ar_cell_label), nb_param))

    tab_param[:, 0] = ar_cell_label
    tab_param[:, 1] = ar_coorx
    tab_param[:, 2] = ar_coory
    tab_param[:, 3] = ar_lambda
    tab_param[:, 4] = ar_Xc
    tab_param[:, 5] = ar_dam
    tab_param[:, 6] = ar_tan_beta
    tab_param[:, 7] = ar_tan_beta_channel
    tab_param[:, 8] = ar_L
    tab_param[:, 9] = ar_Ks
    tab_param[:, 10] = ar_theta_r
    tab_param[:, 11] = ar_theta_s
    tab_param[:, 12] = ar_n_o
    tab_param[:, 13] = ar_n_c
    tab_param[:, 14] = ar_cell_down
    tab_param[:, 15] = ar_pVs_t0
    tab_param[:, 16] = ar_Vo_t0
    tab_param[:, 17] = ar_Qc_t0
    tab_param[:, 18] = ar_kc

    np.savetxt(file_out, tab_param)
Ejemplo n.º 8
0
def zero_slope_management(ini_file='zero_slope_management.ini'):
    """Fix zero slopes

    Correct the cell and channel slopes to avoid situations with zero
    slopes. Water cannot drain downstream in these cases and the ODE's
    fail.

    Parameters
    ----------
    ini_file : string
        The name and full path to an ini file containing the required
        configuration.

    Returns
    -------
    Nothing

    """

    ##================================##
    ##  Read the input file (*.ini)   ##
    ##================================##
    config.read(ini_file)
    print 'Read the file ', ini_file
    ##~~~~~~~~~~~ input files ~~~~~~~~~~~##
    #Param
    file_cell_param = config.get('input_files', 'file_cell_param')
    file_cell_param_out = config.get('output_files', 'file_cell_param_out')
    ##~~~~~~ numerical_values ~~~~~~##
    nb_param = config.getfloat('numerical_values', 'nb_param')
    X = config.getfloat('numerical_values', 'X')

    ##============================##
    ## Pretreatment of input data ##
    ##============================##
    print 'Pretreatment of input data'
    #~~~~Read Cell parameters file
    ar_cell_label, ar_coorx, ar_coory, \
    ar_lambda, ar_Xc, ar_dam, ar_tan_beta, \
    ar_tan_beta_channel, ar_L, ar_Ks, ar_theta_r, \
    ar_theta_s, ar_n_o, ar_n_c, ar_cell_down, ar_pVs_t0, ar_Vo_t0, \
    ar_Qc_t0, ar_kc, psi_b, lamda = pm.read_cell_parameters(file_cell_param)

    #~~~~Number of cell in the catchment
    nb_cell = len(ar_cell_label)
    #~~~~Computation of cell order
    ar_label_sort = pm.sort_cell(ar_cell_label, ar_cell_down)

    print 'Treatment of cell slopes'
    #Adjust the outlet slope if equal to zero
    if ar_tan_beta[ar_label_sort[-1]] == 0:
        ar_tan_beta[ar_label_sort[-1]] = 1. / X

    for cell1 in ar_label_sort:
        print cell1
        cell = np.where(ar_cell_label == cell1)[0][0]

        li_label_path = [cell]
        li_slope_path = [ar_tan_beta[cell]]

        down_cell = ar_cell_down[cell]

        # Extract the arrays of (i) all cells in the path
        # (ar_label_path) (ii) all corresponding slopes
        # (ar_slope_path)
        while down_cell > -1:
            li_label_path.append(down_cell)
            li_slope_path.append(ar_tan_beta[down_cell])
            down_cell = ar_cell_down[down_cell]
        ar_label_path = np.array(li_label_path)
        ar_slope_path = np.array(li_slope_path)

        # Go into the slope vector to detect the series of zero slopes
        # The method first consists of creating an array containing
        # the index of begining and end of zero series

        # Example: ar_slope_path=np.array([1, 2, 0, 0, 1, 0, 1, 0, 0, 0, 0])
        #--> ar_ind_start=array([2,5,7])
        #--> ar_ind_end=array([4,6,-1])
        li_ind_start = []
        li_ind_end = []
        #print len(ar_slope_path), np.where(ar_slope_path==0.)
        for i in np.arange(len(ar_slope_path)):
            if i == 0:
                if ar_slope_path[i] == 0.:
                    li_ind_start.append(i)
            elif i == np.arange(len(ar_slope_path))[-1]:
                if ar_slope_path[i] == 0. and ar_slope_path[i - 1] == 0.:
                    li_ind_end.append(-1)
                if ar_slope_path[i] != 0. and ar_slope_path[i - 1] == 0.:
                    li_ind_end.append(i)
            else:
                if ar_slope_path[i] == 0. and ar_slope_path[i - 1] != 0:
                    li_ind_start.append(i)
                if ar_slope_path[i] != 0. and ar_slope_path[i - 1] == 0.:
                    li_ind_end.append(i)
        ar_ind_start = np.array(li_ind_start, int)
        ar_ind_end = np.array(li_ind_end, int)
        if len(ar_ind_start) - len(ar_ind_end) != 0:
            print 'problem length'
            print 'ar_ind_start:', len(ar_ind_start), 'ar_ind_end:', len(
                ar_ind_end)
            print ar_ind_start
            print ar_ind_end
            stop
        a = ar_ind_end - ar_ind_start
        if len(np.where(a < 0)[0]) != 0:
            print 'problem index'
            stop

        #Then the slope are changed according to the defined index arrays
        if len(ar_ind_start) > 0:
            print 'Number of sections with zero slopes:', len(ar_ind_start)
        for i in np.arange(len(ar_ind_start)):
            #Compute the length of the zero path
            if ar_ind_end[i] != -1:
                length_path = ar_ind_end[i] - ar_ind_start[i]
            else:
                length_path = len(ar_slope_path) - ar_ind_start[i]
            #Compute the corresponding slope
            slope = 1. / (length_path * X)
            #Replace the values of slope in the initial ar_tan_beta
            #select the cell labels
            ar_label_cell_zero = ar_label_path[ar_ind_start[i]:ar_ind_end[i]]
            #select the cell index (if different from the label)
            ar_ind_cell_zero = np.array(ar_label_cell_zero, int)
            n = -1
            for label in ar_label_cell_zero:
                n = n + 1
                ind = np.where(ar_cell_label == label)[0][0]
                ar_ind_cell_zero[n] = ind

            #Change the values
            ar_tan_beta[ar_ind_cell_zero] = slope

    print 'Treatment of channel slopes'
    #Adjust the outlet slope if equal to zero
    if ar_tan_beta_channel[ar_label_sort[-1]] == 0:
        ar_tan_beta_channel[ar_label_sort[-1]] = 1. / X

    for cell1 in ar_label_sort:
        print cell1
        cell = np.where(ar_cell_label == cell1)[0][0]

        li_label_path = [cell]
        li_slope_path = [ar_tan_beta_channel[cell]]

        down_cell = ar_cell_down[cell]

        # Extract the arrays of (i) all cells in the path
        # (ar_label_path) (ii) all corresponding slopes
        # (ar_slope_path)
        while down_cell > -1:
            li_label_path.append(down_cell)
            li_slope_path.append(ar_tan_beta_channel[down_cell])
            down_cell = ar_cell_down[down_cell]
        ar_label_path = np.array(li_label_path)
        ar_slope_path = np.array(li_slope_path)

        # Go into the slope vector to detect the series of zero slopes
        # The method first consists of creating an array containing
        # the index of begining and end of zero series

        # Example: ar_slope_path=np.array([1, 2, 0, 0, 1, 0, 1, 0, 0, 0, 0])
        #--> ar_ind_start=array([2,5,7])
        #--> ar_ind_end=array([4,6,-1])
        li_ind_start = []
        li_ind_end = []
        #print len(ar_slope_path), np.where(ar_slope_path==0.)
        for i in np.arange(len(ar_slope_path)):
            if i == 0:
                if ar_slope_path[i] == 0.:
                    li_ind_start.append(i)
            elif i == np.arange(len(ar_slope_path))[-1]:
                if ar_slope_path[i] == 0. and ar_slope_path[i - 1] == 0.:
                    li_ind_end.append(-1)
                if ar_slope_path[i] != 0. and ar_slope_path[i - 1] == 0.:
                    li_ind_end.append(i)
            else:
                if ar_slope_path[i] == 0. and ar_slope_path[i - 1] != 0:
                    li_ind_start.append(i)
                if ar_slope_path[i] != 0. and ar_slope_path[i - 1] == 0.:
                    li_ind_end.append(i)
        ar_ind_start = np.array(li_ind_start, int)
        ar_ind_end = np.array(li_ind_end, int)
        if len(ar_ind_start) - len(ar_ind_end) != 0:
            print 'problem length'
            print 'ar_ind_start:', len(ar_ind_start), 'ar_ind_end:', len(
                ar_ind_end)
            print ar_ind_start
            print ar_ind_end
            stop
        a = ar_ind_end - ar_ind_start
        if len(np.where(a < 0)[0]) != 0:
            print 'problem index'
            stop

        # Then the slope are changed according to the defined index
        # arrays
        if len(ar_ind_start) > 0:
            print 'Number of sections with zero slopes:', len(ar_ind_start)
        for i in np.arange(len(ar_ind_start)):
            # Compute the length of the zero path
            if ar_ind_end[i] != -1:
                length_path = ar_ind_end[i] - ar_ind_start[i]
            else:
                length_path = len(ar_slope_path) - ar_ind_start[i]
            # Compute the corresponding slope
            slope = 1. / (length_path * X)
            # Replace the values of slope in the initial
            # ar_tan_beta_channel select the cell labels
            ar_label_cell_zero = ar_label_path[ar_ind_start[i]:ar_ind_end[i]]
            # Select the cell index (if different from the label)
            ar_ind_cell_zero = np.array(ar_label_cell_zero, int)
            n = -1
            for label in ar_label_cell_zero:
                n = n + 1
                ind = np.where(ar_cell_label == label)[0][0]
                ar_ind_cell_zero[n] = ind

            # Change the values
            ar_tan_beta_channel[ar_ind_cell_zero] = slope

    # Write parameter file
    param_table = np.zeros((len(ar_cell_label), nb_param))
    param_table[:, 0] = ar_cell_label
    param_table[:, 1] = ar_coorx
    param_table[:, 2] = ar_coory
    param_table[:, 3] = ar_lambda
    param_table[:, 4] = ar_Xc
    param_table[:, 5] = ar_dam
    param_table[:, 6] = ar_tan_beta
    param_table[:, 7] = ar_tan_beta_channel
    param_table[:, 8] = ar_L
    param_table[:, 9] = ar_Ks
    param_table[:, 10] = ar_theta_r
    param_table[:, 11] = ar_theta_s
    param_table[:, 12] = ar_n_o
    param_table[:, 13] = ar_n_c
    param_table[:, 14] = ar_cell_down
    param_table[:, 15] = ar_pVs_t0
    param_table[:, 16] = ar_Vo_t0
    param_table[:, 17] = ar_Qc_t0
    param_table[:, 18] = ar_kc
    param_table[:, 19] = psi_b
    param_table[:, 20] = lamda

    format = '%d %f %f %d %f %d %f %f %f %f %f %f %f %f %d %f %f %f %f %f %f'
    np.savetxt(file_cell_param_out, param_table, fmt=format)