Completed
Push — master ( 9e9700...486a1c )
by Michael
12:17
created

StationRetriever.StationRetriever.getStations()   C

Complexity

Conditions 9

Size

Total Lines 59
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 59
rs 6.4339
c 0
b 0
f 0
cc 9
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import os
2
import sys
3
from awips import ThriftClient
4
from dynamicserialize.dstypes.gov.noaa.nws.ncep.common.dataplugin.gempak.request import GetStationsRequest
5
6
7
class StationRetriever:
8
    """ Retrieves all requested stations """
9
10
    def __init__(self, server, pluginName):
11
        self.pluginName = pluginName
12
        self.outdir = os.getcwd()
13
        self.host = os.getenv("DEFAULT_HOST", server)
14
        self.port = os.getenv("DEFAULT_PORT", "9581")
15
        self.client = ThriftClient.ThriftClient(self.host, self.port)
16
17
    def getStations(self):
18
        """ Sends ThriftClient request and writes out received files."""
19
        req = GetStationsRequest()
20
        req.setPluginName(self.pluginName)
21
        resp = self.client.sendRequest(req)
22
23
        for i, rec in enumerate(resp):
24
            resp[i] = {
25
                key.decode() if isinstance(key, bytes) else key:
26
                    val.decode() if isinstance(val, bytes) else val
27
                for key, val in rec.items()
28
            }
29
30
        stns = []
31
        for item in resp:
32
            stationstr = '{:<8}'.format(item.getStationId())
33
34
            if sys.byteorder == 'little':
35
                stnid = (ord(stationstr[3]) << 24) + (ord(stationstr[2]) << 16) + \
36
                        (ord(stationstr[1]) << 8) + ord(stationstr[0])
37
                stnid2 = (ord(stationstr[7]) << 24) + (ord(stationstr[6]) << 16) + \
38
                         (ord(stationstr[5]) << 8) + ord(stationstr[4])
39
            else:
40
                stnid = (ord(stationstr[0]) << 24) + (ord(stationstr[1]) << 16) + \
41
                        (ord(stationstr[2]) << 8) + ord(stationstr[3])
42
                stnid2 = (ord(stationstr[4]) << 24) + (ord(stationstr[5]) << 16) + \
43
                         (ord(stationstr[6]) << 8) + ord(stationstr[7])
44
45
            if item.getState() is None:
46
                stationstr = '    '
47
            else:
48
                stationstr = '{:<4}'.format(item.getState())
49
50
            if sys.byteorder == 'little':
51
                state = (ord(stationstr[3]) << 24) + (ord(stationstr[2]) << 16) \
52
                        + (ord(stationstr[1]) << 8) + ord(stationstr[0])
53
            else:
54
                state = (ord(stationstr[0]) << 24) + (ord(stationstr[1]) << 16) \
55
                        + (ord(stationstr[2]) << 8) + ord(stationstr[3])
56
57
            stationstr = '{:<4}'.format(item.getCountry())
58
            if sys.byteorder == 'little':
59
                cntry = (ord(stationstr[3]) << 24) + (ord(stationstr[2]) << 16) \
60
                        + (ord(stationstr[1]) << 8) + ord(stationstr[0])
61
            else:
62
                cntry = (ord(stationstr[0]) << 24) + (ord(stationstr[1]) << 16) \
63
                        + (ord(stationstr[2]) << 8) + ord(stationstr[3])
64
65
            stns.append(9999)
66
            stns.append(stnid)
67
            stns.append(item.getWmoIndex())
68
            stns.append(int(item.getLatitude()*100))
69
            stns.append(int(item.getLongitude()*100))
70
            stns.append(int(item.getElevation()))
71
            stns.append(state)
72
            stns.append(cntry)
73
            stns.append(stnid2)
74
            stns.append(0)
75
        return stns
76
77
78
def getstations(server, table, key, dummy, dummy2):
79
    sr = StationRetriever(server, table)
80
    return sr.getStations()
81
82
83
# This is the standard boilerplate that runs this script as a main
84
if __name__ == '__main__':
85
    # Run Test
86
    srv = 'edex-cloud.unidata.ucar.edu'
87
    key = '-'
88
    print('OBS - METAR')
89
    tbl = 'obs'
90
    print(getstations(srv, tbl, key))
91
    print('SFCOBS - SYNOP')
92
    tbl = 'sfcobs'
93
    print(getstations(srv, tbl, key))
94