Skip to content

Commit

Permalink
Add output argument; use "_defringe" as default output suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
philhodge committed Nov 1, 2019
1 parent dc3160b commit 0d06b41
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions stistools/defringe.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,41 @@
sdqflags = 4 + 8 + 512


def defringe(science_file, fringe_flat, overwrite=True, verbose=True):
def defringe(science_file, fringe_flat, output=None,
overwrite=True, verbose=True):
# dark_file=None, pixel_flat=None
"""Defringe by dividing the science spectrum by the fringe flat
Parameters
----------
science_file : str
The name of the input science file.
The name (including directory) of the input science file.
fringe_flat : str
The name of the input fringe flat file. This is the output from
`mkfringeflat`.
The name (including directory) of the input fringe flat file. This
is the output from `mkfringeflat`.
output : str or None
If `output` is None, the name of the output file will be constructed
by appending "_defringe" to the root of the input file name.
If `output` is not None, this is the name (not including directory)
of the output file. The output will be written to the same directory
as the input science file.
overwrite : bool
The name of the output file will be constructed from the name of the
input science file (`science_file`) by replacing the suffix with
'drj'. If the input name has suffix 'drj', a RuntimeError will be
raised, rather than modifying the input in-place.
If there is an existing file with the same name as the output name,
the existing file will be overwritten if `overwrite` is True (the
default is True).
If the input and output names are the same, a RuntimeError will be
raised, rather than modifying the input in-place.
verbose : bool
If True (the default), print more info.
Returns
-------
drj_filename : str
The name of the output file.
output_filename : str
The name of the output file, including directory.
"""

if science_file.endswith("_raw.fits"):
Expand All @@ -55,9 +61,13 @@ def defringe(science_file, fringe_flat, overwrite=True, verbose=True):
# Define new filename:
science_file = os.path.normpath(expandFileName(science_file)) # Expand IRAF and UNIX $VARS
sci_dir, sci_filename = os.path.split(science_file)
sci_root = re.split('\.fits.*', sci_filename, flags=re.IGNORECASE)[0].rsplit('_',1)[0]
drj_filename = os.path.join(sci_dir, sci_root + '_drj.fits')
if science_file == drj_filename:
if output is None:
sci_root = re.split('\.fits.*', sci_filename,

This comment has been minimized.

Copy link
@sean-lockwood

sean-lockwood Nov 1, 2019

Member

'\.fits.*' --> r'\.fits.*' to put a backslash in the regex.

flags=re.IGNORECASE)[0].rsplit('_',1)[0]
output_filename = os.path.join(sci_dir, sci_root + '_defringe.fits')
else:
output_filename = os.path.join(sci_dir, output)
if science_file == output_filename:
raise RuntimeError('The input and output file names cannot be the same.')

# Get the data from the fringe flat file:
Expand Down Expand Up @@ -166,13 +176,13 @@ def defringe(science_file, fringe_flat, overwrite=True, verbose=True):

# Write to a new FITS file
# Remove old version, if it exists:
if os.path.exists(drj_filename) and overwrite:
print('Removing and recreating {}'.format(drj_filename))
os.remove(drj_filename)
science_hdu.writeto(drj_filename)
print('Defringed science data saved to {}'.format(drj_filename))
if os.path.exists(output_filename) and overwrite:
print('Removing and recreating {}'.format(output_filename))
os.remove(output_filename)
science_hdu.writeto(output_filename)
print('Defringed science data saved to {}'.format(output_filename))

return drj_filename
return output_filename


def parse_args():
Expand All @@ -190,7 +200,7 @@ def parse_args():
Output
----------------------------------------------------------------
Outputs a file that has the same rootname as the input science file,
but with '_drj.fits' at the end. This is the final defringed data.
but with '_defringe.fits' at the end. This is the final defringed data.
'''), description='Script to calibrate science data in preparation for defringing')

parser.add_argument('science',
Expand Down

0 comments on commit 0d06b41

Please sign in to comment.