iers 0.3.1 → 0.4.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.
@@ -45,7 +45,7 @@ module IERS
45
45
  def between(start_date, end_date)
46
46
  start_mjd = TimeScale.to_mjd(start_date)
47
47
  end_mjd = TimeScale.to_mjd(end_date)
48
- entries = Data.finals_entries
48
+ entries = Data.eop_entries
49
49
 
50
50
  EopLookup
51
51
  .range(entries, start_mjd, end_mjd)
@@ -1,16 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "pathname"
4
+
3
5
  module IERS
4
6
  class Configuration
5
7
  DEFAULT_CACHE_DIR = Pathname("~/.cache/iers").expand_path
6
8
  DEFAULT_SOURCES = {
7
9
  finals: "https://datacenter.iers.org/data/latestVersion/finals.all.iau2000.txt",
8
- leap_seconds: "https://hpiers.obspm.fr/iers/bul/bulc/Leap_Second.dat"
10
+ leap_seconds: "https://hpiers.obspm.fr/iers/bul/bulc/Leap_Second.dat",
11
+ c04: "https://hpiers.obspm.fr/iers/eop/eopc04/eopc04.1962-now"
9
12
  }.freeze
10
13
  DEFAULT_DOWNLOAD_TIMEOUT = 30
11
14
  DEFAULT_INTERPOLATION = :lagrange
12
15
  DEFAULT_LAGRANGE_ORDER = 4
13
16
  INTERPOLATION_METHODS = %i[lagrange linear].freeze
17
+ DEFAULT_EOP_SOURCE = :c04
18
+ EOP_SOURCES = %i[c04 finals].freeze
14
19
 
15
20
  # @return [Pathname]
16
21
  attr_reader :cache_dir
@@ -22,6 +27,11 @@ module IERS
22
27
  attr_reader :finals_path
23
28
  # @return [Pathname, nil]
24
29
  attr_reader :leap_second_path
30
+ # @return [Pathname, nil]
31
+ attr_reader :c04_path
32
+ # @return [Symbol] +:c04+ to use the C04 series wherever it reaches,
33
+ # or +:finals+ to use finals2000A only
34
+ attr_reader :eop_source
25
35
  # @return [Symbol] +:lagrange+ or +:linear+
26
36
  attr_reader :interpolation
27
37
  # @return [Integer]
@@ -33,6 +43,8 @@ module IERS
33
43
  @download_timeout = DEFAULT_DOWNLOAD_TIMEOUT
34
44
  @finals_path = nil
35
45
  @leap_second_path = nil
46
+ @c04_path = nil
47
+ @eop_source = DEFAULT_EOP_SOURCE
36
48
  @interpolation = DEFAULT_INTERPOLATION
37
49
  @lagrange_order = DEFAULT_LAGRANGE_ORDER
38
50
  end
@@ -68,6 +80,20 @@ module IERS
68
80
  invalidate(:leap_seconds)
69
81
  end
70
82
 
83
+ def c04_path=(value)
84
+ @c04_path = value && Pathname(value)
85
+ invalidate(:c04)
86
+ end
87
+
88
+ def eop_source=(value)
89
+ unless EOP_SOURCES.include?(value)
90
+ raise ConfigurationError,
91
+ "eop_source must be one of: #{EOP_SOURCES.join(", ")}"
92
+ end
93
+
94
+ @eop_source = value
95
+ end
96
+
71
97
  def interpolation=(value)
72
98
  unless INTERPOLATION_METHODS.include?(value)
73
99
  raise ConfigurationError,
data/lib/iers/data.rb CHANGED
@@ -1,23 +1,29 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "pathname"
4
+
3
5
  module IERS
4
6
  module Data
5
7
  FILENAMES = {
6
8
  finals: "finals2000A.all",
7
- leap_seconds: "Leap_Second.dat"
9
+ leap_seconds: "Leap_Second.dat",
10
+ c04: "eopc04.1962-now"
8
11
  }.freeze
9
12
 
13
+ C04_CELESTIAL_POLE_START_MJD = 45700.0
14
+
10
15
  BUNDLED_DIR = Pathname(__dir__).join("..", "..", "data").expand_path
11
16
 
12
17
  @mutex = Mutex.new
13
18
  @finals = nil
19
+ @eop = nil
14
20
  @leap_second_table = nil
15
21
 
16
22
  module_function
17
23
 
18
24
  # @return [Boolean]
19
25
  def loaded?
20
- !@finals.nil? || !@leap_second_table.nil?
26
+ [@finals, @eop, @leap_second_table].any?
21
27
  end
22
28
 
23
29
  # @param sources [Array<Symbol>] data sources to update (default: all)
@@ -91,11 +97,24 @@ module IERS
91
97
 
92
98
  # @return [Array<Parsers::Finals::Entry>]
93
99
  def finals_entries
100
+ @mutex.synchronize { load_finals }
101
+ end
102
+
103
+ # @return [Array<Parsers::C04::Entry>]
104
+ def c04_entries
105
+ Parsers::C04.parse(resolve_read_path(:c04)).freeze
106
+ end
107
+
108
+ # @return [Array<Parsers::Finals::Entry>]
109
+ def eop_entries
110
+ source = IERS.configuration.eop_source
111
+
94
112
  @mutex.synchronize do
95
- @finals ||= begin
96
- path = resolve_read_path(:finals)
97
- Parsers::Finals.parse(path).freeze
113
+ if @eop.nil? || @eop[:source] != source
114
+ @eop = {source: source, entries: build_eop_entries(source)}
98
115
  end
116
+
117
+ @eop[:entries]
99
118
  end
100
119
  end
101
120
 
@@ -126,7 +145,62 @@ module IERS
126
145
  when :leap_seconds
127
146
  config.leap_second_path ||
128
147
  config.cache_dir.join(FILENAMES[:leap_seconds])
148
+ when :c04
149
+ config.c04_path || config.cache_dir.join(FILENAMES[:c04])
150
+ end
151
+ end
152
+
153
+ def load_finals
154
+ @finals ||= Parsers::Finals.parse(resolve_read_path(:finals)).freeze
155
+ end
156
+
157
+ def build_eop_entries(source)
158
+ return load_finals if source == :finals
159
+
160
+ finals = @finals || Parsers::Finals.parse(resolve_read_path(:finals))
161
+ c04 = read_c04
162
+ return finals.freeze if c04.nil?
163
+
164
+ by_mjd = c04.to_h { |entry| [entry.mjd, entry] }
165
+ finals.map { |entry| overlay_c04(entry, by_mjd[entry.mjd]) }.freeze
166
+ end
167
+
168
+ def read_c04
169
+ paths = [resolve_read_path(:c04), BUNDLED_DIR.join(FILENAMES[:c04])]
170
+
171
+ paths.uniq.each do |path|
172
+ return Parsers::C04.parse(path)
173
+ rescue DataError, SystemCallError
174
+ next
175
+ end
176
+
177
+ nil
178
+ end
179
+
180
+ def overlay_c04(entry, c04)
181
+ return entry if c04.nil?
182
+ return entry unless entry.pm_flag == "I" && entry.ut1_flag == "I"
183
+
184
+ values = {
185
+ pm_flag: "I",
186
+ pm_x: c04.pm_x, pm_x_error: c04.pm_x_error,
187
+ pm_y: c04.pm_y, pm_y_error: c04.pm_y_error,
188
+ ut1_flag: "I",
189
+ ut1_utc: c04.ut1_utc, ut1_utc_error: c04.ut1_utc_error,
190
+ lod: c04.lod * 1000, lod_error: c04.lod_error * 1000,
191
+ bulletin_b_pm_x: nil, bulletin_b_pm_y: nil, bulletin_b_ut1_utc: nil
192
+ }
193
+
194
+ if c04.mjd >= C04_CELESTIAL_POLE_START_MJD
195
+ values.merge!(
196
+ nutation_flag: "I",
197
+ dx: c04.dx * 1000, dx_error: c04.dx_error * 1000,
198
+ dy: c04.dy * 1000, dy_error: c04.dy_error * 1000,
199
+ bulletin_b_dx: nil, bulletin_b_dy: nil
200
+ )
129
201
  end
202
+
203
+ entry.with(**values)
130
204
  end
131
205
 
132
206
  def resolve_read_path(source, config = IERS.configuration)
@@ -144,7 +218,7 @@ module IERS
144
218
  end
145
219
 
146
220
  def custom_paths_configured?(config)
147
- config.finals_path || config.leap_second_path
221
+ config.finals_path || config.leap_second_path || config.c04_path
148
222
  end
149
223
 
150
224
  def cache_exists?(config)
@@ -172,6 +246,7 @@ module IERS
172
246
 
173
247
  @mutex.synchronize do
174
248
  @finals = nil if sources.include?(:finals)
249
+ @eop = nil if sources.intersect?(%i[finals c04])
175
250
  @leap_second_table = nil if sources.include?(:leap_seconds)
176
251
  end
177
252
 
@@ -192,6 +267,10 @@ module IERS
192
267
  private_class_method :validate_sources!,
193
268
  :resolve_path,
194
269
  :resolve_read_path,
270
+ :load_finals,
271
+ :read_c04,
272
+ :build_eop_entries,
273
+ :overlay_c04,
195
274
  :validate_source!,
196
275
  :custom_paths_configured?,
197
276
  :cache_exists?,
data/lib/iers/delta_t.rb CHANGED
@@ -81,7 +81,7 @@ module IERS
81
81
  # The EOP series is the better source wherever it reaches, so it is asked
82
82
  # first; the polynomial covers whatever is left within its own validity.
83
83
  def series_covers?(mjd)
84
- entries = Data.finals_entries
84
+ entries = Data.eop_entries
85
85
  return false if entries.empty?
86
86
 
87
87
  mjd.between?(entries.first.mjd, entries.last.mjd)
@@ -96,7 +96,7 @@ module IERS
96
96
  end
97
97
 
98
98
  def raise_uncovered!(query_mjd)
99
- entries = Data.finals_entries
99
+ entries = Data.eop_entries
100
100
  series =
101
101
  if entries.empty?
102
102
  "is empty"
@@ -3,6 +3,7 @@
3
3
  require "net/http"
4
4
  require "uri"
5
5
  require "tempfile"
6
+ require "pathname"
6
7
 
7
8
  module IERS
8
9
  class Downloader
data/lib/iers/eop.rb CHANGED
@@ -65,7 +65,7 @@ module IERS
65
65
  def between(start_date, end_date)
66
66
  start_mjd = TimeScale.to_mjd(start_date)
67
67
  end_mjd = TimeScale.to_mjd(end_date)
68
- entries = Data.finals_entries
68
+ entries = Data.eop_entries
69
69
 
70
70
  EopLookup
71
71
  .range(entries, start_mjd, end_mjd)
@@ -7,7 +7,7 @@ module IERS
7
7
 
8
8
  def resolve(input, jd:, mjd:, interpolation:)
9
9
  query_mjd = TimeScale.to_mjd(input, jd: jd, mjd: mjd)
10
- entries = Data.finals_entries
10
+ entries = Data.eop_entries
11
11
  method = interpolation || IERS.configuration.interpolation
12
12
 
13
13
  window = case method
@@ -43,7 +43,7 @@ module IERS
43
43
  def between(start_date, end_date)
44
44
  start_mjd = TimeScale.to_mjd(start_date)
45
45
  end_mjd = TimeScale.to_mjd(end_date)
46
- entries = Data.finals_entries
46
+ entries = Data.eop_entries
47
47
 
48
48
  EopLookup
49
49
  .range(entries, start_mjd, end_mjd)
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+ require "pathname"
5
+
6
+ module IERS
7
+ module Parsers
8
+ module C04
9
+ Entry = ::Data.define(
10
+ :date, :mjd,
11
+ :pm_x, :pm_y, :ut1_utc, :dx, :dy, :pm_x_rate, :pm_y_rate, :lod,
12
+ :pm_x_error, :pm_y_error, :ut1_utc_error, :dx_error, :dy_error,
13
+ :pm_x_rate_error, :pm_y_rate_error, :lod_error
14
+ )
15
+
16
+ FIELD_COUNT = 21
17
+ HEADER_PREFIX = "# YR"
18
+ HEADER_COLUMNS = ["dX(\")", "dY(\")"].freeze
19
+
20
+ module_function
21
+
22
+ def parse(path)
23
+ path = Pathname(path)
24
+
25
+ unless path.exist?
26
+ raise FileNotFoundError.new(
27
+ "File not found: #{path}",
28
+ path: path.to_s
29
+ )
30
+ end
31
+
32
+ entries = []
33
+ header_seen = false
34
+
35
+ path.each_line.with_index(1) do |raw, line_number|
36
+ line = raw.scrub
37
+ next if line.strip.empty?
38
+
39
+ if line.start_with?("#")
40
+ if line.start_with?(HEADER_PREFIX)
41
+ check_header!(line, path, line_number)
42
+ header_seen = true
43
+ end
44
+ next
45
+ end
46
+
47
+ unless header_seen
48
+ raise ParseError.new(
49
+ "Failed to parse line #{line_number}: no column header " \
50
+ "before the first data line",
51
+ path: path.to_s,
52
+ line_number: line_number
53
+ )
54
+ end
55
+
56
+ entries << parse_line(line, path, line_number)
57
+ end
58
+
59
+ entries.freeze
60
+ end
61
+
62
+ def check_header!(line, path, line_number)
63
+ columns = line.split
64
+ missing = HEADER_COLUMNS.reject { |column| columns.include?(column) }
65
+ return if missing.empty?
66
+
67
+ raise ParseError.new(
68
+ "Failed to parse line #{line_number}: expected columns " \
69
+ "#{HEADER_COLUMNS.join(", ")}, missing #{missing.join(", ")}",
70
+ path: path.to_s,
71
+ line_number: line_number
72
+ )
73
+ end
74
+
75
+ def parse_line(line, path, line_number)
76
+ fields = line.split
77
+
78
+ unless fields.size == FIELD_COUNT
79
+ raise ParseError.new(
80
+ "Failed to parse line #{line_number}: expected " \
81
+ "#{FIELD_COUNT} fields, got #{fields.size}",
82
+ path: path.to_s,
83
+ line_number: line_number
84
+ )
85
+ end
86
+
87
+ year, month, day, hour = fields[0, 4].map { |f| Integer(f, 10) }
88
+
89
+ unless hour.zero?
90
+ raise ParseError.new(
91
+ "Failed to parse line #{line_number}: expected values at 0h " \
92
+ "UTC, got #{hour}h",
93
+ path: path.to_s,
94
+ line_number: line_number
95
+ )
96
+ end
97
+
98
+ values = fields[4..].map { |f| Float(f) }
99
+
100
+ Entry.new(
101
+ Date.new(year, month, day),
102
+ *values
103
+ )
104
+ rescue ArgumentError, TypeError => e
105
+ raise ParseError.new(
106
+ "Failed to parse line #{line_number}: #{e.message}",
107
+ path: path.to_s,
108
+ line_number: line_number
109
+ )
110
+ end
111
+
112
+ private_class_method :check_header!, :parse_line
113
+ end
114
+ end
115
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "date"
4
+ require "pathname"
4
5
 
5
6
  module IERS
6
7
  module Parsers
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "date"
4
+ require "pathname"
4
5
 
5
6
  module IERS
6
7
  module Parsers
data/lib/iers/parsers.rb CHANGED
@@ -2,3 +2,4 @@
2
2
 
3
3
  require_relative "parsers/finals"
4
4
  require_relative "parsers/leap_second"
5
+ require_relative "parsers/c04"
@@ -14,8 +14,9 @@ module IERS
14
14
  include HasDate
15
15
  include HasDataQuality
16
16
 
17
- # Polar motion rotation matrix W per IERS Conventions 2010, Section 5.4.1:
18
- # W = R3(-s') * R2(xp) * R1(yp)
17
+ # Polar motion rotation matrix from the TIRS to the ITRS, including the
18
+ # TIO locator s'. This is ERFA's +pom00+, the transpose of
19
+ # W = R3(-s') * R2(xp) * R1(yp) in IERS Conventions 2010, Section 5.4.1.
19
20
  #
20
21
  # All elements use exact trigonometry — no small-angle approximation.
21
22
  #
@@ -83,7 +84,7 @@ module IERS
83
84
  def between(start_date, end_date)
84
85
  start_mjd = TimeScale.to_mjd(start_date)
85
86
  end_mjd = TimeScale.to_mjd(end_date)
86
- entries = Data.finals_entries
87
+ entries = Data.eop_entries
87
88
 
88
89
  EopLookup
89
90
  .range(entries, start_mjd, end_mjd)
@@ -4,9 +4,10 @@ module IERS
4
4
  module TerrestrialRotation
5
5
  module_function
6
6
 
7
- # Compute R(ERA) × W, the rotation from ITRS to TIRS
8
- # (IERS Conventions 2010, eq. 5.1), combining the Earth Rotation
9
- # Angle and polar motion matrices.
7
+ # Compute W × R3(ERA), the rotation from the CIRS to the ITRS
8
+ # (IERS Conventions 2010, eq. 5.1, transposed), combining the Earth
9
+ # Rotation Angle and the polar motion matrix. Matches ERFA's +c2tcio+ with
10
+ # the CIRS as the celestial frame.
10
11
  #
11
12
  # @param input [Time, Date, DateTime, nil]
12
13
  # @param jd [Float, nil] Julian Date
@@ -21,7 +22,7 @@ module IERS
21
22
  mjd: query_mjd, interpolation: interpolation
22
23
  )
23
24
 
24
- multiply(r3_matrix(era), w)
25
+ multiply(w, r3_matrix(era))
25
26
  end
26
27
 
27
28
  def r3_matrix(angle)
@@ -6,6 +6,7 @@ module IERS
6
6
  # @api private
7
7
  module TimeScale
8
8
  JD_MJD_OFFSET = 2_400_000.5
9
+ JD_MJD_OFFSET_EXACT = Rational(4_800_001, 2)
9
10
  JD_J2000 = 2_451_545.0
10
11
  MJD_J2000 = 51_544.5
11
12
  DAYS_PER_JULIAN_CENTURY = 36_525.0
@@ -32,9 +33,9 @@ module IERS
32
33
  elsif jd
33
34
  to_float(jd) - JD_MJD_OFFSET
34
35
  elsif input.is_a?(Time)
35
- input.to_datetime.ajd.to_f - JD_MJD_OFFSET
36
+ (input.to_datetime.ajd - JD_MJD_OFFSET_EXACT).to_f
36
37
  elsif input.is_a?(Date)
37
- input.ajd.to_f - JD_MJD_OFFSET
38
+ (input.ajd - JD_MJD_OFFSET_EXACT).to_f
38
39
  else
39
40
  raise ArgumentError,
40
41
  "Expected Time, Date, DateTime, jd: or mjd: keyword, " \
data/lib/iers/ut1.rb CHANGED
@@ -43,7 +43,7 @@ module IERS
43
43
  def between(start_date, end_date)
44
44
  start_mjd = TimeScale.to_mjd(start_date)
45
45
  end_mjd = TimeScale.to_mjd(end_date)
46
- entries = Data.finals_entries
46
+ entries = Data.eop_entries
47
47
 
48
48
  EopLookup
49
49
  .range(entries, start_mjd, end_mjd)
data/lib/iers/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IERS
4
- VERSION = "0.3.1"
4
+ VERSION = "0.4.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rémy Hannequin
@@ -150,6 +150,7 @@ files:
150
150
  - README.md
151
151
  - Rakefile
152
152
  - data/Leap_Second.dat
153
+ - data/eopc04.1962-now
153
154
  - data/finals2000A.all
154
155
  - lib/iers.rb
155
156
  - lib/iers/celestial_pole_offset.rb
@@ -170,6 +171,7 @@ files:
170
171
  - lib/iers/leap_second.rb
171
172
  - lib/iers/length_of_day.rb
172
173
  - lib/iers/parsers.rb
174
+ - lib/iers/parsers/c04.rb
173
175
  - lib/iers/parsers/finals.rb
174
176
  - lib/iers/parsers/leap_second.rb
175
177
  - lib/iers/polar_motion.rb