Class: Ephem::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/ephem/cli.rb

Class Method Summary collapse

Class Method Details

.gregorian_to_julian(date_str) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ephem/cli.rb', line 8

def self.gregorian_to_julian(date_str)
  date = Date.parse(date_str)

  a = (14 - date.month) / 12
  y = date.year + 4800 - a
  m = date.month + 12 * a - 3

  date.day +
    ((153 * m + 2) / 5) +
    365 * y +
    (y / 4) -
    (y / 100) +
    (y / 400) -
    32045
end

.handle_excerpt(args) ⇒ Object

Handle the excerpt command



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/ephem/cli.rb', line 70

def self.handle_excerpt(args)
  # Parse options
  options = {target_ids: nil, debug: false}

  option_parser = OptionParser.new do |opts|
    opts.banner = "Usage: ruby-ephem excerpt [options] START_DATE END_DATE INPUT_FILE OUTPUT_FILE"

    opts.on("--targets TARGET_IDS", "Comma-separated list of target IDs to include") do |targets|
      options[:target_ids] = targets.split(",").map(&:strip).map(&:to_i)
    end

    opts.on("--debug", "Enable debug output") do
      options[:debug] = true
    end
  end

  begin
    option_parser.parse!(args)
  rescue OptionParser::InvalidOption => e
    puts e.message
    puts option_parser
    return
  end

  if args.size < 4
    puts "Not enough arguments."
    puts option_parser
    return
  end

  start_date_str = args[0]
  end_date_str = args[1]
  input_file = args[2]
  output_file = args[3]

  unless File.exist?(input_file)
    puts "Error: Input file '#{input_file}' does not exist."
    return
  end

  begin
    start_jd = gregorian_to_julian(start_date_str)
    end_jd = gregorian_to_julian(end_date_str)
  rescue Date::Error => e
    puts "Error parsing dates: #{e.message}"
    puts "Dates should be in YYYY-MM-DD format."
    return
  end

  begin
    puts "Creating excerpt from #{input_file} to #{output_file}..."
    puts "Date range: #{start_date_str} to #{end_date_str} (JD #{start_jd} to #{end_jd})"
    if options[:target_ids]
      puts "Including targets: #{options[:target_ids].join(", ")}"
    else
      puts "Including all targets"
    end

    spk = Ephem::SPK.open(input_file)

    excerpt_spk = spk.excerpt(
      output_path: output_file,
      start_jd: start_jd,
      end_jd: end_jd,
      target_ids: options[:target_ids],
      debug: options[:debug]
    )

    puts "Excerpt created successfully!"
    puts "Original segments: #{spk.segments.size}"
    puts "Excerpt segments: #{excerpt_spk.segments.size}"

    original_size = File.size(input_file)
    excerpt_size = File.size(output_file)
    reduction_percentage =
      ((original_size - excerpt_size) / original_size.to_f * 100).round(2)

    puts "File size reduced by #{reduction_percentage}%"
    puts "Original: #{original_size} bytes"
    puts "Excerpt: #{excerpt_size} bytes"

    spk.close
    excerpt_spk.close
  rescue => e
    puts "Error creating excerpt: #{e.message}"
    puts e.backtrace if options[:debug]
  end
end

.show_helpObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ephem/cli.rb', line 45

def self.show_help
  puts <<~HELP
    Ruby Ephem - A tool for working with JPL Ephemerides

    Commands:
      excerpt - Create an excerpt of an SPK file
      help    - Show this help message

    Excerpt command:
      ruby-ephem excerpt [options] START_DATE END_DATE INPUT_FILE OUTPUT_FILE

    Options:
      --targets TARGET_IDS  - Comma-separated list of target IDs to include
                              (default: all targets)

    Example:
      ruby-ephem excerpt --targets 3,10,399 2000-01-01 2030-01-01 de440s.bsp excerpt.bsp

    This will create an excerpt of de440s.bsp containing only the specified
    targets (Earth-Moon barycenter, Sun, Earth) for the period from
    2000-01-01 to 2030-01-01.
  HELP
end

.start(args) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ephem/cli.rb', line 24

def self.start(args)
  if args.empty?
    puts "Usage: ruby-ephem excerpt [options] START_DATE END_DATE INPUT_FILE OUTPUT_FILE"
    puts "For help: ruby-ephem help"
    return
  end

  if args[0] == "help"
    show_help
    return
  end

  if args[0] == "excerpt"
    handle_excerpt(args[1..])
    return
  end

  puts "Unknown command: #{args[0]}"
  puts "Available commands: excerpt, help"
end