Skip to content

Commit

Permalink
#174 Add qlook option for debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
astropenguin committed Jul 14, 2024
1 parent 8d15c42 commit 9b2912a
Showing 1 changed file with 104 additions and 38 deletions.
142 changes: 104 additions & 38 deletions decode/qlook.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@


# standard library
from contextlib import contextmanager
from logging import DEBUG, basicConfig, getLogger
from pathlib import Path
from typing import Any, Literal, Optional, Sequence, Union, cast
from warnings import catch_warnings, simplefilter
Expand All @@ -30,6 +32,7 @@
# constants
DATA_FORMATS = "csv", "nc", "zarr", "zarr.zip"
DEFAULT_DATA_TYPE = "auto"
DEFAULT_DEBUG = False
DEFAULT_FIGSIZE = 12, 4
DEFAULT_FORMAT = "png"
DEFAULT_FREQUENCY_UNITS = "GHz"
Expand All @@ -42,6 +45,20 @@
DEFAULT_SKYCOORD_GRID = "6 arcsec"
DEFAULT_SKYCOORD_UNITS = "arcsec"
SIGMA_OVER_MAD = 1.4826
LOGGER = getLogger(__name__)


@contextmanager
def set_logger(debug: bool):
level = LOGGER.level

if debug:
LOGGER.setLevel(DEBUG)

try:
yield
finally:
LOGGER.setLevel(level)


def auto(dems: Path, /, **options: Any) -> Path:
Expand All @@ -52,9 +69,7 @@ def auto(dems: Path, /, **options: Any) -> Path:
Args:
dems: Input DEMS file (netCDF or Zarr).
Keyword Args:
options: Options for the selected command.
**options: Options for the selected command.
See the command help for all available options.
Returns:
Expand Down Expand Up @@ -116,6 +131,8 @@ def daisy(
outdir: Path = DEFAULT_OUTDIR,
overwrite: bool = DEFAULT_OVERWRITE,
suffix: str = "daisy",
# other options
debug: bool = DEFAULT_DEBUG,
**options: Any,
) -> Path:
"""Quick-look at a daisy scan observation.
Expand Down Expand Up @@ -145,16 +162,19 @@ def daisy(
skycoord_units: Units of the sky coordinate axes.
format: Output image format of quick-look result.
outdir: Output directory for the quick-look result.
suffix: Suffix that precedes the file extension.
overwrite: Whether to overwrite the output if it exists.
Keyword Args:
options: Other options for saving the output (e.g. dpi).
suffix: Suffix that precedes the file extension.
debug: Whether to print detailed logs for debugging.
**options: Other options for saving the output (e.g. dpi).
Returns:
Absolute path of the saved file.
"""
with set_logger(debug):
for key, val in locals().items():
LOGGER.debug(f"{key}: {val!r}")

with xr.set_options(keep_attrs=True):
da = load_dems(
dems,
Expand Down Expand Up @@ -250,6 +270,8 @@ def pswsc(
outdir: Path = DEFAULT_OUTDIR,
overwrite: bool = DEFAULT_OVERWRITE,
suffix: str = "pswsc",
# other options
debug: bool = DEFAULT_DEBUG,
**options: Any,
) -> Path:
"""Quick-look at a PSW observation with sky chopper.
Expand All @@ -271,14 +293,17 @@ def pswsc(
outdir: Output directory for the quick-look result.
overwrite: Whether to overwrite the output if it exists.
suffix: Suffix that precedes the file extension.
Keyword Args:
options: Other options for saving the output (e.g. dpi).
debug: Whether to print detailed logs for debugging.
**options: Other options for saving the output (e.g. dpi).
Returns:
Absolute path of the saved file.
"""
with set_logger(debug):
for key, val in locals().items():
LOGGER.debug(f"{key}: {val!r}")

with xr.set_options(keep_attrs=True):
da = load_dems(
dems,
Expand Down Expand Up @@ -339,6 +364,8 @@ def raster(
outdir: Path = DEFAULT_OUTDIR,
overwrite: bool = DEFAULT_OVERWRITE,
suffix: str = "raster",
# other options
debug: bool = DEFAULT_DEBUG,
**options: Any,
) -> Path:
"""Quick-look at a raster scan observation.
Expand Down Expand Up @@ -366,16 +393,19 @@ def raster(
skycoord_units: Units of the sky coordinate axes.
format: Output image format of quick-look result.
outdir: Output directory for the quick-look result.
suffix: Suffix that precedes the file extension.
overwrite: Whether to overwrite the output if it exists.
Keyword Args:
options: Other options for saving the output (e.g. dpi).
suffix: Suffix that precedes the file extension.
debug: Whether to print detailed logs for debugging.
**options: Other options for saving the output (e.g. dpi).
Returns:
Absolute path of the saved file.
"""
with set_logger(debug):
for key, val in locals().items():
LOGGER.debug(f"{key}: {val!r}")

with xr.set_options(keep_attrs=True):
da = load_dems(
dems,
Expand Down Expand Up @@ -464,6 +494,8 @@ def skydip(
outdir: Path = DEFAULT_OUTDIR,
overwrite: bool = DEFAULT_OVERWRITE,
suffix: str = "skydip",
# other options
debug: bool = DEFAULT_DEBUG,
**options: Any,
) -> Path:
"""Quick-look at a skydip observation.
Expand All @@ -489,16 +521,19 @@ def skydip(
the atmospheric transmission when chan_weight is std/tx.
format: Output image format of quick-look result.
outdir: Output directory for the quick-look result.
suffix: Suffix that precedes the file extension.
overwrite: Whether to overwrite the output if it exists.
Keyword Args:
options: Other options for saving the output (e.g. dpi).
suffix: Suffix that precedes the file extension.
debug: Whether to print detailed logs for debugging.
**options: Other options for saving the output (e.g. dpi).
Returns:
Absolute path of the saved file.
"""
with set_logger(debug):
for key, val in locals().items():
LOGGER.debug(f"{key}: {val!r}")

with xr.set_options(keep_attrs=True):
da = load_dems(
dems,
Expand Down Expand Up @@ -556,6 +591,8 @@ def still(
outdir: Path = DEFAULT_OUTDIR,
overwrite: bool = DEFAULT_OVERWRITE,
suffix: str = "still",
# other options
debug: bool = DEFAULT_DEBUG,
**options: Any,
) -> Path:
"""Quick-look at a still observation.
Expand All @@ -581,16 +618,19 @@ def still(
the atmospheric transmission when chan_weight is std/tx.
format: Output data format of the quick-look result.
outdir: Output directory for the quick-look result.
suffix: Suffix that precedes the file extension.
overwrite: Whether to overwrite the output if it exists.
Keyword Args:
options: Other options for saving the output (e.g. dpi).
suffix: Suffix that precedes the file extension.
debug: Whether to print detailed logs for debugging.
**options: Other options for saving the output (e.g. dpi).
Returns:
Absolute path of the saved file.
"""
with set_logger(debug):
for key, val in locals().items():
LOGGER.debug(f"{key}: {val!r}")

with xr.set_options(keep_attrs=True):
da = load_dems(
dems,
Expand Down Expand Up @@ -647,6 +687,8 @@ def xscan(
outdir: Path = DEFAULT_OUTDIR,
overwrite: bool = DEFAULT_OVERWRITE,
suffix: str = "zscan",
# other options
debug: bool = DEFAULT_DEBUG,
**options: Any,
) -> Path:
"""Quick-look at an observation of subref X-axis focus scan.
Expand All @@ -672,16 +714,19 @@ def xscan(
the atmospheric transmission when chan_weight is std/tx.
format: Output image format of quick-look result.
outdir: Output directory for the quick-look result.
suffix: Suffix that precedes the file extension.
overwrite: Whether to overwrite the output if it exists.
Keyword Args:
options: Other options for saving the output (e.g. dpi).
suffix: Suffix that precedes the file extension.
debug: Whether to print detailed logs for debugging.
**options: Other options for saving the output (e.g. dpi).
Returns:
Absolute path of the saved file.
"""
with set_logger(debug):
for key, val in locals().items():
LOGGER.debug(f"{key}: {val!r}")

return _scan(
dems,
"x",
Expand Down Expand Up @@ -721,6 +766,8 @@ def yscan(
outdir: Path = DEFAULT_OUTDIR,
overwrite: bool = DEFAULT_OVERWRITE,
suffix: str = "zscan",
# other options
debug: bool = DEFAULT_DEBUG,
**options: Any,
) -> Path:
"""Quick-look at an observation of subref Y-axis focus scan.
Expand All @@ -746,16 +793,19 @@ def yscan(
the atmospheric transmission when chan_weight is std/tx.
format: Output image format of quick-look result.
outdir: Output directory for the quick-look result.
suffix: Suffix that precedes the file extension.
overwrite: Whether to overwrite the output if it exists.
Keyword Args:
options: Other options for saving the output (e.g. dpi).
suffix: Suffix that precedes the file extension.
debug: Whether to print detailed logs for debugging.
**options: Other options for saving the output (e.g. dpi).
Returns:
Absolute path of the saved file.
"""
with set_logger(debug):
for key, val in locals().items():
LOGGER.debug(f"{key}: {val!r}")

return _scan(
dems,
"y",
Expand Down Expand Up @@ -795,6 +845,8 @@ def zscan(
outdir: Path = DEFAULT_OUTDIR,
overwrite: bool = DEFAULT_OVERWRITE,
suffix: str = "zscan",
# other options
debug: bool = DEFAULT_DEBUG,
**options: Any,
) -> Path:
"""Quick-look at an observation of subref Z-axis focus scan.
Expand All @@ -820,16 +872,19 @@ def zscan(
the atmospheric transmission when chan_weight is std/tx.
format: Output image format of quick-look result.
outdir: Output directory for the quick-look result.
suffix: Suffix that precedes the file extension.
overwrite: Whether to overwrite the output if it exists.
Keyword Args:
options: Other options for saving the output (e.g. dpi).
suffix: Suffix that precedes the file extension.
debug: Whether to print detailed logs for debugging.
**options: Other options for saving the output (e.g. dpi).
Returns:
Absolute path of the saved file.
"""
with set_logger(debug):
for key, val in locals().items():
LOGGER.debug(f"{key}: {val!r}")

return _scan(
dems,
"z",
Expand All @@ -843,6 +898,7 @@ def zscan(
chan_weight=chan_weight,
pwv=pwv,
# options for saving
debug=debug,
format=format,
outdir=outdir,
overwrite=overwrite,
Expand Down Expand Up @@ -870,6 +926,8 @@ def _scan(
outdir: Path = DEFAULT_OUTDIR,
overwrite: bool = DEFAULT_OVERWRITE,
suffix: str = "_scan",
# other options
debug: bool = DEFAULT_DEBUG,
**options: Any,
) -> Path:
"""Quick-look at an observation of subref axial/radial focus scan.
Expand All @@ -896,16 +954,19 @@ def _scan(
the atmospheric transmission when chan_weight is std/tx.
format: Output image format of quick-look result.
outdir: Output directory for the quick-look result.
suffix: Suffix that precedes the file extension.
overwrite: Whether to overwrite the output if it exists.
Keyword Args:
options: Other options for saving the output (e.g. dpi).
suffix: Suffix that precedes the file extension.
debug: Whether to print detailed logs for debugging.
**options: Other options for saving the output (e.g. dpi).
Returns:
Absolute path of the saved file.
"""
with set_logger(debug):
for key, val in locals().items():
LOGGER.debug(f"{key}: {val!r}")

with xr.set_options(keep_attrs=True):
da = load_dems(
dems,
Expand Down Expand Up @@ -1172,6 +1233,11 @@ def save_qlook(
def main() -> None:
"""Entry point of the decode-qlook command."""

basicConfig(
datefmt="%Y-%m-%d %H:%M:%S",
format="[%(asctime)s %(name)s %(funcName)s %(levelname)s] %(message)s",
)

Fire(
{
"auto": auto,
Expand Down

0 comments on commit 9b2912a

Please sign in to comment.