[AstroPy] Rebin FITS images and preserve or recalculate WCS?

Adam Ginsburg adam.g.ginsburg at gmail.com
Tue Aug 4 14:41:21 EDT 2020


I'm a bit late to this, but I think astropy handles a lot of this really
well internally with the wcs object.  You can resample any WCS object with:
wcs[::2, ::2]
for example, and it will do something at least close to correct (in other
words, check that it looks right!)

I had written downsampling code years ago in this standalone package, but
I'm sure it's outdated - I just can't find the astropy-specific tool that
supplanted it
https://fits-tools.readthedocs.io/en/latest/_modules/FITS_tools/downsample.html

I think the code I link there is just a more complicated version of Gary's
suggestion:
>> 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
but if you piece Gary's suggestion together with `wcs[::s, ::s]`, you
should be able to do this all with vetted tools.

On Tue, Aug 4, 2020 at 2:20 PM Kelle Cruz <kellecruz at gmail.com> wrote:

> Link to FB discussion:
> https://www.facebook.com/groups/astropython/permalink/2702447229999950/
>
> and link to a printout of the discussion:
>
> https://www.dropbox.com/s/zyy67kfemo03w66/%282%29%20Python%20users%20in%20Astronomy.pdf?dl=0
>
> Kelle
>
> --
> Kelle Cruz, PhD (she/her, they/them)
> Assoc. Professor, Physics and Astronomy, Hunter College
> CEO, ScienceBetter Consulting, LLC
> CFO, Startorialist, Inc. <http://shop.startorialist.com>
> Editor-in-Chief, AstroBetter Blog and Wiki <http://astrobetter.com>
> Director, McNulty Scholars Program, Hunter College
> Research Associate, American Museum of Natural History
> Visiting Scholar, Center for Computational Astrophysics
>
>
> On Tue, Aug 4, 2020 at 11:43 AM Slavin, Jonathan <jslavin at cfa.harvard.edu>
> wrote:
>
>> Hi Eric,
>>
>> I posted a very similar question to the Python users in Astronomy
>> Facebook group a few weeks ago (July 7). You might look up that discussion.
>> There were some interesting suggestions, though I wouldn't say any
>> definitive answers.
>>
>> Regards,
>> Jon
>>
>> On Tue, Aug 4, 2020 at 9:18 AM <astropy-request at python.org> wrote:
>>
>>> Today's Topics:
>>>
>>>    1. Re: Rebin FITS images and preserve or recalculate WCS?
>>>       (Eric Jensen)
>>>    2. Re: Rebin FITS images and preserve or recalculate WCS?
>>>       (Eric Jensen)
>>>
>>>
>>> ----------------------------------------------------------------------
>>>
>>> Message: 1
>>> Date: Tue, 4 Aug 2020 09:16:29 -0400
>>> From: Eric Jensen <ejensen1 at swarthmore.edu>
>>> To: Astronomical Python mailing list <astropy at python.org>
>>> Subject: Re: [AstroPy] Rebin FITS images and preserve or recalculate
>>>         WCS?
>>> Message-ID: <4A90B8D1-DCF3-4BCB-8D57-90E20781CC86 at swarthmore.edu>
>>> Content-Type: text/plain; charset="utf-8"
>>>
>>> Thanks, Gary and Petr!  I appreciate the answers so far.  A couple of
>>> additional notes:
>>>
>>> I should have been clear that I realize that the basics of the binning
>>> part are hard with numpy operations, though I appreciate the clear code
>>> from Gary.   There are possibly edge-of-image issues, but those are
>>> probably best handled by just restricting the binning factor to numbers
>>> that divide evenly into the array dimensions.
>>>
>>> However, there are potential bit-depth issues - if I add together
>>> several pixels with 50,000 counts (previously stored as 16-bit unsigned
>>> integers) I?ll need to change the bit depth.  Maybe astropy?s FITS-handling
>>> routines make that automatic when writing an array to a new FITS file.
>>>
>>> That said, it?s mostly the subtleties of the WCS part that I?d like to
>>> handle with vetted tools if possible.
>>>
>>> For example, if I look at a FITS header from an image solved by
>>> astrometry.net, it has not just CRVAL1,2 + CRPIX1,2 + a CD matrix, but
>>> it also has SIP distortion coefficients.  It?s possible that those are
>>> negligible for our images (I haven?t yet checked them to work out the
>>> details of the standard to figure out how much difference they make) and
>>> they could just be ignored.  (And our images straight off the telescope,
>>> using MaximDL, have some kind of proprietary/undocumented distortion
>>> correction scheme that I?ll just need to ignore anyway, I think, so maybe
>>> the distortion issue isn?t important.  I?ll have to test that.)
>>>
>>> But it highlights the general issue that WCS has lots of permutations,
>>> and it seems better to handle it (if possible) with tested tools such as
>>> already exist in astropy.  The question is whether there?s a useful way to
>>> deploy the astropy (or other library) WCS functionality here, or whether it
>>> is indeed best just to do it manually with the linear coefficients
>>> following the solution outlined by Gary and Petr already.
>>>
>>> One specific question:
>>>
>>> > On Aug 4, 2020, at 6:33 AM, Petr Kub?nek <petr at kubanek.net> wrote:
>>> >
>>> > And if you happen to have WCS in CD_ matrix, divide it by 2 (scalar,
>>> eg. all members).
>>>
>>> These would need to be *multiplied* by the binning factor, yes?
>>>
>>> If there is no general solution already implemented and tested, I
>>> certainly can dig into it more myself - I think the basic outline (minus
>>> distortion) is already given, modulo subtleties I haven?t though of.  But
>>> in general I?ve encouraged my students to use tested libraries rather than
>>> re-inventing the wheel when possible, so I?m trying to take my own advice!
>>> :-)
>>>
>>> Thanks for any additional thoughts,
>>>
>>> Eric
>>>
>>>
>>> > On Aug 4, 2020, at 6:33 AM, Petr Kub?nek <petr at kubanek.net> wrote:
>>> >
>>> > And if you happen to have WCS in CD_ matrix, divide it by 2 (scalar,
>>> eg. all members).
>>> >
>>> > Petr
>>> >
>>> >> 4. 8. 2020 v 11:44 dop., Bernstein, Gary M <garyb at PHYSICS.UPENN.EDU>:
>>> >>
>>> >> 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.
>>> >>
>>> >> Cheers,
>>> >> Gary
>>> >>
>>> >>> 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
>>> >>>
>>> >>> Eric Jensen
>>> >>> Professor of Astronomy
>>> >>> Dept. of Physics and Astronomy
>>> >>> Swarthmore College
>>> >>> _______________________________________________
>>> >>> AstroPy mailing list
>>> >>> AstroPy at python.org
>>> >>> https://mail.python.org/mailman/listinfo/astropy
>>> >>
>>> >> _______________________________________________
>>> >> AstroPy mailing list
>>> >> AstroPy at python.org
>>> >> https://mail.python.org/mailman/listinfo/astropy
>>> >
>>> > _______________________________________________
>>> > AstroPy mailing list
>>> > AstroPy at python.org
>>> > https://mail.python.org/mailman/listinfo/astropy
>>>
>>> -------------- next part --------------
>>> A non-text attachment was scrubbed...
>>> Name: smime.p7s
>>> Type: application/pkcs7-signature
>>> Size: 3946 bytes
>>> Desc: not available
>>> URL: <
>>> http://mail.python.org/pipermail/astropy/attachments/20200804/ba4e8cae/attachment-0001.bin
>>> >
>>>
>>> ------------------------------
>>>
>>> Message: 2
>>> Date: Tue, 4 Aug 2020 09:18:25 -0400
>>> From: Eric Jensen <ejensen1 at swarthmore.edu>
>>> To: Astronomical Python mailing list <astropy at python.org>
>>> Subject: Re: [AstroPy] Rebin FITS images and preserve or recalculate
>>>         WCS?
>>> Message-ID: <0799D0A8-35B3-4BF3-9F28-61CF2DC10E76 at swarthmore.edu>
>>> Content-Type: text/plain; charset="utf-8"
>>>
>>>
>>> > On Aug 4, 2020, at 9:16 AM, Eric Jensen <ejensen1 at swarthmore.edu>
>>> wrote:
>>> >
>>> >  I realize that the basics of the binning part are hard with numpy
>>> operations,
>>>
>>> Sorry, this should have said ?are *not* hard??
>>>
>>> -------------- next part --------------
>>> An HTML attachment was scrubbed...
>>> URL: <
>>> http://mail.python.org/pipermail/astropy/attachments/20200804/29285de5/attachment.html
>>> >
>>> -------------- next part --------------
>>> A non-text attachment was scrubbed...
>>> Name: smime.p7s
>>> Type: application/pkcs7-signature
>>> Size: 3946 bytes
>>> Desc: not available
>>> URL: <
>>> http://mail.python.org/pipermail/astropy/attachments/20200804/29285de5/attachment.bin
>>> >
>>>
>>> ------------------------------
>>>
>>> Subject: Digest Footer
>>>
>>> _______________________________________________
>>> AstroPy mailing list
>>> AstroPy at python.org
>>> https://mail.python.org/mailman/listinfo/astropy
>>>
>>>
>>> ------------------------------
>>>
>>> End of AstroPy Digest, Vol 167, Issue 2
>>> ***************************************
>>>
>>
>>
>> --
>> Jonathan D. Slavin
>> Astrophysicist - High Energy Astrophysics Division
>> Center for Astrophysics | Harvard & Smithsonian
>> Office: (617) 496-7981 | Cell: (781) 363-0035
>> 60 Garden Street | MS 83 | Cambridge, MA 02138
>>
>>
>> _______________________________________________
>> AstroPy mailing list
>> AstroPy at python.org
>> https://mail.python.org/mailman/listinfo/astropy
>>
> _______________________________________________
> AstroPy mailing list
> AstroPy at python.org
> https://mail.python.org/mailman/listinfo/astropy
>


-- 
Adam Ginsburg
Assistant Professor, Department of Astronomy
University of Florida, Gainesville
http://www.adamgginsburg.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/astropy/attachments/20200804/9c7344ee/attachment-0001.html>


More information about the AstroPy mailing list