Class: Cosmos::TlmExtractorConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/cosmos/tools/tlm_extractor/tlm_extractor_config.rb

Overview

Class holds a telemetry extractor configuration and controls writing to one output file

Constant Summary collapse

VALUE_TYPES =
[:RAW, :CONVERTED, :FORMATTED, :WITH_UNITS]
DEFAULT_UNIQUE_IGNORED =
['RECEIVED_TIMEFORMATTED', 'RECEIVED_TIMESECONDS']
ITEM =
'ITEM'.freeze
TEXT =
'TEXT'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file = nil) ⇒ TlmExtractorConfig

Returns a new instance of TlmExtractorConfig.



35
36
37
38
39
40
41
42
# File 'lib/cosmos/tools/tlm_extractor/tlm_extractor_config.rb', line 35

def initialize(config_file = nil)
  @output_filename = nil
  @input_filenames = []
  @output_file = nil
  reset_settings()
  clear_items()
  restore(config_file) if config_file
end

Instance Attribute Details

#delimiterObject

Returns the value of attribute delimiter.



27
28
29
# File 'lib/cosmos/tools/tlm_extractor/tlm_extractor_config.rb', line 27

def delimiter
  @delimiter
end

#downsample_secondsObject

Returns the value of attribute downsample_seconds.



28
29
30
# File 'lib/cosmos/tools/tlm_extractor/tlm_extractor_config.rb', line 28

def downsample_seconds
  @downsample_seconds
end

#fill_downObject

Returns the value of attribute fill_down.



24
25
26
# File 'lib/cosmos/tools/tlm_extractor/tlm_extractor_config.rb', line 24

def fill_down
  @fill_down
end

#input_filenamesObject

Returns the value of attribute input_filenames.



33
34
35
# File 'lib/cosmos/tools/tlm_extractor/tlm_extractor_config.rb', line 33

def input_filenames
  @input_filenames
end

#itemsObject (readonly)

Returns the value of attribute items.



30
31
32
# File 'lib/cosmos/tools/tlm_extractor/tlm_extractor_config.rb', line 30

def items
  @items
end

#matlab_headerObject

Returns the value of attribute matlab_header.



23
24
25
# File 'lib/cosmos/tools/tlm_extractor/tlm_extractor_config.rb', line 23

def matlab_header
  @matlab_header
end

#output_filenameObject

Returns the value of attribute output_filename.



32
33
34
# File 'lib/cosmos/tools/tlm_extractor/tlm_extractor_config.rb', line 32

def output_filename
  @output_filename
end

Returns the value of attribute print_filenames_to_output.



29
30
31
# File 'lib/cosmos/tools/tlm_extractor/tlm_extractor_config.rb', line 29

def print_filenames_to_output
  @print_filenames_to_output
end

#share_columnsObject

Returns the value of attribute share_columns.



25
26
27
# File 'lib/cosmos/tools/tlm_extractor/tlm_extractor_config.rb', line 25

def share_columns
  @share_columns
end

#unique_onlyObject

Returns the value of attribute unique_only.



26
27
28
# File 'lib/cosmos/tools/tlm_extractor/tlm_extractor_config.rb', line 26

def unique_only
  @unique_only
end

Instance Method Details

#add_item(target_name, packet_name, item_name, value_type = :CONVERTED) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cosmos/tools/tlm_extractor/tlm_extractor_config.rb', line 64

def add_item(target_name, packet_name, item_name, value_type = :CONVERTED)
  target_name = target_name.upcase
  packet_name = packet_name.upcase
  item_name = item_name.upcase

  raise "Unknown Value Type: #{value_type}" unless VALUE_TYPES.include?(value_type)

  hash_index = item_name + ' ' + value_type.to_s

  if @share_columns and @columns_hash[hash_index]
    column_index = @columns_hash[hash_index]
  else
    @columns << [item_name, value_type, nil]
    column_index = @columns.length - 1
    @columns_hash[hash_index] = column_index
  end
  @packet_to_column_mapping[target_name] ||= {}
  @packet_to_column_mapping[target_name][packet_name] ||= []
  @packet_to_column_mapping[target_name][packet_name] << column_index
  @items << [ITEM, target_name, packet_name, item_name, value_type]
end

#add_text(column_name, text) ⇒ Object



86
87
88
89
90
# File 'lib/cosmos/tools/tlm_extractor/tlm_extractor_config.rb', line 86

def add_text(column_name, text)
  @columns << [column_name, nil, nil]
  @items << [TEXT, column_name, text, nil, nil]
  @text_items << [@columns.length - 1, text]
end

#clear_itemsObject



55
56
57
58
59
60
61
62
# File 'lib/cosmos/tools/tlm_extractor/tlm_extractor_config.rb', line 55

def clear_items
  @packet_to_column_mapping = {}
  @columns = []
  @columns_hash = {}
  @previous_row = nil
  @items = []
  @text_items = []
end

#close_output_fileObject



279
280
281
282
283
284
285
286
287
# File 'lib/cosmos/tools/tlm_extractor/tlm_extractor_config.rb', line 279

def close_output_file
  begin
    @output_file.close if @output_file and !@output_file.closed?
  rescue
    # Oh well
  ensure
    @output_file = nil
  end
end

#column_namesObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/cosmos/tools/tlm_extractor/tlm_extractor_config.rb', line 92

def column_names
  row = Array.new(@columns.length + 2)
  row[0] = 'TARGET'
  row[1] = 'PACKET'
  index = 0
  @columns.each do |column_name, column_value_type, item_data_type|
    case column_value_type
    when :CONVERTED, nil
      row[index + 2] = column_name
    else
      row[index + 2] = (column_name + '(' + column_value_type.to_s + ')')
    end
    index += 1
  end

  row
end

#open_output_fileObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/cosmos/tools/tlm_extractor/tlm_extractor_config.rb', line 239

def open_output_file
  # Reset per output state
  @row_index = 1
  @current_values = Array.new(@columns.length)
  @packet_timestamp_mapping = {}
  @previous_row = nil

  # Open the output file
  @output_file = File.open(@output_filename, 'w')

  if @print_filenames_to_output
    # Print input filenames to output file
    @input_filenames.each do |input_filename|
      if @matlab_header
        @output_file.puts "%#{input_filename}"
      else
        @output_file.puts input_filename
      end
      @row_index += 1
    end

    # Print blank row to output file
    if @matlab_header
      @output_file.puts "%"
    else
      @output_file.puts ""
    end
    @row_index += 1
  end

  # Print column headings to output file
  @output_file.print "%" if @matlab_header
  column_names().each do |column_name|
    @output_file.print column_name
    @output_file.print @delimiter
  end
  @output_file.puts ""
  @row_index += 1
end

#process_packet(packet) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
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
# File 'lib/cosmos/tools/tlm_extractor/tlm_extractor_config.rb', line 289

def process_packet(packet)
  changed = false
  target_mapping = @packet_to_column_mapping[packet.target_name]
  if target_mapping
    packet_mapping = target_mapping[packet.packet_name]
    if packet_mapping
      if @downsample_seconds != 0.0
        target_timestamp_mapping = @packet_timestamp_mapping[packet.target_name]
        unless target_timestamp_mapping
          @packet_timestamp_mapping[packet.target_name] = {}
          target_timestamp_mapping = @packet_timestamp_mapping[packet.target_name]
        end
        previous_timestamp = target_timestamp_mapping[packet.packet_name]
        return if previous_timestamp and (packet.received_time - previous_timestamp) < @downsample_seconds
        target_timestamp_mapping[packet.packet_name] = packet.received_time
      end

      # Create a new row
      if @fill_down and @previous_row
        row = @previous_row
      else
        row = Array.new(@columns.length)
      end

      # Add each packet item to the row
      packet_mapping.each do |column_index|
        column_name, column_value_type, item_data_type = @columns[column_index]

        # Lookup item data type on first use
        unless item_data_type
          _, item = System.telemetry.packet_and_item(packet.target_name, packet.packet_name, column_name)
          item_data_type = item.data_type
          @columns[column_index] = [column_name, column_value_type, item_data_type]
        end

        if item_data_type == :BLOCK
          case column_value_type
          when :RAW
            value = packet.read(column_name, :RAW).to_s.simple_formatted
            row[column_index] = value
            changed = true if @unique_only and @current_values[column_index] != value and !@unique_ignored.include?(column_name)
            @current_values[column_index] = value
          when :CONVERTED, :FORMATTED, :WITH_UNITS
            value = packet.read(column_name, :CONVERTED).to_s.simple_formatted
            row[column_index] = value
            changed = true if @unique_only and @current_values[column_index] != value and !@unique_ignored.include?(column_name)
            @current_values[column_index] = value
          end
        else
          value = packet.read(column_name, column_value_type)
          row[column_index] = value
          changed = true if @unique_only and @current_values[column_index] != value and !@unique_ignored.include?(column_name)
          @current_values[column_index] = value
        end
      end

      # Add text items to the row
      @text_items.each do |column_index, text|
        row[column_index] = text.gsub('%', @row_index.to_s)
      end

      if !@unique_only or changed
        # Output the row
        @output_file.print packet.target_name
        @output_file.print @delimiter
        @output_file.print packet.packet_name
        row.each_with_index do |value, index|
          @output_file.print @delimiter
          @output_file.print value if value
        end
        @output_file.puts ""
        @row_index += 1
      end

      # Save previous row for handling fill_down
      @previous_row = row
    end # if packet_mapping
  end # if target_mapping
end

#reset_settingsObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/cosmos/tools/tlm_extractor/tlm_extractor_config.rb', line 44

def reset_settings
  @matlab_header = false
  @fill_down = false
  @share_columns = false
  @unique_only = false
  @delimiter = "\t"
  @unique_ignored = DEFAULT_UNIQUE_IGNORED.clone
  @downsample_seconds = 0.0
  @print_filenames_to_output = true
end

#restore(filename) ⇒ Object



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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/cosmos/tools/tlm_extractor/tlm_extractor_config.rb', line 110

def restore(filename)
  Cosmos.set_working_dir do
    reset_settings()
    clear_items()

    parser = ConfigParser.new
    parser.parse_file(filename) do |keyword, params|
      case keyword
      when 'DELIMITER'
        # Expect 1 parameter
        parser.verify_num_parameters(1, 1, "DELIMITER <Delimiter>")
        delimiter = params[0]
        if delimiter == "tab"
          delimiter = "\t"
        end
        @delimiter = delimiter

      when 'FILL_DOWN'
        # Expect 0 parameters
        parser.verify_num_parameters(0, 0, "FILL_DOWN")
        @fill_down = true

      when 'SHARE_COLUMNS'
        # Expect 0 parameters
        parser.verify_num_parameters(0, 0, "SHARE_COLUMNS")
        @share_columns = true

      when 'UNIQUE_ONLY'
        # Expect 0 parameters
        parser.verify_num_parameters(0, 0, "UNIQUE_ONLY")
        @unique_only = true

      when 'UNIQUE_IGNORE'
        # Expect 1 parameter
        parser.verify_num_parameters(1, 1, "UNIQUE_IGNORE")
        @unique_ignored << params[0].upcase

      when 'DOWNSAMPLE_SECONDS'
        # Expect 1 parameter
        parser.verify_num_parameters(1, 1, "DOWNSAMPLE_SECONDS <Seconds>")
        @downsample_seconds = Float(params[0])

      when 'ITEM'
        # Expect 3 or 4 parameters
        parser.verify_num_parameters(3, 4, "ITEM <Target Name> <Packet Name> <Item Name> <Data Type (optional)>")
        target_name = params[0].upcase
        packet_name = params[1].upcase
        item_name = params[2].upcase
        if params.length == 3
          value_type = :CONVERTED
        else
          value_type = params[3].upcase
          case value_type
          when 'CONVERTED', 'RAW', 'FORMATTED', 'WITH_UNITS'
            value_type = value_type.intern
          else
            raise "Unknown Value Type: #{value_type}"
          end
        end
        add_item(target_name, packet_name, item_name, value_type)

      when 'TEXT'
        # Expect 2 parameters
        parser.verify_num_parameters(2, 2, "TEXT <Column Name> <Text>")
        add_text(params[0], params[1])

      when 'MATLAB_HEADER'
        # Expect 0 parameters
        parser.verify_num_parameters(0, 0, "MATLAB_HEADER")
        @matlab_header = true

      when 'DONT_OUTPUT_FILENAMES'
        # Expect 0 parameters
        parser.verify_num_parameters(0, 0, "DONT_OUTPUT_FILENAMES")
        @print_filenames_to_output = false

      else
        raise "Unknown keyword: #{keyword}"
      end
    end
  end
end

#save(filename) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/cosmos/tools/tlm_extractor/tlm_extractor_config.rb', line 193

def save(filename)
  Cosmos.set_working_dir do
    File.open(filename, "w") do |file|
      if @fill_down
        file.puts 'FILL_DOWN'
      end
      if @matlab_header
        file.puts 'MATLAB_HEADER'
      end
      if !@print_filenames_to_output
        file.puts 'DONT_OUTPUT_FILENAMES'
      end
      if @share_columns
        file.puts 'SHARE_COLUMNS'
      end
      if @unique_only
        file.puts 'UNIQUE_ONLY'
      end
      if @downsample_seconds != 0.0
        file.puts "DOWNSAMPLE_SECONDS #{@downsample_seconds}"
      end
      if @delimiter != "\t"
        file.puts "DELIMITER \"#{@delimiter}\""
      end
      if @unique_ignored != DEFAULT_UNIQUE_IGNORED
        @unique_ignored.each do |name|
          if !DEFAULT_UNIQUE_IGNORED.include?(name)
            file.puts "UNIQUE_IGNORE #{name}"
          end
        end
      end
      @items.each do |item_type, target_name_or_column_name, packet_name_or_text, item_name, value_type|
        if item_type == ITEM
          if value_type != :CONVERTED
            file.puts "#{item_type} #{target_name_or_column_name} #{packet_name_or_text} #{item_name} #{value_type}"
          else
            file.puts "#{item_type} #{target_name_or_column_name} #{packet_name_or_text} #{item_name}"
          end
        else
          file.puts "#{item_type} \"#{target_name_or_column_name}\" \"#{packet_name_or_text}\""
        end
      end
    end
  end # Cosmos.set_working_dir
end