예제 #1
0
def DeleteBasins(basins, pathout):
    """
    ===========================================================
         DeleteBasins(basins,pathout)
    ===========================================================
    this function deletes all the basins in a basin raster created when delineating
    a catchment and leave only the first basin which is the biggest basin in the raster
    
    Inputs:
    ----------
        1- basins:
            [gdal.dataset] raster you create during delineation of a catchment 
            values of its cells are the number of the basin it belongs to
        2- pathout:
            [String] path you want to save the resulted raster to it should include
            the extension ".tif"
    Outputs:
    ----------
        1- raster with only one basin (the basin that its name is 1 )
    
    Example:
    ----------
        basins=gdal.Open("Data/basins.tif")    
        pathout="mask.tif"
        DeleteBasins(basins,pathout)
    """
    # input data validation
    # data type
    assert type(pathout) == str, "A_path input should be string type"
    assert type(
        basins
    ) == gdal.Dataset, "basins raster should be read using gdal (gdal dataset please read it using gdal library) "

    # input values
    # check wether the user wrote the extension of the raster or not
    ext = pathout[-4:]
    assert ext == ".tif", "please add the extension at the end of the path input"

    # get number of rows
    rows = basins.RasterYSize
    # get number of columns
    cols = basins.RasterXSize
    # array
    basins_A = basins.ReadAsArray()
    # no data value
    no_val = np.float32(basins.GetRasterBand(1).GetNoDataValue())
    # get number of basins and there names
    basins_val = list(
        set([
            int(basins_A[i, j]) for i in range(rows) for j in range(cols)
            if basins_A[i, j] != no_val
        ]))

    # keep the first basin and delete the others by filling their cells by nodata value
    for i in range(rows):
        for j in range(cols):
            if basins_A[i, j] != no_val and basins_A[i, j] != basins_val[0]:
                basins_A[i, j] = no_val

    Raster.RasterLike(basins, basins_A, pathout)
예제 #2
0
def SaveParameters(DistParFn,
                   Raster,
                   Par,
                   No_parameters,
                   no_lumped_par,
                   lumped_par_pos,
                   snow,
                   kub,
                   klb,
                   Path=None):
    """
    ============================================================
        SaveParameters(DistParFn, Raster, Par, No_parameters, snow, kub, klb, Path=None)
    ============================================================
    this function takes generated parameters by the calibration algorithm, 
    distributed them with a given function and save them as a rasters
    
    Inputs:
    ----------
        1-DistParFn:
            [function] function to distribute the parameters (all functions are
            in Hapi.DistParameters )
        2-Raster:
            [gdal.dataset] raster to get the spatial information
        3-Par
            [list or numpy ndarray] parameters as 1D array or list
        4-no_parameters:
            [int] number of the parameters in the conceptual model
        5-snow:
            [integer] number to define whether to take parameters of 
            the conceptual model with snow subroutine or without
        5-kub:
            [numeric] upper bound for k parameter in muskingum function
        6-klb:
            [numeric] lower bound for k parameter in muskingum function
         7-Path:
             [string] path to the folder you want to save the parameters in
             default value is None (parameters are going to be saved in the
             current directory)
     
    Outputs:
    ----------
         Rasters for parameters of the distributed model
     
   Examples:     
   ----------
        DemPath = path+"GIS/4000/dem4000.tif"
        Raster=gdal.Open(DemPath)
        ParPath = "par15_7_2018.txt"
        par=np.loadtxt(ParPath)
        klb=0.5
        kub=1
        no_parameters=12
        DistParFn=DP.par3dLumped
        Path="parameters/"
        snow=0
        
        SaveParameters(DistParFn, Raster, par, no_parameters,snow ,kub, klb,Path)
    """
    assert callable(
        DistParFn), " please check the function to distribute your parameters"
    assert type(
        Raster
    ) == gdal.Dataset, "raster should be read using gdal (gdal dataset please read it using gdal library) "
    assert type(Par) == np.ndarray or type(
        Par) == list, "par_g should be of type 1d array or list"
    assert type(No_parameters) == int, "No of parameters should be integer"
    assert isinstance(kub, numbers.Number), " kub should be a number"
    assert isinstance(klb, numbers.Number), " klb should be a number"
    assert type(Path) == str, "path should be of type string"
    assert os.path.exists(Path), Path + " you have provided does not exist"

    par2d = DistParFn(Par, Raster, No_parameters, no_lumped_par,
                      lumped_par_pos, kub, klb)

    # save
    if snow == 0:  # now snow subroutine
        pnme = [
            "01_rfcf.tif", "02_FC.tif", "03_BETA.tif", "04_ETF.tif",
            "05_LP.tif", "06_CFLUX.tif", "07_K.tif", "08_K1.tif",
            "09_ALPHA.tif", "10_PERC.tif", "11_Kmuskingum.tif",
            "12_Xmuskingum.tif"
        ]
    else:  # there is snow subtoutine
        pnme = [
            "01_ltt.tif", "02_utt.tif", "03_rfcf.tif", "04_sfcf.tif",
            "05_ttm.tif", "06_cfmax.tif", "07_cwh.tif", "08_cfr.tif",
            "09_fc.tif", "10_fc.tif", "11_beta.tif", "12_etf.tif", "13_lp.tif",
            "14_cflux.tif", "15_k.tif", "16_k1.tif", "17_alpha.tif",
            "18_perc.tif"
        ]

    if Path != None:
        pnme = [Path + i for i in pnme]

    for i in range(np.shape(par2d)[2]):
        Raster.RasterLike(Raster, par2d[:, :, i], pnme[i])