-
Notifications
You must be signed in to change notification settings - Fork 0
/
cutout_point.py
66 lines (54 loc) · 2.05 KB
/
cutout_point.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import time
import zipfile
from pathlib import Path
# install required dependencies with `pip install requests structlog`
import requests
import structlog
log = structlog.get_logger()
url = 'https://files.isimip.org/api/v2'
paths = [
'ISIMIP3a/InputData/climate/atmosphere/obsclim/global/daily/historical/CHELSA-W5E5v1.0/chelsa-w5e5v1.0_obsclim_tas_30arcsec_global_daily_197901.nc',
]
data = {
'paths': paths,
'operations': [
{
'operation': 'cutout_point',
'point': [
13.064332, # longitude
52.38051 # latitude
],
}
]
}
download_path = 'download'
# perform the initial request to the server
response = requests.post(url, json=data)
try:
response.raise_for_status() # check if the request was successfull
job = response.json() # extract the job object from the response
except (requests.exceptions.HTTPError, requests.exceptions.JSONDecodeError):
log.error('job submission failed', error=response.text)
else:
log.info('job submitted', id=job.get('id'), status=job.get('status'))
while job.get('status') in ['queued', 'started']:
# wait for 4 sec
time.sleep(4)
# check the status of the job
job = requests.get(job['job_url']).json()
log.info('job updated', id=job['id'], status=job['status'], meta=job['meta'])
if job.get('status') == 'finished':
# download file
zip_path = Path(download_path) / job['file_name']
zip_path.parent.mkdir(exist_ok=True)
log.info('downloading', file_url=job['file_url'])
with requests.get(job['file_url'], stream=True) as response:
with zip_path.open('wb') as fp:
for chunk in response.iter_content(chunk_size=8192):
fp.write(chunk)
# extract zip file
out_path = zip_path.with_suffix('')
out_path.mkdir(exist_ok=True)
log.info('extracting', zip_path=str(out_path.resolve()))
with zipfile.ZipFile(zip_path, 'r') as zf:
zf.extractall(out_path)