ephem 0.5.2 → 0.6.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.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +185 -0
  3. data/README.md +171 -15
  4. data/bin/ruby-ephem +1 -2
  5. data/lib/ephem/cli.rb +174 -100
  6. data/lib/ephem/computation/chebyshev_polynomial.rb +82 -110
  7. data/lib/ephem/core/calendar_calculations.rb +17 -26
  8. data/lib/ephem/core/constants/bodies.rb +2 -0
  9. data/lib/ephem/core/constants/time.rb +1 -1
  10. data/lib/ephem/core/orientation.rb +23 -7
  11. data/lib/ephem/core/state.rb +48 -13
  12. data/lib/ephem/core/vector.rb +92 -24
  13. data/lib/ephem/download.rb +77 -14
  14. data/lib/ephem/error.rb +3 -3
  15. data/lib/ephem/excerpt.rb +126 -105
  16. data/lib/ephem/io/binary_reader.rb +29 -8
  17. data/lib/ephem/io/daf.rb +1 -4
  18. data/lib/ephem/io/endianness_manager.rb +5 -18
  19. data/lib/ephem/io/record_data.rb +2 -20
  20. data/lib/ephem/io/record_parser.rb +1 -1
  21. data/lib/ephem/io/summary_manager.rb +24 -15
  22. data/lib/ephem/pck.rb +24 -7
  23. data/lib/ephem/segments/base_segment.rb +38 -20
  24. data/lib/ephem/segments/chebyshev_type2.rb +105 -58
  25. data/lib/ephem/segments/coverage.rb +66 -0
  26. data/lib/ephem/segments/orientation_segment.rb +17 -15
  27. data/lib/ephem/segments/orientation_source.rb +4 -2
  28. data/lib/ephem/segments/position_chain.rb +124 -0
  29. data/lib/ephem/segments/position_group.rb +24 -0
  30. data/lib/ephem/segments/segment.rb +87 -18
  31. data/lib/ephem/segments/segment_group.rb +19 -5
  32. data/lib/ephem/spk.rb +134 -11
  33. data/lib/ephem/version.rb +1 -1
  34. data/lib/ephem.rb +5 -3
  35. metadata +4 -10
  36. data/.rspec +0 -3
  37. data/.standard.yml +0 -3
  38. data/.tool-versions +0 -1
  39. data/CODE_OF_CONDUCT.md +0 -132
  40. data/Rakefile +0 -12
  41. data/benchmarks/run.rb +0 -431
  42. data/bin/console +0 -11
  43. data/bin/setup +0 -8
  44. data/lib/tasks/validate_accuracy.rake +0 -47
data/lib/ephem/cli.rb CHANGED
@@ -5,41 +5,53 @@ require "date"
5
5
 
6
6
  module Ephem
7
7
  class CLI
8
+ SUCCESS = 0
9
+ FAILURE = 1
10
+ EXCERPT_USAGE = "Usage: ruby-ephem excerpt [options] " \
11
+ "START_DATE END_DATE INPUT_FILE OUTPUT_FILE"
12
+ EXCERPT_ARGUMENT_COUNT = 4
13
+ DATE_FORMAT = /\A([+-]?\d+)-(\d{1,2})-(\d{1,2})\z/
14
+
15
+ # Converts a YYYY-MM-DD date to the Julian Date at midnight (00:00) of
16
+ # that day.
17
+ #
18
+ # Dates are read in the proleptic Gregorian calendar, as in ISO 8601, so
19
+ # dates before the 1582 calendar reform are not read as Julian calendar
20
+ # dates.
21
+ #
22
+ # @param date_str [String] Date to convert, such as "2000-01-01"
23
+ # @return [Float] Julian Date at midnight, such as 2451544.5
24
+ # @raise [Date::Error] if the date is not a valid YYYY-MM-DD date
8
25
  def self.gregorian_to_julian(date_str)
9
- date = Date.parse(date_str)
10
-
11
- a = (14 - date.month) / 12
12
- y = date.year + 4800 - a
13
- m = date.month + 12 * a - 3
14
-
15
- date.day +
16
- ((153 * m + 2) / 5) +
17
- 365 * y +
18
- (y / 4) -
19
- (y / 100) +
20
- (y / 400) -
21
- 32045
26
+ match = DATE_FORMAT.match(date_str)
27
+ raise Date::Error, "invalid date: #{date_str}" unless match
28
+
29
+ year, month, day = match.captures.map { |part| Integer(part, 10) }
30
+ Date.new(year, month, day, Date::GREGORIAN).ajd.to_f
22
31
  end
23
32
 
33
+ # Runs the command given on the command line.
34
+ #
35
+ # @param args [Array<String>] Command line arguments
36
+ # @return [Integer] Exit status, 0 on success and 1 on failure
24
37
  def self.start(args)
25
- if args.empty?
26
- puts "Usage: ruby-ephem excerpt [options] START_DATE END_DATE INPUT_FILE OUTPUT_FILE"
27
- puts "For help: ruby-ephem help"
28
- return
29
- end
30
-
31
- if args[0] == "help"
38
+ command, *command_args = args
39
+
40
+ case command
41
+ when nil
42
+ warn EXCERPT_USAGE
43
+ warn "For help: ruby-ephem help"
44
+ FAILURE
45
+ when "help"
32
46
  show_help
33
- return
34
- end
35
-
36
- if args[0] == "excerpt"
37
- handle_excerpt(args[1..])
38
- return
47
+ SUCCESS
48
+ when "excerpt"
49
+ handle_excerpt(command_args)
50
+ else
51
+ warn "Unknown command: #{command}"
52
+ warn "Available commands: excerpt, help"
53
+ FAILURE
39
54
  end
40
-
41
- puts "Unknown command: #{args[0]}"
42
- puts "Available commands: excerpt, help"
43
55
  end
44
56
 
45
57
  def self.show_help
@@ -53,6 +65,9 @@ module Ephem
53
65
  Excerpt command:
54
66
  ruby-ephem excerpt [options] START_DATE END_DATE INPUT_FILE OUTPUT_FILE
55
67
 
68
+ START_DATE and END_DATE are read in the Gregorian calendar and
69
+ converted to the Julian Date at midnight of that day.
70
+
56
71
  Options:
57
72
  --targets TARGET_IDS - Comma-separated list of target/body IDs to
58
73
  include (default: all)
@@ -66,98 +81,157 @@ module Ephem
66
81
  HELP
67
82
  end
68
83
 
69
- # Handle the excerpt command
84
+ # Handles the excerpt command.
85
+ #
86
+ # @param args [Array<String>] Arguments following the command name
87
+ # @return [Integer] Exit status, 0 on success and 1 on failure
70
88
  def self.handle_excerpt(args)
71
- # Parse options
72
89
  options = {target_ids: nil, debug: false}
73
-
74
- option_parser = OptionParser.new do |opts|
75
- opts.banner = "Usage: ruby-ephem excerpt [options] START_DATE END_DATE INPUT_FILE OUTPUT_FILE"
76
-
77
- opts.on("--targets TARGET_IDS", "Comma-separated list of target IDs to include") do |targets|
78
- options[:target_ids] = targets.split(",").map(&:strip).map(&:to_i)
79
- end
80
-
81
- opts.on("--debug", "Enable debug output") do
82
- options[:debug] = true
83
- end
84
- end
90
+ option_parser = excerpt_option_parser(options)
85
91
 
86
92
  begin
87
- option_parser.parse!(args)
88
- rescue OptionParser::InvalidOption => e
89
- puts e.message
90
- puts option_parser
91
- return
93
+ arguments = option_parser.parse(args)
94
+ rescue OptionParser::ParseError => error
95
+ warn error.message
96
+ warn option_parser
97
+ return FAILURE
92
98
  end
93
99
 
94
- if args.size < 4
95
- puts "Not enough arguments."
96
- puts option_parser
97
- return
100
+ if arguments.size != EXCERPT_ARGUMENT_COUNT
101
+ warn "Expected #{EXCERPT_ARGUMENT_COUNT} arguments, " \
102
+ "got #{arguments.size}."
103
+ warn option_parser
104
+ return FAILURE
98
105
  end
99
106
 
100
- start_date_str = args[0]
101
- end_date_str = args[1]
102
- input_file = args[2]
103
- output_file = args[3]
107
+ start_date_str, end_date_str, input_file, output_file = arguments
108
+
109
+ unless File.file?(input_file)
110
+ warn "Error: Input file '#{input_file}' does not exist " \
111
+ "or is not a file."
112
+ return FAILURE
113
+ end
104
114
 
105
- unless File.exist?(input_file)
106
- puts "Error: Input file '#{input_file}' does not exist."
107
- return
115
+ if File.exist?(output_file) && File.identical?(input_file, output_file)
116
+ warn "Error: Output file must be different from the input file."
117
+ return FAILURE
108
118
  end
109
119
 
110
120
  begin
111
121
  start_jd = gregorian_to_julian(start_date_str)
112
122
  end_jd = gregorian_to_julian(end_date_str)
113
- rescue Date::Error => e
114
- puts "Error parsing dates: #{e.message}"
115
- puts "Dates should be in YYYY-MM-DD format."
116
- return
123
+ rescue Date::Error => error
124
+ warn "Error parsing dates: #{error.message}"
125
+ warn "Dates should be in YYYY-MM-DD format."
126
+ return FAILURE
117
127
  end
118
128
 
119
- begin
120
- puts "Creating excerpt from #{input_file} to #{output_file}..."
121
- puts "Date range: #{start_date_str} to #{end_date_str} (JD #{start_jd} to #{end_jd})"
122
- if options[:target_ids]
123
- puts "Including targets: #{options[:target_ids].join(", ")}"
124
- else
125
- puts "Including all targets"
129
+ if start_jd > end_jd
130
+ warn "Error: START_DATE must not be after END_DATE."
131
+ return FAILURE
132
+ end
133
+
134
+ create_excerpt(
135
+ input_file: input_file,
136
+ output_file: output_file,
137
+ start_date_str: start_date_str,
138
+ end_date_str: end_date_str,
139
+ start_jd: start_jd,
140
+ end_jd: end_jd,
141
+ target_ids: options[:target_ids],
142
+ debug: options[:debug]
143
+ )
144
+ end
145
+
146
+ def self.excerpt_option_parser(options)
147
+ OptionParser.new do |parser|
148
+ parser.banner = EXCERPT_USAGE
149
+
150
+ parser.on(
151
+ "--targets TARGET_IDS",
152
+ "Comma-separated list of target IDs to include"
153
+ ) do |targets|
154
+ options[:target_ids] = parse_target_ids(targets)
126
155
  end
127
156
 
128
- kernel = open_kernel(input_file)
129
-
130
- excerpt_kernel = kernel.excerpt(
131
- output_path: output_file,
132
- start_jd: start_jd,
133
- end_jd: end_jd,
134
- target_ids: options[:target_ids],
135
- debug: options[:debug]
136
- )
137
-
138
- puts "Excerpt created successfully!"
139
- puts "Original segments: #{kernel.segments.size}"
140
- puts "Excerpt segments: #{excerpt_kernel.segments.size}"
141
-
142
- original_size = File.size(input_file)
143
- excerpt_size = File.size(output_file)
144
- reduction_percentage =
145
- ((original_size - excerpt_size) / original_size.to_f * 100).round(2)
146
-
147
- puts "File size reduced by #{reduction_percentage}%"
148
- puts "Original: #{original_size} bytes"
149
- puts "Excerpt: #{excerpt_size} bytes"
150
-
151
- kernel.close
152
- excerpt_kernel.close
153
- rescue => e
154
- puts "Error creating excerpt: #{e.message}"
155
- puts e.backtrace if options[:debug]
157
+ parser.on("--debug", "Enable debug output") do
158
+ options[:debug] = true
159
+ end
156
160
  end
157
161
  end
158
162
 
163
+ # Parses a comma-separated list of integer target IDs.
164
+ #
165
+ # @param value [String] Value of the --targets option, such as "3,10,399"
166
+ # @return [Array<Integer>] Target IDs
167
+ # @raise [OptionParser::InvalidArgument] if the list is empty or contains
168
+ # a value that is not an integer
169
+ def self.parse_target_ids(value)
170
+ target_ids = value.split(",", -1).map { |id| Integer(id.strip, 10) }
171
+ raise ArgumentError, "no target IDs" if target_ids.empty?
172
+
173
+ target_ids
174
+ rescue ArgumentError
175
+ raise OptionParser::InvalidArgument, value
176
+ end
177
+
178
+ def self.create_excerpt(
179
+ input_file:,
180
+ output_file:,
181
+ start_date_str:,
182
+ end_date_str:,
183
+ start_jd:,
184
+ end_jd:,
185
+ target_ids:,
186
+ debug:
187
+ )
188
+ puts "Creating excerpt from #{input_file} to #{output_file}..."
189
+ puts "Date range: #{start_date_str} to #{end_date_str} " \
190
+ "(JD #{start_jd} to #{end_jd})"
191
+ if target_ids
192
+ puts "Including targets: #{target_ids.join(", ")}"
193
+ else
194
+ puts "Including all targets"
195
+ end
196
+
197
+ kernel = open_kernel(input_file)
198
+ excerpt_kernel = kernel.excerpt(
199
+ output_path: output_file,
200
+ start_jd: start_jd,
201
+ end_jd: end_jd,
202
+ target_ids: target_ids,
203
+ debug: debug
204
+ )
205
+
206
+ puts "Excerpt created successfully!"
207
+ puts "Original segments: #{kernel.segments.size}"
208
+ puts "Excerpt segments: #{excerpt_kernel.segments.size}"
209
+ print_size_reduction(input_file, output_file)
210
+
211
+ SUCCESS
212
+ rescue => error
213
+ warn "Error creating excerpt: #{error.message}"
214
+ warn error.backtrace if debug
215
+ FAILURE
216
+ ensure
217
+ excerpt_kernel&.close
218
+ kernel&.close
219
+ end
220
+
221
+ def self.print_size_reduction(input_file, output_file)
222
+ original_size = File.size(input_file)
223
+ excerpt_size = File.size(output_file)
224
+ reduction_percentage =
225
+ ((original_size - excerpt_size) / original_size.to_f * 100).round(2)
226
+
227
+ puts "File size reduced by #{reduction_percentage}%"
228
+ puts "Original: #{original_size} bytes"
229
+ puts "Excerpt: #{excerpt_size} bytes"
230
+ end
231
+
159
232
  def self.open_kernel(path)
160
- daf = Ephem::IO::DAF.new(File.open(path, "rb"))
233
+ file = File.open(path, "rb")
234
+ daf = Ephem::IO::DAF.new(file)
161
235
 
162
236
  if daf.file_type == :pck
163
237
  Ephem::PCK.new(daf: daf)
@@ -165,7 +239,7 @@ module Ephem
165
239
  Ephem::SPK.new(daf: daf)
166
240
  end
167
241
  rescue
168
- daf&.close
242
+ (daf || file)&.close
169
243
  raise
170
244
  end
171
245
  end
@@ -13,134 +13,106 @@ module Ephem
13
13
  ##
14
14
  # Evaluates a 3D Chebyshev polynomial at a given normalized time.
15
15
  #
16
- # @param coeffs [Array<Array<Float>>] Array of coefficients; shape is
17
- # [n_terms][3].
18
- # @param t [Float] The normalized independent variable, in [-1, 1].
19
- # @return [Array<Float>] The 3-vector result at t: [x, y, z]
20
- def self.evaluate(coeffs, t)
21
- n = coeffs.size
22
- b1x = b1y = b1z = 0.0
23
- b2x = b2y = b2z = 0.0
24
-
25
- t2 = 2.0 * t
26
- k = n - 1
27
- while k > 0
28
- c = coeffs[k]
29
- c0 = c[0]
30
- c1 = c[1]
31
- c2 = c[2]
32
- tx = t2 * b1x - b2x + c0
33
- ty = t2 * b1y - b2y + c1
34
- tz = t2 * b1z - b2z + c2
35
- b2x = b1x
36
- b2y = b1y
37
- b2z = b1z
38
- b1x = tx
39
- b1y = ty
40
- b1z = tz
41
- k -= 1
42
- end
43
-
44
- c0, c1, c2 = coeffs[0]
45
- [t * b1x - b2x + c0, t * b1y - b2y + c1, t * b1z - b2z + c2]
46
- end
47
-
48
- ##
49
- # Evaluates the time derivative of a 3D Chebyshev polynomial at a given
50
- # normalized time.
51
- #
52
- # @param coeffs [Array<Array<Float>>] Array of coefficients; shape is
53
- # [n_terms][3].
54
- # @param t [Float] The normalized independent variable (in [-1, 1]).
55
- # @param radius [Float] The half-length of the time interval (seconds).
56
- # @return [Array<Float>] The 3-vector derivative (velocity), in units per
57
- # day.
58
- def self.evaluate_derivative(coeffs, t, radius)
59
- n = coeffs.size
60
- return [0.0, 0.0, 0.0] if n < 2
16
+ # @param coefficients [Array<Array<Float>>] Array of coefficients; shape
17
+ # is [n_terms][3].
18
+ # @param normalized_time [Float] The normalized independent variable, in
19
+ # [-1, 1].
20
+ # @return [Array<Float>] The 3-vector result at normalized_time:
21
+ # [x, y, z]
22
+ def self.evaluate(coefficients, normalized_time)
23
+ sum_x = sum_y = sum_z = 0.0
24
+ previous_sum_x = previous_sum_y = previous_sum_z = 0.0
61
25
 
62
- d1x = d1y = d1z = 0.0
63
- d2x = d2y = d2z = 0.0
64
-
65
- t2 = 2.0 * t
66
- k = n - 1
67
- while k > 0
68
- c = coeffs[k]
69
- c0 = c[0]
70
- c1 = c[1]
71
- c2 = c[2]
72
- k2 = 2 * k
73
- tx = t2 * d1x - d2x + k2 * c0
74
- ty = t2 * d1y - d2y + k2 * c1
75
- tz = t2 * d1z - d2z + k2 * c2
76
- d2x = d1x
77
- d2y = d1y
78
- d2z = d1z
79
- d1x = tx
80
- d1y = ty
81
- d1z = tz
82
- k -= 1
26
+ twice_time = 2.0 * normalized_time
27
+ degree = coefficients.size - 1
28
+ while degree > 0
29
+ term = coefficients[degree]
30
+ coefficient_x = term[0]
31
+ coefficient_y = term[1]
32
+ coefficient_z = term[2]
33
+ new_sum_x = twice_time * sum_x - previous_sum_x + coefficient_x
34
+ new_sum_y = twice_time * sum_y - previous_sum_y + coefficient_y
35
+ new_sum_z = twice_time * sum_z - previous_sum_z + coefficient_z
36
+ previous_sum_x = sum_x
37
+ previous_sum_y = sum_y
38
+ previous_sum_z = sum_z
39
+ sum_x = new_sum_x
40
+ sum_y = new_sum_y
41
+ sum_z = new_sum_z
42
+ degree -= 1
83
43
  end
84
44
 
85
- scale = Ephem::Core::Constants::Time::SECONDS_PER_DAY / (2.0 * radius)
86
- [d1x * scale, d1y * scale, d1z * scale]
45
+ coefficient_x, coefficient_y, coefficient_z = coefficients[0]
46
+ [
47
+ normalized_time * sum_x - previous_sum_x + coefficient_x,
48
+ normalized_time * sum_y - previous_sum_y + coefficient_y,
49
+ normalized_time * sum_z - previous_sum_z + coefficient_z
50
+ ]
87
51
  end
88
52
 
89
53
  ##
90
54
  # Evaluates a 3D Chebyshev polynomial and its time derivative in a single
91
- # pass. It runs the same value and derivative recurrences as {evaluate}
92
- # and {evaluate_derivative}, but fused into one loop so the coefficient
93
- # fetch and loop control are shared. Results are bit-for-bit identical to
94
- # calling the two methods separately.
55
+ # pass, sharing the coefficient fetch and loop control. The position is
56
+ # bit-for-bit identical to {evaluate}.
95
57
  #
96
- # @param coeffs [Array<Array<Float>>] coefficients; shape [n_terms][3].
97
- # @param t [Float] normalized independent variable, in [-1, 1].
58
+ # @param coefficients [Array<Array<Float>>] coefficients; shape
59
+ # [n_terms][3].
60
+ # @param normalized_time [Float] normalized independent variable, in
61
+ # [-1, 1].
98
62
  # @param radius [Float] half-length of the time interval (seconds).
99
63
  # @return [Array(Array<Float>, Array<Float>)] [position, velocity], with
100
64
  # velocity in units per day.
101
- def self.evaluate_with_derivative(coeffs, t, radius)
102
- n = coeffs.size
103
- b1x = b1y = b1z = 0.0
104
- b2x = b2y = b2z = 0.0
105
- d1x = d1y = d1z = 0.0
106
- d2x = d2y = d2z = 0.0
65
+ def self.evaluate_with_derivative(coefficients, normalized_time, radius)
66
+ sum_x = sum_y = sum_z = 0.0
67
+ previous_sum_x = previous_sum_y = previous_sum_z = 0.0
68
+ derivative_x = derivative_y = derivative_z = 0.0
69
+ previous_derivative_x = previous_derivative_y =
70
+ previous_derivative_z = 0.0
107
71
 
108
- t2 = 2.0 * t
109
- k = n - 1
110
- while k > 0
111
- c = coeffs[k]
112
- c0 = c[0]
113
- c1 = c[1]
114
- c2 = c[2]
115
- k2 = 2 * k
72
+ twice_time = 2.0 * normalized_time
73
+ degree = coefficients.size - 1
74
+ while degree > 0
75
+ term = coefficients[degree]
76
+ coefficient_x = term[0]
77
+ coefficient_y = term[1]
78
+ coefficient_z = term[2]
79
+ twice_degree = 2 * degree
116
80
 
117
- bx = t2 * b1x - b2x + c0
118
- by = t2 * b1y - b2y + c1
119
- bz = t2 * b1z - b2z + c2
120
- dx = t2 * d1x - d2x + k2 * c0
121
- dy = t2 * d1y - d2y + k2 * c1
122
- dz = t2 * d1z - d2z + k2 * c2
81
+ new_sum_x = twice_time * sum_x - previous_sum_x + coefficient_x
82
+ new_sum_y = twice_time * sum_y - previous_sum_y + coefficient_y
83
+ new_sum_z = twice_time * sum_z - previous_sum_z + coefficient_z
84
+ new_derivative_x = twice_time * derivative_x -
85
+ previous_derivative_x + twice_degree * coefficient_x
86
+ new_derivative_y = twice_time * derivative_y -
87
+ previous_derivative_y + twice_degree * coefficient_y
88
+ new_derivative_z = twice_time * derivative_z -
89
+ previous_derivative_z + twice_degree * coefficient_z
123
90
 
124
- b2x = b1x
125
- b2y = b1y
126
- b2z = b1z
127
- b1x = bx
128
- b1y = by
129
- b1z = bz
130
- d2x = d1x
131
- d2y = d1y
132
- d2z = d1z
133
- d1x = dx
134
- d1y = dy
135
- d1z = dz
136
- k -= 1
91
+ previous_sum_x = sum_x
92
+ previous_sum_y = sum_y
93
+ previous_sum_z = sum_z
94
+ sum_x = new_sum_x
95
+ sum_y = new_sum_y
96
+ sum_z = new_sum_z
97
+ previous_derivative_x = derivative_x
98
+ previous_derivative_y = derivative_y
99
+ previous_derivative_z = derivative_z
100
+ derivative_x = new_derivative_x
101
+ derivative_y = new_derivative_y
102
+ derivative_z = new_derivative_z
103
+ degree -= 1
137
104
  end
138
105
 
139
- c0, c1, c2 = coeffs[0]
140
- position = [t * b1x - b2x + c0, t * b1y - b2y + c1, t * b1z - b2z + c2]
106
+ coefficient_x, coefficient_y, coefficient_z = coefficients[0]
107
+ position = [
108
+ normalized_time * sum_x - previous_sum_x + coefficient_x,
109
+ normalized_time * sum_y - previous_sum_y + coefficient_y,
110
+ normalized_time * sum_z - previous_sum_z + coefficient_z
111
+ ]
141
112
 
142
113
  scale = Ephem::Core::Constants::Time::SECONDS_PER_DAY / (2.0 * radius)
143
- velocity = [d1x * scale, d1y * scale, d1z * scale]
114
+ velocity = [derivative_x * scale, derivative_y * scale,
115
+ derivative_z * scale]
144
116
 
145
117
  [position, velocity]
146
118
  end
@@ -32,40 +32,31 @@ module Ephem
32
32
  # Convert Julian Date to Gregorian calendar date using the algorithm
33
33
  # from Fundamentals of Astrodynamics (Bate, Mueller, White) pg. 42
34
34
 
35
- # Adjust Julian Date and calculate integer and fractional parts
36
- jd = julian_date + 0.5 # Add 0.5 to Julian Date for correct rounding
37
- z = jd.floor # Integer part of adjusted Julian Date
38
- f = jd - z # Fractional part of adjusted Julian Date
35
+ shifted_date = julian_date + 0.5
36
+ day_number = shifted_date.floor
37
+ day_fraction = shifted_date - day_number
39
38
 
40
- # Handle dates before and after the Gregorian calendar reform of 1582
41
- a = if z < 2299161 # 2299161 is the Julian Date of 1582-10-15T00:00:00Z
42
- z # Use integer part directly for Julian calendar dates
39
+ # 2299161 is the Julian Date of 1582-10-15T00:00:00Z
40
+ calendar_day_number = if day_number < 2299161
41
+ day_number
43
42
  else
44
43
  # 1867216.25 is the Julian Date of 1500-03-01T00:00:00Z
45
- alpha = ((z - 1867216.25) / 36524.25).floor
46
- # Adjust for Gregorian calendar
47
- z + 1 + alpha - (alpha / 4).floor
44
+ elapsed_centuries = ((day_number - 1867216.25) / 36524.25).floor
45
+ day_number + 1 + elapsed_centuries - (elapsed_centuries / 4).floor
48
46
  end
49
47
 
50
- # Calculate intermediate values
51
- # 1524 is the number of days from 1 BC to 1 AD
52
- b = a + 1524
53
- # 365.25 is the number of days in a Julian year
54
- c = ((b - 122.1) / 365.25).floor
55
- # Number of days since 1 BC
56
- d = (365.25 * c).floor
57
- # 30.6001 is the average number of days in a month
58
- e = ((b - d) / 30.6001).floor
48
+ days_since_epoch = calendar_day_number + 1524
49
+ year_count = ((days_since_epoch - 122.1) / 365.25).floor
50
+ days_in_whole_years = (365.25 * year_count).floor
51
+ month_count = ((days_since_epoch - days_in_whole_years) / 30.6001).floor
59
52
 
60
- # Extract day, month, and year values from intermediate values
61
- # Fractional part gives the day of month
62
- day = b - d - (30.6001 * e).floor + f
63
- # Months are numbered from 1 (January) to 12 (December)
64
- month = (e < 14) ? e - 1 : e - 13
53
+ day = days_since_epoch - days_in_whole_years -
54
+ (30.6001 * month_count).floor + day_fraction
55
+ month = (month_count < 14) ? month_count - 1 : month_count - 13
65
56
  # 4716 BC is the year of Julian Date 0
66
- year = (month > 2) ? c - 4716 : c - 4715
57
+ year = (month > 2) ? year_count - 4716 : year_count - 4715
67
58
 
68
- [year, month, day.to_i] # Return [year, month, day] as an array
59
+ [year, month, day.to_i]
69
60
  end
70
61
 
71
62
  # Formats a calendar date as YYYY-MM-DD
@@ -25,6 +25,8 @@ module Ephem
25
25
  URANUS = 799
26
26
  NEPTUNE = 899
27
27
  PLUTO = 999
28
+ TIME_CENTER = 1_000_000_000
29
+ TT_MINUS_TDB = 1_000_000_001
28
30
  end
29
31
  end
30
32
  end
@@ -4,7 +4,7 @@ module Ephem
4
4
  module Core
5
5
  module Constants
6
6
  module Time
7
- # Julian Date of J2000 epoch (2000-01-01T12:00:00Z)
7
+ # Julian Date of the J2000 epoch (2000-01-01 12:00:00 TT)
8
8
  J2000_EPOCH = 2451545.0
9
9
 
10
10
  # Number of seconds in a day