-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseline.py
110 lines (96 loc) · 3.74 KB
/
baseline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
__filename__ = "baseline.py"
__author__ = "Bob Mottram"
__license__ = "GPL3+"
__version__ = "2.0.0"
__maintainer__ = "Bob Mottram"
__email__ = "[email protected]"
__status__ = "Production"
__module_group__ = "Commandline Interface"
def _get_baseline_for_year(id: str, stations_data: {}, year: int) -> []:
"""Returns the baseline for a given station id
"""
baseline = [None] * 12
if year not in stations_data[id]:
return baseline
month_data = stations_data[id][year]['month']
for month_index in range(12):
month_data2 = month_data[month_index]
if month_data2['qcflag'] == 'M':
# Flagged as error
continue
if month_data2['av'] > -80:
if month_data2['av'] < 80:
baseline[month_index] = month_data2['av']
return baseline
def _get_baseline(id: str, stations_data: {},
start_year: int, end_year: int) -> []:
"""Returns the baseline for a given station id
"""
if id not in stations_data:
return [None] * 12
if start_year == end_year:
return _get_baseline_for_year(id, stations_data, start_year)
baseline = [0.0] * 12
hits = [0] * 12
for year in range(start_year, end_year, 1):
if year not in stations_data[id]:
continue
month_data = stations_data[id][year]['month']
for month_index in range(12):
month_data2 = month_data[month_index]
if month_data2['qcflag'] == 'M':
# Flagged as error
continue
if month_data2['av'] > -80:
if month_data2['av'] < 80:
baseline[month_index] += month_data2['av']
hits[month_index] += 1
for month_index in range(12):
if hits[month_index] > 0:
baseline[month_index] /= float(hits[month_index])
else:
baseline[month_index] = None
return baseline
def _baseline_for_stations(stations_data: {}, station_locations: {},
start_year: int, end_year: int,
station_ids: set,
min_latitude: float, max_latitude: float) -> []:
"""Returns a baseline for the given range of years
and the given station ids
"""
if not station_ids:
return [None] * 12, False
baseline = [0.0] * 12
hits = [0] * 12
for sid in station_ids:
if station_locations[sid]['latitude'] < min_latitude or \
station_locations[sid]['latitude'] > max_latitude:
continue
station_baseline = \
_get_baseline(sid, stations_data, start_year, end_year)
for month_index in range(12):
if station_baseline[month_index] is not None:
baseline[month_index] += station_baseline[month_index]
hits[month_index] += 1
for month_index in range(12):
if hits[month_index] > 0:
baseline[month_index] /= float(hits[month_index])
else:
baseline[month_index] = None
return baseline, True
def update_grid_baselines(grid: [], stations_data: {}, station_locations: {},
start_year: int, end_year: int,
min_latitude: float, max_latitude: float) -> int:
"""Calculates reference baselines for each grid cell
"""
ctr = 0
for grid_cell in grid:
if grid_cell['station_ids']:
grid_cell['baseline'], has_data = \
_baseline_for_stations(stations_data, station_locations,
start_year, end_year,
grid_cell['station_ids'],
min_latitude, max_latitude)
if has_data:
ctr += 1
return ctr