reproject icon indicating copy to clipboard operation
reproject copied to clipboard

`reproject_and_coadd` is picky with WCS

Open meindertsma opened this issue 2 years ago • 4 comments

I'm trying to make a mosaic using reproject_and_coadd. I have fits files in Lum with proper wcs's, and want to use these for the rest of the filters as well, since they are all aligned, however I get the following error which confuses me:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-59-0b2ebe682520> in <module>
      8 for filt in filters:
      9     hdulist_list = [fits.open(combined_images[obj][filt]) for obj in objects] #list of images to combine in one filter
---> 10     data, footprint = reproject_and_coadd(hdulist_list, wcs, shape_out=shape, reproject_function=reproject_interp)
     11     display_image(data, 5, 99, scale='squared', figsize=(16, 12), title=f"Mosaic {filt}")
     12     write_to_fits(procdir, data, f"Mosaic_{filt}.fits", header=header)

/Software/users/modules/7/software/anaconda3/2020.07/lib/python3.8/site-packages/reproject/mosaicking/coadd.py in reproject_and_coadd(input_data, output_projection, shape_out, input_weights, hdu_in, reproject_function, hdu_weights, combine_function, match_background, background_reference, **kwargs)
    122         xc = np.array([-0.5, nx - 0.5, nx - 0.5, -0.5])
    123         yc = np.array([-0.5, -0.5, ny - 0.5, ny - 0.5])
--> 124         xc_out, yc_out = wcs_out.world_to_pixel(wcs_in.pixel_to_world(xc, yc))
    125 
    126         # Determine the cutout parameters

/Software/users/modules/7/software/anaconda3/2020.07/lib/python3.8/site-packages/astropy/wcs/wcsapi/high_level_api.py in world_to_pixel(self, *world_objects)
    183                 w = world_objects[ikey]
    184                 if not isinstance(w, klass):
--> 185                     raise ValueError("Expected the following order of world "
    186                                      "arguments: {}".format(', '.join([k.__name__ for (k, _, _) in classes.values()])))
    187 

ValueError: Expected the following order of world arguments: SkyCoord

I'm don't know where SkyCoord comes from or what I need to do with it, it is not in my fits files, and a list of one element doesn't have an ordering. Also, reproject_function is not a keyword argument of reproject_and_coadd, but is required, I assume this is temporary. Edit: It does work when I use the same files for the generating mosaic as for the wcs.

meindertsma avatar Jun 10 '22 20:06 meindertsma

I found a workaround: adding the WCS information from the fits files uploaded to astrometry.net to the HDUs of the corresponding fits files in other filters made the issue disappear. It does make the code a lot more complicated though.

meindertsma avatar Jun 11 '22 17:06 meindertsma

@meindertsma - is there any way you could share the files you are using with me? If so you could send a link to them to [email protected] - thanks!

astrofrog avatar Jun 14 '22 13:06 astrofrog

Hi, Using the reproject_and_coadd for a project where I have thousands of file to stack, I found the same issue which I was able to track to one file not having a proper WCS. In fact the file was a calibration observation and thus had a physical WCS but no celestial WCS. The minimal working example is :

files = [datadir+'acisf00065N004_full_img2.fits.gz', datadir+'acisf62130_000N003_full_img2.fits.gz']

hdr = fits.getheader(files[0])

mosaic, footprint = reproject_and_coadd(files, hdu_in=0, output_projection=hdr,
                                   reproject_function=reproject_interp, combine_function='sum')

The error printed is pretty cryptic

   215 w = world_objects[ikey]
    216 if not isinstance(w, klass):
--> 217     raise ValueError

ValueError: Expected the following order of world arguments: SkyCoord

I can add/send the files if needed.

I was able to fix the issue by a simple if wcs_in.has_celestial: in the coadd.py that ignores this file but maybe an Error should be showed. I could try a small PR if you think this is an ok solution.

What do you think @astrofrog ?

facero avatar Feb 28 '23 17:02 facero

An error would be great!

astrofrog avatar Feb 28 '23 18:02 astrofrog