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.
-
#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.
-
#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
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 269 270 271 272 273 |
# File 'lib/ctioga2/plotmaker.rb', line 243 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…
220 221 222 |
# File 'lib/ctioga2/plotmaker.rb', line 220 def cleanup @cleanup end |
#curve_generator ⇒ Object
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_stack ⇒ Object
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_stack ⇒ Object
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_name ⇒ Object
The name of the figure
200 201 202 |
# File 'lib/ctioga2/plotmaker.rb', line 200 def figure_name @figure_name end |
#interpreter ⇒ Object
The Commands::Interpreter object which runs all the commands.
183 184 185 |
# File 'lib/ctioga2/plotmaker.rb', line 183 def interpreter @interpreter end |
#latex_font ⇒ Object
Global font information
209 210 211 |
# File 'lib/ctioga2/plotmaker.rb', line 209 def latex_font @latex_font end |
#latex_preamble ⇒ Object
Additional preamble for LaTeX output
206 207 208 |
# File 'lib/ctioga2/plotmaker.rb', line 206 def latex_preamble @latex_preamble end |
#mark ⇒ Object
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_directory ⇒ Object
The output directory
203 204 205 |
# File 'lib/ctioga2/plotmaker.rb', line 203 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.
228 229 230 |
# File 'lib/ctioga2/plotmaker.rb', line 228 def pause_on_errors @pause_on_errors end |
#postprocess ⇒ Object
What happens to generated PDF files (a PostProcess object)
212 213 214 |
# File 'lib/ctioga2/plotmaker.rb', line 212 def postprocess @postprocess end |
#root_object ⇒ Object
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
.plotmaker ⇒ Object
Returns the first created instance of PlotMaker. This sounds less object-oriented, yes, but that can come in useful some times.
237 238 239 |
# File 'lib/ctioga2/plotmaker.rb', line 237 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.
424 425 426 427 428 429 430 431 |
# File 'lib/ctioga2/plotmaker.rb', line 424 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
437 438 439 440 441 442 443 444 445 446 447 448 |
# File 'lib/ctioga2/plotmaker.rb', line 437 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 ?
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 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'lib/ctioga2/plotmaker.rb', line 337 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_line ⇒ Object
Returns a quoted version of the command line, that possibly could be used again to reproduce the same results.
320 321 322 323 324 325 326 |
# File 'lib/ctioga2/plotmaker.rb', line 320 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:
311 312 313 314 315 316 |
# File 'lib/ctioga2/plotmaker.rb', line 311 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.
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 303 304 305 306 307 308 |
# File 'lib/ctioga2/plotmaker.rb', line 276 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 end |