Skip to content

Commit

Permalink
References (#582)
Browse files Browse the repository at this point in the history
  • Loading branch information
virajkaram authored Aug 4, 2023
1 parent e3cf5a0 commit a88ca0d
Show file tree
Hide file tree
Showing 33 changed files with 1,652 additions and 1,049 deletions.
17 changes: 17 additions & 0 deletions mirar/data/utils/coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"""
import logging

from astropy import units as u
from astropy.io import fits
from astropy.units import Quantity
from astropy.wcs import WCS

from mirar.data import Image
Expand Down Expand Up @@ -90,3 +92,18 @@ def write_regions_file(
regions_f.write(f"{system}\n")
for ind, x in enumerate(x_coords):
regions_f.write(f"CIRCLE({x},{y_coords[ind]},{region_radius})\n")


def get_image_dims_from_header(header: fits.Header) -> (Quantity, Quantity):
"""
Get image dimensions from the header
Args:
header:
Returns:
"""
nx, ny = header["NAXIS1"], header["NAXIS2"]
dx, dy = header["CD1_1"], header["CD2_2"]
img_dims = (dx * nx * u.deg, dy * ny * u.deg)
return img_dims
47 changes: 39 additions & 8 deletions mirar/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from astropy.utils.exceptions import AstropyUserWarning, AstropyWarning

from mirar.data import Image
from mirar.paths import BASE_NAME_KEY, RAW_IMG_KEY, core_fields
from mirar.paths import BASE_NAME_KEY, LATEST_SAVE_KEY, RAW_IMG_KEY, core_fields

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -93,7 +93,9 @@ def open_fits(path: str | Path) -> tuple[np.ndarray, fits.Header]:
:param path: path of fits file
:return: tuple containing image data and image header
"""
with fits.open(path, memmap=False) as img:
if isinstance(path, str):
path = Path(path)
with fits.open(path, memmap=False, ignore_missing_simple=True) as img:
hdu = img.pop(0)
hdu.verify("silentfix+ignore")
data = hdu.data
Expand All @@ -103,11 +105,33 @@ def open_fits(path: str | Path) -> tuple[np.ndarray, fits.Header]:
header[BASE_NAME_KEY] = Path(path).name

if RAW_IMG_KEY not in header.keys():
header[RAW_IMG_KEY] = path
header[RAW_IMG_KEY] = path.as_posix()

return data, header


def save_fits(
image: Image,
path: str | Path,
):
"""
Save an Image to path
:param image: Image to save
:param path: path
:return: None
"""
if isinstance(path, str):
path = Path(path)
check_image_has_core_fields(image)
data = image.get_data()
header = image.get_header()
if header is not None:
header[LATEST_SAVE_KEY] = path.as_posix()
logger.debug(f"Saving to {path.as_posix()}")
save_to_path(data, header, path)


def open_raw_image(
path: str | Path,
open_f: Callable[[str | Path], tuple[np.ndarray, fits.Header]] = open_fits,
Expand All @@ -119,6 +143,9 @@ def open_raw_image(
:param open_f: function to open the raw image
:return: Image object
"""
if isinstance(path, str):
path = Path(path)

data, header = open_f(path)

new_img = Image(data.astype(np.float64), header)
Expand Down Expand Up @@ -292,10 +319,14 @@ def check_image_has_core_fields(img: Image):
if key not in img.keys():
if BASE_NAME_KEY in img.keys():
msg = f"({img[BASE_NAME_KEY]}) "

err = (
f"New image {msg}is missing the core field {key}. "
f"Available fields are {list(img.keys())}."
)
err = (
f"New image {msg}is missing the core field {key}. "
f"Available fields are {list(img.keys())}."
)
else:
err = (
f"New image is missing the core field {BASE_NAME_KEY}. Available "
f"fields are {list(img.keys())}."
)
logger.error(err)
raise MissingCoreFieldError(err)
2 changes: 2 additions & 0 deletions mirar/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ def get_astrometry_keys() -> list:
GAIN_KEY = "GAIN"
EXPTIME_KEY = "EXPTIME"
ZP_KEY = "ZP"
ZP_STD_KEY = "ZPSTD"
ZP_NSTARS_KEY = "ZPNSTARS"
SATURATE_KEY = "SATURATE"
PSF_FLUX_KEY = "psf_flux"
PSF_FLUXUNC_KEY = "psf_fluxunc"
Expand Down
9 changes: 3 additions & 6 deletions mirar/pipelines/sedmv2/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
sedmv2_reference_image_resampler,
sedmv2_reference_psfex,
sedmv2_reference_sextractor,
sedmv2_zogy_catalogs_purifier,
)
from mirar.pipelines.sedmv2.load_sedmv2_image import load_sedmv2_mef_image
from mirar.processors import BiasCalibrator, FlatCalibrator
Expand All @@ -44,11 +45,7 @@

# from mirar.processors.utils.cal_hunter import CalHunter
from mirar.processors.utils.header_annotate import HeaderEditor
from mirar.processors.zogy.zogy import (
ZOGY,
ZOGYPrepare,
default_sedmv2_catalog_purifier,
)
from mirar.processors.zogy.zogy import ZOGY, ZOGYPrepare

load_raw = [
MEFLoader(
Expand Down Expand Up @@ -218,7 +215,7 @@
ZOGYPrepare(
output_sub_dir="subtract",
sci_zp_header_key="ZP_AUTO",
catalog_purifier=default_sedmv2_catalog_purifier,
catalog_purifier=sedmv2_zogy_catalogs_purifier,
),
ZOGY(output_sub_dir="subtract"),
]
Expand Down
25 changes: 24 additions & 1 deletion mirar/pipelines/sedmv2/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ def sedmv2_reference_image_resampler(**kwargs) -> Swarp:
:param kwargs: kwargs
:return: Swarp processor
"""
return Swarp(swarp_config_path=swarp_config_path, cache=True, **kwargs)
return Swarp(
swarp_config_path=swarp_config_path, cache=True, subtract_bkg=True, **kwargs
)


def sedmv2_reference_sextractor(output_sub_dir: str, gain: float) -> Sextractor:
Expand Down Expand Up @@ -142,3 +144,24 @@ def sedmv2_reference_psfex(output_sub_dir: str, norm_fits: bool) -> PSFex:
output_sub_dir=output_sub_dir,
norm_fits=norm_fits,
)


def sedmv2_zogy_catalogs_purifier(sci_catalog, ref_catalog):
"""
TODO: This should be in sedmv2?
"""
good_sci_sources = (
(sci_catalog["FLAGS"] == 0)
& (sci_catalog["SNR_WIN"] > 5)
& (sci_catalog["FWHM_WORLD"] < 4.0 / 3600)
& (sci_catalog["FWHM_WORLD"] > 0.5 / 3600)
& (sci_catalog["SNR_WIN"] < 1000)
)

good_ref_sources = (
(ref_catalog["SNR_WIN"] > 5)
& (ref_catalog["FWHM_WORLD"] < 5.0 / 3600)
& (ref_catalog["FWHM_WORLD"] > 0.5 / 3600)
)

return good_sci_sources, good_ref_sources
9 changes: 3 additions & 6 deletions mirar/pipelines/summer/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
summer_reference_image_resampler,
summer_reference_psfex,
summer_reference_sextractor,
summer_zogy_catalogs_purifier,
)
from mirar.pipelines.summer.load_summer_image import (
load_proc_summer_image,
Expand Down Expand Up @@ -56,11 +57,7 @@
from mirar.processors.utils.cal_hunter import CalHunter
from mirar.processors.utils.header_annotate import HeaderEditor
from mirar.processors.utils.simulate_realtime import RealtimeImageSimulator
from mirar.processors.zogy.zogy import (
ZOGY,
ZOGYPrepare,
default_summer_catalog_purifier,
)
from mirar.processors.zogy.zogy import ZOGY, ZOGYPrepare

load_raw = [
ImageLoader(load_image=load_raw_summer_image),
Expand Down Expand Up @@ -231,7 +228,7 @@
output_sub_dir="subtract",
sci_zp_header_key="ZP_AUTO",
ref_zp_header_key="ZP",
catalog_purifier=default_summer_catalog_purifier,
catalog_purifier=summer_zogy_catalogs_purifier,
),
ZOGY(output_sub_dir="subtract"),
]
Expand Down
30 changes: 29 additions & 1 deletion mirar/pipelines/summer/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ def summer_reference_image_resampler(**kwargs) -> Swarp:
:param kwargs: kwargs
:return: Swarp processor
"""
return Swarp(swarp_config_path=swarp_config_path, cache=True, **kwargs)
return Swarp(
swarp_config_path=swarp_config_path, cache=True, subtract_bkg=True, **kwargs
)


def summer_reference_sextractor(output_sub_dir: str, gain: float) -> Sextractor:
Expand Down Expand Up @@ -170,3 +172,29 @@ def summer_reference_psfex(output_sub_dir: str, norm_fits: bool) -> PSFex:
output_sub_dir=output_sub_dir,
norm_fits=norm_fits,
)


def summer_zogy_catalogs_purifier(sci_catalog: Table, ref_catalog: Table):
"""
:param sci_catalog: science catalog
:param ref_catalog: reference catalog
:return: good_sci_sources, good_ref_sources
"""
# Need to do this because the summer data is typically much
# shallower than the PS1 data, and only the brightest
# sources in PS1 xmatch to it.
good_sci_sources = (
(sci_catalog["FLAGS"] == 0)
& (sci_catalog["SNR_WIN"] > 5)
& (sci_catalog["FWHM_WORLD"] < 4.0 / 3600)
& (sci_catalog["FWHM_WORLD"] > 0.5 / 3600)
& (sci_catalog["SNR_WIN"] < 1000)
)

good_ref_sources = (
(ref_catalog["SNR_WIN"] > 5)
& (ref_catalog["FWHM_WORLD"] < 5.0 / 3600)
& (ref_catalog["FWHM_WORLD"] > 0.5 / 3600)
)

return good_sci_sources, good_ref_sources
11 changes: 6 additions & 5 deletions mirar/pipelines/summer/load_summer_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Module with functions to load raw and processed summer images
"""
import logging
import os
import warnings
from pathlib import Path

Expand Down Expand Up @@ -31,7 +30,7 @@
logger = logging.getLogger(__name__)


def load_raw_summer_fits(path: str) -> tuple[np.array, astropy.io.fits.Header]:
def load_raw_summer_fits(path: str | Path) -> tuple[np.array, astropy.io.fits.Header]:
"""
Function to load a raw summer image and add/modify the required headers
Args:
Expand All @@ -40,6 +39,8 @@ def load_raw_summer_fits(path: str) -> tuple[np.array, astropy.io.fits.Header]:
Returns: [image data, image header]
"""
if isinstance(path, str):
path = Path(path)
data, header = open_fits(path)

with warnings.catch_warnings():
Expand Down Expand Up @@ -85,8 +86,8 @@ def load_raw_summer_fits(path: str) -> tuple[np.array, astropy.io.fits.Header]:
header["TELDEC"] = tel_crd.dec.deg
header["BZERO"] = 0

header[LATEST_SAVE_KEY] = path
header[RAW_IMG_KEY] = path
header[LATEST_SAVE_KEY] = path.as_posix()
header[RAW_IMG_KEY] = path.as_posix()

data = data * 1.0 # pylint: disable=no-member

Expand All @@ -95,7 +96,7 @@ def load_raw_summer_fits(path: str) -> tuple[np.array, astropy.io.fits.Header]:

header[PROC_HISTORY_KEY] = ""

base_name = os.path.basename(path)
base_name = path.name
header[BASE_NAME_KEY] = base_name

pipeline_version = __version__
Expand Down
19 changes: 13 additions & 6 deletions mirar/pipelines/winter/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
MAX_DITHER_KEY,
OBSCLASS_KEY,
TARGET_KEY,
ZP_KEY,
base_output_dir,
)
from mirar.pipelines.winter.config import (
Expand All @@ -31,9 +32,8 @@
winter_astrostat_catalog_purifier,
winter_fourier_filtered_image_generator,
winter_photometric_catalog_generator,
winter_refbuild_reference_generator,
winter_reference_generator,
winter_reference_image_resampler,
winter_reference_image_resampler_for_zogy,
winter_reference_psfex,
winter_reference_sextractor,
winter_stackid_annotator,
Expand Down Expand Up @@ -134,7 +134,7 @@
]

refbuild = [
GetReferenceImage(ref_image_generator=winter_refbuild_reference_generator),
GetReferenceImage(ref_image_generator=winter_reference_generator),
ImageSaver(output_dir_name="stacked_ref"),
]

Expand Down Expand Up @@ -350,22 +350,29 @@
load_stack = [
ImageLoader(input_sub_dir="final"),
ImageBatcher(["BOARD_ID", "FILTER", TARGET_KEY, "SUBCOORD"]),
# ImageSelector(
# (BASE_NAME_KEY, "WINTERcamera_20230727-035357-778_mef_4_0_0.fits_stack.fits")
# ),
]

imsub = [
HeaderAnnotator(input_keys=[SUB_ID_KEY], output_key="SUBDETID"),
ProcessReference(
ref_image_generator=winter_reference_generator,
swarp_resampler=winter_reference_image_resampler,
swarp_resampler=winter_reference_image_resampler_for_zogy,
sextractor=winter_reference_sextractor,
ref_psfex=winter_reference_psfex,
),
Sextractor(**sextractor_reference_config, output_sub_dir="subtract", cache=False),
PSFex(config_path=psfex_path, output_sub_dir="subtract", norm_fits=True),
# ImageSaver(output_dir_name="presubtract"),
ZOGYPrepare(output_sub_dir="subtract", sci_zp_header_key="ZP_AUTO"),
ZOGYPrepare(
output_sub_dir="subtract", sci_zp_header_key="ZP_AUTO", ref_zp_header_key=ZP_KEY
),
# ImageSaver(output_dir_name="prezogy"),
ZOGY(output_sub_dir="subtract", sci_zp_header_key="ZP_AUTO"),
ZOGY(
output_sub_dir="subtract", sci_zp_header_key="ZP_AUTO", ref_zp_header_key=ZP_KEY
),
ImageSaver(output_dir_name="diffs"),
]

Expand Down
Loading

0 comments on commit a88ca0d

Please sign in to comment.