astropy / reproject

Python-based Astronomical image reprojection :milky_way: - maintainer @astrofrog
https://reproject.readthedocs.io
BSD 3-Clause "New" or "Revised" License
106 stars 66 forks source link

reproject_interp with a distortion table #213

Open charvey2718 opened 4 years ago

charvey2718 commented 4 years ago

Dear all, Is reproject meant to be able to cope with distortion tables? If yes, I'm having an issue with that:

I have an HDUList as follows, which is the input to reproject_interp: No. Name Ver Type Cards Dimensions Format 0 PRIMARY 1 PrimaryHDU 34 (1280, 960) uint8 1 WCSDVARR 1 ImageHDU 15 (320, 240) float32 2 WCSDVARR 2 ImageHDU 15 (320, 240) float32

The first HDU contains the image data and a header which points to the distortion tables, and the second and third HDUs are the distortion tables themselves.

This results in:

Traceback (most recent call last): File "reproject-image.py", line 87, in array, footprint = reproject_interp(meteor_lens, w[0].header, shape_out=(width, height)) File "c:\Python36\lib\site-packages\astropy\utils\decorators.py", line 521, in wrapper return function(*args, **kwargs) File "c:\Python36\lib\site-packages\reproject\interpolation\high_level.py", line 77, in reproject_interp array_in, wcs_in = parse_input_data(input_data, hdu_in=hdu_in) File "c:\Python36\lib\site-packages\reproject\utils.py", line 26, in parse_input_data return parse_input_data(input_data[hdu_in]) File "c:\Python36\lib\site-packages\reproject\utils.py", line 28, in parse_input_data return input_data.data, WCS(input_data.header) File "c:\Python36\lib\site-packages\astropy\wcs\wcs.py", line 453, in init header, fobj, dist='CPDIS', err=minerr) File "c:\Python36\lib\site-packages\astropy\wcs\wcs.py", line 939, in _read_distortion_kw raise ValueError('an astropy.io.fits.HDUList is ' ValueError: an astropy.io.fits.HDUList is required for Lookup table distortion.

Working back up through the traceback, I can see that fobj is of type None in wcs.py at lines 453 and 939. I can see that at line 28 in utils.py,

If I replace the input to reproject_interp to something with no distortion tables, i.e. just a primary HDU, it works as expected.

astrofrog commented 4 years ago

@charvey2718 - is there any way you could provide an example script and input file(s)? If so, please send them to thomas.robitaille@gmai.com - thanks!

Caglow commented 4 years ago

I encountered the same problem. I attached an example script (example.py) that loads a FITS file with a distortion table (blank.fits.gz, which is an HST ACS/WFC frame, where I zeroed out the image data to make it fit in the 10MB upload limit, but left the distortion tables alone; the original is here), and attempts to reproject it onto an undistorted copy of the WCS: example.zip

Attempting to run example.py gives the same error as in the first post.

jmbhughes commented 4 months ago

I recently encountered this same bug with reproject_adaptive. Here's a minimal example:

from reproject import reproject_adaptive
from astropy import wcs
import numpy as np

example_wcs = wcs.WCS(naxis=2)

constant_distortion = wcs.DistortionLookupTable(
    np.ones((10, 10), dtype=np.float32),
    (0, 0),
    (0, 0),
    (1, 1)
)
example_wcs.cpdis1 = constant_distortion
example_wcs.cpdis2 = constant_distortion

hdul = example_wcs.to_fits()
hdul[0].data = np.zeros((100, 100), dtype=np.float32)

reproject_adaptive(hdul, example_wcs, shape_out=(100, 100))

This produces:

Traceback (most recent call last):
  File "/Users/jhughes/Desktop/repos/thuban/minimal_example_reproject.py", line 19, in <module>
    reproject_adaptive(hdul, example_wcs, shape_out=(100, 100))
  File "/Users/jhughes/Desktop/repos/thuban/.venv/lib/python3.10/site-packages/reproject/adaptive/high_level.py", line 227, in reproject_adaptive
    array_in, wcs_in = parse_input_data(input_data, hdu_in=hdu_in)
  File "/Users/jhughes/Desktop/repos/thuban/.venv/lib/python3.10/site-packages/reproject/utils.py", line 85, in parse_input_data
    return parse_input_data(input_data[hdu_in])
  File "/Users/jhughes/Desktop/repos/thuban/.venv/lib/python3.10/site-packages/reproject/utils.py", line 87, in parse_input_data
    return input_data.data, WCS(input_data.header)
  File "/Users/jhughes/Desktop/repos/thuban/.venv/lib/python3.10/site-packages/astropy/wcs/wcs.py", line 523, in __init__
    cpdis = self._read_distortion_kw(header, fobj, dist="CPDIS", err=minerr)
  File "/Users/jhughes/Desktop/repos/thuban/.venv/lib/python3.10/site-packages/astropy/wcs/wcs.py", line 1084, in _read_distortion_kw
    raise ValueError(
ValueError: an astropy.io.fits.HDUList is required for Lookup table distortion.

The easy work around is to just pass a data and wcs tuple in as reproject_adaptive((data, data_wcs), target_wcs, shape_out=(100, 100)). So it's not a problematic bug, but it caught me at first.

I think I understand how to fix it, so I may open a PR if no one has worked on this.