-
Notifications
You must be signed in to change notification settings - Fork 3
/
store_test_data.py
171 lines (152 loc) · 4.92 KB
/
store_test_data.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""Provide some basic data to allow for better testing"""
import glob
import os
import subprocess
import sys
import psycopg
import requests
NETWORKS = [
"IA_RWIS",
"IA_ASOS",
"IACLIMATE",
"IA_COOP",
"WFO",
"IA_DCP",
"ISUSM",
"ISUAG",
"TALLTOWERS",
"RAOB",
"NWSCLI",
"NEXRAD",
]
def _s(val):
if val is None:
return None
return val[:10]
def do_stations(network: str):
"""hack"""
req = requests.get(
f"http://mesonet.agron.iastate.edu/api/1/network/{network}.json",
timeout=60,
)
data = req.json()
for dbname in "mesosite iem".split():
pgconn = psycopg.connect(
f"postgresql://mesonet@localhost/{dbname}?gssencmode=disable"
)
cursor = pgconn.cursor()
for entry in data["data"]:
cursor.execute(
"""
INSERT into stations(iemid, id, name, state, country, elevation,
network,online, county, plot_name, climate_site, wfo, tzname,
metasite, ugc_county, ugc_zone, ncdc81, ncei91, archive_begin,
archive_end, geom, remote_id) VALUES (%s, %s, %s, %s, %s, %s,
%s, 't', %s, %s, %s, %s, %s, 'f', %s, %s, %s,
%s, %s, %s, ST_Point(%s, %s, 4326), %s)
""",
(
entry["iemid"],
entry["id"],
entry["name"],
entry["state"],
entry["country"],
entry["elevation"],
network,
entry["county"],
entry["name"],
entry["climate_site"],
entry["wfo"],
entry["tzname"],
entry["ugc_county"],
entry["ugc_zone"],
entry["ncdc81"],
entry["ncei91"],
_s(entry["archive_begin"]),
_s(entry["archive_end"]),
entry["longitude"],
entry["latitude"],
entry["remote_id"],
),
)
# We have messed use the stations_iemid_seq at this point, so
# we need to fix it
cursor.execute(
"SELECT setval('stations_iemid_seq', max(iemid)) from stations"
)
cursor.close()
pgconn.commit()
pgconn.close()
def id3b_realtime():
"""Fake some data."""
pgconn = psycopg.connect("postgresql://mesonet@localhost/id3b")
cursor = pgconn.cursor()
cursor.execute(
"""
update ldm_product_log SET
entered_at = now() - ('2024-12-03 19:30+00'::timestamptz - entered_at),
valid_at = now() - ('2024-12-03 19:30+00'::timestamptz - valid_at),
wmo_valid_at = now() -
('2024-12-03 19:30+00'::timestamptz - wmo_valid_at)
where entered_at between '2024-12-03 16:00+00'
and '2024-12-03 20:00+00'
"""
)
cursor.close()
pgconn.commit()
pgconn.close()
def add_webcam():
"""Add a webcam"""
pgconn = psycopg.connect("postgresql://mesonet@localhost/mesosite")
cursor = pgconn.cursor()
cursor.execute(
"""
INSERT into webcams(id, name, network, online)
VALUES ('KCCI-027', 'ISU Ag Farm', 'KCCI', 't')
"""
)
cursor.close()
pgconn.commit()
pgconn.close()
def process_dbfiles(psql):
"""Process the DB files."""
files = glob.glob(os.path.dirname(__file__) + "/data/*.sql*")
files.sort()
args = ["-v", "ON_ERROR_STOP=1", "-U", "mesonet", "-h", "localhost"]
for fn in files:
# Le Sigh for iemre_china and iemre_europe
sep = "__" if fn.find("__") > 0 else "_"
dbname = os.path.basename(fn).split(sep)[0]
if fn.endswith(".gz"):
with subprocess.Popen(
["zcat", fn], stdout=subprocess.PIPE
) as zproc:
with subprocess.Popen(
[psql, *args, dbname],
stdin=zproc.stdout,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
) as proc:
proc.wait()
print(f"{fn} {proc.stderr.read()} {proc.stdout.read()}")
if proc.returncode != 0:
raise ValueError(f"{psql} returned non-zero!")
continue
with subprocess.Popen(
[psql, *args, "-f", fn, dbname],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
) as proc:
proc.wait()
print(f"{fn} {proc.stderr.read()} {proc.stdout.read()}")
if proc.returncode != 0:
raise ValueError(f"{psql} returned non-zero!")
def main(argv):
"""Workflow"""
_ = [do_stations(network) for network in NETWORKS]
psql = "psql" if len(argv) == 1 else argv[1]
process_dbfiles(psql)
add_webcam()
id3b_realtime()
if __name__ == "__main__":
main(sys.argv)