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