Class: CTioga2::PlotMaker

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/ctioga2/plotmaker.rb

Overview

This class is the core of ctioga. It parses the command-line arguments, reads all necessary files and plots graphs. Most of its functionality is delegated into classes.

todo An important point would be to provide a facility that holds all the default values. To each would be assigned a given name, and programs would only use something like code value = Default::value(‘stuff’) endcode

Setting up defaults would only be a question of using one single command (with admittedly many optional arguments)

Constant Summary collapse

@@first_plotmaker_instance =

The first instance of PlotMaker created

nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

debug, error, fatal, #format_exception, #identify, info, init_logger, logger, set_level, #spawn, warn

Constructor Details

#initializePlotMaker

Setting up of the PlotMaker object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/ctioga2/plotmaker.rb', line 231

def initialize
  CTioga2::Log::init_logger
  @data_stack = Data::DataStack.new
  @root_object = Graphics::RootObject.new
  @interpreter = Commands::Interpreter.new(self)
  @curve_generator = Graphics::CurveGenerator.new

  # Figure name:
  @figure_name = nil

  # Original preamble
  @latex_preamble = ""

  @postprocess = PostProcess.new

  # Make sure it is registered
  @@first_plotmaker_instance ||= self

  # We mark by default, as it comes dead useful.
  @mark = true

  # Remove intermediate files by default.
  @cleanup = true

  # Make curve style stack empty
  @curve_style_stack = []
end

Instance Attribute Details

#cleanupObject

Whether intermediate files are cleaned up automatically afterwards or not…



213
214
215
# File 'lib/ctioga2/plotmaker.rb', line 213

def cleanup
  @cleanup
end

#curve_generatorObject

A Graphics::CurveGenerator object in charge of producing suitable elements to be added to the Graphics::RootObject



190
191
192
# File 'lib/ctioga2/plotmaker.rb', line 190

def curve_generator
  @curve_generator
end

#curve_style_stackObject

The stack of CurveStyle objects that were used so far.



216
217
218
# File 'lib/ctioga2/plotmaker.rb', line 216

def curve_style_stack
  @curve_style_stack
end

#data_stackObject

The Data::DataStack object that manipulates Dataset objects



182
183
184
# File 'lib/ctioga2/plotmaker.rb', line 182

def data_stack
  @data_stack
end

#figure_nameObject

The name of the figure



196
197
198
# File 'lib/ctioga2/plotmaker.rb', line 196

def figure_name
  @figure_name
end

#interpreterObject

The Commands::Interpreter object which runs all the commands.



179
180
181
# File 'lib/ctioga2/plotmaker.rb', line 179

def interpreter
  @interpreter
end

#latex_preambleObject

Additional preamble for LaTeX output



202
203
204
# File 'lib/ctioga2/plotmaker.rb', line 202

def latex_preamble
  @latex_preamble
end

#markObject

Whether or not to include the command-line used to produce the file in the target PDF file.



209
210
211
# File 'lib/ctioga2/plotmaker.rb', line 209

def mark
  @mark
end

#output_directoryObject

The output directory



199
200
201
# File 'lib/ctioga2/plotmaker.rb', line 199

def output_directory
  @output_directory
end

#postprocessObject

What happens to generated PDF files (a PostProcess object)



205
206
207
# File 'lib/ctioga2/plotmaker.rb', line 205

def postprocess
  @postprocess
end

#root_objectObject

The Graphics::RootObject in charge of holding all things that will eventually get drawn



186
187
188
# File 'lib/ctioga2/plotmaker.rb', line 186

def root_object
  @root_object
end

Class Method Details

.plotmakerObject

Returns the first created instance of PlotMaker. This sounds less object-oriented, yes, but that can come in useful some times.



225
226
227
# File 'lib/ctioga2/plotmaker.rb', line 225

def self.plotmaker
  return @@first_plotmaker_instance
end

Instance Method Details

#add_curve(dataset, options = {}) ⇒ Object

Add one Data::Dataset object using the current style (that can be overridden by stuff given as options) to the #root_object.

todo here, keep a state of the current styles:

  • which is the color/marker/filling and so on of the curve ?

  • are we drawing plain 2D curve, a histogram or something even more fancy ?

  • this should be a separated class.

todo all curve objects should only take a Data::Dataset and a style as arguments to new.



376
377
378
379
380
381
382
383
# File 'lib/ctioga2/plotmaker.rb', line 376

def add_curve(dataset, options = {})
  plot = @root_object.current_plot
  curve = @curve_generator.
    curve_from_dataset(plot, dataset, options)
  plot.add_element(curve)
  @curve_style_stack << curve.curve_style
  info { "Adding curve '#{dataset.name}' to the current plot" }
end

#add_curves(dataset_spec, options = {}) ⇒ Object

Transforms a dataset_spec into one or several Data::Dataset using the current backend (or any other that might be specified in the options), and add them as curves to the #root_object, using #add_curve



389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/ctioga2/plotmaker.rb', line 389

def add_curves(dataset_spec, options = {})
  begin
    sets = @data_stack.get_datasets(dataset_spec, options)
  rescue Exception => exception
    error { "A problem occurred while processing dataset '#{dataset_spec}' using backend #{@data_stack.backend_factory.current.description.name}. Ignoring it." }
    debug { format_exception(exception) }
    return
  end
  for set in sets
    add_curve(set, options)
  end
end

#draw_figure(figname = "Plot-%03d", last = false) ⇒ Object

Draws the figure currently accumulated in the #root_object. It returns the path of the PDF file produced.

If figname contains a % sign, it will be interpreted as a format, and ctioga will attempt to find the first numbered file that does not exists.

todo

  • cleanup or not ?



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
# File 'lib/ctioga2/plotmaker.rb', line 312

def draw_figure(figname = "Plot-%03d", last = false)
  return if @root_object.empty?
  
  if figname =~ /%/
    i = 0
    prev = figname.dup
    while true
      f = figname % i
      if f == prev
        figname = f
        break
      end
      if File::exist?("#{f}.pdf")
        i += 1
      else
        figname = f
        break
      end
      prev = f
    end
  end
  
  info { "Producing figure '#{figname}'" }

  t = create_figure_maker
  # If figname is clearly a path, we split it into directory/name
  # and set the output directory to directory.
  if File::basename(figname) != figname
    dir = File::dirname(figname)
    # If path is relative and output_directory is specified, we make
    # the path relative to output_dir
    if @output_directory && dir =~ /^[^\/~]/
      dir = File::join(@output_directory, dir)
    end
    t.save_dir = dir
    figname = File::basename(figname)
  elsif @output_directory
    t.save_dir = @output_directory
  end

  t.def_figure(figname) do
    @root_object.draw_root_object(t)
  end
  t.make_preview_pdf(t.figure_index(figname))

  file = t.save_dir ? File::join(t.save_dir, figname + ".pdf") : 
    figname + ".pdf"

  # Feed it
  @postprocess.process_file(file, last)
  return file
end

#quoted_command_lineObject

Returns a quoted version of the command line, that possibly could be used again to reproduce the same results.



295
296
297
298
299
300
301
# File 'lib/ctioga2/plotmaker.rb', line 295

def quoted_command_line
  quoted_args = @command_line.collect do |s|
    Utils::shell_quote_string(s)
  end.join ' '
  
  return "#{File.basename($0)} #{quoted_args}"
end

#reset_graphicsObject

Flushes the current root object and starts a new one:



286
287
288
289
290
291
# File 'lib/ctioga2/plotmaker.rb', line 286

def reset_graphics
  draw_figure(@figure_name || "Plot-%03d", true)

  @root_object = Graphics::RootObject.new
  @curve_generator = Graphics::CurveGenerator.new
end

#run(command_line) ⇒ Object

ctioga’s entry point.



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/ctioga2/plotmaker.rb', line 260

def run(command_line)

  # The main catch-all around the plot:
  begin
    @command_line = command_line.dup
    if ENV.key? 'CTIOGA2_PRE'
      command_line.unshift(*Shellwords.shellwords(ENV['CTIOGA2_PRE']))
    end
    
    if ENV.key? 'CTIOGA2_POST'
      command_line.push(*Shellwords.shellwords(ENV['CTIOGA2_POST']))
    end
    
    @interpreter.run_command_line(command_line)
    
    # Now, draw the main figure
    file = draw_figure(@figure_name || "Plot-%03d", true)
  rescue SystemExit => e
    # We special-case the exit exception ;-)...
  rescue Exception => e
    debug { format_exception(e) }
    fatal { "#{e.message}" }
  end
end