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.

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



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/ctioga2/plotmaker.rb', line 197

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
end

Instance Attribute Details

#cleanupObject

Whether intermediate files are cleaned up automatically afterwards or not…



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

def cleanup
  @cleanup
end

#curve_generatorObject

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



159
160
161
# File 'lib/ctioga2/plotmaker.rb', line 159

def curve_generator
  @curve_generator
end

#data_stackObject

The Data::DataStack object that manipulates Dataset objects



151
152
153
# File 'lib/ctioga2/plotmaker.rb', line 151

def data_stack
  @data_stack
end

#figure_nameObject

The name of the figure



165
166
167
# File 'lib/ctioga2/plotmaker.rb', line 165

def figure_name
  @figure_name
end

#interpreterObject

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



148
149
150
# File 'lib/ctioga2/plotmaker.rb', line 148

def interpreter
  @interpreter
end

#latex_preambleObject

Additional preamble for LaTeX output



171
172
173
# File 'lib/ctioga2/plotmaker.rb', line 171

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.



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

def mark
  @mark
end

#output_directoryObject

The output directory



168
169
170
# File 'lib/ctioga2/plotmaker.rb', line 168

def output_directory
  @output_directory
end

#postprocessObject

What happens to generated PDF files (a PostProcess object)



174
175
176
# File 'lib/ctioga2/plotmaker.rb', line 174

def postprocess
  @postprocess
end

#root_objectObject

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



155
156
157
# File 'lib/ctioga2/plotmaker.rb', line 155

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.



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

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.



315
316
317
318
319
320
321
# File 'lib/ctioga2/plotmaker.rb', line 315

def add_curve(dataset, options = {})
  plot = @root_object.current_plot
  curve = @curve_generator.
    curve_from_dataset(plot, dataset, options)
  plot.add_element(curve)
  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



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/ctioga2/plotmaker.rb', line 327

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
    # We first trim elements from options that are not inside
    # Graphics::Styles::CurveStyleFactory::PlotCommandOptions
    options.delete_if { |k,v|
      ! Graphics::Styles::
      CurveStyleFactory::PlotCommandOptions.key?(k)
    }
    add_curve(set, options)
  end
end

#draw_figure(figname = "Plot", last = false) ⇒ Object

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

TODO:

  • cleanup or not ?



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/ctioga2/plotmaker.rb', line 271

def draw_figure(figname = "Plot", last = false)
  return if @root_object.empty?
  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.



258
259
260
261
262
263
264
# File 'lib/ctioga2/plotmaker.rb', line 258

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:



249
250
251
252
253
254
# File 'lib/ctioga2/plotmaker.rb', line 249

def reset_graphics
  draw_figure(@figure_name || "Plot", true)

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

#run(command_line) ⇒ Object

ctioga’s entry point.



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/ctioga2/plotmaker.rb', line 223

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", true)
  rescue SystemExit => e
    # We special-case the exit exception ;-)...
  rescue Exception => e
    debug format_exception(e)
    fatal "#{e.message}"
  end
end