Class: CTioga2::PlotMaker
- Inherits:
-
Object
- Object
- CTioga2::PlotMaker
- 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
-
#cleanup ⇒ Object
Whether intermediate files are cleaned up automatically afterwards or not…
-
#curve_generator ⇒ Object
A Graphics::CurveGenerator object in charge of producing suitable elements to be added to the Graphics::RootObject.
-
#curve_style_stack ⇒ Object
The stack of CurveStyle objects that were used so far.
-
#data_stack ⇒ Object
The Data::DataStack object that manipulates Dataset objects.
-
#figure_name ⇒ Object
The name of the figure.
-
#interpreter ⇒ Object
The Commands::Interpreter object which runs all the commands.
-
#latex_font ⇒ Object
Global font information.
-
#latex_preamble ⇒ Object
Additional preamble for LaTeX output.
-
#makefile_dependencies ⇒ Object
If not empty, a Makefile file where the dependencies are written out.
-
#mark ⇒ Object
Whether or not to include the command-line used to produce the file in the target PDF file.
-
#output_directory ⇒ Object
The output directory.
-
#pause_on_errors ⇒ Object
Whether or not one should pause at the end if there are errors.
-
#pdf_resolution ⇒ Object
The resolution of the PDF file.
-
#postprocess ⇒ Object
What happens to generated PDF files (a PostProcess object).
-
#root_object ⇒ Object
The Graphics::RootObject in charge of holding all things that will eventually get drawn.
Class Method Summary collapse
-
.plotmaker ⇒ Object
Returns the first created instance of PlotMaker.
Instance Method Summary collapse
-
#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.
-
#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.
-
#draw_figure(figname = "Plot-%03d", last = false) ⇒ Object
Draws the figure currently accumulated in the #root_object.
-
#initialize ⇒ PlotMaker
constructor
Setting up of the PlotMaker object.
-
#quoted_command_line ⇒ Object
Returns a quoted version of the command line, that possibly could be used again to reproduce the same results.
-
#reset_graphics ⇒ Object
Flushes the current root object and starts a new one:.
-
#run(command_line) ⇒ Object
ctioga’s entry point.
Methods included from Log
context, counts, debug, error, fatal, #format_exception, #identify, info, init_logger, log_to, logger, set_level, #spawn, warn
Constructor Details
#initialize ⇒ PlotMaker
Setting up of the PlotMaker object
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 278 279 280 281 282 283 |
# File 'lib/ctioga2/plotmaker.rb', line 253 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
#cleanup ⇒ Object
Whether intermediate files are cleaned up automatically afterwards or not…
224 225 226 |
# File 'lib/ctioga2/plotmaker.rb', line 224 def cleanup @cleanup end |
#curve_generator ⇒ Object
A Graphics::CurveGenerator object in charge of producing suitable elements to be added to the Graphics::RootObject
198 199 200 |
# File 'lib/ctioga2/plotmaker.rb', line 198 def curve_generator @curve_generator end |
#curve_style_stack ⇒ Object
The stack of CurveStyle objects that were used so far.
227 228 229 |
# File 'lib/ctioga2/plotmaker.rb', line 227 def curve_style_stack @curve_style_stack end |
#data_stack ⇒ Object
The Data::DataStack object that manipulates Dataset objects
190 191 192 |
# File 'lib/ctioga2/plotmaker.rb', line 190 def data_stack @data_stack end |
#figure_name ⇒ Object
The name of the figure
204 205 206 |
# File 'lib/ctioga2/plotmaker.rb', line 204 def figure_name @figure_name end |
#interpreter ⇒ Object
The Commands::Interpreter object which runs all the commands.
187 188 189 |
# File 'lib/ctioga2/plotmaker.rb', line 187 def interpreter @interpreter end |
#latex_font ⇒ Object
Global font information
213 214 215 |
# File 'lib/ctioga2/plotmaker.rb', line 213 def latex_font @latex_font end |
#latex_preamble ⇒ Object
Additional preamble for LaTeX output
210 211 212 |
# File 'lib/ctioga2/plotmaker.rb', line 210 def latex_preamble @latex_preamble end |
#makefile_dependencies ⇒ Object
If not empty, a Makefile file where the dependencies are written out
235 236 237 |
# File 'lib/ctioga2/plotmaker.rb', line 235 def makefile_dependencies @makefile_dependencies end |
#mark ⇒ Object
Whether or not to include the command-line used to produce the file in the target PDF file.
220 221 222 |
# File 'lib/ctioga2/plotmaker.rb', line 220 def mark @mark end |
#output_directory ⇒ Object
The output directory
207 208 209 |
# File 'lib/ctioga2/plotmaker.rb', line 207 def output_directory @output_directory end |
#pause_on_errors ⇒ Object
Whether or not one should pause at the end if there are errors. Useful on windows, where the windows closes before one has a chance to see anything.
232 233 234 |
# File 'lib/ctioga2/plotmaker.rb', line 232 def pause_on_errors @pause_on_errors end |
#pdf_resolution ⇒ Object
The resolution of the PDF file
238 239 240 |
# File 'lib/ctioga2/plotmaker.rb', line 238 def pdf_resolution @pdf_resolution end |
#postprocess ⇒ Object
What happens to generated PDF files (a PostProcess object)
216 217 218 |
# File 'lib/ctioga2/plotmaker.rb', line 216 def postprocess @postprocess end |
#root_object ⇒ Object
The Graphics::RootObject in charge of holding all things that will eventually get drawn
194 195 196 |
# File 'lib/ctioga2/plotmaker.rb', line 194 def root_object @root_object end |
Class Method Details
.plotmaker ⇒ Object
Returns the first created instance of PlotMaker. This sounds less object-oriented, yes, but that can come in useful some times.
247 248 249 |
# File 'lib/ctioga2/plotmaker.rb', line 247 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.
446 447 448 449 450 451 452 453 |
# File 'lib/ctioga2/plotmaker.rb', line 446 def add_curve(dataset, = {}) plot = @root_object.current_plot curve = @curve_generator. curve_from_dataset(plot, dataset, ) 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
459 460 461 462 463 464 465 466 467 468 469 470 |
# File 'lib/ctioga2/plotmaker.rb', line 459 def add_curves(dataset_spec, = {}) begin sets = @data_stack.get_datasets(dataset_spec, ) 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, ) 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 ?
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 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'lib/ctioga2/plotmaker.rb', line 351 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 Utils::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" if @makefile_dependencies File.open(@makefile_dependencies, "w") do |f| deps = Utils::used_files.values # TODO: handle spaces f.puts "#{file}: #{deps.join(" ")}" end end # Feed it @postprocess.process_file(file, last) return file end |
#quoted_command_line ⇒ Object
Returns a quoted version of the command line, that possibly could be used again to reproduce the same results.
334 335 336 337 338 339 340 |
# File 'lib/ctioga2/plotmaker.rb', line 334 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_graphics ⇒ Object
Flushes the current root object and starts a new one:
325 326 327 328 329 330 |
# File 'lib/ctioga2/plotmaker.rb', line 325 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.
Returns true if there was no errors and false if there was one or more.
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 |
# File 'lib/ctioga2/plotmaker.rb', line 289 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.}" } end errs = Log.counts[:error] warns = Log.counts[:warn] if errs + warns > 0 puts "ctioga2 finished with #{errs} errors and #{warns} warning" if @pause_on_errors puts "Hit ENTER to exit" STDIN.gets end end return errs == 0 end |