<div dir="ltr"><div>The following attempt to save a Cutout2D object to a FITS file:<br><br>########<br>from <a href="http://astropy.io">astropy.io</a> import fits<br>from astropy import wcs<br>from astropy.nddata import Cutout2D<br><br>f = fits.open('fitfile.fits')<br>w = wcs.WCS(f['image'].header)<br>#<br>position = (168,155)<br>shape = (20, 20)<br>cutout = Cutout2D(f['image'].data, position, shape, wcs=w)<br>#<br></div># block 2<br>#<br><div>hdu = fits.PrimaryHDU()<br>hdu.data = cutout.data<br>hdu.header = f['image'].header<br>hdu.header.update(cutout.wcs.to_header())<br>hdu.writeto('test_cutout.fits')<br>#########<br><div><div><div><div><div><br></div><div>fails with the following exception:<br></div><div><br>VerifyError: <br>Verification reported errors:<br>HDU 0:<br>    'SIMPLE' card does not exist.<br>Note: astropy.io.fits uses zero-based indexing.<br><br></div><div>While this one is OK:<br>#<br></div><div># block 2<br>#<br></div><div>hdu = fits.PrimaryHDU()<br>hdu2 = fits.ImageHDU(cutout.data)<br>hdu2.header = f['image'].header<br>hdu2.header.update(cutout.wcs.to_header())<br>hdul = fits.HDUList([hdu,hdu2])<br>hdul.writeto('test_cutout.fits')<br>########<br></div><br></div><div>Problem solved but it is probably a good idea to have a utility method to save directly a cutout object to a FITS...<br><br></div><div>Thanks,<br></div><div>Ivan<br></div><div><div><br></div></div></div></div></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, 13 Mar 2018 at 16:38 Thomas Boch <<a href="mailto:thomas.boch@astro.unistra.fr">thomas.boch@astro.unistra.fr</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  
    
  
  <div text="#000000" bgcolor="#FFFFFF">
    Hi Ivan,<br>
    <br>
    the Cutout2D object has attributes <i>data</i> and <i>wcs</i>
    which can be used to create an astropy.io.fits.PrimaryHDU instance
    that can then be written to a file.<br>
    <br>
    Cheers,<br>
    <br>
    Thomas</div><div text="#000000" bgcolor="#FFFFFF"><br>
    <br>
    <div class="m_9074580160900206332moz-cite-prefix">Le 13/03/2018 à 22:27, Ivan Valtchanov
      a écrit :<br>
    </div>
    </div><div text="#000000" bgcolor="#FFFFFF"><blockquote type="cite">
      <div dir="ltr">
        <div>
          <div>
            <div>
              <div>Hi all,<br>
                <br>
              </div>
              I've been trying to use<span class="m_9074580160900206332inbox-inbox-nn">
                astropy.nddata</span> <span class="m_9074580160900206332inbox-inbox-k"></span><span class="m_9074580160900206332inbox-inbox-n">Cutout2D</span> to crop a large
              image. I don't see how to save the cutout result to a FITS
              file. cutout.writeto('image.fits') does not work and in
              the API I cannot find a method in the Cutout2D class that
              saves the result to a FITS file. <br>
              <br>
            </div>
            is there a way to do it?<br>
            <br>
          </div>
          Thanks,<br>
        </div>
        Ivan Valtchanov<br>
      </div>
      <br>
      <fieldset class="m_9074580160900206332mimeAttachmentHeader"></fieldset>
      <br>
      </blockquote></div><div text="#000000" bgcolor="#FFFFFF"><blockquote type="cite"><pre>_______________________________________________
AstroPy mailing list
<a class="m_9074580160900206332moz-txt-link-abbreviated" href="mailto:AstroPy@python.org" target="_blank">AstroPy@python.org</a>
<a class="m_9074580160900206332moz-txt-link-freetext" href="https://mail.python.org/mailman/listinfo/astropy" target="_blank">https://mail.python.org/mailman/listinfo/astropy</a>
</pre>
    </blockquote>
    <br>
  </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>