-
Notifications
You must be signed in to change notification settings - Fork 20
/
buoystation.py
296 lines (249 loc) · 12.5 KB
/
buoystation.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
from .basestation import BaseStation
from .buoydata import BuoyData
from .buoyspectra import BuoySpectra
from .swell import Swell
from .location import Location
from datetime import datetime
from .tools import parse_float, steepness
from . import units
import re
try:
import requests
except:
pass
import math
import pytz
class BuoyStation(BaseStation):
class BuoyType:
none = ''
buoy = 'buoy'
fixed = 'fixed'
oilrig = 'oilrig'
dart = 'dart'
tao = 'tao'
other = 'other'
def __init__(self, station_id, location, owner='', program='', active=False, currents=False, water_quality=False, dart=False, buoy_type=BuoyType.none):
super(BuoyStation, self).__init__(station_id, location)
# Attributes
self.owner = owner
self.program = program
self.type = buoy_type
self.active = active
self.currents = currents
self.water_quality = water_quality
self.dart = dart
# Data
self.data = []
@property
def latest_reading_url(self):
return 'https://www.ndbc.noaa.gov/data/latest_obs/' + self.station_id + '.txt'
@property
def meteorological_reading_url(self):
return 'https://www.ndbc.noaa.gov/data/realtime2/' + self.station_id + '.txt'
@property
def detailed_wave_reading_url(self):
return 'https://www.ndbc.noaa.gov/data/realtime2/' + self.station_id + '.spec'
@property
def wave_energy_reading_url(self):
return 'https://www.ndbc.noaa.gov/data/realtime2/' + self.station_id + '.data_spec'
@property
def directional_wave_reading_url(self):
return 'https://www.ndbc.noaa.gov/data/realtime2/' + self.station_id + '.swdir'
def parse_latest_reading_data(self, raw_data):
raw_data = raw_data.split('\n')
if len(raw_data) < 6:
print(raw_data)
return False
data = BuoyData(units.Units.english)
data.date = pytz.utc.localize(datetime.strptime(raw_data[4], '%H%M %Z %m/%d/%y'))
swell_period_read = False
swell_direction_read = False
swell_component = Swell(units.Units.english)
wind_wave_component = Swell(units.Units.english)
for i in range(5, len(raw_data)):
comps = raw_data[i].split(':')
if len(comps) < 2:
continue
variable = comps[0].lower()
raw_value = comps[1].strip().split()
if variable == 'wind':
data.wind_direction = parse_float(re.findall("\d+", raw_value[1])[0])
data.wind_compass_direction = units.degree_to_direction(data.wind_direction)
data.wind_speed = units.convert(parse_float(raw_value[2]), units.Measurement.speed, units.Units.knots, units.Units.english)
elif variable == 'gust':
data.wind_gust = units.convert(parse_float(raw_value[0]), units.Measurement.speed, units.Units.knots, units.Units.english)
elif variable == 'seas':
data.wave_summary.wave_height = parse_float(raw_value[0])
elif variable == 'peak period':
data.wave_summary.period = parse_float(raw_value[0])
elif variable == 'pres':
data.pressure = parse_float(raw_value[0])
if len(raw_value) > 1:
if 'falling' in raw_value[1]:
data.pressure_tendency = -1.0
elif 'rising' in raw_value[1]:
data.pressure_tendency = 1.0
elif 'steady' in raw_value[1]:
data.pressure_tendency = 0.0
elif variable == 'air temp':
data.air_temperature = parse_float(raw_value[0])
elif variable == 'water temp':
data.water_temperature = parse_float(raw_value[0])
elif variable == 'dew point':
data.dewpoint_temperature = parse_float(raw_value[0])
elif variable == 'swell':
swell_component.wave_height = parse_float(raw_value[0])
elif variable == 'wind wave':
wind_wave_component.wave_height = parse_float(raw_value[0])
elif variable == 'period':
if not swell_period_read:
swell_component.period = parse_float(raw_value[0])
swell_period_read = True
else:
wind_wave_component.period = parse_float(raw_value[0])
elif variable == 'direction':
if not swell_direction_read:
swell_component.compass_direction = raw_value[0]
swell_component.direction = units.direction_to_degree(swell_component.compass_direction)
swell_direction_read = True
else:
wind_wave_component.compass_direction = raw_value[0]
wind_wave_component.direction = units.direction_to_degree(wind_wave_component.compass_direction)
if not math.isnan(swell_component.wave_height):
data.swell_components.append(swell_component)
if not math.isnan(wind_wave_component.wave_height):
data.swell_components.append(wind_wave_component)
if not math.isnan(wind_wave_component.wave_height) and not math.isnan(swell_component.wave_height):
data.interpolate_dominant_wave_direction()
data.find_expiration_date()
if len(self.data) > 0:
self.data[0] = [data] + self.data
else:
self.data = [data]
return True
def parse_meteorological_reading_data(self, raw_data, count_limit):
raw_data = raw_data.split('\n')
if len(raw_data) < 2:
return False
header_lines = 2
data_lines = len(raw_data) - header_lines
if data_lines > count_limit and count_limit > 0:
data_lines = count_limit
for i in range(header_lines, header_lines + data_lines):
raw_data_line = raw_data[i].split()
data = BuoyData(units.Units.metric)
data.date = pytz.utc.localize(datetime(*[int(x) for x in raw_data_line[0:5]]))
data.wind_direction = parse_float(raw_data_line[5])
data.wind_compass_direction = units.degree_to_direction(data.wind_direction)
data.wind_speed = parse_float(raw_data_line[6])
data.wind_gust = parse_float(raw_data_line[7])
data.wave_summary.wave_height = parse_float(raw_data_line[8])
data.wave_summary.period = parse_float(raw_data_line[9])
data.average_period = parse_float(raw_data_line[10])
data.wave_summary.direction = parse_float(raw_data_line[11])
data.wave_summary.compass_direction = units.degree_to_direction(data.wave_summary.direction)
data.pressure = parse_float(raw_data_line[12])
data.air_temperature = parse_float(raw_data_line[13])
data.water_temperature = parse_float(raw_data_line[14])
data.dewpoint_temperature = parse_float(raw_data_line[15])
data.pressure_tendency = parse_float(raw_data_line[17])
data.water_level = units.convert(parse_float(raw_data_line[18]), units.Measurement.length, units.Units.english, units.Units.metric)
data.find_expiration_date()
self.data.append(data)
return True
def parse_detailed_wave_reading_data(self, raw_data, count_limit):
raw_data = raw_data.split('\n')
if len(raw_data) < 2:
return False
header_lines = 2
data_lines = len(raw_data) - header_lines
if data_lines > count_limit and count_limit > 0:
data_lines = count_limit
for i in range(header_lines, header_lines + data_lines):
raw_data_line = raw_data[i].split()
data = BuoyData(units.Units.metric)
swell_component = Swell(units.Units.metric)
wind_wave_component = Swell(units.Units.metric)
data.date = pytz.utc.localize(datetime(*[int(x) for x in raw_data_line[0:5]]))
data.wave_summary.wave_height = parse_float(raw_data_line[5])
swell_component.wave_height = parse_float(raw_data_line[6])
swell_component.period = parse_float(raw_data_line[7])
wind_wave_component.wave_height = parse_float(raw_data_line[8])
wind_wave_component.period = parse_float(raw_data_line[9])
swell_component.compass_direction = raw_data_line[10]
swell_component.direction = units.direction_to_degree(swell_component.compass_direction)
wind_wave_component.compass_direction = raw_data_line[11]
wind_wave_component.direction = units.direction_to_degree(wind_wave_component.compass_direction)
data.steepness = raw_data_line[12]
data.average_period = parse_float(raw_data_line[13])
data.wave_summary.direction = parse_float(raw_data_line[14])
data.wave_summary.compass_direction = units.degree_to_direction(data.wave_summary.direction)
data.swell_components.append(swell_component)
data.swell_components.append(wind_wave_component)
data.interpolate_dominant_wave_period()
data.interpolate_dominant_wave_direction()
data.find_expiration_date()
self.data.append(data)
return True
def parse_wave_spectra_reading_data(self, energy_data, directional_data, count_limit):
energy_data = energy_data.split('\n')
directional_data = directional_data.split('\n')
if len(energy_data) != len(directional_data):
return False
elif len(energy_data) < 2:
return False
header_lines = 1
data_lines = len(energy_data) - header_lines
if data_lines > count_limit and count_limit > 0:
data_lines = count_limit
for i in range(header_lines, header_lines + data_lines):
raw_energy = energy_data[i].strip().replace(')', '').replace('(', '').split()
raw_directional = directional_data[i].strip().replace(')', '').replace('(', '').split()
spectra = BuoySpectra()
data = BuoyData(units.Units.metric)
data.date = pytz.utc.localize(datetime(*[int(x) for x in raw_energy[0:5]]))
for j in range(5, len(raw_directional), 2):
spectra.frequency.append(parse_float(raw_directional[j + 1]))
spectra.angle.append(parse_float(raw_directional[j]))
spectra.energy.append(parse_float(raw_energy[j + 1]))
spectra.seperation_frequency = parse_float(raw_energy[5])
data.wave_spectra = spectra
data.wave_summary = spectra.wave_summary
data.swell_components = spectra.swell_components
data.steepness = steepness(data.wave_summary.wave_height, data.wave_summary.period)
data.average_period = spectra.average_period
data.find_expiration_date()
self.data.append(data)
return True
def fetch_latest_wave_reading(self):
response = requests.get(self.latest_reading_url)
if len(response.text) < 1:
return False
return self.parse_latest_reading_data(response.text)
def fetch_meteorological_reading(self, data_count=20):
response = requests.get(self.meteorological_reading_url)
if len(response.text) < 1:
return False
return self.parse_meteorological_reading_data(response.text, data_count)
def fetch_detailed_wave_reading(self, data_count=20):
response = requests.get(self.detailed_wave_reading_url)
if len(response.text) < 1:
return False
return self.parse_detailed_wave_reading_data(response.text, data_count)
def fetch_wave_spectra_reading(self, data_count=20):
energy_response = requests.get(self.wave_energy_reading_url)
directional_response = requests.get(self.directional_wave_reading_url)
if len(energy_response.text) < 1 or len(directional_response.text) < 1:
return False
return self.parse_wave_spectra_reading_data(energy_response.text, directional_response.text, data_count)
def data_index_for_date(self, datetime):
if len(self.data) < 1:
return None
min_duration = (datetime - self.data[0].date).seconds
min_index = 0
for i in range(1, len(self.data)):
duration = (datetime - self.data[i].date).seconds
if abs(duration) < min_duration:
min_duration = duration
min_index = i
return min_index, min_duration