added multires tutorial files
Browse files
multiresolution_tutorial/data.fits.gz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:834d93bee156e7bb0e6bf28c8be394beba6e246d1512b002097e684b1fa46616
|
| 3 |
+
size 375290
|
multiresolution_tutorial/get_source_catalog.py
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
from jax import vmap
|
| 4 |
+
import jax.numpy as jnp
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
import astropy.io.fits as fits
|
| 8 |
+
from astropy.wcs import WCS
|
| 9 |
+
|
| 10 |
+
import scarlet2
|
| 11 |
+
from scarlet2 import Starlet
|
| 12 |
+
|
| 13 |
+
# Installing data package if not already installed
|
| 14 |
+
from scarlet2.utils import import_scarlet_test_data
|
| 15 |
+
|
| 16 |
+
import_scarlet_test_data()
|
| 17 |
+
from scarlet_test_data import data_path
|
| 18 |
+
|
| 19 |
+
import sep
|
| 20 |
+
|
| 21 |
+
from astropy.table import Table
|
| 22 |
+
|
| 23 |
+
# Load the HSC image data
|
| 24 |
+
obs_hdu = fits.open(os.path.join(data_path, "test_resampling", "Cut_HSC1.fits"))
|
| 25 |
+
data_hsc = obs_hdu[0].data.astype('float32')
|
| 26 |
+
wcs_hsc = WCS(obs_hdu[0].header)
|
| 27 |
+
channels_hsc = ['g', 'r', 'i', 'z', 'y']
|
| 28 |
+
|
| 29 |
+
# Load the HSC PSF data
|
| 30 |
+
psf_hsc_data = fits.open(os.path.join(data_path, "test_resampling", "PSF_HSC.fits"))[0].data.astype('float32')
|
| 31 |
+
psf_hsc = scarlet2.ArrayPSF(psf_hsc_data)
|
| 32 |
+
|
| 33 |
+
# Load the HST image data
|
| 34 |
+
hst_hdu = fits.open(os.path.join(data_path, "test_resampling", "Cut_HST1.fits"))
|
| 35 |
+
data_hst = hst_hdu[0].data.astype('float32')
|
| 36 |
+
wcs_hst = WCS(hst_hdu[0].header)
|
| 37 |
+
channels_hst = ['F814W']
|
| 38 |
+
|
| 39 |
+
# Load the HST PSF data
|
| 40 |
+
psf_hst_data = fits.open(os.path.join(data_path, "test_resampling", "PSF_HST.fits"))[0].data.astype('float32')
|
| 41 |
+
psf_hst_data = psf_hst_data[None, :, :]
|
| 42 |
+
psf_hst = scarlet2.ArrayPSF(psf_hst_data)
|
| 43 |
+
|
| 44 |
+
# Scale the HST data
|
| 45 |
+
data_hst = data_hst[None, ...].astype('float32')
|
| 46 |
+
data_hst *= data_hsc.max() / data_hst.max()
|
| 47 |
+
|
| 48 |
+
"""
|
| 49 |
+
Next we have to create a source catalog for the images. We’ll use `sep` for that,
|
| 50 |
+
but any other detection method will do. Since HST is higher resolution and less affected by blending,
|
| 51 |
+
we use it for detection but we also run detection on the HSC image to calculate the background RMS
|
| 52 |
+
"""
|
| 53 |
+
|
| 54 |
+
def makeCatalog(data_lr, data_hr, lvl=3, wave=True):
|
| 55 |
+
# Create a catalog of detected source by running SEP on the wavelet transform
|
| 56 |
+
# of the sum of the high resolution images and the low resolution images interpolated
|
| 57 |
+
# to the high resolution grid
|
| 58 |
+
|
| 59 |
+
coords_in = jnp.stack(jnp.meshgrid(jnp.linspace(0, 1, data_lr.shape[-1] + 2)[1:-1],
|
| 60 |
+
jnp.linspace(0, 1, data_lr.shape[-2] + 2)[1:-1]), -1)
|
| 61 |
+
|
| 62 |
+
coords_out = jnp.stack(jnp.meshgrid(jnp.linspace(0, 1, data_hr.shape[-2] + 2)[1:-1],
|
| 63 |
+
jnp.linspace(0, 1, data_hr.shape[-2] + 2)[1:-1]), -1)
|
| 64 |
+
|
| 65 |
+
interp_im = vmap(scarlet2.interpolation.resample2d,
|
| 66 |
+
in_axes=(0, None, None))(data_lr, coords_in, coords_out)
|
| 67 |
+
|
| 68 |
+
# Normalisation
|
| 69 |
+
interp_im = interp_im / jnp.sum(interp_im, axis=(1, 2))[:, None, None]
|
| 70 |
+
hr_images = data_hr / jnp.sum(data_hr, axis=(1, 2))[:, None, None]
|
| 71 |
+
|
| 72 |
+
# Summation to create a detection image
|
| 73 |
+
detect_image = jnp.sum(interp_im, axis=0) + jnp.sum(hr_images, axis=0)
|
| 74 |
+
# Rescaling to HR image flux
|
| 75 |
+
detect_image *= jnp.sum(data_hr)
|
| 76 |
+
# Wavelet transform
|
| 77 |
+
wave_detect = Starlet.from_image(detect_image).coefficients
|
| 78 |
+
|
| 79 |
+
if wave:
|
| 80 |
+
# Creates detection from the first 3 wavelet levels
|
| 81 |
+
detect = wave_detect[:lvl, :, :].sum(axis=0)
|
| 82 |
+
else:
|
| 83 |
+
detect = detect_image
|
| 84 |
+
|
| 85 |
+
# Runs SEP detection
|
| 86 |
+
bkg = sep.Background(np.array(detect))
|
| 87 |
+
catalog = sep.extract(np.array(detect), 3, err=bkg.globalrms)
|
| 88 |
+
bg_rms = []
|
| 89 |
+
for img in [np.array(data_lr), np.array(data_hr)]:
|
| 90 |
+
if np.size(img.shape) == 3:
|
| 91 |
+
bg_rms.append(np.array([sep.Background(band).globalrms for band in img]))
|
| 92 |
+
else:
|
| 93 |
+
bg_rms.append(sep.Background(img).globalrms)
|
| 94 |
+
|
| 95 |
+
return catalog, bg_rms, detect_image
|
| 96 |
+
|
| 97 |
+
# Making catalog.
|
| 98 |
+
# With the wavelet option on, only the first 3 wavelet levels are used for detection. Set to 1 for better detection
|
| 99 |
+
wave = 1
|
| 100 |
+
lvl = 3
|
| 101 |
+
catalog_hst, (bg_hsc, bg_hst), detect = makeCatalog(data_hsc, data_hst, lvl, wave)
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
# we can now set the empirical noise rms for both observations
|
| 105 |
+
obs_hst_weights = np.ones(data_hst.shape) / (bg_hst ** 2)[:, None, None]
|
| 106 |
+
obs_hsc_weights = np.ones(data_hsc.shape) / (bg_hsc ** 2)[:, None, None]
|
| 107 |
+
|
| 108 |
+
obs_hst = scarlet2.Observation(data_hst,
|
| 109 |
+
wcs=wcs_hst,
|
| 110 |
+
psf=psf_hst,
|
| 111 |
+
channels=channels_hst,
|
| 112 |
+
weights=obs_hst_weights)
|
| 113 |
+
|
| 114 |
+
pixel_hst = np.stack((catalog_hst['y'], catalog_hst['x']), axis=1)
|
| 115 |
+
# Convert the HST pixel coordinates to sky coordinates
|
| 116 |
+
coords = obs_hst.frame.get_sky_coord(pixel_hst)
|
| 117 |
+
|
| 118 |
+
# Saving everything in a single fits file
|
| 119 |
+
|
| 120 |
+
# Coordinates table (FK5 J2000.0)
|
| 121 |
+
table = Table([coords.ra.deg, coords.dec.deg], names=('RA', 'DEC'))
|
| 122 |
+
coord_hdu = fits.BinTableHDU(data=table)
|
| 123 |
+
coord_hdu.header['RADECSYS'] = coords.frame.name.upper() # 'FK5'
|
| 124 |
+
coord_hdu.header['EQUINOX'] = coords.equinox.jyear # 2000.0
|
| 125 |
+
|
| 126 |
+
# Build HDUs images
|
| 127 |
+
primary_hdu = fits.PrimaryHDU(data=data_hsc, header=wcs_hsc.to_header()) # Observation 1 Image
|
| 128 |
+
primary_hdu.header['EXTNAME'] = 'HSC_OBS'
|
| 129 |
+
|
| 130 |
+
psf1_hdu = fits.ImageHDU(data=psf_hsc_data)
|
| 131 |
+
psf1_hdu.header['EXTNAME'] = 'HSC_PSF'
|
| 132 |
+
|
| 133 |
+
weight1_hdu = fits.ImageHDU(data=obs_hsc_weights)
|
| 134 |
+
weight1_hdu.header['EXTNAME'] = 'HSC_WEIGHTS'
|
| 135 |
+
|
| 136 |
+
image2_hdu = fits.ImageHDU(data=data_hst, header=wcs_hst.to_header())
|
| 137 |
+
image2_hdu.header['EXTNAME'] = 'HST_OBS'
|
| 138 |
+
|
| 139 |
+
psf2_hdu = fits.ImageHDU(data=psf_hst_data)
|
| 140 |
+
psf2_hdu.header['EXTNAME'] = 'HST_PSF'
|
| 141 |
+
|
| 142 |
+
weight2_hdu = fits.ImageHDU(data=obs_hst_weights)
|
| 143 |
+
weight2_hdu.header['EXTNAME'] = 'HST_WEIGHTS'
|
| 144 |
+
|
| 145 |
+
coord_hdu.header['EXTNAME'] = 'CATALOG'
|
| 146 |
+
|
| 147 |
+
# Bundle all HDUs
|
| 148 |
+
hdulist = fits.HDUList([
|
| 149 |
+
primary_hdu,
|
| 150 |
+
psf1_hdu,
|
| 151 |
+
weight1_hdu,
|
| 152 |
+
image2_hdu,
|
| 153 |
+
psf2_hdu,
|
| 154 |
+
weight2_hdu,
|
| 155 |
+
coord_hdu
|
| 156 |
+
])
|
| 157 |
+
|
| 158 |
+
hdulist.writeto('multiresolution_tutorial_data.fits', overwrite=True)
|