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.
- checksums.yaml +4 -4
- data/README.md +132 -3
- data/lib/opentideconstants/bgzf.rb +436 -0
- data/lib/opentideconstants/cache.rb +559 -0
- data/lib/opentideconstants/cache_json.rb +67 -0
- data/lib/opentideconstants/canonical.rb +756 -0
- data/lib/opentideconstants/canonical_types.rb +2537 -0
- data/lib/opentideconstants/client.rb +1135 -0
- data/lib/opentideconstants/downloader.rb +400 -0
- data/lib/opentideconstants/errors.rb +210 -0
- data/lib/opentideconstants/fold.rb +24 -0
- data/lib/opentideconstants/historical_fingerprint.rb +58 -0
- data/lib/opentideconstants/http.rb +483 -0
- data/lib/opentideconstants/models.rb +635 -0
- data/lib/opentideconstants/name_fold.json +949 -0
- data/lib/opentideconstants/predict/astro.rb +85 -0
- data/lib/opentideconstants/predict/daily-facts.json +1 -0
- data/lib/opentideconstants/predict/daily.rb +62 -0
- data/lib/opentideconstants/predict/engine.rb +506 -0
- data/lib/opentideconstants/predict/harmonic.rb +228 -0
- data/lib/opentideconstants/predict/local.rb +49 -0
- data/lib/opentideconstants/predict/results.rb +357 -0
- data/lib/opentideconstants/predict/retained.rb +487 -0
- data/lib/opentideconstants/predict.rb +192 -0
- data/lib/opentideconstants/reader.rb +493 -0
- data/lib/opentideconstants/release.rb +1531 -0
- data/lib/opentideconstants/version.rb +15 -0
- data/lib/opentideconstants.rb +24 -3
- data/supported-formats.json +5 -0
- metadata +37 -5
|
@@ -0,0 +1,635 @@
|
|
|
1
|
+
require "date"
|
|
2
|
+
require "time"
|
|
3
|
+
|
|
4
|
+
class OpenTideConstants
|
|
5
|
+
# Helpers shared by the data objects. Every object is frozen and keeps the
|
|
6
|
+
# parsed JSON object as #raw (a deep-frozen Hash) where the API manifest
|
|
7
|
+
# lists one, so a caller can read
|
|
8
|
+
# fields that a newer minor format adds.
|
|
9
|
+
module Util
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
def deep_freeze(v)
|
|
13
|
+
case v
|
|
14
|
+
when Hash then v.each_value { |x| deep_freeze(x) }
|
|
15
|
+
when Array then v.each { |x| deep_freeze(x) }
|
|
16
|
+
end
|
|
17
|
+
v.freeze
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# An enum value as a symbol; a value this SDK does not know is :other.
|
|
21
|
+
def enum(value, known)
|
|
22
|
+
return nil if value.nil?
|
|
23
|
+
known.include?(value) ? value.to_sym : :other
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
TIME_FIELDS = /\A(\d{4})-(\d{2})-(\d{2})[Tt ](\d{2}):(\d{2}):(\d{2})/
|
|
27
|
+
|
|
28
|
+
# An RFC 3339 time in UTC. Impossible calendar fields, hour 24, a leap
|
|
29
|
+
# second :60 and a time out of years 1 to 9999 in UTC are malformed:
|
|
30
|
+
# Time.iso8601 would roll them over into another date.
|
|
31
|
+
def time(value)
|
|
32
|
+
return nil if value.nil?
|
|
33
|
+
raise InvalidReleaseError, "bad time: #{value.inspect}" unless value.is_a?(String)
|
|
34
|
+
y, mo, d, h, mi, s = TIME_FIELDS.match(value)&.captures&.map(&:to_i)
|
|
35
|
+
unless y && Date.valid_date?(y, mo, d, Date::GREGORIAN) && h <= 23 && mi <= 59 && s <= 59
|
|
36
|
+
raise InvalidReleaseError, "bad time: #{value.inspect}"
|
|
37
|
+
end
|
|
38
|
+
t = Time.iso8601(value).utc
|
|
39
|
+
raise InvalidReleaseError, "bad time: #{value.inspect}" unless (1..9999).cover?(t.year)
|
|
40
|
+
t.freeze
|
|
41
|
+
rescue ArgumentError
|
|
42
|
+
raise InvalidReleaseError, "bad time: #{value.inspect}"
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
SURROGATE_ESCAPE = /\\u[dD][89a-fA-F]/.freeze
|
|
46
|
+
# One escape of a JSON text: a surrogate pair, a lone surrogate (group
|
|
47
|
+
# 1), another \u escape, or any other escaped character (so an escaped
|
|
48
|
+
# backslash before "ud800" is not read as an escape).
|
|
49
|
+
ESCAPE = /\\(?:u[dD][89abAB]\h{2}\\u[dD][c-fC-F]\h{2}|(u[dD][89a-fA-F]\h{2})|u\h{4}|.)/m.freeze
|
|
50
|
+
|
|
51
|
+
# invalid_release when a \u escape of a JSON text makes an unpaired
|
|
52
|
+
# UTF-16 surrogate. Checked on the text, before
|
|
53
|
+
# JSON.parse: some versions of the json gem replace a lone surrogate
|
|
54
|
+
# with "?" (and drop the next character) instead of raising.
|
|
55
|
+
def no_lone_surrogate!(text)
|
|
56
|
+
return unless SURROGATE_ESCAPE.match?(text)
|
|
57
|
+
text.scan(ESCAPE) do |lone|
|
|
58
|
+
raise InvalidReleaseError, "a string with an unpaired surrogate (\\#{lone[0]})" if lone[0]
|
|
59
|
+
end
|
|
60
|
+
nil
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# A build-only time (provenance.build): a malformed one is nil, not a
|
|
64
|
+
# failed station.
|
|
65
|
+
def time_or_nil(value)
|
|
66
|
+
time(value)
|
|
67
|
+
rescue InvalidReleaseError
|
|
68
|
+
nil
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
ENUMS = {
|
|
73
|
+
fallback_reason: ["time_base_disputed", "time_base_unresolved", "refused_fit", "short_record_uncalibrated", "short_record_long_period", "published_full_list", "reliability_withheld", "licence_reading", "datum_gate_failed", "not_in_otc_input", "historical_offsets"],
|
|
74
|
+
source_role: ["gauge_station", "published_harmonic_record", "model", "reference"],
|
|
75
|
+
source_disposition: ["metadata_only", "not_fitted", "licence_restricted", "unavailable", "retained"],
|
|
76
|
+
extreme_type: ["high", "low"],
|
|
77
|
+
current_event_type: ["max_flood", "max_ebb", "slack_before_flood", "slack_before_ebb"],
|
|
78
|
+
prediction_method: ["harmonic", "folded_offsets", "offsets"],
|
|
79
|
+
kind: %w[tide current],
|
|
80
|
+
station_type: %w[reference subordinate],
|
|
81
|
+
station_status: %w[active removed],
|
|
82
|
+
source_type: %w[official gauge model],
|
|
83
|
+
quantity: %w[water_level current],
|
|
84
|
+
removed_reason: %w[merged duplicate],
|
|
85
|
+
qc_status: %w[ok flagged no_constants],
|
|
86
|
+
phase_reference: %w[greenwich_utc local],
|
|
87
|
+
nodal_handling: %w[f_u_at_prediction none],
|
|
88
|
+
height_adjusted_type: %w[R A],
|
|
89
|
+
dropped_reason: %w[rayleigh noise long_period_rule non_tidal_rule convention other],
|
|
90
|
+
qc_flag: ["time_base", "broken_record", "microtidal", "non_tidal_signal", "short_record", "sibling_disagreement", "carried_over", "fallback:time_base_disputed", "fallback:time_base_unresolved", "fallback:refused_fit", "fallback:short_record_uncalibrated", "fallback:short_record_long_period", "fallback:published_full_list", "fallback:reliability_withheld", "fallback:licence_reading", "fallback:datum_gate_failed", "fallback:not_in_otc_input", "fallback:historical_offsets"],
|
|
91
|
+
time_base_code: %w[verified corrected unverified disputed],
|
|
92
|
+
alias_system: %w[noaa gesla ticon xtide kartverket slackwater webcaltides linz shom pegelonline rws mi dmi smhi fmi
|
|
93
|
+
uhslc]
|
|
94
|
+
}.freeze
|
|
95
|
+
|
|
96
|
+
# One file of a release: name, url, size and sha256.
|
|
97
|
+
class FileInfo
|
|
98
|
+
attr_reader :name, :url, :size, :sha256
|
|
99
|
+
|
|
100
|
+
def initialize(name:, url: nil, size: nil, sha256: nil)
|
|
101
|
+
@name = name.freeze
|
|
102
|
+
@url = url&.freeze
|
|
103
|
+
@size = size
|
|
104
|
+
@sha256 = sha256&.freeze
|
|
105
|
+
freeze
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# An entry of the latest pointer or the release index; no station data.
|
|
110
|
+
class ReleaseInfo
|
|
111
|
+
attr_reader :datestamp, :format_version, :concept_doi, :zenodo_version_doi, :content_sha256, :files
|
|
112
|
+
|
|
113
|
+
def initialize(raw, files)
|
|
114
|
+
Util.deep_freeze(raw)
|
|
115
|
+
@datestamp = raw["datestamp"]
|
|
116
|
+
@format_version = raw["format_version"]
|
|
117
|
+
@concept_doi = raw["concept_doi"]
|
|
118
|
+
@zenodo_version_doi = raw["zenodo_version_doi"]
|
|
119
|
+
@content_sha256 = raw["content_sha256"]
|
|
120
|
+
@files = files.freeze
|
|
121
|
+
freeze
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
class Licence
|
|
126
|
+
attr_reader :licence_id, :spdx, :provider, :citation, :attribution, :url, :commercial_use, :restriction,
|
|
127
|
+
:terms_url, :raw
|
|
128
|
+
|
|
129
|
+
def initialize(raw)
|
|
130
|
+
@raw = Util.deep_freeze(raw)
|
|
131
|
+
@licence_id = raw["licence_id"]
|
|
132
|
+
@spdx = raw["spdx"]
|
|
133
|
+
@provider = raw["provider"]
|
|
134
|
+
@citation = raw["citation"]
|
|
135
|
+
@attribution = raw["attribution"]
|
|
136
|
+
@url = raw["url"]
|
|
137
|
+
@commercial_use = raw["commercial_use"]
|
|
138
|
+
@restriction = raw["restriction"]
|
|
139
|
+
@terms_url = raw["terms_url"]
|
|
140
|
+
freeze
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# The load rules of the licence fields:
|
|
144
|
+
# commercial_use is true or false (absent or null is invalid);
|
|
145
|
+
# restriction and terms_url, when present, are Strings (a JSON null is
|
|
146
|
+
# not one); a non-commercial licence has both, and an spdx,
|
|
147
|
+
# which the non-commercial notice prints.
|
|
148
|
+
def self.check!(raw)
|
|
149
|
+
lid = raw["licence_id"]
|
|
150
|
+
cu = raw["commercial_use"]
|
|
151
|
+
raise InvalidReleaseError, "licence #{lid}: commercial_use must be true or false" unless cu == true || cu == false
|
|
152
|
+
%w[restriction terms_url].each do |k|
|
|
153
|
+
raise InvalidReleaseError, "licence #{lid}: #{k} must be a String" if raw.key?(k) && !raw[k].is_a?(String)
|
|
154
|
+
end
|
|
155
|
+
return if cu
|
|
156
|
+
%w[restriction terms_url spdx].each do |k|
|
|
157
|
+
raise InvalidReleaseError, "licence #{lid}: a licence with commercial_use false needs #{k}" unless raw[k].is_a?(String)
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
class Convention
|
|
163
|
+
attr_reader :convention_id, :phase_reference, :utc_offset_hours, :v0_model, :nodal_handling,
|
|
164
|
+
:nodal_formula_ids, :constituent_table_version, :tables_sha256, :canary, :raw
|
|
165
|
+
|
|
166
|
+
def initialize(raw)
|
|
167
|
+
@raw = Util.deep_freeze(raw)
|
|
168
|
+
@convention_id = raw["convention_id"]
|
|
169
|
+
@phase_reference = Util.enum(raw["phase_reference"], ENUMS[:phase_reference])
|
|
170
|
+
@utc_offset_hours = raw["utc_offset_hours"]
|
|
171
|
+
@v0_model = raw["v0_model"]
|
|
172
|
+
@nodal_handling = Util.enum(raw["nodal_handling"], ENUMS[:nodal_handling])
|
|
173
|
+
@nodal_formula_ids = raw["nodal_formula_ids"]
|
|
174
|
+
@constituent_table_version = raw["constituent_table_version"]
|
|
175
|
+
@tables_sha256 = raw["tables_sha256"]
|
|
176
|
+
@canary = raw.key?("canary") ? raw["canary"] : raw["validation"]
|
|
177
|
+
freeze
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
class Constituent
|
|
182
|
+
attr_reader :name, :source_name, :doodson, :speed_deg_per_hour, :amplitude_m, :phase_deg,
|
|
183
|
+
:amp_uncertainty_m, :phase_uncertainty_deg, :kept_reason, :raw
|
|
184
|
+
|
|
185
|
+
def initialize(raw)
|
|
186
|
+
@raw = Util.deep_freeze(raw)
|
|
187
|
+
@name = raw["name"]
|
|
188
|
+
@source_name = raw["source_name"]
|
|
189
|
+
@doodson = raw["doodson"]
|
|
190
|
+
@speed_deg_per_hour = raw["speed_deg_per_hour"]
|
|
191
|
+
@amplitude_m = raw["amplitude_m"]
|
|
192
|
+
@phase_deg = raw["phase_deg"]
|
|
193
|
+
@amp_uncertainty_m = raw["amp_uncertainty_m"]
|
|
194
|
+
@phase_uncertainty_deg = raw["phase_uncertainty_deg"]
|
|
195
|
+
@kept_reason = raw["kept_reason"]
|
|
196
|
+
freeze
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# The build metadata of a set (provenance.build).
|
|
201
|
+
class ProvenanceBuild
|
|
202
|
+
attr_reader :build_commit, :built_at, :fetched_at, :run_id, :tool_versions
|
|
203
|
+
|
|
204
|
+
def initialize(raw)
|
|
205
|
+
Util.deep_freeze(raw)
|
|
206
|
+
@build_commit = raw["build_commit"]
|
|
207
|
+
@built_at = Util.time_or_nil(raw["built_at"])
|
|
208
|
+
@fetched_at = Util.time_or_nil(raw["fetched_at"])
|
|
209
|
+
@run_id = raw["run_id"]
|
|
210
|
+
@tool_versions = raw["tool_versions"].is_a?(Hash) ? raw["tool_versions"] : nil
|
|
211
|
+
freeze
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# How a set was made and chosen. The schema allows more fields than the
|
|
216
|
+
# typed ones; they stay in #raw.
|
|
217
|
+
class Provenance
|
|
218
|
+
attr_reader :build, :adapter_version, :input_sha256, :carried_from_release, :selection_reason, :decision, :raw
|
|
219
|
+
|
|
220
|
+
def initialize(raw)
|
|
221
|
+
raw = {} if raw.nil? # a set with no provenance (a non-object fails the load: Release.check_station!)
|
|
222
|
+
@raw = Util.deep_freeze(raw)
|
|
223
|
+
# A missing build happens only in malformed input: nil, and the load goes on.
|
|
224
|
+
@build = raw["build"].is_a?(Hash) ? ProvenanceBuild.new(raw["build"]) : nil
|
|
225
|
+
@adapter_version = raw["adapter_version"]
|
|
226
|
+
@input_sha256 = raw["input_sha256"]
|
|
227
|
+
@carried_from_release = raw["carried_from_release"]
|
|
228
|
+
@selection_reason = raw["selection_reason"]
|
|
229
|
+
@decision = raw["decision"]
|
|
230
|
+
freeze
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
# The time-base annotation of a gauge set's record. correction
|
|
235
|
+
# is a frozen Hash {"from", "to"} or nil; each evidence entry is a frozen
|
|
236
|
+
# Hash with the keys comparator_kind, comparator_id, distance_km, lag_min
|
|
237
|
+
# and detail (nil when the file leaves one out).
|
|
238
|
+
class TimeBase
|
|
239
|
+
EVIDENCE_KEYS = %w[comparator_kind comparator_id distance_km lag_min detail].freeze
|
|
240
|
+
|
|
241
|
+
attr_reader :code, :reason, :declared, :published, :correction, :months_dropped, :decided_in,
|
|
242
|
+
:decided_build_commit, :evidence
|
|
243
|
+
|
|
244
|
+
def initialize(raw)
|
|
245
|
+
Util.deep_freeze(raw)
|
|
246
|
+
@code = Util.enum(raw["code"], ENUMS[:time_base_code])
|
|
247
|
+
@reason = raw["reason"]
|
|
248
|
+
@declared = raw["declared"]
|
|
249
|
+
@published = raw["published"]
|
|
250
|
+
corr = raw["correction"]
|
|
251
|
+
@correction = corr.is_a?(Hash) ? { "from" => corr["from"], "to" => corr["to"] }.freeze : nil
|
|
252
|
+
@months_dropped = Array(raw["months_dropped"]).freeze
|
|
253
|
+
@decided_in = raw["decided_in"]
|
|
254
|
+
@decided_build_commit = raw["decided_build_commit"]
|
|
255
|
+
@evidence = Array(raw["evidence"]).select { |e| e.is_a?(Hash) }
|
|
256
|
+
.map do |e|
|
|
257
|
+
EVIDENCE_KEYS.to_h do |k|
|
|
258
|
+
value = e[k]
|
|
259
|
+
value = value.to_f if %w[distance_km lag_min].include?(k) && value.is_a?(Numeric)
|
|
260
|
+
[k, value]
|
|
261
|
+
end.freeze
|
|
262
|
+
end.freeze
|
|
263
|
+
freeze
|
|
264
|
+
end
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
# One QC flag of a set: the flag, a one-line reason, and numbers by name (or nil).
|
|
268
|
+
class QcFlag
|
|
269
|
+
attr_reader :flag, :reason, :values
|
|
270
|
+
|
|
271
|
+
def initialize(raw)
|
|
272
|
+
Util.deep_freeze(raw)
|
|
273
|
+
@flag = Util.enum(raw["flag"], ENUMS[:qc_flag])
|
|
274
|
+
@reason = raw["reason"]
|
|
275
|
+
@values = raw["values"].is_a?(Hash) ? raw["values"] : nil
|
|
276
|
+
freeze
|
|
277
|
+
end
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
class DroppedConstituent
|
|
281
|
+
attr_reader :name, :dropped_reason, :detail
|
|
282
|
+
|
|
283
|
+
def initialize(raw)
|
|
284
|
+
Util.deep_freeze(raw)
|
|
285
|
+
@name = raw["name"]
|
|
286
|
+
@dropped_reason = Util.enum(raw["dropped_reason"], ENUMS[:dropped_reason])
|
|
287
|
+
@detail = raw["detail"]
|
|
288
|
+
freeze
|
|
289
|
+
end
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
class RecordSpan
|
|
293
|
+
attr_reader :start, :end, :good_samples
|
|
294
|
+
|
|
295
|
+
def initialize(raw)
|
|
296
|
+
Util.deep_freeze(raw)
|
|
297
|
+
@start = Util.time(raw["start"])
|
|
298
|
+
@end = Util.time(raw["end"])
|
|
299
|
+
@good_samples = raw["good_samples"]
|
|
300
|
+
freeze
|
|
301
|
+
end
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
class Datum
|
|
305
|
+
attr_reader :msl_offset_m, :named
|
|
306
|
+
|
|
307
|
+
def initialize(raw)
|
|
308
|
+
Util.deep_freeze(raw)
|
|
309
|
+
@msl_offset_m = raw["msl_offset_m"]
|
|
310
|
+
@named = raw["named"] || {}.freeze
|
|
311
|
+
freeze
|
|
312
|
+
end
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
# The accuracy of one set against an official reference, for one window.
|
|
316
|
+
class Validation
|
|
317
|
+
NUMBERS = %w[time_mae_min time_p95_min time_bias_min height_mae_m range_error_m missed_events extra_events].freeze
|
|
318
|
+
attr_reader :set_id, :reference_source, :reference_station, :reference_distance_km, :window,
|
|
319
|
+
:time_mae_min, :time_p95_min, :time_bias_min, :height_mae_m, :range_error_m,
|
|
320
|
+
:missed_events, :extra_events, :previous_release
|
|
321
|
+
|
|
322
|
+
def initialize(raw, previous: false)
|
|
323
|
+
Util.deep_freeze(raw)
|
|
324
|
+
unless previous
|
|
325
|
+
@set_id = raw["set_id"]
|
|
326
|
+
@reference_source = raw["reference_source"]
|
|
327
|
+
@reference_station = raw["reference_station"]
|
|
328
|
+
@reference_distance_km = raw["reference_distance_km"]
|
|
329
|
+
@window = raw["window"]
|
|
330
|
+
prev = raw["previous_release"]
|
|
331
|
+
@previous_release = prev.is_a?(Hash) ? Validation.new(prev, previous: true) : nil
|
|
332
|
+
end
|
|
333
|
+
# In previous_release a value that is not a number is nil, as in every SDK.
|
|
334
|
+
NUMBERS.each { |k| instance_variable_set("@#{k}", previous && !Release.real?(raw[k]) ? nil : raw[k]) }
|
|
335
|
+
freeze
|
|
336
|
+
end
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
class Fallback
|
|
340
|
+
attr_reader :reason, :basis, :otc_finding, :preferred_set_id, :raw
|
|
341
|
+
def initialize(raw)
|
|
342
|
+
@raw = Util.deep_freeze(raw)
|
|
343
|
+
@reason = Util.enum(raw["reason"], ENUMS[:fallback_reason])
|
|
344
|
+
@basis = raw["basis"]
|
|
345
|
+
@otc_finding = raw["otc_finding"]
|
|
346
|
+
@preferred_set_id = raw["preferred_set_id"]
|
|
347
|
+
freeze
|
|
348
|
+
end
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
class SourceRecord
|
|
352
|
+
attr_reader :source, :source_version, :source_record_id, :role, :disposition, :detail,
|
|
353
|
+
:licence_id, :upstream_licence_id, :published_set_ids, :provenance, :original_metadata,
|
|
354
|
+
:original_harmonics, :licence, :upstream_licence, :raw
|
|
355
|
+
def initialize(raw, licences:)
|
|
356
|
+
@raw = Util.deep_freeze(raw)
|
|
357
|
+
%w[source source_version source_record_id detail licence_id upstream_licence_id provenance original_harmonics].each do |key|
|
|
358
|
+
instance_variable_set("@#{key}", raw[key])
|
|
359
|
+
end
|
|
360
|
+
@detail = raw.fetch("detail", "".freeze)
|
|
361
|
+
@role = Util.enum(raw["role"], ENUMS[:source_role])
|
|
362
|
+
@disposition = Util.enum(raw["disposition"], ENUMS[:source_disposition])
|
|
363
|
+
@published_set_ids = raw["published_set_ids"].freeze
|
|
364
|
+
@original_metadata = raw.fetch("original_metadata", {}.freeze)
|
|
365
|
+
@licence = licences[@licence_id]
|
|
366
|
+
@upstream_licence = licences[@upstream_licence_id]
|
|
367
|
+
[@licence_id, @upstream_licence_id].compact.each do |id|
|
|
368
|
+
raise InvalidReleaseError, "source record #{@source_record_id}: licence #{id} does not resolve" unless licences.key?(id)
|
|
369
|
+
end
|
|
370
|
+
freeze
|
|
371
|
+
end
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
class HistoricalOffsetVintage
|
|
375
|
+
attr_reader :publication_year, :source_lifecycle, :removal_events, :current_validity, :current_accuracy,
|
|
376
|
+
:reference_2020_equivalence, :reference_2020_equivalence_sha256
|
|
377
|
+
def initialize(raw)
|
|
378
|
+
Util.deep_freeze(raw)
|
|
379
|
+
%w[publication_year source_lifecycle removal_events current_validity current_accuracy reference_2020_equivalence reference_2020_equivalence_sha256].each do |key|
|
|
380
|
+
instance_variable_set("@#{key}", raw[key])
|
|
381
|
+
end
|
|
382
|
+
freeze
|
|
383
|
+
end
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
class HistoricalOffsetProvenance
|
|
387
|
+
attr_reader :source_manifest_sha256, :input_sha256, :book, :annual_table_row, :printed_page, :pdf_page,
|
|
388
|
+
:raw_row_text, :raw_values_json, :id_crosswalk, :reference_binding, :acceptance_report_sha256
|
|
389
|
+
def initialize(raw)
|
|
390
|
+
Util.deep_freeze(raw)
|
|
391
|
+
%w[source_manifest_sha256 input_sha256 book annual_table_row printed_page pdf_page raw_row_text raw_values_json id_crosswalk reference_binding acceptance_report_sha256].each do |key|
|
|
392
|
+
instance_variable_set("@#{key}", raw[key])
|
|
393
|
+
end
|
|
394
|
+
freeze
|
|
395
|
+
end
|
|
396
|
+
end
|
|
397
|
+
|
|
398
|
+
class SubordinateOffsets
|
|
399
|
+
attr_reader :reference_station_id, :time_offset_high_min, :time_offset_low_min, :height_offset_high,
|
|
400
|
+
:height_offset_low, :height_adjusted_type, :licence_id, :offset_id, :source, :source_type,
|
|
401
|
+
:source_version, :source_record_id, :qc_status, :qc_flags, :fallback, :vintage, :provenance
|
|
402
|
+
|
|
403
|
+
def self.routing_id(station_id, offsets)
|
|
404
|
+
return nil unless offsets
|
|
405
|
+
return offsets["offset_id"] unless offsets["offset_id"].nil?
|
|
406
|
+
reference = offsets["reference_station_id"]
|
|
407
|
+
reference && "#{station_id}/subordinate-offsets/#{reference}"
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
def initialize(raw, station_id: nil, offset_id: nil)
|
|
411
|
+
Util.deep_freeze(raw)
|
|
412
|
+
@reference_station_id = raw["reference_station_id"]
|
|
413
|
+
@time_offset_high_min = raw["time_offset_high_min"]
|
|
414
|
+
@time_offset_low_min = raw["time_offset_low_min"]
|
|
415
|
+
@height_offset_high = raw["height_offset_high"]
|
|
416
|
+
@height_offset_low = raw["height_offset_low"]
|
|
417
|
+
@height_adjusted_type = Util.enum(raw["height_adjusted_type"], ENUMS[:height_adjusted_type])
|
|
418
|
+
@licence_id = raw["licence_id"]
|
|
419
|
+
%w[offset_id source source_version source_record_id].each { |key| instance_variable_set("@#{key}", raw[key]) }
|
|
420
|
+
@offset_id = self.class.routing_id(station_id, raw) if station_id
|
|
421
|
+
@offset_id = offset_id if offset_id
|
|
422
|
+
@source_type = Util.enum(raw["source_type"], ENUMS[:source_type])
|
|
423
|
+
@qc_status = Util.enum(raw["qc_status"], ENUMS[:qc_status])
|
|
424
|
+
@qc_flags = Array(raw["qc_flags"]).map { |f| QcFlag.new(f) }.freeze
|
|
425
|
+
@fallback = raw["fallback"] && Fallback.new(raw["fallback"])
|
|
426
|
+
@vintage = raw["offset_id"] && raw["vintage"] ? HistoricalOffsetVintage.new(raw["vintage"]) : nil
|
|
427
|
+
@provenance = raw["offset_id"] && raw["provenance"] ? HistoricalOffsetProvenance.new(raw["provenance"]) : nil
|
|
428
|
+
freeze
|
|
429
|
+
end
|
|
430
|
+
end
|
|
431
|
+
|
|
432
|
+
# The constants from one source record.
|
|
433
|
+
class ConstantSet
|
|
434
|
+
attr_reader :set_id, :source, :source_type, :quantity, :source_record_id, :source_version, :record_span,
|
|
435
|
+
:datum, :qc_status, :qc_flags, :dropped_constituents, :time_base, :convention, :licence, :provenance,
|
|
436
|
+
:validation, :constituents, :fallback, :raw
|
|
437
|
+
|
|
438
|
+
# The reported flag can differ from the default eligible for strict prediction.
|
|
439
|
+
Recommendation = Data.define(:reported, :effective)
|
|
440
|
+
private_constant :Recommendation
|
|
441
|
+
|
|
442
|
+
def self.with_recommendations(raw, convention:, licence:, validation:, recommended:, effective:)
|
|
443
|
+
selection = recommended == effective ? recommended : Recommendation.new(recommended, effective)
|
|
444
|
+
new(raw, convention: convention, licence: licence, validation: validation, recommended: selection)
|
|
445
|
+
end
|
|
446
|
+
private_class_method :with_recommendations
|
|
447
|
+
|
|
448
|
+
def initialize(raw, convention:, licence:, validation:, recommended:)
|
|
449
|
+
@raw = Util.deep_freeze(raw)
|
|
450
|
+
@fallback = raw["fallback"] && Fallback.new(raw["fallback"])
|
|
451
|
+
@set_id = raw["set_id"]
|
|
452
|
+
@source = raw["source"]
|
|
453
|
+
@source_type = Util.enum(raw["source_type"], ENUMS[:source_type])
|
|
454
|
+
@quantity = Util.enum(raw["quantity"], ENUMS[:quantity])
|
|
455
|
+
@source_record_id = raw["source_record_id"]
|
|
456
|
+
@source_version = raw["source_version"]
|
|
457
|
+
@record_span = raw["record_span"].is_a?(Hash) ? RecordSpan.new(raw["record_span"]) : nil
|
|
458
|
+
@datum = raw["datum"].is_a?(Hash) ? Datum.new(raw["datum"]) : nil
|
|
459
|
+
@qc_status = Util.enum(raw["qc_status"], ENUMS[:qc_status])
|
|
460
|
+
@qc_flags = (raw["qc_flags"] || []).map { |f| QcFlag.new(f) }.freeze
|
|
461
|
+
@dropped_constituents = (raw["dropped_constituents"] || []).map { |d| DroppedConstituent.new(d) }.freeze
|
|
462
|
+
@time_base = raw["time_base"].is_a?(Hash) ? TimeBase.new(raw["time_base"]) : nil
|
|
463
|
+
@convention = convention
|
|
464
|
+
@licence = licence
|
|
465
|
+
@provenance = Provenance.new(raw["provenance"])
|
|
466
|
+
@validation = validation.freeze
|
|
467
|
+
@constituents = (raw["constituents"] || []).map { |c| Constituent.new(c) }.freeze
|
|
468
|
+
@by_name = @constituents.to_h { |c| [c.name, c] }.freeze
|
|
469
|
+
if recommended.is_a?(Recommendation)
|
|
470
|
+
@recommended = recommended.reported
|
|
471
|
+
@effective_recommendation = recommended.effective
|
|
472
|
+
else
|
|
473
|
+
@recommended = recommended
|
|
474
|
+
@effective_recommendation = nil
|
|
475
|
+
end
|
|
476
|
+
freeze
|
|
477
|
+
end
|
|
478
|
+
|
|
479
|
+
def recommended?
|
|
480
|
+
@recommended
|
|
481
|
+
end
|
|
482
|
+
|
|
483
|
+
def effective_recommendation?
|
|
484
|
+
@effective_recommendation.nil? ? recommended? : @effective_recommendation
|
|
485
|
+
end
|
|
486
|
+
private :effective_recommendation?
|
|
487
|
+
|
|
488
|
+
# Looked up by OTC canonical name, case-sensitive.
|
|
489
|
+
def constituent(name)
|
|
490
|
+
@by_name[name]
|
|
491
|
+
end
|
|
492
|
+
end
|
|
493
|
+
|
|
494
|
+
# An active station.
|
|
495
|
+
class Station
|
|
496
|
+
attr_reader :station_id, :name, :country, :lat, :lon, :type, :kind, :timezone, :aliases, :status,
|
|
497
|
+
:recommended_set, :subordinate_offsets, :subordinate_offset_alternatives, :validation, :recommended_is_fallback, :source_records, :raw
|
|
498
|
+
|
|
499
|
+
# origin: the producing Release's private token; it retains no Release
|
|
500
|
+
# or reader descriptor (attribution still checks identity).
|
|
501
|
+
def initialize(raw, sets:, kind:, validation:, origin: nil, licences: {})
|
|
502
|
+
@raw = Util.deep_freeze(raw)
|
|
503
|
+
@recommended_is_fallback = raw["recommended_is_fallback"]
|
|
504
|
+
@source_records = Array(raw["source_records"]).map { |r| SourceRecord.new(r, licences: licences) }.freeze
|
|
505
|
+
@origin = origin.is_a?(Release) ? origin.__send__(:station_origin) : origin
|
|
506
|
+
@station_id = raw["station_id"]
|
|
507
|
+
@name = raw["name"]
|
|
508
|
+
@country = raw["country"]
|
|
509
|
+
@lat = raw["lat"]
|
|
510
|
+
@lon = raw["lon"]
|
|
511
|
+
@type = Util.enum(raw["type"], ENUMS[:station_type])
|
|
512
|
+
@kind = kind
|
|
513
|
+
@timezone = raw["timezone"]
|
|
514
|
+
# a line with no status is an active station
|
|
515
|
+
@status = Util.enum(raw["status"].nil? ? "active" : raw["status"], ENUMS[:station_status])
|
|
516
|
+
@aliases = (raw["aliases"] || {}).to_h do |system, ids|
|
|
517
|
+
[system.to_sym, (ids.is_a?(Array) ? ids : [ids]).freeze]
|
|
518
|
+
end.freeze
|
|
519
|
+
@sets = sets.freeze
|
|
520
|
+
@recommended_set = sets.find(&:recommended?)
|
|
521
|
+
@prediction_recommendation = sets.find { |set| effective_recommendation?(set) }
|
|
522
|
+
off = raw["subordinate_offsets"]
|
|
523
|
+
@subordinate_offsets = off.is_a?(Hash) ? SubordinateOffsets.new(off, station_id: @station_id) : nil
|
|
524
|
+
@subordinate_offset_alternatives = Array(raw["subordinate_offset_alternatives"]).map do |wrapper|
|
|
525
|
+
SubordinateOffsets.new(wrapper["offsets"], offset_id: wrapper["offset_id"])
|
|
526
|
+
end.freeze
|
|
527
|
+
@validation = validation.freeze
|
|
528
|
+
freeze
|
|
529
|
+
end
|
|
530
|
+
|
|
531
|
+
def reference?
|
|
532
|
+
@type == :reference
|
|
533
|
+
end
|
|
534
|
+
|
|
535
|
+
def subordinate?
|
|
536
|
+
@type == :subordinate
|
|
537
|
+
end
|
|
538
|
+
|
|
539
|
+
def tide?
|
|
540
|
+
@kind == :tide
|
|
541
|
+
end
|
|
542
|
+
|
|
543
|
+
def current?
|
|
544
|
+
@kind == :current
|
|
545
|
+
end
|
|
546
|
+
|
|
547
|
+
# The recommended set first, then the others by set_id. Sets with
|
|
548
|
+
# qc_status no_constants (listed, with no fitted constants) are left
|
|
549
|
+
# out unless include_no_constants is true.
|
|
550
|
+
def constant_sets(include_no_constants: false)
|
|
551
|
+
unless include_no_constants == true || include_no_constants == false
|
|
552
|
+
raise InvalidArgumentError, "include_no_constants must be true or false"
|
|
553
|
+
end
|
|
554
|
+
list = include_no_constants ? @sets : @sets.reject { |s| s.qc_status == :no_constants }
|
|
555
|
+
order_constant_sets(list, &:recommended?)
|
|
556
|
+
end
|
|
557
|
+
|
|
558
|
+
def constant_set(set_id)
|
|
559
|
+
@sets.find { |s| s.set_id == set_id }
|
|
560
|
+
end
|
|
561
|
+
|
|
562
|
+
private
|
|
563
|
+
|
|
564
|
+
attr_reader :prediction_recommendation
|
|
565
|
+
|
|
566
|
+
def prediction_constant_sets
|
|
567
|
+
order_constant_sets(@sets) { |set| effective_recommendation?(set) }
|
|
568
|
+
end
|
|
569
|
+
|
|
570
|
+
def order_constant_sets(list, &recommended)
|
|
571
|
+
rec = list.select(&recommended)
|
|
572
|
+
(rec + (list - rec).sort_by(&:set_id)).freeze
|
|
573
|
+
end
|
|
574
|
+
|
|
575
|
+
def effective_recommendation?(set)
|
|
576
|
+
return set.recommended? unless set.is_a?(ConstantSet)
|
|
577
|
+
set.__send__(:effective_recommendation?)
|
|
578
|
+
end
|
|
579
|
+
end
|
|
580
|
+
|
|
581
|
+
# A removed station id: why it was removed and, when merged, the station
|
|
582
|
+
# that holds its records.
|
|
583
|
+
class Tombstone
|
|
584
|
+
attr_reader :station_id, :name, :status, :removed_in, :removed_reason, :merged_into, :detail, :raw
|
|
585
|
+
|
|
586
|
+
def initialize(raw)
|
|
587
|
+
@raw = Util.deep_freeze(raw)
|
|
588
|
+
@station_id = raw["station_id"]
|
|
589
|
+
@name = raw["name"]
|
|
590
|
+
@status = Util.enum(raw["status"], ENUMS[:station_status])
|
|
591
|
+
@removed_in = raw["removed_in"]
|
|
592
|
+
@removed_reason = Util.enum(raw["removed_reason"], ENUMS[:removed_reason])
|
|
593
|
+
@merged_into = raw["merged_into"]
|
|
594
|
+
@detail = raw["detail"]
|
|
595
|
+
freeze
|
|
596
|
+
end
|
|
597
|
+
end
|
|
598
|
+
|
|
599
|
+
# Counts by type, kind and country (active stations) and by source and
|
|
600
|
+
# qc_status (constant sets). Each is a frozen Hash {value => count}.
|
|
601
|
+
class Stats
|
|
602
|
+
attr_reader :type, :kind, :country, :source, :qc_status
|
|
603
|
+
|
|
604
|
+
def initialize(type:, kind:, country:, source:, qc_status:)
|
|
605
|
+
@type = type
|
|
606
|
+
@kind = kind
|
|
607
|
+
@country = country
|
|
608
|
+
@source = source
|
|
609
|
+
@qc_status = qc_status
|
|
610
|
+
freeze
|
|
611
|
+
end
|
|
612
|
+
end
|
|
613
|
+
|
|
614
|
+
# A station and its great-circle distance from the query point.
|
|
615
|
+
class Nearby
|
|
616
|
+
attr_reader :station, :distance_km
|
|
617
|
+
|
|
618
|
+
def initialize(station, distance_km)
|
|
619
|
+
@station = station
|
|
620
|
+
@distance_km = distance_km
|
|
621
|
+
freeze
|
|
622
|
+
end
|
|
623
|
+
end
|
|
624
|
+
|
|
625
|
+
class UpdateResult
|
|
626
|
+
attr_reader :updated, :from, :to
|
|
627
|
+
|
|
628
|
+
def initialize(updated, from, to)
|
|
629
|
+
@updated = updated
|
|
630
|
+
@from = from
|
|
631
|
+
@to = to
|
|
632
|
+
freeze
|
|
633
|
+
end
|
|
634
|
+
end
|
|
635
|
+
end
|