blib/lib/PDL/Graphics/PGPLOT.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/PDL/LiteF.pm ↑ Summary blib/lib/PDL/Fit/Linfit.pm »
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> $a = pdl [1..100]
12 pdl> $b = sqrt($a)
13 pdl> line $b
14 pdl> hold
15 Graphics on HOLD
16 pdl> $c = sin($a/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|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|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|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|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 # Just a plain function exporting package
145  
146 1 ▶use PDL::Core qw/:Func :Internal/; # Grab the Core names
Subroutine BEGIN: called CC=1 SCAR=0.0
147 1 ▶use PDL::Graphics::PGPLOTOptions qw(default_options);
Subroutine BEGIN: called CC=1 SCAR=0.0
148 1 ▶use PDL::Graphics::PGPLOT::Window;
Subroutine BEGIN: called CC=1 SCAR=2.6
149 use PGPLOT;
150 use Exporter;
151  
152 use strict;
153  
154 use vars qw (@ISA @EXPORT);
155  
156 @ISA = ('Exporter');
157  
158 @EXPORT = qw( dev hold release rel env bin errb line points
159 fits_imag imag imag1 fits_cont cont fits_vect vect
160 draw_wedge ctab ctab_info hi2d poly CtoF77coords
161 new_window focus_window window_list close_window
162 label_axes text legend cursor circle ellipse rectangle
163 tpoints tline retrieve_state replay turn_off_recording
164 turn_on_recording clear_state autolog get_current_window
165 transform
166 );
167  
168 *rel = *release; # Alias
169 *image = *imag;
170  
171 ############################################################################
172  
173 #############################################################
174 # This is a new version of PGPLOT which uses PDL::Options for
175 # option parsing.
176 #############################################################
177  
178 # Option explanation:
179 #
180 # Each routine has a set of options, and there is also a set of
181 # global options that may or may not affect a particular routine.
182 # The global options are defined here in the start of the code.
183 #
184 # This require a minor adjustment to the PDL::Options code since
185 # otherwise we would need to define the global options everywhere.
186 #
187 # The actual setting of default parameters is split off in a separate
188 # file PDL::Graphics::PGPLOTOptions - which also exports default_options
189 # used below.
190  
191 # The list of default global opttions, synonyms and translations.
192 #
193  
194 END { # Destructor to close plot when perl exits
195 _close_windows();
196 }
197  
198 #############################################################
199 # We now want to be able to have several plotting windows #
200 # The current set of windows is stored in these #
201 # variables - accessed by the local subs. Added JB 12/7/00 #
202 # #
203 # This has lead to a substantial rewrite of the code since #
204 # all the real work is now done in the Window object which #
205 # this routine only provides a convenient (and backwards #
206 # compatible) interface to. #
207 #############################################################
208  
209  
210  
211  
212 my @_WINDOWS=(); # The list of windows to access - the value is the options.
213 my %_WINDOWNAMES = (); # A map of names for each window to their number.
214 my $CW = undef;
215  
216  
217 =head2 new_window
218  
219 =for ref
220  
221 Open a PGPLOT graphics device
222  
223 =for usage
224  
225 $win = new_window($dev, $nx, $ny, $opt);
226  
227 This function is identical to L<dev|dev> except that it always creates a new
228 window. This means that the user is required to close all windows
229 explicitly using L<close_window|close_window>. All functionality is otherwise like C<dev>
230 so see the documentation for L<dev|dev> for details of use.
231  
232 =cut
233  
234 sub new_window {
235 my ($dev, $nx, $ny, $opt)=@_;
236  
237 if (ref($dev) eq 'HASH') {
238 $opt = $dev;
239 ($dev, $nx, $ny)=(undef, undef, undef);
240 } elsif (ref($nx) eq 'HASH') {
241 $opt = $nx;
242 ($nx, $ny)=(undef, undef);
243 }
244  
245 $opt={} unless defined($opt);
246  
247 # This will cause problems if people both pass dev, nx & ny _and_
248 # passes them in an options hash with poor spelling - don't do that..
249 $opt->{Device}=$dev if defined($dev);
250 $opt->{NXPanel}=$nx if defined($nx);
251 $opt->{NYPanel}=$ny if defined($ny);
252  
253 # Now insert the necessary information in the variables above.
254 # barf "Options must be an anonymous hash!\n" if defined($_[0]) &&
255 # ref($_[0]) ne 'HASH';
256 my $win = PDL::Graphics::PGPLOT::Window->new($opt);
257 my ($name, $id) = ($win->name(), $win->id());
258 $_WINDOWS[$id] = $name;
259 $_WINDOWNAMES{$name}=$win;
260 $_WINDOWNAMES{$id}=$name; # Reverse lookup for speed
261  
262 $CW = $win;
263 if (wantarray) {
264 return ($id, $name, $win);
265 } else {
266 return $id;
267 }
268 }
269  
270  
271 # Close all windows.
272 sub _close_windows {
273 close_window({All=>1}); # Do all windows...
274 }
275  
276  
277  
278 =head2 close_window
279  
280 =for ref
281  
282 Close a PGPLOT output device
283  
284 =for usage
285  
286 Usage: 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|focus_window> after
294 any call to C<close_window>.
295  
296 =cut
297  
298 sub close_window {
299 my ($name)=@_;
300  
301 if (ref($name) eq 'HASH') { # Hack - to avoid checking window names..
302 for (my $id=0; $id<=$#_WINDOWS; $id++) {
303 next unless defined($_WINDOWS[$id]);
304 my $n = $_WINDOWS[$id];
305 $_WINDOWNAMES{$n}->close();
306 delete $_WINDOWNAMES{$n};
307 delete $_WINDOWNAMES{$id};
308 }
309 @_WINDOWS=(); # Remove all windows.
310 $CW = undef; # No current window
311 } else {
312 #
313 # Delete a specific window
314 #
315 my $id = _get_windownumber($name);
316 my $CWid = $CW->id();
317  
318 my $n= $_WINDOWNAMES{$id}; # In case the name was not passed..
319 $_WINDOWNAMES{$n}->close();
320 delete $_WINDOWNAMES{$n};
321 delete $_WINDOWNAMES{$id};
322 $_WINDOWS[$id]=undef; #splice(@_WINDOWS, $id, 1);
323  
324  
325 if ($CWid == $id) {
326 # Now determine the current window, viz the lowest numbered
327 # window existing.
328  
329 $CW = undef;
330 for (my $i=0; $i<=$#_WINDOWS; $i++) {
331 if (defined($_WINDOWS[$i])) {
332 $CW = $_WINDOWNAMES{$_WINDOWS[$i]};
333 last;
334 }
335 }
336 }
337  
338 # Since we set the corresponding array elements to undef - we
339 # have to check if we have in fact deleted the whole shebang in
340 # which case @_WINDOWS should be reset.
341 @_WINDOWS=() if (!defined($CW));
342 }
343 }
344  
345 # Utility function - allowing both numbers and names to be used.
346  
347 =head2 _get_windownumber
348  
349 Internal function to obtain the ID of a window. This allows the user
350 to refer to a window with its name.
351  
352 =cut
353  
354  
355 sub _get_windownumber {
356 my ($name)=@_;
357 if (!defined($name) || $name eq '') {
358 return -1 unless defined($CW);
359 return $CW->id();
360 }
361 my $windownumber = -1;
362 if (!exists($_WINDOWNAMES{$name}) || !ref($_WINDOWNAMES{$name})) {
363 # Then it ought to be a number
364 if ($name =~ m/^\d+$/) {
365 $windownumber = $name;
366 } else {
367 print "Valid window names: \n";
368 foreach my $k (keys %_WINDOWNAMES) {
369 print "$k\n";
370 }
371 barf ("I cannot switch to window $name - no such name\n");
372 }
373 } else {
374 $windownumber = $_WINDOWNAMES{$name}->id();
375 }
376 barf("Invalid windownumber ($name)\n") if
377 !defined($_WINDOWS[$windownumber]);
378  
379 return $windownumber;
380 }
381  
382  
383  
384 {
385 my $dev_options = undef;
386  
387 sub dev {
388 # This delayed creation of the options variable is for increased speed.
389 # Although for dev() this is unnecessary...
390 if (!defined($dev_options)) {
391 $dev_options = PDL::Options->new({NewWindow => 0});
392 $dev_options->warnonmissing(0); # Turn off warnings.
393 }
394  
395 # Get the input options.
396 my ($dev, $nx, $ny, $u_opt)=@_;
397  
398 if (ref($nx) eq 'HASH') {
399 $u_opt = $nx;
400 ($nx, $ny)=(undef, undef);
401 }
402 $u_opt = {} if !defined($u_opt);
403 my $opt = $dev_options->options($u_opt);
404  
405 # Then we want to close the current one before opening a new one.
406 if (!$opt->{NewWindow}) {
407 my ($state,$len);
408 pgqinf('STATE',$state,$len);
409 close_window() if $state eq 'OPEN';
410 }
411  
412 my $win=new_window(@_);
413 return $win;
414 }
415  
416 }
417  
418 =head2 focus_window
419  
420 =for ref
421  
422 Switch to another output window.
423  
424 =for usage
425  
426 Usage: focus_window($id);
427  
428 This command is used to switch output focus to another window created by
429 L<dev|dev> or L<new_window|new_window>. The window can be referred to either by its
430 ID or by its name.
431  
432 =for example
433  
434 $win1 = dev('/xs', {WindowName => 'X-output'});
435 $win2 = dev('test.ps/ps', {WindowName => 'PS-output'});
436  
437 focus_window('X-output'); # Or focus_window($win1);
438 <.. Commands ..>
439 focus_window($win2); # Or focus_window('PS-output');
440 <.. Commands ..>
441  
442 =cut
443  
444  
445 # Switch to a new window.
446 sub focus_window {
447 my ($name)=@_;
448 my $windownumber = _get_windownumber($name);
449 die "No such window ($name)\n" if $windownumber < 0;
450 $CW = $_WINDOWNAMES{$_WINDOWS[$windownumber]};
451 print "Window focus switched to Window nr $windownumber ($_WINDOWNAMES{$windownumber})\n" if $PDL::verbose;
452 $CW->focus();
453 }
454  
455  
456 =head2 window_list
457  
458 =for ref
459  
460 Return a list of ID numbers and names of the windows currently opened using
461 L<dev|dev> or L<new_window|new_window>.
462  
463 =for usage
464  
465 Usage: ($numbers, $names)=window_list();
466  
467 C<$numbers> and C<$names> are anonymous arrays giving the ID numbers and
468 names of the open windows respectively.
469  
470 =cut
471  
472 # And getting a list of windows
473 sub window_list {
474 my @numbers=();
475 my @names=();
476 foreach (keys %_WINDOWNAMES) {
477 if (ref($_WINDOWNAMES{$_}) eq 'PDL::Graphics::PGPLOT::Window') {
478 push @names, $_;
479 } else {
480 push @numbers, $_;
481 }
482 }
483 return (wantarray ? (\@numbers, \@names) : \@numbers);
484 }
485  
486  
487 sub label_axes {
488 # We do not label axes when there is no plot window.
489 return if !defined($CW);
490 $CW->label_axes(@_);
491 }
492  
493 sub turn_on_recording {
494 if (!defined($CW)) {
495 warn "You can only turn on recording when you have a device open!\n";
496 return;
497 }
498 $CW->turn_on_recording();
499 }
500  
501 sub turn_off_recording {
502 if (!defined($CW)) {
503 warn "You can only turn off recording when you have a device open!\n";
504 return;
505 }
506 $CW->turn_off_recording();
507 }
508  
509 sub retrieve_state {
510 if (!defined($CW)) {
511 warn "You can only retrieve the state when a device is open\n";
512 return;
513 }
514 $CW->retrieve_state();
515 }
516  
517  
518 sub replay {
519 if (!defined($CW)) {
520 warn "You can only replay plotting commands when a device is open!\n";
521 return;
522 }
523 $CW->replay(@_);
524 }
525  
526 sub clear_state {
527 if (!defined($CW)) {
528 warn "You can only clear the state when a device is open\n";
529 return;
530 }
531 $CW->clear_state();
532 }
533  
534  
535 sub autolog { # for this one we use the class method to set autolog globally
536 dev() if !defined($CW);
537 PDL::Graphics::PGPLOT::Window->autolog(@_);
538 }
539  
540 sub text {
541 barf 'Open a plot window first!' if !defined($CW);
542 $CW->text(@_);
543 }
544  
545 sub cursor {
546 barf 'Open a plot window first!' if !defined($CW);
547 $CW->cursor(@_);
548 }
549  
550 sub legend {
551 barf 'Open a plot window first!' if !defined($CW);
552 $CW->legend(@_);
553 }
554  
555 # should add these routines to EXPORT array as we create
556 # each routine
557 #
558 foreach my $func (
559 qw(
560 env bin cont fits_cont errb line tline points tpoints
561 imag fits_imag imag1 draw_wedge
562 ctab ctab_info hi2d poly vect fits_vect
563 CtoF77coords circle ellipse rectangle
564 ) ) {
565 eval <<"ENDOFFUNC";
566 sub $func {
567 dev() if !defined(\$CW);
568 \$CW->${func}(\@_);
569 }
570 ENDOFFUNC
571 }
572  
573 sub transform {
574 barf 'Open a plot window first!' if !defined($CW);
575 $CW->transform(@_);
576 }
577  
578 sub hold { return if !defined($CW); $CW->hold(); print "Graphics on HOLD\n" if $PDL::verbose;};
579 sub release { return if !defined($CW); $CW->release(); print "Graphics RELEASED\n" if $PDL::verbose;};
580 #sub held { return 0 if !defined($CW); return $CW->held()};
581 #sub current_device { return $CW->device(); };
582  
583 sub get_current_window { return $CW; } # return current window or undef if none exists
584  
585 1; # Exit with OK status
586  
587  
588  
589  
« blib/lib/PDL/LiteF.pm ↑ Summary blib/lib/PDL/Fit/Linfit.pm »