From d8fa3db01a58f73db44283aa953527ef1cbcfcaf Mon Sep 17 00:00:00 2001 From: James Chiang Date: Thu, 28 May 2026 17:32:19 -0700 Subject: [PATCH] use a temp dir to do an atomic write for the raw file output; update test code to actually do the write --- imsim/readout.py | 9 ++++++++- tests/test_raw_file_writing.py | 12 +++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/imsim/readout.py b/imsim/readout.py index fea757e1..444418d3 100644 --- a/imsim/readout.py +++ b/imsim/readout.py @@ -1,4 +1,5 @@ import os +import tempfile import numpy as np import subprocess import scipy @@ -529,7 +530,13 @@ def prepare_hdus(self, rng): def write_raw_file(hdus, file_name): """Write the raw data file.""" hdus[0].header['OUTFILE'] = os.path.basename(file_name) - hdus.writeto(file_name, overwrite=True) + + # Use a temporary directory to do an atomic write. + dir_name = os.path.dirname(os.path.abspath(file_name)) + with tempfile.TemporaryDirectory(dir=dir_name) as tmp_dir: + tmp_path = os.path.join(tmp_dir, "raw_file.fits.fz") + hdus.writeto(tmp_path) + os.replace(tmp_path, file_name) class CameraReadout(ExtraOutputBuilder): diff --git a/tests/test_raw_file_writing.py b/tests/test_raw_file_writing.py index ebd79e2f..95dc6fc3 100644 --- a/tests/test_raw_file_writing.py +++ b/tests/test_raw_file_writing.py @@ -3,6 +3,7 @@ """ import os from pathlib import Path +import logging import unittest from astropy.io import fits import galsim @@ -40,9 +41,11 @@ def test_raw_file_writing(self): added_keywords = {'GAUSFWHM': 0.4, 'FOOBAR': 'hello, world'} - hdu = imsim.get_primary_hdu(eimage, "E2V-CCD250-382", - added_keywords=added_keywords) - hdr = hdu.header + logger = logging.getLogger() + ccd_readout = imsim.CcdReadout(eimage, logger, added_keywords=added_keywords) + rng = galsim.BaseDeviate() + hdus = ccd_readout.prepare_hdus(rng) + hdr = hdus[0].header # Test some keywords. self.assertAlmostEqual(hdr['RA'], eimage.header['RATEL']) @@ -66,6 +69,9 @@ def test_raw_file_writing(self): for key, value in added_keywords.items(): self.assertEqual(hdr[key], value) + # Write the file. + ccd_readout.write_raw_file(hdus, self.outfile) + if __name__ == '__main__': unittest.main()