opentideconstants 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,192 @@
1
+ require_relative "predict/astro"
2
+ require_relative "predict/harmonic"
3
+ require_relative "predict/results"
4
+ require_relative "predict/engine"
5
+ require_relative "predict/local"
6
+
7
+ class OpenTideConstants
8
+ class Release
9
+ def astro_table(id = nil)
10
+ check_open!
11
+ Predict.astro_table(self, id)
12
+ end
13
+ def heights(station, start:, stop:, step_s: 600, datum: nil, set_id: nil, offset_id: nil, units: "meters", profile: nil)
14
+ check_open!
15
+ set_id, offset_id, profile_info = resolve_profile(station, profile, set_id, offset_id)
16
+ Predict.validate_units(units, "meters")
17
+ first, last = Predict.window(start, stop, step_s)
18
+ raise InvalidArgumentError, "step_s must be a positive integer" unless step_s.is_a?(Integer) && step_s > 0
19
+ c = Predict.context(self, station, "water_level", set_id, datum, series: true, offset_id: offset_id)
20
+ c.profile_info = profile_info
21
+ items = []
22
+ first.step(last - 1, step_s) { |t| items << HeightPoint.new(time: Predict.utc(t), height_m: c.total.value(t)) } if first < last
23
+ Predict.result(self, c, HeightSeries, items, datum, units: units)
24
+ end
25
+ def heights_at(station, times, datum: nil, set_id: nil, offset_id: nil, units: "meters", profile: nil)
26
+ check_open!
27
+ set_id, offset_id, profile_info = resolve_profile(station, profile, set_id, offset_id)
28
+ Predict.validate_units(units, "meters")
29
+ raise InvalidArgumentError, "times must be an Array of Time" unless times.is_a?(Array)
30
+ seconds = times.map { |t| Predict.instant(t) }
31
+ c = Predict.context(self, station, "water_level", set_id, datum, series: true, offset_id: offset_id)
32
+ c.profile_info = profile_info
33
+ Predict.result(self, c, HeightSeries, seconds.map { |t| HeightPoint.new(time: Predict.utc(t), height_m: c.total.value(t)) }, datum, units: units)
34
+ end
35
+ def extremes(station, start:, stop:, datum: nil, set_id: nil, offset_id: nil, units: "meters", profile: nil)
36
+ check_open!
37
+ default_selection = set_id.nil? && offset_id.nil? && profile.nil?
38
+ set_id, offset_id, profile_info = resolve_profile(station, profile, set_id, offset_id)
39
+ Predict.validate_units(units, "meters")
40
+ first, last = Predict.window(start, stop)
41
+ event_offset = Predict.default_event_offset_id(self, station) if default_selection && set_id.nil? && offset_id.nil?
42
+ offset_id = event_offset if event_offset
43
+ c = Predict.context(self, station, "water_level", set_id, datum, offset_id: offset_id)
44
+ c.event_default = !event_offset.nil?
45
+ c.profile_info = profile_info
46
+ if c.method == "offsets" && first != last
47
+ off = c.offsets
48
+ margin = [120.0, [(off["time_offset_high_min"] || 0).abs, (off["time_offset_low_min"] || 0).abs].max + 60.0].max * 60.0
49
+ candidates = Predict.events(c.total, first - margin, last + margin).map do |root, kind, value|
50
+ t = root + (off["time_offset_#{kind}_min"] || 0) * 60.0
51
+ adjustment = off["height_offset_#{kind}"]
52
+ height = off["height_adjusted_type"] == "R" ? value * (adjustment.nil? ? 1.0 : adjustment) : value + (adjustment || 0.0)
53
+ [t, kind, height + off["_subterm"]]
54
+ end.sort_by { |row| (row[0] + 0.5).floor }
55
+ else
56
+ candidates = Predict.events(c.total, first, last)
57
+ end
58
+ items = candidates.filter_map do |t, kind, value|
59
+ whole = (t + 0.5).floor
60
+ Extreme.new(time: Predict.utc(whole), type: kind.to_sym, height_m: value) if first <= whole && whole < last
61
+ end
62
+ Predict.result(self, c, ExtremeList, items, datum, units: units)
63
+ end
64
+ def currents(station, start:, stop:, step_s: 600, bin_number: nil, set_id: nil, units: "m/s", profile: nil)
65
+ check_open!
66
+ set_id, _, profile_info = resolve_profile(station, profile, set_id)
67
+ Predict.validate_units(units, "m/s")
68
+ first, last = Predict.window(start, stop, step_s)
69
+ raise InvalidArgumentError, "step_s must be a positive integer" unless step_s.is_a?(Integer) && step_s > 0
70
+ c = Predict.context(self, station, "current", set_id, nil, bin_number, series: true)
71
+ c.profile_info = profile_info
72
+ theta = c.current_bin.fetch("azimuth_deg", 0.0) * Predict::RAD
73
+ items = []
74
+ if first < last
75
+ first.step(last - 1, step_s) do |t|
76
+ major, minor = c.total.value(t), c.minor.value(t)
77
+ east = major * Math.sin(theta) + minor * Math.sin(theta + Math::PI / 2.0)
78
+ north = major * Math.cos(theta) + minor * Math.cos(theta + Math::PI / 2.0)
79
+ speed = Math.hypot(east, north)
80
+ direction = speed < 1e-9 ? nil : Predict.angle(Math.atan2(east, north) / Predict::RAD)
81
+ items << CurrentPoint.new(time: Predict.utc(t), velocity_major_ms: c.sigma * major, velocity_minor_ms: c.sigma * minor, speed_ms: speed, direction_deg: direction)
82
+ end
83
+ end
84
+ Predict.result(self, c, CurrentSeries, items, units: units)
85
+ end
86
+ def current_events(station, start:, stop:, bin_number: nil, set_id: nil, units: "m/s", profile: nil)
87
+ check_open!
88
+ set_id, _, profile_info = resolve_profile(station, profile, set_id)
89
+ Predict.validate_units(units, "m/s")
90
+ first, last = Predict.window(start, stop)
91
+ c = Predict.context(self, station, "current", set_id, nil, bin_number)
92
+ c.profile_info = profile_info
93
+ if c.method == "offsets"
94
+ entry = c.offsets
95
+ kinds = %w[max_flood max_ebb slack_before_flood slack_before_ebb]
96
+ kept = kinds.select { |k| !entry["time_adj_#{k}_min"].nil? && (k.start_with?("slack") || !entry[k == "max_flood" ? "flood_amp_ratio" : "ebb_amp_ratio"].nil?) }
97
+ c.omitted = (kinds - kept).map(&:to_sym)
98
+ margin = [120.0, (kept.map { |k| entry["time_adj_#{k}_min"].abs }.max || 0.0) + 60.0].max * 60.0
99
+ candidates = first == last ? [] : Predict.current_events(c, first - margin, last + margin)
100
+ candidates = candidates.filter_map do |t, k, value, direction|
101
+ next unless kept.include?(k)
102
+ t += entry["time_adj_#{k}_min"] * 60.0
103
+ if %w[max_flood max_ebb].include?(k)
104
+ value *= entry[k == "max_flood" ? "flood_amp_ratio" : "ebb_amp_ratio"]
105
+ direction = entry[k == "max_flood" ? "mean_flood_dir_deg" : "mean_ebb_dir_deg"]
106
+ end
107
+ [t, k, value, direction]
108
+ end.sort_by { |row| (row[0] + 0.5).floor }
109
+ else
110
+ candidates = Predict.current_events(c, first, last)
111
+ end
112
+ items = candidates.filter_map do |t, k, value, direction|
113
+ whole = (t + 0.5).floor
114
+ CurrentEvent.new(time: Predict.utc(whole), type: k.to_sym, velocity_ms: value, direction_deg: direction) if first <= whole && whole < last
115
+ end
116
+ Predict.result(self, c, CurrentEventList, items, units: units)
117
+ end
118
+ def resolve_profile(station, profile, set_id, offset_id = nil)
119
+ Predict.options(set_id)
120
+ raise InvalidArgumentError, "offset_id must be a string" unless offset_id.nil? || offset_id.is_a?(String)
121
+ raise InvalidArgumentError, "set_id and offset_id are mutually exclusive" if set_id && offset_id
122
+ unless profile.nil? || %w[observed-best official].include?(profile)
123
+ raise InvalidArgumentError, "profile must be observed-best or official"
124
+ end
125
+ selected = Predict.station_for(self, station)
126
+ bindings = selected.raw["prediction_profiles"]
127
+ resolved = profile || (set_id.nil? && offset_id.nil? ? "observed-best" : nil)
128
+ binding = bindings && resolved && bindings[resolved]
129
+ raise InvalidReleaseError, "prediction profile binding is missing" if bindings && resolved && binding.nil?
130
+ target = binding ? binding["set_id"] : (resolved ? selected.raw["recommended_set_id"] : set_id)
131
+ target_offset = binding && binding["offset_id"]
132
+ implicit_blocked = profile.nil? && set_id.nil? && offset_id.nil? && binding && target && @commercial_only &&
133
+ (bound = selected.constant_set(target)) && !commercial_set?(bound.raw)
134
+ if profile && set_id && set_id != target
135
+ raise InvalidArgumentError, "set_id does not match profile #{resolved}"
136
+ end
137
+ if profile && offset_id && offset_id != target_offset
138
+ raise InvalidArgumentError, "offset_id does not match profile #{resolved}"
139
+ end
140
+ info = { requested_profile: resolved, resolved_profile: implicit_blocked ? nil : resolved,
141
+ selection_status: resolved ? (binding && !implicit_blocked ? binding["selection_status"] : "unmeasured") : nil,
142
+ selection_evidence: binding && !implicit_blocked ? binding["selection_evidence"] : nil }
143
+ [set_id || (implicit_blocked ? nil : binding && target), offset_id || target_offset, info]
144
+ end
145
+ private :resolve_profile
146
+ def datum_sources(station, set_id: nil)
147
+ check_open!
148
+ station = Predict.station_for(self, station)
149
+ Predict.options(set_id)
150
+ constants = Predict.chosen(self, station, set_id)
151
+ Predict.datum_sources(self, station, constants, set_id)
152
+ end
153
+ def datums(station, set_id: nil, all_sets: false)
154
+ check_open!
155
+ station = Predict.station_for(self, station)
156
+ Predict.options(set_id)
157
+ raise InvalidArgumentError, "all_sets must be a boolean" unless all_sets == true || all_sets == false
158
+ if all_sets
159
+ infos = station.constant_sets(include_no_constants: true).map { |s| Predict.datum_info(s.set_id, Predict.retained_datum(self, station, s)) }
160
+ offsets = station.raw["subordinate_offsets"]
161
+ if offsets
162
+ effective = Predict.retained_datum(self, station, offsets: offsets)
163
+ infos << Predict.datum_info(nil, effective) if effective["basis"].values.any? { |b| b["_retained"] }
164
+ end
165
+ return infos.freeze
166
+ end
167
+ constants = Predict.chosen(self, station, set_id)
168
+ return Predict.datum_info(constants.set_id, Predict.retained_datum(self, station, constants)) if constants
169
+ offsets = station.raw["subordinate_offsets"]
170
+ offsets && Predict.datum_info(nil, Predict.retained_datum(self, station, offsets: offsets))
171
+ end
172
+ def ranges(station, set_id: nil)
173
+ check_open!
174
+ info = datums(station, set_id: set_id)
175
+ levels = info.is_a?(DatumInfo) ? info.levels.to_h { |l| [l.key, l] } : {}
176
+ difference = lambda do |a, b|
177
+ high, low = levels[a], levels[b]
178
+ next nil unless high && low && high.value_m && low.value_m && high.kind && high.kind == low.kind && high.epoch == low.epoch
179
+ high.value_m - low.value_m
180
+ end
181
+ Ranges.new(mn: difference.call("mhw", "mlw"), gt: difference.call("mhhw", "mllw"), dhq: difference.call("mhhw", "mhw"), dlq: difference.call("mlw", "mllw"))
182
+ end
183
+ end
184
+ %i[heights heights_at extremes currents current_events datums datum_sources ranges].each do |name|
185
+ define_method(name) do |*args, **kw, &blk|
186
+ check_open!
187
+ maybe_auto_update
188
+ @release.public_send(name, *args, **kw, &blk)
189
+ end
190
+ end
191
+ private_constant :Predict
192
+ end