예제 #1
0
def getDailyDataFromFTP(localPath, datesList=[]):
    dailyDir = 'pub/org/chg/products/CHIRPS-2.0/global_daily/tifs/p25/'
    chirpsBaseName = 'chirps-v2.0.'
    chirpsExt = '.tif.gz'
    # if have dates, only get those files
    if not datesList:
        # get all files not already in localPath
        getFilesFromFTP(ftp_address_CHIRPS, 'anonymous', 'anonymous@', dailyDir, localPath)
    else:
        sd = datesList[0]
        ed = datesList[1]
        ftp = openFTP(ftp_address_CHIRPS)

        while sd <= ed:
            yrs = '{0}'.format(sd.year)
            yrDir = path.join(dailyDir, yrs)
            fname = sd.strftime("chirps-v2.0.%Y.%m.%d.tif.gz")
            logger.debug('Looking for: %s in %s' % fname, yrDir)
            localFile = path.join(localPath, fname)
            # try to find file in directory
            fn = yrDir + '/' + fname
            # Download file
            getFileFromFTP(ftp, fn, localFile, False)
            sd = sd + timedelta(days=1)
        ftp.close()
    return 0
예제 #2
0
def downloadDailyDataFromFTP(localPath, dates=None):
    dailyDir = 'pub/org/chg/products/CHIRPS-2.0/global_daily/tifs/p25/'
    all_files = []
    if not dates:
        # get all files
        getFilesFromFTP(ftp_address_CHIRPS, 'anonymous', 'anonymous@', dailyDir, localPath)
    else:
        transferList = []
        _curYear = ""
        _first = True
        for d in dates:
            y = d.strftime("%Y")
            m = d.strftime("%m")
            dd = d.strftime("%d")
            if y != _curYear:
                if _first:
                    _first = False
                    _curYear = y
                else:
                    # different year, get all files currently in list
                    _curDir = "{0}{1}/".format(dailyDir, _curYear)
                    all_files.extend(getFilesFromFTP(ftp_address_CHIRPS, 'anonymous', 'anonymous@', _curDir, localPath,
                                                     False, transferList))
                    transferList[:] = []
                    _curYear = y
            filename = "chirps-v2.0.{0}.{1}.{2}.tif".format(y, m, dd)
            transferList.append(filename)
        if transferList:
            # get remaining files
            _curDir = "{0}{1}/".format(dailyDir, _curYear)
            all_files.extend(getFilesFromFTP(ftp_address_CHIRPS, 'anonymous', 'anonymous@', _curDir, localPath, False, transferList))
    return all_files
예제 #3
0
def downloadMonthlyDataFromFTP(localPath, dates=None):
    monthlyDir = 'pub/org/chg/products/CHIRPS-2.0/global_monthly/tifs/'
    all_files = []
    if not dates:
        # get all files
        all_files.extend(getFilesFromFTP(ftp_address_CHIRPS, 'anonymous', 'anonymous@', monthlyDir, localPath))
    else:
        transferList = []
        for d in dates:
            if type(d) is not datetime.date:
                d = datetime.datetime.strptime(d, '%Y-%m')
            y = d.strftime("%Y")
            m = d.strftime("%m")
            filename = "chirps-v2.0.{0}.{1}.tif.gz".format(y, m)
            transferList.append(filename)
        all_files.extend(getFilesFromFTP(ftp_address_CHIRPS, 'anonymous', 'anonymous@', monthlyDir, localPath, False, transferList))
    return all_files
예제 #4
0
def getPentadDataFromFTP(localPath, dates=None):
    pentadDir = 'pub/org/chg/products/CHIRPS-2.0/global_pentad/tifs/'
    if not dates:
        # get all files
        getFilesFromFTP(ftp_address_CHIRPS, 'anonymous', 'anonymous@', pentadDir, localPath)
    else:
        transferList = []
        for d in dates:
            if type(d) is not datetime.date:
                d = datetime.datetime.strptime(d, '%Y-%m-%d')
            y = d.strftime("%Y")
            m = d.strftime("%m")
            dd = (d.strftime("%d")).lstrip("0")
            filename = "chirps-v2.0.{0}.{1}.{2}.tif.gz".format(y, m, dd)
            transferList.append(filename)
        if transferList:
            getFilesFromFTP(ftp_address_CHIRPS, 'anonymous', 'anonymous@', pentadDir, localPath, False, transferList)
    return 0
예제 #5
0
def getMonthlyDataFromFTP(localPath, startDate = None, endDate = None):
    monthlyDir = 'pub/org/chg/products/CHIRPS-2.0/global_monthly/tifs/'
    if not startDate:
        # get all files
        getFilesFromFTP(ftp_address_CHIRPS, 'anonymous', 'anonymous@', monthlyDir, localPath)
    elif not endDate:
        # get all months AFTER startDate
        ftp = openFTP(ftp_address_CHIRPS, 'anonymous', 'anonymous@')
        ftp.cwd(monthlyDir)
        filesList = ftp.nlst()
        ftp.close()
        transferList = []
        for f in filesList:
            y = int(getCHIRPSYear(f))
            m = int(getCHIRPSMonth(f))
            file_date = date(y, m, 1)
            if (file_date >= startDate):
                transferList.append(f)
        getFilesFromFTP(ftp_address_CHIRPS, 'anonymous', 'anonymous@', monthlyDir, localPath, False, transferList)
    else:
        # get all months between startDate and endDate (inclusive)
        ftp = openFTP(ftp_address_CHIRPS, 'anonymous', 'anonymous@')
        ftp.cwd(monthlyDir)
        filesList = ftp.nlst()
        ftp.close()
        transferList = []
        for f in filesList:
            y = getCHIRPSYear(f)
            m = getCHIRPSMonth(f)
            file_date = date(y, m, 1)
            if (file_date >= startDate) and (file_date <= endDate):
                transferList.append(f)
        getFilesFromFTP(ftp_address_CHIRPS, 'anonymous', 'anonymous@', monthlyDir, localPath, False, transferList)
    return 0
예제 #6
0
def downloadSeasonalDataFromFTP(localPath, dates=None):
    seasonalDir = 'pub/org/chg/products/CHIRPS-2.0/global_3-monthly/tifs/'
    all_files = []
    if not dates:
        # get all files
        all_files.extend(getFilesFromFTP(ftp_address_CHIRPS, 'anonymous', 'anonymous@', seasonalDir, localPath))
    else:
        transferList = []
        for d in dates:
            if type(d) is not datetime.date:
                d = datetime.datetime.strptime(d, '%Y-%m')
            y = d.strftime("%Y")
            m = d.strftime("%m")
            if m == '11':
                s = "111201"
            elif m == '12':
                s = "120102"
            else:
                s = "{0}{1}{2}".format(m, str(int(m)+1).zfill(2), str(int(m)+2).zfill(2))
            filename = "chirps-v2.0.{0}.{1}.tiff.gz".format(y, s)
            transferList.append(filename)
        all_files.extend(getFilesFromFTP(ftp_address_CHIRPS, 'anonymous', 'anonymous@', seasonalDir, localPath, False, transferList))
    return all_files
예제 #7
0
def getSeasonalDataFromFTP(localPath):
    seasonalDir = 'pub/org/chg/products/CHIRPS-2.0/global_3-monthly/tifs/'
    getFilesFromFTP(ftp_address_CHIRPS, 'anonymous', 'anonymous@', seasonalDir, localPath)
    return 0