#! /usr/bin/env python """ Author: Valentino Gonzalez MIT License Copyright (c) 2019 GREATS team. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import sys import numpy as np from scipy.ndimage import rotate from astropy.io import fits from astropy.wcs import WCS def _build_grid_wcs_for_map(header): """WCS structure to find best approximate positions in coarse grid. The psf reconstruction fits file provided with the GREATS data release has 2 extensions: the first one is the master psf at position angle 0; the second extension is a map that contains information on the position angles and weights of the multiple images that are combined. This information is available on a coarse grid. When the psf is requested at a given RA and DEC, it is necessary to find the point in the coarse grid that best matches the RA and DEC requested. For this, the map contains, in the header, WCS information to find this point. This function simplifies the header and creates a astropy.wcs.WCS structure to handle the transformation from RA and DEC to grid position. """ header['NAXIS'] = 2 del header['NAXIS4'] del header['NAXIS3'] wcs = WCS(header) return wcs def compose_psf_at(ra, dec, psf_fname): """Reconstructs the GREATS psf at a given RA and DEC. Parameters ---------- ra, dec : float Right Ascension and Declination in degrees. This is the positions at which the psf will be reconstructed. psf_fname : str The name of the fits file that contains the information to reconstruct psf for the GREATS data release. Returns ------- reconstructed_psf : np.ndarray 2D numpy array with the reconstructed psf. The pixel scale is the same as in the master psf in the fits file that contains the information to reconstruct the psf. The reconstructed psf is normalized. Example ------- >>> psf_filename = 'GREATS_GOODS-S_CH1_v1.2_psf.fits' >>> reconstructed_psf = compose_psf_at(53.0575093, -27.7037789, psf_filename) >>> reconstructed_psf.shape (81, 81) """ master_psf = fits.getdata(psf_fname, header=False, ext=1) psf_map, map_header = fits.getdata(psf_fname, header=True, ext=2) map_wcs = _build_grid_wcs_for_map(map_header) grid_positions = map_wcs.all_world2pix([[ra, dec]], 0) # We just round up to the closest coordinate in the grid but you may # consider keeping a finer grid and interpolating the weights and PAs. x, y = np.round(grid_positions).astype('int')[0] aor_weights = psf_map[:, 0, y, x] aor_pas = psf_map[:, 1, y, x] reconstructed_psf = np.zeros(master_psf.shape) for weight, pa in zip(aor_weights, aor_pas): if weight != 0: rotated_psf = rotate(master_psf, -pa, axes=(1, 0), reshape=False, output=None, order=3, mode='constant', cval=0.0, prefilter=True) reconstructed_psf += rotated_psf * weight reconstructed_psf = reconstructed_psf / np.sum(reconstructed_psf) return reconstructed_psf if __name__ == "__main__": reconstructed_psf = compose_psf_at(float(sys.argv[1]), float(sys.argv[2]), sys.argv[3]) hdu = fits.PrimaryHDU(reconstructed_psf) if len(sys.argv) == 5: fout=sys.argv[4] else: fout='psf.fits' hdu.writeto(fout, overwrite=True)