Skip to content

Commit

Permalink
Merge pull request #225 from LSSTDESC/u/jchiang/add_option_to_move_ch…
Browse files Browse the repository at this point in the history
…eckpoint_files

add option to specify archive directory for checkpoint files
  • Loading branch information
jchiang87 authored Aug 26, 2019
2 parents c7654ac + 3b6b33c commit a3e4b71
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
7 changes: 6 additions & 1 deletion bin.src/imsim.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
"This will be prepended to any existing IMSIM_IMAGE_PATH "
"environment variable, for which $CWD is included by "
"default.")
parser.add_argument('--ckpt_archive_dir', type=str, default=None,
help="Archive directory for checkpoint files. "
"If None, then delete them (if the checkpointing.cleanup "
"configuration is True).")

args = parser.parse_args()

Expand Down Expand Up @@ -89,6 +93,7 @@
apply_sensor_model=apply_sensor_model,
create_centroid_file=args.create_centroid_file,
file_id=args.file_id,
log_level=args.log_level)
log_level=args.log_level,
ckpt_archive_dir=args.ckpt_archive_dir)

image_simulator.run(processes=args.processes)
32 changes: 27 additions & 5 deletions python/desc/imsim/ImageSimulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import warnings
import gzip
import shutil
import tempfile
import sqlite3
import numpy as np
from astropy._erfa import ErfaWarning
Expand Down Expand Up @@ -49,7 +50,8 @@ class ImageSimulator:
"""
def __init__(self, instcat, psf, numRows=None, config=None, seed=267,
outdir='fits', sensor_list=None, apply_sensor_model=True,
create_centroid_file=False, file_id=None, log_level='WARN'):
create_centroid_file=False, file_id=None, log_level='WARN',
ckpt_archive_dir=None):
"""
Parameters
----------
Expand Down Expand Up @@ -80,6 +82,12 @@ def __init__(self, instcat, psf, numRows=None, config=None, seed=267,
If None, then no checkpoint file will be used
log_level: str ['WARN']
Logging level ('DEBUG', 'INFO', 'WARN', 'ERROR', 'CRITICAL').
ckpt_archive_dir: str [None]
If this is not None, then after a sensor-visit has written
its output FITS file(s), the associated checkpoint file will
be moved to this directory instead of deleted. If set to
`None`, then delete the checkpoint file (assuming the
`checkpointing.cleanup` config parameter is True).
"""
self.config = read_config(config)
self.log_level = log_level
Expand All @@ -103,6 +111,10 @@ def __init__(self, instcat, psf, numRows=None, config=None, seed=267,
self._make_gs_interpreters(seed, sensor_list, file_id)
self.log_level = log_level
self.logger = get_logger(self.log_level, name='ImageSimulator')
self.ckpt_archive_dir = ckpt_archive_dir
if (self.ckpt_archive_dir is not None and
not os.path.isdir(self.ckpt_archive_dir)):
os.makedirs(self.ckpt_archive_dir)

def _gather_checkpoint_files(self, sensor_list, file_id=None):
"""
Expand Down Expand Up @@ -423,13 +435,23 @@ def __call__(self, gs_objects):
# Write out the centroid files if they were made.
gs_interpreter.write_centroid_files()

# The image for the sensor-visit has been drawn, so delete any
# existing checkpoint file if the config says to do so.
# The image for the sensor-visit has been drawn, so delete or
# move to the archive area any existing checkpoint file if the
# config says perform the cleanup.
if (gs_interpreter.checkpoint_file is not None
and os.path.isfile(gs_interpreter.checkpoint_file)
and IMAGE_SIMULATOR.config['checkpointing']['cleanup']):
os.remove(gs_interpreter.checkpoint_file)

if IMAGE_SIMULATOR.ckpt_archive_dir is None:
os.remove(gs_interpreter.checkpoint_file)
else:
src_file = gs_interpreter.checkpoint_file
dest_dir = os.path.abspath(IMAGE_SIMULATOR.ckpt_archive_dir)
dest_file = os.path.join(dest_dir, os.path.basename(src_file))
with tempfile.NamedTemporaryFile(mode='wb', delete=False,
dir=dest_dir) as tmp:
shutil.copy(src_file, tmp.name)
os.rename(tmp.name, dest_file)
os.remove(src_file)
# Remove reference to gs_interpreter in order to recover the
# memory associated with that object.
IMAGE_SIMULATOR.gs_interpreters[self.sensor_name] = None
Expand Down

0 comments on commit a3e4b71

Please sign in to comment.