Last active
September 25, 2019 22:44
-
-
Save giovanisleite/ff58f091e521b76cc29d8ba8ff41649d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import shutil | |
BASE_FILE_URLS = [ | |
'https://www.wsl.ch/lud/chelsa/data/timeseries20c/prec/CHELSAcruts_prec_{}_{}_V.1.0.tif', | |
'https://www.wsl.ch/lud/chelsa/data/timeseries20c/tmax/CHELSAcruts_tmax_{}_{}_V.1.0.tif', | |
'https://www.wsl.ch/lud/chelsa/data/timeseries20c/tmin/CHELSAcruts_tmin_{}_{}_V.1.0.tif', | |
] | |
def list_files(): | |
for base_url in BASE_FILE_URLS: | |
for year in range(2001, 2017): | |
for month in range(1, 13): | |
yield base_url.format(month, year) | |
variable = base_url.replace('https://www.wsl.ch/lud/chelsa/data/timeseries20c/', '').split('/')[0] | |
print(f'FINISHED YEAR {year} of variable: {variable}') | |
def download_file(url): | |
filename = url.split('/')[-1] | |
try: | |
with requests.get(url, stream=True) as r: | |
with open(filename, 'wb') as f: | |
shutil.copyfileobj(r.raw, f) | |
return filename | |
except Exception: | |
print('Download of {url} FAILED') | |
if __name__ == "__main__": | |
for file_url in list_files(): | |
download_file(file_url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment