Skip to content

Commit

Permalink
write selected LSST Stack package versions and tags to FITS primary h…
Browse files Browse the repository at this point in the history
…eader for the set up products
  • Loading branch information
jchiang87 committed Oct 1, 2019
1 parent d1eac95 commit 4bcf33a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
5 changes: 5 additions & 0 deletions data/default_imsim_configs
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@ sort_magnorm = True
# AtmosphericPSF + OptWF PSF to account for additional instrumental
# effects.
gaussianFWHM = 0.4

[stack_packages]
lsst_sims
throughputs
sims_skybrightness_data
15 changes: 13 additions & 2 deletions python/desc/imsim/camera_readout.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
getRotSkyPos, ObservationMetaData, altAzPaFromRaDec
from lsst.sims.GalSimInterface import LsstObservatory
from .camera_info import CameraInfo, getHourAngle
from .imSim import get_logger, get_config, airmass
from .imSim import get_logger, get_config, airmass, get_stack_products
from .cosmic_rays import CosmicRays
from .version import __version__ as imsim_version

Expand Down Expand Up @@ -476,7 +476,6 @@ def write_fits_file(self, outfile, overwrite=True, run_number=None,
output[0].header.insert(5, ('WCSAXES', wcsaxes, ''))
if run_number is None:
run_number = self.visit
output[0].header['IMSIMVER'] = imsim_version
output[0].header['RUNNUM'] = str(run_number)
output[0].header['DARKTIME'] = output[0].header['EXPTIME']
output[0].header['TIMESYS'] = 'TAI'
Expand Down Expand Up @@ -531,6 +530,18 @@ def write_fits_file(self, outfile, overwrite=True, run_number=None,
amp_name = '_C'.join((self.sensor_id, seg_id))
output.append(self.get_amplifier_hdu(amp_name, compress=compress))
output[-1].header['EXTNAME'] = 'Segment%s' % seg_id

# Set the imSim version and LSST Stack product versions and
# tags in the primary HDU.
output[0].header['IMSIMVER'] = imsim_version
products = get_stack_products()
for iprod, (product_name, product) in enumerate(products.items()):
output[0].header[f'PKG{iprod:05d}'] = product_name
output[0].header[f'VER{iprod:05d}'] = product.version
# Use the "first" semantically meaningful tag.
tag = sorted([_ for _ in product.tags if _ != 'current'])[0]
output[0].header[f'TAG{iprod:05d}'] = tag

self.fits_atomic_write(output, outfile, overwrite=overwrite)

@staticmethod
Expand Down
27 changes: 25 additions & 2 deletions python/desc/imsim/imSim.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import copy
import psutil
import galsim
import eups

# python_future no longer handles configparser as of 0.16.
# This is needed for PY2/3 compatibility.
Expand Down Expand Up @@ -61,7 +62,8 @@
'_POINT_SOURCE', '_SERSIC_2D', '_RANDOM_WALK', '_FITS_IMAGE',
'parsePhoSimInstanceFile',
'add_treering_info', 'airmass', 'FWHMeff', 'FWHMgeom', 'make_psf',
'save_psf', 'load_psf', 'TracebackDecorator', 'GsObjectList']
'save_psf', 'load_psf', 'TracebackDecorator', 'GsObjectList',
'get_stack_products']


class PhosimInstanceCatalogParseError(RuntimeError):
Expand Down Expand Up @@ -611,7 +613,7 @@ def read_config(config_file=None):
config_file.
"""
my_config = ImSimConfiguration()
cp = configparser.ConfigParser()
cp = configparser.ConfigParser(allow_no_value=True)
cp.optionxform = str
if config_file is None:
config_file = os.path.join(lsstUtils.getPackageDir('imsim'),
Expand Down Expand Up @@ -904,3 +906,24 @@ def __call__(self, *args, **kwds):
except Exception as eobj:
traceback.print_exc()
raise eobj

def get_stack_products(product_names=None):
"""
Get the LSST Stack products corresponding to a list of product
names.
Parameters
----------
product_names: list-like [None]
A list of LSST Stack package names for which to get the
corresponding set up eups.Product. If None, then return the
products listed in the config file.
Returns
-------
dict of eups.Products keyed by package name.
"""
config = get_config()
stack_packages = set(config['stack_packages'].keys())
eupsenv = eups.Eups()
return {_: eupsenv.getSetupProducts(_)[0] for _ in stack_packages}
38 changes: 38 additions & 0 deletions tests/test_get_stack_products.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
Unit test for get_stack_products function.
"""
import unittest
import subprocess
from desc.imsim import get_stack_products


class GetStackProductsTestCase(unittest.TestCase):
"""
TestCase subclass for testing get_stacks_products.
"""
def setUp(self):
pass

def tearDown(self):
pass

def test_get_stack_products(self):
"""Test the get_stack_products function."""
targets = 'lsst_sims throughputs sims_skybrightness_data'.split()
products = get_stack_products(targets)

for target in targets:
# Test result against eups command line result.
command = f'eups list {target} -s'
line = subprocess.check_output(command, shell=True)
tokens = line.decode('utf-8').strip().split()
version = tokens[0]
tags = set(tokens[1:])
tags.remove('setup')

self.assertEqual(products[target].version, version)
self.assertEqual(set(products[target].tags), tags)


if __name__ == '__main__':
unittest.main()

0 comments on commit 4bcf33a

Please sign in to comment.