示例#1
0
def read_jwst_siaf(instrument=None, filename=None, basepath=None):
    """Read the JWST SIAF and return a collection of apertures.

    Parameters
    ----------
    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
    -------
    apertures : dict
        dictionary of apertures

    """
    from pysiaf import aperture  # runtime import to avoid circular import on startup

    if (filename is None) and (instrument is None):
        raise ValueError('Specify either input instrument or filename')

    if filename is None:
        if basepath is None:
            basepath = JWST_PRD_DATA_ROOT
        if not os.path.isdir(basepath):
            raise RuntimeError("Could not find SIAF data "
                               "in {}".format(basepath))
        filename = os.path.join(basepath, JWST_INSTRUMENT_NAME_MAPPING[instrument.lower()]
                                + '_SIAF.xml')
    else:
        filename = filename

    apertures = OrderedDict()

    file_seed, file_extension = os.path.splitext(filename)
    if file_extension == '.xml':
        tree = ET.parse(filename)
        instrument = get_jwst_siaf_instrument(tree)

        # generate Aperture objects from SIAF XML file, parse the XML
        for entry in tree.getroot().iter('SiafEntry'):
            if instrument.upper() == 'NIRSPEC':
                jwst_aperture = aperture.NirspecAperture()
            else:
                jwst_aperture = aperture.JwstAperture()
            for node in entry.iterchildren():
                if (node.tag in aperture.ATTRIBUTES_THAT_CAN_BE_NONE) and (node.text is None):
                    value = node.text
                elif node.tag in aperture.INTEGER_ATTRIBUTES:
                    try:
                        value = int(node.text)
                    except (TypeError, ValueError) as e:
                        # print('{}: {}: {}'.format(e, node.tag, node.text))
                        if node.tag == 'DetSciYAngle':
                            value = np.int(float((node.text)))
                        else:
                            raise TypeError
                elif node.tag in aperture.STRING_ATTRIBUTES:
                    value = node.text
                else:
                    try:
                        value = float(node.text)
                    except TypeError:
                        print('{}: {}'.format(node.tag, node.text))
                        raise TypeError

                setattr(jwst_aperture, node.tag, value)

            apertures[jwst_aperture.AperName] = jwst_aperture

    else:
        raise NotImplementedError

    # handle special case of NIRSpec, where auxiliary TRANSFORM apertures are defined and hold
    # transformation parameters
    # simple workaround is to attach the TRANSFORM aperture as attribute to the respective NIRSpec
    # aperture
    if instrument.upper() == 'NIRSPEC':

        # Fundamental aperture definitions: names, types, reference positions, dependencies
        siaf_aperture_definitions = read_siaf_aperture_definitions('NIRSpec')

        for AperName in apertures:
            jwst_aperture = apertures[AperName]
            if jwst_aperture.AperType in ['FULLSCA', 'OSS']:
                for transform_aperture_name in 'CLEAR_GWA_OTE F110W_GWA_OTE F140X_GWA_OTE'.split():
                    setattr(jwst_aperture, '_{}'.format(transform_aperture_name),
                            apertures[transform_aperture_name])
                apertures[AperName] = jwst_aperture
            elif jwst_aperture.AperType in ['SLIT']:
                # attach TA apertures
                for transform_aperture_name in 'CLEAR_GWA_OTE F110W_GWA_OTE F140X_GWA_OTE'.split():
                    setattr(jwst_aperture, '_{}'.format(transform_aperture_name),
                            apertures[transform_aperture_name])

                # attach parent aperture (name stored in _parent_apertures, aperture object stored
                # in _parent_aperture)
                index = siaf_aperture_definitions['AperName'].tolist().index(AperName)
                parent_aperture_name = siaf_aperture_definitions['parent_apertures'][index]
                if (parent_aperture_name is not None) and \
                        (not np.ma.is_masked(parent_aperture_name)):
                    jwst_aperture._parent_apertures = parent_aperture_name
                    jwst_aperture._parent_aperture = apertures[jwst_aperture._parent_apertures]

                apertures[AperName] = jwst_aperture

    return apertures
示例#2
0
def read_jwst_siaf(instrument=None, filename=None, basepath=None):
    """Read the JWST SIAF and return a collection of apertures.

    Parameters
    ----------
    instrument
    filename
    basepath

    Returns
    -------
    apertures : dict
        dictionary of apertures

    """
    from pysiaf import aperture  # runtime import to avoid circular import on startup
    if (filename is None) and (instrument is None):
        raise ValueError('Specify either input instrument or filename')

    if filename is None:
        if basepath is None:
            basepath = JWST_PRD_DATA_ROOT
        if not os.path.isdir(basepath):
            raise RuntimeError("Could not find SIAF data "
                               "in {}".format(basepath))
        filename = os.path.join(basepath, instrument + '_SIAF.xml')
    else:
        filename = filename

    apertures = OrderedDict()

    file_seed, file_extension = os.path.splitext(filename)
    if file_extension == '.xml':
        tree = ET.parse(filename)
        instrument = get_jwst_siaf_instrument(tree)

        # generate Aperture objects from SIAF XML file, parse the XML
        for entry in tree.getroot().iter('SiafEntry'):
            if instrument.upper() == 'NIRSPEC':
                jwst_aperture = aperture.NirspecAperture()
            else:
                jwst_aperture = aperture.JwstAperture()
            for node in entry.iterchildren():
                if (node.tag in aperture.ATTRIBUTES_THAT_CAN_BE_NONE) and (
                        node.text is None):
                    value = node.text
                elif node.tag in aperture.INTEGER_ATTRIBUTES:
                    try:
                        value = int(node.text)
                    except TypeError:
                        print('{}: {}'.format(node.tag, node.text))
                        raise TypeError
                elif node.tag in aperture.STRING_ATTRIBUTES:
                    value = node.text
                else:
                    try:
                        value = float(node.text)
                    except TypeError:
                        print('{}: {}'.format(node.tag, node.text))
                        raise TypeError

                # except (ValueError, TypeError):
                #     value = node.text

                setattr(jwst_aperture, node.tag, value)

            apertures[jwst_aperture.AperName] = jwst_aperture

    else:
        raise NotImplementedError

    # handle special case of NIRSpec, where auxiliary TRANSFORM apertures are defined and hold transformation parameters
    # simple workaround is to attach the TRANSFORM aperture as attribute to the respective NIRSpec aperture
    if instrument.upper() == 'NIRSPEC':
        for AperName in apertures:
            jwst_aperture = apertures[AperName]
            if jwst_aperture.AperType in ['FULLSCA', 'OSS']:
                for transform_aperture_name in 'CLEAR_GWA_OTE F110W_GWA_OTE F140X_GWA_OTE'.split(
                ):
                    setattr(jwst_aperture,
                            '_{}'.format(transform_aperture_name),
                            apertures[transform_aperture_name])
                apertures[AperName] = jwst_aperture

    return apertures