-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.py
executable file
·208 lines (181 loc) · 6.01 KB
/
fetch.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#! /usr/bin/env python3
'''
Program for adding realtime X-ray data to postgresql.
Copyright 2024 Finnish Meteorological Institute
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Aurthor(s): Ilja Honkonen
'''
import argparse
from datetime import datetime
from json import loads
import os
try:
import psycopg2
except Exception as e:
print("Couldn't import psycopg2, try pip3 install --user psycopg2:", e)
exit(1)
try:
import requests
except Exception as e:
print("Couldn't import requests, try pip3 install --user requests:", e)
exit(1)
parser = argparse.ArgumentParser(
description = 'Fetches X-ray data into postgresql.',
formatter_class = argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'--db-name',
default = 'test',
metavar = 'N',
help = 'Operate on database named N.')
parser.add_argument(
'--db-user',
default = 'test',
metavar = 'U',
help = 'Operate on database as user U.')
parser.add_argument(
'--db-password-env',
default = 'XRAYPW',
metavar = 'S',
help = 'Use password from env var S for database connection.')
parser.add_argument(
'--db-host',
default = 'localhost',
metavar = 'H',
help = 'Operate on database at address H.')
parser.add_argument(
'--db-port',
type = int,
default = 5432,
metavar = 'P',
help = 'Operate on database at port P.')
parser.add_argument(
'--table',
default = 'test',
metavar = 'T',
help = 'Use table T in database N.')
parser.add_argument(
'--url',
default = 'https://services.swpc.noaa.gov/json/goes/primary/xrays-6-hour.json,https://services.swpc.noaa.gov/json/goes/secondary/xrays-6-hour.json',
help = 'Comma-separated list of URLs for downloading new data.')
args = parser.parse_args()
args.url = args.url.split(',')
if not args.db_password_env in os.environ:
print('Environment variable for db password', args.db_password_env, "doesn't exist")
exit(1)
try:
connection = psycopg2.connect(
dbname = args.db_name,
user = args.db_user,
password = os.environ[args.db_password_env],
host = args.db_host,
port = args.db_port)
except Exception as e:
print("Couldn't connect to database: ", e)
exit(1)
texts = []
for url in args.url:
text = None
try:
text = requests.get(url).text
# fix common problems
if text.endswith(', {"'):
text = text[:-4] + ']'
elif text.endswith('m"'):
text += '}]'
elif text.endswith('"},'):
text = text[:-1] + ']'
elif text.endswith('"}'):
text += ']'
except Exception as e:
print("Couldn't download data from " + url + ':', e)
finally:
texts.append(text)
ok = False
for i in range(len(texts)):
if texts[i] == None or len(texts[i]) == 0:
print('No data from download', args.url[i])
else:
ok = True
if not ok:
print('No data from downloads.')
exit()
jsons = []
for i in range(len(texts)):
json = None
try:
if texts[i] != None:
json = loads(texts[i])
except Exception as e:
print("Couldn't interpret JSON data from", args.url[i], ': ', e)
finally:
jsons.append(json)
ok = False
for i in range(len(jsons)):
if jsons[i] == None:
print('No JSON data from', args.url[i])
elif not 'time_tag' in jsons[i][0]:
print('No time tag in first item from', args.url[i], ': ', jsons[i][0])
else:
ok = True
if not ok:
print('No json data.')
exit(1)
data = []
satellites = set()
energies = set()
for json in jsons:
for item in json:
satellites.add(item['satellite'])
energies.add(item['energy'])
item['corrected_flux'] = item['flux']
item.pop('flux', None)
data.append(item)
cursor = connection.cursor()
# possibly create db
cursor.execute('create table if not exists ' + args.table + ' (datetime varchar, satellite int, energy varchar, corrected_flux real, observed_flux real, electron_correction real, primary key (datetime, satellite, energy))')
connection.commit()
# TODO: make sure that correct columns exist
# exclude old data
latest_data = dict()
for sat in satellites:
if not sat in latest_data:
latest_data[sat] = dict()
for nrj in energies:
cursor.execute('select max(datetime) from ' + args.table + ' where satellite = %s and energy = %s', [sat, nrj])
result = cursor.fetchone()[0]
if result == None:
latest_data[sat][nrj] = datetime.strptime('1900-01-01T00:00:00Z', '%Y-%m-%dT%H:%M:%S%z')
else:
latest_data[sat][nrj] = datetime.strptime(result, '%Y-%m-%dT%H:%M:%S%z')
new_data = []
for d in data:
if datetime.strptime(d['time_tag'], '%Y-%m-%dT%H:%M:%S%z') > latest_data[d['satellite']][d['energy']]:
new_data.append(d)
try:
cursor.execute('begin transaction')
cursor.execute('lock table ' + args.table + ' in exclusive mode nowait')
except Exception as e:
print('Someone already writing to table', args.table, 'of database', args.db_name)
exit()
inserted = 0
for d in new_data:
cursor.execute('insert into ' + args.table + ' (datetime, satellite, energy, corrected_flux, observed_flux, electron_correction) values (%s, %s, %s, %s, %s, %s) on conflict (datetime, satellite, energy) do nothing', (d['time_tag'], d['satellite'], d['energy'], d['corrected_flux'], d['observed_flux'], d['electron_correction']))
inserted += 1
connection.commit()
cursor.close()
connection.close()
print(inserted, 'new values added to database')