Beispiel #1
0
    def __init__(self,
                 name,
                 extension_name,
                 extension_wave=None,
                 extension_ivar=None,
                 extension_mask=None,
                 db_table='spaxel',
                 unit=u.dimensionless_unscaled,
                 scale=1,
                 formats={},
                 pixmask_flag='MANGA_DRP3PIXMASK',
                 description=''):

        self.name = name

        self._extension_name = extension_name
        self._extension_wave = extension_wave
        self._extension_ivar = extension_ivar
        self._extension_mask = extension_mask

        self.pixmask_flag = pixmask_flag

        self.db_table = db_table

        self._parent = None

        self.formats = formats

        self.description = description

        self.unit = u.CompositeUnit(scale, unit.bases, unit.powers)
Beispiel #2
0
    def __new__(cls,
                array,
                unit=None,
                scale=1,
                ivar=None,
                mask=None,
                binid=None,
                pixmask_flag=None,
                dtype=None,
                copy=True):

        if scale is not None:
            unit = units.CompositeUnit(unit.scale * scale, unit.bases,
                                       unit.powers)

        obj = units.Quantity(np.array(array),
                             unit=unit,
                             dtype=dtype,
                             copy=copy)
        obj = obj.view(cls)
        obj._set_unit(unit)

        obj._maps = None
        obj._datamodel = None

        obj.ivar = np.array(ivar) if ivar is not None else None
        obj.mask = np.array(mask) if mask is not None else None
        obj.binid = np.array(binid) if binid is not None else None

        obj.pixmask_flag = pixmask_flag

        return obj
Beispiel #3
0
    def __init__(self,
                 name,
                 extension_name,
                 extension_wave=None,
                 extension_std=None,
                 extension_mask=None,
                 db_table='cube',
                 unit=u.dimensionless_unscaled,
                 scale=1,
                 formats={},
                 pixmask_flag=None,
                 description=''):

        self.name = name

        self._extension_name = extension_name
        self._extension_wave = extension_wave
        self._extension_std = extension_std
        self._extension_mask = extension_mask

        self.pixmask_flag = pixmask_flag

        self.db_table = db_table

        self.formats = formats

        self.description = description

        self._parent = None

        self.unit = u.CompositeUnit(scale, unit.bases, unit.powers)
Beispiel #4
0
    def descale(self):
        """Returns a copy of the object in which the scale is unity.

        Example:

            >>> dc.unit
            Unit("1e-17 erg / (Angstrom cm2 s spaxel)")
            >> dc[100, 15, 15]
            <DataCube 0.270078063011169 1e-17 erg / (Angstrom cm2 s spaxel)>
            >>> dc_descaled = dc.descale()
            >>> d_descaled.unit
            Unit("Angstrom cm2 s spaxel")
            >>> dc[100, 15, 15]
            <DataCube 2.70078063011169e-18 erg / (Angstrom cm2 s spaxel)>

        """

        if self.unit.scale == 1:
            return self

        value_descaled = self.value * self.unit.scale
        value_unit = units.CompositeUnit(1, self.unit.bases, self.unit.powers)

        if self.ivar is not None:
            ivar_descaled = self.ivar / (self.unit.scale**2)
        else:
            ivar_descaled = None

        return self.__class__(value_descaled,
                              self.wavelength,
                              unit=value_unit,
                              ivar=ivar_descaled,
                              mask=self.mask)
Beispiel #5
0
    def __init__(self, name, extension_name, extension_wave=None,
                 extension_ivar=None, extension_mask=None, channels=[],
                 unit=u.dimensionless_unscaled, scale=1, formats={},
                 parent=None, binid=None, description='', db_table='modelspaxel'):

        self.name = name

        self._extension_name = extension_name
        self._extension_wave = extension_wave
        self._extension_ivar = extension_ivar
        self._extension_mask = extension_mask

        self.channels = channels

        self.unit = u.CompositeUnit(scale, unit.bases, unit.powers)

        self.formats = formats
        self.description = description
        self.db_table = db_table

        self._binid = binid

        self._parent = None
        self.parent = parent

        self._binid = copy_mod.deepcopy(binid)
        if self._binid is not None:
            self._binid.parent = self.parent
Beispiel #6
0
    def __init__(self, name, channel=None, ivar=False, mask=False, unit=None,
                 scale=1, formats={}, parent=None, binid=None, description=''):

        self.name = name
        self.channel = copy_mod.deepcopy(channel)

        self.ivar = ivar
        self.mask = mask

        self.formats = formats

        if unit is not None:
            self.unit = u.CompositeUnit(scale, unit.bases, unit.powers)
        elif unit is None and self.channel is None:
            self.unit = u.dimensionless_unscaled
        else:
            self.unit = self.channel.unit

        self._binid = binid

        # Makes sure the channel shares the units and scale
        if self.channel:
            self.channel.unit = self.unit

        self.description = description

        self._parent = None
        self.parent = parent

        self._binid = copy_mod.deepcopy(binid)
        if self._binid is not None:
            self._binid.parent = self.parent
Beispiel #7
0
def test_initialisation():
    assert u.Unit(u.m) is u.m

    ten_meter = u.Unit(10. * u.m)
    assert ten_meter == u.CompositeUnit(10., [u.m], [1])
    assert u.Unit(ten_meter) is ten_meter

    assert u.Unit(10. * ten_meter) == u.CompositeUnit(100., [u.m], [1])

    foo = u.Unit('foo', (10. * ten_meter)**2, namespace=locals())
    assert foo == u.CompositeUnit(10000., [u.m], [2])

    assert u.Unit('m') == u.m
    assert u.Unit('') == u.dimensionless_unscaled
    assert u.one == u.dimensionless_unscaled
    assert u.Unit('10 m') == ten_meter
    assert u.Unit(10.) == u.CompositeUnit(10., [], [])
Beispiel #8
0
    def __init__(self, name, unit=u.dimensionless_unscaled, scale=1, formats={},
                 idx=None, db_name=None, description=''):

        self.name = name
        self.unit = u.CompositeUnit(scale, unit.bases, unit.powers)
        self.formats = formats
        self.idx = idx
        self.db_name = db_name or self.name
        self.description = description
Beispiel #9
0
    def __new__(cls, value, wavelength, scale=None, unit=units.dimensionless_unscaled,
                wavelength_unit=units.Angstrom, redcorr=None, ivar=None, mask=None,
                binid=None, pixmask_flag=None, dtype=None, copy=True, **kwargs):

        # If the scale is defined, creates a new composite unit with the input scale.
        if scale is not None:
            unit = units.CompositeUnit(unit.scale * scale, unit.bases, unit.powers)

        assert wavelength is not None, 'a valid wavelength array is required'

        assert isinstance(value, np.ndarray) and value.ndim == 3, 'value must be a 3D array.'
        assert isinstance(wavelength, np.ndarray) and wavelength.ndim == 1, \
            'wavelength must be a 1D array.'
        assert len(wavelength) == value.shape[0], \
            'wavelength and value spectral dimensions do not match'

        if ivar is not None:
            assert isinstance(ivar, np.ndarray) and ivar.shape == value.shape, 'invalid ivar shape'
        if mask is not None:
            assert isinstance(mask, np.ndarray) and mask.shape == value.shape, 'invalid mask shape'
        if binid is not None:
            assert (isinstance(binid, np.ndarray) and
                    binid.shape == value.shape[1:]), 'invalid binid shape'

        obj = units.Quantity(value, unit=unit, **kwargs)
        obj = obj.view(cls)
        obj._set_unit(unit)

        assert wavelength is not None, 'invalid wavelength'

        if isinstance(wavelength, units.Quantity):
            obj.wavelength = wavelength
        else:
            obj.wavelength = np.array(wavelength) * wavelength_unit

        obj.ivar = np.array(ivar) if ivar is not None else None
        obj.mask = np.array(mask) if mask is not None else None
        obj.binid = np.array(binid) if binid is not None else None

        obj.pixmask_flag = pixmask_flag

        if redcorr is not None:
            assert len(redcorr) == len(obj.wavelength), 'invalid length for redcorr.'
            obj.redcorr = np.array(redcorr)

        return obj
Beispiel #10
0
def standardise_unit(unit):
    """Standardise unit.

    Changes applied by this function:

    * Drop "photon" == "ph" from the unit
    * Drop "count" == "ct" from the unit

    Parameters
    ----------
    unit : `~astropy.units.Unit` or str
        Any old unit

    Returns
    -------
    unit : `~astropy.units.Unit`
        Shiny new, standardised unit

    Examples
    --------
    >>> from gammapy.utils.units import standardise_unit
    >>> standardise_unit('ph cm-2 s-1')
    Unit("1 / (cm2 s)")
    >>> standardise_unit('ct cm-2 s-1')
    Unit("1 / (cm2 s)")
    >>> standardise_unit('cm-2 s-1')
    Unit("1 / (cm2 s)")
    """
    unit = u.Unit(unit)
    bases, powers = [], []
    for base, power in zip(unit.bases, unit.powers):
        if str(base) not in {"ph", "ct"}:
            bases.append(base)
            powers.append(power)

    return u.CompositeUnit(scale=unit.scale, bases=bases, powers=powers)
Beispiel #11
0
def test_composite_unit_get_format_name():
    """See #1576"""
    unit1 = u.Unit('nrad/s')
    unit2 = u.Unit('Hz(1/2)')
    assert (str(u.CompositeUnit(1, [unit1, unit2],
                                [1, -1])) == 'nrad / (Hz(1/2) s)')