File Coverage

blib/lib/Astro/FITS/CFITSIO/Simple/Image.pm
Criterion Covered Total %
statement 53 53 100.0
branch 7 12 58.3
condition n/a
subroutine 13 13 100.0
pod n/a
total 73 78 93.5


line stmt bran cond sub pod time code
1             package Astro::FITS::CFITSIO::Simple::Image;
2              
3             # ABSTRACT: Read FITS Images
4              
5 11     11   227 use 5.008002;
  11         42  
6 11     11   59 use strict;
  11         23  
  11         228  
7 11     11   53 use warnings;
  11         23  
  11         448  
8              
9             require Exporter;
10              
11 11     11   66 use Params::Validate qw/ :all /;
  11         23  
  11         1931  
12              
13 11     11   79 use Carp;
  11         22  
  11         616  
14              
15 11     11   68 use PDL;
  11         31  
  11         79  
16              
17 11     11   31501 use Astro::FITS::CFITSIO qw/ :constants /;
  11         27  
  11         9336  
18 11     11   85 use Astro::FITS::CFITSIO::CheckStatus;
  11         25  
  11         289  
19 11     11   59 use Astro::FITS::CFITSIO::Simple::PDL qw/ :all /;
  11         27  
  11         1177  
20 11     11   75 use Astro::FITS::Header;
  11         48  
  11         266  
21 11     11   60 use Astro::FITS::Header::CFITSIO;
  11         38  
  11         4965  
22              
23             our @ISA = qw(Exporter);
24              
25             # Items to export into callers namespace by default. Note: do not export
26             # names by default without a very good reason. Use EXPORT_OK instead.
27             # Do not simply export all your public functions/methods/constants.
28              
29             # This allows declaration use Astro::FITS::CFITSIO::Table ':all';
30             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
31             # will save memory.
32             our %EXPORT_TAGS = (
33             'all' => [ qw(
34             _rdfitsImage
35             ) ] );
36              
37             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
38              
39             our @EXPORT = qw(
40              
41             );
42              
43             our $VERSION = '0.20';
44              
45             # this must be called ONLY from rdfits. it makes assumptions about
46             # the validity of arguments that have been verified by rdfits.
47              
48             sub _rdfitsImage {
49              
50 10 50   10   49 my $opts = 'HASH' eq ref $_[-1] ? pop : {};
51              
52             # first arg is fitsfilePtr
53             # second is cleanup object; must keep around until we're done,
54             # so it'll cleanup at the correct time.
55 10         19 my $fptr = shift;
56              
57             # we don't expect any more arguments; complain if we do...
58 10 50       37 croak( "unexpected extra arguments in call to rdfitsImage\n" )
59             if @_;
60              
61             my %opt = validate_with(
62             params => [$opts],
63 28     28   538 normalize_keys => sub { lc $_[0] },
64 10         229 spec => {
65             nullval => { type => SCALAR, optional => 1 },
66             dtype => { isa => 'PDL::Type', optional => 1 },
67             },
68             );
69              
70 8         87 tie my $status, 'Astro::FITS::CFITSIO::CheckStatus';
71              
72             # get image type and size
73 8         376 $fptr->get_img_equivtype( my $btype, $status );
74 8         270 $fptr->get_img_size( \my @naxes, $status );
75              
76             # what's the PDL type that encompasses the CFITSIO type?
77 8 100       224 my $ptype = $opt{dtype} ? $opt{dtype} : fits2pdl_imgtype( $btype );
78 8         108 my $data = PDL->new_from_specification( $ptype, @naxes );
79              
80             # grab header, delete scaling keywords, stuff it into piddle
81 8         677 my $hdr = Astro::FITS::Header::CFITSIO->new( fitsID => $fptr );
82 8         11182 tie my %hdr, 'Astro::FITS::Header', $hdr;
83 8         124 delete @hdr{qw/ BSCALE BZERO /};
84 8         2164 $data->sethdr( \%hdr );
85              
86             # what we tell CFITSIO that we're reading. some deception,
87             # as all we care about is the size of the data type
88 8         30 my $ctype = pdl2cfitsio( $ptype );
89              
90             # How to handle null pixels. A nullval of zero signals CFITSIO to
91             # ignore null pixels
92             my $nullval
93             = exists $opt{nullval} ? $opt{nullval}
94 8 50       45 : $PDL::Bad::Status ? my_badvalue( $ptype )
    50          
95             : 0;
96              
97             $fptr->read_pix(
98             $ctype, [ ( 1 ) x @naxes ],
99 8         347 $data->nelem, $nullval, ${ $data->get_dataref },
  8         156  
100             my $anynul, $status
101             );
102 8         543 $data->upd_data;
103              
104 8 50       61 $data->badflag( $anynul ) if $PDL::Bad::Status;
105              
106 8         247 $data;
107             }
108              
109             1;
110              
111             #
112             # This file is part of Astro-FITS-CFITSIO-Simple
113             #
114             # This software is Copyright (c) 2008 by Smithsonian Astrophysical Observatory.
115             #
116             # This is free software, licensed under:
117             #
118             # The GNU General Public License, Version 3, June 2007
119             #
120              
121             __END__