/
datasource.py
117 lines (91 loc) · 3.18 KB
/
datasource.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2010-2011.
# Author(s):
# Martin Raspaud <martin.raspaud@smhi.se>
# This file is part of pytroll.
# Pytroll 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.
# Pytroll 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
# pytroll. If not, see <http://www.gnu.org/licenses/>.
"""A datasource for global metop granules.
"""
from posttroll.publisher import Publisher
from posttroll.message import Message
from posttroll.message_broadcaster import sendaddresstype
import socket
import time
from datetime import datetime, timedelta
import glob
import os
def get_own_ip():
"""Get the host's ip number.
"""
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect(('smhi.se', 0))
ip_ = sock.getsockname()[0]
sock.close()
return ip_
PATH = "/data/prod/satellit/metop"
PATTERN = "AVHR_xxx_1B_M02_*"
stamp = datetime.utcnow() - timedelta(hours=1)
def get_file_list(timestamp):
"""Get files.
"""
flist = glob.glob(os.path.join(PATH, PATTERN))
result = []
for fil in flist:
if not os.path.isfile(fil):
continue
mtime = os.stat(fil).st_mtime
dt_ = datetime.utcfromtimestamp(mtime)
if timestamp < dt_:
result.append((fil, dt_))
return sorted(result, lambda x, y: cmp(x[1], y[1]))
def younger_than_stamp_files():
"""Uses glob polling to get new files.
"""
global stamp
for fil, tim in get_file_list(stamp):
yield os.path.join(PATH, fil)
stamp = tim
def send_new_files():
"""Create messages and send away.
"""
for fil in younger_than_stamp_files():
base = os.path.basename(fil)
metadata = {
"filename": base,
"URIs": ["file://"+fil],
"type": "HRPT 1b",
"format": "EPS 1b",
"time_of_first_scanline": datetime.strptime(base[16:30],
"%Y%m%d%H%M%S").isoformat(),
"time_of_last_scanline": datetime.strptime(base[32:46],
"%Y%m%d%H%M%S").isoformat()}
import pprint
pprint.pprint(metadata)
yield Message('/dc/polar/gds', 'file', metadata)
PUB_ADDRESS = "tcp://" + str(get_own_ip()) + ":9000"
BROADCASTER = sendaddresstype('p1', PUB_ADDRESS, "HRPT 1b", 2).start()
time.sleep(10)
PUB = Publisher(PUB_ADDRESS)
try:
#for msg in SUB(timeout=1):
# print "Consumer got", msg
counter = 0
while True:
counter += 1
for i in send_new_files():
print "publishing " + str(i)
PUB.send(str(i))
time.sleep(60)
except KeyboardInterrupt:
print "terminating datasource..."
BROADCASTER.stop()
PUB.stop()