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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +185 -0
- data/README.md +171 -15
- data/bin/ruby-ephem +1 -2
- data/lib/ephem/cli.rb +174 -100
- data/lib/ephem/computation/chebyshev_polynomial.rb +82 -110
- data/lib/ephem/core/calendar_calculations.rb +17 -26
- data/lib/ephem/core/constants/bodies.rb +2 -0
- data/lib/ephem/core/constants/time.rb +1 -1
- data/lib/ephem/core/orientation.rb +23 -7
- data/lib/ephem/core/state.rb +48 -13
- data/lib/ephem/core/vector.rb +92 -24
- data/lib/ephem/download.rb +77 -14
- data/lib/ephem/error.rb +3 -3
- data/lib/ephem/excerpt.rb +126 -105
- data/lib/ephem/io/binary_reader.rb +29 -8
- data/lib/ephem/io/daf.rb +1 -4
- data/lib/ephem/io/endianness_manager.rb +5 -18
- data/lib/ephem/io/record_data.rb +2 -20
- data/lib/ephem/io/record_parser.rb +1 -1
- data/lib/ephem/io/summary_manager.rb +24 -15
- data/lib/ephem/pck.rb +24 -7
- data/lib/ephem/segments/base_segment.rb +38 -20
- data/lib/ephem/segments/chebyshev_type2.rb +105 -58
- data/lib/ephem/segments/coverage.rb +66 -0
- data/lib/ephem/segments/orientation_segment.rb +17 -15
- data/lib/ephem/segments/orientation_source.rb +4 -2
- data/lib/ephem/segments/position_chain.rb +124 -0
- data/lib/ephem/segments/position_group.rb +24 -0
- data/lib/ephem/segments/segment.rb +87 -18
- data/lib/ephem/segments/segment_group.rb +19 -5
- data/lib/ephem/spk.rb +134 -11
- data/lib/ephem/version.rb +1 -1
- data/lib/ephem.rb +5 -3
- metadata +4 -10
- data/.rspec +0 -3
- data/.standard.yml +0 -3
- data/.tool-versions +0 -1
- data/CODE_OF_CONDUCT.md +0 -132
- data/Rakefile +0 -12
- data/benchmarks/run.rb +0 -431
- data/bin/console +0 -11
- data/bin/setup +0 -8
- data/lib/tasks/validate_accuracy.rake +0 -47
data/lib/ephem/excerpt.rb
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "securerandom"
|
|
4
|
+
|
|
3
5
|
module Ephem
|
|
4
6
|
# The Excerpt class creates DAF excerpts (SPK or binary PCK) with reduced time
|
|
5
7
|
# spans and bodies. This is useful for creating smaller files that focus only
|
|
@@ -17,7 +19,12 @@ module Ephem
|
|
|
17
19
|
# Constants for time calculations
|
|
18
20
|
S_PER_DAY = Core::Constants::Time::SECONDS_PER_DAY
|
|
19
21
|
J2000_EPOCH = Core::Constants::Time::J2000_EPOCH
|
|
20
|
-
RECORD_SIZE =
|
|
22
|
+
RECORD_SIZE = IO::BinaryReader::RECORD_SIZE
|
|
23
|
+
SUPPORTED_DATA_TYPES = {spk: [2, 3], pck: [2]}.freeze
|
|
24
|
+
DATA_TYPE_INDEXES = {
|
|
25
|
+
spk: SPK::DATA_TYPE_IDENTIFIER,
|
|
26
|
+
pck: PCK::DATA_TYPE_IDENTIFIER
|
|
27
|
+
}.freeze
|
|
21
28
|
|
|
22
29
|
# @param kernel [Ephem::SPK, Ephem::PCK] The kernel to excerpt from
|
|
23
30
|
def initialize(kernel)
|
|
@@ -40,33 +47,57 @@ module Ephem
|
|
|
40
47
|
def extract(output_path:, start_jd:, end_jd:, target_ids: nil, debug: false)
|
|
41
48
|
start_seconds = seconds_since_j2000(start_jd)
|
|
42
49
|
end_seconds = seconds_since_j2000(end_jd)
|
|
43
|
-
|
|
44
|
-
copy_file_header(output_file)
|
|
45
|
-
initialize_summary_section(output_file)
|
|
46
|
-
writer = create_daf_writer(output_file, debug)
|
|
47
|
-
process_segments(writer, start_seconds, end_seconds, target_ids, debug)
|
|
48
|
-
output_file.close
|
|
50
|
+
write_excerpt(output_path, start_seconds, end_seconds, target_ids, debug)
|
|
49
51
|
|
|
50
52
|
reopen(output_path)
|
|
51
53
|
end
|
|
52
54
|
|
|
53
55
|
private
|
|
54
56
|
|
|
57
|
+
def write_excerpt(
|
|
58
|
+
output_path,
|
|
59
|
+
start_seconds,
|
|
60
|
+
end_seconds,
|
|
61
|
+
target_ids,
|
|
62
|
+
debug
|
|
63
|
+
)
|
|
64
|
+
temp_file = File.open(
|
|
65
|
+
"#{output_path}.#{SecureRandom.hex(8)}.tmp",
|
|
66
|
+
File::RDWR | File::CREAT | File::EXCL | File::BINARY,
|
|
67
|
+
0o666
|
|
68
|
+
)
|
|
69
|
+
copy_file_header(temp_file)
|
|
70
|
+
initialize_summary_section(temp_file)
|
|
71
|
+
writer = create_daf_writer(temp_file)
|
|
72
|
+
process_segments(writer, start_seconds, end_seconds, target_ids, debug)
|
|
73
|
+
temp_file.close
|
|
74
|
+
File.rename(temp_file.path, output_path)
|
|
75
|
+
temp_file = nil
|
|
76
|
+
ensure
|
|
77
|
+
discard(temp_file) if temp_file
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def discard(file)
|
|
81
|
+
file.close
|
|
82
|
+
File.unlink(file.path)
|
|
83
|
+
rescue SystemCallError
|
|
84
|
+
nil
|
|
85
|
+
end
|
|
86
|
+
|
|
55
87
|
def reopen(path)
|
|
56
88
|
(@daf.file_type == :pck) ? PCK.open(path) : SPK.open(path)
|
|
57
89
|
end
|
|
58
90
|
|
|
59
|
-
def seconds_since_j2000(
|
|
60
|
-
(
|
|
91
|
+
def seconds_since_j2000(julian_date)
|
|
92
|
+
(julian_date - J2000_EPOCH) * S_PER_DAY
|
|
61
93
|
end
|
|
62
94
|
|
|
63
95
|
def copy_file_header(output_file)
|
|
64
|
-
|
|
65
|
-
fward = @daf.record_data.forward_record
|
|
96
|
+
first_summary_record = @daf.record_data.forward_record
|
|
66
97
|
|
|
67
98
|
# Copy file record and comments
|
|
68
|
-
(1...
|
|
69
|
-
data = @binary_reader.read_record(
|
|
99
|
+
(1...first_summary_record).each do |record_number|
|
|
100
|
+
data = @binary_reader.read_record(record_number)
|
|
70
101
|
output_file.write(data)
|
|
71
102
|
end
|
|
72
103
|
end
|
|
@@ -78,11 +109,12 @@ module Ephem
|
|
|
78
109
|
output_file.write(name_data)
|
|
79
110
|
end
|
|
80
111
|
|
|
81
|
-
def create_daf_writer(output_file
|
|
82
|
-
writer = DAFWriter.new(output_file
|
|
83
|
-
|
|
84
|
-
writer.
|
|
85
|
-
writer.
|
|
112
|
+
def create_daf_writer(output_file)
|
|
113
|
+
writer = DAFWriter.new(output_file)
|
|
114
|
+
first_summary_record = @daf.record_data.forward_record
|
|
115
|
+
writer.forward_record = first_summary_record
|
|
116
|
+
writer.backward_record = first_summary_record
|
|
117
|
+
writer.free = (first_summary_record + 1) * (RECORD_SIZE / 8) + 1
|
|
86
118
|
writer.endianness = @daf.endianness
|
|
87
119
|
writer.setup_formats(
|
|
88
120
|
@daf.record_data.double_count,
|
|
@@ -154,22 +186,31 @@ module Ephem
|
|
|
154
186
|
puts "start=#{start_pos}, end=#{end_pos}"
|
|
155
187
|
end
|
|
156
188
|
|
|
189
|
+
ensure_supported_data_type(name, values)
|
|
190
|
+
|
|
157
191
|
# Read the metadata from the end of the array
|
|
158
|
-
|
|
159
|
-
|
|
192
|
+
initial_epoch, interval_length, record_length, record_count =
|
|
193
|
+
@daf.read_array(end_pos - 3, end_pos)
|
|
194
|
+
record_length = record_length.to_i
|
|
160
195
|
|
|
161
196
|
if debug
|
|
162
|
-
puts " Metadata: init=#{
|
|
197
|
+
puts " Metadata: init=#{initial_epoch}, " \
|
|
198
|
+
"intlen=#{interval_length}, rsize=#{record_length}, " \
|
|
199
|
+
"n=#{record_count}"
|
|
163
200
|
end
|
|
164
201
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
202
|
+
first_record = ((start_seconds - initial_epoch) / interval_length)
|
|
203
|
+
.clamp(0, record_count).to_i
|
|
204
|
+
end_record = ((end_seconds - initial_epoch) / interval_length + 1)
|
|
205
|
+
.clamp(0, record_count).to_i
|
|
168
206
|
|
|
169
|
-
|
|
207
|
+
if debug
|
|
208
|
+
puts " Date range: records #{first_record} to #{end_record} " \
|
|
209
|
+
"out of #{record_count}"
|
|
210
|
+
end
|
|
170
211
|
|
|
171
212
|
# Skip if no overlap with requested date range
|
|
172
|
-
if
|
|
213
|
+
if first_record == end_record
|
|
173
214
|
if debug
|
|
174
215
|
puts "Segment #{segment_index} (#{name}):"
|
|
175
216
|
puts "No overlap with requested date range"
|
|
@@ -178,16 +219,16 @@ module Ephem
|
|
|
178
219
|
return false
|
|
179
220
|
end
|
|
180
221
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
n = j - i
|
|
222
|
+
initial_epoch += first_record * interval_length
|
|
223
|
+
record_count = end_record - first_record
|
|
184
224
|
|
|
185
|
-
|
|
225
|
+
if debug
|
|
226
|
+
puts " New metadata: init=#{initial_epoch}, n=#{record_count}"
|
|
227
|
+
end
|
|
186
228
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
excerpt_end = start_pos + rsize * j + extra - 1
|
|
229
|
+
metadata_size = 4
|
|
230
|
+
excerpt_start = start_pos + record_length * first_record
|
|
231
|
+
excerpt_end = start_pos + record_length * end_record + metadata_size - 1
|
|
191
232
|
|
|
192
233
|
puts " Reading array from #{excerpt_start} to #{excerpt_end}" if debug
|
|
193
234
|
|
|
@@ -195,14 +236,13 @@ module Ephem
|
|
|
195
236
|
|
|
196
237
|
puts " Read #{excerpt.length} values" if debug
|
|
197
238
|
|
|
198
|
-
|
|
199
|
-
|
|
239
|
+
excerpt[-4..-1] =
|
|
240
|
+
[initial_epoch, interval_length, record_length, record_count]
|
|
200
241
|
|
|
201
|
-
new_values =
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
end
|
|
242
|
+
new_values = [
|
|
243
|
+
initial_epoch,
|
|
244
|
+
initial_epoch + record_count * interval_length
|
|
245
|
+
] + values[2...-2]
|
|
206
246
|
|
|
207
247
|
puts " New values: #{new_values.inspect}" if debug
|
|
208
248
|
|
|
@@ -216,48 +256,36 @@ module Ephem
|
|
|
216
256
|
|
|
217
257
|
if debug
|
|
218
258
|
puts "Segment #{segment_index} (#{name}):"
|
|
219
|
-
puts "Included in excerpt
|
|
259
|
+
puts "Included in excerpt " \
|
|
260
|
+
"(#{first_record} to #{end_record} of #{record_count})"
|
|
220
261
|
end
|
|
221
262
|
|
|
222
263
|
true
|
|
223
|
-
rescue => e
|
|
224
|
-
puts "Error processing segment #{segment_index} (#{name}): #{e.message}"
|
|
225
|
-
puts e.backtrace.join("\n") if debug
|
|
226
|
-
false
|
|
227
264
|
end
|
|
228
265
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
266
|
+
def ensure_supported_data_type(name, values)
|
|
267
|
+
file_type = (@daf.file_type == :pck) ? :pck : :spk
|
|
268
|
+
data_type = values[DATA_TYPE_INDEXES.fetch(file_type)].to_i
|
|
269
|
+
return if SUPPORTED_DATA_TYPES.fetch(file_type).include?(data_type)
|
|
270
|
+
|
|
271
|
+
raise UnsupportedError,
|
|
272
|
+
"Cannot excerpt #{file_type.upcase} segment #{name.strip.inspect}: " \
|
|
273
|
+
"data type #{data_type} is not supported"
|
|
232
274
|
end
|
|
233
275
|
|
|
234
276
|
# Helper class for writing DAF files
|
|
235
277
|
# This class handles the low-level details of DAF file format
|
|
236
278
|
class DAFWriter
|
|
237
|
-
attr_reader :
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
:endianness,
|
|
242
|
-
:double_format,
|
|
243
|
-
:int_format,
|
|
244
|
-
:nd,
|
|
245
|
-
:ni,
|
|
246
|
-
:summary_format,
|
|
247
|
-
:summary_control_format,
|
|
248
|
-
:summary_length,
|
|
249
|
-
:summary_step,
|
|
250
|
-
:summaries_per_record
|
|
251
|
-
|
|
252
|
-
def initialize(file, debug = false)
|
|
279
|
+
attr_reader :summary_format
|
|
280
|
+
attr_writer :forward_record, :backward_record, :free, :endianness
|
|
281
|
+
|
|
282
|
+
def initialize(file)
|
|
253
283
|
@file = file
|
|
254
|
-
@debug = debug
|
|
255
|
-
@mutex = Mutex.new
|
|
256
284
|
end
|
|
257
285
|
|
|
258
|
-
def setup_formats(
|
|
259
|
-
@
|
|
260
|
-
@
|
|
286
|
+
def setup_formats(double_count, integer_count)
|
|
287
|
+
@double_count = double_count
|
|
288
|
+
@integer_count = integer_count
|
|
261
289
|
|
|
262
290
|
# Double is always 8 bytes, int is always 4 bytes
|
|
263
291
|
double_size = 8
|
|
@@ -266,7 +294,7 @@ module Ephem
|
|
|
266
294
|
# Set formats based on endianness
|
|
267
295
|
if @endianness == :little
|
|
268
296
|
@double_format = "E" # Little-endian double
|
|
269
|
-
@int_format = "l"
|
|
297
|
+
@int_format = "l<" # Little-endian signed long (32-bit)
|
|
270
298
|
else
|
|
271
299
|
@double_format = "G" # Big-endian double
|
|
272
300
|
@int_format = "l>" # Big-endian signed long (32-bit)
|
|
@@ -275,10 +303,12 @@ module Ephem
|
|
|
275
303
|
# Create formats for summary structures
|
|
276
304
|
@summary_control_format =
|
|
277
305
|
"#{@double_format}#{@double_format}#{@double_format}"
|
|
278
|
-
@summary_format = @double_format.to_s * @
|
|
306
|
+
@summary_format = @double_format.to_s * @double_count +
|
|
307
|
+
@int_format.to_s * @integer_count
|
|
279
308
|
|
|
280
309
|
# Calculate segment summary sizes
|
|
281
|
-
@summary_length =
|
|
310
|
+
@summary_length =
|
|
311
|
+
double_size * @double_count + int_size * @integer_count
|
|
282
312
|
|
|
283
313
|
# Pad to 8 bytes
|
|
284
314
|
@summary_step = @summary_length + (-@summary_length % 8)
|
|
@@ -291,52 +321,43 @@ module Ephem
|
|
|
291
321
|
data = @file.read(RECORD_SIZE)
|
|
292
322
|
|
|
293
323
|
# Update pointers directly in the data buffer
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
324
|
+
forward_position = 76
|
|
325
|
+
backward_position = 80
|
|
326
|
+
free_position = 84
|
|
297
327
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
else
|
|
303
|
-
data[fward_pos, 4] = [@fward].pack("N")
|
|
304
|
-
data[bward_pos, 4] = [@bward].pack("N")
|
|
305
|
-
data[free_pos, 4] = [@free].pack("N")
|
|
306
|
-
end
|
|
328
|
+
int_format = (@endianness == :little) ? "l<" : "l>"
|
|
329
|
+
data[forward_position, 4] = [@forward_record].pack(int_format)
|
|
330
|
+
data[backward_position, 4] = [@backward_record].pack(int_format)
|
|
331
|
+
data[free_position, 4] = [@free].pack(int_format)
|
|
307
332
|
|
|
308
333
|
# Write the updated record back to the file
|
|
309
334
|
@file.seek(0)
|
|
310
335
|
@file.write(data)
|
|
311
336
|
end
|
|
312
337
|
|
|
313
|
-
def read_record(
|
|
314
|
-
@
|
|
315
|
-
|
|
316
|
-
@file.read(RECORD_SIZE)
|
|
317
|
-
end
|
|
338
|
+
def read_record(record_number)
|
|
339
|
+
@file.seek((record_number - 1) * RECORD_SIZE)
|
|
340
|
+
@file.read(RECORD_SIZE)
|
|
318
341
|
end
|
|
319
342
|
|
|
320
|
-
def write_record(
|
|
321
|
-
@
|
|
322
|
-
|
|
323
|
-
@file.write(data)
|
|
324
|
-
end
|
|
343
|
+
def write_record(record_number, data)
|
|
344
|
+
@file.seek((record_number - 1) * RECORD_SIZE)
|
|
345
|
+
@file.write(data)
|
|
325
346
|
end
|
|
326
347
|
|
|
327
348
|
def add_array(name, values, array)
|
|
328
|
-
record_number = @
|
|
349
|
+
record_number = @backward_record
|
|
329
350
|
data = read_record(record_number).dup
|
|
330
351
|
|
|
331
352
|
control_data = data[0, 24].unpack(@summary_control_format)
|
|
332
353
|
next_record = control_data[0].to_i
|
|
333
354
|
previous_record = control_data[1].to_i
|
|
334
|
-
|
|
355
|
+
summary_count = control_data[2].to_i
|
|
335
356
|
|
|
336
|
-
if
|
|
357
|
+
if summary_count < @summaries_per_record
|
|
337
358
|
# Add to the existing record
|
|
338
359
|
summary_record = record_number
|
|
339
|
-
data[0, 24] = [next_record, previous_record,
|
|
360
|
+
data[0, 24] = [next_record, previous_record, summary_count + 1]
|
|
340
361
|
.pack(@summary_control_format)
|
|
341
362
|
write_record(summary_record, data)
|
|
342
363
|
else
|
|
@@ -345,11 +366,11 @@ module Ephem
|
|
|
345
366
|
name_record = summary_record + 1
|
|
346
367
|
free_record = summary_record + 2
|
|
347
368
|
|
|
348
|
-
data[0, 24] = [summary_record, previous_record,
|
|
369
|
+
data[0, 24] = [summary_record, previous_record, summary_count]
|
|
349
370
|
.pack(@summary_control_format)
|
|
350
371
|
write_record(record_number, data)
|
|
351
372
|
|
|
352
|
-
|
|
373
|
+
summary_count = 0
|
|
353
374
|
summaries = [0, record_number, 1]
|
|
354
375
|
.pack(@summary_control_format)
|
|
355
376
|
.ljust(RECORD_SIZE, "\0")
|
|
@@ -357,7 +378,7 @@ module Ephem
|
|
|
357
378
|
write_record(summary_record, summaries)
|
|
358
379
|
write_record(name_record, names)
|
|
359
380
|
|
|
360
|
-
@
|
|
381
|
+
@backward_record = summary_record
|
|
361
382
|
@free = (free_record - 1) * RECORD_SIZE / 8 + 1
|
|
362
383
|
end
|
|
363
384
|
|
|
@@ -372,11 +393,11 @@ module Ephem
|
|
|
372
393
|
@free = end_word + 1
|
|
373
394
|
write_file_record
|
|
374
395
|
|
|
375
|
-
|
|
376
|
-
|
|
396
|
+
new_values =
|
|
397
|
+
values[0, @double_count + @integer_count - 2] + [start_word, end_word]
|
|
377
398
|
|
|
378
399
|
base = RECORD_SIZE * (summary_record - 1)
|
|
379
|
-
offset =
|
|
400
|
+
offset = summary_count * @summary_step
|
|
380
401
|
@file.seek(base + 24 + offset) # 24 is summary_control_struct size
|
|
381
402
|
@file.write(new_values.pack(@summary_format))
|
|
382
403
|
@file.seek(base + RECORD_SIZE + offset)
|
|
@@ -6,6 +6,7 @@ module Ephem
|
|
|
6
6
|
RECORD_SIZE = 1024
|
|
7
7
|
ENDIANNESS_DOUBLE_FORMATS = {little: "E", big: "G"}.freeze
|
|
8
8
|
ENDIANNESS_UINT32_FORMATS = {little: "V", big: "N"}.freeze
|
|
9
|
+
ENDIANNESS_INT32_FORMATS = {little: "l<", big: "l>"}.freeze
|
|
9
10
|
|
|
10
11
|
def initialize(file_object)
|
|
11
12
|
validate_file_object(file_object)
|
|
@@ -22,8 +23,9 @@ module Ephem
|
|
|
22
23
|
validate_record_number(record_number)
|
|
23
24
|
|
|
24
25
|
@lock.synchronize do
|
|
26
|
+
ensure_open
|
|
25
27
|
seek_to_record(record_number)
|
|
26
|
-
|
|
28
|
+
read_full_record(record_number)
|
|
27
29
|
end
|
|
28
30
|
end
|
|
29
31
|
|
|
@@ -35,13 +37,14 @@ module Ephem
|
|
|
35
37
|
# enter this critical section at a time. This ensures atomic reads of
|
|
36
38
|
# the array data, preventing data corruption
|
|
37
39
|
@lock.synchronize do
|
|
40
|
+
ensure_open
|
|
38
41
|
seek_to_word(start_word)
|
|
39
42
|
read_array_data(length, endianness)
|
|
40
43
|
end
|
|
41
44
|
end
|
|
42
45
|
|
|
43
46
|
def close
|
|
44
|
-
@file.close
|
|
47
|
+
@lock.synchronize { @file.close }
|
|
45
48
|
end
|
|
46
49
|
|
|
47
50
|
private
|
|
@@ -72,6 +75,12 @@ module Ephem
|
|
|
72
75
|
end
|
|
73
76
|
end
|
|
74
77
|
|
|
78
|
+
def ensure_open
|
|
79
|
+
return unless @file.respond_to?(:closed?) && @file.closed?
|
|
80
|
+
|
|
81
|
+
raise ClosedKernelError, "Cannot read from a closed kernel"
|
|
82
|
+
end
|
|
83
|
+
|
|
75
84
|
def seek_to_record(record_number)
|
|
76
85
|
@file.seek((record_number - 1) * RECORD_SIZE, ::IO::SEEK_SET)
|
|
77
86
|
end
|
|
@@ -80,18 +89,30 @@ module Ephem
|
|
|
80
89
|
@file.seek(8 * (word - 1), ::IO::SEEK_SET)
|
|
81
90
|
end
|
|
82
91
|
|
|
83
|
-
def
|
|
92
|
+
def read_full_record(record_number)
|
|
84
93
|
data = @file.read(RECORD_SIZE)
|
|
85
|
-
|
|
94
|
+
read_bytes = data ? data.bytesize : 0
|
|
95
|
+
if read_bytes < RECORD_SIZE
|
|
96
|
+
raise FileFormatError,
|
|
97
|
+
"Truncated file: expected #{RECORD_SIZE} bytes of record " \
|
|
98
|
+
"#{record_number}, got #{read_bytes}"
|
|
99
|
+
end
|
|
86
100
|
|
|
87
|
-
data
|
|
101
|
+
data
|
|
88
102
|
end
|
|
89
103
|
|
|
90
104
|
def read_array_data(length, endianness)
|
|
91
|
-
|
|
92
|
-
|
|
105
|
+
format = "#{endianness_format(endianness)}#{length}"
|
|
106
|
+
expected_bytes = 8 * length
|
|
107
|
+
data = @file.read(expected_bytes)
|
|
108
|
+
read_bytes = data ? data.bytesize : 0
|
|
109
|
+
if read_bytes < expected_bytes
|
|
110
|
+
raise FileFormatError,
|
|
111
|
+
"Truncated file: expected #{expected_bytes} bytes of array " \
|
|
112
|
+
"data, got #{read_bytes}"
|
|
113
|
+
end
|
|
93
114
|
|
|
94
|
-
data.unpack(
|
|
115
|
+
data.unpack(format)
|
|
95
116
|
end
|
|
96
117
|
|
|
97
118
|
def endianness_format(endianness)
|
data/lib/ephem/io/daf.rb
CHANGED
|
@@ -33,7 +33,6 @@ module Ephem
|
|
|
33
33
|
endianness: @endianness
|
|
34
34
|
)
|
|
35
35
|
end
|
|
36
|
-
alias_method :map_array, :read_array
|
|
37
36
|
|
|
38
37
|
def close
|
|
39
38
|
@binary_reader&.close
|
|
@@ -55,9 +54,7 @@ module Ephem
|
|
|
55
54
|
|
|
56
55
|
def setup_file_format
|
|
57
56
|
file_record = @binary_reader.read_record(1)
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
@endianness = endianness_info[:endianness]
|
|
57
|
+
@endianness = EndiannessManager.new(file_record).detect_endianness
|
|
61
58
|
@record_parser = RecordParser.new(endianness: @endianness)
|
|
62
59
|
@record_data = @record_parser.parse_file_record(file_record)
|
|
63
60
|
end
|
|
@@ -34,10 +34,8 @@ module Ephem
|
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
def detect_from_naif_daf
|
|
37
|
-
FORMATS.
|
|
38
|
-
if
|
|
39
|
-
return {format: format, endianness: endian}
|
|
40
|
-
end
|
|
37
|
+
FORMATS.each_value do |endianness|
|
|
38
|
+
return endianness if valid_endianness?(endianness)
|
|
41
39
|
end
|
|
42
40
|
raise EndiannessError, "Unable to determine file endianness"
|
|
43
41
|
end
|
|
@@ -45,23 +43,12 @@ module Ephem
|
|
|
45
43
|
def detect_from_daf_prefix
|
|
46
44
|
validate_ftpstr
|
|
47
45
|
format = extract_format
|
|
48
|
-
|
|
46
|
+
FORMATS.fetch(format) do
|
|
49
47
|
raise FileFormatError, "Unknown format #{format.inspect}"
|
|
50
48
|
end
|
|
51
|
-
|
|
52
|
-
{format: format, endianness: endianness}
|
|
53
49
|
end
|
|
54
50
|
|
|
55
51
|
def validate_ftpstr
|
|
56
|
-
# The Fixed-length Transfer Protocol String (FTPSTR) is expected to be
|
|
57
|
-
# found in bytes 501-1000 of the file record. If it's not present, the
|
|
58
|
-
# file is considered damaged.
|
|
59
|
-
#
|
|
60
|
-
# The file format and endianness are determined from bytes 89-96 of the
|
|
61
|
-
# file record. If the format is unknown, an exception is raised.
|
|
62
|
-
#
|
|
63
|
-
# Finally, the file record is unpacked using the determined format and
|
|
64
|
-
# endianness.
|
|
65
52
|
ftpstr_section = @file_record[500, 500].gsub(/\A\0+|\0+\z/, "")
|
|
66
53
|
unless ftpstr_section == FTPSTR
|
|
67
54
|
raise FileFormatError, "This SPK file has been damaged"
|
|
@@ -72,8 +59,8 @@ module Ephem
|
|
|
72
59
|
@file_record[88, 8].strip
|
|
73
60
|
end
|
|
74
61
|
|
|
75
|
-
def
|
|
76
|
-
temp_parser = RecordParser.new(endianness:
|
|
62
|
+
def valid_endianness?(endianness)
|
|
63
|
+
temp_parser = RecordParser.new(endianness: endianness)
|
|
77
64
|
record_data = temp_parser.parse_file_record(@file_record)
|
|
78
65
|
record_data.double_count == 2
|
|
79
66
|
rescue
|
data/lib/ephem/io/record_data.rb
CHANGED
|
@@ -4,34 +4,16 @@ module Ephem
|
|
|
4
4
|
module IO
|
|
5
5
|
class RecordData
|
|
6
6
|
attr_reader :locator_identifier, :double_count, :integer_count,
|
|
7
|
-
:internal_filename, :forward_record
|
|
8
|
-
:format, :reserved_area_1, :ftpstr, :reserved_area_2
|
|
7
|
+
:internal_filename, :forward_record
|
|
9
8
|
|
|
10
9
|
def initialize(*data)
|
|
11
10
|
@locator_identifier, @double_count, @integer_count, @internal_filename,
|
|
12
|
-
@forward_record
|
|
13
|
-
@reserved_area_1, @ftpstr, @reserved_area_2 = data
|
|
11
|
+
@forward_record = data
|
|
14
12
|
|
|
15
|
-
convert_numeric_fields
|
|
16
|
-
strip_text_fields
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
private
|
|
20
|
-
|
|
21
|
-
def convert_numeric_fields
|
|
22
13
|
@double_count = @double_count.to_i
|
|
23
14
|
@integer_count = @integer_count.to_i
|
|
24
15
|
@forward_record = @forward_record.to_i
|
|
25
|
-
@backward_record = @backward_record.to_i
|
|
26
|
-
@free_record = @free_record.to_i
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
def strip_text_fields
|
|
30
16
|
@internal_filename = @internal_filename.rstrip
|
|
31
|
-
@format = @format.rstrip
|
|
32
|
-
@reserved_area_1 = @reserved_area_1.rstrip
|
|
33
|
-
@ftpstr = @ftpstr.rstrip
|
|
34
|
-
@reserved_area_2 = @reserved_area_2.rstrip
|
|
35
17
|
end
|
|
36
18
|
end
|
|
37
19
|
end
|