Completed
Push — master ( 98c26c...9e9700 )
by Michael
15:43
created

TimeRetriever   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 50
dl 0
loc 69
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A TimeRetriever.__init__() 0 7 1
A TimeRetriever.getTimes() 0 26 4

1 Function

Rating   Name   Duplication   Size   Complexity  
A gettimes() 0 3 1
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
        timelist = []
26
        for item in resp.getTimes():
27
            if isinstance(item, GregorianCalendar):
28
                tstamp = item.getTimeInMillis()
29
            else:
30
                tstamp = item.getTime()
31
            time = datetime.utcfromtimestamp(tstamp/1000)
32
            timelist.append(time)
33
34
        timelist.sort(reverse=True)
35
36
        times = []
37
        for time in timelist:
38
            times.append(9999)
39
            times.append((time.year % 100) * 10000 + (time.month * 100) + time.day)
40
            times.append((time.hour * 100) + time.minute)
41
42
        # GEMPAK can only handle up to 200 times, which is 600 elements
43
        # in this array -- [9999, DATE, TIME] -- repeated
44
        return times[0:600]
45
46
47
def gettimes(server, table, key, dummy, dummy2):
48
    tr = TimeRetriever(server, table, key)
49
    return tr.getTimes()
50
51
52
# This is the standard boilerplate that runs this script as a main
53
if __name__ == '__main__':
54
    srv = 'edex-cloud.unidata.ucar.edu'
55
    print('OBS - METAR')
56
    tbl = 'obs'
57
    key = 'refHour'
58
    print(gettimes(srv, tbl, key))
59
60
    print('SFCOBS - SYNOP')
61
    tbl = 'sfcobs'
62
    key = 'refHour'
63
    print(gettimes(srv, tbl, key))
64
65
    print('BUFRUA')
66
    tbl = 'bufrua'
67
    key = 'dataTime.refTime'
68
    print(gettimes(srv, tbl, key))
69