Skip to content

Commit

Permalink
Merge branch 'skipfluxcal'
Browse files Browse the repository at this point in the history
  • Loading branch information
ajmejia committed Jan 19, 2024
2 parents d2c24c8 + 4332642 commit a031564
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
5 changes: 3 additions & 2 deletions bin/drp
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,13 @@ cli.add_command(metacli)
@click.option('-f', '--use-fiducial-master', is_flag=True, default=False, help='use fiducial master calibration frames')
@click.option('-s', '--skip-sky-subtraction', is_flag=True, help='skip sky subtraction')
@click.option('--sky-weights', type=(float, float), default=None, help='weights (east, west) for the master sky combination')
@click.option('-c', '--skip-flux-calibration', is_flag=True, default=False, help='skip flux calibration')
@click.option('-n', '--ncpus', type=int, default=None, help='number of CPUs to use during extraction')
@click.option("-a", "--aperture-extraction", is_flag=True, default=False, help="run quick reduction with aperture extraction")
def quick_reduction(expnum: int, use_fiducial_master: bool, skip_sky_subtraction: bool,
sky_weights: tuple, ncpus: int, aperture_extraction: bool) -> None:
sky_weights: tuple, skip_flux_calibration: bool, ncpus: int, aperture_extraction: bool) -> None:
quick_science_reduction(expnum, use_fiducial_master, skip_sky_subtraction,
sky_weights, ncpus, aperture_extraction)
sky_weights, skip_flux_calibration, ncpus, aperture_extraction)


# register quick DRP command
Expand Down
28 changes: 19 additions & 9 deletions python/lvmdrp/functions/fluxCalMethod.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@
"correctTelluric_drp",
]


def apply_fluxcal(in_rss: str, out_rss: str, display_plots: bool = False):
def apply_fluxcal(in_rss: str, out_rss: str, skip_fluxcal: bool = False, display_plots: bool = False):
"""applies flux calibration to spectrograph-combined data
Parameters
Expand All @@ -76,6 +75,8 @@ def apply_fluxcal(in_rss: str, out_rss: str, display_plots: bool = False):
input RSS file
out_rss : str
output RSS file
skip_fluxcal : bool, optional
whether to skip flux calibration, by default False
display_plots : bool, optional
Returns
Expand Down Expand Up @@ -123,7 +124,7 @@ def apply_fluxcal(in_rss: str, out_rss: str, display_plots: bool = False):
sens_ave = biweight_location(sens_arr, axis=1, ignore_nan=True)
sens_rms = biweight_scale(sens_arr, axis=1, ignore_nan=True)

# fix all zeros
# fix case of all invalid values
if (sens_ave == 0).all() or np.isnan(sens_ave).all():
log.warning(
"all sensitivity values are zero or NaN, impossible to flux-calibrate"
Expand Down Expand Up @@ -168,12 +169,21 @@ def apply_fluxcal(in_rss: str, out_rss: str, display_plots: bool = False):
ext = np.interp(rss._wave, lext, ext)
sci_secz = rss._header["TESCIAM"]

log.info("flux-calibrating data science and sky spectra")

rss._data *= sens_ave * 10 ** (0.4 * ext * (sci_secz)) / exptimes[:, None]
rss._error *= sens_ave * 10 ** (0.4 * ext * (sci_secz)) / exptimes[:, None]
rss._sky *= sens_ave * 10 ** (0.4 * ext * (sci_secz)) / exptimes[:, None]
rss._sky_error *= sens_ave * 10 ** (0.4 * ext * (sci_secz)) / exptimes[:, None]
# optionally sky flux calibration
if skip_fluxcal:
log.info("skipping flux calibration")
rss._data /= exptimes[:, None]
rss._error /= exptimes[:, None]
rss._sky /= exptimes[:, None]
rss._sky_error /= exptimes[:, None]
rss.setHdrValue("FLUXCAL", False, "flux-calibrated?")
rss.setHdrValue("BUNIT", "electron/s/A", "flux units")
else:
log.info("flux-calibrating data science and sky spectra")
rss._data *= sens_ave * 10 ** (0.4 * ext * (sci_secz)) / exptimes[:, None]
rss._error *= sens_ave * 10 ** (0.4 * ext * (sci_secz)) / exptimes[:, None]
rss._sky *= sens_ave * 10 ** (0.4 * ext * (sci_secz)) / exptimes[:, None]
rss._sky_error *= sens_ave * 10 ** (0.4 * ext * (sci_secz)) / exptimes[:, None]

log.info(f"writing output file in {os.path.basename(out_rss)}")
rss.writeFitsData(out_rss)
Expand Down
3 changes: 2 additions & 1 deletion python/lvmdrp/functions/run_quickdrp.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def get_master_mjd(sci_mjd: int) -> int:
def quick_science_reduction(expnum: int, use_fiducial_master: bool = False,
skip_sky_subtraction: bool = False,
sky_weights: Tuple[float, float] = None,
skip_flux_calibration: bool = False,
ncpus: int = None,
aperture_extraction: bool = False) -> None:
""" Run the Quick DRP for a given exposure number.
Expand Down Expand Up @@ -275,7 +276,7 @@ def quick_science_reduction(expnum: int, use_fiducial_master: bool = False,
sci_paths = sorted(drp.path.expand("lvm_anc", drpver=drpver, tileid=sci_tileid, mjd=sci_mjd, kind="", imagetype="object", camera="*", expnum=sci_expnum))
sci_paths = [sci_path for sci_path in sci_paths if "lvm-object-sp" not in sci_path]
for sci_path in sci_paths:
flux_tasks.apply_fluxcal(in_rss=sci_path, out_rss=sci_path)
flux_tasks.apply_fluxcal(in_rss=sci_path, out_rss=sci_path, skip_fluxcal=skip_flux_calibration)

# combine channels
drp.combine_channels(tileid=sci_tileid, mjd=sci_mjd, expnum=sci_expnum)
Expand Down

0 comments on commit a031564

Please sign in to comment.