Method: Ephem::Excerpt::DAFWriter#add_array

Defined in:
lib/ephem/excerpt.rb

#add_array(name, values, array) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/ephem/excerpt.rb', line 322

def add_array(name, values, array)
  record_number = @bward
  data = read_record(record_number).dup

  control_data = data[0, 24].unpack(@summary_control_format)
  next_record = control_data[0].to_i
  previous_record = control_data[1].to_i
  n_summaries = control_data[2].to_i

  if n_summaries < @summaries_per_record
    # Add to the existing record
    summary_record = record_number
    data[0, 24] = [next_record, previous_record, n_summaries + 1]
      .pack(@summary_control_format)
    write_record(summary_record, data)
  else
    # Create a new record
    summary_record = ((@free - 1) * 8 + 1023) / 1024 + 1
    name_record = summary_record + 1
    free_record = summary_record + 2

    data[0, 24] = [summary_record, previous_record, n_summaries]
      .pack(@summary_control_format)
    write_record(record_number, data)

    n_summaries = 0
    summaries = [0, record_number, 1]
      .pack(@summary_control_format)
      .ljust(RECORD_SIZE, "\0")
    names = " ".ljust(RECORD_SIZE, "\0")
    write_record(summary_record, summaries)
    write_record(name_record, names)

    @bward = summary_record
    @free = (free_record - 1) * RECORD_SIZE / 8 + 1
  end

  # Convert array to binary data
  array_data = array.pack("#{@double_format}*")

  start_word = @free
  @file.seek((start_word - 1) * 8)
  @file.write(array_data)
  end_word = @file.tell / 8

  @free = end_word + 1
  write_file_record

  # Using values up to nd+ni-2, then adding start_word and end_word
  new_values = values[0, @nd + @ni - 2] + [start_word, end_word]

  base = RECORD_SIZE * (summary_record - 1)
  offset = n_summaries * @summary_step
  @file.seek(base + 24 + offset)  # 24 is summary_control_struct size
  @file.write(new_values.pack(@summary_format))
  @file.seek(base + RECORD_SIZE + offset)
  @file.write(name[0, @summary_length].ljust(@summary_step, " "))
end