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

GridInfoRetriever.GridInfoRetriever.__init__()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nop 6
1
import os
2
from datetime import datetime
3
from operator import itemgetter
4
from awips import ThriftClient
5
from dynamicserialize.dstypes.gov.noaa.nws.ncep.common.dataplugin.gempak.request import GetGridInfoRequest
6
7
8
class GridInfoRetriever:
9
10
    def __init__(self, server, pluginName, modelId, cycle=None, forecast=None):
11
        self.pluginName = pluginName
12
        self.modelId = modelId
13
        self.cycle = cycle
14
        self.forecast = forecast
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 getInfo(self):
20
        """ Sends ThriftClient request and writes out received files."""
21
        req = GetGridInfoRequest()
22
        req.setPluginName(self.pluginName)
23
        req.setModelId(self.modelId)
24
25
        req.setReftime(self.cycle)
26
        if len(self.cycle) > 2:
27
            dt = datetime.strptime(self.cycle, '%y%m%d/%H%M')
28
            ct = datetime.strftime(dt, '%Y-%m-%d %H:%M:%S')
29
            req.setReftime(ct)
30
31
        req.setFcstsec(self.forecast)
32
33
        resp = self.client.sendRequest(req)
34
35
        sortresp = sorted(sorted(resp, key=itemgetter("reftime"), reverse=True), key=itemgetter("fcstsec"))
36
37
        grids = []
38
39
        count = 0
40
        for record in sortresp:
41
            s = '{:<12}'.format(record['param'])
42
43
            if sys.byteorder == 'little':
44
                parm1 = (ord(s[3]) << 24) + (ord(s[2]) << 16) + (ord(s[1]) << 8) + ord(s[0])
45
                parm2 = (ord(s[7]) << 24) + (ord(s[6]) << 16) + (ord(s[5]) << 8) + ord(s[4])
46
                parm3 = (ord(s[11]) << 24) + (ord(s[10]) << 16) + (ord(s[9]) << 8) + ord(s[8])
47
            else:
48
                parm1 = (ord(s[0]) << 24) + (ord(s[1]) << 16) + (ord(s[2]) << 8) + ord(s[3])
49
                parm2 = (ord(s[4]) << 24) + (ord(s[5]) << 16) + (ord(s[6]) << 8) + ord(s[7])
50
                parm3 = (ord(s[8]) << 24) + (ord(s[9]) << 16) + (ord(s[10]) << 8) + ord(s[11])
51
52
            dt = datetime.strptime(record['reftime'], '%Y-%m-%d %H:%M:%S.%f')
53
            dattim = dt.month * 100000000 + dt.day * 1000000 + (dt.year%100) * 10000 + dt.hour * 100 + dt.minute
54
            fcsth = (int(record['fcstsec']) / 60) / 60
55
            fcstm = (int(record['fcstsec']) / 60) % 60
56
            fcst = 100000 + fcsth * 100 + fcstm
57
58
            lv1 = float(record['level1'])
59
            if lv1 == -999999.0:
60
                lv1 = -1.0
61
            lv2 = float(record['level2'])
62
            if lv2 == -999999.0:
63
                lv2 = -1.0
64
65
            vcd = record['vcoord']
66
            if vcd == 'NONE':
67
                ivcd = 0
68
            elif vcd == 'PRES':
69
                ivcd = 1
70
            elif vcd == 'THTA':
71
                ivcd = 2
72
            elif vcd == 'HGHT':
73
                ivcd = 3
74
            elif vcd == 'SGMA':
75
                ivcd = 4
76
                if lv1 >= 0.0:
77
                    lv1 = lv1 * 10000.0
78
                if lv2 >= 0.0:
79
                    lv2 = lv2 * 10000.0
80
            elif vcd == 'DPTH':
81
                ivcd = 5
82
                if lv1 >= 0.0:
83
                    lv1 = lv1 * 100.0
84
                if lv2 >= 0.0:
85
                    lv2 = lv2 * 100.0
86
            elif vcd == 'HYBL':
87
                ivcd = 6
88
            else:
89
                v = '{:<4}'.format(vcd)
90
                if sys.byteorder == 'little':
91
                    ivcd = (ord(v[3]) << 24) + (ord(v[2]) << 16) + (ord(v[1]) << 8) + ord(v[0])
92
                else:
93
                    ivcd = (ord(v[0]) << 24) + (ord(v[1]) << 16) + (ord(v[2]) << 8) + ord(v[3])
94
                if vcd == 'POTV':
95
                    if lv1 >= 0.0:
96
                        lv1 = lv1 * 1000.0
97
                    if lv2 >= 0.0:
98
                        lv2 = lv2 * 1000.0
99
            grids.append(9999)
100
            grids.append(dattim)
101
            grids.append(fcst)
102
            grids.append(0)
103
            grids.append(0)
104
            grids.append(int(lv1))
105
            grids.append(int(lv2))
106
            grids.append(ivcd)
107
            grids.append(parm1)
108
            grids.append(parm2)
109
            grids.append(parm3)
110
            count += 1
111
            if count > 29998:
112
                break
113
114
        return grids
115
             
116
117
def getinfo(server, table, model, cycle, forecast):
118
    gir = GridInfoRetriever(server, table, model, cycle, forecast)
119
    return gir.getInfo()
120
121
122
def getrow(server, table, model, cycle, forecast):
123
    idata = []
124
    idata.append(9999)
125
    idata.append(1)
126
    return idata
127
128
129
# This is the standard boilerplate that runs this script as a main
130
if __name__ == '__main__':
131
    import sys 
132
    # Run Test
133
    srv = 'edex-cloud.unidata.ucar.edu'
134
    tbl = 'grid'
135
    mdl = 'NAM40'
136
    print(getrow(srv, tbl, mdl))
137
    print(getinfo(srv, tbl, mdl))
138