blib/lib/PDL/Graphics/X/Fits.pm untested

filter: Statementstmt 0.0% 0 / 329 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 467 CC 46 SCAR 48.2
CC
46
Cov
0%
new
380.0
_numticks
42.0
_drawAxes
30.0
CRAP 124.1
1 package PDL::Graphics::X::Fits;
2  
3 use 5.008001;
4 use strict;
5 use warnings;
6  
7 require Exporter;
8  
9 our @ISA = qw(Exporter);
10  
11 our %EXPORT_TAGS = ( 'all' => [ qw() ] );
12  
13 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
14  
15 our @EXPORT = qw();
16  
17 our $VERSION = '0.01';
18  
19 #
20 # Simple OO fits image display built on PDL::Graphics::X
21 #
22 #
23 #
24 # Hazen 3/05
25 #
26  
27 use PDL;
28 use PDL::ImageND;
29 use PDL::Graphics::X;
30  
31 #
32 # Global variables
33 #
34  
35 my $warning_message = "PDL::Graphics::X::Fits";
36  
37 # hard coded display defaults
38  
39 my $BORDER_WIDTH = 75;
40 my $BORDER_COLOR = [0, 1, 0];
41 my $LABEL_COLOR = [0, 1, 0];
42 my $MINOR_TICK_LEN = 4;
43 my $MAJOR_TICK_LEN = 7;
44 my $TICK_COLOR = [0, 1, 0];
45 my $WINDOW_BACKGROUND_COLOR = [0, 0, 0];
46  
47 # hash for parameters associated with displaying the fits image
48  
49 my %fits_params = (
50 FITS_X => 100,
51 FITS_Y => 100,
52 MIN => undef,
53 MAX => undef,
54 RESCALE_X => 1.0,
55 RESCALE_Y => 1.0,
56 RGB => 0,
57 WIN_MAX_X => 100,
58 WIN_MAX_Y => 100,
59 X_LABEL => 'X',
60 X_MAX => 0,
61 X_MIN => 0,
62 X_UNIT => 'arb',
63 Y_LABEL => 'Y',
64 Y_MAX => 0,
65 Y_MIN => 0,
66 Y_UNIT => 'arb',
67 );
68  
69 # default window options, these are just passed through to PDL::Graphics::X
70  
71 my %default_options = (
72 CTAB => undef,
73 MIN => undef,
74 MAX => undef,
75 RGB => 0,
76 WIN_TITLE => "X::FITS",
77 );
78  
79 #
80 # Private sub-routines
81 #
82  
83 # parse options hashes
84  
85 sub _parseOptions {
86 my $input_options = shift;
87 my $default_options = shift;
88  
89 while ( my($temp_key, $temp_value) = each %{$input_options} ) {
90 if (exists $default_options->{$temp_key}) {
91 $default_options->{$temp_key} = $temp_value;
92 } else {
93 print "$warning_message, no such option : $temp_key\n";
94 }
95 }
96 }
97  
98 # given a number, figure out how many digits it is away from zero.
99  
100 sub _digitsToZero{
101 my $num = shift;
102 my $sign = shift;
103  
104 $num = abs($num);
105 my $num_d = 1;
106 my $sign_d = 1;
107 if($num>0.0){
108 my $temp = log($num);
109 if($temp < 0) {
110 $sign_d = -1;
111 }
112 $num_d = abs($temp)/log(10);
113 }
114 $num_d = ceil($num_d);
115 return ($num_d, $sign_d);
116 }
117  
118 # figure out the maximum number of digits & direction from zero of two numbers
119  
120 sub _numDigits {
121 my $min = shift;
122 my $max = shift;
123  
124 my ($num_d_min, $sign_d_min) = _digitsToZero($min);
125 my ($num_d_max, $sign_d_max) = _digitsToZero($max);
126  
127 my $num_d = $num_d_min;
128 my $sign_d = $sign_d_min;
129 if($num_d_max > $num_d_min){
130 $num_d = $num_d_max;
131 $sign_d = $sign_d_max;
132 }
133  
134 return ($num_d, $sign_d);
135 }
136
137 # convert number to string
138  
139 sub _makeLabel {
140 my $num = shift;
141 my $min = shift;
142 my $max = shift;
143
144 my $range = $max - $min;
145 my ($num_d, $sign_d) = _digitsToZero($range);
146 my $text;
147 if($sign_d > 0){
148 my $format = "%.f";
149 if($num_d < 2){
150 $format = "%.1f";
151 }
152 $text = sprintf($format, $num);
153 } else {
154 my $format = "%." . ($num_d+1) . "f";
155 $text = sprintf($format, $num);
156 }
157
158 return $text;
159 }
160  
161 # adjust the axis size and exponent based on the number of
162 # digits and sign needed to represent the axis
163  
164 sub _adjustAxis{
165 my $max = shift;
166 my $min = shift;
167  
168 my $exp = "";
169  
170 my($digits, $sign) = _numDigits($max, $min);
171 if($sign > 0){
172 if($digits > 2){
173 $max = $max/(10.0 ** ($digits - 1));
174 $min = $min/(10.0 ** ($digits - 1));
175 $exp = " x10^" . ($digits - 1);
176 }
177 } else {
178 if($digits > 1){
179 $max = $max * (10.0 ** ($digits - 1));
180 $min = $min * (10.0 ** ($digits - 1));
181 $exp = " x10^-" . ($digits - 1);
182 }
183 }
184 return($max, $min, $exp);
185 }
186  
187 # compute a reasonable number of ticks & sub-ticks for the plot
188 #
189 # this is a perl version of the PLplot function pldtik, see src/pdltik.c
190 #
191 # Copyright information for the original PLplot function is as follows :
192 #
193 # Copyright (C) 2004 Alan W. Irwin
194 #
195 # This file is part of PLplot.
196 #
197 # PLplot is free software; you can redistribute it and/or modify
198 # it under the terms of the GNU General Library Public License as published
199 # by the Free Software Foundation; either version 2 of the License, or
200 # (at your option) any later version.
201 #
202 # PLplot is distributed in the hope that it will be useful,
203 # but WITHOUT ANY WARRANTY; without even the implied warranty of
204 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
205 # GNU Library General Public License for more details.
206 #
207 # You should have received a copy of the GNU Library General Public License
208 # along with PLplot; if not, write to the Free Software
209 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
210 #
211  
212 sub _numticks {
213 my $range = shift;
214 my $axis_size = shift;
215  
216 # Magnitude of min/max difference to get tick spacing
217  
218 my $t1 = log(abs($range))/log(10);
219 my $np = floor($t1);
220 $t1 = $t1 - $np;
221  
222 # Get tick spacing
223  
224 my $t2;
225 my $ns;
226 if ($t1 > 0.7781512503) {
227 $t2 = 2.0;
228 $ns = 4;
229 }
230 elsif ($t1 > 0.4771212549) {
231 $t2 = 1.0;
232 $ns = 5;
233 }
234 elsif ($t1 > 0.1760912591) {
235 $t2 = 5.0;
236 $ns = 5;
237 $np = $np - 1;
238 }
239 else {
240 $t2 = 2.0;
241 $ns = 4;
242 $np = $np - 1;
243 }
244  
245 # Now compute reasonable tick spacing
246  
247 my $tick = $t2 * (10.0**$np);
248 my $nsubt = abs($ns);
249  
250 # adjust for very small / very large images
251
252 if($axis_size < 300){
253 $tick = 2*$tick;
254 }
255  
256 if($axis_size > 900){
257 $tick = 0.5*$tick;
258 }
259
260 return($tick, $nsubt);
261 }
262  
263 # draw the image as a bitmap in the window with appropriate scale, etc...
264  
265 sub _displayFits {
266 my $self = shift;
267
268 my $off_x = $BORDER_WIDTH;
269 my $off_y = $BORDER_WIDTH;
270 my $width = $self->{Params}->{"FITS_X"} * $self->{Params}->{"RESCALE_X"};
271 my $height = $self->{Params}->{"FITS_Y"} * $self->{Params}->{"RESCALE_Y"};
272 my $min = $self->{Params}->{"MIN"};
273 my $max = $self->{Params}->{"MAX"};
274 my $to_display = rebin($self->{Fits}, $width, $height);
275 if($self->{Params}->{"RGB"}){
276 $to_display = $to_display->slice(":," . ($height-1) . ":0,0:2");
277 } else {
278 $to_display = $to_display->slice(":," . ($height-1) . ":0");
279 }
280
281 $self->{X_win}->imag($to_display, {DEST_X => $off_x, DEST_Y => $off_y, DEST_W => $width, DEST_H => $height, MIN => $min, MAX => $max});
282 }
283  
284 # draw border around bitmap & hash marks & text & axis labels
285  
286 sub _drawAxes {
287 my $self = shift;
288  
289 my $width = $self->{Params}->{"FITS_X"} * $self->{Params}->{"RESCALE_X"};
290 my $height = $self->{Params}->{"FITS_Y"} * $self->{Params}->{"RESCALE_Y"};
291
292 # draw a border around the image
293
294 my $x1 = $BORDER_WIDTH - 1;
295 my $x2 = $x1 + $width + 1;
296 my $y1 = $BORDER_WIDTH - 1;
297 my $y2 = $y1 + $height + 1;
298 $self->{X_win}->rect($x1, $y1, $x2, $y2, {LINEWIDTH => 1, LINESTYLE => 0, COLOR => $BORDER_COLOR});
299
300 # calculate the range & the number of ticks
301 # if the range is very large or very small, we adjust & add
302 # the exponent to the axis label
303  
304 my $x_exp = "";
305 my $x_max = $self->{Params}->{"X_MAX"};
306 my $x_min = $self->{Params}->{"X_MIN"};
307 ($x_max, $x_min, $x_exp) = _adjustAxis($x_max, $x_min);
308 my $x_range = $x_max - $x_min;
309 my ($x_ticks, $n_x_sub_ticks) = _numticks($x_range, $width);
310  
311 my $y_exp = "";
312 my $y_max = $self->{Params}->{"Y_MAX"};
313 my $y_min = $self->{Params}->{"Y_MIN"};
314 ($y_max, $y_min, $y_exp) = _adjustAxis($y_max, $y_min);
315 my $y_range = $y_max - $y_min;
316 my ($y_ticks, $n_y_sub_ticks) = _numticks($y_range, $width);
317  
318 # draw x ticks & tick labels
319
320 my $cur_x = floor($x_min/($x_ticks/$n_x_sub_ticks));
321 while($cur_x < $x_max){
322 my $x = (($cur_x - $x_min)/$x_range) * $width + $BORDER_WIDTH;
323 if($x > $BORDER_WIDTH){
324 my $tick_len = $MINOR_TICK_LEN;
325 if((abs($cur_x - $x_ticks * rint($cur_x/$x_ticks))) < (0.1 * $x_ticks)) {
326 $tick_len = $MAJOR_TICK_LEN;
327 my $label = _makeLabel($cur_x, $x_min, $x_max);
328 my $l_len = length($label);
329 $self->{X_win}->text($label, $x - 4.0*$l_len, $y2 + 14, 0.0, {CHARSIZE => 14, COLOR => $LABEL_COLOR});
330 }
331 my $xvs = pdl($x, $x);
332 my $yvs = pdl($y1, $y1+$tick_len);
333 $self->{X_win}->line($xvs, $yvs, {LINEWIDTH => 1, LINESTYLE => 0, COLOR => $TICK_COLOR});
334 $xvs = pdl($x, $x);
335 $yvs = pdl($y2, $y2-$tick_len);
336 $self->{X_win}->line($xvs, $yvs, {LINEWIDTH => 1, LINESTYLE => 0, COLOR => $TICK_COLOR});
337 }
338 $cur_x += ($x_ticks/$n_x_sub_ticks);
339 }
340  
341 # draw y ticks & tick labels
342
343 my $cur_y = floor($y_min/($y_ticks/$n_y_sub_ticks));
344 while($cur_y < $y_max){
345 my $y = $height - (($cur_y - $y_min)/$y_range) * $height + $BORDER_WIDTH;
346 if($y < ($BORDER_WIDTH + $height)){
347 my $tick_len = $MINOR_TICK_LEN;
348 if((abs($cur_y - $y_ticks * rint($cur_y/$y_ticks))) < (0.1 * $y_ticks)) {
349 $tick_len = $MAJOR_TICK_LEN;
350 my $label = _makeLabel($cur_y, $y_min, $y_max);
351 my $l_len = length($label);
352 $self->{X_win}->text($label, $x1 - 8.0*$l_len - 4, $y + 5, 0.0, {CHARSIZE => 14, COLOR => $LABEL_COLOR});
353 }
354 my $xvs = pdl($x1, $x1+$tick_len);
355 my $yvs = pdl($y, $y);
356 $self->{X_win}->line($xvs, $yvs, {LINEWIDTH => 1, LINESTYLE => 0, COLOR => $TICK_COLOR});
357 $xvs = pdl($x2, $x2-$tick_len);
358 $yvs = pdl($y, $y);
359 $self->{X_win}->line($xvs, $yvs, {LINEWIDTH => 1, LINESTYLE => 0, COLOR => $TICK_COLOR});
360 }
361 $cur_y += ($y_ticks/$n_y_sub_ticks);
362 }
363
364 # draw axis labels
365  
366 my $label = $self->{Params}->{"X_LABEL"} . " (" . $self->{Params}->{"X_UNIT"} . ")" . $x_exp;
367 my $l_len = length($label);
368 $self->{X_win}->text($label, -5.0*$l_len + 0.5*(2.0*$BORDER_WIDTH + $width), $y2 + 0.5*$BORDER_WIDTH + 6, 0.0, {CHARSIZE => 18, COLOR => $LABEL_COLOR});
369
370 $label = $self->{Params}->{"Y_LABEL"} . " (" . $self->{Params}->{"Y_UNIT"} . ")" . $y_exp;
371 $l_len = length($label);
372 $self->{X_win}->text($label, $x1 - 0.8*$BORDER_WIDTH - 4, -5.0*$l_len + 0.5*(2.0*$BORDER_WIDTH + $width), -90.0, {CHARSIZE => 18, COLOR => $LABEL_COLOR});
373  
374 }
375  
376 # handles making a "pretty" string for wedge labels
377  
378 sub _wedgeLabel {
379 my $num = shift;
380  
381 my $label;
382 if(($num < 9999)&&($num > 0.01)){
383 if($num > 1) { $label = sprintf("%d", $num); }
384 elsif($num > 0.1) { $label = sprintf("%.2f", $num); }
385 else { $label = sprintf("%.3f", $num); }
386 } elsif ($num == 0.0) {
387 $label = "0.0";
388 } else {
389 $label = sprintf("%.1e", $num);
390 }
391
392 return $label;
393 }
394  
395 # draw a color "wedge" & associated text
396  
397 sub _drawWedge {
398 my $self = shift;
399  
400 my $width = $self->{Params}->{"FITS_X"} * $self->{Params}->{"RESCALE_X"};
401 my $height = $self->{Params}->{"FITS_Y"} * $self->{Params}->{"RESCALE_Y"};
402
403 my $wedge = 255 - yvals(zeroes(20,256));
404 if($height < 256){
405 $wedge = rebin($wedge, 20, $height);
406 }
407
408 my $x1 = $width + 1.5 * $BORDER_WIDTH - 10;
409 my $x2 = $x1 + 20;
410 my $y1 = $BORDER_WIDTH + 0.5 * $height - 0.5 * $wedge->dim(1);
411 my $y2 = $y1 + $wedge->dim(1);
412  
413 $self->{X_win}->imag($wedge, {DEST_X => $x1, DEST_Y => $y1});
414 $self->{X_win}->rect($x1-1, $y1-1, $x2, $y2, {LINEWIDTH => 1, LINESTYLE => 0, COLOR => $BORDER_COLOR});
415  
416 my $label = _wedgeLabel($self->{Params}->{"MIN"});
417 my $l_len = length($label);
418 $self->{X_win}->text($label, $x1 - 4.0*$l_len + 10, $y2 + 15, 0.0, {CHARSIZE => 14, COLOR => $LABEL_COLOR});
419  
420 $label = _wedgeLabel($self->{Params}->{"MAX"});
421 $l_len = length($label);
422 $self->{X_win}->text($label, $x1 - 4.0*$l_len + 10, $y1 - 4, 0.0, {CHARSIZE => 14, COLOR => $LABEL_COLOR});
423 }
424  
425 #
426 # Object Methods
427 #
428  
429 # creates a new fits window object
430  
431 sub new {
432 my $self = shift;
433 my $fits = shift;
434 my $opt = shift;
435  
436 my %fits_p = %fits_params;
437 my %wopt = %default_options;
438 if(defined($opt)){ _parseOptions($opt, \%wopt); }
439  
440 if(defined($fits)){
441 if($fits->hdr->{NAXIS} > 1){
442 my $X_win = PDL::Graphics::X->new({WIN_TITLE => $wopt{"WIN_TITLE"}, SIZE_X => 100, SIZE_Y => 100, BACK_COLOR => $WINDOW_BACKGROUND_COLOR});
443 if(defined($X_win)){
444
445 # size window appropriately
446
447 my ($max_x, $max_y) = ($X_win->winsize())[2,3];
448 my $fits_x = $fits->hdr->{NAXIS1};
449 my $fits_y = $fits->hdr->{NAXIS2};
450 my $re_scale = 1.0;
451 while(($fits_x * $re_scale) > ($max_x - 2*$BORDER_WIDTH)) { $re_scale = 0.6 * $re_scale; }
452 while(($fits_y * $re_scale) > ($max_y - 2*$BORDER_WIDTH)) { $re_scale = 0.6 * $re_scale; }
453 $X_win->resize(($fits_x * $re_scale + 2*$BORDER_WIDTH), ($fits_y * $re_scale + 2*$BORDER_WIDTH));
454  
455 # read the parameters from the fits image into the parameters hash
456
457 $fits_p{"FITS_X"} = $fits_x;
458 $fits_p{"FITS_Y"} = $fits_y;
459 $fits_p{"MIN"} = $wopt{"MIN"};
460 $fits_p{"MAX"} = $wopt{"MAX"};
461 unless(defined($fits_p{"MIN"})){ $fits_p{"MIN"} = (minmax($fits))[0]; }
462 unless(defined($fits_p{"MAX"})){ $fits_p{"MAX"} = (minmax($fits))[1]; }
463 $fits_p{"RESCALE_X"} = $re_scale;
464 $fits_p{"RESCALE_Y"} = $re_scale;
465 $fits_p{"WIN_MAX_X"} = $max_x;
466 $fits_p{"WIN_MAX_Y"} = $max_y;
467
468 if(defined($fits->hdr->{CTYPE1})) { $fits_p{"X_LABEL"} = $fits->hdr->{CTYPE1}; }
469 if(defined($fits->hdr->{CUNIT1})) { $fits_p{"X_UNIT"} = $fits->hdr->{CUNIT1}; }
470 if(defined($fits->hdr->{CTYPE2})) { $fits_p{"Y_LABEL"} = $fits->hdr->{CTYPE2}; }
471 if(defined($fits->hdr->{CUNIT2})) { $fits_p{"Y_UNIT"} = $fits->hdr->{CUNIT2}; }
472  
473 my $x_delt = 1.0;
474 my $y_delt = 1.0;
475 if(defined($fits->hdr->{CDELT1})) { $x_delt = $fits->hdr->{CDELT1}; }
476 if(defined($fits->hdr->{CDELT2})) { $y_delt = $fits->hdr->{CDELT2}; }
477 my $x_zero = 0.0;
478 my $y_zero = 0.0;
479 if(defined($fits->hdr->{CRPIX1})) { $x_zero = $fits->hdr->{CRPIX1}; }
480 if(defined($fits->hdr->{CRPIX2})) { $y_zero = $fits->hdr->{CRPIX2}; }
481 $fits_p{"X_MAX"} = $x_delt * ($fits_x - $x_zero);
482 $fits_p{"X_MIN"} = $x_delt * ( 0 - $x_zero);
483 $fits_p{"Y_MAX"} = $y_delt * ($fits_y - $y_zero);
484 $fits_p{"Y_MIN"} = $y_delt * ( 0 - $y_zero);
485
486 if($wopt{"RGB"}) {
487 if($fits->dim(2) == 3) {
488 $fits_p{"RGB"} = 1;
489 } else {
490 print "$warning_message, attempt to display non-RGB image as RGB\n";
491 }
492 }
493 my $param = {X_win => $X_win, Fits => $fits, Params => \%fits_p};
494 $self = bless($param, $self);
495
496 if(defined($wopt{"CTAB"})){
497 $X_win->ctab($wopt{"CTAB"});
498 }
499 _displayFits($self);
500 _drawAxes($self);
501 unless($fits_p{"RGB"}) { _drawWedge($self); }
502 return $self;
503 } else {
504 print "$warning_message, X initialize failed\n";
505 }
506 } else {
507 print "$warning_message, 1 or 0 dimensional FITS display is not supported\n";
508 }
509 } else {
510 print "$warning_message, FITS image is undefined\n";
511 }
512 return undef;
513 }
514  
515 # returns mouse coordinates in the picture coordinate system
516  
517 sub cursor {
518 my $self = shift;
519  
520 my $width = $self->{Params}->{"FITS_X"} * $self->{Params}->{"RESCALE_X"};
521 my $height = $self->{Params}->{"FITS_Y"} * $self->{Params}->{"RESCALE_Y"};
522 my $x_range = $self->{Params}->{"X_MAX"} - $self->{Params}->{"X_MIN"};
523 my $y_range = $self->{Params}->{"Y_MAX"} - $self->{Params}->{"Y_MIN"};
524  
525 while(1){
526 my($x, $y) = $self->{X_win}->cursor();
527 if(defined($x)){
528 if(($x > ($BORDER_WIDTH-1))&&($x < ($width+$BORDER_WIDTH-1))){
529 if(($y > ($BORDER_WIDTH-1))&&($y < ($width+$BORDER_WIDTH-1))){
530 $x = (($x - $BORDER_WIDTH)/$width) * $x_range + $self->{Params}->{"X_MIN"};
531 $y = (1.0 - (($y - $BORDER_WIDTH)/$height)) * $y_range + $self->{Params}->{"Y_MIN"};
532 return ($x, $y);
533 }
534 }
535 } else {
536 print "$warning_message, unable to get mouse coordinates\n";
537 return (0,0);
538 }
539 }
540 }
541  
542 # catch object destroy for any clean-up
543  
544 sub DESTROY {
545 my $self = shift;
546 }
547  
548 1;
549 __END__