Skip to content

Commit

Permalink
Merge pull request #158 from lsst-ts/tickets/DM-45260
Browse files Browse the repository at this point in the history
Add configuration schema validation support to BaseCalsys and schema files for ATCalsys and MTCalsys classes
  • Loading branch information
MarcoRocchietti authored Sep 20, 2024
2 parents 909631c + f09fc19 commit 5284eb0
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 6 deletions.
1 change: 1 addition & 0 deletions doc/news/DM-45260.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add configuration schema validation support to ``BaseCalsys`` and schema validation files for ``ATCalsys`` and ``MTCalsys`` classes.
49 changes: 43 additions & 6 deletions python/lsst/ts/observatory/control/base_calsys.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import logging
import typing

import jsonschema
import yaml
from lsst.ts import salobj
from lsst.ts.xml.enums.Electrometer import UnitToRead
Expand Down Expand Up @@ -186,23 +187,44 @@ async def calculate_optimized_exposure_times(
def load_calibration_config_file(self, filename: str | None = None) -> None:
"""Load the calibration configuration file.
By default it will determine the filename based on
the class name. However, it is possible to provide
an override with the full file name.
By default it will determine the filename based on the class
name. However, it is possible to provide an override with the
full file name.
It performs schema validation of the calibration configuration
file against the corresponding schema validation YAML file. The
schema file must be present and should be named with the class
name followed by the '_schema' suffix. If the validation fails,
an exception is raised.
Parameters
----------
filename : `str`, optional
Alternative file name with the calibration
configuration file.
Alternative file name with the calibration configuration
file.
Raises
------
FileNotFoundError:
If the calibration configuration file or the schema
validation file doesn't exist.
RuntimeError:
If the validation of the calibration configuration file
fails.
"""

base_name = type(self).__name__.lower()

data_path = (
(get_data_path() / f"{type(self).__name__.lower()}.yaml").as_posix()
(get_data_path() / f"{base_name}.yaml").as_posix()
if filename is None
else filename
)

schema_path = (get_data_path() / f"{base_name}_schema.yaml").as_posix()

if len(self.calibration_config) > 0:
self.log.warning(
"Calibration configuration already loaded."
Expand All @@ -212,6 +234,21 @@ def load_calibration_config_file(self, filename: str | None = None) -> None:
with open(data_path, "r") as f:
self.calibration_config = yaml.safe_load(f)

with open(schema_path, "r") as f:
config_validator = salobj.DefaultingValidator(schema=yaml.safe_load(f))

validation_errors = ""
for item in self.calibration_config:
try:
config_validator.validate(self.calibration_config[item])
except jsonschema.ValidationError as e:
validation_errors += f"\t{item} failed validation: {e.message}.\n"
self.log.exception(f"{item} failed validation.")
if validation_errors:
raise RuntimeError(
f"Failed schema validation:\n{validation_errors}Check logs for more information."
)

def get_calibration_configuration(self, name: str) -> dict[str, typing.Any]:
"""Returns the configuration attributes given a configuration
name.
Expand Down
67 changes: 67 additions & 0 deletions python/lsst/ts/observatory/control/data/atcalsys_schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
$schema: http://json-schema.org/draft-07/schema#
$id: https://github.com/lsst-ts/ts_observatory_control/blob/develop/python/lsst/ts/observatory/control/data/atcalsys_schema.yaml
title: Configuration file validation schema of Auxiliary Telescope Calibration System
type: object
properties:
calib_type:
type: string
enum:
- Mono
- WhiteLight
use_camera:
type: boolean
atspec_filter:
type: string
atspec_grating:
type: string
wavelength:
type: number
wavelength_width:
type: number
wavelength_resolution:
type: number
monochromator_grating:
type: string
enum:
- MIRROR
- RED
- BLUE
exit_slit:
type: number
entrance_slit:
type: number
electrometer_integration_time:
type: number
electrometer_mode:
type: string
enum:
- CURRENT
- CHARGE
- VOLTAGE
- RESISTANCE
electrometer_range:
type: number
use_fiberspectrograph:
type: boolean
use_electrometer:
type: boolean
exposure_times:
type: array
items:
type: number
required:
- calib_type
- use_camera
- atspec_filter
- atspec_grating
- wavelength
- monochromator_grating
- exit_slit
- entrance_slit
- electrometer_integration_time
- electrometer_mode
- electrometer_range
- use_fiberspectrograph
- use_electrometer
- exposure_times
additionalProperties: false
75 changes: 75 additions & 0 deletions python/lsst/ts/observatory/control/data/mtcalsys_schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
$schema: http://json-schema.org/draft-07/schema#
$id: https://github.com/lsst-ts/ts_observatory_control/blob/develop/python/lsst/ts/observatory/control/data/mtcalsys_schema.yaml
title: Configuration file validation schema of Main Telescope Calibration System
type: object
properties:
calib_type:
type: string
enum:
- Mono
- WhiteLight
use_camera:
type: boolean
mtcamera_filter:
type: string
led_name:
type: array
items:
type: string
enum:
- M385L3
- M455L4
- M505L4
- M565L3
- M660L4
- M730L5
- M780LP1
- M850L3
- M940L3
- M970L4
wavelength:
type: number
wavelength_width:
type: number
wavelength_resolution:
type: number
led_location:
type: number
led_focus:
type: number
use_electrometer:
type: boolean
use_fiberspectrograph_red:
type: boolean
use_fiberspectrograph_blue:
type: boolean
electrometer_integration_time:
type: number
electrometer_mode:
type: string
enum:
- CURRENT
- CHARGE
- VOLTAGE
- RESISTANCE
electrometer_range:
type: number
exposure_times:
type: array
items:
type: number
required:
- calib_type
- use_camera
- mtcamera_filter
- wavelength
- led_location
- led_focus
- use_electrometer
- use_fiberspectrograph_red
- use_fiberspectrograph_blue
- electrometer_integration_time
- electrometer_mode
- electrometer_range
- exposure_times
additionalProperties: false

0 comments on commit 5284eb0

Please sign in to comment.