Skip to content

Commit 61c10e3

Browse files
authored
Merge pull request #2519 from desihub/emline_outfile
emline use tempfile for output
2 parents 6132ed6 + 74745c7 commit 61c10e3

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

py/desispec/io/emlinefit.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from desiutil.dust import ebv as dust_ebv
1616
from desiutil.log import get_logger
1717
from desispec.emlinefit import get_rf_em_waves
18-
from .util import checkgzip
18+
from .util import checkgzip, get_tempfilename
1919

2020
def get_targetids(d, bitnames, log=None):
2121
"""
@@ -355,13 +355,14 @@ def write_emlines(
355355
for i_emname, emname in enumerate(emnames):
356356
hdr["RFWAVE{:02d}".format(i_emname)] = ",".join(get_rf_em_waves(emname).astype(str))
357357

358-
# AR write to fits
358+
# AR write to fits via tempfile to avoid corrupted final file in case of a crash
359359
if os.path.isfile(output):
360360
log.info("Removing existing {}".format(output))
361361
os.remove(output)
362-
fd = fitsio.FITS(output, "rw")
363-
fd.write(d, extname="EMLINEFIT", header=hdr)
364-
fd.close()
362+
tmpfile = get_tempfilename(output)
363+
with fitsio.FITS(tmpfile, "rw") as fd:
364+
fd.write(d, extname="EMLINEFIT", header=hdr)
365+
os.rename(tmpfile, output)
365366

366367

367368
def plot_emlines(

0 commit comments

Comments
 (0)