コード例 #1
0
    def Download(self, Waitbar=1, cores=None):
        """
        This function downloads CHIRPS daily or monthly data

        Keyword arguments:

        Waitbar -- 1 (Default) will print a waitbar
        cores -- The number of cores used to run the routine. It can be 'False'
                 to avoid using parallel computing routines.
        """

        # Pass variables to parallel function and run
        args = [self.output_folder, self.Time, self.xID, self.yID, self.lonlim, self.latlim]

        if not cores:
            # Create Waitbar
            if Waitbar == 1:
                total_amount = len(self.Dates)
                amount = 0
                weirdFn.printWaitBar(amount, total_amount, prefix = 'Progress:', suffix = 'Complete', length = 50)

            for Date in self.Dates:
                CHIRPS.RetrieveData(Date, args)
                if Waitbar == 1:
                    amount = amount + 1
                    weirdFn.printWaitBar(amount, total_amount, prefix = 'Progress:', suffix = 'Complete', length = 50)
            results = True
        else:
            results = Parallel(n_jobs=cores)(delayed(CHIRPS.RetrieveData)(Date, args)
                                             for Date in self.Dates)
        return results
コード例 #2
0
ファイル: remotesensing.py プロジェクト: nguyetlm/Hapi
    def Download(self, Waitbar=1, cores=None):
        """
        ============================================================
            Download(self, Waitbar=1, cores=None)
        ============================================================
        Download method downloads CHIRPS data

        Parameters
        ----------
        Waitbar : TYPE, optional
            will print a waitbar. The default is 1.
        cores : TYPE, optional
            The number of cores used to run the routine. It can be 'False'
                 to avoid using parallel computing routines. The default is None.

        Returns
        -------
        results : TYPE
            DESCRIPTION.

        """
        # Pass variables to parallel function and run
        args = [
            self.output_folder, self.Time, self.xID, self.yID, self.lonlim,
            self.latlim
        ]

        if not cores:
            # Create Waitbar
            if Waitbar == 1:
                total_amount = len(self.Dates)
                amount = 0
                weirdFn.printWaitBar(amount,
                                     total_amount,
                                     prefix='Progress:',
                                     suffix='Complete',
                                     length=50)

            for Date in self.Dates:
                CHIRPS.RetrieveData(Date, args)
                if Waitbar == 1:
                    amount = amount + 1
                    weirdFn.printWaitBar(amount,
                                         total_amount,
                                         prefix='Progress:',
                                         suffix='Complete',
                                         length=50)
            results = True
        else:
            results = Parallel(n_jobs=cores)(
                delayed(CHIRPS.RetrieveData)(Date, args)
                for Date in self.Dates)
        return results
コード例 #3
0
    def DownloadData(self, Var, Waitbar):
        """
        This function downloads ECMWF six-hourly, daily or monthly data

        Keyword arguments:

        """

        # Load factors / unit / type of variables / accounts
        VarInfo = Variables(self.Time)
        Varname_dir = VarInfo.file_name[Var]

        # Create Out directory
        out_dir = os.path.join(self.Path, self.Time, Varname_dir)

        if not os.path.exists(out_dir):
              os.makedirs(out_dir)

        DownloadType = VarInfo.DownloadType[Var]


        if DownloadType == 1:
            string1 = 'oper'
            string4 = "0"
            string6 = "00:00:00/06:00:00/12:00:00/18:00:00"
            string2 = 'sfc'
            string8 = 'an'

        if DownloadType == 2:
            string1 = 'oper'
            string4 = "12"
            string6 = "00:00:00/12:00:00"
            string2 = 'sfc'
            string8 = 'fc'

        if DownloadType == 3:
            string1 = 'oper'
            string4 = "0"
            string6 = "00:00:00/06:00:00/12:00:00/18:00:00"
            string2 = 'pl'
            string8 = 'an'

        parameter_number = VarInfo.number_para[Var]

        string3 = '%03d.128' %(parameter_number)
        string5 = '0.125/0.125'
        string9 = 'ei'
        string10 = '%s/%s/%s/%s' %(self.latlim_corr[1], self.lonlim_corr[0],
                                   self.latlim_corr[0], self.lonlim_corr[1])   #N, W, S, E


        # Download data by using the ECMWF API
        print('Use API ECMWF to collect the data, please wait')
        RemoteSensing.API(self.Path, DownloadType, string1, string2, string3, string4,
                          string5, string6, self.string7, string8, string9, string10)


        # Open the downloaded data
        NC_filename = os.path.join(self.Path,'data_interim.nc')
        fh = Dataset(NC_filename, mode='r')

        # Get the NC variable parameter
        parameter_var = VarInfo.var_name[Var]
        Var_unit = VarInfo.units[Var]
        factors_add = VarInfo.factors_add[Var]
        factors_mul = VarInfo.factors_mul[Var]

        # Open the NC data
        Data = fh.variables[parameter_var][:]
        Data_time = fh.variables['time'][:]
        lons = fh.variables['longitude'][:]
        lats = fh.variables['latitude'][:]

        # Define the georeference information
        Geo_four = np.nanmax(lats)
        Geo_one = np.nanmin(lons)
        Geo_out = tuple([Geo_one, 0.125, 0.0, Geo_four, 0.0, -0.125])

        # Create Waitbar
        if Waitbar == 1:
            total_amount = len(self.Dates)
            amount = 0
            weirdFn.printWaitBar(amount, total_amount, prefix = 'Progress:', suffix = 'Complete', length = 50)

        for date in self.Dates:

            # Define the year, month and day
            year =  date.year
            month =  date.month
            day =  date.day

            # Hours since 1900-01-01
            start = dt.datetime(year=1900, month=1, day=1)
            end = dt.datetime(year, month, day)
            diff = end - start
            hours_from_start_begin = diff.total_seconds()/60/60

            Date_good = np.zeros(len(Data_time))

            if self.Time == 'daily':
                 days_later = 1
            if self.Time == 'monthly':
                 days_later = calendar.monthrange(year,month)[1]

            Date_good[np.logical_and(Data_time>=hours_from_start_begin, Data_time<(hours_from_start_begin + 24 * days_later))] = 1

            Data_one = np.zeros([int(np.sum(Date_good)),int(np.size(Data,1)),int(np.size(Data,2))])
            Data_one = Data[np.int_(Date_good) == 1, :, :]

            # Calculate the average temperature in celcius degrees
            Data_end = factors_mul * np.nanmean(Data_one,0) + factors_add

            if VarInfo.types[Var] == 'flux':
                Data_end = Data_end * days_later

            VarOutputname = VarInfo.file_name[Var]

            # Define the out name
            name_out = os.path.join(out_dir, "%s_ECMWF_ERA-Interim_%s_%s_%d.%02d.%02d.tif" %(VarOutputname, Var_unit, self.Time, year,month,day))

            # Create Tiff files
            # Raster.Save_as_tiff(name_out, Data_end, Geo_out, "WGS84")
            Raster.CreateRaster(Path=name_out, data=Data_end, geo=Geo_out, EPSG="WGS84")

            if Waitbar == 1:
                amount = amount + 1
                weirdFn.printWaitBar(amount, total_amount, prefix = 'Progress:', suffix = 'Complete', length = 50)


        fh.close()

        return()