-
Notifications
You must be signed in to change notification settings - Fork 0
/
mask_landonly.py
62 lines (50 loc) · 1.93 KB
/
mask_landonly.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
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 = [
'ISIMIP3b/InputData/climate/atmosphere/bias-adjusted/global/daily/ssp585/GFDL-ESM4/gfdl-esm4_r1i1p1f1_w5e5_ssp585_tas_global_daily_2015_2020.nc'
]
data = {
'paths': paths,
'operations': [
{
'operation': 'mask_landonly'
}
]
}
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)