Skip to content

Commit

Permalink
Merge pull request #825 from mperrin/improve_speed_siaf
Browse files Browse the repository at this point in the history
Performance enhancement: avoid repeated slow loads of the SIAF
  • Loading branch information
obi-wan76 authored Apr 2, 2024
2 parents be9aefb + 4123c02 commit 9040bd7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
3 changes: 2 additions & 1 deletion webbpsf/distortion.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from scipy.ndimage import rotate

from soc_roman_tools.siaf.siaf import RomanSiaf
import webbpsf.webbpsf_core

def _get_default_siaf(instrument, aper_name):
"""
Expand Down Expand Up @@ -39,7 +40,7 @@ def _get_default_siaf(instrument, aper_name):
siaf = RomanSiaf()
aper = siaf[aper_name]
else:
siaf = pysiaf.Siaf(siaf_name)
siaf = webbpsf.webbpsf_core.get_siaf_with_caching(siaf_name)
aper = siaf.apertures[aper_name]

return aper
Expand Down
2 changes: 1 addition & 1 deletion webbpsf/opds.py
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ def __init__(self, name='Unnamed OPD', opd=None, opd_index=0, transmission=None,
elif 'nrs' in control_point_detector:
control_point_instr = 'nirspec'

self.ote_control_point = pysiaf.Siaf(control_point_instr)[self.control_point_fieldpoint.upper()].reference_point('tel')*u.arcsec
self.ote_control_point = webbpsf.webbpsf_core.get_siaf_with_caching(control_point_instr)[self.control_point_fieldpoint.upper()].reference_point('tel')*u.arcsec

if zero:
self.zero()
Expand Down
3 changes: 2 additions & 1 deletion webbpsf/optics.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from scipy.interpolate import griddata, RegularGridInterpolator
from scipy.ndimage import rotate

from . import webbpsf_core
from . import utils
from . import constants

Expand Down Expand Up @@ -735,7 +736,7 @@ def __init__(self, name="unnamed BLC", kind='nircamcircular', module='A', nd_squ
raise NotImplementedError("invalid name for NIRCam occulter: " + self.name)

# EDIT: updated on 8 Dec 2021 to grab offsets directly from pySIAF
self.siaf = pysiaf.Siaf('NIRCAM')
self.siaf = webbpsf_core.get_siaf_with_caching('NIRCAM')
self.offset_swb = {filt: self.get_bar_offset_from_siaf(filt, channel='SW')
for filt in ["F182M", "F187N", "F210M", "F212N", "F200W", 'narrow']}
self.offset_lwb = {filt: self.get_bar_offset_from_siaf(filt, channel='LW')
Expand Down
10 changes: 9 additions & 1 deletion webbpsf/webbpsf_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import os
import glob
from collections import namedtuple, OrderedDict
import functools
import numpy as np
import scipy.interpolate, scipy.ndimage

Expand Down Expand Up @@ -753,7 +754,7 @@ class JWInstrument(SpaceTelescopeInstrument):
def __init__(self, *args, **kwargs):
super(JWInstrument, self).__init__(*args, **kwargs)

self.siaf = pysiaf.Siaf(self.name)
self.siaf = get_siaf_with_caching(self.name)

opd_path = os.path.join(self._datapath, 'OPD')
self.opd_list = []
Expand Down Expand Up @@ -2932,6 +2933,13 @@ def calc_or_load_PSF(filename, inst, overwrite=False, **kwargs):

#########################

@functools.lru_cache
def get_siaf_with_caching(instrname):
""" Parsing and loading the SIAF information is particularly time consuming,
(can be >0.1 s per call, so multiple invokations can be a large overhead)
Therefore avoid unnecessarily reloading it by caching results.
This is a small speed optimization. """
return pysiaf.Siaf(instrname)

class DetectorGeometry(object):
""" Utility class for converting between detector coordinates
Expand Down

0 comments on commit 9040bd7

Please sign in to comment.