blib/lib/Chart/Scientific.pm untested

filter: Statementstmt 0.0% 0 / 525 Branchbran 0.0% 0 / 224 Conditioncond 0.0% 0 / 197 MC/DCmcdc 0.0% 0 / 106 Subroutinesub 0.0% 0 / 63 Podpod 0.0% 0 / 50 totaltotal 0.0% 0 / 1165 CC 113 SCAR 37.4
CC
113
Cov
0%
massage_args
156.0
read_file
110.0
set_window
90.0
CRAP 42.3
1  
2 package Chart::Scientific;
3  
4 use warnings;
5 use strict;
6  
7 require Exporter;
8  
9 use Carp;
10 use Data::Dumper;
11 use PDL;
12 use PDL::NiceSlice;
13 use PDL::Graphics::PGPLOT;
14 use Tie::IxHash;
15  
16 our @ISA = qw(Exporter);
17 our $VERSION = '0.16';
18  
19 our @EXPORT_OK = qw/make_plot/;
20  
21 # TODO:
22 # Add option to force display to a geometric square on the screen, so
23 # circles will appear circular
24 # Add fuction-plotting ability. Default it to nopoints
25 # Class methods clear and restore_defaults don't work right (don't seem to
26 # reset the data associate with the instance!).
27 #
28  
29  
30 ################################################################################
31 # Class methods:
32 #
33  
34 sub make_plot {
35 my $plotter = Chart::Scientific->new ( @_ );
36 $plotter->plot ();
37 }
38  
39 sub default_args {
40 return {
41 title => '',
42 subtitle => undef,
43 x_label => '',
44 y_label => '',
45 residuals_label => '',
46 residuals_pos => 0,
47 nolegend => 0,
48 legend_location => '.02,-.05',
49 residuals_size => 0.25,
50  
51 x_range => undef,
52 y_range => undef,
53 x_log => 0,
54 y_log => 0,
55 colors => 'black,red,green,blue,cyan,magenta,gray',
56 symbols => [ 3, 0, 5, 4, 6..99 ],
57  
58 font => '1',
59 char_size => '1',
60 line_width => '2',
61  
62 nopoints => 0,
63 noline => 0,
64 axis => 0,
65 axis_residuals => 0,
66  
67 filename => undef,
68 split => undef,
69  
70 device => '/xs',
71 only => 'only',
72 defaults => 0,
73 verbose => 0,
74 help => 0,
75 usage => 0,
76 version => 0,
77 };
78 }
79  
80 # Class hash member and member method to determine if
81 # a given input is allowed or not:
82 #
83 our %legal_inputs = (
84 axis => 1,
85 axis_residuals => 1,
86 char_size => 1,
87 colors => 1,
88 defaults => 1,
89 #derived_legend => 1,
90 device => 1,
91 filename => 1,
92 font => 1,
93 function => 1,
94 group_col => 1,
95 help => 1,
96 legend_location => 1,
97 legend_text => 1,
98 #limits => 1,
99 line_width => 1,
100 nopoints => 1,
101 nolegend => 1,
102 noline => 1,
103 #only => 1,
104 #opts => 1,
105 #pdls => 1,
106 #PlotPosition => 1,
107 #points => 1,
108 residuals => 1,
109 residuals_label => 1,
110 residuals_pos => 1,
111 residuals_size => 1,
112 split => 1,
113 subtitle => 1,
114 symbols => 1,
115 title => 1,
116 usage => 1,
117 verbose => 1,
118 #win => 1,
119 #
120 # [xy]_ # [xy][^_] These should be fixed before testing now.
121 #
122 x_col => 1, # xcol => 1,
123 x_data => 1, # xdata => 1,
124 x_label => 1, # xlabel => 1,
125 x_log => 1, # xlog => 1,
126 x_range => 1, # xrange => 1,
127 y_col => 1, # ycol => 1,
128 y_data => 1, # ydata => 1,
129 y_err_col => 1, # yerr_col => 1,
130 y_err_data => 1, # yerr_data => 1,
131 y_label => 1, # ylabel => 1,
132 y_log => 1, # ylog => 1,
133 y_range => 1, # yrange => 1,
134 );
135  
136 our %depreciated_options = (
137 # These options originally had this spelling, instead of [xy]_.
138 # It was inconsistent and confusing!
139 #
140 xlabel => 1,
141 xlog => 1,
142 xrange => 1,
143 yerr_col => 1,
144 yerr_data => 1,
145 ylabel => 1,
146 ylog => 1,
147 yrange => 1,
148 );
149  
150 sub is_legal_input {
151 return exists $legal_inputs{ $_[0] };
152 }
153  
154 sub is_depreciated_option {
155 return exists $depreciated_options{ $_[0] };
156 }
157  
158 sub newstyle_option {
159 my ( $option_name ) = @_;
160 substr ( $option_name, 1, 0, "_" );
161 return $option_name;
162 }
163  
164 ################################################################################
165 # Object methods:
166 #
167  
168 sub new {
169 my $proto = shift;
170 my $class = ref ( $proto ) || $proto;
171 my @args = @_;
172  
173 # Set defaults:
174 #
175 my $self = default_args ();
176 bless $self, $class;
177  
178 # Use the initial arguments to set up member variables:
179 #
180 $self->setvars ( @args );
181 $self->massage_args ();
182  
183 $self->read_points () if !$self->points_loaded () && !$self->pdls_loaded ();
184 $self->make_pdls () if $self->points_loaded () && !$self->pdls_loaded ();
185 $self->make_pdl_residuals () if $self->{residuals};
186  
187 return $self;
188 }
189  
190 sub clear {
191 my $self = shift () or die "no self";
192 $self = {};
193 }
194  
195 sub restore_defaults {
196 my $self = shift () or die "no self";
197 $self = default_args ();
198 }
199  
200 sub DESTROY {
201 my $self = shift () or die "no self";
202 undef $self;
203 }
204  
205 sub points_loaded {
206 my $self = shift () or die "no self in points_loaded";
207 return defined $self->{points};
208 }
209  
210 sub pdls_loaded {
211 my $self = shift () or die "no self in pdls_loaded";
212 return defined $self->{pdls};
213 }
214  
215 sub setvars {
216 my $self = shift () or die "no self";
217 my @args = @_;
218  
219 while ( @args ) {
220 my $arg = shift ( @args );
221  
222 if ( 'HASH' ne ref $arg ) {
223 $arg = newstyle_option ( $arg )
224 if is_depreciated_option ( $arg );
225 die "Attempting to set illegal data member $arg\n"
226 if ! is_legal_input ( $arg );
227 $self->{$arg} = shift ( @args );
228 }
229 else {
230 foreach ( keys %$arg ) {
231 my $new_key = is_depreciated_option ( $_ )
232 ? newstyle_option ( $_ )
233 : $_;
234  
235 die "Attempting to set illegal data member $_\n"
236 if ! is_legal_input ( $new_key );
237 $self->{$new_key} = $arg->{$_};
238 }
239 }
240 }
241 $self->death_or_help_if_necessary ();
242 }
243  
244 sub death_or_help_if_necessary {
245 my $self = shift () or die "no self";
246  
247 die "Chart::Scientific version $VERSION"
248 if $self->{version};
249  
250 die "Cannot specify x_data/y_data if filename has been specified"
251 if $self->{filename} &&
252 exists $self->{y_data};
253  
254 die "Must specify y_data if filename has not been specified " .
255 "(are you trying to call the constructor without plot data?)\n"
256 if ! defined $self->{y_data} &&
257 ! $self->{filename};
258  
259 # Die if defaults are requested
260 #
261 die Data::Dumper->Dump( [ $self ], [ qw( Chart::Scientific ) ] ),
262 "Printing defaults and exiting on user request"
263 if $self->{defaults};
264  
265 help ( 1 ) if $self->{help};
266 help ( 2 ) if $self->{usage};
267 }
268  
269 sub getvars {
270 croak 'usage : $p->getvars( $var )'
271 if scalar @_ < 2;
272 my ( $self, @vars ) = @_;
273 return map { $self->{$_} } @vars;
274 }
275  
276 sub read_points {
277 my $self = shift () or die "no self in read_points";
278  
279 # If we're here, we must be reading from a file:
280 #
281 die "Can't find $self->{filename}"
282 if not ( -f $self->{filename} || 'stdin' eq $self->{filename} );
283  
284 # If $self->{split} has been specified, assume the data file is delimitted
285 # by that. Otherwise, assume it's a RDB (or tab-delimitted) file:
286 #
287 ( defined $self->{split} )
288 ? $self->read_file ()
289 : $self->read_RDB ();
290  
291 print "read-in points: ", Dumper $self->{points}, "\n",
292 "Legend array: ", Dumper $self->{derived_legend}
293 if $self->{verbose} > 1;
294 }
295  
296 sub read_RDB {
297 my $self = shift () or die "no self in read_RDB";
298  
299 print "reading with RDB.pm\n"
300 if $self->{verbose} > 0;
301  
302 my $rdb = RDB->new ( $self->{filename} )
303 or die "RDB open failed on file '$self->{filename}' $!";
304  
305 my $r_line = {};
306  
307 $self->{derived_legend} = {};
308 tie %{$self->{derived_legend}}, "Tie::IxHash";
309  
310 # Read each line of the RDB file, and stuff it into the
311 # $self->{points}{$group_col}[$i] structure:
312 #
313 while ( $rdb->read( $r_line ) ) {
314  
315 my $brk = ( defined $self->{group_col} )
316 ? $r_line->{$self->{group_col}}
317 : $self->{only};
318  
319 push @{$self->{points}{$brk}{x}}, $r_line->{$self->{x_col}};
320  
321 foreach ( 0 .. scalar @{$self->{y_col}} - 1 ) {
322  
323 print "read: $_ $r_line->{$self->{y_col}[$_]} $self->{y_col}[$_]\n"
324 if $self->{verbose} > 3;
325  
326 push @{$self->{points}{$brk}{y}[ $_]},
327 $r_line->{$self->{y_col}[ $_]};
328 push @{$self->{points}{$brk}{y_err}[$_]},
329 $r_line->{$self->{y_err_col}[$_]}
330 if defined $self->{y_err_col};
331  
332 my $leg_key = $self->{y_col}[$_];
333 $leg_key .= $brk
334 if $brk ne $self->{only};
335  
336 $self->{derived_legend}{$leg_key} = 1;
337 }
338 }
339 $self->{derived_legend} = [ keys %{$self->{derived_legend}} ];
340 }
341  
342 sub get_fh {
343 my $self = shift () or die "no self";
344  
345 # If a filename is given, read from it; otherwise, read from STDIN:
346 #
347 my $fh;
348 if ( 'stdin' eq $self->{filename} ) {
349 $fh = 'STDIN';
350 }
351 else {
352 open $fh, $self->{filename} or die "can't open $self->{filename}: $!"
353 if $self->{filename} ne 'stdin';
354 }
355 return $fh;
356 }
357  
358 sub read_file {
359 my $self = shift () or die "no self in read_file";
360  
361 print "reading from regular file\n"
362 if $self->{verbose} > 0;
363  
364 my ( $fh, @col_names, %col_number ) = ( $self->get_fh (), (), () );
365 $self->{derived_legend} = {};
366 tie %{$self->{derived_legend}}, "Tie::IxHash";
367  
368 while ( <$fh> ) {
369 next if /^#/ || /^[#SNM\t]+$/; # skip comments and definition lines (if
370 # you're kludge-reading an RDB file)
371  
372 chomp ( my @vals = split /$self->{split}/, $_ );
373 die "Split returned less than 2 items per line-- abort!\n"
374 if 2 > scalar @vals;
375  
376 # Read in the names from the first line, if applicable:
377 #
378 if ( 0 == scalar @col_names ) {
379 @col_names = @vals;
380 %col_number = map {$col_names[$_] => $_} 0..scalar @col_names-1;
381 next;
382 }
383 my $brk = ( defined $self->{group_col} )
384 ? $vals[ $col_number{ $self->{group_col} } ]
385 : $self->{only};
386 push @{$self->{points}{$brk}{x}},
387 $vals[ $col_number{ $self->{x_col} } ];
388  
389 foreach ( 0 .. scalar @{$self->{y_col}} - 1 ) {
390 my $cur_ycol = $col_number{ $self->{y_col}[$_] };
391 my $cur_ecol = $col_number{ $self->{y_err_col}[$_] }
392 if defined $self->{y_err_col};
393  
394 push @{$self->{points}{$brk}{y}[ $_]}, $vals[ $cur_ycol ];
395 push @{$self->{points}{$brk}{y_err}[$_]}, $vals[ $cur_ecol ]
396 if defined $self->{y_err_col};
397  
398 my $leg_key = $self->{y_col}[$_] .
399 ( $brk eq $self->{only} ? "" : ", $brk:" );
400 $self->{derived_legend}{$leg_key} = 1;
401 }
402 }
403 $self->{derived_legend} = [ keys %{$self->{derived_legend}} ];
404  
405 close $fh if $self->{filename} ne 'stdin';
406 }
407  
408 sub plot {
409 my $self = shift () or die "no self in draw_points";
410  
411 $self->make_pdls () if ! $self->pdls_loaded ();
412 $self->make_pdl_residuals () if $self->{residuals};
413 $self->logify_pdls () if $self->{x_log} || $self->{y_log};
414  
415 $self->setup_win ();
416 $self->get_limits ( 0 );
417 $self->set_window ();
418  
419 $self->points_draw_loop ( );
420  
421 my $font_charsize_opts = {
422 Font => $self->{font},
423 HardFont => $self->{font},
424 CharSize => $self->{char_size},
425 HardCH => $self->{char_size},
426 };
427 $self->{win}->label_axes (
428 ( defined $self->{residuals}
429 ? ( "", @{$self}{'y_label','title'} )
430 : ( @{$self}{'x_label','y_label','title'} ) ),
431 $font_charsize_opts
432 );
433 PGPLOT::pgmtxt ( 'T', 0.5, 0.5, 0.5, $self->{subtitle} )
434 if defined $self->{subtitle};
435  
436 $self->write_legend () if ! $self->{nolegend};
437  
438 $self->plot_residuals ( $font_charsize_opts )
439 if defined $self->{residuals};
440  
441 $self->{win}->close ();
442 }
443  
444 sub plot_residuals {
445 my $self = shift () or die "no self";
446 my $font_charsize_opts = shift () or die "no font_charsize_opts";
447  
448 # Draw the residuals:
449 #
450 $self->get_limits ( 1 );
451 $self->set_window ( 1 );
452  
453 $self->points_draw_loop( 1 );
454 $self->{win}->label_axes (
455 $self->{x_label},
456 $self->{residuals_label},
457 $font_charsize_opts
458 );
459 }
460  
461 sub points_draw_loop {
462 my $self = shift () or die "no self";
463 my $bRes = shift () || 0;
464  
465 my $opt_num = 0;
466 my @syms = @{$self->{symbols}};
467 my @colors = split /,/, $self->{colors};
468  
469 my ( $y_key, $e_key ) = $bRes ? qw/res_data res_errs/
470 : qw/ y_data y_errs/;
471  
472 foreach my $brk ( sort keys %{$self->{pdls}{x_data}} ) {
473 foreach ( 0 .. scalar @{$self->{pdls}{$y_key}{$brk}} - 1 ) {
474  
475 $self->{opts}{pt_col}[$opt_num] =$colors[$opt_num % scalar @colors];
476 $self->{opts}{symbol}[$opt_num] =$syms[ $opt_num % scalar @syms ];
477 $self->{opts}{ln_col}[$opt_num] =$colors[$opt_num % scalar @colors];
478 $self->{opts}{ln_sty}[$opt_num] =1 + $opt_num % 5;
479  
480 my $pointsopt = { color => $self->{opts}{pt_col}[$opt_num],
481 symbol => $self->{opts}{symbol}[$opt_num],
482 linewidth => $self->{line_width} };
483 my $lineopt = { color => $self->{opts}{ln_col}[$opt_num],
484 linestyle => $self->{opts}{ln_sty}[$opt_num],
485 linewidth => $self->{line_width} };
486  
487 my @plot_data = ( $self->{pdls}{x_data}{$brk},
488 $self->{pdls}{$y_key}{$brk}[$_] );
489 my @errs_data;
490 if ( exists $self->{pdls}{$e_key} &&
491 defined $self->{pdls}{$e_key}{$brk}[$_] )
492 {
493 @errs_data = ( $self->{pdls}{$y_key}{$brk}[$_] -
494 $self->{pdls}{"${e_key}_lo"}{$brk}[$_],
495  
496 $self->{pdls}{"${e_key}_hi"}{$brk}[$_] -
497 $self->{pdls}{$y_key}{$brk}[$_] );
498 }
499  
500 $self->{win}->line ( @plot_data, $lineopt ) if !$self->{noline};
501 $self->{win}->points( @plot_data, $pointsopt) if !$self->{nopoints};
502 $self->{win}->errb ( @plot_data,
503 undef,undef,
504 @errs_data,
505 $lineopt )
506 if exists $self->{pdls}{$e_key} &&
507 defined $self->{pdls}{$e_key}{$brk}[$_];
508 ++$opt_num;
509 }
510 last if $bRes;
511 }
512 }
513  
514 sub make_pdls {
515 my $self = shift () or die "no self in make_pdls";
516  
517 # Do x, y, and y_err:
518 #
519 foreach my $brk ( sort keys %{$self->{points}}) {
520 $self->{pdls}{x_data}{$brk} = pdl $self->{points}{$brk}{x};
521  
522 foreach ( 0 .. scalar @{$self->{points}{$brk}{y}} - 1 ) {
523 push @{$self->{pdls}{y_data}{$brk}},
524 pdl $self->{points}{$brk}{y}[$_];
525 if ( defined $self->{y_err_col}[$_] ||
526 exists $self->{points}{$brk}{y_err}[$_] )
527 {
528 push @{$self->{pdls}{y_errs}{$brk}},
529 pdl $self->{points}{$brk}{y_err}[$_];
530 push @{$self->{pdls}{y_errs_hi}{$brk}},
531 $self->{pdls}{y_data}{$brk}[-1] +
532 $self->{pdls}{y_errs}{$brk}[-1];
533 push @{$self->{pdls}{y_errs_lo}{$brk}},
534 $self->{pdls}{y_data}{$brk}[-1] -
535 $self->{pdls}{y_errs}{$brk}[-1];
536 }
537 }
538 }
539 print Dumper $self->{pdls};
540 }
541  
542 sub make_pdl_residuals {
543 my $self = shift () or die "no self in make_pdl_residuals";
544  
545 die "No pdls exist in make_pdl_residuals"
546 if ! $self->pdls_loaded ();
547  
548 my $first_key = (keys %{$self->{pdls}{y_data}})[0];
549 my $nPdls = scalar @{$self->{pdls}{y_data}{$first_key}};
550  
551 # If there are two or more columns of y data, do the residuals between
552 # the first and the second columns. Otherwise, use the grouping
553 # column, and make the residuals be between the first group and
554 # the second group. If there is only one column of y-data, and
555 # the grouping flag hasn't been specified, we're in a world of
556 # hurt, and something's wrong in massage_args.
557 #
558 if ( $nPdls > 1 ) {
559 $self->make_pdl_residuals_multicolumn ();
560 }
561 # Group column version-- need to interpolate set 1 to get to set 2:
562 #
563 elsif ( defined $self->{group_col} ) {
564 $self->make_pdl_residuals_grouped ();
565 }
566 else {
567 die "Panic in make_pdl_residuals.";
568 }
569 }
570  
571 sub make_pdl_residuals_multicolumn {
572 my $self = shift () or die "no self";
573  
574 foreach my $brk ( sort keys %{$self->{pdls}{y_data}} ) {
575 $self->{pdls}{res_data}{$brk} = [
576 $self->{pdls}{y_data}{$brk}[0] -
577 $self->{pdls}{y_data}{$brk}[1]
578 ];
579  
580 # If there are errors, add them in quadrature:
581 #
582 if ( defined $self->{pdls}{y_errs}{$brk}[0] &&
583 defined $self->{pdls}{y_errs}{$brk}[0] )
584 {
585 $self->{pdls}{res_errs}{$brk} = undef;
586 $self->{pdls}{res_errs}{$brk} = [
587 sqrt ( $self->{pdls}{y_errs}{$brk}[0]**2 +
588 $self->{pdls}{y_errs}{$brk}[1]**2 )
589 ];
590 $self->{pdls}{res_errs_lo}{$brk} = [
591 $self->{pdls}{res_data}{$brk}[0] -
592 $self->{pdls}{res_errs}{$brk}[0],
593 ];
594 $self->{pdls}{res_errs_hi}{$brk} = [
595 $self->{pdls}{res_data}{$brk}[0] +
596 $self->{pdls}{res_errs}{$brk}[0],
597 ];
598 }
599 }
600 }
601  
602 sub make_pdl_residuals_grouped {
603 my $self = shift () or die "no self";
604  
605 my @brk = ( sort keys %{$self->{points}} )[0..1];
606  
607 foreach ( 0 .. scalar @{$self->{pdls}{y_data}{$brk[0]}} - 1 ) {
608 $self->{pdls}{res_data}{$brk[0]}[$_] =
609 ( interpolate (
610 $self->{pdls}{x_data}{$brk[1]},
611 $self->{pdls}{x_data}{$brk[0]},
612 $self->{pdls}{y_data}{$brk[0]}[$_]
613 )
614 )[0] - $self->{pdls}{y_data}{$brk[1]}[$_];
615 if ( $self->{pdls}{y_errs} ) {
616 $self->{pdls}{res_errs}{$brk[0]}[$_] =
617 $self->{pdls}{y_errs}{$brk[0]}[$_];
618 $self->{pdls}{res_errs_lo}{$brk[0]}[$_] =
619 $self->{pdls}{y_data}{$brk[0]}[$_] -
620 $self->{pdls}{y_errs}{$brk[0]}[$_];
621 $self->{pdls}{res_errs_hi}{$brk[0]}[$_] =
622 $self->{pdls}{y_errs}{$brk[0]}[$_] +
623 $self->{pdls}{y_errs}{$brk[0]}[$_];
624 }
625 }
626 $self->{pdls}{res_data}{$brk[1]} = $self->{pdls}{res_data}{$brk[0]};
627 }
628  
629 sub setup_win {
630 my $self = shift () or die "no self in setup_win";
631  
632 $self->{win} =
633 PDL::Graphics::PGPLOT::Window->new (
634 Device => $self->{device},
635 WindowName => "plot created by $0",
636 AxisColor => 'black',
637 Color => 'black',
638 Font => $self->{font},
639 HardFont => $self->{font},
640 CharSize => $self->{char_size},
641 HardCH => $self->{char_size},
642 );
643 }
644  
645 sub set_window {
646 my $self = shift () or die "no self in set_window";
647 my $iWin = shift () || 0;
648  
649 my @env_pars = (
650 @{$self->{limits}{x}}{'lo','hi'},
651 @{$self->{limits}{y}}{'lo','hi'},
652 {
653 PlotPosition => $self->{PlotPosition}[$iWin],
654 Axis => [ 'BCNST', 'BCMSTV' ],
655 }
656 );
657  
658 if ( 0 == $iWin ) { # main window
659 $env_pars[-1]{Axis} = [ "BCST", "BCSTNV" ]
660 if $self->{residuals};
661  
662 if ( $self->{axis} ) {
663 $env_pars[-1]{Axis}[0] .= 'A';
664 $env_pars[-1]{Axis}[1] .= 'A';
665 }
666 }
667 else { # residual window
668 if ( $self->{residuals} ) {
669 if ( $self->{axis_residuals} ) {
670 $env_pars[-1]{Axis}[0] .= 'A';
671 $env_pars[-1]{Axis}[1] .= 'A';
672 }
673 }
674 }
675  
676 $env_pars[-1]{Axis}[0] .= 'L' if $self->{x_log};
677 $env_pars[-1]{Axis}[1] .= 'L' if $self->{y_log};
678  
679 $env_pars[-1]{Axis}[1] =~ s/M/N/
680 if ! $self->{residuals_pos};
681  
682 $self->{win}->env ( @env_pars );
683 }
684  
685 sub write_legend {
686 my $self = shift () or die "no self in write_legend";
687  
688 my @loc = ( $self->{limits}{x}{lo}, $self->{limits}{y}{hi} );
689  
690 my @deltas = ( $self->{limits}{x}{hi} - $self->{limits}{x}{lo},
691 $self->{limits}{y}{hi} - $self->{limits}{y}{lo} );
692  
693 @{$self->{legend_location}} = [ .1, -.1 ]
694 if ! $self->{legend_location};
695  
696 @loc = map { $loc[$_] + $self->{legend_location}[$_] * $deltas[$_] } 0..1;
697  
698 # Legend Usage:
699 #
700 # [ names ],
701 # x,y
702 # { option hash }
703 #
704 my $text = ( $self->{legend_text}
705 ? $self->{legend_text}
706 : $self->{derived_legend} );
707 $self->{win}->legend (
708 $text, @loc,
709 {
710 LineStyle => $self->{opts}{ln_sty},
711 Color => $self->{opts}{ln_col},
712 },
713 );
714 $self->{win}->legend (
715 $text, @loc,
716 {
717 Symbol => $self->{opts}{symbol},
718 LineStyle => $self->{opts}{ln_sty},
719 Color => $self->{opts}{ln_col},
720 LineWidth => [ 50, 50 ],
721 TextShift => 0,
722 Font => $self->{font},
723 HardFont => $self->{font},
724 CharSize => $self->{char_size},
725 HardCH => $self->{char_size},
726 Fraction => 0.5,
727 }
728 );
729 }
730  
731 sub logify_pdls {
732 my $self = shift () or die "no self";
733  
734 if ( $self->{x_log} ) {
735 foreach ( keys %{$self->{pdls}{x_data}} ) {
736 $self->{pdls}{x_data}{$_}->inplace->log10;
737 }
738 }
739  
740 if ( $self->{y_log} ) {
741 foreach my $y_type ( $self->all_ydata_names () ) {
742 foreach my $brk ( keys %{$self->{pdls}{$y_type}} ) {
743 foreach ( @{$self->{pdls}{$y_type}{$brk}} ) {
744 $_->inplace->log10;
745 }
746 }
747 }
748 }
749  
750 # Get the indices for finite-everywhere elements use only those
751 # elements from ALL piddles:
752 #
753 my $fin_indx = $self->get_finite_indices ();
754  
755 $self->{pdls}{x_data}{$_} = $self->{pdls}{x_data}{$_}->( $fin_indx )
756 foreach keys %{$self->{pdls}{x_data}};
757  
758 foreach my $y_type ( $self->all_ydata_names () ) {
759 foreach my $brk ( keys %{$self->{pdls}{$y_type}} ) {
760 foreach ( @{$self->{pdls}{$y_type}{$brk}} ) {
761 $_ = $_->( $fin_indx );
762 }
763 }
764 }
765 }
766  
767 sub all_ydata_names {
768 my $self = shift () or die "no self";
769 return grep { $_ ne 'x_data' } keys %{$self->{pdls}};
770 }
771  
772 sub get_finite_indices {
773 my $self = shift () or die "no self";
774  
775 # Make a mask that includes only elements where the piddle elements are
776 # finite in all piddles.
777 #
778 my $size = $self->first_x_pdl_size ();
779 my $finite_mask = ones ( $size );
780  
781 $finite_mask = $finite_mask & $self->{pdls}{x_data}{$_}->isfinite
782 foreach keys %{$self->{pdls}{x_data}};
783  
784 foreach my $y_type ( $self->all_ydata_names () ) {
785 foreach my $brk ( keys %{$self->{pdls}{$y_type}} ) {
786 foreach ( @{$self->{pdls}{$y_type}{$brk}} ) {
787 $finite_mask = $finite_mask & $_->isfinite;
788 }
789 }
790 }
791 my $inds = $finite_mask->which;
792  
793 print STDERR "Negative data excluded after logarithm\n"
794 if $self->{verbose} >= 0 &&
795 $size != $inds->nelem &&
796 ($self->{y_log} || $self->{x_log});
797 die "None of the data is finite after logarithm operation-- quitting."
798 if $inds->nelem < 1;
799  
800 return $inds;
801 }
802  
803 sub first_x_pdl_size {
804 my $self = shift () or die "no self";
805 my $key = (keys %{$self->{pdls}{x_data}})[0];
806 return $self->{pdls}{x_data}{$key}->nelem;
807 }
808  
809 sub has_errs {
810 my $self = shift () or die "no self";
811 my ( $brk, $e_key ) = @_;
812  
813 return exists $self->{pdls}{$e_key} &&
814 defined $self->{pdls}{$e_key}{$brk}[$_];
815 }
816  
817 sub get_ylimits {
818 my $self = shift () or die "no self";
819 my ( $y_key, $e_key, $brk, $i ) = @_;
820  
821 my %y_data = (
822 lo => $self->{pdls}{$y_key}{$brk}[$i]->copy,
823 hi => $self->{pdls}{$y_key}{$brk}[$i]->copy,
824 );
825 if ( $self->has_errs ( $brk, $e_key ) ) {
826 $y_data{lo} = $self->{pdls}{"${e_key}_lo"}{$brk}[$i]->copy;
827 $y_data{hi} = $self->{pdls}{"${e_key}_hi"}{$brk}[$i]->copy;
828 }
829  
830 return ( $y_data{lo}->min, $y_data{hi}->max )
831 if not defined $self->{x_range};
832  
833 return (
834 where (
835 $y_data{lo},
836 ( $self->{pdls}{x_data}{$brk} > $self->{x_range}[0] ) &
837 ( $self->{pdls}{x_data}{$brk} < $self->{x_range}[1] )
838 )->min(),
839 where (
840 $y_data{hi},
841 ( $self->{pdls}{x_data}{$brk} > $self->{x_range}[0] ) &
842 ( $self->{pdls}{x_data}{$brk} < $self->{x_range}[1] )
843 )->max()
844 );
845 }
846  
847 sub limits_exist {
848 my $self = shift () or die "no self";
849 my $axis = shift () or die "no axis in limits_exist";
850 return exists $self->{limits}{$axis};
851 }
852  
853 sub try_new_limits {
854 my $self = shift () or die "no self";
855 my ( $axis, @vals ) = @_;
856  
857 foreach ( @vals ) {
858 if ( ! $self->limits_exist ( $axis ) ) {
859 $self->{limits}{$axis}{lo} = $_;
860 $self->{limits}{$axis}{hi} = $_;
861 }
862 else {
863 $self->{limits}{$axis}{lo} = $_ if $_ < $self->{limits}{$axis}{lo};
864 $self->{limits}{$axis}{hi} = $_ if $_ > $self->{limits}{$axis}{hi};
865 }
866 }
867 }
868  
869 sub get_limits {
870 my $self = shift () or die "no self";
871 my $bRes = shift () || 0;
872  
873 $self->{limits} = undef if 0 != $bRes;
874 # Format of limits data member: $self->{limits}{x,y}{lo,hi}
875 #
876 # Get data extremes, and make them the limits:
877 #
878 my @brks = sort keys %{$self->{pdls}{x_data}};
879 @brks = @brks[0,1]
880 if $bRes && $self->{group_col};
881 my ( $y_key, $e_key ) = $bRes ? qw/res_data res_errs/
882 : qw/ y_data y_errs/;
883  
884 foreach my $brk ( @brks ) {
885 $self->try_new_limits (
886 'x',
887 $self->{pdls}{x_data}{$brk}->min,
888 $self->{pdls}{x_data}{$brk}->max,
889 );
890  
891 foreach ( 0 .. scalar @{$self->{pdls}{$y_key}{$brk}} - 1 ) {
892 $self->try_new_limits (
893 'y',
894 $self->get_ylimits ( $y_key, $e_key, $brk, $_ )
895 );
896 }
897 }
898 $self->pad_limits ();
899 }
900  
901 sub limits {
902 my $self = shift () or die "no self";
903 return $self->{limits};
904 }
905  
906 sub limits_width {
907 my ( $self, @axes ) = @_;
908 return map {
909 die "No limits for $_"
910 if ! exists $self->{limits}{$_};
911 $self->{limits}{$_}{hi} -
912 $self->{limits}{$_}{lo}
913 } @axes;
914 }
915  
916 sub pad_limits {
917 my $self = shift () or die "no self";
918  
919 my ( $dx, $dy ) = map { $_/10.0 } $self->limits_width ( 'x', 'y' );
920  
921 # Pad limits to create pleasing margins:
922 #
923 $self->{limits}{x}{lo} -= $dx;
924 $self->{limits}{x}{hi} += $dx;
925 $self->{limits}{y}{lo} -= $dy;
926 $self->{limits}{y}{hi} += $dy;
927  
928 # Override if user-specified limits exist:
929 #
930 if ( defined $self->{x_range} ) {
931 $self->{limits}{x}{lo} = $self->{x_range}[0];
932 $self->{limits}{x}{hi} = $self->{x_range}[1];
933 }
934 if ( defined $self->{y_range} ) {
935 $self->{limits}{y}{lo} = $self->{y_range}[0];
936 $self->{limits}{y}{hi} = $self->{y_range}[1];
937 }
938  
939 # Give a 2-unit range is either axis's range is zero:
940 #
941 my $epsilon = 1.0e-9;
942 if ( not defined $self->{x_range} &&
943 $self->limits_width ( 'x' ) < $epsilon )
944 {
945 $self->{limits}{x}{lo} -= .1;
946 $self->{limits}{x}{hi} += .1;
947 }
948 if ( not defined $self->{y_range} &&
949 $self->limits_width ( 'y' ) < $epsilon )
950 {
951 $self->{limits}{y}{lo} -= .1;
952 $self->{limits}{y}{hi} += .1;
953 }
954 }
955  
956  
957 sub massage_args {
958 my $self = shift () or die "no self";
959  
960 # Package single y_data values in an array to allow y_data => $y_pdl
961 # syntax instead of cumbersome y_data => [ $y_pdl ]
962 #
963 $self->{y_data} = [ $self->{y_data} ]
964 if $self->single_y_data ();
965  
966 $self->setup_RDB_split () if not defined $self->{split};
967 $self->read_nonfile_points () if defined $self->{x_data};
968  
969 # Split data params on commas if they are defined:
970 #
971 foreach ( qw/y_col y_err_col x_range y_range legend_location legend_text/ ) {
972 $self->{$_} = [ split /,/, $self->{$_} ]
973 if exists $self->{$_} &&
974 defined $self->{$_};
975 }
976  
977 die "Must have two y data columns or a group_col column to use residuals"
978 if $self->{residuals} &&
979 ( grep { defined $self->{$_} &&
980 scalar @{$self->{$_}} < 2 } qw/y y_data/ )
981 && not defined $self->{group_col};
982  
983 $self->deduce_x_y_names ();
984 $self->set_plot_position ();
985  
986 if ( $self->{filename} ) {
987 $self->setvars ( x_label => "$self->{x_col}" )
988 if '' eq $self->{x_label};
989 $self->setvars ( y_label => "@{$self->{y_col}}" )
990 if '' eq $self->{y_label};
991 }
992 else {
993 die "The x_col and y_col parameters are used only with the filename " .
994 "parameter. See the docs for examples."
995 if $self->{x_col} || $self->{y_col};
996 }
997  
998 $self->{residuals_label} = "deltas"
999 if '' eq $self->{residuals_label};
1000  
1001 $self->setup_title ();
1002 $self->setup_subtitle ();
1003  
1004 print Dumper $self
1005 if $self->{verbose} > 3;
1006 }
1007  
1008 sub single_y_data {
1009 my $self = shift () or die "no self";
1010  
1011 return 'PDL' eq ref $self->{y_data} ||
1012 'ARRAY' eq ref $self->{y_data} &&
1013 '' eq ref $self->{y_data}[0];
1014 }
1015  
1016 sub get_nonfile_points_datatype {
1017 my $self = shift () or die "no self";
1018  
1019 # Determine if given data are array refs or pdls, and make sure they're
1020 # all the same kind:
1021 #
1022 my %refs;
1023 $refs{ ref $_ } = 1 foreach $self->{x_data}, @{$self->{y_data}};
1024 my @datatypes = keys %refs;
1025  
1026 die "x_data, y_data must all be the same datatype"
1027 if 1 != scalar @datatypes;
1028 die "x_data, y_data items must be either pdls or array refs"
1029 if 'ARRAY' ne $datatypes[0] && 'PDL' ne $datatypes[0];
1030  
1031 return $datatypes[0];
1032 }
1033  
1034 sub read_nonfile_points {
1035 my $self = shift () or die "no self";
1036  
1037 # Read data given with the x_data, y_data[, y_err_data] arguments.
1038 #
1039 die "no x_data given. shouldn't be here!"
1040 if not exists $self->{x_data};
1041 die "if x_data is given, y_data must be given"
1042 if not exists $self->{y_data};
1043  
1044 # TODO: add support for multi-group data input, which will have
1045 # to be in the form: x_data => [\@x1,\@x2], y_data => [\@y1,\@y2]
1046 #
1047 my $brk = $self->{only};
1048  
1049 my $datatype = $self->get_nonfile_points_datatype ();
1050  
1051 if ( 'ARRAY' eq $datatype ) {
1052 $self->{points}{$brk}{x} = [ @{$self->{x_data}} ];
1053 foreach (0 .. scalar @{$self->{y_data}} - 1) {
1054 $self->{points}{$brk}{y}[$_] = [ @{$self->{y_data}[$_]} ];
1055 $self->{points}{$brk}{y_err}[$_] = [ @{$self->{y_err_data}[$_]} ]
1056 if defined $self->{y_err_data};
1057 }
1058 }
1059 elsif ( 'PDL' eq $datatype ) {
1060 $self->{pdls}{x_data}{$brk} = $self->{x_data}->copy;
1061 foreach (0 .. scalar @{$self->{y_data}} - 1) {
1062 $self->{pdls}{y_data}{$brk}[$_] = $self->{y_data}[$_]->copy;
1063 if ( defined $self->{y_err_data} &&
1064 defined $self->{y_err_data}[$_] )
1065 {
1066 $self->{pdls}{y_errs}{$brk}[$_] = $self->{y_err_data}[$_]->copy;
1067 $self->{pdls}{y_errs_hi}{$brk}[$_] =
1068 $self->{pdls}{y_data}{$brk}[$_] +
1069 $self->{pdls}{y_errs}{$brk}[$_];
1070 $self->{pdls}{y_errs_lo}{$brk}[$_] =
1071 $self->{pdls}{y_data}{$brk}[$_] -
1072 $self->{pdls}{y_errs}{$brk}[$_];
1073 }
1074 }
1075 }
1076 else {
1077 die "in read_nonfile_points: this should NEVER be reached";
1078 }
1079 $self->{derived_legend} = ( defined $self->{y_col} )
1080 ? [ split /,/, $self->{y_col} ]
1081 : [ map { "y$_" }
1082 0..scalar @{$self->{y_data}} - 1 ];
1083 }
1084  
1085 sub set_plot_position {
1086 my $self = shift () or die "no self";
1087  
1088 my $height = $self->{residuals_size} * ( 0.90 - 0.10 ) + 0.10;
1089 $self->{PlotPosition} = [ [ 0.1, 0.9, 0.1, 0.9 ] ];
1090  
1091 if ( defined $self->{residuals} ) {
1092 $self->{PlotPosition} = [
1093 [ 0.1, 0.9, $height, 0.9 ],
1094 [ 0.1, 0.9, 0.1, $height ]
1095 ];
1096 }
1097 }
1098  
1099 sub setup_title {
1100 my $self = shift () or die "no self";
1101  
1102 if ( exists $self->{x_data} &&
1103 exists $self->{y_data} &&
1104 not exists $self->{x_col} &&
1105 not exists $self->{y_col} )
1106 {
1107 $self->{x_col} = "x";
1108 $self->{y_col} = [ map { "y$_" }
1109 0 .. scalar @{$self->{y_data}} - 1 ];
1110 }
1111 if ( '' eq $self->{title} ) {
1112 $self->{title} = "@{$self->{y_col}} vs $self->{x_col}";
1113 $self->{title} .= " grouped by $self->{group_col}"
1114 if $self->{group_col};
1115 }
1116 }
1117  
1118 sub setup_subtitle {
1119 my $self = shift () or die "no self";
1120 if ( defined $self->{subtitle} ) {
1121 eval { require PGPLOT };
1122 if ( my $err = $@ ) {
1123 $self->{title} .= $self->{subtitle};
1124 undef $self->{subtitle};
1125 print STDERR "$err: PGPLOT.pm not found. PGPLOT.pm is ",
1126 "required for subtitles. The subtitle text ",
1127 "will be appended to title.\n";
1128 }
1129 }
1130 }
1131  
1132 # Determine if user wants to use RDB.pm. Include it if it exists,
1133 # otherwise, set split to '\t', warn user, and hope for the best.
1134 #
1135 sub setup_RDB_split {
1136 my $self = shift () or die "no self";
1137 if ( not defined $self->{split} ) {
1138 eval { require RDB };
1139 if ( $@ ) {
1140 $self->{split} = "\t";
1141 print STDERR "RDB.pm not found, splitting on tabs.\n"
1142 if $self->{verbose} > 0;
1143 }
1144 }
1145 }
1146  
1147 sub deduce_x_y_names {
1148 my $self = shift () or die "no self";
1149  
1150 # Return if there isn't a filename (passing data in directly) or if the x
1151 # and y names are already defined:
1152 #
1153 return if !$self->{filename} ||
1154 defined $self->{x_col} && defined $self->{y_col} ||
1155 defined $self->{x_col} && defined $self->{y_col};
1156  
1157 # If the data is coming from STDIN, write it to a file to grab the
1158 # header. There's GOT to be a better way to do this.
1159 #
1160 my $tmpfile = '.plot.hdr';
1161 if ( 'stdin' eq $self->{filename} ) {
1162 open my $write_fh, '>', $tmpfile;
1163 print $write_fh $_ while <>;
1164 close $write_fh;
1165 $self->{filename} = $tmpfile;
1166 }
1167  
1168 # Read in the first non-commented line as $definition_line.
1169 #
1170 open my $fh, $self->{filename};
1171 my $definition_line = '#';
1172 chomp ( $definition_line = <$fh> )
1173 while $definition_line =~ /^\s*#/;
1174 close $fh;
1175  
1176 # Parse out the column names, Non-RDB file case:
1177 #
1178 if ( defined $self->{split} ) {
1179 $self->{x_col} = ( split /$self->{split}/, $definition_line )[0];
1180 $self->{y_col} = [ ( split /$self->{split}/, $definition_line )[1] ];
1181 }
1182 # Parse out the column names, RDB file case:
1183 #
1184 else {
1185 $self->{x_col} = ( split /\t/, $definition_line )[0];
1186 $self->{y_col} = [ ( split /\t/, $definition_line )[1] ];
1187 }
1188 }
1189  
1190 sub help {
1191 my ( $verbose ) = @_;
1192 require IO::Page;
1193 require Pod::Usage;
1194 Pod::Usage::pod2usage ( { -exitval => 0, -verbose => $verbose } );
1195 }
1196  
1197 1;
1198  
1199 =pod
1200  
1201 =head1 NAME
1202  
1203 Chart::Scientific - Generate simple 2-D scientific plots with logging, errbars, etc.
1204  
1205 =head1 SYNOPSIS
1206  
1207 =head2 Procedural interface
1208  
1209 use Chart::Scientific qw/make_plot/;
1210 make_plot ( x_data => \@x_values, y_data => \@yvalues );
1211  
1212 The subroutine make_plot creates a Chart::Scientific object
1213 passing along every argument it was given. See B<OPTIONS> below
1214 for a full list of allowed arguments.
1215  
1216 =head2 Object Oriented interface
1217  
1218 Plot data from two arrays:
1219  
1220 use Chart::Scientific;
1221 my $plt = Chart::Scientific->new (
1222 x_data => \@x_values,
1223 y_data => \@y_values,
1224 );
1225 $plt->plot ();
1226  
1227 or piddles:
1228  
1229 use Chart::Scientific;
1230 my $plt = Chart::Scientific->new (
1231 x_data => $x_pdl,
1232 y_data => $y_pdl,
1233 );
1234 $plt->plot ();
1235  
1236 Plot data from an arbitrarily-delimitted file (the data in columns "vel" and
1237 "acc" vs the data in the column "time", with errorbars from the columns
1238 "vel_err" and "acc_err"):
1239  
1240 my $plt = Chart::Scientific->new (
1241 filename => 'data.tab-separated',
1242 split => '\t',
1243 x_col => 'time',
1244 y_col => 'vel,acc',
1245 err_col => 'vel_err,acc_err',
1246 x_label => "time",
1247 y_label => "velocity and acceleration",
1248 );
1249 $plt->plot ();
1250  
1251 Plot data in arrays:
1252  
1253 my $plt = Chart::Scientific->new (
1254 x_data => \@height,
1255 y_data => [ \@weight, \@body_mass_index ],
1256 );
1257 $plt->plot ();
1258  
1259 Plot data in pdls:
1260  
1261 my $plt = Chart::Scientific->new (
1262 x_data => $pdl_x,
1263 y_data => [ $pdl_y1, $pdl_y2 ],
1264 );
1265 $plt->plot ();
1266  
1267 Plot the above data to a file:
1268  
1269 my $plt = Chart::Scientific->new (
1270 x_data => $pdl_x,
1271 y_data => [ $pdl_y1, $pdl_y2 ],
1272 device => 'myplot.ps/cps',
1273 );
1274 $plt->plot ();
1275  
1276 Generate multiple plots with the same object:
1277  
1278 my @x1 = 10..19;
1279 my @y1 = 20..29;
1280 my @y2 = 50..59;
1281  
1282 my $plt = Chart::Scientific->new (
1283 x_data => \@x1,
1284 y_data => \@y1,
1285 x_label => "test x",
1286 y_label => "test y",
1287 );
1288 $plt->setvars ( title => 'testa', device => '1/xs' );
1289 $plt->plot ();
1290  
1291 $plt->setvars ( title => 'testb', device => '2/xs' );
1292 $plt->plot ();
1293  
1294 =head1 DESCRIPTION
1295  
1296 B<Chart::Scientific> is a simple PDL-based plotter. 2-D plots can be easily
1297 made from data in an array or PDL, or from a file containing columns of data.
1298 The columns can be delimited with any character(s) or regular expression.
1299  
1300 There are many plotting options:
1301  
1302 Graph axes can be logged (non-finite data, i.e. negative data that is logged,
1303 will be ignored, with a warning), error bars can be plotted, the axes can be
1304 displayed, residuals can be plotted, font, line thickness, character size, plot
1305 point style, and colors can all be adjusted, line or point plotting, or both,
1306 can be supressed. Labels can be written on either axis, and the x and y ranges
1307 can be specified.
1308  
1309 =head1 PUBLIC METHODS
1310  
1311 =head2 new ( %options | option-values list )
1312  
1313 Creates a new Chart::Scientific object, and intializes it with the given
1314 options. The options defining plot data (I<i.e.> {filename,x_col,y_col} or
1315 {x_data,y_data}) B<must> be specified in the constructor. All other options
1316 can be given either as a hash or a simple list of option => value pairs. Legal
1317 options are given in the B<OPTIONS> section.
1318  
1319 =head2 setvars ( %options | option-values list )
1320  
1321 Sets new options for a Chart::Scientific instance or overwrites existing
1322 options. The input format is identical to the Constructor's. See the
1323 B<OPTIONS> section for a complete list of options.
1324  
1325 =head2 plot
1326  
1327 Create the plot. The plot is written the the existing device,
1328 whether it is a window or a file.
1329  
1330 =head2 getvars ( option list )
1331  
1332 Returns a list of the values of the options listed in the argument.
1333  
1334 =head2 restore_defaults
1335  
1336 Restore the Chart::Scientific object to the default settings.
1337  
1338 =head2 clear
1339  
1340 Completely clear the Chart::Scientific object, setting it equal to
1341 an empty hashref. Arguably less useful than restore_defaults.
1342  
1343 =head1 OPTIONS
1344  
1345 =over 8
1346  
1347 =item I<Data from arrays or piddles>
1348  
1349 Plotting data may come from either arrays, piddles, or a file, but
1350 all data must be of the same type.
1351
1352 =over 8
1353  
1354 =item B<x_data>
1355  
1356 An array or piddle that contains the x-data for this plot.
1357 The x_data, y_data, and y_err_data specified must be of the same datatype,
1358 arrays or piddles.
1359  
1360 =item B<y_data>
1361  
1362 An array or piddle that contains the y-data for this plot.
1363 Multiple sets of y-data to plot against the same x-data can be specified
1364 with an array of arrays or an array of piddles.
1365 The x_data, y_data, and y_err_data specified must be of the same datatype,
1366 arrays or piddles.
1367  
1368 =item B<y_err_data>
1369  
1370 An array or piddle that contains the error bars for the y-data.
1371  
1372 There must be the same number of y_err_data as there are y_data, e.g.,
1373  
1374 y_data => [ \@y_data1, \@y_data2 ]
1375  
1376 cannot be acompanied by:
1377  
1378 y_err_data => [ \@y_err ]
1379  
1380 but
1381  
1382 y_err_data => [ \@y_err1, \@y_err2 ]
1383  
1384 would be allowed.
1385  
1386 The x_data, y_data, and y_err_data specified must be of the same datatype,
1387 arrays or piddles.
1388  
1389 =back
1390  
1391 =item I<Options concerning data from a file>
1392  
1393 =over 8
1394  
1395 =item B<filename>
1396  
1397 The name of the file to read data from. Specify 'stdin' to read from STDIN
1398 (using the constructor or setvars). If B<split> (see below) is not specified,
1399 the file is assumed to be an RDB file (see
1400 http://hea-www.harvard.edu/MST/simul/software/docs/rdb.html). An kludgy
1401 attempt to read the file if the RDB.pm module is not on the local system will
1402 be made: RDB comments (leading '#'s) are stripped, the column definition line
1403 is ignored, and the body of the file is split into columns on tabs.
1404  
1405 =item B<split>
1406  
1407 Used for non-RDB files, B<split> specifies which character(s) (or regex) to
1408 split the data from the file on. For a comma-delimitted file, --split ','
1409 would be the correct usage, or --split '\|' for a pipe-delimitted file (the
1410 pipe is a special char in perl regexes, so we must escape it). If the file
1411 specified by B<filename> is an RDB file, this switch should not be used.
1412  
1413 The first line of a file must list the names of the columns, delimmited
1414 identically.
1415  
1416 =item B<x_col>
1417  
1418 The name of the x column.
1419  
1420 =item B<y_col>
1421  
1422 A comma-separated list of the name(s) of the y column(s).
1423  
1424 =item B<y_err_col>
1425  
1426 A comma-separated list of the name(s) of the y errorbar column(s).
1427  
1428 =item B<group_col>
1429  
1430 (Optional) The name of the grouping column. The grouping column separates
1431 a x_col or y_col into different datasets, based on the value of the grouping
1432 column in each row. For example, if x_col => x, y__col => y, group => g:
1433  
1434 x y g
1435 1 2 dataset1
1436 2 3 dataset1
1437 3 4 dataset1
1438 4 5 dataset1
1439 5 12 dataset2
1440 6 13 dataset2
1441 7 14 dataset2
1442 8 15 dataset2
1443  
1444 There would be two groups, dataset1 containing x = (1,2,3,4) and y = (2,3,4,5),
1445 and dataset2 containing x = (5,6,7,8) and y = (12,13,14,15). The two groups
1446 of data would be plotted as separate lines on the plot.
1447  
1448 =back
1449  
1450 =item I<Plot limit Options>
1451  
1452 =over 8
1453  
1454 =item B<x_range >
1455  
1456 Specify a comma-separated non-default range for the X values. Example: an
1457 x_range value of '-5,5' will plot the data from x=-5 to x=5. If the I<x_log>
1458 flag is on, the x_range values must be specified in powers of ten. E.G. -x_log
1459 -x -1,2 will plot the data on a logged X range from 0.1 to 100.
1460  
1461 =item B<y_range>
1462  
1463 Specify a comma-separated non-default range for the X values. Example: a
1464 y_range value of '-5,5' will plot the data from y=-5 to y=5. If the I<y_log>
1465 flag is on, the y_range values must be specified in powers of ten. E.G. -y_log
1466 -y -1,2 will plot the data on a logged Y range from 0.1 to 100.
1467  
1468 =back
1469  
1470 =item I<Output device options>
1471  
1472 =over 8
1473  
1474 =item B<device>
1475  
1476 The PGPLOT plotting device. Use "filename.ps/cps" to print to a postscript
1477 file, or "filename.png/png" to print to a PNG file. All plotting devices
1478 supported by PDL::Graphics::PGPLOT are supported. Defaults to '/xs', which
1479 prints to a new window.
1480  
1481 =back
1482  
1483 =item I<Plot type options>
1484  
1485 =over 8
1486  
1487 =item B<nopoints>
1488  
1489 Set to true to supress points plotting. Points are plotted by
1490 default.
1491  
1492 =item B<noline>
1493  
1494 Set to true to supress line drawing. Lines are drawn by default.
1495  
1496 =back
1497  
1498 =item I<Logarithmic plot options>
1499  
1500 =over 8
1501  
1502 =item B<x_log>
1503  
1504 Set to true to create a plot with a logged x axis.
1505  
1506 =item B<y_log>
1507  
1508 Set to true to create a plot with a logged y axis.
1509  
1510 =back
1511  
1512 =item I<Residual options>
1513  
1514 =over 8
1515  
1516 =item B<residuals>
1517  
1518 If true, residuals will be calculated and drawn in a second pane. The
1519 residuals will be between the first two specified y-values (this needs an
1520 upgrade). The default is false.
1521  
1522 For plots that pull two or more y-data sets from the same rows (i.e., no
1523 group_col column), the residuals are the difference of the first two specified
1524 y-data columns. For plots that use a group_col column, the residuals are
1525 the interpolated differences between the first and the second group_col-column
1526 sets of y-data.
1527  
1528 =item B<residuals_size>
1529  
1530 The fraction of the plotting area that the residuals occupy.
1531 The default is 0.25, and the range is 0.0 to 1.0.
1532  
1533 =back
1534  
1535 =item I<Legend options>
1536  
1537 =over 8
1538  
1539 =item B<nolegend>
1540  
1541 Setting this to a true value will suppress legend drawing. The
1542 default is 0.
1543  
1544 =item B<legend_location>
1545  
1546 A comma-separated list that to specify a location for the plot's
1547 legend. The default is .02,-.05. The coordates are in the range
1548 [0-1] for x, and [0,-1] for y, with the origin in the upper-left
1549 corner of the plot.
1550  
1551 =item B<legend_text>
1552  
1553 A comma-separated list, with one item to specify the text for each
1554 set of dependent data. The list must be given in in the same order
1555 as the data sets are given.
1556  
1557 =back
1558  
1559 =item I<Labelling options>
1560  
1561 =over 8
1562  
1563 =item B<title>
1564  
1565 A string to specify the title of the plot.
1566  
1567 =item B<subtitle>
1568  
1569 A string to specify the subtitle of the plot. If the PGPLOT.pm
1570 module is not on the system, the subtitle will be appended to the
1571 title.
1572  
1573 =item B<x_label>
1574  
1575 A string to specify the label for the x axis.
1576  
1577 =item B<y_label>
1578  
1579 A string to specify the label for the y axis.
1580  
1581 =item B<residuals_label>
1582  
1583 A string to specify the label for the residuals. The default is
1584 "deltas".
1585  
1586 =item B<residuals_pos>
1587  
1588 Setting this to a true value will move the numbering on the
1589 residuals plot from the left to the right side of the pane.
1590  
1591 =back
1592  
1593 =item I<Formatting options>
1594  
1595 =over 8
1596  
1597 =item B<line_width>
1598  
1599 The PGPlot line width. The default is 2.
1600  
1601 =item B<char_size>
1602  
1603 The PGPlot character size. The default is 1.
1604  
1605 =item B<colors>
1606  
1607 A comma-separated string that specifies the color set to use for
1608 drawing points and lines. For example, if 'red,blue,black' is the
1609 argument, the first set of points will be drawn red, the second
1610 blue, the third black, and then the fourth will be drawn red agan.
1611 The default is 'black,red,green,blue,yellow,cyan,magenta,gray'.
1612  
1613 =item B<font>
1614  
1615 A PGPlot font integer. The default is 1, and the range is 1-4.
1616  
1617 =item B<symbols>
1618  
1619 An anonymous array of PGPLOT symbol types, which specify the symbol set to use
1620 for drawing points and lines. For example, if [0,3,4] is the argument, the
1621 first set of points will be drawn with symbol 0, the second with symbol 3, and
1622 the third with symbol 4. The fourth set of points will be drawn with symbol 0,
1623 and so forth. The default is [ 3, 0, 5, 4, 6..99 ].
1624  
1625 =back
1626  
1627 =item I<Axis drawing options>
1628  
1629 =over 8
1630  
1631 =item B<axis>
1632  
1633 Setting this to true will draw the x=0 and y=0 axes on the main
1634 plotting pane.
1635  
1636 =item B<axis_residuals>
1637  
1638 Setting this to true will draw the x=0 and y=0 axes on the residuals plotting
1639 pane.
1640  
1641 =back
1642  
1643 =item I<Help and verbosity options>
1644  
1645 =over 8
1646  
1647 =item B<help>
1648  
1649 Set this to true to print a short help message and exit.
1650  
1651 =item B<usage>
1652  
1653 Set this to true to print a lengthy help message and exit.
1654  
1655 =item B<defaults>
1656  
1657 Set this to true to print default values of the arguments and exit.
1658  
1659 =item B<verbose>
1660  
1661 A verbose setting of 0 results in nearly silent operation. -1 suppresses all
1662 nonfatal output. The range is -1 to 4, with increasing verbosity at each
1663 level.
1664  
1665 =back
1666  
1667 =back
1668  
1669 =head1 SEE ALSO
1670  
1671 PDL, especially PDL::Graphics::PGPLOT, and PGPLOT.pm.
1672  
1673 =head1 LICENSE
1674  
1675 This software is released under the GNU General Public License. You
1676 may find a copy at
1677  
1678 http://www.fsf.org/copyleft/gpl.html
1679  
1680 =head1 AUTHOR
1681  
1682 Kester Allen (kester@gmail.com)
1683  
1684 =cut