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

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

Constructor Details

#initializePlotMaker

Setting up of the PlotMaker object



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

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 = ""

  @latex_font = Graphics::Styles::FullLatexFont.new

  @latex_font.size = 15

  @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…



220
221
222
# File 'lib/ctioga2/plotmaker.rb', line 220

def cleanup
  @cleanup
end

#curve_generatorObject

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



194
195
196
# File 'lib/ctioga2/plotmaker.rb', line 194

def curve_generator
  @curve_generator
end

#curve_style_stackObject

The stack of CurveStyle objects that were used so far.



223
224
225
# File 'lib/ctioga2/plotmaker.rb', line 223

def curve_style_stack
  @curve_style_stack
end

#data_stackObject

The Data::DataStack object that manipulates Dataset objects



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

def data_stack
  @data_stack
end

#figure_nameObject

The name of the figure



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

def figure_name
  @figure_name
end

#interpreterObject

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



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

def interpreter
  @interpreter
end

#latex_fontObject

Global font information



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

def latex_font
  @latex_font
end

#latex_preambleObject

Additional preamble for LaTeX output



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

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.



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

def mark
  @mark
end

#output_directoryObject

The output directory



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

def output_directory
  @output_directory
end

#postprocessObject

What happens to generated PDF files (a PostProcess object)



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

def postprocess
  @postprocess
end

#root_objectObject

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



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

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.



232
233
234
# File 'lib/ctioga2/plotmaker.rb', line 232

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.



410
411
412
413
414
415
416
417
# File 'lib/ctioga2/plotmaker.rb', line 410

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



423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/ctioga2/plotmaker.rb', line 423

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 ?



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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/ctioga2/plotmaker.rb', line 323

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

  path = Pathname.new(figname)
  if @output_directory
    out = Pathname.new(@output_directory)
    path = out + path
  end

  # We always cd into the target directory for creading the
  Dir.chdir(path.dirname) do
    fn = path.basename.to_s

    efn = fn.gsub(/[.\s]/) do |x|
      "__#{x[0].ord}__"
    end

    if efn != fn
      debug { "Mangled name to '#{efn}'"}
    end

    t.def_figure(efn) do
      @latex_font.set_global_font(t)
      @root_object.draw_root_object(t)
    end
    t.make_preview_pdf(t.figure_index(efn))

    # We look for latex errors
    if t.respond_to? :pdflatex_errors
      errs = t.pdflatex_errors
      if errs.size > 0
        error { "pdflatex returned with #{errs.size} error lines"}
        for l in errs
          warn { "pdflatex error: #{l.chomp}" }
        end
      end
    end

    # We first rename the PDF file if it was mangled.
    if efn != fn
      File::rename("#{efn}.pdf", "#{fn}.pdf")
    end

  end

  file = path.to_s + ".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.



306
307
308
309
310
311
312
# File 'lib/ctioga2/plotmaker.rb', line 306

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:



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

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.



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

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