Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 68 additions & 36 deletions python/lvmdrp/functions/imageMethod.py
Original file line number Diff line number Diff line change
Expand Up @@ -2975,52 +2975,84 @@ def reprojectRSS_drp(
rep.writeto(f"{out_path}/{out_name}_2d.fits", overwrite=True)


def testres_drp(image, trace, fwhm, flux):
"""
Historic task used for debugging of the the extraction routine...
def validate_extraction(in_image, in_cent, in_width, in_rss, plot_columns=[1000, 2000, 3000], display_plots=False):
"""Evaluates the extracted flux into the original 2D pixel grid

This routine will evaluate the extracted flux in the original
2D grid and compare the resulting 2D model against the original
2D image. Three images are stored as outputs:

- The residual 2D image: model - data
- The ratio 2D image: model / data
- The 2D model

Parameters
----------
in_image : str
Path to the original 2D image of the extracted flux
in_cent : str
Path to the fiber centroids trace
in_width : str
Path to the fiber width (FWHM) trace
in_rss : str
Path to the extracted flux in RSS format
plot_columns : array_like, optional
columns to show in plot, by default [1000, 2000, 3000]
display_plots : bool, optional
whether to display plots to screen or not, by dafult False
"""
log.info(f"loading 2D image {in_image}")
img = Image()
# t1 = time.time()
img.loadFitsData(image, extension_data=0)
trace_mask = TraceMask()
trace_mask.loadFitsData(trace, extension_data=0)
trace_fwhm = TraceMask()
# trace_fwhm.setData(data=numpy.ones(trace_mask._data.shape)*2.5)
trace_fwhm.loadFitsData(fwhm, extension_data=0)
img._data = numpy.nan_to_num(img._data)
img.loadFitsData(in_image)

trace_flux = TraceMask()
trace_flux.loadFitsData(flux, extension_data=0)
log.info(f"loading fiber parameters in {in_cent} and {in_width}")
cent = TraceMask.from_file(in_cent)
width = TraceMask.from_file(in_width)
width._data /= 2.354

log.info(f"loading extracted flux in {in_rss}")
rss = RSS.from_file(in_rss)
rss._data = numpy.nan_to_num(rss._data)

ypix_cor = rss._slitmap[["spectrographid"] == int(img._header["CCD"][1])]["ypix_z"]
ypix_ori = img._slitmap[["spectrographid"] == int(img._header["CCD"][1])]["ypix_z"]
thermal_shift = ypix_cor - ypix_ori
log.info(f"fiber thermal shift in slitmap: {thermal_shift:.4f}")
cent._data += thermal_shift

log.info(f"evaluating extracted flux into 2D pixel grid for {img._dim[1]} columns")
x = numpy.arange(img._dim[0])
out = numpy.zeros(img._dim)
fact = numpy.sqrt(2.0 * numpy.pi)

fig, axs = create_subplots(to_display=display_plots, nrows=len(plot_columns), ncols=1, figsize=(15,5), sharex=True, layout="constrained")
for i in range(img._dim[1]):
# print i
A = (
1.0
* numpy.exp(
-0.5
* (
(x[:, numpy.newaxis] - trace_mask._data[:, i][numpy.newaxis, :])
/ abs(trace_fwhm._data[:, i][numpy.newaxis, :] / 2.354)
)
** 2
)
/ (fact * abs(trace_fwhm._data[:, i][numpy.newaxis, :] / 2.354))
)
spec = numpy.dot(A, trace_flux._data[:, i])
A = (numpy.exp(-0.5 * ((x[:, None] - cent._data[:, i][None, :]) / abs(width._data[:, i][None, :])) ** 2) / (fact * abs(width._data[:, i][None, :])))
spec = numpy.dot(A, rss._data[:, i])
out[:, i] = spec
if i == 1000:
plt.plot(spec, "-r")
plt.plot(img._data[:, i], "ok")
plt.show()

if i in plot_columns:
axs[plot_columns.index(i)].step(x, img._data[:, i], color="k", lw=1, where="mid")
axs[plot_columns.index(i)].step(x, spec, color="r", lw=1, where="mid")

out_path = os.path.dirname(in_image)
out_name = os.path.basename(in_image).split(".fits")[0]
out_residual = os.path.join(out_path, f"{out_name}_residual.fits")
out_2dimage = os.path.join(out_path, f"{out_name}_2dimage.fits")
out_ratio = os.path.join(out_path, f"{out_name}_ratio.fits")
save_fig(fig, product_path=out_2dimage, to_display=display_plots, figure_path="qa", label="2D_extracted_model")

log.info(f"writing residual to {out_residual}")
hdu = pyfits.PrimaryHDU(img._data - out)
hdu.writeto("res.fits", overwrite=True)
hdu = pyfits.PrimaryHDU(out)
hdu.writeto("fit.fits", overwrite=True)
hdu.writeto(out_residual, overwrite=True)

hdu = pyfits.PrimaryHDU((img._data - out) / img._data)
hdu.writeto("res_rel.fits", overwrite=True)
log.info(f"writing ratio to {out_ratio}")
hdu = pyfits.PrimaryHDU(out / img._data)
hdu.writeto(out_ratio, overwrite=True)

log.info(f"writing 2D model {out_2dimage}")
hdu = pyfits.PrimaryHDU(out)
hdu.writeto(out_2dimage, overwrite=True)


# TODO: for arcs take short exposures for bright lines & long exposures for faint lines
Expand Down
10 changes: 10 additions & 0 deletions python/lvmdrp/functions/skyMethod.py
Original file line number Diff line number Diff line change
Expand Up @@ -1615,6 +1615,16 @@ def quick_sky_subtraction(in_cframe, out_sframe, skymethod: str = 'farlines_near
skyesky, skyesky_error = create_skysub_spectrum(sky_hdu, tel="skye", method=skymethod)
skywsky, skywsky_error = create_skysub_spectrum(sky_hdu, tel="skyw", method=skymethod)

# mask Halpha to avoid wrong geocoronal subtraction
from lvmdrp.core.fluxcal import interpolate_mask
plt.figure()
plt.plot(cframe._wave, scisky)
scisky = interpolate_mask(cframe._wave, scisky, (cframe._wave>6559)&(cframe._wave<6566))

plt.plot(cframe._wave, scisky)
plt.show()
return

# select correct fibers
data = cframe._data
error = cframe._error
Expand Down
Loading