Ejemplo n.º 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)
Ejemplo n.º 2
0
    def SaveParameters(self, Path):
        """
        SaveParameters method 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 type(Path) == str, "path should be of type string"
        assert os.path.exists(Path), Path + " you have provided does not exist"

        # save
        if self.Snow == 0: # now snow subroutine
            pnme=["01_rfcf","02_FC", "03_BETA", "04_ETF", "05_LP", "06_K0","07_K1",
                  "08_K2","09_UZL","10_PERC", "11_Kmuskingum", "12_Xmuskingum"]
        else: # there is snow subtoutine
            pnme=["01_ltt", "02_rfcf", "03_sfcf", "04_cfmax", "05_cwh", "06_cfr",
                  "07_fc", "08_beta","09_etf","10_lp", "11_k0", "12_k1", "13_k2",
                  "14_uzl", "18_perc"]

        if Path != None:
            pnme = [Path+i+"_"+str(dt.datetime.now())[0:10]+".tif" for i in pnme]

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