1
|
|
|
import os |
2
|
|
|
from datetime import datetime |
3
|
|
|
from awips import ThriftClient |
4
|
|
|
from dynamicserialize.dstypes.java.util import GregorianCalendar |
5
|
|
|
from dynamicserialize.dstypes.gov.noaa.nws.ncep.common.dataplugin.gempak.request import GetTimesRequest |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class TimeRetriever: |
9
|
|
|
""" Retrieves all requested times""" |
10
|
|
|
|
11
|
|
|
def __init__(self, server, pluginName, timeField): |
12
|
|
|
self.pluginName = pluginName |
13
|
|
|
self.timeField = timeField |
14
|
|
|
self.outdir = os.getcwd() |
15
|
|
|
self.host = os.getenv("DEFAULT_HOST", server) |
16
|
|
|
self.port = os.getenv("DEFAULT_PORT", "9581") |
17
|
|
|
self.client = ThriftClient.ThriftClient(self.host, self.port) |
18
|
|
|
|
19
|
|
|
def getTimes(self): |
20
|
|
|
""" Sends ThriftClient request and writes out received files.""" |
21
|
|
|
req = GetTimesRequest() |
22
|
|
|
req.setPluginName(self.pluginName) |
23
|
|
|
req.setTimeField(self.timeField) |
24
|
|
|
resp = self.client.sendRequest(req) |
25
|
|
|
|
26
|
|
|
for i, rec in enumerate(resp): |
27
|
|
|
resp[i] = { |
28
|
|
|
key.decode() if isinstance(key, bytes) else key: |
29
|
|
|
val.decode() if isinstance(val, bytes) else val |
30
|
|
|
for key, val in rec.items() |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
timelist = [] |
34
|
|
|
for item in resp.getTimes(): |
35
|
|
|
if isinstance(item, GregorianCalendar): |
36
|
|
|
tstamp = item.getTimeInMillis() |
37
|
|
|
else: |
38
|
|
|
tstamp = item.getTime() |
39
|
|
|
time = datetime.utcfromtimestamp(tstamp/1000) |
40
|
|
|
timelist.append(time) |
41
|
|
|
|
42
|
|
|
timelist.sort(reverse=True) |
43
|
|
|
|
44
|
|
|
times = [] |
45
|
|
|
for time in timelist: |
46
|
|
|
times.append(9999) |
47
|
|
|
times.append((time.year % 100) * 10000 + (time.month * 100) + time.day) |
48
|
|
|
times.append((time.hour * 100) + time.minute) |
49
|
|
|
|
50
|
|
|
# GEMPAK can only handle up to 200 times, which is 600 elements |
51
|
|
|
# in this array -- [9999, DATE, TIME] -- repeated |
52
|
|
|
return times[0:600] |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
def gettimes(server, table, key, dummy, dummy2): |
56
|
|
|
tr = TimeRetriever(server, table, key) |
57
|
|
|
return tr.getTimes() |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
# This is the standard boilerplate that runs this script as a main |
61
|
|
|
if __name__ == '__main__': |
62
|
|
|
srv = 'edex-cloud.unidata.ucar.edu' |
63
|
|
|
print('OBS - METAR') |
64
|
|
|
tbl = 'obs' |
65
|
|
|
key = 'refHour' |
66
|
|
|
print(gettimes(srv, tbl, key)) |
67
|
|
|
|
68
|
|
|
print('SFCOBS - SYNOP') |
69
|
|
|
tbl = 'sfcobs' |
70
|
|
|
key = 'refHour' |
71
|
|
|
print(gettimes(srv, tbl, key)) |
72
|
|
|
|
73
|
|
|
print('BUFRUA') |
74
|
|
|
tbl = 'bufrua' |
75
|
|
|
key = 'dataTime.refTime' |
76
|
|
|
print(gettimes(srv, tbl, key)) |
77
|
|
|
|