Skip to content

Commit

Permalink
automatic determination of bin_width
Browse files Browse the repository at this point in the history
  • Loading branch information
oczoske committed Jun 19, 2023
1 parent 41321e4 commit 0ccad52
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 31 deletions.
15 changes: 5 additions & 10 deletions scopesim/effects/spectral_trace_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,6 @@ def apply_to(self, obj, **kwargs):
logging.info("Making cube")
obj.cube = obj.make_cube_hdu()

# ..todo: obj will be changed to a single one covering the full field of view
# covered by the image slicer (28 slices for LMS; for LSS still only a single slit)
# We need a loop over spectral_traces that chops up obj into the single-slice fov before
# calling map_spectra...
trace_id = obj.meta["trace_id"]
spt = self.spectral_traces[trace_id]
obj.hdu = spt.map_spectra_to_focal_plane(obj)
Expand Down Expand Up @@ -264,7 +260,7 @@ def rectify_traces(self, hdulist, xi_min=None, xi_max=None, interps=None,
filtwaves = filtcurve.table['wavelength']
filtwave = filtwaves[filtcurve.table['transmission'] > 0.01]
wave_min, wave_max = min(filtwave), max(filtwave)
logging.info("Extracted wavelength range: %.02f .. %.02f um",
logging.info("Full wavelength range: %.02f .. %.02f um",
wave_min, wave_max)

if xi_min is None or xi_max is None:
Expand All @@ -282,9 +278,8 @@ def rectify_traces(self, hdulist, xi_min=None, xi_max=None, interps=None,
""")
return None

bin_width = kwargs.get(
"bin_width",
from_currsys(self.meta["spectral_bin_width"]))

bin_width = kwargs.get("bin_width", None)

if interps is None:
logging.info("Computing interpolation functions")
Expand All @@ -300,11 +295,11 @@ def rectify_traces(self, hdulist, xi_min=None, xi_max=None, interps=None,
hdu = self[trace_id].rectify(hdulist,
interps=interps,
bin_width=bin_width,
xi_min=xi_min, x_max=xi_max,
xi_min=xi_min, xi_max=xi_max,
wave_min=wave_min, wave_max=wave_max)
if hdu is not None: # ..todo: rectify does not do that yet
outhdul.append(hdu)
outhdul[0].header[f"EXTNAME{i}"] = trace_id
outhdul[0].header[f"EXTNAME{i+1}"] = trace_id

outhdul[0].header.update(inhdul[0].header)

Expand Down
64 changes: 43 additions & 21 deletions scopesim/effects/spectral_trace_list_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,7 @@ def map_spectra_to_focal_plane(self, fov):
xmin_mm, ymin_mm = fpa_wcsd.all_pix2world(xmin, ymin, 0)
xmax_mm, ymax_mm = fpa_wcsd.all_pix2world(xmax, ymax, 0)

# Computation of dispersion dlam_per_pix along xi=0
# ..todo: This may have to be generalised - xi=0 is at the centre of METIS slits
# and the short MICADO slit.
xi = np.array([0] * 1001)
lam = np.linspace(wave_min, wave_max, 1001)
x_mm = self.xilam2x(xi, lam)
y_mm = self.xilam2y(xi, lam)
if self.dispersion_axis == "x":
dlam_grad = self.xy2lam.gradient()[0] # dlam_by_dx
else:
dlam_grad = self.xy2lam.gradient()[1] # dlam_by_dy
self.dlam_per_pix = interp1d(lam, dlam_grad(x_mm, y_mm) * pixsize,
fill_value="extrapolate")
self._set_dispersion(wave_min, wave_max, pixsize=pixsize)
try:
xilam = XiLamImage(fov, self.dlam_per_pix)
self._xilamimg = xilam # ..todo: remove or make available with a debug flag?
Expand Down Expand Up @@ -313,10 +301,7 @@ def rectify(self, hdulist, interps=None, wcs=None, **kwargs):
the header of the hdulist, but this is not yet provided by scopesim
"""
logging.info("Rectifying %s", self.trace_id)
# ..todo: build wcs if not provided
bin_width = kwargs.get(
"bin_width",
from_currsys(self.meta["spectral_bin_width"]))

wave_min = kwargs.get("wave_min",
self.wave_min)
wave_max = kwargs.get("wave_max",
Expand All @@ -328,13 +313,30 @@ def rectify(self, hdulist, interps=None, wcs=None, **kwargs):
wave_max = min(wave_max, self.wave_max)
logging.info(" %.02f .. %.02f um", wave_min, wave_max)

# bin_width is taken as the minimum dispersion of the trace
bin_width = kwargs.get("bin_width", None)
if bin_width is None:
self._set_dispersion(wave_min, wave_max)
bin_width = self.dlam_per_pix.y.min()
logging.info(" Bin width %.02g um", bin_width)

pixscale = from_currsys(self.meta['pixel_scale'])

# Temporary solution to get slit length
xi_min = kwargs.get("xi_min",
hdulist[0].header["HIERARCH INS SLIT XIMIN"])
xi_max = kwargs.get("xi_max",
hdulist[0].header["HIERARCH INS SLIT XIMAX"])
xi_min = kwargs.get("xi_min", None)
if xi_min is None:
try:
xi_min = hdulist[0].header["HIERARCH INS SLIT XIMIN"]
except KeyError:
logging.error("xi_min not found")
return None
xi_max = kwargs.get("xi_max", None)
if xi_max is None:
try:
xi_max = hdulist[0].header["HIERARCH INS SLIT XIMAX"]
except KeyError:
logging.error("xi_max not found")
return None

if wcs is None:
wcs = WCS(naxis=2)
Expand Down Expand Up @@ -521,6 +523,26 @@ def trace_id(self):
"""Return the name of the trace"""
return self.meta['trace_id']

def _set_dispersion(self, wave_min, wave_max, pixsize=None):
"""Computation of dispersion dlam_per_pix along xi=0
"""
#..todo: This may have to be generalised - xi=0 is at the centre
#of METIS slits and the short MICADO slit.

xi = np.array([0] * 1001)
lam = np.linspace(wave_min, wave_max, 1001)
x_mm = self.xilam2x(xi, lam)
y_mm = self.xilam2y(xi, lam)
if self.dispersion_axis == "x":
dlam_grad = self.xy2lam.gradient()[0] # dlam_by_dx
else:
dlam_grad = self.xy2lam.gradient()[1] # dlam_by_dy
pixsize = (from_currsys(self.meta['pixel_scale']) /
from_currsys(self.meta['plate_scale']))
self.dlam_per_pix = interp1d(lam,
dlam_grad(x_mm, y_mm) * pixsize,
fill_value="extrapolate")

def __repr__(self):
msg = (f"<SpectralTrace> \"{self.meta['trace_id']}\" : "
f"[{self.wave_min:.4f}, {self.wave_max:.4f}]um : "
Expand Down

0 comments on commit 0ccad52

Please sign in to comment.