Class: Cosmos::TabbedPlotsConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb,
ext/cosmos/ext/tabbed_plots_config/tabbed_plots_config.c

Overview

Provides capabilities to read an ascii file that lists the details for a set of plots drawn in tabs.

Constant Summary collapse

DEFAULT_SECONDS_PLOTTED =

Default Values

100.0
DEFAULT_POINTS_SAVED =
1000000
DEFAULT_POINTS_PLOTTED =
1000
DEFAULT_REFRESH_RATE_HZ =
10.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, plot_types, data_object_types, plot_type_to_data_object_type_mapping) ⇒ TabbedPlotsConfig

Processes a file and adds in the configuration defined in the file



64
65
66
67
68
69
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
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
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 64

def initialize(filename,
               plot_types,
               data_object_types,
               plot_type_to_data_object_type_mapping)
  @plot_types = plot_types
  @data_object_types = data_object_types
  @plot_type_to_data_object_type_mapping = plot_type_to_data_object_type_mapping

  @mutex = Mutex.new
  @tabs = []
  @points_saved = DEFAULT_POINTS_SAVED
  @seconds_plotted = DEFAULT_SECONDS_PLOTTED
  @points_plotted = DEFAULT_POINTS_PLOTTED
  @refresh_rate_hz = DEFAULT_REFRESH_RATE_HZ
  @packet_count = 0
  @configuration_errors = []

  if File.exist?(filename.to_s)
    # Loop over each line of the configuration file
    parser = ConfigParser.new
    parser.parse_file(filename) do |keyword, parameters|
      begin
        # Handle each keyword
        case keyword

        when 'TAB'
          # Expect 0 or 1 parameter
          parser.verify_num_parameters(0, 1, "TAB <Tab Text (optional)>")

          # Add a new tab to the array of tabs
          add_tab(parameters[0])

        when 'PLOT'
          # Expect 1 parameter
          parser.verify_num_parameters(1, 1, "PLOT <Plot Type>")

          # Add a new plot to the current tab
          add_plot(-1, create_plot(parameters[0]))

        when 'DATA_OBJECT'
          # Expect 1 parameter
          parser.verify_num_parameters(1, 1, "DATA_OBJECT <Data Object Type>")

          # Require data object file
          data_object_filename = parameters[0].downcase << '_data_object.rb'
          data_object = Cosmos.require_class(data_object_filename).new
          data_object.plot = @tabs[-1].plots[-1]

          # Add a new data object to the current plot
          @tabs[-1].plots[-1].data_objects << data_object

        else
          # Unknown keywords are passed to the current data object or current plot if there is a current tab
          current_tab = @tabs[-1]
          if current_tab
            current_plot = current_tab.plots[-1]
            if current_plot
              current_data_object = current_plot.data_objects[-1]
              if current_data_object
                current_data_object.handle_keyword(parser, keyword, parameters)
              else
                current_plot.handle_keyword(parser, keyword, parameters)
              end
            else
              raise ArgumentError, "A PLOT must be defined before using keyword: #{keyword}"
            end
          else
            case keyword
            when 'POINTS_SAVED'
              # Expect 1 parameter
              parser.verify_num_parameters(1, 1, "POINTS_SAVED <Points Saved>")

              # Update Points Saved
              @points_saved = parameters[0].to_i

            when 'SECONDS_PLOTTED'
              # Expect 1 parameter
              parser.verify_num_parameters(1, 1, "SECONDS_PLOTTED <Seconds Plotted>")

              # Update Seconds Plotted
              @seconds_plotted = parameters[0].to_f

            when 'POINTS_PLOTTED'
              # Expect 1 parameter
              parser.verify_num_parameters(1, 1, "POINTS_PLOTTED <Points Plotted>")

              # Update Points Plotted
              @points_plotted = Integer(parameters[0])

            when 'REFRESH_RATE_HZ'
              # Expect 1 parameter
              parser.verify_num_parameters(1, 1, "REFRESH_RATE_HZ <Refresh Rate in Hz>")

              # Update Points Plotted
              @refresh_rate_hz = parameters[0].to_f
              raise ArgumentError, "Invalid Refresh Rate Hz: #{@refresh_rate_hz}" if @refresh_rate_hz <= 0.0

            else
              # Handle unknown keywords
              raise ArgumentError, "A TAB must be defined before using keyword: #{keyword}"

            end
          end

        end # case keyword
      rescue Exception => error
        @configuration_errors << error
      end
    end # CosmosConfig.each
  else
    # Use default config of one tab and one plot
    add_tab()
    add_plot(-1, create_plot(@plot_types[0]))
  end

  # Build initial packet to data object mapping
  build_packet_to_data_objects_mapping()

end

Instance Attribute Details

#configuration_errorsObject

Gives access to array of errors that occurred while processing configuration file



61
62
63
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 61

def configuration_errors
  @configuration_errors
end

#data_object_typesObject

Data object types known by this tabbed plots definition



55
56
57
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 55

def data_object_types
  @data_object_types
end

#plot_type_to_data_object_type_mappingObject

Mapping of what data object types a plot can handle



58
59
60
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 58

def plot_type_to_data_object_type_mapping
  @plot_type_to_data_object_type_mapping
end

#plot_typesObject

Plot types known by this tabbed plots definition



52
53
54
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 52

def plot_types
  @plot_types
end

#points_plottedObject

Global Points Plotted Setting



46
47
48
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 46

def points_plotted
  @points_plotted
end

#points_savedObject

Global Points Saved Setting



43
44
45
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 43

def points_saved
  @points_saved
end

#refresh_rate_hzObject

Global Refresh Rate Hz Setting



49
50
51
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 49

def refresh_rate_hz
  @refresh_rate_hz
end

#seconds_plottedObject

Global Seconds Plotted Setting



40
41
42
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 40

def seconds_plotted
  @seconds_plotted
end

#tabsObject

Gives access to the array of tabs defined by the configuration file



37
38
39
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 37

def tabs
  @tabs
end

Instance Method Details

#add_data_object(tab_index, plot_index, data_object) ⇒ Object

Adds a data object to the definition



239
240
241
242
243
244
245
246
247
248
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 239

def add_data_object(tab_index, plot_index, data_object)
  @mutex.synchronize do
    plot = @tabs[tab_index].plots[plot_index]
    data_object.plot = plot
    plot.data_objects << data_object
    build_packet_to_data_objects_mapping()
    plot.redraw_needed = true
    data_object
  end
end

#add_plot(tab_index, plot) ⇒ Object

Adds a plot to the definition



226
227
228
229
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 226

def add_plot(tab_index, plot)
  plot.tab = @tabs[tab_index]
  @tabs[tab_index].plots << plot
end

#add_tab(tab_text = nil) ⇒ Object

Adds a tab to the definition



201
202
203
204
205
206
207
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 201

def add_tab(tab_text = nil)
  @mutex.synchronize do
    # Add a new tab to the array of tabs
    @tabs << TabbedPlotsTab.new(tab_text)
    @tabs[-1]
  end
end

#configuration_stringObject

Returns the configuration



185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 185

def configuration_string
  @mutex.synchronize do
    configuration = ''
    configuration << "SECONDS_PLOTTED #{@seconds_plotted}\n"
    configuration << "POINTS_SAVED #{@points_saved}\n"
    configuration << "POINTS_PLOTTED #{@points_plotted}\n"
    configuration << "REFRESH_RATE_HZ #{@refresh_rate_hz}\n"
    @tabs.each do |tab|
      configuration << "\n"
      configuration << tab.configuration_string
    end
    configuration
  end
end

#create_plot(plot_type, tab_index = -1)) ⇒ Object

Creates a new plot object



217
218
219
220
221
222
223
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 217

def create_plot(plot_type, tab_index = -1)
  @mutex.synchronize do
    # Require plot file
    plot_filename = plot_type.downcase << '_plot.rb'
    Cosmos.require_class(plot_filename).new
  end
end

#duplicate_data_object(tab_index, plot_index, data_object_index) ⇒ Object

Duplicates a data object in the definition and adds it to the definition



281
282
283
284
285
286
287
288
289
290
291
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 281

def duplicate_data_object(tab_index, plot_index, data_object_index)
  @mutex.synchronize do
    plot = @tabs[tab_index].plots[plot_index]
    data_object = plot.data_objects[data_object_index]
    plot.data_objects << data_object.copy
    build_packet_to_data_objects_mapping()
    plot.data_objects[-1].plot = plot
    plot.redraw_needed = true
    plot.data_objects[-1]
  end
end

#each_data_objectObject

Yields each data object (mutex must already be acquired before calling)



375
376
377
378
379
380
381
382
383
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 375

def each_data_object
  @tabs.each do |tab|
    tab.plots.each do |plot|
      plot.data_objects.each do |data_object|
        yield data_object
      end
    end
  end
end

#edit_data_object(tab_index, plot_index, data_object_index, editted_data_object) ⇒ Object

Edits a data object in the definition



269
270
271
272
273
274
275
276
277
278
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 269

def edit_data_object(tab_index, plot_index, data_object_index, editted_data_object)
  data_object = @tabs[tab_index].plots[plot_index].data_objects[data_object_index]
  if data_object.edit_safe?(editted_data_object)
    @mutex.synchronize do
      data_object.edit(editted_data_object)
    end
  else
    replace_data_object(tab_index, plot_index, data_object_index, editted_data_object)
  end
end

#export_data_objects(progress = nil, tab_index = nil, plot_index = nil, data_object_index = nil) ⇒ Object

Exports each data object



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 320

def export_data_objects(progress = nil, tab_index = nil, plot_index = nil, data_object_index = nil)
  columns = []
  @mutex.synchronize do
    tabs = tab_index ? [@tabs[tab_index]] : @tabs
    tabs.each do |tab|
      plots = plot_index ? [tab.plots[plot_index]] : tab.plots
      plots.each do |plot|
        data_objects = data_object_index ? [plot.data_objects[data_object_index]] : plot.data_objects
        data_objects.each do |data_object|
          progress.append_text("Exporting #{data_object.name} on plot '#{plot.title}' on tab '#{tab.tab_text}'") if progress
          columns.concat(data_object.export)
        end
      end
    end
  end
  columns
end

#move_data_object(tab_index, plot_index, start_index, end_index) ⇒ Object

Moves a data object from one index to another



251
252
253
254
255
256
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 251

def move_data_object(tab_index, plot_index, start_index, end_index)
  @mutex.synchronize do
    data_objects = @tabs[tab_index].plots[plot_index].data_objects
    data_objects.insert(end_index, data_objects.delete_at(start_index))
  end
end

#mu_synchronizeObject

Takes the mutex protecting the tabbed plot definition



386
387
388
389
390
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 386

def mu_synchronize
  @mutex.synchronize do
    yield
  end
end

#process_packet(packet) ⇒ Object

Processes a packet for all data objects that request it



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 339

def process_packet(packet)
  # Increment packet count
  @packet_count += 1

  # Get target name and packet name
  target_name = packet.target_name
  packet_name = packet.packet_name

  # Route packet to its data object(s)
  if target_name and packet_name
    index = target_name + ' ' + packet_name
    @mutex.synchronize do
      data_objects = @packet_to_data_objects_mapping[index]
      process_packet_in_each_data_object(data_objects, packet, @packet_count) if data_objects
    end
  end
end

#process_packet_in_each_data_objectObject

Optimization method to move each call to C code



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'ext/cosmos/ext/tabbed_plots_config/tabbed_plots_config.c', line 22

static VALUE process_packet_in_each_data_object (VALUE self, VALUE data_objects, VALUE packet, VALUE packet_count)
{
  int index = 0;
  long length = 0;
  volatile VALUE data_object = Qnil;

  length = RARRAY_LEN(data_objects);
  if (length > 0)
  {
    for (index = 0; index < length; index++)
    {
      data_object = rb_ary_entry(data_objects, index);
      rb_funcall(data_object, id_method_process_packet, 2, packet, packet_count);
    }
  }

  return Qnil;
}

#remove_data_object(tab_index, plot_index, data_object_index) ⇒ Object

Removes a data object from the definition



259
260
261
262
263
264
265
266
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 259

def remove_data_object(tab_index, plot_index, data_object_index)
  @mutex.synchronize do
    plot = @tabs[tab_index].plots[plot_index]
    plot.data_objects.delete_at(data_object_index)
    build_packet_to_data_objects_mapping()
    plot.redraw_needed = true
  end
end

#remove_plot(tab_index, plot_index) ⇒ Object

Removes a plot from the definition



232
233
234
235
236
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 232

def remove_plot(tab_index, plot_index)
  @mutex.synchronize do
    @tabs[tab_index].plots.delete_at(plot_index)
  end
end

#remove_tab(tab_index) ⇒ Object

Removes a tab from the definition



210
211
212
213
214
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 210

def remove_tab(tab_index)
  @mutex.synchronize do
    @tabs.delete_at(tab_index)
  end
end

#replace_data_object(tab_index, plot_index, data_object_index, data_object) ⇒ Object

Replaces one data object in the definition with another



294
295
296
297
298
299
300
301
302
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 294

def replace_data_object(tab_index, plot_index, data_object_index, data_object)
  @mutex.synchronize do
    plot = @tabs[tab_index].plots[plot_index]
    data_object.plot = plot
    plot.data_objects[data_object_index] = data_object
    build_packet_to_data_objects_mapping()
    plot.redraw_needed = true
  end
end

#reset_data_objects(tab_index = nil, plot_index = nil, data_object_index = nil) ⇒ Object

Resets each data object



305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 305

def reset_data_objects(tab_index = nil, plot_index = nil, data_object_index = nil)
  @mutex.synchronize do
    if data_object_index
      @tabs[tab_index].plots[plot_index].data_objects[data_object_index].reset
    elsif plot_index
      @tabs[tab_index].plots[plot_index].data_objects.each {|data_object| data_object.reset}
    elsif tab_index
      @tabs[tab_index].plots.each {|plot| plot.data_objects.each {|data_object| data_object.reset}}
    else
      @tabs.each {|tab| tab.plots.each {|plot| plot.data_objects.each {|data_object| data_object.reset}}}
    end
  end
end

#update_max_points_saved(total_points_saved) ⇒ Object

Updates max points saved in each data object



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb', line 358

def update_max_points_saved(total_points_saved)
  @mutex.synchronize do
    # Count number of data objects
    num_data_objects = 0
    each_data_object {|_| num_data_objects += 1}

    if num_data_objects > 0
      points_saved_per_data_object = total_points_saved / num_data_objects
      points_saved_per_data_object = 1 if points_saved_per_data_object < 1
      each_data_object do |data_object|
        data_object.max_points_saved = points_saved_per_data_object
      end
    end
  end
end