Skip to content

Commit

Permalink
Merge pull request #48 from sdss/twilights
Browse files Browse the repository at this point in the history
Implementing reduction sequence for twilights
  • Loading branch information
ajmejia authored Mar 6, 2024
2 parents b52ad67 + 4bd3ce2 commit cf808e8
Show file tree
Hide file tree
Showing 12 changed files with 7,355 additions and 107 deletions.
79 changes: 79 additions & 0 deletions bin/drp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import pathlib
import shutil
from typing import Tuple

import click
import cloup
Expand All @@ -15,6 +16,48 @@ from lvmdrp.functions.skyMethod import configureSkyModel_drp
from lvmdrp.utils.metadata import get_frames_metadata, get_master_metadata

from lvmdrp.functions.run_quickdrp import quick_science_reduction
from lvmdrp.functions.run_twilights import reduce_twilight_sequence


class IntRangeType(click.ParamType):
name = 'int_list'

def convert(self, value, param, ctx):
try:
# Split the input value by commas
parts = value.split(',')

if len(parts) == 1:
# If only one part, treat it as a single integer
return [int(parts[0])]
elif len(parts) == 2:
# If two parts, treat them as initial and final values for a range
start, end = map(int, parts)
return list(range(start, end + 1))
else:
self.fail(f'{value} is not a valid input', param, ctx)

except ValueError:
self.fail(f'{value} is not a valid input', param, ctx)


class WaveRange(click.ParamType):
name = 'wave_ranges'

def convert(self, value, param, ctx):
try:
# Split the input value by commas
waves = value.split(',')

if len(waves) == 2:
# Only if two parts, treat them as initial and final values for a range
iwave, fwave = map(float, waves)
return iwave, fwave
else:
self.fail(f'{value} is not a valid input', param, ctx)

except ValueError:
self.fail(f'{value} is not a valid input', param, ctx)


@click.group('drp', short_help='CLI for the LVM data reduction')
Expand Down Expand Up @@ -128,6 +171,42 @@ def quick_reduction(expnum: int, use_fiducial_master: bool, skip_sky_subtraction
cli.add_command(quick_reduction)


@click.command('twilight', short_help='Run the twilight sequence reduction')
@click.option('-e', '--expnums', type=IntRangeType(), multiple=True, help='a list of exposure numbers to reduce')
@click.option('-b', '--median-box', type=int, default=5, help='the median box size')
@click.option('-n', '--niter', type=int, default=1000, help='the number of iterations')
@click.option('-t', '--threshold', type=tuple, default=(0.5,2.0), help='the thresholds (lower,upper) for the sigma clipping')
@click.option('-k', '--nknots', type=int, default=80, help='the number of knots for the spline')
@cloup.option_group(
'Channel masks',
'Options for masking wavelength ranges for each spectrograph channel',
cloup.option('--b-mask', type=WaveRange(), multiple=True, help='the wavelength ranges to mask for b channel'),
cloup.option('--r-mask', type=WaveRange(), multiple=True, help='the wavelength ranges to mask for r channel'),
cloup.option('--z-mask', type=WaveRange(), multiple=True, help='the wavelength ranges to mask for z channel')
)
@click.option('-d', '--display-plots', is_flag=True, default=False, help='Flag to display the plots')
def twilight_reduction(expnums: list, median_box: int, niter: int, threshold: Tuple[float,float],
nknots: int, b_mask: list, r_mask: list, z_mask: list,
display_plots: bool) -> None:
""" Run the twilight sequence reduction """
# parse multiple exposure number lists into a single list
expnums = [expnum for expnum_list in expnums for expnum in expnum_list]
reduce_twilight_sequence(
expnums=expnums,
median_box=median_box,
niter=niter,
threshold=threshold,
nknots=nknots,
b_mask=b_mask,
r_mask=r_mask,
z_mask=z_mask,
display_plots=display_plots)


# register twilight sequence reduction
cli.add_command(twilight_reduction)


@cli.command('erase', short_help='Remove the DRP reductions')
@click.option('-d', '--drpver', type=str, help='the DRP version', required=True)
def erase(drpver: str):
Expand Down
Loading

0 comments on commit cf808e8

Please sign in to comment.