Skip to content

Commit

Permalink
point_source function to be used with an arbitrary spectra, other min…
Browse files Browse the repository at this point in the history
…or changes
  • Loading branch information
miguelverdugo committed Apr 7, 2022
1 parent c44b779 commit 2473a03
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 18 deletions.
16 changes: 6 additions & 10 deletions scopesim_templates/calibration/calibration.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import numpy as np
from astropy import units as u
from astropy.io import fits

from spextra import Spextrum

from scopesim_templates.rc import Source, __config__
from scopesim_templates.utils.general_utils import function_call_str, make_img_wcs_header
from spextra import Spextrum
from ..misc.misc import uniform_source


def flat_field(temperature=5000, amplitude=0*u.ABmag, filter_curve="V", extend=60):
Expand Down Expand Up @@ -35,18 +38,11 @@ def flat_field(temperature=5000, amplitude=0*u.ABmag, filter_curve="V", extend=6
"""

sp = Spextrum.black_body_spectrum(temperature=temperature, amplitude=amplitude, filter_curve=filter_curve)

if isinstance(amplitude, u.Quantity) is False:
amplitude = amplitude * u.ABmag

data = np.ones(shape=(extend, extend))
header = make_img_wcs_header(ra=0, dec=0, pixel_scale=1, image_size=data.shape)
hdu = fits.ImageHDU(header=header, data=data)

src = Source(spectra=sp, image_hdu=hdu)
src = uniform_source(sed=sp, amplitude=amplitude, filter_curve=filter_curve, extend=extend)

return src


def empty_sky():
"""
Returns an empty source so that instrumental fluxes can be simulated
Expand Down
43 changes: 36 additions & 7 deletions scopesim_templates/misc/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from astropy import units as u
from astropy.wcs import WCS
from astropy.utils.decorators import deprecated_renamed_argument
from astropy.table import Table

from synphot import SourceSpectrum, Empirical1D

Expand All @@ -14,33 +15,59 @@
from ..utils import general_utils as gu


def homogeneous_source(sed, amplitude=15, filter_curve="V", extend=60, ra=gu.RA0, dec=gu.DEC0):
def point_source(sed, amplitude=None, filter_curve="V", x=0, y=0, ra=gu.RA0, dec=gu.DEC0):

if (isinstance(amplitude, u.Quantity)) is False and amplitude is not None:
amplitude = amplitude * u.ABmag
if isinstance(sed, str):
sp = Spextrum(sed)
scaled_sp = sp.scale_to_magnitude(amplitude=amplitude, filter_curve=filter_curve)
elif isinstance(sed, (Spextrum, SourceSpectrum)):
sp = Spextrum(modelclass=sed)
if amplitude is None:
scaled_sp = sp
else:
scaled_sp = sp.scale_to_magnitude(amplitude=amplitude, filter_curve=filter_curve)

src = Source(spectra=scaled_sp, x=[x], y=[y], ref=[0], weight=[1])
return src


def uniform_source(sed, amplitude=None, filter_curve="V", extend=60, ra=gu.RA0, dec=gu.DEC0):
"""
Creates a homogeneous source with an arbitrary spectrum
Creates a extended uniform source with an arbitrary spectrum
sed : synphot or spextra sed
amplitude : magnitude or flux of the spectrum in the filter_curve
amplitude : magnitude or flux (PER ARCSEC^2) of the spectrum in the specified filter_curve
filter_curve : any filter curve
extend : int
extension of the field in arcsec, will always produce an square field
ra : RA
dec : DEC
"""
if isinstance(amplitude, u.Quantity) is False:
if (isinstance(amplitude, u.Quantity)) is False and amplitude is not None:
amplitude = amplitude * u.ABmag
if isinstance(sed, str):
sp = Spextrum(sed)
scaled_sp = sp.scale_to_magnitude(amplitude=amplitude, filter_curve=filter_curve)
elif isinstance(sed, (Spextrum, SourceSpectrum)):
sp = Spextrum(modelclass=sed)
scaled_sp = sp.scale_to_magnitude(amplitude=amplitude, filter_curve=filter_curve)
if amplitude is None:
scaled_sp = sp
else:
scaled_sp = sp.scale_to_magnitude(amplitude=amplitude, filter_curve=filter_curve)

data = np.ones(shape=(extend, extend))
header = gu.make_img_wcs_header(ra=ra, dec=ra, pixel_scale=1, image_size=data.shape)

header = gu.make_img_wcs_header(ra=ra, dec=dec, pixel_scale=1, image_size=data.shape)
hdu = fits.ImageHDU(header=header, data=data)

src = Source(spectra=scaled_sp, image_hdu=hdu)

return src


def source_from_imagehdu(image_hdu, filter_name, pixel_unit_amplitude=None,
def source_from_imagehdu(image_hdu, filter_name, pixel_unit_amplitude=None, waverange=None,
inst_pkg_path=None):
"""
Creates a scopesim.Source object directly from an fits.ImageHDU
Expand All @@ -50,6 +77,8 @@ def source_from_imagehdu(image_hdu, filter_name, pixel_unit_amplitude=None,
image_hdu : fits.ImageHDU
filter_name : str
Either a standard filter name or a filter from an instrument package
waverange: tuple
wave_min and wave_max of the spectral range
pixel_unit_amplitude, optional
A Quantity that corresponds to a pixel value of 1 in the image
If not given, header keyword BUNIT is used
Expand Down
3 changes: 3 additions & 0 deletions scopesim_templates/stellar/stars.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ def stars(filter_name, amplitudes, spec_types, x, y, library="pyckles", ra=RA0,
the spectral type(s) of the stars, e.g. "A0V", "G5III"
x, y : arrays of float
[arcsec] x and y coordinates of the stars on the focal plane
ra, dec : float
coordinates of the center of the field
library: str
Library where the spectroscopic types are taken. By default are taken from the pickles library using
Expand Down
28 changes: 27 additions & 1 deletion scopesim_templates/tests/test_misc/test_misc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from os.path import exists
import pytest
import synphot
from pytest import raises
from astropy import units as u
from astropy.io import fits
Expand Down Expand Up @@ -51,11 +52,36 @@ def test_is_happy_with_metis_filter_and_bunit(self):
assert isinstance(src, Source)


class TestPointSource:
def test_initialize_from_spextra(self):
src = misc.point_source("pickles/a0v", amplitude=16)

assert isinstance(src, Source)

def test_initialize_from_synphot(self):
sp = synphot.SourceSpectrum(synphot.Empirical1D, points=[1000, 10000], lookup_table=[1, 1])
src = misc.point_source(sed=sp)
assert isinstance(src, Source)


class TestUniformSource:
def test_initialize_from_spextra(self):
src = misc.uniform_source("pickles/a0v", amplitude=16)

assert isinstance(src, Source)

def test_initialize_from_synphot(self):
sp = synphot.SourceSpectrum(synphot.Empirical1D, points=[1000, 10000], lookup_table=[1, 1])
src = misc.uniform_source(sed=sp)
assert isinstance(src, Source)


def test_poorman_cube_source_is_working():
cube = so._make_dummy_cube(scale=0.2, wave_unit=u.AA, ref_wave=5000,
wave_step=1, wave_type="WAVE", bunit="erg / (s cm2 Angstrom)")

hdul = fits.HDUList(cube)
cube_source = misc.poorman_cube_source(hdu=hdul, ext=0)

assert isinstance(cube_source, Source)
assert isinstance(cube_source, Source)

0 comments on commit 2473a03

Please sign in to comment.