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,487 @@
|
|
|
1
|
+
require "date"
|
|
2
|
+
require "digest"
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
class OpenTideConstants
|
|
6
|
+
module Predict
|
|
7
|
+
module_function
|
|
8
|
+
|
|
9
|
+
def binding_json(value)
|
|
10
|
+
case value
|
|
11
|
+
when Hash
|
|
12
|
+
"{" + value.keys.sort.map { |key| JSON.generate(key) + ":" + binding_json(value[key]) }.join(",") + "}"
|
|
13
|
+
when Array
|
|
14
|
+
"[" + value.map { |item| binding_json(item) }.join(",") + "]"
|
|
15
|
+
when Float
|
|
16
|
+
# Source binding uses Python JSON, independently of encode_cache_json cache JSON.
|
|
17
|
+
raise JSON::GeneratorError, "nonfinite binding number" unless value.finite?
|
|
18
|
+
text = value.to_s
|
|
19
|
+
if text.include?("e")
|
|
20
|
+
coefficient, exponent = text.split("e")
|
|
21
|
+
power = exponent.to_i
|
|
22
|
+
if power >= -4 && power < 16
|
|
23
|
+
text = format("%.#{[0, coefficient.delete("-.").length - 1 - power].max}f", value)
|
|
24
|
+
text += ".0" unless text.include?(".")
|
|
25
|
+
else
|
|
26
|
+
coefficient = coefficient.delete_suffix(".0")
|
|
27
|
+
text = coefficient + "e" + (power < 0 ? "-" : "+") + power.abs.to_s.rjust(2, "0")
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
text
|
|
31
|
+
else
|
|
32
|
+
JSON.generate(value)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def binding_digest(value)
|
|
37
|
+
Digest::SHA256.hexdigest(binding_json(value))
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def finite_height?(value)
|
|
41
|
+
(value.is_a?(Integer) || value.is_a?(Float)) && value.finite?
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def binding_hash?(value)
|
|
45
|
+
value.is_a?(String) && value.match?(/\A[0-9a-f]{64}\z/)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def own_source?(source, model)
|
|
49
|
+
aliases = {"ticon" => "ticon-4", "gesla" => "gesla-fit"}
|
|
50
|
+
aliases.fetch(source["source"], source["source"]) == model["source"] &&
|
|
51
|
+
%w[source_record_id source_version source_model].all? { |key| source[key] == model[key] }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def published_own_source?(source, model)
|
|
55
|
+
own_source?(source, model) && Array(source["published_set_ids"]).include?(model["set_id"])
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def response_object(response)
|
|
59
|
+
JSON.parse(response)
|
|
60
|
+
rescue JSON::ParserError, TypeError
|
|
61
|
+
nil
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def published_model_anchor(station, constants, table, fields, epoch)
|
|
65
|
+
model = constants.raw
|
|
66
|
+
digest = binding_digest(model)
|
|
67
|
+
begin
|
|
68
|
+
years = epoch.split("-").map { |year| Integer(year) }
|
|
69
|
+
return nil unless years.length == 2
|
|
70
|
+
first, last = years
|
|
71
|
+
start = Time.utc(first, 1, 1).to_f
|
|
72
|
+
stop = Time.utc(last + 1, 1, 1).to_f
|
|
73
|
+
rescue ArgumentError, TypeError, NoMethodError
|
|
74
|
+
return nil
|
|
75
|
+
end
|
|
76
|
+
%w[MTL MHW MLW MHHW MLLW].each do |key|
|
|
77
|
+
published, field = table[key], fields[key] || {}
|
|
78
|
+
next unless finite_height?(published) && field["name"] == key && field["value"] == published
|
|
79
|
+
method = %w[MHHW MLLW].include?(key) ? "sdk_harmonic_extremes_monthly_tidal_day_means_v1" : "sdk_harmonic_extremes_event_means_v1"
|
|
80
|
+
candidates = []
|
|
81
|
+
Array(station.raw["source_records"]).each do |source|
|
|
82
|
+
next unless published_own_source?(source, model)
|
|
83
|
+
Array(source["retained_datums"]).each do |candidate|
|
|
84
|
+
binding, window = candidate["input_binding"] || {}, candidate["window"] || {}
|
|
85
|
+
value = (candidate["values"] || {})[key.downcase]
|
|
86
|
+
next unless candidate["kind"] == "computed" && candidate["method"] == method && candidate["units"] == "m" &&
|
|
87
|
+
Array((candidate["reliability"] || {})["limitations"]).include?("published_anchor_alignment_only") &&
|
|
88
|
+
(candidate["frame"] || {})["kind"] == "harmonic_mean" &&
|
|
89
|
+
candidate["source_record_id"] == source["source_record_id"] && candidate["set_id"] == model["set_id"] &&
|
|
90
|
+
binding["set_id"] == model["set_id"] && binding["constant_set_sha256"] == digest && binding["algorithm"] == "otc-predict-1" &&
|
|
91
|
+
binding["convention_id"] == model["convention_id"] && binding["astro_table_id"] == constants.convention.raw["astro_table_id"] &&
|
|
92
|
+
%w[request_sha256 results_sha256 native_sha256 binary_sha256].all? { |name| binding_hash?(binding[name]) } && finite_height?(value)
|
|
93
|
+
begin
|
|
94
|
+
first_value, last_value = window.fetch("start"), window.fetch("end")
|
|
95
|
+
next if Date._iso8601(first_value)[:offset].nil? || Date._iso8601(last_value)[:offset].nil?
|
|
96
|
+
next unless Time.iso8601(first_value).to_i == start && Time.iso8601(last_value).to_i == stop
|
|
97
|
+
rescue ArgumentError, TypeError, KeyError
|
|
98
|
+
next
|
|
99
|
+
end
|
|
100
|
+
candidates << [value, candidate]
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
next if candidates.empty?
|
|
104
|
+
return nil unless candidates.all? { |value, _| value == candidates.first[0] }
|
|
105
|
+
return [key, candidates.first[0], candidates.first[1]]
|
|
106
|
+
end
|
|
107
|
+
nil
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def published_msl_datums(station, constants, raw)
|
|
111
|
+
return raw unless constants && constants.quantity == :water_level
|
|
112
|
+
named, basis = (raw["named"] || {}).dup, (raw["basis"] || {}).dup
|
|
113
|
+
additions, superseded_additions = {}, {}
|
|
114
|
+
endpoint = "https://api.tidesandcurrents.noaa.gov/mdapi/prod/webapi/stations/"
|
|
115
|
+
height_keys = %w[stnd navd88 vivd09 msl mhhw mhw mtl dtl mlw mllw hat lat mhwn mlwn mhws mlws]
|
|
116
|
+
Array(station.raw["source_records"]).each do |source|
|
|
117
|
+
ident = source["source_record_id"]
|
|
118
|
+
next unless source["source"] == "noaa" && ident.is_a?(String) && ident.match?(/\A[[:digit:]]+\z/)
|
|
119
|
+
metadata = source["original_metadata"] || {}
|
|
120
|
+
Array(source["retained_datums"]).each do |candidate|
|
|
121
|
+
binding, frame, proof = candidate["input_binding"] || {}, candidate["frame"] || {}, candidate["basis"] || {}
|
|
122
|
+
epoch, digest = proof["epoch"], binding["sha256"]
|
|
123
|
+
resource = binding["source_url"] == endpoint + ident + "/supersededdatums.json?units=metric" ? "supersededdatums" : "datums"
|
|
124
|
+
superseded = resource == "supersededdatums"
|
|
125
|
+
next unless candidate["kind"] == "published" && candidate["units"] == "m" && candidate["source_record_id"] == ident &&
|
|
126
|
+
frame["kind"] == "gauge_zero" && frame["name"] == "STND" && frame["tie_status"] == "source_declared" &&
|
|
127
|
+
epoch.is_a?(String) && !epoch.empty? && binding_hash?(digest) &&
|
|
128
|
+
binding["source_url"] == endpoint + ident + "/" + resource + ".json?units=metric"
|
|
129
|
+
source_metadata = proof["source_metadata"] || {}
|
|
130
|
+
next if superseded && !(proof["historical"] == true && binding["source_resource"] == resource && binding["identity_station_id"] == ident &&
|
|
131
|
+
source_metadata["self"] == endpoint + ident + "/supersededdatums.json" && source_metadata["units"] == "meters" && source_metadata["epoch"] == epoch)
|
|
132
|
+
values, fields = candidate["values"] || {}, proof["source_fields"] || {}
|
|
133
|
+
table, response = values, metadata[resource + "_response_json"]
|
|
134
|
+
next if response.nil? && !binding_hash?(binding["receipt_sha256"])
|
|
135
|
+
unless response.nil?
|
|
136
|
+
next unless response.is_a?(String) && Digest::SHA256.hexdigest(response) == digest && metadata[resource + "_response_sha256"] == digest
|
|
137
|
+
body = response_object(response)
|
|
138
|
+
next unless body.is_a?(Hash) && body["self"] == endpoint + ident + "/" + resource + ".json" && body["units"] == "meters" && body["epoch"] == epoch
|
|
139
|
+
table = Array(body["datums"]).select { |row| row.is_a?(Hash) }.to_h { |row| [row["name"], row["value"]] }
|
|
140
|
+
end
|
|
141
|
+
msl, anchor = table["MSL"], nil
|
|
142
|
+
if msl.nil?
|
|
143
|
+
anchor = published_model_anchor(station, constants, table, fields, epoch)
|
|
144
|
+
next unless anchor
|
|
145
|
+
msl = table[anchor[0]]
|
|
146
|
+
elsif !finite_height?(msl) || response.nil? && (fields["MSL"] || {})["value"] != msl
|
|
147
|
+
next
|
|
148
|
+
end
|
|
149
|
+
next unless msl.is_a?(Integer) || msl.is_a?(Float)
|
|
150
|
+
identity = response_object(metadata["identity_binding_json"] || "{}")
|
|
151
|
+
next unless identity
|
|
152
|
+
model = constants.raw
|
|
153
|
+
same_gauge = model["source"] == "noaa" && model["source_record_id"] == ident
|
|
154
|
+
original = identity["original_inventory_source_tuple"] || {}
|
|
155
|
+
same_gauge ||= identity["same_gauge_asserted"] == true && original["source"] == "gesla" && model["source"] == "gesla-fit" &&
|
|
156
|
+
%w[source_record_id source_version].all? { |key| original[key] == model[key] }
|
|
157
|
+
flags = [superseded || !same_gauge ? "low" : "medium", "bound via published MSL; assumes model mean = agency MSL (epoch #{epoch})"]
|
|
158
|
+
if anchor
|
|
159
|
+
key, _, computed = anchor
|
|
160
|
+
proof_binding = computed["input_binding"]
|
|
161
|
+
flags = ["low", "aligned via published #{key} (#{epoch}); model-computed #{key} matched",
|
|
162
|
+
"assumes published and modeled tidal anchors coincide; no surveyed tie or accuracy asserted",
|
|
163
|
+
"never default; explicit named datum only",
|
|
164
|
+
"model anchor set #{model['set_id']}; model SHA256 #{proof_binding['constant_set_sha256']}",
|
|
165
|
+
"model anchor request SHA256 #{proof_binding['request_sha256']}; results SHA256 #{proof_binding['results_sha256']}"]
|
|
166
|
+
end
|
|
167
|
+
flags.concat(["superseded NOAA epoch#{epoch.tr('-', '–')}, published", "no current-epoch or current-profile tie is asserted", "never default; explicit named datum only"]) if superseded
|
|
168
|
+
flags << "different-model binding; no same-gauge or surveyed frame tie asserted" unless same_gauge
|
|
169
|
+
model_epoch = ((raw["basis"] || {})["msl"] || {})["epoch"]
|
|
170
|
+
flags << "model and agency MSL epochs differ or model epoch is unknown" if anchor.nil? && model_epoch != epoch
|
|
171
|
+
mean_in_zero = raw["zero"] == "harmonic_mean" ? 0.0 : raw["msl_offset_m"]
|
|
172
|
+
values.each do |key, value|
|
|
173
|
+
name, field = key.downcase, fields[key] || {}
|
|
174
|
+
next if superseded && name != "stnd"
|
|
175
|
+
next if anchor && !%w[stnd navd88].include?(name)
|
|
176
|
+
next unless height_keys.include?(name) && !named.key?(name) && finite_height?(value) && field["name"] == key && field["value"] == value && table[key] == value
|
|
177
|
+
level = value - msl + (anchor ? anchor[1] : 0.0) + (mean_in_zero || 0.0)
|
|
178
|
+
target = superseded ? superseded_additions : additions
|
|
179
|
+
entry = {"kind" => "published", "method" => anchor ? "published_anchor_binding" : "published_msl_binding", "source_name" => "NOAA " + ident,
|
|
180
|
+
"epoch" => epoch, "uncertainty_m" => nil, "flags" => flags, "_retained" => true, "_retained_mean" => mean_in_zero.nil?}
|
|
181
|
+
entry["data_span"] = anchor[2]["window"] if anchor
|
|
182
|
+
(target[name] ||= []) << [level, entry]
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
additions.each do |key, candidates|
|
|
187
|
+
msl_candidates = candidates.select { |item| item[1]["method"] == "published_msl_binding" }
|
|
188
|
+
candidates = msl_candidates unless msl_candidates.empty?
|
|
189
|
+
named[key], basis[key] = candidates.first if candidates.length == 1
|
|
190
|
+
end
|
|
191
|
+
if !named.key?("stnd") && Array(additions["stnd"]).empty?
|
|
192
|
+
candidates = superseded_additions["stnd"] || []
|
|
193
|
+
msl_candidates = candidates.select { |item| item[1]["method"] == "published_msl_binding" }
|
|
194
|
+
candidates = msl_candidates unless msl_candidates.empty?
|
|
195
|
+
named["stnd"], basis["stnd"] = candidates.first if candidates.length == 1
|
|
196
|
+
end
|
|
197
|
+
raw.merge("named" => named, "basis" => basis)
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def observed_datum_candidates(station, constants)
|
|
201
|
+
additions = {}
|
|
202
|
+
return additions unless constants
|
|
203
|
+
model = constants.raw
|
|
204
|
+
datum, provenance = model["datum"] || {}, model["provenance"] || {}
|
|
205
|
+
zero = datum["msl_offset_m"]
|
|
206
|
+
return additions unless model["source"] == "gesla-fit" && model["quantity"] == "water_level" && finite_height?(zero) &&
|
|
207
|
+
model["time_base"].is_a?(Hash) && model["time_base"]["published"]
|
|
208
|
+
convention = constants.convention.raw
|
|
209
|
+
return additions unless convention["phase_reference"] == "greenwich_utc" && convention["v0_model"] == "schureman_tcd" && convention["nodal_handling"] == "f_u_at_prediction"
|
|
210
|
+
model_sha = binding_digest(model)
|
|
211
|
+
fields = %w[constant_set_sha256 model_tcd_sha256 source_candidate_sha256]
|
|
212
|
+
Array(station.raw["source_records"]).each do |source|
|
|
213
|
+
next unless source["source"] == "gesla" && published_own_source?(source, model)
|
|
214
|
+
Array(source["retained_datums"]).each do |candidate|
|
|
215
|
+
proof, frame = candidate["input_binding"] || {}, candidate["frame"] || {}
|
|
216
|
+
next unless proof.is_a?(Hash) && frame.is_a?(Hash)
|
|
217
|
+
next unless candidate["kind"] == "observed" && candidate["method"] == "first_reduction" && candidate["units"] == "m" && frame["kind"] == "gauge_zero" &&
|
|
218
|
+
candidate["set_id"] == model["set_id"] && candidate["source_record_id"] == source["source_record_id"] && candidate["uncertainty_m"].nil? &&
|
|
219
|
+
(fields + %w[fit_sha256 tcd_sha256 member_sha256]).all? { |key| proof[key].is_a?(String) && proof[key].length == 64 } && proof["constant_set_sha256"] == model_sha
|
|
220
|
+
next unless proof["station_id"] == station.station_id && proof["set_id"] == model["set_id"] && proof["source_record_id"] == source["source_record_id"] &&
|
|
221
|
+
Array(provenance["input_sha256"]).include?(proof["member_sha256"]) && proof["model_tcd_sha256"] == provenance["tcd_sha256"] &&
|
|
222
|
+
proof["model_tcd_sha256"] == convention["tables_sha256"] && proof["time_base"] == model["time_base"] && proof["fit_segment"] == model["fit_segment"]
|
|
223
|
+
original_zero = proof["original_zero"] || {}
|
|
224
|
+
next unless original_zero.is_a?(Hash) && %w[zero zero_name msl_offset_m].all? { |key| original_zero.key?(key) && original_zero[key] == datum[key] }
|
|
225
|
+
original = candidate.merge("input_binding" => proof.reject { |key, _| fields.include?(key) })
|
|
226
|
+
next unless binding_digest(original) == proof["source_candidate_sha256"] && Array(source["retained_datums"]).include?(original)
|
|
227
|
+
window, candidate_basis = candidate["window"] || {}, candidate["basis"] || {}
|
|
228
|
+
months = candidate_basis.is_a?(Hash) ? candidate_basis["counted_months"] || [] : []
|
|
229
|
+
begin
|
|
230
|
+
next unless !months.empty? && months == months.uniq.sort
|
|
231
|
+
first, last = [months.first, months.last].map { |month| Date.iso8601(month + "-01") }
|
|
232
|
+
next unless Date.iso8601(window.fetch("start")) == first && Date.iso8601(window.fetch("end")) == Date.new(last.year, last.month, -1)
|
|
233
|
+
rescue KeyError, TypeError, ArgumentError
|
|
234
|
+
next
|
|
235
|
+
end
|
|
236
|
+
reliability = candidate["reliability"] || {}
|
|
237
|
+
flags = Array(reliability["limitations"]) + ["observed gauge_zero converted using authenticated own-model zero; no surveyed tie asserted", "observed window #{window['start']} to #{window['end']}"]
|
|
238
|
+
local = candidate.merge("_observed_bound" => true, "reliability" => reliability.merge("limitations" => flags))
|
|
239
|
+
(candidate["values"] || {}).each { |key, value| (additions[key] ||= []) << [value, local] if finite_height?(value) }
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
additions
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
def ticon_msl_datums(station, constants, raw)
|
|
246
|
+
return raw unless constants && constants.source == "ticon-4" && constants.quantity == :water_level
|
|
247
|
+
model = constants.raw
|
|
248
|
+
return raw unless (model["datum"] || {})["msl_offset_m"].nil? && model["time_base"].is_a?(Hash) && model["time_base"]["published"]
|
|
249
|
+
named, basis, additions = (raw["named"] || {}).dup, (raw["basis"] || {}).dup, {}
|
|
250
|
+
label = "assumes model mean = observed MSL (TICON same-frame), not an exact tie"
|
|
251
|
+
Array(station.raw["source_records"]).each do |source|
|
|
252
|
+
next unless source["source"] == "ticon" && published_own_source?(source, model)
|
|
253
|
+
Array(source["retained_datums"]).each do |candidate|
|
|
254
|
+
proof, frame = candidate["input_binding"] || {}, candidate["frame"] || {}
|
|
255
|
+
original_zero = proof["original_zero"] || {}
|
|
256
|
+
next unless candidate["kind"] == "observed" && candidate["method"] == "first_reduction" && candidate["units"] == "m" && frame["kind"] == "gauge_zero" &&
|
|
257
|
+
candidate["set_id"] == model["set_id"] && candidate["source_record_id"] == source["source_record_id"] && candidate["uncertainty_m"].nil? &&
|
|
258
|
+
proof["station_id"] == station.station_id && proof["set_id"] == model["set_id"] && proof["source_record_id"] == source["source_record_id"] &&
|
|
259
|
+
proof["time_base"] == model["time_base"] && proof["fit_segment"] == model["fit_segment"] &&
|
|
260
|
+
%w[zero zero_name msl_offset_m].all? { |key| original_zero.key?(key) && original_zero[key].nil? } &&
|
|
261
|
+
%w[member_sha256 fit_sha256].all? { |key| binding_hash?(proof[key]) }
|
|
262
|
+
values = candidate["values"] || {}
|
|
263
|
+
msl = values["msl"]
|
|
264
|
+
next unless finite_height?(msl)
|
|
265
|
+
flags = ["low", label, "never default; explicit named datum only"] + Array((candidate["reliability"] || {})["limitations"])
|
|
266
|
+
%w[mhhw mllw].each do |key|
|
|
267
|
+
value = values[key]
|
|
268
|
+
next if named.key?(key) || !finite_height?(value)
|
|
269
|
+
(additions[key] ||= []) << [value - msl, {"kind" => "observed", "method" => "ticon_observed_msl_binding", "source_name" => "TICON " + source["source_record_id"],
|
|
270
|
+
"data_span" => candidate["window"], "uncertainty_m" => nil, "flags" => flags, "_retained" => true, "_retained_mean" => true}]
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
end
|
|
274
|
+
additions.each { |key, candidates| named[key], basis[key] = candidates.first if candidates.length == 1 }
|
|
275
|
+
raw.merge("named" => named, "basis" => basis)
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
def chs_mwl_datums(release, station, constants, raw)
|
|
279
|
+
return raw unless constants && constants.quantity == :water_level && !(raw["named"] || {}).key?("llwlt")
|
|
280
|
+
label = "bound via CHS MWL; assumes model mean = CHS MWL; frame/epoch undeclared by CHS"
|
|
281
|
+
type_ids = {"mwl" => "5cec2eba3d0f4a04cc64d5d2", "llwlt" => "5cec2eba3d0f4a04cc64d5d3"}
|
|
282
|
+
bridges = []
|
|
283
|
+
Array(station.raw["source_records"]).each do |source|
|
|
284
|
+
ident = source["source_record_id"]
|
|
285
|
+
next unless source["source"] == "chs-iwls" && ident.is_a?(String) && !ident.empty?
|
|
286
|
+
licence = release.licence(source["licence_id"])
|
|
287
|
+
next if release.instance_variable_get(:@commercial_only) && (!licence || licence.commercial_use != true)
|
|
288
|
+
metadata = source["original_metadata"] || {}
|
|
289
|
+
response, digest = metadata["metadata_response_json"], metadata["metadata_response_sha256"]
|
|
290
|
+
url = "https://api-iwls.dfo-mpo.gc.ca/api/v1/stations/" + ident + "/metadata"
|
|
291
|
+
inputs = Array((source["provenance"] || {})["input_sha256"])
|
|
292
|
+
next unless response.is_a?(String) && Digest::SHA256.hexdigest(response) == digest && inputs.include?(digest) && metadata["metadata_response_url"] == url
|
|
293
|
+
body = response_object(response)
|
|
294
|
+
next unless body.is_a?(Hash) && body["id"] == ident && body["code"] == metadata["station_code"] && body["heights"].is_a?(Array)
|
|
295
|
+
pair = type_ids.to_h { |key, _| [key, []] }
|
|
296
|
+
Array(source["retained_datums"]).each do |candidate|
|
|
297
|
+
proof, basis = candidate["input_binding"] || {}, candidate["basis"] || {}
|
|
298
|
+
source_response = proof["source_response"] || {}
|
|
299
|
+
registry, units = proof["height_type_registry"] || {}, proof["unit_registry"] || {}
|
|
300
|
+
height_type, phenomenon = registry["height_type"] || {}, units["phenomenon"] || {}
|
|
301
|
+
frame, row_index = candidate["frame"] || {}, source_response["source_row_index"]
|
|
302
|
+
next unless candidate["kind"] == "published" && candidate["units"] == "m" && candidate["source_record_id"] == ident && candidate["uncertainty_m"].nil? &&
|
|
303
|
+
frame["kind"] == "unknown" && frame["tie_status"] == "unknown" && basis["epoch"].nil? && basis["reference"].nil? && basis["source_licence_id"] == source["licence_id"] &&
|
|
304
|
+
source_response["sha256"] == digest && source_response["source_url"] == url && source_response["source_field"] == "heights" &&
|
|
305
|
+
row_index.is_a?(Integer) && row_index >= 0 && row_index < body["heights"].length && inputs.include?(registry["sha256"]) && inputs.include?(units["sha256"]) &&
|
|
306
|
+
phenomenon["unit"] == "m" && phenomenon["code"] == "wl" && height_type["phenomenonId"] == phenomenon["id"]
|
|
307
|
+
row = body["heights"][row_index]
|
|
308
|
+
next unless row.is_a?(Hash) && row == basis["source_fields"]
|
|
309
|
+
type_ids.each do |key, type_id|
|
|
310
|
+
value = (candidate["values"] || {})[key]
|
|
311
|
+
next unless basis["source_name"] == key.upcase && height_type["code"] == key.upcase && height_type["id"] == type_id && row["heightTypeId"] == type_id &&
|
|
312
|
+
finite_height?(value) && (row["value"].is_a?(Integer) || row["value"].is_a?(Float)) && value == row["value"]
|
|
313
|
+
pair[key] << [value, candidate]
|
|
314
|
+
end
|
|
315
|
+
end
|
|
316
|
+
next unless pair.values.all? { |candidates| candidates.length == 1 }
|
|
317
|
+
mwl, mwl_candidate = pair["mwl"].first
|
|
318
|
+
target, target_candidate = pair["llwlt"].first
|
|
319
|
+
next unless mwl_candidate["frame"] == target_candidate["frame"]
|
|
320
|
+
flags = ["low", label, "never default; explicit named datum only"]
|
|
321
|
+
[mwl_candidate, target_candidate].each do |candidate|
|
|
322
|
+
flags.concat(Array((candidate["reliability"] || {})["limitations"]))
|
|
323
|
+
rights = (candidate["basis"] || {})["rights_limitation"]
|
|
324
|
+
flags << rights if rights.is_a?(String) && !rights.empty?
|
|
325
|
+
flags << "CHS source " + ident + "; candidate SHA256 " + binding_digest(candidate)
|
|
326
|
+
end
|
|
327
|
+
mean_in_zero = raw["zero"] == "harmonic_mean" ? 0.0 : raw["msl_offset_m"]
|
|
328
|
+
bridges << [target - mwl + (mean_in_zero || 0.0), {"kind" => "published", "method" => "chs_mwl_binding", "source_name" => "CHS " + body["code"],
|
|
329
|
+
"epoch" => nil, "uncertainty_m" => nil, "flags" => flags.reject { |flag| flag.nil? || flag == false || flag == "" }.uniq,
|
|
330
|
+
"_retained" => true, "_retained_mean" => mean_in_zero.nil?}]
|
|
331
|
+
end
|
|
332
|
+
return raw unless bridges.length == 1
|
|
333
|
+
value, provenance = bridges.first
|
|
334
|
+
raw.merge("named" => (raw["named"] || {}).merge("llwlt" => value), "basis" => (raw["basis"] || {}).merge("llwlt" => provenance))
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
def own_series_stnd(station, constants, raw)
|
|
338
|
+
zero = raw["msl_offset_m"]
|
|
339
|
+
return raw unless constants && constants.source == "gesla-fit" && constants.quantity == :water_level && !(raw["named"] || {}).key?("stnd") &&
|
|
340
|
+
[nil, "gauge_zero"].include?(raw["zero"]) && [nil, "Station Datum"].include?(raw["zero_name"]) && finite_height?(zero)
|
|
341
|
+
model = constants.raw
|
|
342
|
+
labels = ["Station Datum", "USGS Station Datum (see station page for tie to geocentric datum)"]
|
|
343
|
+
matches = []
|
|
344
|
+
Array(station.raw["source_records"]).each do |source|
|
|
345
|
+
next unless source["source"] == "gesla" && published_own_source?(source, model)
|
|
346
|
+
metadata = source["original_metadata"] || {}
|
|
347
|
+
label, member = metadata["DATUM INFORMATION"], metadata["original_member_sha256"]
|
|
348
|
+
next unless labels.include?(label) && metadata["FILE NAME"] == model["source_record_id"] && binding_hash?(member) && Array((model["provenance"] || {})["input_sha256"]).include?(member)
|
|
349
|
+
header = response_object(metadata["original_row_json"] || "{}")
|
|
350
|
+
next unless header.is_a?(Hash) && header["DATUM INFORMATION"] == label && header["FILE NAME"] == model["source_record_id"]
|
|
351
|
+
flags = ["OWN-SERIES " + label + "; authenticated GESLA series origin", "NOAA STND or surveyed-plane equivalence is not established",
|
|
352
|
+
"uncertainty unproved; no numeric uncertainty asserted", "never default; explicit named datum only", "own series member SHA256 " + member]
|
|
353
|
+
flags << "geocentric tie is not established; source wording retained without inferring a transform" if label == labels[1]
|
|
354
|
+
matches << {"kind" => "published", "method" => "own_series_station_datum", "source_name" => "GESLA own series " + source["source_record_id"],
|
|
355
|
+
"epoch" => nil, "uncertainty_m" => nil, "_retained" => true, "_retained_mean" => false, "flags" => flags}
|
|
356
|
+
end
|
|
357
|
+
return raw unless matches.length == 1
|
|
358
|
+
raw.merge("named" => (raw["named"] || {}).merge("stnd" => 0.0), "basis" => (raw["basis"] || {}).merge("stnd" => matches.first))
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
def retained_datum(release, station, constants = nil, offsets: nil)
|
|
362
|
+
model = constants ? constants.raw : {}
|
|
363
|
+
raw = (offsets || model)["datum"] || {}
|
|
364
|
+
named, basis, additions = (raw["named"] || {}).dup, (raw["basis"] || {}).dup, {}
|
|
365
|
+
Array(station.raw["source_records"]).each do |source|
|
|
366
|
+
Array(source["retained_datums"]).each do |candidate|
|
|
367
|
+
next unless candidate["kind"] == "computed" && candidate["units"] == "m"
|
|
368
|
+
next if Array((candidate["reliability"] || {})["limitations"]).include?("published_anchor_alignment_only")
|
|
369
|
+
next unless candidate["source_record_id"] == source["source_record_id"]
|
|
370
|
+
frame, binding = candidate["frame"] || {}, candidate["input_binding"] || {}
|
|
371
|
+
if offsets.nil?
|
|
372
|
+
next unless !model.empty? && frame["kind"] == "harmonic_mean" && candidate["set_id"] == model["set_id"] && own_source?(source, model)
|
|
373
|
+
next unless binding["set_id"] == model["set_id"] && binding["constant_set_sha256"] == binding_digest(model)
|
|
374
|
+
else
|
|
375
|
+
method = candidate["method"]
|
|
376
|
+
next unless %w[noaa_subordinate_reference_datums_v1 noaa_subordinate_mtl_msl_proxy_v1].include?(method) && frame["kind"] == "unknown" &&
|
|
377
|
+
frame["name"] == "NOAA subordinate MLLW" && (offsets["datum"] || {})["zero_name"] == "MLLW"
|
|
378
|
+
native = (candidate["basis"] || {})["native_offsets"] || {}
|
|
379
|
+
keys = %w[reference_station_id height_adjusted_type height_offset_high height_offset_low time_offset_high_min time_offset_low_min]
|
|
380
|
+
next unless keys.all? { |key| native.key?(key) && native[key] == offsets[key] }
|
|
381
|
+
next if !offsets["source_record_id"].nil? && %w[source source_record_id source_version].any? { |key| offsets[key] != source[key] }
|
|
382
|
+
own, reference = binding["published_offsets"] || {}, binding["published_reference"] || {}
|
|
383
|
+
next unless %w[source source_record_id source_version].all? { |key| own[key] == source[key] } && reference["native_station_id"] == offsets["reference_station_id"]
|
|
384
|
+
reference_station = release.station(reference["native_station_id"])
|
|
385
|
+
next unless reference_station && Array(reference_station.raw["source_records"]).any? { |record|
|
|
386
|
+
record["source"] == reference["source"] && record["source_record_id"] == reference["source_record_id"] && Array(record["retained_datums"]).any? { |datum|
|
|
387
|
+
datum["kind"] == "published" && (datum["input_binding"] || {})["sha256"] == reference["sha256"] && (datum["input_binding"] || {})["source_url"] == reference["source_url"]
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
next unless [own, reference].all? { |proof| proof["response_json"].is_a?(String) && Digest::SHA256.hexdigest(proof["response_json"]) == proof["sha256"] }
|
|
391
|
+
published, reference_body = response_object(own["response_json"]), response_object(reference["response_json"])
|
|
392
|
+
endpoint = "https://api.tidesandcurrents.noaa.gov/mdapi/prod/webapi/stations/"
|
|
393
|
+
next unless published.is_a?(Hash) && reference_body.is_a?(Hash) && own["source_url"] == endpoint + source["source_record_id"] + "/tidepredoffsets.json" &&
|
|
394
|
+
published["refStationId"] == reference["source_record_id"] && own["reference_source_record_id"] == reference["source_record_id"] &&
|
|
395
|
+
reference_body["self"] == endpoint + reference["source_record_id"] + "/datums.json" && reference_body["units"] == "meters" &&
|
|
396
|
+
(candidate["basis"] || {})["reference_epoch"] == reference_body["epoch"]
|
|
397
|
+
operation = published["heightAdjustedType"]
|
|
398
|
+
factor = operation == "F" ? 0.3048 : 1.0
|
|
399
|
+
adjustments = [["height_offset_high", "heightOffsetHighTide", factor], ["height_offset_low", "heightOffsetLowTide", factor],
|
|
400
|
+
["time_offset_high_min", "timeOffsetHighTide", 1.0], ["time_offset_low_min", "timeOffsetLowTide", 1.0]]
|
|
401
|
+
next unless %w[R F].include?(operation) && offsets["height_adjusted_type"] == (operation == "F" ? "A" : "R") && adjustments.all? { |native_key, field, scale|
|
|
402
|
+
finite_height?(published[field]) && (published[field] * scale - offsets[native_key]).abs <= 1e-12
|
|
403
|
+
}
|
|
404
|
+
if method == "noaa_subordinate_mtl_msl_proxy_v1"
|
|
405
|
+
proof_basis = candidate["basis"] || {}
|
|
406
|
+
next unless proof_basis["approximation"] == "approximation: MTL used as MSL proxy, subordinate-derived" && proof_basis["proxy_of"] == "derived_mtl" &&
|
|
407
|
+
proof_basis["never_default"] == true && (candidate["reliability"] || {})["status"] == "low"
|
|
408
|
+
end
|
|
409
|
+
end
|
|
410
|
+
(candidate["values"] || {}).each do |key, value|
|
|
411
|
+
next if %w[mn gt dhq dlq hwi lwi].include?(key) || named.key?(key) || !finite_height?(value)
|
|
412
|
+
(additions[key] ||= []) << [value, candidate]
|
|
413
|
+
end
|
|
414
|
+
end
|
|
415
|
+
end
|
|
416
|
+
observed_datum_candidates(station, constants).each { |key, candidates| additions[key] ||= candidates } if offsets.nil?
|
|
417
|
+
additions.each do |key, candidates|
|
|
418
|
+
next if named.key?(key) || %w[mn gt dhq dlq hwi lwi].include?(key)
|
|
419
|
+
if candidates.length != 1
|
|
420
|
+
semantic = ->(candidate) { candidate.reject { |field, _| field == "input_binding" } }
|
|
421
|
+
next unless candidates.all? { |value, candidate| candidate["kind"] == "computed" && value == candidates.first[0] && semantic.call(candidate) == semantic.call(candidates.first[1]) }
|
|
422
|
+
encoded = candidates.map { |_, candidate| binding_json(candidate) }.sort
|
|
423
|
+
provenance = encoded.map { |value| "retained source " + candidates.first[1]["source_record_id"] + "; candidate SHA256 " + Digest::SHA256.hexdigest(value) }
|
|
424
|
+
value, candidate = candidates.min_by { |item| binding_json(item[1]) }
|
|
425
|
+
reliability = candidate["reliability"] || {}
|
|
426
|
+
candidate = candidate.merge("reliability" => reliability.merge("limitations" => Array(reliability["limitations"]) + provenance))
|
|
427
|
+
candidates = [[value, candidate]]
|
|
428
|
+
end
|
|
429
|
+
value, candidate = candidates.first
|
|
430
|
+
reliability = candidate["reliability"] || {}
|
|
431
|
+
label = (candidate["basis"] || {})["approximation"]
|
|
432
|
+
flags = [reliability["status"], label].reject { |flag| flag.nil? || flag == false || flag == "" } + Array(reliability["limitations"])
|
|
433
|
+
named[key] = value
|
|
434
|
+
basis[key] = {"kind" => candidate["kind"], "method" => candidate["method"], "uncertainty_m" => candidate["uncertainty_m"], "flags" => flags,
|
|
435
|
+
"data_span" => candidate["window"], "_retained" => true, "_retained_mean" => offsets.nil? && !candidate["_observed_bound"]}
|
|
436
|
+
end
|
|
437
|
+
effective = raw.merge("named" => named, "basis" => basis)
|
|
438
|
+
return effective unless offsets.nil?
|
|
439
|
+
effective = ticon_msl_datums(station, constants, published_msl_datums(station, constants, effective))
|
|
440
|
+
own_series_stnd(station, constants, chs_mwl_datums(release, station, constants, effective))
|
|
441
|
+
end
|
|
442
|
+
|
|
443
|
+
def datum_metadata(context, key, basis)
|
|
444
|
+
context.datum_name, context.datum_kind, context.datum_uncertainty = key, basis["kind"], basis["uncertainty_m"]
|
|
445
|
+
if basis["_retained"]
|
|
446
|
+
context.datum_method = basis["method"]
|
|
447
|
+
context.datum_flags = Array(basis["flags"])
|
|
448
|
+
end
|
|
449
|
+
end
|
|
450
|
+
|
|
451
|
+
def observed_monthly_profiles(station)
|
|
452
|
+
profiles, ambiguous = {}, []
|
|
453
|
+
Array(station.raw["source_records"]).each do |source|
|
|
454
|
+
model_source = {"ticon" => "ticon-4", "gesla" => "gesla-fit"}[source["source"]]
|
|
455
|
+
next unless model_source
|
|
456
|
+
Array(source["retained_datums"]).each do |candidate|
|
|
457
|
+
binding = candidate["input_binding"] || {}
|
|
458
|
+
model = station.constant_set(candidate["set_id"])
|
|
459
|
+
next unless candidate["kind"] == "observed" && candidate["method"] == "monthly_tabulation" && candidate["units"] == "m" && candidate["uncertainty_m"].nil? &&
|
|
460
|
+
(candidate["frame"] || {})["kind"] == "gauge_zero" && model && model.source == model_source && model.quantity == :water_level && published_own_source?(source, model.raw) &&
|
|
461
|
+
candidate["source_record_id"] == source["source_record_id"] && binding["station_id"] == station.station_id && binding["set_id"] == model.set_id &&
|
|
462
|
+
binding["source_record_id"] == source["source_record_id"] && binding["time_base"] == model.raw["time_base"] && binding["fit_segment"] == model.raw["fit_segment"] &&
|
|
463
|
+
%w[zero zero_name msl_offset_m].all? { |key| (binding["original_zero"] || {}).key?(key) && binding["original_zero"][key].nil? } &&
|
|
464
|
+
%w[member_sha256 fit_sha256].all? { |key| binding_hash?(binding[key]) }
|
|
465
|
+
window = candidate["window"] || {}
|
|
466
|
+
begin
|
|
467
|
+
start, stop = Date.iso8601(window.fetch("start")), Date.iso8601(window.fetch("end"))
|
|
468
|
+
next unless start.day == 1 && stop == Date.new(start.year, start.month, -1) && Array((candidate["basis"] || {})["counted_months"]) == [start.strftime("%Y-%m")]
|
|
469
|
+
rescue ArgumentError, TypeError, KeyError
|
|
470
|
+
next
|
|
471
|
+
end
|
|
472
|
+
msl = (candidate["values"] || {})["msl"]
|
|
473
|
+
next unless finite_height?(msl)
|
|
474
|
+
identity = {"set_id" => model.set_id, "window" => window}
|
|
475
|
+
%w[source source_record_id source_version source_model].each { |key| identity[key] = source[key] }
|
|
476
|
+
%w[member_sha256 fit_sha256].each { |key| identity[key] = binding[key] }
|
|
477
|
+
handle = station.station_id + "/observed-monthly/" + binding_digest(identity)
|
|
478
|
+
profile = {"offset_id" => handle, "model" => model, "source" => source, "candidate" => candidate, "msl" => msl,
|
|
479
|
+
"order" => [-(stop - start).to_i, -stop.jd, model.set_id == station.raw["recommended_set_id"] ? 0 : 1, model.set_id]}
|
|
480
|
+
ambiguous << handle if profiles.key?(handle) && profiles[handle]["candidate"] != candidate
|
|
481
|
+
profiles[handle] = profile
|
|
482
|
+
end
|
|
483
|
+
end
|
|
484
|
+
profiles.reject { |handle, _| ambiguous.include?(handle) }.values.sort_by { |profile| profile["order"] }
|
|
485
|
+
end
|
|
486
|
+
end
|
|
487
|
+
end
|