PDL/Graphics/PGPLOT.pm untested

filter: Statementstmt 0.0% 0 / 167 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 310 CC 42 SCAR 31.5
CC
42
Cov
0%
new_window
72.0
close_window
42.0
_get_windownumber
42.0
CRAP 23.3
1 # Graphics functions for the PDL module, this module
2 # requires the PGPLOT module be previously installed.
3 # PGPLOT functions are also made available to the caller.
4  
5 =head1 NAME
6  
7 PDL::Graphics::PGPLOT - PGPLOT enhanced interface for PDL
8  
9 =head1 SYNOPSIS
10  
11 pdl> $x = pdl [1..100]
12 pdl> $y = sqrt($x)
13 pdl> line $y
14 pdl> hold
15 Graphics on HOLD
16 pdl> $c = sin($x/10)*2 + 4
17 pdl> line $c
18  
19 =head1 DESCRIPTION
20  
21 C<PDL::Graphics::PGPLOT> is a convenience interface to the PGPLOT commands,
22 implemented using the object oriented PGPLOT plotting package in
23 L<PDL::Graphics::PGPLOT::Window>. See the documentation for that package
24 for in-depth information about the usage of these commands and the options
25 they accept.
26  
27 The list of currently availably commands:
28  
29 imag - Display an image (uses pgimag()/pggray() as appropriate)
30 im - Shorthand to display an image with aspect ratio of 1
31 fits_imag - Display a FITS image with appropriate transforms & labels
32 cont - Display image as contour map
33 fits_cont - Display a FITS image in scientific coordinates as a contour map
34 vect - Display 2 images as a vector field
35 fits_vect - Display 2 FITS images in sci. coordinates as a vector field
36 ctab - Load an image colour table
37 ctab_info - Get information about currently loaded colour table
38 line - Plot vector as connected points
39 points - Plot vector as points
40 errb - Plot error bars
41 bin - Plot vector as histogram (e.g. bin(hist($data)) )
42 hi2d - Plot image as 2d histogram (not very good IMHO...)
43 poly - Draw a polygon
44 text - Write text in the plot area
45 label_axes - Print axis titles
46 legend - Create a legend with different texts, linestyles etc.
47 cursor - Interactively read cursor positions.
48 circle - Draw a circle
49 ellipse - Draw an ellipse.
50  
51 Device manipulation commands:
52  
53 hold - Hold current plot window range - allows overlays etc.
54 release - Release back to autoscaling of new plot window for each
55 command
56 rel - short alias for 'release'
57 env - Define a plot window, put on 'hold'
58 dev - Explicitly set a new PGPLOT graphics device
59 new_window - Create a new plot window (use of dev is recommended)
60 focus_window - Change focus to a new window
61 window_list - Get a list of currently existing plot windows
62 close_window - Close an open window
63  
64  
65 =head1 FUNCTIONS
66  
67 The following is a list of the functions that are private to this package,
68 for the other functions please read the L<PDL::Graphics::PGPLOT::Window>
69 documentation.
70  
71 =head2 dev
72  
73 =for ref
74  
75 Open PGPLOT graphics device
76  
77 =for usage
78  
79 Usage: dev $device, [$nx,$ny, $opt];
80  
81 C<$device> is a PGPLOT graphics device such as "/xserve" or "/ps",
82 if omitted defaults to last used device (or value of env
83 var C<PGPLOT_DEV> if first time).
84 C<$nx>, C<$ny> specify sub-panelling. The function returns the id of
85 the newly created window - this can subsequently be used as argument to
86 C<focus_window> to select the window.
87  
88 The result of this command can be modified using options. The options
89 recognised are the same as for C<new_window> - which primarily and in
90 addition it is possible to set the default values for a window that are
91 defined in L<PDL::Graphics::PGPLOTOptions>, see this for details but see below for
92 a synopsis.
93  
94 In addition C<dev> recognises the option C<NewWindow> which allows the
95 user to specify that a C<dev> command is to create a new window rather than
96 closing the previous. This allows a large number of output destinations to
97 be open at the same time, which occasionally can be convenient.
98  
99 Here is a quick summary of the most useful additional options that can
100 be given:
101  
102 =over
103  
104 =item Device
105  
106 Alternative to C<$device>.
107  
108 =item AspectRatio
109  
110 The aspect ratio of the output window
111  
112 =item WindowWidth
113  
114 The width of the plot window in inches
115  
116 =item AxisColour
117  
118 The axis colour to be used as default for plots in this window. In the same
119 way it is possible to set the default character size (C<CharSize>) and axis
120 and box styles. See L<PDL::Graphics::PGPLOTOptions> for details.
121  
122 =item WindowName
123  
124 The name of a window. This name can subsequently be used to refer to the
125 window instead of its ID, making interactive use somewhat more intuitive.
126  
127 =back
128  
129 =for example
130  
131 To open a X-window output that will stay on screen:
132  
133 $win = dev('/xs');
134  
135 To open two windows, one small and square, one large and wide:
136  
137 $win1 = dev('/xs', {Aspect => 1, WindowWidth => 4});
138 $win2 = dev('/xs', {Aspect => 0.5, WindowWidth => 10});
139  
140 =cut
141  
142 package PDL::Graphics::PGPLOT;
143  
144 use strict;
145 use warnings;
146  
147 # Just a plain function exporting package
148  
149 use PDL::Core qw/:Func :Internal/; # Grab the Core names
150 use PDL::Graphics::PGPLOTOptions qw(default_options);
151 use PDL::Graphics::PGPLOT::Window;
152 use PGPLOT;
153 use Exporter;
154  
155 our @ISA = qw(Exporter);
156 our @EXPORT = qw( dev hold release rel env bin errb line points
157 fits_imag imag imag1 fits_cont cont fits_vect vect
158 draw_wedge ctab ctab_info hi2d poly CtoF77coords
159 new_window focus_window window_list close_window
160 label_axes text legend cursor circle ellipse rectangle
161 tpoints tline retrieve_state replay turn_off_recording
162 turn_on_recording clear_state autolog get_current_window
163 transform
164 );
165  
166 *rel = *release; # Alias
167 *image = *imag;
168  
169 ############################################################################
170  
171 #############################################################
172 # This is a new version of PGPLOT which uses PDL::Options for
173 # option parsing.
174 #############################################################
175  
176 # Option explanation:
177 #
178 # Each routine has a set of options, and there is also a set of
179 # global options that may or may not affect a particular routine.
180 # The global options are defined here in the start of the code.
181 #
182 # This require a minor adjustment to the PDL::Options code since
183 # otherwise we would need to define the global options everywhere.
184 #
185 # The actual setting of default parameters is split off in a separate
186 # file PDL::Graphics::PGPLOTOptions - which also exports default_options
187 # used below.
188  
189 # The list of default global opttions, synonyms and translations.
190 #
191  
192 END { # Destructor to close plot when perl exits
193 _close_windows();
194 }
195  
196 #############################################################
197 # We now want to be able to have several plotting windows #
198 # The current set of windows is stored in these #
199 # variables - accessed by the local subs. Added JB 12/7/00 #
200 # #
201 # This has lead to a substantial rewrite of the code since #
202 # all the real work is now done in the Window object which #
203 # this routine only provides a convenient (and backwards #
204 # compatible) interface to. #
205 #############################################################
206  
207  
208  
209  
210 my @_WINDOWS=(); # The list of windows to access - the value is the options.
211 my %_WINDOWNAMES = (); # A map of names for each window to their number.
212 my $CW = undef;
213  
214  
215 =head2 new_window
216  
217 =for ref
218  
219 Open a PGPLOT graphics device
220  
221 =for usage
222  
223 $win = new_window($dev, $nx, $ny, $opt);
224  
225 This function is identical to L</dev> except that it always creates a new
226 window. This means that the user is required to close all windows
227 explicitly using L</close_window>. All functionality is otherwise like C<dev>
228 so see the documentation for L</dev> for details of use.
229  
230 =cut
231  
232 sub new_window {
233 my ($dev, $nx, $ny, $opt)=@_;
234  
235 if (ref($dev) eq 'HASH') {
236 $opt = $dev;
237 ($dev, $nx, $ny)=(undef, undef, undef);
238 } elsif (ref($nx) eq 'HASH') {
239 $opt = $nx;
240 ($nx, $ny)=(undef, undef);
241 }
242  
243 $opt={} unless defined($opt);
244  
245 # This will cause problems if people both pass dev, nx & ny _and_
246 # passes them in an options hash with poor spelling - don't do that..
247 $opt->{Device}=$dev if defined($dev);
248 $opt->{NXPanel}=$nx if defined($nx);
249 $opt->{NYPanel}=$ny if defined($ny);
250  
251 # Now insert the necessary information in the variables above.
252 # barf "Options must be an anonymous hash!\n" if defined($_[0]) &&
253 # ref($_[0]) ne 'HASH';
254 my $win = PDL::Graphics::PGPLOT::Window->new($opt);
255 my ($name, $id) = ($win->name(), $win->id());
256 $_WINDOWS[$id] = $name;
257 $_WINDOWNAMES{$name}=$win;
258 $_WINDOWNAMES{$id}=$name; # Reverse lookup for speed
259  
260 $CW = $win;
261 if (wantarray) {
262 return ($id, $name, $win);
263 } else {
264 return $id;
265 }
266 }
267  
268  
269 # Close all windows.
270 sub _close_windows {
271 close_window({All=>1}); # Do all windows...
272 }
273  
274  
275  
276 =head2 close_window
277  
278 =for ref
279  
280 Close a PGPLOT output device
281  
282 =for usage
283  
284 $id = dev($ENV{PGPLOT_DEV});
285 points $x, $y; # etc
286 close_window($id);
287  
288 This function closes a PGPLOT output device created with C<dev> or
289 C<new_window>. It requires the id of the window to close. If C<$id> is
290 left undefined, the currently focussed window is deleted and focus is
291 transferred to the lowest numbered window in existence. If many windows
292 have been created and deleted this might not be what you expect, so
293 it is recommended to make an explicit call to L</focus_window> after
294 any call to C<close_window>.
295  
296 The C<$id> will probably need to have been passed through L</dev>
297 (see usage). On X Windows, for C</XSERVE>, the window won't get closed,
298 but it will with C</XWINDOW>.
299  
300 =cut
301  
302 sub close_window {
303 my ($name)=@_;
304  
305 if (ref($name) eq 'HASH') { # Hack - to avoid checking window names..
306 for (my $id=0; $id<=$#_WINDOWS; $id++) {
307 next unless defined($_WINDOWS[$id]);
308 my $n = $_WINDOWS[$id];
309 $_WINDOWNAMES{$n}->close();
310 delete $_WINDOWNAMES{$n};
311 delete $_WINDOWNAMES{$id};
312 }
313 @_WINDOWS=(); # Remove all windows.
314 $CW = undef; # No current window
315 } else {
316 #
317 # Delete a specific window
318 #
319 my $id = _get_windownumber($name);
320 my $CWid = $CW->id();
321  
322 my $n= $_WINDOWNAMES{$id}; # In case the name was not passed..
323 $_WINDOWNAMES{$n}->close();
324 delete $_WINDOWNAMES{$n};
325 delete $_WINDOWNAMES{$id};
326 $_WINDOWS[$id]=undef; #splice(@_WINDOWS, $id, 1);
327  
328  
329 if ($CWid == $id) {
330 # Now determine the current window, viz the lowest numbered
331 # window existing.
332  
333 $CW = undef;
334 for (my $i=0; $i<=$#_WINDOWS; $i++) {
335 if (defined($_WINDOWS[$i])) {
336 $CW = $_WINDOWNAMES{$_WINDOWS[$i]};
337 last;
338 }
339 }
340 }
341  
342 # Since we set the corresponding array elements to undef - we
343 # have to check if we have in fact deleted the whole shebang in
344 # which case @_WINDOWS should be reset.
345 @_WINDOWS=() if (!defined($CW));
346 }
347 }
348  
349 # Utility function - allowing both numbers and names to be used.
350  
351 =head2 _get_windownumber
352  
353 Internal function to obtain the ID of a window. This allows the user
354 to refer to a window with its name.
355  
356 =cut
357  
358  
359 sub _get_windownumber {
360 my ($name)=@_;
361 if (!defined($name) || $name eq '') {
362 return -1 unless defined($CW);
363 return $CW->id();
364 }
365 my $windownumber = -1;
366 if (!exists($_WINDOWNAMES{$name}) || !ref($_WINDOWNAMES{$name})) {
367 # Then it ought to be a number
368 if ($name =~ m/^\d+$/) {
369 $windownumber = $name;
370 } else {
371 print "Valid window names: \n";
372 foreach my $k (sort keys %_WINDOWNAMES) {
373 print "$k\n";
374 }
375 barf ("I cannot switch to window $name - no such name\n");
376 }
377 } else {
378 $windownumber = $_WINDOWNAMES{$name}->id();
379 }
380 barf("Invalid windownumber ($name)\n") if
381 !defined($_WINDOWS[$windownumber]);
382  
383 return $windownumber;
384 }
385  
386  
387  
388 {
389 my $dev_options = undef;
390  
391 sub dev {
392 # This delayed creation of the options variable is for increased speed.
393 # Although for dev() this is unnecessary...
394 if (!defined($dev_options)) {
395 $dev_options = PDL::Options->new({NewWindow => 0});
396 $dev_options->warnonmissing(0); # Turn off warnings.
397 }
398  
399 # Get the input options.
400 my ($dev, $nx, $ny, $u_opt)=@_;
401  
402 if (ref($nx) eq 'HASH') {
403 $u_opt = $nx;
404 ($nx, $ny)=(undef, undef);
405 }
406 $u_opt = {} if !defined($u_opt);
407 my $opt = $dev_options->options($u_opt);
408  
409 # Then we want to close the current one before opening a new one.
410 if (!$opt->{NewWindow}) {
411 my ($state,$len);
412 pgqinf('STATE',$state,$len);
413 close_window() if $state eq 'OPEN';
414 }
415  
416 my $win=new_window(@_);
417 return $win;
418 }
419  
420 }
421  
422 =head2 focus_window
423  
424 =for ref
425  
426 Switch to another output window.
427  
428 =for usage
429  
430 Usage: focus_window($id);
431  
432 This command is used to switch output focus to another window created by
433 L</dev> or L</new_window>. The window can be referred to either by its
434 ID or by its name.
435  
436 =for example
437  
438 $win1 = dev('/xs', {WindowName => 'X-output'});
439 $win2 = dev('test.ps/ps', {WindowName => 'PS-output'});
440  
441 focus_window('X-output'); # Or focus_window($win1);
442 <.. Commands ..>
443 focus_window($win2); # Or focus_window('PS-output');
444 <.. Commands ..>
445  
446 =cut
447  
448  
449 # Switch to a new window.
450 sub focus_window {
451 my ($name)=@_;
452 my $windownumber = _get_windownumber($name);
453 die "No such window ($name)\n" if $windownumber < 0;
454 $CW = $_WINDOWNAMES{$_WINDOWS[$windownumber]};
455 print "Window focus switched to Window nr $windownumber ($_WINDOWNAMES{$windownumber})\n" if $PDL::verbose;
456 $CW->focus();
457 }
458  
459  
460 =head2 window_list
461  
462 =for ref
463  
464 Return a list of ID numbers and names of the windows currently opened using
465 L</dev> or L</new_window>.
466  
467 =for usage
468  
469 Usage: ($numbers, $names)=window_list();
470  
471 C<$numbers> and C<$names> are anonymous arrays giving the ID numbers and
472 names of the open windows respectively.
473  
474 =cut
475  
476 # And getting a list of windows
477 sub window_list {
478 my @numbers=();
479 my @names=();
480 foreach (sort keys %_WINDOWNAMES) {
481 if (ref($_WINDOWNAMES{$_}) eq 'PDL::Graphics::PGPLOT::Window') {
482 push @names, $_;
483 } else {
484 push @numbers, $_;
485 }
486 }
487 return (wantarray ? (\@numbers, \@names) : \@numbers);
488 }
489  
490  
491 sub label_axes {
492 # We do not label axes when there is no plot window.
493 return if !defined($CW);
494 $CW->label_axes(@_);
495 }
496  
497 sub turn_on_recording {
498 if (!defined($CW)) {
499 warn "You can only turn on recording when you have a device open!\n";
500 return;
501 }
502 $CW->turn_on_recording();
503 }
504  
505 sub turn_off_recording {
506 if (!defined($CW)) {
507 warn "You can only turn off recording when you have a device open!\n";
508 return;
509 }
510 $CW->turn_off_recording();
511 }
512  
513 sub retrieve_state {
514 if (!defined($CW)) {
515 warn "You can only retrieve the state when a device is open\n";
516 return;
517 }
518 $CW->retrieve_state();
519 }
520  
521  
522 sub replay {
523 if (!defined($CW)) {
524 warn "You can only replay plotting commands when a device is open!\n";
525 return;
526 }
527 $CW->replay(@_);
528 }
529  
530 sub clear_state {
531 if (!defined($CW)) {
532 warn "You can only clear the state when a device is open\n";
533 return;
534 }
535 $CW->clear_state();
536 }
537  
538  
539 sub autolog { # for this one we use the class method to set autolog globally
540 dev() if !defined($CW);
541 PDL::Graphics::PGPLOT::Window->autolog(@_);
542 }
543  
544 sub text {
545 barf 'Open a plot window first!' if !defined($CW);
546 $CW->text(@_);
547 }
548  
549 sub cursor {
550 barf 'Open a plot window first!' if !defined($CW);
551 $CW->cursor(@_);
552 }
553  
554 sub legend {
555 barf 'Open a plot window first!' if !defined($CW);
556 $CW->legend(@_);
557 }
558  
559 # should add these routines to EXPORT array as we create
560 # each routine
561 #
562 foreach my $func (
563 qw(
564 env bin cont fits_cont errb line tline points tpoints
565 imag fits_imag imag1 draw_wedge
566 ctab ctab_info hi2d poly vect fits_vect
567 CtoF77coords circle ellipse rectangle
568 ) ) {
569 eval <<"ENDOFFUNC";
570 sub $func {
571 dev() if !defined(\$CW);
572 \$CW->${func}(\@_);
573 }
574 ENDOFFUNC
575 }
576  
577 sub transform {
578 barf 'Open a plot window first!' if !defined($CW);
579 $CW->transform(@_);
580 }
581  
582 sub hold { return if !defined($CW); $CW->hold(); print "Graphics on HOLD\n" if $PDL::verbose;};
583 sub release { return if !defined($CW); $CW->release(); print "Graphics RELEASED\n" if $PDL::verbose;};
584 #sub held { return 0 if !defined($CW); return $CW->held()};
585 #sub current_device { return $CW->device(); };
586  
587 sub get_current_window { return $CW; } # return current window or undef if none exists
588  
589 1; # Exit with OK status