Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Entries for releases before this file existed were generated from commit subject

## [Unreleased]

- The GUI writes a FITS header into the frames it saves (it was empty). `SbigCamera` and the GUI build the header cards
with `make_frame_header` from pyobs-core, so key names and comments are the same everywhere. Needs pyobs-core 2.28.0.
- Closing the camera warms up first (pyobs-core `CoolingRampMixin`). The time limit `cooling_close_timeout` is now estimated from the current setpoint and `cooling_rate_warm` instead of a fixed 300 s, so a deeply cooled camera is warmed up completely before it is closed. Needs pyobs-core 2.28.0.
- Switching cooling off sets the camera to the off temperature (20 degC by default) or, if the sensor is warmer than
that, to just above the sensor, so that the cooler does not keep cooling below ambient. Errors when polling the
cooling status are logged (once, until it works again) instead of being ignored. Needs pyobs-core 2.27.0.
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "pyobs-sbig"
version = "2.1.1"
version = "2.2.0"
description = "pyobs module for SBIG cameras"
authors = [{ name = "Tim-Oliver Husser", email = "thusser@uni-goettingen.de" }]
requires-python = ">=3.11"
Expand All @@ -9,7 +9,7 @@ readme = "README.md"
dependencies = [
"astropy>=7.0.1,<8",
"numpy>=2.2.5,<3",
"pyobs-core>=2.27.0,<3",
"pyobs-core>=2.28.0,<3",
]

[project.urls]
Expand All @@ -20,7 +20,7 @@ Issues = "https://github.com/pyobs/pyobs-sbig/issues"

[project.optional-dependencies]
gui = [
"pyobs-core[gui]>=2.24.0,<3",
"pyobs-core[gui]>=2.28.0,<3",
"qasync>=0.28.0",
]

Expand Down
59 changes: 56 additions & 3 deletions src/pyobs_sbig/gui.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import asyncio
import sys
import time
from datetime import UTC, datetime
from typing import Any

import qasync # type: ignore
from astropy.io import fits
from numpy.typing import NDArray
from pyobs.utils.frameheader import FrameSettings, make_frame_header
from pyobs.utils.gui.camera import (
BinningWidget,
DataDisplayWidget,
Expand All @@ -16,6 +20,48 @@
from .sbigudrv import SBIGCam, SBIGImg # type: ignore


def make_header(
cam: SBIGCam,
date_obs: datetime,
exposure_time: float,
window: tuple[int, int, int, int],
binning: tuple[int, int],
data: NDArray[Any],
) -> fits.Header:
"""Builds the FITS header for a frame, reading temperature and cooling from the camera (blocking).

Args:
cam: Camera to read the cooling from.
date_obs: UTC time the exposure was started.
exposure_time: Exposure time in seconds.
window: Left, top, width and height of the window in binned pixels, as the camera takes them.
binning: Binning in x and y.
data: The frame.

Returns:
The header.
"""
_, temp, setpoint, power = cam.get_cooling()
return make_frame_header(
FrameSettings(
date_obs=date_obs,
exposure_time=exposure_time,
binning=binning,
# the camera takes the window in binned pixels, FITS wants unbinned ones
window=(
window[0] * binning[0],
window[1] * binning[1],
window[2] * binning[0],
window[3] * binning[1],
),
temperature=temp,
setpoint=setpoint,
cooler_power=round(power * 100),
data=data,
)
)


class MainWindow(QtWidgets.QMainWindow):
def __init__(self) -> None:
super().__init__()
Expand Down Expand Up @@ -64,9 +110,13 @@ async def _expose_clicked(self) -> None:
loop = asyncio.get_running_loop()

# set exposure time, window and binning, then start the exposure off the Qt thread
date_obs = datetime.now(UTC)
exposure_time = self.exposure_time.value
window = self.window_widget.values

def _start() -> None:
self._cam.exposure_time = self.exposure_time.value
self._cam.window = self.window_widget.values
self._cam.exposure_time = exposure_time
self._cam.window = window
self._cam.binning = binning
self._cam.start_exposure(self._img, False)

Expand Down Expand Up @@ -103,7 +153,10 @@ def _wait() -> bool:
data = self._img.data

self.expose.set_exposures_left()
image = fits.PrimaryHDU(data)
header = await loop.run_in_executor(
None, make_header, self._cam, date_obs, exposure_time, window, binning, data
)
image = fits.PrimaryHDU(data, header=header)
self.data_display_widget.set_data(image)

@qasync.asyncSlot() # type: ignore
Expand Down
34 changes: 15 additions & 19 deletions src/pyobs_sbig/sbigcamera.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from datetime import UTC, datetime
from typing import Any, TypeVar, cast

import numpy as np
from pyobs.images import Image
from pyobs.interfaces import IAbortable, IBinning, ICamera, ICooling, ITemperatures, IWindow
from pyobs.interfaces.IBinning import Binning, BinningCapabilities, BinningState
Expand All @@ -17,6 +16,7 @@
from pyobs.modules.camera.basecamera import BaseCamera
from pyobs.utils import exceptions as exc
from pyobs.utils.enums import ExposureStatus
from pyobs.utils.frameheader import FrameSettings, make_frame_header

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -290,24 +290,20 @@ def _get_cooling() -> tuple[Any, float, float, float]:

# create FITS image and set header
img = Image(data)
img.header["DATE-OBS"] = (date_obs, "Date and time of start of exposure")
img.header["EXPTIME"] = (exposure_time, "Exposure time [s]")
img.header["DET-TEMP"] = (temp, "CCD temperature [C]")
img.header["DET-TSET"] = (setpoint, "Cooler setpoint [C]")
img.header["DET-COOL"] = (round(power * 100), "Cooler power [percent]")

# binning
img.header["XBINNING"] = img.header["DET-BIN1"] = (self._binning[0], "Binning factor used on X axis")
img.header["YBINNING"] = img.header["DET-BIN2"] = (self._binning[1], "Binning factor used on Y axis")

# window
img.header["XORGSUBF"] = (self._window[0], "Subframe origin on X axis")
img.header["YORGSUBF"] = (self._window[1], "Subframe origin on Y axis")

# statistics
img.header["DATAMIN"] = (float(np.min(data)), "Minimum data value")
img.header["DATAMAX"] = (float(np.max(data)), "Maximum data value")
img.header["DATAMEAN"] = (float(np.mean(data)), "Mean data value")
img.header.update(
make_frame_header(
FrameSettings(
date_obs=date_obs,
exposure_time=exposure_time,
binning=self._binning,
window=self._window,
temperature=temp,
setpoint=setpoint,
cooler_power=round(power * 100),
data=data,
)
)
)

# biassec/trimsec
self.set_biassec_trimsec(img.header, *self._full_frame)
Expand Down
30 changes: 30 additions & 0 deletions tests/test_guiheader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Tests for the FITS header the GUI writes."""

from __future__ import annotations

from datetime import UTC, datetime

import numpy as np
import pytest

try:
from pyobs_sbig.gui import make_header
except ImportError as e: # no Qt extras, or the camera library is not installed
pytest.skip(f"GUI cannot be imported: {e}", allow_module_level=True)


class FakeCam:
def get_cooling(self) -> tuple[bool, float, float, float]:
return True, -9.5, -10.0, 0.5


DATE = datetime(2026, 11, 17, 10, 0, 0, tzinfo=UTC)
DATA = np.array([[1, 2], [3, 6]], dtype=np.uint16)


def test_header_has_the_module_keys() -> None:
h = make_header(FakeCam(), DATE, 5.0, (10, 20, 100, 100), (2, 2), DATA) # type: ignore[arg-type]
assert h["EXPTIME"] == 5.0 and h["DATE-OBS"] == "2026-11-17T10:00:00.000000"
assert h["DET-TEMP"] == -9.5 and h["DET-TSET"] == -10.0 and h["DET-COOL"] == 50
assert h["DET-BIN1"] == 2 and h["XORGSUBF"] == 20 and h["YORGSUBF"] == 40
assert (h["DATAMIN"], h["DATAMAX"], h["DATAMEAN"]) == (1.0, 6.0, 3.0)
Loading
Loading