Skip to content

Commit

Permalink
Merge 2ad3e4f into 8fe8cb5
Browse files Browse the repository at this point in the history
  • Loading branch information
d-lamb committed Mar 4, 2018
2 parents 8fe8cb5 + 2ad3e4f commit 6ec79c8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
16 changes: 14 additions & 2 deletions IO/FITS/FITS.pm
Expand Up @@ -117,6 +117,10 @@ Suffix magic:
$pdl = rfits('file.fits.gz[3]'); # Read 3rd extension
@pdls = rfits('file.fits'); # Read primary data and extensions
Tilde expansion:
#expand leading ~ to home directory (using glob())
$pdl = rfits '~/filename.fits';
$hdr = rfits('file.fits',{data=>0}); # Options hash changes behavior
In list context, C<rfits> reads the primary image and all possible
Expand Down Expand Up @@ -342,7 +346,9 @@ sub PDL::rfits {
# indicator which cancelled the check for empty primary data array at the end.
my $explicit_extension = ($file =~ m/\[\d+\]$/ ? 1 : 0);
$extnum = ( ($file =~ s/\[(\d+)\]$//) ? $1 : 0 );


$file =~ s/^(~)/glob($1)/e; #tilde expansion.

$file = "gunzip -c $file |" if $file =~ /\.gz$/; # Handle compression
$file = "uncompress -c $file |" if $file =~ /\.Z$/;

Expand Down Expand Up @@ -1562,6 +1568,10 @@ Suffix magic:
# Automatically compress through pipe to compress
wfits $pdl, 'filename.fits.Z';
Tilde expansion:
#expand leading ~ to home directory (using glob())
wfits $pdl, '~/filename.fits';
=over 3
=item * Ordinary (PDL) data handling:
Expand Down Expand Up @@ -1862,7 +1872,9 @@ sub PDL::wfits {
}

my ($k, $buff, $off, $ndims, $sz);


$file =~ s/^(~)/glob($1)/e; # tilde expansion

local $SIG{PIPE};

if ($file =~ /\.gz$/) { # Handle suffix-style compression
Expand Down
13 changes: 11 additions & 2 deletions t/fits.t
Expand Up @@ -12,7 +12,7 @@ use PDL::Config;

kill 'INT',$$ if $ENV{UNDER_DEBUGGER}; # Useful for debugging.

use Test::More tests => 95;
use Test::More tests => 97;

BEGIN {
use_ok( "PDL::IO::FITS" ); #1
Expand Down Expand Up @@ -316,6 +316,15 @@ SKIP:{
ok(all($b==$a),"The new image matches the old one (longlong)");
unlink $file;
}


###############################
# Check that tilde expansion works
my $tildefile = cfile('~',"PDL-IO-FITS-test_$$.fits");
eval { sequence(3,5,2)->wfits($tildefile); };
ok(!$@, "wfits tilde expansion didn't fail");
eval { rfits($tildefile); };
ok(!$@, "rfits tilde expansion didn't fail");
$tildefile =~ s/^(~)/glob($1)/e; #use the same trick as in FITS.pm to resolve this filename.
unlink($tildefile) or warn "Could not delete $tildefile: $!\n"; #clean up.

1;

0 comments on commit 6ec79c8

Please sign in to comment.