Skip to content

Commit

Permalink
Merge pull request #272 from AstarVienna/fh/refac
Browse files Browse the repository at this point in the history
Minor refactoring
  • Loading branch information
hugobuddel committed Sep 14, 2023
2 parents 7650119 + 57f84f0 commit a1f621e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 29 deletions.
4 changes: 2 additions & 2 deletions scopesim/effects/detector_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ def __init__(self, **kwargs):
if col in self.table.colnames:
self.table[col] = self.table[col] * mult_cols[col]
self.table.rename_column(col, new_name)
if not "x_size_unit" in self.meta and "xhw_unit" in self.meta:
if "x_size_unit" not in self.meta and "xhw_unit" in self.meta:
self.meta["x_size_unit"] = self.meta["xhw_unit"]
if not "y_size_unit" in self.meta and "yhw_unit" in self.meta:
if "y_size_unit" not in self.meta and "yhw_unit" in self.meta:
self.meta["y_size_unit"] = self.meta["yhw_unit"]

def apply_to(self, obj, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions scopesim/effects/metis_lms_trace_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ def apply_to(self, obj, **kwargs):
if isinstance(obj, FOVSetupBase):
# Create a single volume that covers the aperture and
# the maximum wavelength range of LMS
volumes = [self.spectral_traces[key].fov_grid()
for key in self.spectral_traces]
volumes = [spectral_trace.fov_grid()
for spectral_trace in self.spectral_traces.values()]
wave_min = min(vol["wave_min"] for vol in volumes)
wave_max = max(vol["wave_max"] for vol in volumes)
extracted_vols = obj.extract(axes=["wave"],
Expand Down
16 changes: 8 additions & 8 deletions scopesim/effects/psfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ def apply_to(self, obj, **kwargs):

return obj


def fov_grid(self, which="waveset", **kwargs):
"""See parent docstring."""
waveset = []
Expand All @@ -124,10 +123,11 @@ def fov_grid(self, which="waveset", **kwargs):
_waveset = self._waveset
waves = 0.5 * (np.array(_waveset)[1:] +
np.array(_waveset)[:-1])
wave_min = kwargs["wave_min"] if "wave_min" in kwargs else np.min(_waveset)
wave_max = kwargs["wave_max"] if "wave_max" in kwargs else np.max(_waveset)
wave_min = kwargs.get("wave_min", np.min(_waveset))
wave_max = kwargs.get("wave_max", np.max(_waveset))
mask = (wave_min < waves) * (waves < wave_max)
waveset = np.unique([wave_min] + list(waves[mask]) + [wave_max])
waveset = np.unique([wave_min] + list(waves[mask]) +
[wave_max])

return waveset

Expand Down Expand Up @@ -821,11 +821,11 @@ def get_kernel(self, fov):
# rescale the pixel scale of the kernel to match the fov images
pix_ratio = fov_pixel_scale / kernel_pixel_scale
if abs(pix_ratio - 1) > self.meta["flux_accuracy"]:
for ii in range(len(self.kernel)):
self.kernel[ii][0] = pu.rescale_kernel(self.kernel[ii][0], pix_ratio)
for ii, kern in enumerate(self.kernel):
self.kernel[ii][0] = pu.rescale_kernel(kern[0], pix_ratio)

for i in range(len(self.kernel)):
self.kernel[i][0] /= np.sum(self.kernel[i][0])
for i, kern in enumerate(self.kernel):
self.kernel[i][0] /= np.sum(kern[0])

return self.kernel

Expand Down
4 changes: 2 additions & 2 deletions scopesim/effects/spectral_trace_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ def apply_to(self, obj, **kwargs):
"""
if isinstance(obj, FOVSetupBase):
# Setup of FieldOfView object
volumes = [self.spectral_traces[key].fov_grid()
for key in self.spectral_traces]
volumes = [spectral_trace.fov_grid()
for spectral_trace in self.spectral_traces.values()]
new_vols_list = []
for vol in volumes:
wave_edges = [vol["wave_min"], vol["wave_max"]]
Expand Down
29 changes: 14 additions & 15 deletions scopesim/optics/image_plane_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def add_table_to_imagehdu(table: Table, canvas_hdu: fits.ImageHDU,
eps = -1e-7
mask = (xpix >= eps) * (xpix < naxis1) * (ypix >= eps) * (ypix < naxis2)

if sub_pixel is True:
if sub_pixel:
canvas_hdu = _add_subpixel_sources_to_canvas(canvas_hdu, xpix, ypix, f,
mask)
else:
Expand Down Expand Up @@ -468,10 +468,9 @@ def rescale_imagehdu(imagehdu: fits.ImageHDU, pixel_scale: float,
imagehdu : fits.ImageHDU
"""
wcs_suffix = wcs_suffix
s0 = wcs_suffix[0] if len(wcs_suffix) > 0 else ""
cdelt1 = imagehdu.header["CDELT1"+s0]
cdelt2 = imagehdu.header["CDELT2"+s0]
s0 = wcs_suffix[0] if wcs_suffix else ""
cdelt1 = imagehdu.header[f"CDELT1{s0}"]
cdelt2 = imagehdu.header[f"CDELT2{s0}"]

# making sure that zoom1,zoom2 are positive
zoom1 = np.abs(cdelt1 / pixel_scale)
Expand All @@ -494,13 +493,13 @@ def rescale_imagehdu(imagehdu: fits.ImageHDU, pixel_scale: float,
imagehdu.data = new_im

for ii in range(max(1, len(wcs_suffix))):
si = wcs_suffix[ii] if len(wcs_suffix) > 0 else ""
imagehdu.header["CRPIX1"+si] *= zoom1
imagehdu.header["CRPIX2"+si] *= zoom2
imagehdu.header["CDELT1"+si] = pixel_scale
imagehdu.header["CDELT2"+si] = pixel_scale
imagehdu.header["CUNIT1"+si] = "mm" if si == "D" else "deg"
imagehdu.header["CUNIT2"+si] = "mm" if si == "D" else "deg"
si = wcs_suffix[ii] if wcs_suffix else ""
imagehdu.header[f"CRPIX1{si}"] *= zoom1
imagehdu.header[f"CRPIX2{si}"] *= zoom2
imagehdu.header[f"CDELT1{si}"] = pixel_scale
imagehdu.header[f"CDELT2{si}"] = pixel_scale
imagehdu.header[f"CUNIT1{si}"] = "mm" if si == "D" else "deg"
imagehdu.header[f"CUNIT2{si}"] = "mm" if si == "D" else "deg"

return imagehdu

Expand Down Expand Up @@ -552,9 +551,9 @@ def reorient_imagehdu(imagehdu: fits.ImageHDU, wcs_suffix: str = "",
new_im *= np.sum(imagehdu.data) / np.sum(new_im)

imagehdu.data = new_im
hdr["CRPIX1"+s] = hdr["NAXIS1"] / 2.
hdr["CRPIX2"+s] = hdr["NAXIS2"] / 2.
for card in ["PC1_1"+s, "PC1_2"+s, "PC2_1"+s, "PC2_2"+s]:
hdr[f"CRPIX1{s}"] = hdr["NAXIS1"] / 2.
hdr[f"CRPIX2{s}"] = hdr["NAXIS2"] / 2.
for card in [f"PC1_1{s}", f"PC1_2{s}", f"PC2_1{s}", f"PC2_2{s}"]:
hdr.remove(card)
imagehdu.header = hdr

Expand Down

0 comments on commit a1f621e

Please sign in to comment.