def _vega_source(mag=0, x=0, y=0): specs = [vega_spectrum(mag)] tbl = Table(names=["x", "y", "ref", "weight"], data=[[x] * u.arcsec, [y] * u.arcsec, [0], [1]]) tbl_source = Source(table=tbl, spectra=specs) return tbl_source
def test_initialises_with_table_and_2_spectrum(self, ii, input_tables, input_spectra): table = input_tables[ii] src = Source(table=table, spectra=input_spectra) assert isinstance(src, Source) assert isinstance(src.spectra[0], SourceSpectrum) assert isinstance(src.fields[0], Table)
def image_source(): n = 50 unit = u.Unit("ph s-1 m-2 um-1") wave = np.linspace(0.5, 2.5, n) * u.um specs = [ SourceSpectrum(Empirical1D, points=wave, lookup_table=np.linspace(0, 4, n) * unit) ] n = 50 im_wcs = wcs.WCS(naxis=2) im_wcs.wcs.cunit = [u.arcsec, u.arcsec] im_wcs.wcs.cdelt = [0.2, 0.2] im_wcs.wcs.crval = [0, 0] im_wcs.wcs.crpix = [n // 2, n // 2] im_wcs.wcs.ctype = ["RA---TAN", "DEC--TAN"] im = np.ones((n + 1, n + 1)) * 1E-11 im[0, n] += 5 im[n, 0] += 5 im[n // 2, n // 2] += 10 im_hdu = fits.ImageHDU(data=im, header=im_wcs.to_header()) im_hdu.header["SPEC_REF"] = 0 im_source = Source(image_hdu=im_hdu, spectra=specs) return im_source
def _unity_source(dx=0, dy=0, angle=0, weight=1, n=100): unit = u.Unit("ph s-1 m-2 um-1") wave = np.linspace(0.5, 2.5, n) * u.um specs = [ SourceSpectrum(Empirical1D, points=wave, lookup_table=np.ones(n) * unit) ] im_wcs = wcs.WCS(naxis=2) im_wcs.wcs.cunit = [u.arcsec, u.arcsec] im_wcs.wcs.cdelt = [1, 1] im_wcs.wcs.crval = [0, 0] im_wcs.wcs.crpix = [n / 2, n / 2] im_wcs.wcs.ctype = ["RA---TAN", "DEC--TAN"] im = np.ones((n, n)) im_hdu = fits.ImageHDU(data=im, header=im_wcs.to_header()) im_hdu.header["SPEC_REF"] = 0 im_source = Source(image_hdu=im_hdu, spectra=specs) angle = angle * np.pi / 180 im_source.fields[0].header["CRVAL1"] += dx * u.arcsec.to(u.deg) im_source.fields[0].header["CRVAL2"] += dy * u.arcsec.to(u.deg) im_source.fields[0].header["PC1_1"] = np.cos(angle) im_source.fields[0].header["PC1_2"] = np.sin(angle) im_source.fields[0].header["PC2_1"] = -np.sin(angle) im_source.fields[0].header["PC2_2"] = np.cos(angle) im_source.fields[0].data *= weight return im_source
def test_initialises_with_filename_and_spectrum(self, ii, dtype, input_files, input_spectra): fname = input_files[ii] src = Source(filename=fname, spectra=input_spectra) assert isinstance(src, Source) assert isinstance(src.spectra[0], SourceSpectrum) assert isinstance(src.fields[0], dtype)
def test_initialises_with_only_imagehdu_and_arcsec2(self): hdu = fits.ImageHDU(data=np.ones([3, 3])) hdu.header["BUNIT"] = "Jy/arcsec2" hdu.header["CDELT1"] = 0.1 hdu.header["CUNIT1"] = "arcsec" hdu.header["CDELT2"] = 0.1 hdu.header["CUNIT2"] = "arcsec" src = Source(image_hdu=hdu) assert isinstance(src, Source) assert isinstance(src.spectra[0], SourceSpectrum) assert isinstance(src.fields[0], fits.ImageHDU)
def _image_source(dx=0, dy=0, angle=0, weight=1): """ An image with 3 point sources on a random BG Parameters ---------- dx, dy : float [arcsec] Offset from optical axis angle : float [deg] weight : float Returns ------- """ n = 101 unit = u.Unit("ph s-1 m-2 um-1") wave = np.linspace(0.5, 2.5, n) * u.um specs = [ SourceSpectrum(Empirical1D, points=wave, lookup_table=np.linspace(0, 4, n) * unit) ] n = 50 im_wcs = wcs.WCS(naxis=2) im_wcs.wcs.cunit = [u.arcsec, u.arcsec] im_wcs.wcs.cdelt = [0.2, 0.2] im_wcs.wcs.crval = [0, 0] im_wcs.wcs.crpix = [n // 2, n // 2] im_wcs.wcs.ctype = ["RA---TAN", "DEC--TAN"] im = np.random.random(size=(n + 1, n + 1)) * 1e-9 * weight im[n - 1, 1] += 5 * weight im[1, 1] += 5 * weight im[n // 2, n // 2] += 10 * weight im[n // 2, n - 1] += 5 * weight im_hdu = fits.ImageHDU(data=im, header=im_wcs.to_header()) im_hdu.header["SPEC_REF"] = 0 im_source = Source(image_hdu=im_hdu, spectra=specs) angle = angle * np.pi / 180 im_source.fields[0].header["CRVAL1"] += dx / 3600 im_source.fields[0].header["CRVAL2"] += dy / 3600 im_source.fields[0].header["PC1_1"] = np.cos(angle) im_source.fields[0].header["PC1_2"] = np.sin(angle) im_source.fields[0].header["PC2_1"] = -np.sin(angle) im_source.fields[0].header["PC2_2"] = np.cos(angle) return im_source
def _single_table_source(weight=1, n=3): unit = u.Unit("ph s-1 m-2 um-1") wave = np.linspace(0.5, 2.5, n) * u.um specs = [ SourceSpectrum(Empirical1D, points=wave, lookup_table=np.ones(n) * weight * unit) ] tbl = Table(names=["x", "y", "ref", "weight"], data=[[0] * u.arcsec, [0] * u.arcsec, [0], [1]]) tbl_source = Source(table=tbl, spectra=specs) return tbl_source
def _empty_sky(): n = 3 unit = u.Unit("ph s-1 m-2 um-1") wave = np.linspace(0.5, 2.5, n) * u.um specs = [ SourceSpectrum(Empirical1D, points=wave, lookup_table=np.zeros(n) * unit) ] tbl = Table(names=["x", "y", "ref", "weight"], data=[[0], [0], [0], [0]]) tbl_source = Source(table=tbl, spectra=specs) return tbl_source
def test_initialised_with_old_style_arrays(self): x, y = [0, 1], [0, -1] ref, weight = [0, 0], [1, 10] lam = np.linspace(0.5, 2.5, 11) * u.um spectra = np.ones(11) * PHOTLAM src = Source(x=x, y=y, ref=ref, weight=weight, lam=lam, spectra=spectra) assert isinstance(src, Source) assert isinstance(src.spectra[0], SourceSpectrum) assert isinstance(src.fields[0], Table)
def _fits_image_source(): n = 50 unit = u.Unit("ph s-1 m-2 um-1") wave = np.linspace(0.5, 2.5, n) * u.um specs = [ SourceSpectrum(Empirical1D, points=wave, lookup_table=np.linspace(0, 4, n) * unit) ] hdulist = fits.open(os.path.join(FILES_PATH, "test_image.fits")) fits_src = Source(image_hdu=hdulist[0], spectra=specs) return fits_src
def yaml_file_can_be_loaded_into_optical_train(self): # .. todo: get this working on Travis filename = os.path.join(TEST_PATH, "MICADO_SCAO_WIDE_2.yaml") cmds = UserCommands(yamls=[filename]) assert isinstance(cmds, UserCommands) assert isinstance(cmds.yaml_dicts, list) psf_file = cmds.yaml_dicts[1]["effects"][0]["kwargs"]["filename"] if find_file(psf_file) is None: new_file = "test_FVPSF.fits" cmds.yaml_dicts[1]["effects"][0]["kwargs"]["filename"] = new_file opt = OpticalTrain(cmds=cmds) assert isinstance(opt, OpticalTrain) assert isinstance(opt.optics_manager, OpticsManager) assert isinstance( opt.optics_manager.get_all(efs.FieldVaryingPSF)[0], efs.FieldVaryingPSF) assert isinstance(opt.image_plane, ImagePlane) assert opt.image_plane.hdu.header["NAXIS1"] >= 4096 assert isinstance(opt.fov_manager, FOVManager) assert len(opt.fov_manager.fovs) == 64 if PLOTS: for fov in opt.fov_manager.fovs: sky_cnrs, det_cnrs = fov.corners plt.plot(sky_cnrs[0], sky_cnrs[1]) plt.show() r = np.arange(-25, 25) x, y = np.meshgrid(r, r) x = x.flatten() * u.arcsec y = y.flatten() * u.arcsec ref = [0] * len(x) weight = [1] * len(x) spec = sp.SourceSpectrum(sp.Empirical1D, points=[0.5, 3.0] * u.um, lookup_table=[1e3, 1e3] * u.Unit("ph s-1 m-2 um-1")) src = Source(x=x, y=y, ref=ref, weight=weight, spectra=[spec]) opt.observe(src) if PLOTS: plt.imshow(opt.image_plane.image.T, origin="lower", norm=LogNorm()) plt.colorbar() plt.show()
def _table_source(): n = 101 unit = u.Unit("ph s-1 m-2 um-1") wave = np.linspace(0.5, 2.5, n) * u.um specs = [ SourceSpectrum(Empirical1D, points=wave, lookup_table=4 * np.ones(n) * unit), SourceSpectrum(Empirical1D, points=wave, lookup_table=np.linspace(0, 4, n) * unit), SourceSpectrum(Empirical1D, points=wave, lookup_table=np.linspace(0, 4, n)[::-1] * unit) ] tbl = Table(names=["x", "y", "ref", "weight"], data=[[5, 0, -5, 0] * u.arcsec, [5, -10, 5, 0] * u.arcsec, [2, 0, 1, 0], [1, 1, 1, 2]]) tbl_source = Source(table=tbl, spectra=specs) return tbl_source
def mock_extended_source_object(): from scipy.misc import face im = face()[::-1, :, 0] im = im / np.max(im) hdr = { "CDELT1": 0.1, "CDELT2": 0.1, "CRVAL1": 0., "CRVAL2": 0., "CRPIX1": im.shape[1] / 2, "CRPIX2": im.shape[0] / 2, "CTYPE1": "LINEAR", "CTYPE2": "LINEAR", "CUNIT1": "ARCSEC", "CUNIT2": "ARCSEC" } hdu = fits.ImageHDU(data=im, header=fits.Header(hdr)) spec = SourceSpectrum(Empirical1D, points=[1.0, 2.5] * u.um, lookup_table=[1, 1] * PHOTLAM) src = Source(image_hdu=hdu, spectra=spec) return src
def mock_point_source_object(): """ Three point sources, 1 up and 2 below the optical axis, fit inside the above defined apertures """ wave = [1.0, 2.5] * u.um flat = SourceSpectrum(Empirical1D, points=wave, lookup_table=[100, 100] * PHOTLAM) down = SourceSpectrum(Empirical1D, points=wave, lookup_table=[60, 1] * PHOTLAM) up = SourceSpectrum(Empirical1D, points=wave, lookup_table=[1, 60] * PHOTLAM) src = Source(x=[0, -2, 2], y=[1, -1, -1], ref=[0, 1, 2], spectra=[flat, up, down]) return src
def test_initialises_with_image_and_flux(self, input_hdulist): src = Source(image_hdu=input_hdulist[0], flux=20 * u.ABmag) assert isinstance(src, Source) assert isinstance(src.spectra[0], SourceSpectrum) assert isinstance(src.fields[0], fits.ImageHDU)
def test_initialises_with_nothing(self): src = Source() assert isinstance(src, Source)
def test_initialises_with_image_and_0_spectra(self, input_hdulist): with pytest.raises(NotImplementedError): src = Source(image_hdu=input_hdulist[0])
def test_initialises_with_image_and_1_spectrum(self, input_hdulist, input_spectra): src = Source(image_hdu=input_hdulist[0], spectra=input_spectra) assert isinstance(src, Source) assert isinstance(src.spectra[0], SourceSpectrum) assert isinstance(src.fields[0], fits.PrimaryHDU)
def test_initialises_with_only_image(self, input_hdulist): input_hdulist[0].header["BUNIT"] = "ph s-1 cm-2 AA-1" src = Source(image_hdu=input_hdulist[0]) assert len(src.spectra) == 1 assert src.fields[0].header["SPEC_REF"] == 0