/
db_recorder.py
161 lines (127 loc) · 5.23 KB
/
db_recorder.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2012 Martin Raspaud
# Author(s):
# Martin Raspaud <martin.raspaud@smhi.se>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Records new files into the database system.
"""
# TODO: remove old hanging subscriptions
from posttroll.subscriber import Subscriber
from db.pytroll_db import DCManager
from db.hl_file import File
from pyorbital.orbital import Orbital
from datetime import datetime, timedelta
from sqlalchemy.orm.exc import NoResultFound
import np.nameclient as nc
from threading import Thread
import logging
from logger import ColoredFormatter
LOG = logging.getLogger("db_recorder")
LOG.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = ColoredFormatter("[%(asctime)s %(levelname)-19s] %(message)s")
ch.setFormatter(formatter)
LOG.addHandler(ch)
sat_lookup = {"NPP": "SUOMI NPP",
}
class DBRecorder(object):
"""The database recording machine.
Contains a thread listening to incomming messages, and a thread recording
to the database.
"""
def __init__(self,
(nameserver_address, nameserver_port)=("localhost", 16543)):
self.subscriber = Subscriber([], [])
address = "tcp://"+nameserver_address+":"+str(nameserver_port)
self.listener = Subscriber([address], [])
self.listener_thread = Thread(target=self.listen)
self.db_thread = Thread(target=self.record)
self.dbm = None
self.loop = True
def start(self):
"""Starts the logging.
"""
self.dbm = DCManager('postgresql://polar:polar@localhost:5432/sat_db')
self.listener_thread.start()
self.db_thread.start()
def listen(self):
"""Listen to incomming messages.
"""
for addr in nc.get_address(""):
LOG.info("Listening to " + str(addr["URI"]) +
" (" + str(addr["type"]) + ")")
self.subscriber.add(addr["URI"], addr["type"])
for msg in self.listener.recv(1):
if msg:
LOG.info("Now listening to " + str(msg.data["URI"]) +
" (" + str(msg.data["type"]) + ")")
# add new address to subscriber
self.subscriber.add(msg.data["URI"], msg.data["type"])
if not self.loop:
break
LOG.info("Stop listening")
def record(self):
"""Log stuff.
"""
for msg in self.subscriber.recv(1):
if msg:
if msg.type == "file":
try:
file_obj = File(msg.data["filename"], self.dbm,
filetype=msg.data.get("type", None),
fileformat=msg.data.get("format", None))
except NoResultFound:
LOG.warning("Cannot process: " + str(msg))
continue
for key, val in msg.data.items():
if key == "filename":
continue
if key == "uri":
file_obj["URIs"] += [val]
continue
try:
file_obj[key] = val
except NoResultFound:
LOG.warning("Cannot add: " + str((key, val)))
LOG.debug("adding :" + str(msg))
# compute sub_satellite_track
satname = msg.data["satellite"]
sat = Orbital(sat_lookup.get(satname, satname))
dt_ = timedelta(seconds=10)
current_time = msg.data["start_time"]
lonlat_list = []
while current_time <= msg.data["end_time"]:
pos = sat.get_lonlatalt(current_time)
lonlat_list.append(pos[:2])
current_time += dt_
LOG.debug("Computed sub-satellite track")
file_obj["sub_satellite_track"] = lonlat_list
LOG.debug("Added sub-satellite track")
if not self.loop:
LOG.info("Stop recording")
break
def stop(self):
"""Stop the machine.
"""
self.loop = False
if __name__ == '__main__':
import time
try:
recorder = DBRecorder()
recorder.start()
while True:
time.sleep(1)
except KeyboardInterrupt:
recorder.stop()
print "Thanks for using pytroll/db_recorder. See you soon on www.pytroll.org!"