-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess_locally.py
executable file
·69 lines (53 loc) · 1.78 KB
/
process_locally.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
67
68
69
#!/usr/bin/env python
"""This script generates the windfield data required for my master's thesis
auth: jhartman
date: 2020-09-08
args:
remote_dir, optional: the name of the remote directory to load the
predictions from.
"""
import argparse
import logging
from multiprocessing import Pool
from climada.hazard.tc_tracks_forecast import TCForecast
import numpy as np
import psycopg2
import tqdm
from tc_risk_forecast import process_trackset, DSN
NUM_PROC = 20
tclogger = logging.getLogger('climada.hazard.trop_cyclone')
tclogger.setLevel(logging.WARNING)
parser = argparse.ArgumentParser()
parser.add_argument("--remote_dir", default=None)
args = parser.parse_args()
bufr_files = TCForecast.fetch_bufr_ftp(remote_dir=args.remote_dir)
fcast = TCForecast()
fcast.fetch_ecmwf(files=bufr_files)
# drop tracks with only one timestep
fcast.data = [tr for tr in fcast.data if tr.time.size>1]
# interpolate to 3h steps from the original 6h
fcast.equal_timestep(3)
sids = np.unique([storm.sid for storm in fcast.data])
tracks_per_sid = [
fcast.subset({'sid': sid}) for sid in sids
]
with Pool(NUM_PROC) as pool:
res = list(tqdm.tqdm(
pool.imap(process_trackset, tracks_per_sid),
total=len(tracks_per_sid)
))
con = psycopg2.connect(DSN)
with con.cursor() as c:
c.execute('drop index fcast_series_centroid_idx;')
c.execute('drop index fcast_series_storm_idx;')
con.commit()
c.execute('insert into fcast_series_t'
' select * from fcast_series_staging_t;')
c.execute('truncate fcast_series_staging_t;')
con.commit()
c.execute('create index fcast_series_centroid_idx'
' on fcast_series_t(centroid_idx);')
c.execute('create index fcast_series_storm_idx'
' on fcast_series_t(storm_id);')
con.commit()
con.close()