Esempio n. 1
0
    def CreateLumpedInputs(Path):
        """
        =========================================================
             CreateLumpedInputs(Path)
        =========================================================
        CreateLumpedInputs method generate a lumped parameters from
        distributed parameters by taking the average
        Parameters
        ----------
        Path : [str]
            path to folder that contains the parameter rasters.

        Returns
        -------
        data : [array]
            array contains the average values of the distributed parameters.

        """
        # data type
        assert type(Path) == str, "PrecPath input should be string type"
        # check wether the path exists or not
        assert os.path.exists(Path), Path + " you have provided does not exist"
        # check wether the folder has the rasters or not
        assert len(
            os.listdir(Path)) > 0, Path + " folder you have provided is empty"
        # read data
        data = raster.ReadRastersFolder(Path)
        # get the No data value from the first raster in the folder
        _, NoDataValue = raster.GetRasterData(Input=Path + "/" +
                                              os.listdir(Path)[0])
        data[data == NoDataValue] = np.nan

        data = np.nanmean(data, axis=0)
        data = data.mean(0)

        return data
Esempio n. 2
0
    def RetrieveData(Date, args):
        """
        This function retrieves CHIRPS data for a given date from the
        ftp://chg-ftpout.geog.ucsb.edu server.
        https://data.chc.ucsb.edu/
        Keyword arguments:
        Date -- 'yyyy-mm-dd'
        args -- A list of parameters defined in the DownloadData function.
        """
        # Argument
        [output_folder, TimeCase, xID, yID, lonlim, latlim] = args

        # open ftp server
        # ftp = FTP("chg-ftpout.geog.ucsb.edu", "", "")
        ftp = FTP("data.chc.ucsb.edu")
        ftp.login()

    	# Define FTP path to directory
        if TimeCase == 'daily':
            pathFTP = 'pub/org/chg/products/CHIRPS-2.0/global_daily/tifs/p05/%s/' %Date.strftime('%Y')
        elif TimeCase == 'monthly':
            pathFTP = 'pub/org/chg/products/CHIRPS-2.0/global_monthly/tifs/'
        else:
            raise KeyError("The input time interval is not supported")

        # find the document name in this directory
        ftp.cwd(pathFTP)
        listing = []

    	# read all the file names in the directory
        ftp.retrlines("LIST", listing.append)

    	# create all the input name (filename) and output (outfilename, filetif, DiFileEnd) names
        if TimeCase == 'daily':
            filename = 'chirps-v2.0.%s.%02s.%02s.tif.gz' %(Date.strftime('%Y'), Date.strftime('%m'), Date.strftime('%d'))
            outfilename = os.path.join(output_folder,'chirps-v2.0.%s.%02s.%02s.tif' %(Date.strftime('%Y'), Date.strftime('%m'), Date.strftime('%d')))
            DirFileEnd = os.path.join(output_folder,'P_CHIRPS.v2.0_mm-day-1_daily_%s.%02s.%02s.tif' %(Date.strftime('%Y'), Date.strftime('%m'), Date.strftime('%d')))
        elif TimeCase == 'monthly':
            filename = 'chirps-v2.0.%s.%02s.tif.gz' %(Date.strftime('%Y'), Date.strftime('%m'))
            outfilename = os.path.join(output_folder,'chirps-v2.0.%s.%02s.tif' %(Date.strftime('%Y'), Date.strftime('%m')))
            DirFileEnd = os.path.join(output_folder,'P_CHIRPS.v2.0_mm-month-1_monthly_%s.%02s.%02s.tif' %(Date.strftime('%Y'), Date.strftime('%m'), Date.strftime('%d')))
        else:
            raise KeyError("The input time interval is not supported")

        # download the global rainfall file
        try:
            local_filename = os.path.join(output_folder, filename)
            lf = open(local_filename, "wb")
            ftp.retrbinary("RETR " + filename, lf.write, 8192)
            lf.close()

            # unzip the file
            zip_filename = os.path.join(output_folder, filename)
            Raster.ExtractFromGZ(zip_filename, outfilename, delete=True)

            # open tiff file
            dataset,NoDataValue = Raster.GetRasterData(outfilename)

            # clip dataset to the given extent
            data = dataset[yID[0]:yID[1], xID[0]:xID[1]]
            # replace -ve values with -9999
            data[data < 0] = -9999

            # save dataset as geotiff file
            geo = [lonlim[0], 0.05, 0, latlim[1], 0, -0.05]
            Raster.CreateRaster(Path=DirFileEnd, data=data, geo=geo, EPSG="WGS84",NoDataValue = NoDataValue)

            # delete old tif file
            os.remove(outfilename)

        except:
            print("file not exists")
        return True