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

StationRetriever.StationRetriever.getStations()   B

Complexity

Conditions 6

Size

Total Lines 52
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 52
rs 7.9626
c 0
b 0
f 0
cc 6
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
        stns = []
24
        for item in resp:
25
            stationstr = '{:<8}'.format(item.getStationId())
26
27
            if sys.byteorder == 'little':
28
                stnid = (ord(stationstr[3]) << 24) + (ord(stationstr[2]) << 16) + \
29
                        (ord(stationstr[1]) << 8) + ord(stationstr[0])
30
                stnid2 = (ord(stationstr[7]) << 24) + (ord(stationstr[6]) << 16) + \
31
                         (ord(stationstr[5]) << 8) + ord(stationstr[4])
32
            else:
33
                stnid = (ord(stationstr[0]) << 24) + (ord(stationstr[1]) << 16) + \
34
                        (ord(stationstr[2]) << 8) + ord(stationstr[3])
35
                stnid2 = (ord(stationstr[4]) << 24) + (ord(stationstr[5]) << 16) + \
36
                         (ord(stationstr[6]) << 8) + ord(stationstr[7])
37
38
            if item.getState() is None:
39
                stationstr = '    '
40
            else:
41
                stationstr = '{:<4}'.format(item.getState())
42
43
            if sys.byteorder == 'little':
44
                state = (ord(stationstr[3]) << 24) + (ord(stationstr[2]) << 16) \
45
                        + (ord(stationstr[1]) << 8) + ord(stationstr[0])
46
            else:
47
                state = (ord(stationstr[0]) << 24) + (ord(stationstr[1]) << 16) \
48
                        + (ord(stationstr[2]) << 8) + ord(stationstr[3])
49
50
            stationstr = '{:<4}'.format(item.getCountry())
51
            if sys.byteorder == 'little':
52
                cntry = (ord(stationstr[3]) << 24) + (ord(stationstr[2]) << 16) \
53
                        + (ord(stationstr[1]) << 8) + ord(stationstr[0])
54
            else:
55
                cntry = (ord(stationstr[0]) << 24) + (ord(stationstr[1]) << 16) \
56
                        + (ord(stationstr[2]) << 8) + ord(stationstr[3])
57
58
            stns.append(9999)
59
            stns.append(stnid)
60
            stns.append(item.getWmoIndex())
61
            stns.append(int(item.getLatitude()*100))
62
            stns.append(int(item.getLongitude()*100))
63
            stns.append(int(item.getElevation()))
64
            stns.append(state)
65
            stns.append(cntry)
66
            stns.append(stnid2)
67
            stns.append(0)
68
        return stns
69
70
71
def getstations(server, table, key, dummy, dummy2):
72
    sr = StationRetriever(server, table)
73
    return sr.getStations()
74
75
76
# This is the standard boilerplate that runs this script as a main
77
if __name__ == '__main__':
78
    # Run Test
79
    srv = 'edex-cloud.unidata.ucar.edu'
80
    key = '-'
81
    print('OBS - METAR')
82
    tbl = 'obs'
83
    print(getstations(srv, tbl, key))
84
    print('SFCOBS - SYNOP')
85
    tbl = 'sfcobs'
86
    print(getstations(srv, tbl, key))
87