Skip to content

Commit

Permalink
Merge pull request #70 from Johannes-Sahlmann/ignore-case
Browse files Browse the repository at this point in the history
Allow for flexible capitalization of instrument names
  • Loading branch information
Johannes-Sahlmann authored Nov 14, 2018
2 parents 495b5c0 + 4b18df0 commit 25cc564
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 54 deletions.
19 changes: 9 additions & 10 deletions pysiaf/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@

__all__ = ['JWST_PRD_VERSION', 'JWST_PRD_DATA_ROOT', 'HST_PRD_VERSION', 'HST_PRD_DATA_ROOT']

_DATA_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'prd_data')
_THIS_DIRECTORY = os.path.dirname(os.path.abspath(__file__))

# _JWST_STAGING_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'staging_data')
_JWST_TEMPORARY_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'temporary_data')
_DATA_ROOT = os.path.join(_THIS_DIRECTORY, 'prd_data')

JWST_SOURCE_DATA_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'source_data')
_JWST_TEMPORARY_ROOT = os.path.join(_THIS_DIRECTORY, 'temporary_data')

JWST_TEMPORARY_DATA_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'temporary_data')
JWST_SOURCE_DATA_ROOT = os.path.join(_THIS_DIRECTORY, 'source_data')

JWST_DELIVERY_DATA_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'pre_delivery_data')
JWST_TEMPORARY_DATA_ROOT = os.path.join(_THIS_DIRECTORY, 'temporary_data')

JWST_DELIVERY_DATA_ROOT = os.path.join(_THIS_DIRECTORY, 'pre_delivery_data')

# test data directory
TEST_DATA_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tests', 'test_data')
TEST_DATA_ROOT = os.path.join(_THIS_DIRECTORY, 'tests', 'test_data')

# directory for reports
REPORTS_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'reports')
REPORTS_ROOT = os.path.join(_THIS_DIRECTORY, 'reports')

AVAILABLE_PRD_JWST_VERSIONS = [os.path.basename(dir_name) for dir_name in
glob.glob(os.path.join(_DATA_ROOT, 'JWST', '*'))]
Expand Down
62 changes: 36 additions & 26 deletions pysiaf/iando/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import lxml.etree as ET

from ..constants import HST_PRD_DATA_ROOT, JWST_PRD_DATA_ROOT, JWST_SOURCE_DATA_ROOT

from ..siaf import JWST_INSTRUMENT_NAME_MAPPING

def get_siaf(input_siaf, observatory='JWST'):
"""Return a Siaf object corresponding to input_siaf which can be a string path or a Siaf object.
Expand All @@ -44,12 +44,12 @@ def get_siaf(input_siaf, observatory='JWST'):
# initilize siaf as empty object
siaf_object = siaf.Siaf(None)
siaf_object.instrument = aperture_collection[list(aperture_collection.items())[0][0]].\
InstrName
InstrName.lower()

if siaf_object.instrument == 'NIRCAM':
siaf_object.instrument = 'NIRCam'
elif siaf_object.instrument == 'NIRSPEC':
siaf_object.instrument = 'NIRSpec'
# if siaf_object.instrument == 'NIRCAM':
# siaf_object.instrument = 'NIRCam'
# elif siaf_object.instrument == 'NIRSPEC':
# siaf_object.instrument = 'NIRSpec'

siaf_object.apertures = aperture_collection
siaf_object.description = os.path.basename(input_siaf)
Expand Down Expand Up @@ -256,9 +256,12 @@ def read_jwst_siaf(instrument=None, filename=None, basepath=None):
Parameters
----------
instrument
filename
basepath
instrument : str
instrument name (case-insensitive)
filename : str
Absolute path to alternative SIAF xml file
basepath : str
Directory containing alternative SIAF xml file conforming with standard naming convention
Returns
-------
Expand All @@ -277,7 +280,8 @@ def read_jwst_siaf(instrument=None, filename=None, basepath=None):
if not os.path.isdir(basepath):
raise RuntimeError("Could not find SIAF data "
"in {}".format(basepath))
filename = os.path.join(basepath, instrument + '_SIAF.xml')
filename = os.path.join(basepath, JWST_INSTRUMENT_NAME_MAPPING[instrument.lower()]
+ '_SIAF.xml')
else:
filename = filename

Expand Down Expand Up @@ -377,18 +381,18 @@ def read_siaf_aperture_definitions(instrument):
Parameters
----------
instrument
instrument : str
instrument name (case insensitive)
Returns
-------
: astropy table
content of SIAF reference file
"""
filename = os.path.join(JWST_SOURCE_DATA_ROOT, instrument, '{}_siaf_aperture_definition.txt'.
format(instrument.lower()))
filename = os.path.join(JWST_SOURCE_DATA_ROOT, JWST_INSTRUMENT_NAME_MAPPING[instrument.lower()],
'{}_siaf_aperture_definition.txt'.format(instrument.lower()))

# converters = {'XDetRef': [ascii.convert_numpy(np.float32)]}
# , converters = converters, guess = False
return Table.read(filename, format='ascii.basic', delimiter=',', fill_values=('None', 0))


Expand All @@ -397,14 +401,15 @@ def read_siaf_ddc_mapping_reference_file(instrument):
Parameters
----------
instrument
instrument : str
instrument name (case insensitive)
Returns
-------
: astropy table
"""
ddc_mapping_file = os.path.join(JWST_SOURCE_DATA_ROOT, instrument,
ddc_mapping_file = os.path.join(JWST_SOURCE_DATA_ROOT, JWST_INSTRUMENT_NAME_MAPPING[instrument.lower()],
'{}_siaf_ddc_apername_mapping.txt'.format(instrument.lower()))

ddc_mapping_table = Table.read(ddc_mapping_file, format='ascii.basic', delimiter=',')
Expand Down Expand Up @@ -435,15 +440,16 @@ def read_siaf_detector_reference_file(instrument):
Parameters
----------
instrument
instrument : str
instrument name (case insensitive)
Returns
-------
: astropy table
"""
filename = os.path.join(JWST_SOURCE_DATA_ROOT, instrument, '{}_siaf_detector_parameters.txt'.
format(instrument.lower()))
filename = os.path.join(JWST_SOURCE_DATA_ROOT, JWST_INSTRUMENT_NAME_MAPPING[instrument.lower()],
'{}_siaf_detector_parameters.txt'.format(instrument.lower()))

return Table.read(filename, format='ascii.basic', delimiter=',')

Expand All @@ -453,15 +459,17 @@ def read_siaf_distortion_coefficients(instrument, aperture_name):
Parameters
----------
instrument
aperture_name
instrument : str
instrument name (case insensitive)
aperture_name : str
name of master aperture
Returns
-------
: astropy table
"""
distortion_reference_file_name = os.path.join(JWST_SOURCE_DATA_ROOT, instrument,
distortion_reference_file_name = os.path.join(JWST_SOURCE_DATA_ROOT, JWST_INSTRUMENT_NAME_MAPPING[instrument.lower()],
'{}_siaf_distortion_{}.txt'.format(
instrument.lower(), aperture_name.lower()))

Expand All @@ -473,13 +481,15 @@ def read_siaf_xml_field_format_reference_file(instrument):
Parameters
----------
instrument
instrument : str
instrument name (case insensitive)
Returns
-------
: astropy table
"""
filename = os.path.join(JWST_SOURCE_DATA_ROOT, instrument, '{}_siaf_xml_field_format.txt'.
format(instrument.lower()))
filename = os.path.join(JWST_SOURCE_DATA_ROOT, JWST_INSTRUMENT_NAME_MAPPING[instrument.lower()],
'{}_siaf_xml_field_format.txt'.format(instrument.lower()))

return Table.read(filename, format='ascii.basic', delimiter=',')
37 changes: 23 additions & 14 deletions pysiaf/siaf.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,15 @@ def plot_master_apertures(**kwargs):
ax.set_xlim(xlim[::-1])


ACCEPTED_INSTRUMENT_NAMES = 'nircam niriss miri nirspec fgs hst'.split()

# mapping from internal lower-case names to mixed-case names used for xml file names
JWST_INSTRUMENT_NAME_MAPPING = {'nircam': 'NIRCam',
'nirspec': 'NIRSpec',
'miri': 'MIRI',
'niriss': 'NIRISS',
'fgs': 'FGS'}

class Siaf(ApertureCollection):
"""Science Instrument Aperture File class.
Expand Down Expand Up @@ -259,7 +268,7 @@ def __init__(self, instrument, filename=None, basepath=None, AperNames=None):
Parameters
-----------
instrument : string
one of 'NIRCam', 'NIRSpec', 'NIRISS', 'MIRI', 'FGS'; case sensitive.
one of 'NIRCam', 'NIRSpec', 'NIRISS', 'MIRI', 'FGS'; case-insensitive.
basepath : string
Directory to look in for SIAF files
filename : string, optional
Expand All @@ -268,20 +277,20 @@ def __init__(self, instrument, filename=None, basepath=None, AperNames=None):
"""
super(Siaf, self).__init__()

if instrument is None:
return
if (instrument is None) or (isinstance(instrument, str) is False):
raise RuntimeError('Please specify a valid instrument name.')

elif instrument not in ['NIRCam', 'NIRSpec', 'NIRISS', 'MIRI', 'FGS', 'HST']:
raise ValueError('Invalid instrument name: {0}. Note that this is case '
'sensitive.'.format(instrument))
elif instrument.lower() not in ACCEPTED_INSTRUMENT_NAMES:
raise ValueError('Invalid instrument name: {}. It has to be one of {} '
'(case-insensitive).'.format(instrument, ACCEPTED_INSTRUMENT_NAMES))

self.instrument = instrument
self.instrument = instrument.lower()

if instrument == 'HST':
if self.instrument == 'hst':
self.apertures = read.read_hst_siaf()
self.observatory = 'HST'
else:
self.apertures = read.read_jwst_siaf(instrument, filename=filename, basepath=basepath)
self.apertures = read.read_jwst_siaf(self.instrument, filename=filename, basepath=basepath)
self.observatory = 'JWST'

def __repr__(self):
Expand All @@ -295,19 +304,19 @@ def __str__(self):
def _getFullApertures(self):
"""Return whichever subset of apertures correspond to the entire detectors."""
fullaps = []
if self.instrument == 'NIRCam':
if self.instrument == 'nircam':
fullaps.append(self.apertures['NRCA5_FULL'])
fullaps.append(self.apertures['NRCB5_FULL'])
elif self.instrument == 'NIRSpec':
elif self.instrument == 'nirspec':
fullaps.append(self.apertures['NRS_FULL_MSA1'])
fullaps.append(self.apertures['NRS_FULL_MSA2'])
fullaps.append(self.apertures['NRS_FULL_MSA3'])
fullaps.append(self.apertures['NRS_FULL_MSA4'])
elif self.instrument == 'NIRISS':
elif self.instrument == 'niriss':
fullaps.append(self.apertures['NIS_CEN'])
elif self.instrument == 'MIRI':
elif self.instrument == 'miri':
fullaps.append(self.apertures['MIRIM_FULL'])
elif self.instrument == 'FGS':
elif self.instrument == 'fgs':
fullaps.append(self.apertures['FGS1_FULL'])
fullaps.append(self.apertures['FGS2_FULL'])
return fullaps
Expand Down
Empty file removed pysiaf/tests/README.rst
Empty file.
8 changes: 4 additions & 4 deletions pysiaf/tests/test_aperture.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ def test_jwst_aperture_transforms(siaf_objects, verbose=True, threshold=None):

for siaf in siaf_objects:
if threshold is None:
if siaf.instrument in ['MIRI']:
if siaf.instrument in ['miri']:
threshold = 0.2
elif siaf.instrument in ['NIRCam']:
elif siaf.instrument in ['nircam']:
threshold = 42.
else:
threshold = 0.1
Expand All @@ -98,7 +98,7 @@ def test_jwst_aperture_transforms(siaf_objects, verbose=True, threshold=None):
aperture = siaf[aper_name]

if (aperture.AperType in ['COMPOUND', 'TRANSFORM']) or (
siaf.instrument in ['NIRCam', 'MIRI', 'NIRSpec'] and
siaf.instrument in ['nircam', 'miri', 'nirspec'] and
aperture.AperType == 'SLIT'):
skip = True

Expand Down Expand Up @@ -146,7 +146,7 @@ def test_jwst_aperture_vertices(siaf_objects):
aperture = siaf[aper_name]

if (aperture.AperType in ['COMPOUND', 'TRANSFORM']) or \
(siaf.instrument in ['NIRCam', 'MIRI', 'NIRSpec']
(siaf.instrument in ['nircam', 'miri', 'nirspec']
and aperture.AperType == 'SLIT'):
skip = True

Expand Down

0 comments on commit 25cc564

Please sign in to comment.