<div dir="ltr">The astropy function to downsample an array by applying a user-supplied function (e.g., np.mean, np.sum) to local blocks is block_reduce (<a href="https://docs.astropy.org/en/latest/api/astropy.nddata.block_reduce.html">https://docs.astropy.org/en/latest/api/astropy.nddata.block_reduce.html</a>).  It does not handle the WCS.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Aug 4, 2020 at 2:41 PM Adam Ginsburg <<a href="mailto:adam.g.ginsburg@gmail.com">adam.g.ginsburg@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">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:<div>wcs[::2, ::2]</div><div>for example, and it will do something at least close to correct (in other words, check that it looks right!)</div><div><br></div><div>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</div><div><a href="https://fits-tools.readthedocs.io/en/latest/_modules/FITS_tools/downsample.html" target="_blank">https://fits-tools.readthedocs.io/en/latest/_modules/FITS_tools/downsample.html</a>  <br></div><div>I think the code I link there is just a more complicated version of Gary's suggestion:</div><div>>> m,n = img.shape<br>>> img = np.sum( img.reshape(m//s,s,n), axis=1)  # Contract along y<br>>> Img = np.sum( img.reshape(m//s,n//s,s),axis=2). # Contract along x </div><div>but if you piece Gary's suggestion together with `wcs[::s, ::s]`, you should be able to do this all with vetted tools. <br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Aug 4, 2020 at 2:20 PM Kelle Cruz <<a href="mailto:kellecruz@gmail.com" target="_blank">kellecruz@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div style="color:rgb(0,0,0)">Link to FB discussion: <a href="https://www.facebook.com/groups/astropython/permalink/2702447229999950/" target="_blank">https://www.facebook.com/groups/astropython/permalink/2702447229999950/</a></div><div style="color:rgb(0,0,0)"><br></div><div style="color:rgb(0,0,0)">and link to a printout of the discussion: </div><div style="color:rgb(0,0,0)"><span style="color:rgb(34,34,34)"><a href="https://www.dropbox.com/s/zyy67kfemo03w66/%282%29%20Python%20users%20in%20Astronomy.pdf?dl=0" target="_blank">https://www.dropbox.com/s/zyy67kfemo03w66/%282%29%20Python%20users%20in%20Astronomy.pdf?dl=0</a></span></div></div><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div><br></div><div>Kelle</div><div><br></div><div>--</div><div>Kelle Cruz, PhD (she/her, they/them)</div><div><div>Assoc. Professor, Physics and Astronomy, Hunter College<br></div><div>CEO, ScienceBetter Consulting, LLC</div><div>CFO, <a href="http://shop.startorialist.com" target="_blank">Startorialist, Inc.</a></div><div>Editor-in-Chief, <a href="http://astrobetter.com" target="_blank">AstroBetter Blog and Wiki</a></div><div>Director, McNulty Scholars Program, Hunter College</div><div>Research Associate, American Museum of Natural History</div><div>Visiting Scholar, Center for Computational Astrophysics<br></div></div></div></div></div></div></div></div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Aug 4, 2020 at 11:43 AM Slavin, Jonathan <<a href="mailto:jslavin@cfa.harvard.edu" target="_blank">jslavin@cfa.harvard.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div class="gmail_default" style="font-family:tahoma,sans-serif;font-size:small">Hi Eric,</div><div class="gmail_default" style="font-family:tahoma,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;font-size:small">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.</div><div class="gmail_default" style="font-family:tahoma,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;font-size:small">Regards,</div><div class="gmail_default" style="font-family:tahoma,sans-serif;font-size:small">Jon</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Aug 4, 2020 at 9:18 AM <<a href="mailto:astropy-request@python.org" target="_blank">astropy-request@python.org</a>> wrote:</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Today's Topics:<br>
<br>
   1. Re: Rebin FITS images and preserve or recalculate WCS?<br>
      (Eric Jensen)<br>
   2. Re: Rebin FITS images and preserve or recalculate WCS?<br>
      (Eric Jensen)<br>
<br>
<br>
----------------------------------------------------------------------<br>
<br>
Message: 1<br>
Date: Tue, 4 Aug 2020 09:16:29 -0400<br>
From: Eric Jensen <<a href="mailto:ejensen1@swarthmore.edu" target="_blank">ejensen1@swarthmore.edu</a>><br>
To: Astronomical Python mailing list <<a href="mailto:astropy@python.org" target="_blank">astropy@python.org</a>><br>
Subject: Re: [AstroPy] Rebin FITS images and preserve or recalculate<br>
        WCS?<br>
Message-ID: <<a href="mailto:4A90B8D1-DCF3-4BCB-8D57-90E20781CC86@swarthmore.edu" target="_blank">4A90B8D1-DCF3-4BCB-8D57-90E20781CC86@swarthmore.edu</a>><br>
Content-Type: text/plain; charset="utf-8"<br>
<br>
Thanks, Gary and Petr!  I appreciate the answers so far.  A couple of additional notes:<br>
<br>
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.  <br>
<br>
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.  <br>
<br>
That said, it?s mostly the subtleties of the WCS part that I?d like to handle with vetted tools if possible.  <br>
<br>
For example, if I look at a FITS header from an image solved by <a href="http://astrometry.net" rel="noreferrer" target="_blank">astrometry.net</a>, 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.) <br>
<br>
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. <br>
<br>
One specific question: <br>
<br>
> On Aug 4, 2020, at 6:33 AM, Petr Kub?nek <<a href="mailto:petr@kubanek.net" target="_blank">petr@kubanek.net</a>> wrote:<br>
> <br>
> And if you happen to have WCS in CD_ matrix, divide it by 2 (scalar, eg. all members).<br>
<br>
These would need to be *multiplied* by the binning factor, yes? <br>
<br>
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!  :-) <br>
<br>
Thanks for any additional thoughts,<br>
<br>
Eric<br>
<br>
<br>
> On Aug 4, 2020, at 6:33 AM, Petr Kub?nek <<a href="mailto:petr@kubanek.net" target="_blank">petr@kubanek.net</a>> wrote:<br>
> <br>
> And if you happen to have WCS in CD_ matrix, divide it by 2 (scalar, eg. all members).<br>
> <br>
> Petr<br>
> <br>
>> 4. 8. 2020 v 11:44 dop., Bernstein, Gary M <<a href="mailto:garyb@PHYSICS.UPENN.EDU" target="_blank">garyb@PHYSICS.UPENN.EDU</a>>:<br>
>> <br>
>> Hi Eric -<br>
>> 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<br>
>> <br>
>> m,n = img.shape<br>
>> img = np.sum( img.reshape(m//s,s,n), axis=1)  # Contract along y<br>
>> Img = np.sum( img.reshape(m//s,n//s,s),axis=2). # Contract along x<br>
>> <br>
>> 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<br>
>> <br>
>> I think this should work universally because the linear transform is always the first step of a WCS mapping.<br>
>> <br>
>> Cheers,<br>
>> Gary<br>
>> <br>
>>> On Aug 3, 2020, at 11:01 PM, Eric Jensen <<a href="mailto:ejensen1@swarthmore.edu" target="_blank">ejensen1@swarthmore.edu</a>> wrote:<br>
>>> <br>
>>> Hi all, <br>
>>> <br>
>>> 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. <br>
>>> <br>
>>> 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.  <br>
>>> <br>
>>> 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.) <br>
>>> <br>
>>> Thanks for any thoughts, <br>
>>> <br>
>>> Eric<br>
>>> <br>
>>> Eric Jensen<br>
>>> Professor of Astronomy<br>
>>> Dept. of Physics and Astronomy<br>
>>> Swarthmore College<br>
>>> _______________________________________________<br>
>>> AstroPy mailing list<br>
>>> <a href="mailto:AstroPy@python.org" target="_blank">AstroPy@python.org</a><br>
>>> <a href="https://mail.python.org/mailman/listinfo/astropy" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/astropy</a><br>
>> <br>
>> _______________________________________________<br>
>> AstroPy mailing list<br>
>> <a href="mailto:AstroPy@python.org" target="_blank">AstroPy@python.org</a><br>
>> <a href="https://mail.python.org/mailman/listinfo/astropy" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/astropy</a><br>
> <br>
> _______________________________________________<br>
> AstroPy mailing list<br>
> <a href="mailto:AstroPy@python.org" target="_blank">AstroPy@python.org</a><br>
> <a href="https://mail.python.org/mailman/listinfo/astropy" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/astropy</a><br>
<br>
-------------- next part --------------<br>
A non-text attachment was scrubbed...<br>
Name: smime.p7s<br>
Type: application/pkcs7-signature<br>
Size: 3946 bytes<br>
Desc: not available<br>
URL: <<a href="http://mail.python.org/pipermail/astropy/attachments/20200804/ba4e8cae/attachment-0001.bin" rel="noreferrer" target="_blank">http://mail.python.org/pipermail/astropy/attachments/20200804/ba4e8cae/attachment-0001.bin</a>><br>
<br>
------------------------------<br>
<br>
Message: 2<br>
Date: Tue, 4 Aug 2020 09:18:25 -0400<br>
From: Eric Jensen <<a href="mailto:ejensen1@swarthmore.edu" target="_blank">ejensen1@swarthmore.edu</a>><br>
To: Astronomical Python mailing list <<a href="mailto:astropy@python.org" target="_blank">astropy@python.org</a>><br>
Subject: Re: [AstroPy] Rebin FITS images and preserve or recalculate<br>
        WCS?<br>
Message-ID: <<a href="mailto:0799D0A8-35B3-4BF3-9F28-61CF2DC10E76@swarthmore.edu" target="_blank">0799D0A8-35B3-4BF3-9F28-61CF2DC10E76@swarthmore.edu</a>><br>
Content-Type: text/plain; charset="utf-8"<br>
<br>
<br>
> On Aug 4, 2020, at 9:16 AM, Eric Jensen <<a href="mailto:ejensen1@swarthmore.edu" target="_blank">ejensen1@swarthmore.edu</a>> wrote:<br>
> <br>
>  I realize that the basics of the binning part are hard with numpy operations, <br>
<br>
Sorry, this should have said ?are *not* hard?? <br>
<br>
-------------- next part --------------<br>
An HTML attachment was scrubbed...<br>
URL: <<a href="http://mail.python.org/pipermail/astropy/attachments/20200804/29285de5/attachment.html" rel="noreferrer" target="_blank">http://mail.python.org/pipermail/astropy/attachments/20200804/29285de5/attachment.html</a>><br>
-------------- next part --------------<br>
A non-text attachment was scrubbed...<br>
Name: smime.p7s<br>
Type: application/pkcs7-signature<br>
Size: 3946 bytes<br>
Desc: not available<br>
URL: <<a href="http://mail.python.org/pipermail/astropy/attachments/20200804/29285de5/attachment.bin" rel="noreferrer" target="_blank">http://mail.python.org/pipermail/astropy/attachments/20200804/29285de5/attachment.bin</a>><br>
<br>
------------------------------<br>
<br>
Subject: Digest Footer<br>
<br>
_______________________________________________<br>
AstroPy mailing list<br>
<a href="mailto:AstroPy@python.org" target="_blank">AstroPy@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/astropy" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/astropy</a><br>
<br>
<br>
------------------------------<br>
<br>
End of AstroPy Digest, Vol 167, Issue 2<br>
***************************************<br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><img width="350" height="2" style="font-size: 12.6667px; font-family: Arial; white-space: pre-wrap; border: none;"></div><div dir="ltr">Jonathan D. Slavin</div><div dir="ltr">Astrophysicist - High Energy Astrophysics Division</div><div dir="ltr">Center for Astrophysics | Harvard & Smithsonian</div><div dir="ltr">Office: (617) 496-7981 | Cell: (781) 363-0035<br>60 Garden Street | MS 83 | Cambridge, MA 02138</div></div><div dir="ltr"><span><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><span style="font-size:9.5pt;font-family:Arial;background-color:transparent;vertical-align:baseline;white-space:pre-wrap"><img width="350" height="2" style="border: none;"></span><span style="font-size:9.5pt;font-family:Arial;background-color:transparent;vertical-align:baseline;white-space:pre-wrap"><br></span></p></span></div></div></div></div></div></div></div></div>
_______________________________________________<br>
AstroPy mailing list<br>
<a href="mailto:AstroPy@python.org" target="_blank">AstroPy@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/astropy" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/astropy</a><br>
</blockquote></div>
_______________________________________________<br>
AstroPy mailing list<br>
<a href="mailto:AstroPy@python.org" target="_blank">AstroPy@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/astropy" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/astropy</a><br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div>Adam Ginsburg<br>Assistant Professor, Department of Astronomy</div><div>University of Florida, Gainesville</div><div><a href="http://www.adamgginsburg.com/" target="_blank">http://www.adamgginsburg.com/</a></div></div></div></div></div></div></div></div></div></div></div>
_______________________________________________<br>
AstroPy mailing list<br>
<a href="mailto:AstroPy@python.org" target="_blank">AstroPy@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/astropy" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/astropy</a><br>
</blockquote></div>