-
Notifications
You must be signed in to change notification settings - Fork 2
/
webcaltides.rb
450 lines (341 loc) · 14.9 KB
/
webcaltides.rb
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
##
## Primary library of functions. Included on Server.
##
require_relative 'clients/base'
require_relative 'clients/noaa_tides'
require_relative 'clients/chs_tides'
require_relative 'clients/noaa_currents'
module WebCalTides
extend self
include Clients::TimeWindow
def settings; return Server.settings; end
def logger; $LOG; end
##
## Clients
##
def tide_clients(provider = nil)
@tide_clients ||= {
noaa: Clients::NoaaTides.new(logger),
chs: Clients::ChsTides.new(logger)
}
provider ? @tide_clients[provider.to_sym] : @tide_clients
end
def current_clients(provider = nil)
@current_clients ||= {
noaa: Clients::NoaaCurrents.new(logger)
}
provider ? @current_clients[provider.to_sym] : @current_clients
end
##
## Util
##
def convert_depth_to_correct_units(val, curr_units, desired_units)
if desired_units == curr_units
val
elsif desired_units == 'ft' # convert to feet
(val.to_f * 3.28084).round(3)
else # convert to meters
(val.to_f / 3.28084).round(3)
end
end
# Handles decimal (-)X.YYY or deg/min/sec format:
# Supported deg/min/sec format:
# "1°2.3" or "1'2.3" with explicit negative
# "1°2.3N" or "1'2.3W" with implicit negative (S+E -> -)
def parse_gps(str)
if str.match(/\d['°]/)
str = str# blindly fix NSEW, no-op if DNE
.gsub(/(\d['°])(\s*)/, '\1') # remove any space b/w deg
.gsub(/([^\s])\s+([NSEW])/, '\1\2') # remove any space b/w cardinal
.gsub(/([^\s]+)[SE]/, '-\1') # if SE exists, remove + convert to -
.gsub(/([^\s]+)[NW]/, '\1') # if NW exists, remove + ignore (+)
.gsub(/([-]*)(\d+)['°]\s*(\d+)\.(\d+)/) do |m| # Convert to decimal
$1 + ($2.to_f + $3.to_f/60 + $4.to_f/3600).to_s
end
end
# In decimal form now
res = str.split(/[, ]+/)
return nil if res.length != 2 or
res.any? { |s| s.scan(/^[\d\.-]+$/).empty? } or
!res[0].to_f.between?(-90,90) or
!res[1].to_f.between?(-180,180)
return res
end
def timezone_for(lat, long)
filename = "#{settings.cache_dir}/tzs.json"
@tzcache ||= begin
if File.exist? filename
logger.debug "reading #{filename}"
json = File.read(filename)
logger.debug "parsing tzcache"
data = JSON.parse(json) rescue {}
else
logger.debug "initializing #{filename}"
FileUtils.touch filename
data = {}
end
end
key = "#{lat} #{long}"
return @tzcache[key] ||= begin
logger.debug "looking up tz for GPS #{key}"
i = 0
begin
i += 1
tz = Timezone.lookup(lat, long)
rescue Timezone::Error::GeoNames
sleep(i)
retry unless i >= 3
end
logger.debug "GPS #{key} => #{tz.name}"
@tzcache[key] = tz.name
logger.debug "updating tzcache at #{filename}"
File.write(filename, @tzcache.to_json)
tz.name
end
end
##
## Tides
##
# Cache quarterly / every three months
def tide_station_cache_file
now = Time.current.utc
datestamp = now.strftime("%YQ#{now.quarter}")
"#{settings.cache_dir}/tide_stations_v#{DataModels::Station.version}_#{datestamp}.json"
end
def cache_tide_stations(at:tide_station_cache_file, stations:[])
# stations: is used in the re-cache scenario
tide_clients.each_value { |c| stations.concat(c.tide_stations) } if stations.empty?
logger.debug "storing tide station list at #{at}"
File.write(at, stations.map(&:to_h).to_json )
return stations.length > 0
end
def tide_stations
cache_file = tide_station_cache_file
File.exist? cache_file or cache_tide_stations(at:cache_file) and @tide_stations = nil
return @tide_stations ||= begin
logger.debug "reading #{cache_file}"
json = File.read(cache_file)
logger.debug "parsing tide station list"
data = JSON.parse(json) rescue []
data.map { |js| DataModels::Station.from_hash(js) }
end
end
# This is primarily for CHS tide stations, whose metadata is such a broken mess as to not
# reliably indicate, in any way, whether the station is producing tide data or not. See
# chs_tides.rb for details.
def remove_tide_station(station_id)
@tide_stations.delete_if { |s| s.id == station_id }
cache_tide_stations(stations:@tide_stations)
end
def tide_station_for(id)
return nil if id.blank?
return tide_stations.find { |s| s.id == id }
end
# nil == any, units == [ mi, km ]
def find_tide_stations(by:nil, within:nil, units:'mi')
by ||= [""]
by &&= Array(by).map(&:downcase)
logger.debug("finding tide stations by #{by} within '#{within}' #{units}")
by_stations = tide_stations.select do |s|
by.any? do |b|
s.id.downcase == b ||
s.alternate_names.any? { |n| (n.downcase.include?(b) rescue false) } ||
(s.region.downcase.include?(b) rescue false) ||
(s.name.downcase.include?(b) rescue false) ||
s.public_id.downcase.include?(b) rescue false
end
end
# can only do radius search with one result, ignore otherwise
return by_stations unless within and by_stations.size == 1
station = by_stations.first
return find_tide_stations_by_gps(station.lat, station.lon, within:within, units:units)
end
def find_tide_stations_by_gps(lat, long, within:nil, units:'mi')
within = within.to_i
return tide_stations.select do |s|
Geocoder::Calculations.distance_between([lat, long], [s.lat,s.lon], units: units.to_sym) <= within
end
end
def cache_tide_data_for(station, at:, around:)
return false unless station
if tide_data = tide_clients(station.provider).tide_data_for(station, around)
logger.debug "storing tide data at #{at}"
File.write(at, tide_data.map(&:to_h).to_json)
end
return tide_data && tide_data.length > 0
end
def tide_data_for(station, around: Time.current.utc)
return nil unless station
datestamp = around.utc.strftime("%Y%m")
filename = "#{settings.cache_dir}/tides_v#{DataModels::TideData.version}_#{station.id}_#{datestamp}.json"
return nil unless File.exist? filename or cache_tide_data_for(station, at:filename, around:around)
logger.debug "reading #{filename}"
json = File.read(filename)
logger.debug "parsing tides for #{station.id}"
data = JSON.parse(json) rescue []
return data.map{ |js| DataModels::TideData.from_hash(js) }
end
def tide_calendar_for(id, around: Time.current.utc, units: 'imperial')
depth_units = units == 'imperial' ? 'ft' : 'm'
station = tide_station_for(id) or return nil
data = tide_data_for(station, around: around)
return nil unless data
cal = Icalendar::Calendar.new
cal.x_wr_calname = station.name.titleize
logger.debug "generating tide calendar for #{station.name}"
data.each do |tide|
title = "#{tide.type} Tide #{convert_depth_to_correct_units(tide.prediction, tide.units, depth_units)} #{depth_units}"
cal.event do |e|
e.summary = title
e.dtstart = Icalendar::Values::DateTime.new(tide.time, tzid: 'GMT')
e.dtend = Icalendar::Values::DateTime.new(tide.time, tzid: 'GMT')
e.url = tide.url
e.location = station.location
end
end
cal.define_singleton_method(:station) { station }
cal.define_singleton_method(:location) { station.location }
logger.info "tide calendar for #{station.name} generated with #{cal.events.length} events"
return cal
end
##
## Currents
##
def current_station_cache_file
now = Time.current.utc
datestamp = now.strftime("%YQ#{now.quarter}")
"#{settings.cache_dir}/current_stations_v#{DataModels::Station.version}_#{datestamp}.json"
end
def cache_current_stations(at:current_station_cache_file, stations: [])
current_clients.each_value { |c| stations.concat(c.current_stations) } if stations.empty?
logger.debug "storing current station list at #{at}"
File.write(at, stations.map(&:to_h).to_json)
return stations.length > 0
end
def current_stations
cache_file = current_station_cache_file
File.exist? cache_file or cache_current_stations(at:cache_file) and @current_stations = nil
return @current_stations ||= begin
logger.debug "reading #{cache_file}"
json = File.read(cache_file)
logger.debug "parsing current station list"
data = JSON.parse(json) rescue []
data.map { |js| DataModels::Station.from_hash(js) }
end
end
def remove_current_station(station_id)
@current_stations.delete_if { |s| s.id == station_id }
cache_current_stations(stations:@current_stations)
end
def current_station_for(id)
return nil if id.blank?
return current_stations.select { |s| s.id == id || s.bid == id }.first
end
# nil == any, units == [ mi, km ]
def find_current_stations(by:nil, within:nil, units:'mi')
by ||= [""]
by &&= Array(by).map(&:downcase)
logger.debug "finding current stations by #{by} within '#{within}' #{units}"
by_stations = current_stations.select do |s|
by.any? do |b|
(s.bid.downcase.start_with?(b) rescue false) ||
(s.id.downcase.start_with?(b) rescue false) ||
(s.id.downcase.include?(b) rescue false) ||
(s.name.downcase.include?(b)) rescue false
end
end
# can only do radius search with one result, ignore otherwise
return by_stations unless within and by_stations.size == 1
station = by_stations.first
return find_current_stations_by_gps(station.lat, station.lon, within:within, units:units)
end
def find_current_stations_by_gps(lat, long, within:nil, units:'mi')
within = within.to_i
return current_stations.select do |s|
Geocoder::Calculations.distance_between([lat, long], [s.lat,s.lon], units: units.to_sym) <= within
end
end
def cache_current_data_for(station, at:, around:)
return false unless station
if current_data = current_clients(station.provider).current_data_for(station, around)
logger.debug "storing current data at #{at}"
File.write(at, current_data.map(&:to_h).to_json)
end
return current_data && current_data.length > 0
end
def current_data_for(station, around: Time.current.utc)
return nil unless station
datestamp = around.utc.strftime("%Y%m") # 202312
filename = "#{settings.cache_dir}/currents_v#{DataModels::CurrentData.version}_#{station.bid}_#{datestamp}.json"
return nil unless File.exist? filename or cache_current_data_for(station, at:filename, around:around)
logger.debug "reading #{filename}"
json = File.read(filename)
logger.debug "parsing currents for #{station.bid}"
data = JSON.parse(json) rescue []
return data.map { |jc| DataModels::CurrentData.from_hash(jc) }
end
def current_calendar_for(id, around: Time.current.utc)
station = current_station_for(id) or return nil
data = current_data_for(station, around: around)
return nil unless data
cal = Icalendar::Calendar.new
cal.x_wr_calname = station.name.titleize
location = "#{station.name} (#{station.bid})"
logger.debug "generating current calendar for #{location}"
data.each do |current|
date = current.time.strftime("%Y-%m-%d")
title = case current.type
when "ebb" then "Ebb #{current.velocity_major.to_f.abs}kts #{current.mean_ebb_dir}T #{current.depth}ft"
when "flood" then "Flood #{current.velocity_major}kts #{current.mean_flood_dir}T #{current.depth}ft"
when "slack" then "Slack"
end
cal.event do |e|
e.summary = title
e.dtstart = Icalendar::Values::DateTime.new(current.time, tzid: 'GMT')
e.dtend = Icalendar::Values::DateTime.new(e.dtstart, tzid: 'GMT')
e.url = station.url + "?id=" + station.bid + "&d=" + date
e.location = location if location
end
end
cal.define_singleton_method(:station) { station }
cal.define_singleton_method(:location) { location }
logger.info "current calendar for #{location} generated with #{cal.events.length} events"
return cal
end
##
## Solar
##
def solar_calendar_for(calendar, around:Time.current.utc)
cal = Icalendar::Calendar.new
cal.x_wr_calname = "Solar Events"
from = beginning_of_window(around).strftime("%Y%m%d")
to = end_of_window(around).strftime("%Y%m%d")
station = calendar.station
location = calendar.location
logger.debug "generating solar calendar for #{from}-#{to}"
(Date.parse(from)..Date.parse(to)).each do |date|
tz = timezone_for(station.lat, station.lon)
calc = SolarEventCalculator.new(date, station.lat, station.lon)
sunrise = calc.compute_official_sunrise(tz)
sunset = calc.compute_official_sunset(tz)
# I dunno why tzid: GMT is correct vs. tzid: tz, but it works..
cal.event do |e|
e.summary = "Sunrise"
e.dtstart = Icalendar::Values::DateTime.new(sunrise, tzid: 'GMT')
e.dtend = Icalendar::Values::DateTime.new(e.dtstart, tzid: 'GMT')
e.location = location if location
end
cal.event do |e|
e.summary = "Sunset"
e.dtstart = Icalendar::Values::DateTime.new(sunset, tzid: 'GMT')
e.dtend = Icalendar::Values::DateTime.new(e.dtstart, tzid: 'GMT')
e.location = location if location
end
end
logger.info "solar calendar for #{from}-#{to} generated with #{cal.events.length} events"
cal.events.each do |e|
calendar.add_event(e)
end
return cal
end
end