-
Notifications
You must be signed in to change notification settings - Fork 1
/
fit_kriging_model.py
72 lines (58 loc) · 2.01 KB
/
fit_kriging_model.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
70
71
72
"""Fit a spatiotemporal Kriging model to the given points containing radar- and
gauge-measured rainfall accumulations. This implementation uses 3d ordinary
Kriging from PyKrige (https://geostat-framework.readthedocs.io/projects/pykrige/en/stable)
so that the third dimension is reserved for time.
Input
-----
- radar-gauge pair file generated by running collect_radar_gauge_pairs.py with
attribute "gauge_location" included
Output
------
Pickle dump containing the fitted model.
Configuration files (in the config/<profile> directory)
-------------------------------------------------------
- fit_kriging_model.cfg
"""
import argparse
import configparser
import os
import pickle
import numpy as np
from pykrige.ok3d import OrdinaryKriging3D
# parse command-line arguments
argparser = argparse.ArgumentParser()
argparser.add_argument("rgpairfile", type=str, help="radar-gauge pair file")
argparser.add_argument("outfile", type=str, help="output file")
argparser.add_argument("profile", type=str, help="configuration profile to use")
args = argparser.parse_args()
# read configuration file
config = configparser.ConfigParser()
config.read(os.path.join("config", args.profile, "fit_kriging_model.cfg"))
radar_gauge_pairs = pickle.load(open(args.rgpairfile, "rb"))
# collect data points for Kriging
x = []
y = []
z = []
val = []
for timestamp in radar_gauge_pairs.keys():
for fmisid in radar_gauge_pairs[timestamp].keys():
p = radar_gauge_pairs[timestamp][fmisid]
x_, y_ = p[2]["gauge_location"]
x.append(x_)
y.append(y_)
z.append(timestamp.timestamp())
val.append(np.log10(p[1] / p[0]))
if config["kriging"]["time_scaling_factor"] == "auto":
anisotropy_scaling_z = 0.5 * (np.std(x) + np.std(y)) / np.std(z)
else:
anisotropy_scaling_z = float(config["kriging"]["time_scaling_factor"])
model = OrdinaryKriging3D(
x,
y,
z,
val,
variogram_model="exponential",
anisotropy_scaling_z=anisotropy_scaling_z,
verbose=True,
)
pickle.dump(model, open(args.outfile, "wb"))