Skip to content

Commit

Permalink
functions parameters are included in the metadata for many functions,…
Browse files Browse the repository at this point in the history
… lamp function added
  • Loading branch information
miguelverdugo committed Apr 8, 2022
1 parent 2473a03 commit 6752467
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 9 deletions.
21 changes: 21 additions & 0 deletions scopesim_templates/calibration/calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@
from ..misc.misc import uniform_source


def lamp(waves, fwhm, fluxes):
"""
simple lamp function
"""
params = locals()
params["object"] = "lamp"
params["function_call"] = function_call_str(lamp, params)

w_min, w_max = np.min(waves), np.max(waves)
sp = Spextrum.flat_spectrum(amplitude=40, waves=[w_min, w_max]) # A very faint spextrum to add sources
sp = sp.add_emi_lines(center=waves, fwhm=fwhm, flux=fluxes)
src = uniform_source(sed=sp)
src.meta.update(params)

return src


def flat_field(temperature=5000, amplitude=0*u.ABmag, filter_curve="V", extend=60):
"""
This function creates a flat-field source to be used in ScopeSim
Expand Down Expand Up @@ -36,9 +53,13 @@ def flat_field(temperature=5000, amplitude=0*u.ABmag, filter_curve="V", extend=6
src: Source
"""
params = locals()
params["object"] = "flat_field"
params["function_call"] = function_call_str(flat_field, params)

sp = Spextrum.black_body_spectrum(temperature=temperature, amplitude=amplitude, filter_curve=filter_curve)
src = uniform_source(sed=sp, amplitude=amplitude, filter_curve=filter_curve, extend=extend)
src.meta.updata(params)

return src

Expand Down
22 changes: 16 additions & 6 deletions scopesim_templates/extragalactic/galaxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from astropy.io import fits
from astropy.utils import deprecated_renamed_argument
from astropy.utils.data import download_file
from astropy.wcs import WCS

from spextra import Spextrum

Expand Down Expand Up @@ -66,6 +67,10 @@ def galaxy(sed, # The SED of the galaxy
-------
src : scopesim.Source
"""
params = locals()
params["object"] = "galaxy"
params["function_call"] = gu.function_call_str(galaxy, params)

if isinstance(amplitude, u.Quantity) is False:
amplitude = amplitude * u.ABmag
if isinstance(pixel_scale, u.Quantity) is False:
Expand Down Expand Up @@ -95,6 +100,7 @@ def galaxy(sed, # The SED of the galaxy
src = source_from_array(image=gal.intensity, sed=sed, pixel_scale=pixel_scale,
amplitude=amplitude, filter_curve=filter_curve, ra=ra, dec=dec)

src.meta.update(params)
return src


Expand Down Expand Up @@ -155,6 +161,9 @@ def galaxy3d(sed, # The SED of the galaxy,
-------
src : scopesim.Source
"""
params = locals()
params["object"] = "galaxy3D"
params["function_call"] = gu.function_call_str(galaxy3d, params)

if isinstance(amplitude, u.Quantity) is False:
amplitude = amplitude * u.ABmag
Expand Down Expand Up @@ -185,15 +194,15 @@ def galaxy3d(sed, # The SED of the galaxy,
x, y = np.meshgrid(np.arange(image_size),
np.arange(image_size))

galaxy = GalaxyBase(x=x, y=y, x_0=x_0, y_0=y_0,
gal = GalaxyBase(x=x, y=y, x_0=x_0, y_0=y_0,
r_eff=r_eff.value/pixel_scale.value,
amplitude=1, n=n,
ellip=ellip, theta=theta, vmax=vmax, sigma=sigma)

intensity = galaxy.intensity / np.sum(galaxy.intensity)
velocity = galaxy.velocity.value
dispersion = galaxy.dispersion.value
masks = galaxy.get_masks(ngrid=ngrid)
intensity = gal.intensity / np.sum(galaxy.intensity)
velocity = gal.velocity.value
dispersion = gal.dispersion.value
masks = gal.get_masks(ngrid=ngrid)
w, h = intensity.shape

wcs_dict = dict(NAXIS=2,
Expand Down Expand Up @@ -234,6 +243,7 @@ def galaxy3d(sed, # The SED of the galaxy,

src = src + Source(image_hdu=hdu, spectra=spec)

src.meta.update(params)
return src


Expand Down Expand Up @@ -433,4 +443,4 @@ def elliptical(half_light_radius, pixel_scale, filter_name, amplitude,
src = Source(spectra=[spectrum], image_hdu=hdu)
src.meta.update(params)

return src
return src
52 changes: 49 additions & 3 deletions scopesim_templates/misc/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@


def point_source(sed, amplitude=None, filter_curve="V", x=0, y=0, ra=gu.RA0, dec=gu.DEC0):
"""
Creates a point source with an arbitrary spectrum. This is similar to `scopesim_templates.stellar.star
but probably more appropriate for other kind of sources
sed : str or synphot.Source_Spectrum
str will try to download a sed from the speXtra database
alternatively an user manipulated `synphot.Source_Spectrum` or compatible object can be provided
amplitude : float
flux or magnitude of the object. The SED will be scaled to that value
if left to `None` (default) no scaling will be performed
filter_curve : str
any astronomical filter in the speXtra or the spanish VO database
x : float
X-coordinate on the plane in arcsec
y : float
Y-coordinate on the plane in arcsec
ra, dec : float
ra, dec coordinates of the center of the field
"""
params = locals()
params["object"] = "point_source"
params["function_call"] = gu.function_call_str(point_source, params)

if (isinstance(amplitude, u.Quantity)) is False and amplitude is not None:
amplitude = amplitude * u.ABmag
Expand All @@ -30,21 +52,29 @@ def point_source(sed, amplitude=None, filter_curve="V", x=0, y=0, ra=gu.RA0, dec
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])

src.meta.update(params)
return src


def uniform_source(sed, amplitude=None, filter_curve="V", extend=60, ra=gu.RA0, dec=gu.DEC0):
"""
Creates a extended uniform source with an arbitrary spectrum
Creates an extended uniform source with an arbitrary spectrum.
This function reates an image with extend^2 pixels with size of 1 arcsec^2 so provided amplitudes
are in flux or magnitudes per arcsec^2
sed : synphot or spextra sed
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
extension of the field in arcsec, will always produce a square field
ra : RA
dec : DEC
"""
params = locals()
params["object"] = "uniform_source"
params["function_call"] = gu.function_call_str(uniform_source, params)

if (isinstance(amplitude, u.Quantity)) is False and amplitude is not None:
amplitude = amplitude * u.ABmag
if isinstance(sed, str):
Expand All @@ -64,6 +94,7 @@ def uniform_source(sed, amplitude=None, filter_curve="V", extend=60, ra=gu.RA0,

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

src.meta.update(params)
return src


Expand Down Expand Up @@ -109,11 +140,16 @@ def source_from_imagehdu(image_hdu, filter_name, pixel_unit_amplitude=None, wave
filter_name=filter_name,
pixel_unit_amplitude=20*u.Jy)
TODO: Check if the image_hdu has WCS
"""
# if isinstance(inst_pkg_path, str):
# import scopesim
# scopesim.rc.__search_path__.append(inst_pkg_path)

params = locals()
params["object"] = "source_from_imagehdu"
params["function_call"] = gu.function_call_str(source_from_imagehdu, params)

if filter_name is None and waverange is None:
raise ValueError("Wavelength information must be given with either a "
"filter_name or a waverange")
Expand Down Expand Up @@ -144,6 +180,7 @@ def source_from_imagehdu(image_hdu, filter_name, pixel_unit_amplitude=None, wave
image_hdu.header["SPEC_REF"] = 0

src = Source(image_hdu=image_hdu, spectra=spec)
src.meta.update(params)

return src

Expand Down Expand Up @@ -176,6 +213,10 @@ def source_from_imagehdu_with_flux(image_hdu=None, filename=None, ext=1, pixel_s
src : scopesim.Source
"""
params = locals()
params["object"] = "source_from_imagehdu_with_flux"
params["function_call"] = gu.function_call_str(source_from_imagehdu_with_flux, params)

if image_hdu is not None:
header = image_hdu.header
data = image_hdu.data
Expand Down Expand Up @@ -209,6 +250,7 @@ def source_from_imagehdu_with_flux(image_hdu=None, filename=None, ext=1, pixel_s
data = data / np.sum(data)
image_hdu = fits.ImageHDU(data=data, header=header)
src = Source(image_hdu=image_hdu, flux=total_flux)
src.meta.update(params)

return src

Expand All @@ -231,6 +273,10 @@ def source_from_array(arr, sed, pixel_scale, amplitude, filter_curve, ra=gu.RA0,
Returns
-------
"""
params = locals()
params["object"] = "source_from_array"
params["function_call"] = gu.function_call_str(source_from_array, params)

if isinstance(pixel_scale, u.Quantity) is False:
pixel_scale = pixel_scale * u.arcsec

Expand All @@ -245,7 +291,7 @@ def source_from_array(arr, sed, pixel_scale, amplitude, filter_curve, ra=gu.RA0,
data = arr / np.sum(arr)
hdu = fits.ImageHDU(data=data, header=header)
src = Source(image_hdu=hdu, spectra=sp)

src.meta.update(params)
return src


Expand Down

0 comments on commit 6752467

Please sign in to comment.