[AstroPy] Rebin FITS images and preserve or recalculate WCS?
Bernstein, Gary M
garyb at PHYSICS.UPENN.EDU
Tue Aug 4 05:44:19 EDT 2020
Hi Eric -
I don’t know whether this exists but in this case it might be faster to write than to do the Google search. The block summing can be done in numpy by reshaping the array. In the case where the original image shape is divisible by the shrinkage factor s, you’d do
m,n = img.shape
img = np.sum( img.reshape(m//s,s,n), axis=1) # Contract along y
Img = np.sum( img.reshape(m//s,n//s,s),axis=2). # Contract along x
And to fix the WCS, you need to divide CRPIXn by s, and multiply CDELTn by s. There is some slight complication to the first operation because FITS assumes 1-indexed pixels, and the integer marks the center of a pixel, so its really CRPIXn -> (CRPIXn—0.5)/s + 0.5
I think this should work universally because the linear transform is always the first step of a WCS mapping.
> On Aug 3, 2020, at 11:01 PM, Eric Jensen <ejensen1 at swarthmore.edu> wrote:
> Hi all,
> We are looking at purchasing a new CMOS camera that has 4-micron pixels, which would significantly undersample our PSF. (The camera is otherwise excellent for our needs, e.g. excellent QE.) So I’m looking at whether there would be a straightforward way in scripted post-processing to reduce the image file sizes while not losing information. The existing camera driver doesn’t support more than 2x2 binning at this time.
> Is there available Python code that can take a FITS image with a valid WCS in the header and rebin it, and output a FITS image that still has a valid WCS for the rebinned image? The rebinning should also preserve flux, though I think that’s easier than the WCS part. Nothing obvious turns up in a little searching, but I could easily have missed it.
> It seems that Montage might be able to do this (mShrink) but if possible I’d prefer a pure-Python solution, as it would be easier to implement under Windows on our observatory control computer. (A fallback would be to install a Linux distro on top of the Windows Subsystem for Linux, but if it’s possible to do it purely in Python that would be a lot simpler.)
> Thanks for any thoughts,
> Eric Jensen
> Professor of Astronomy
> Dept. of Physics and Astronomy
> Swarthmore College
> AstroPy mailing list
> AstroPy at python.org
More information about the AstroPy