Skip to content

Commit

Permalink
Merge branch 'dev_master' into kl/currsys_recursive_bangs
Browse files Browse the repository at this point in the history
  • Loading branch information
teutoburg committed Jan 30, 2024
2 parents 3690d25 + c4c2264 commit 709d59f
Show file tree
Hide file tree
Showing 25 changed files with 573 additions and 258 deletions.
137 changes: 78 additions & 59 deletions poetry.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ tqdm = "^4.66.1"
synphot = "^1.2.1"
skycalc_ipy = "^0.3.0"
anisocado = "^0.3.0"
astar-utils = {version = "^0.2.0b0", allow-prereleases = true}
astar-utils = {version = "^0.2.0b1", allow-prereleases = true}

[tool.poetry.group.dev]
optional = true
Expand Down Expand Up @@ -90,6 +90,7 @@ filterwarnings = [
"ignore:divide by zero encountered in double_scalars:RuntimeWarning",
"ignore:invalid value encountered in multiply:RuntimeWarning",
"ignore:Cannot merge meta key.*:astropy.utils.metadata.MergeConflictWarning",
"default:The fov_grid*:DeprecationWarning",
# Raised when saving fits files, not so important to fix:
"ignore:.*a HIERARCH card will be created.*:astropy.io.fits.verify.VerifyWarning",
# Web-related issues, fix at some point
Expand Down
31 changes: 6 additions & 25 deletions scopesim/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"Generalised telescope observation simulator."
"""Generalised telescope observation simulator."""

###############################################################################
# TURN OFF WARNINGS #
###############################################################################
import sys
import logging

import warnings
import yaml
from importlib import metadata
Expand All @@ -26,30 +25,12 @@
# SET BASIC LOGGING LEVEL #
###############################################################################

# TODO: this should be replaced with YAML-based config!! see prepipy

# This should be part of ScopeSim (the app) and not scopesim_core eventually
# TODO: need to add a function to reload the config!

top_logger = logging.getLogger("astar")
top_logger.setLevel(logging.WARNING)
sim_logger = top_logger.getChild(__package__)
sim_logger.setLevel(logging.DEBUG)
top_logger.propagate = False
formatter = logging.Formatter("%(name)s - %(levelname)s: %(message)s")

log_dict = rc.__config__["!SIM.logging"]
if log_dict["log_to_file"]:
file_handler = logging.FileHandler(log_dict["file_path"],
log_dict["file_open_mode"])
file_handler.setLevel(log_dict["file_level"]) # DEBUG
file_handler.setFormatter(formatter)
top_logger.addHandler(file_handler)

if log_dict["log_to_console"]:
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setLevel(log_dict["console_level"]) # INFO
stdout_handler.setFormatter(formatter)
top_logger.addHandler(stdout_handler)
# Import convenience functions
from .utils import update_logging, log_to_file, set_console_log_level
update_logging()


###############################################################################
Expand Down
51 changes: 45 additions & 6 deletions scopesim/defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,51 @@ properties :
preamble_file: None

logging :
log_to_file: False
log_to_console: True
file_path: ".scopesim.log"
file_open_mode: "w" # w - overwrite, a - append
file_level: "DEBUG" # DEBUG INFO WARNING ERROR CRITICAL
console_level: "INFO" # DEBUG INFO WARNING ERROR CRITICAL
# This sub-dict enables direct configuration of logging.
# The corresponding schema can be found at:
# https://docs.python.org/3/library/logging.config.html#configuration-dictionary-schema

version: 1
disable_existing_loggers: False # Not sure what's best here??

root: # To allow e.g. warnings -> logging
level: INFO
handlers: [console] # [console, file] or just [console]

loggers:
astar:
level: WARNING
handlers: [console] # [console, file] or just [console]
propagate: False # Any logging from astar stops here.
# Or don't add handlers here, but let it propagate to root?
# This doesn't work because NestedMapping doesn't like "." in keys...
# astar.scopesim:
# level: DEBUG
# propagate: True # Goes through to astar logger.

handlers:
console:
class: logging.StreamHandler
level: INFO
formatter: color
stream: ext://sys.stdout
file:
class : logging.handlers.RotatingFileHandler
level: DEBUG
formatter: verbose
filename: ".scopesim.log"
mode: "w" # w - overwrite, a - append
encoding: "utf-8"
delay: True
maxBytes: 32768
backupCount: 3

formatters:
verbose:
format: '%(asctime)s - %(levelname)-8s - %(name)s - %(funcName)s - %(message)s'
color:
'()': astar_utils.loggers.ColoredFormatter
show_name: True

tests :
# overridden in tests/__init__.py
Expand Down
24 changes: 5 additions & 19 deletions scopesim/effects/apertures.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Effects related to field masks, including spectroscopic slits."""

import warnings
import yaml

import numpy as np
Expand Down Expand Up @@ -127,7 +128,8 @@ def apply_to(self, obj, **kwargs):
# Outdated. Remove when removing all old FOVManager code from effects
def fov_grid(self, which="edges", **kwargs):
"""Return a header with the sky coordinates."""
logger.warning("DetectorList.fov_grid will be depreciated in v1.0")
warnings.warn("The fov_grid method is deprecated and will be removed "
"in a future release.", DeprecationWarning, stacklevel=2)
if which == "edges":
self.meta.update(kwargs)
return self.header
Expand Down Expand Up @@ -206,16 +208,6 @@ def __init__(self, **kwargs):

self.table = self.get_table(**kwargs)

# def fov_grid(self, which="edges", **kwargs):
# """ Returns a header with the sky coordinates """
# if which == "edges":
# self.table = self.get_table(**kwargs)
# return self.header # from base class ApertureMask
#
# elif which == "masks":
# self.meta.update(kwargs)
# return self.mask

def get_table(self, **kwargs):
self.meta.update(kwargs)
x = from_currsys(self.meta["x"])
Expand Down Expand Up @@ -315,14 +307,6 @@ def apply_to(self, obj, **kwargs):

return obj

# def fov_grid(self, which="edges", **kwargs):
# params = deepcopy(self.meta)
# params.update(kwargs)
# if which == "edges":
# return [ap.fov_grid(which=which, **params) for ap in self.apertures]
# if which == "masks":
# return {ap.meta["id"]: ap.mask for ap in self.apertures}

@property
def apertures(self):
return self.get_apertures(range(len(self.table)))
Expand Down Expand Up @@ -459,6 +443,8 @@ def apply_to(self, obj, **kwargs):

def fov_grid(self, which="edges", **kwargs):
"""See parent docstring."""
warnings.warn("The fov_grid method is deprecated and will be removed "
"in a future release.", DeprecationWarning, stacklevel=2)
return self.current_slit.fov_grid(which=which, **kwargs)

def change_slit(self, slitname=None):
Expand Down
5 changes: 4 additions & 1 deletion scopesim/effects/detector_list.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""TBA."""

import warnings

import numpy as np
from astropy import units as u
from astropy.table import Table
Expand Down Expand Up @@ -158,7 +160,8 @@ def apply_to(self, obj, **kwargs):

def fov_grid(self, which="edges", **kwargs):
"""Return an ApertureMask object. kwargs are "pixel_scale" [arcsec]."""
logger.warning("DetectorList.fov_grid will be depreciated in v1.0")
warnings.warn("The fov_grid method is deprecated and will be removed "
"in a future release.", DeprecationWarning, stacklevel=2)
aperture_mask = None
if which == "edges":
self.meta.update(kwargs)
Expand Down
13 changes: 6 additions & 7 deletions scopesim/effects/electronic.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def apply_to(self, obj, **kwargs):
from_currsys("!OBS.detector_readout_mode"))
if isinstance(obj, ImagePlaneBase) and mode_name == "auto":
mode_name = self.select_mode(obj, **kwargs)
print("Detector mode set to", mode_name)
logger.info("Detector mode set to %s", mode_name)

self.meta["detector_readout_mode"] = mode_name
props_dict = self.mode_properties[mode_name]
Expand Down Expand Up @@ -195,10 +195,10 @@ def apply_to(self, obj, **kwargs):

if exptime is None:
exptime = from_currsys("!OBS.dit") * from_currsys("!OBS.ndit")
print(f"Requested exposure time: {exptime:.3f} s")
logger.info("Requested exposure time: %.3f s", exptime)

if exptime < mindit:
print(f" increased to MINDIT: {mindit:.3f} s")
logger.info(" increased to MINDIT: %.3f s", mindit)
exptime = mindit

full_well = from_currsys(self.meta["full_well"])
Expand All @@ -215,12 +215,11 @@ def apply_to(self, obj, **kwargs):
if dit < from_currsys(self.meta["mindit"]):
dit = from_currsys(self.meta["mindit"])
ndit = int(np.floor(exptime / dit))
print("Warning: The detector will be saturated!")
logger.warning("The detector will be saturated!")
# ..todo: turn into proper warning

print("Exposure parameters:")
print(f" DIT: {dit:.3f} s NDIT: {ndit}")
print(f"Total exposure time: {dit * ndit:.3f} s")
logger.info("Exposure parameters: DIT=%.3f s NDIT=%d", dit, ndit)
logger.info("Total exposure time: %.3f s", dit * ndit)

rc.__currsys__["!OBS.dit"] = dit
rc.__currsys__["!OBS.ndit"] = ndit
Expand Down
Loading

0 comments on commit 709d59f

Please sign in to comment.