Skip to content

Commit

Permalink
Fixed some issues with searching and loading standard stars, and load…
Browse files Browse the repository at this point in the history
…ing compressed FITS files for an arc frame.
  • Loading branch information
cylammarco committed Jul 24, 2023
1 parent 8d32c10 commit a2f016f
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 46 deletions.
15 changes: 13 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
We aim to track and report as many changes as possible, but this is not an exhaustive list of all the changes.

Version 0.5.0-dev
Version 0.5.1
-----------------

:Date X-X-2022
:Date 24-7-2022

* Minor bug fixes:

* Fixed a bug that mixed up the filenames and the designations of standard stars.
* Fixed a bug that stops ImageReducer recognising compressed FITS file as a valid format.


Version 0.5.0
-----------------

:Date 13-6-2022

* New Freatures:

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = aspired
version = 0.5.0
version = 0.5.1
license = BSD-3-Clause
license_files = LICENSE
description = The iraf-free spectral data reduction toolkit.
Expand Down
63 changes: 41 additions & 22 deletions src/aspired/flux_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,13 @@ def __init__(

self.library = None
self.target = None
self.designation = None
self.ftype = "flux"
self.cutoff = 0.4

self.designation_to_lib_filename = None
self.lib_to_filename = None
self.lib_to_designation = None
self.filename_to_lib = None

self.spectrum_oned_imported = False
Expand Down Expand Up @@ -154,6 +156,14 @@ def _load_standard_dictionary(self):
encoding="ascii",
)
)
self.lib_to_designation = json.load(
open(
pkg_resources.resource_filename(
"aspired", "standards/lib_to_designation.json"
),
encoding="ascii",
)
)
self.filename_to_lib = json.load(
open(
pkg_resources.resource_filename(
Expand Down Expand Up @@ -182,8 +192,10 @@ def _get_eso_standard(self):
else:
filename = "m"

# the rest of the file name
filename += self.target
# match the target designation to the file name
filename += self.designation_to_lib_filename[self.designation][
self.library
]

# the extension
filename += ".dat"
Expand All @@ -208,7 +220,9 @@ def _get_ing_standard(self):
folder = self.library

# the first part of the file name
filename = self.target
filename = self.designation_to_lib_filename[self.designation][
self.library
]
extension = self.library[3:]

# last letter (or nothing) of the file name
Expand Down Expand Up @@ -293,7 +307,10 @@ def _get_iraf_standard(self):
folder = self.library

# file name and extension
filename = self.target + ".dat"
filename = (
self.designation_to_lib_filename[self.designation][self.library]
+ ".dat"
)

filepath = os.path.join(base_dir, "standards", folder, filename)

Expand Down Expand Up @@ -386,6 +403,7 @@ def lookup_standard_libraries(self, target: str, cutoff: float = 0.4):
self.logger.critical(error_msg)
raise ValueError(error_msg)

# Return pair(s) of filename and library
return [
(f, l) for l, f in zip(library_list, filename_list)
], exact_match
Expand All @@ -402,21 +420,21 @@ def lookup_closet_match_in_library(
Parameters
----------
target: str
Name of the standard star
Desination of the standard star
library: str
Name of the library
cutoff: float (Default: 0.4)
The toleranceold for the word similarity in the range of [0, 1].
Return:
-------
The closest names to the target in that (closet) library.
The closest designation to the target in that (closet) library.
"""

# Load the list of libraries
library_name = difflib.get_close_matches(
library, list(self.lib_to_filename.keys()), n=1, cutoff=cutoff
library, list(self.lib_to_designation.keys()), n=1, cutoff=cutoff
)

if library_name == []:
Expand All @@ -426,19 +444,19 @@ def lookup_closet_match_in_library(
library_name = library_name[0]

# difflib uses Gestalt pattern matching.
target_name = difflib.get_close_matches(
target_designation = difflib.get_close_matches(
target.lower(),
self.lib_to_filename[library_name],
self.lib_to_designation[library_name],
n=1,
cutoff=cutoff,
)
if target_name == []:
target_name = None
if target_designation == []:
target_designation = None

else:
target_name = target_name[0]
target_designation = target_designation[0]

return target_name, library_name
return target_designation, library_name

def load_standard(
self,
Expand Down Expand Up @@ -478,13 +496,17 @@ def load_standard(
self.library = library
if self.library is not None:
(
_target,
_designation,
_library,
) = self.lookup_closet_match_in_library(self.target, self.library)

if _target is not None:
self.target = _target
if _designation is not None:
self.designation = _designation
self.library = _library
self.logger.info(
f"The requested standard star {self.target} is found in the"
f" given library {self.library}."
)

# If not, search again with the first one returned from lookup.
else:
Expand All @@ -509,12 +531,9 @@ def load_standard(
"is used because it has the closest matching name."
)

if not self.verbose:
if self.library is None:
# Use the default library order
self.logger.warning(
f"Standard library is not given, {self.library} is used."
)
self.designation, _ = self.lookup_closet_match_in_library(
self.target, self.library
)

if self.library.startswith("iraf"):
self._get_iraf_standard()
Expand Down
38 changes: 22 additions & 16 deletions src/aspired/image_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1629,10 +1629,12 @@ def load_data(self):
self.logger.debug("Loading light frame: %s.", self.light_list[i])
light = fits.open(self.light_list[i])[self.light_hdunum[i]]

data, header, exposure_time = self._get_data_and_header(
light, self.exptime_light_keyword
)
self.add_light(data, header, exposure_time)
(
light_data,
light_header,
light_exposure_time,
) = self._get_data_and_header(light, self.exptime_light_keyword)
self.add_light(light_data, light_header, light_exposure_time)

# Cosmic ray cleaning
if self.cosmicray:
Expand Down Expand Up @@ -1740,8 +1742,8 @@ def load_data(self):
# Open all the light frames
arc = fits.open(self.arc_list[i])[self.arc_hdunum[i]]

data, header, _ = self._get_data_and_header(arc)
self.add_arc(data, header)
arc_data, arc_header, _ = self._get_data_and_header(arc)
self.add_arc(arc_data, arc_header)

self.logger.debug("Arc frame header: %s.", self.arc_header[i])

Expand All @@ -1760,10 +1762,12 @@ def load_data(self):
# Open all the dark frames
dark = fits.open(self.dark_list[i])[self.dark_hdunum[i]]

data, header, exposure_time = self._get_data_and_header(
dark, self.exptime_dark_keyword
)
self.add_dark(data, header, exposure_time)
(
dark_data,
dark_header,
dark_exposure_time,
) = self._get_data_and_header(dark, self.exptime_dark_keyword)
self.add_dark(dark_data, dark_header, dark_exposure_time)

self.logger.debug(
"Dark frame header: %s.", self.dark_header[i]
Expand All @@ -1784,10 +1788,12 @@ def load_data(self):
# Open all the flatfield frames
flat = fits.open(self.flat_list[i])[self.flat_hdunum[i]]

data, header, exposure_time = self._get_data_and_header(
flat, self.exptime_flat_keyword
)
self.add_flat(data, header, exposure_time)
(
flat_data,
flat_header,
flat_exposure_time,
) = self._get_data_and_header(flat, self.exptime_flat_keyword)
self.add_flat(flat_data, flat_header, flat_exposure_time)

self.logger.debug(
"Flat frame header: %s.", self.flat_header[i]
Expand All @@ -1808,8 +1814,8 @@ def load_data(self):
# Open all the flatfield frames
bias = fits.open(self.bias_list[i])[self.bias_hdunum[i]]

data, header, _ = self._get_data_and_header(bias)
self.add_bias(data, header)
bias_data, bias_header, _ = self._get_data_and_header(bias)
self.add_bias(bias_data, bias_header)

self.logger.debug(
"Flat frame header: %s.", self.bias_header[i]
Expand Down
5 changes: 4 additions & 1 deletion src/aspired/onedspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -3602,7 +3602,10 @@ def load_standard(
self.fluxcal.load_standard(
target=target, library=library, ftype=ftype, cutoff=cutoff
)
self.logger.info(f"Loaded standard: {target} from {library}")
self.logger.info(
f"Loaded standard: {self.fluxcal.target} from"
f" {self.fluxcal.library}"
)

def inspect_standard(
self,
Expand Down
7 changes: 4 additions & 3 deletions src/aspired/spectrum_oneD.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,10 +603,10 @@ def add_arc_header(self, header: Union[fits.Header, list, np.ndarray]):
"""

if header is not None:
if type(header) == fits.Header:
if isinstance(header, fits.Header):
self.arc_header = header
self.logger.info("arc_header is stored.")
elif type(header[0]) == fits.Header:
elif isinstance(header[0], fits.Header):
self.arc_header = header[0]
self.logger.info("arc_header is stored.")
else:
Expand Down Expand Up @@ -5636,7 +5636,8 @@ def create_fits(
"flux_resampled_atm_ext_telluric_corrected"
]:
self.logger.info(
"Creating flux_resampled_atm_ext_telluric_corrected HDU now."
"Creating flux_resampled_atm_ext_telluric_corrected HDU"
" now."
)
self.create_flux_resampled_atm_ext_telluric_corrected_fits()
self.logger.info(
Expand Down
5 changes: 5 additions & 0 deletions src/aspired/standards/generate_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

# Create dictionary for libraries to filenames
lib_to_filename = {}
lib_to_designation = {}

# And the mapping of designations and filename
lib_filename_to_designation_position = {}
Expand All @@ -77,6 +78,7 @@
)
ra = c.ra.value
dec = c.dec.value
lib_to_designation[lib_name] = [d.replace(" ", "").lower() for d in v]
lib_filename_to_designation_position[lib_name + "/" + k] = [
[d.replace(" ", "").lower(), ra, dec] for d in v
]
Expand Down Expand Up @@ -115,6 +117,9 @@
with open("lib_to_filename.json", "w") as f:
json.dump(lib_to_filename, f)

with open("lib_to_designation.json", "w") as f:
json.dump(lib_to_designation, f)

with open("filename_to_lib.json", "w") as f:
json.dump(filename_to_lib, f)

Expand Down
Loading

0 comments on commit a2f016f

Please sign in to comment.