Created
December 5, 2012 17:51
-
-
Save astrolitterbox/4217869 to your computer and use it in GitHub Desktop.
bin cube: Perl snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ($#ARGV<2) { | |
print "USE: bin_cube.pl INPUT.CUBE.fits BIN_BOX OUTPUT.CUBE.fits \n"; | |
exit; | |
} | |
$infile=$ARGV[0]; | |
#scale factor | |
$N=$ARGV[1]; | |
$outfile=$ARGV[2]; | |
#Perl data language: read FITS file | |
$pdl=rfits($infile); | |
$h=$pdl->hdr; | |
#dimensions of input image | |
($nx,$ny,$nz)=$pdl->dims; | |
#dimensions/scale factor; if $N < 1 -- array is enlarged | |
$NX=int($nx/$N); | |
$NY=int($ny/$N); | |
#output array | |
$out=zeroes($NX,$NY,$nz); | |
#reading info from FITS header: | |
#dimensions of input array | |
$$h{NAXIS1}=$NX; | |
$$h{NAXIS2}=$NY; | |
#physical scale | |
$$h{CRPIX1}=$$h{CRPIX1}/$N; | |
$$h{CRPIX2}=$$h{CRPIX2}/$N; | |
$$h{CDELT1}=$$h{CDELT1}*$N; | |
$$h{CDELT2}=$$h{CDELT2}*$N; | |
$$h{CD1_1}=$$h{CD1_1}*$N; | |
$$h{CD2_2}=$$h{CD2_2}*$N; | |
#loop through the array | |
for ($i=0;$i<$NX;$i++) { | |
for ($j=0;$j<$NY;$j++) { | |
#cut out small slice out of the array | |
$nx1=$i*$N; | |
$nx2=($i+1)*$N; | |
$ny1=$j*$N; | |
$ny2=($j+1)*$N; | |
if ($nx1<0) { | |
$nx1=0; | |
} | |
if ($ny1<0) { | |
$ny1=0; | |
} | |
if ($nx2>=$nx) { | |
$nx2=$nx-1; | |
} | |
if ($ny2>=$ny) { | |
$ny2=$ny-1; | |
} | |
# The section below: it resizes the array and takes the _mean_ of the bin as the rebinned pixel value | |
#instead of preserving the flux, or am I wrong? | |
my $sec=$pdl->slice("$nx1:$nx2,$ny1:$ny2,"); | |
my $sum=sumover($sec); #http://pdl.perl.org/?docs=Ufunc&title=sumover#sumover | |
my $sum2=sumover($sum); #summing across the other dimension of the slice | |
$t = $out->slice("($i),($j),"); | |
$t .=$sum2/($N*$N); #sum is divided by the number of initial pixels in the bin | |
} | |
} | |
#write out | |
$out->sethdr($h); | |
$out->wfits($outfile); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment