<commit_before>#!/usr/bin/env python
# encoding: utf-8
#
# spaxel.py
#
# Licensed under a 3-clause BSD license.
#
# Revision history:
#     11 Apr 2016 <NAME>
#       Initial version


from __future__ import division
from __future__ import print_function

import warnings

import numpy as np

import marvin
import marvin.core.core
import marvin.core.exceptions
import marvin.utils.general.general
import marvin.tools.cube
import marvin.tools.maps
import marvin.tools.modelcube

from marvin.api import api
from marvin.core import MarvinError, MarvinUserWarning
from marvin.tools.analysis_props import AnalysisProperty, DictOfProperties
from marvin.tools.spectrum import Spectrum


class Spaxel(object):
    """A class to interface with a spaxel in a cube.

    This class represents an spaxel with information from the reduced DRP
    spectrum, the DAP maps properties, and the model spectrum from the DAP
    logcube. A ``Spaxel`` can be initialised with all or only part of that
    information, and either from a file, a database, or remotely via the
    Marvin API.

    Parameters:
        x,y (int):
            The `x` and `y` coordinates of the spaxel in the cube (0-indexed).
        cube_filename (str):
            The path of the data cube file containing the spaxel to load.
        maps_filename (str):
            The path of the DAP Maps file containing the spaxel to load.
        modelcube_filename (str):
            The path of the DAP model cube file containing the spaxel to load.
        mangaid (str):
            The mangaid of the cube/maps of the spaxel to load.
        plateifu (str):
            The plate-ifu of the cube/maps of the spaxel to load (either
            ``mangaid`` or ``plateifu`` can be used, but not both).
        cube (:class:`~marvin.tools.cube.Cube` object or bool):
            If ``cube`` is a :class:`~marvin.tools.cube.Cube` object, that
            cube will be used for the ``Spaxel`` initilisitation. This mode
            is mostly intended for
            :class:`~marvin.utils.general.general.getSpaxel` as it
            significantly improves loading time. Otherwise, ``cube`` can be
            ``True`` (default), in which case a cube will be instantiated using
            the input ``filename``, ``mangaid``, or ``plateifu``. If
            ``cube=False``, no cube will be used and ``Spaxel.spectrum`` will
            not be populated.
        maps (:class:`~marvin.tools.maps.Maps` object or bool)
            As ``cube`` but populates ``Spaxel.properties`` with a dictionary
            of DAP measurements corresponding to the spaxel in the maps that
            matches ``bintype``, ``template_kin``, and ``template_pop``.
        modelcube (:class:`marvin.tools.modelcube.ModelCube` object or bool)
            As ``cube`` but populates ``Spaxel.model_flux``, ``Spaxel.model``,
            ``Spaxel.redcorr``, ``Spaxel.emline``, ``Spaxel.emline_base``, and
            ``Spaxel.stellar_continuum`` from the corresponding
            spaxel of the DAP modelcube that matches ``bintype``,
            ``template_kin``, and ``template_pop``.
        bintype (str or None):
            The binning type. For MPL-4, one of the following: ``'NONE',
            'RADIAL', 'STON'`` (if ``None`` defaults to ``'NONE'``).
            For MPL-5 and successive, one of, ``'ALL', 'NRE', 'SPX', 'VOR10'``
            (defaults to ``'ALL'``).
        template_kin (str or None):
            The template use for kinematics. For MPL-4, one of
            ``'M11-STELIB-ZSOL', 'MILES-THIN', 'MIUSCAT-THIN'`` (if ``None``,
            defaults to ``'MIUSCAT-THIN'``). For MPL-5 and successive, the only
            option in ``'GAU-MILESHC'`` (``None`` defaults to it).
        template_pop (str or None):
            A placeholder for a future version in which stellar populations
            are fitted using a different template that ``template_kin``. It
            has no effect for now.
        drpall (str):
            The path to the drpall file to use. Defaults to
            ``marvin.config.drpall``.
        drpver (str):
            The DRP version to use. Defaults to ``marvin.config.drpver``.
        dapver (str):
            The DAP version to use. Defaults to ``marvin.config.dapver``.

    Attributes:
        spectrum (:class:`~marvin.tools.spectrum.Spectrum` object):
            A `Spectrum` object with the DRP spectrum and associated ivar and
            mask for this spaxel.
        specres (Numpy array):
            Median spectral resolution as a function of wavelength for the
            fibers in this IFU.
        specresd (Numpy array):
            Standard deviation of spectral resolution as a function of
            wavelength for the fibers in this IFU.
        properties (:class:`~marvin.tools.analysis_props.DictOfProperties`):
            A dotable, case-insensitive dictionary of
            :class:`~marvin.tools.analysis_props.AnalysisProperty` objects
            from the DAP maps extensions. The keys are a combination of
            category and channel, when applicable, e.g.,
            ``emline_sflux_siii_9533``.
        model_flux (:class:`~marvin.tools.spectrum.Spectrum` object):
            A `Spectrum` object with the flux of the binned spectrum. Includes
            ``ivar`` and ``mask``.
        wavelength (Numpy array):
            Wavelength vector, in Angstrom.
        redcorr (Numpy array):
            Reddening correction applied during the fitting procedures;
            ``dereddened_flux = model_flux.flux * redcorr``.
        model (:class:`~marvin.tools.spectrum.Spectrum` object):
            The best fitting model spectra (sum of the fitted continuum and
            emission-line models). Includes ``mask``.
        emline (:class:`~marvin.tools.spectrum.Spectrum` object):
            The model spectrum with only the emission lines. Includes ``mask``.
        emline_base (:class:`~marvin.tools.spectrum.Spectrum` object):
            The bitmask that only applies to the emission-line modeling.
            Includes ``mask``.

    """

    def __init__(self, *args, **kwargs):

        valid_kwargs = [
            'x', 'y', 'cube_filename', 'maps_filename', 'modelcube_filename',
            'mangaid', 'plateifu', 'cube', 'maps', 'modelcube', 'bintype',
            'template_kin', 'template_pop', 'drpall', 'drpver', 'dapver']

        assert len(args) == 0, 'Spaxel does not accept arguments, only keywords.'
        for kw in kwargs:
            assert kw in valid_kwargs, 'keyword {0} is not valid'.format(kw)

        self.cube = kwargs.pop('cube', True) or False
        self.maps = kwargs.pop('maps', True) or False
        self.modelcube = kwargs.pop('modelcube', True) or False

        if not self.cube and not self.maps and not self.modelcube:
            raise MarvinError('either cube, maps, or modelcube must be True or '
                              'a Marvin Cube, Maps, or ModelCube object must be specified.')

        self._drpver = kwargs.get('drpver', marvin.config.drpver)
        self._dapver = kwargs.get('dapver', marvin.config.dapver)

        self.bintype = marvin.tools.maps._get_bintype(
            self._dapver, bintype=kwargs.get('bintype', None))
        self.template_kin = marvin.tools.maps._get_template_kin(
            self._dapver, template_kin=kwargs.get('template_kin', None))

        self.plateifu = None
        self.mangaid = None

        if len(args) > 0:
            self.x = args[0]
            self.y = args[1]
        else:
            self.x = kwargs.pop('x', None)
            self.y = kwargs.pop('y', None)

        assert self.x is not None and self.y is not None, 'Spaxel requires x and y to initialise.'

        self.specres = None
        self.specresd = None
        self.spectrum = None
        self.properties = {}

        self.model_flux = None
        self.redcorr = None
        self.model = None
        self.emline = None
        self.emline_base = None
        self.stellar_continuum = None

        self.plateifu = kwargs.pop('plateifu', None)
        self.mangaid = kwargs.pop('plateifu', None)

        cube_filename = kwargs.pop('cube_filename', None)
        self._check_cube(cube_filename)

        maps_filename = kwargs.pop('maps_filename', None)
        self._check_maps(maps_filename)

        modelcube_filename = kwargs.pop('modelcube_filename', None)
        self._check_modelcube(modelcube_filename)

    def _check_cube(self, cube_filename):
        """Loads the cube and the spectrum."""

        # Checks that the cube is correct or load ones if cube == True.
        if not isinstance(self.cube, bool):
            assert isinstance(self.cube, marvin.tools.cube.Cube), \
                'cube is not an instance of marvin.tools.cube.Cube or a boolean.'
        elif self.cube is True:
            self.cube = marvin.tools.cube.Cube(filename=cube_filename, plateifu=self.plateifu,
                                               mangaid=self.mangaid, drpver=self._drpver)
        else:
            self.cube = None
            return

        if self.plateifu is not None:
            assert self.plateifu == self.cube.plateifu, \
                'input plateifu does not match the cube plateifu. '
        else:
            self.plateifu = self.cube.plateifu

        if self.mangaid is not None:<commit_msg>fixed declaration of mangaid from kwargs<commit_after>#!/usr/bin/env python
# encoding: utf-8
#
# spaxel.py
#
# Licensed under a 3-clause BSD license.
#
# Revision history:
#     11 Apr 2016 <NAME>
#       Initial version


from __future__ import division
from __future__ import print_function

import warnings

import numpy as np

import marvin
import marvin.core.core
import marvin.core.exceptions
import marvin.utils.general.general
import marvin.tools.cube
import marvin.tools.maps
import marvin.tools.modelcube

from marvin.api import api
from marvin.core import MarvinError, MarvinUserWarning
from marvin.tools.analysis_props import AnalysisProperty, DictOfProperties
from marvin.tools.spectrum import Spectrum


class Spaxel(object):
    """A class to interface with a spaxel in a cube.

    This class represents an spaxel with information from the reduced DRP
    spectrum, the DAP maps properties, and the model spectrum from the DAP
    logcube. A ``Spaxel`` can be initialised with all or only part of that
    information, and either from a file, a database, or remotely via the
    Marvin API.

    Parameters:
        x,y (int):
            The `x` and `y` coordinates of the spaxel in the cube (0-indexed).
        cube_filename (str):
            The path of the data cube file containing the spaxel to load.
        maps_filename (str):
            The path of the DAP Maps file containing the spaxel to load.
        modelcube_filename (str):
            The path of the DAP model cube file containing the spaxel to load.
        mangaid (str):
            The mangaid of the cube/maps of the spaxel to load.
        plateifu (str):
            The plate-ifu of the cube/maps of the spaxel to load (either
            ``mangaid`` or ``plateifu`` can be used, but not both).
        cube (:class:`~marvin.tools.cube.Cube` object or bool):
            If ``cube`` is a :class:`~marvin.tools.cube.Cube` object, that
            cube will be used for the ``Spaxel`` initilisitation. This mode
            is mostly intended for
            :class:`~marvin.utils.general.general.getSpaxel` as it
            significantly improves loading time. Otherwise, ``cube`` can be
            ``True`` (default), in which case a cube will be instantiated using
            the input ``filename``, ``mangaid``, or ``plateifu``. If
            ``cube=False``, no cube will be used and ``Spaxel.spectrum`` will
            not be populated.
        maps (:class:`~marvin.tools.maps.Maps` object or bool)
            As ``cube`` but populates ``Spaxel.properties`` with a dictionary
            of DAP measurements corresponding to the spaxel in the maps that
            matches ``bintype``, ``template_kin``, and ``template_pop``.
        modelcube (:class:`marvin.tools.modelcube.ModelCube` object or bool)
            As ``cube`` but populates ``Spaxel.model_flux``, ``Spaxel.model``,
            ``Spaxel.redcorr``, ``Spaxel.emline``, ``Spaxel.emline_base``, and
            ``Spaxel.stellar_continuum`` from the corresponding
            spaxel of the DAP modelcube that matches ``bintype``,
            ``template_kin``, and ``template_pop``.
        bintype (str or None):
            The binning type. For MPL-4, one of the following: ``'NONE',
            'RADIAL', 'STON'`` (if ``None`` defaults to ``'NONE'``).
            For MPL-5 and successive, one of, ``'ALL', 'NRE', 'SPX', 'VOR10'``
            (defaults to ``'ALL'``).
        template_kin (str or None):
            The template use for kinematics. For MPL-4, one of
            ``'M11-STELIB-ZSOL', 'MILES-THIN', 'MIUSCAT-THIN'`` (if ``None``,
            defaults to ``'MIUSCAT-THIN'``). For MPL-5 and successive, the only
            option in ``'GAU-MILESHC'`` (``None`` defaults to it).
        template_pop (str or None):
            A placeholder for a future version in which stellar populations
            are fitted using a different template that ``template_kin``. It
            has no effect for now.
        drpall (str):
            The path to the drpall file to use. Defaults to
            ``marvin.config.drpall``.
        drpver (str):
            The DRP version to use. Defaults to ``marvin.config.drpver``.
        dapver (str):
            The DAP version to use. Defaults to ``marvin.config.dapver``.

    Attributes:
        spectrum (:class:`~marvin.tools.spectrum.Spectrum` object):
            A `Spectrum` object with the DRP spectrum and associated ivar and
            mask for this spaxel.
        specres (Numpy array):
            Median spectral resolution as a function of wavelength for the
            fibers in this IFU.
        specresd (Numpy array):
            Standard deviation of spectral resolution as a function of
            wavelength for the fibers in this IFU.
        properties (:class:`~marvin.tools.analysis_props.DictOfProperties`):
            A dotable, case-insensitive dictionary of
            :class:`~marvin.tools.analysis_props.AnalysisProperty` objects
            from the DAP maps extensions. The keys are a combination of
            category and channel, when applicable, e.g.,
            ``emline_sflux_siii_9533``.
        model_flux (:class:`~marvin.tools.spectrum.Spectrum` object):
            A `Spectrum` object with the flux of the binned spectrum. Includes
            ``ivar`` and ``mask``.
        wavelength (Numpy array):
            Wavelength vector, in Angstrom.
        redcorr (Numpy array):
            Reddening correction applied during the fitting procedures;
            ``dereddened_flux = model_flux.flux * redcorr``.
        model (:class:`~marvin.tools.spectrum.Spectrum` object):
            The best fitting model spectra (sum of the fitted continuum and
            emission-line models). Includes ``mask``.
        emline (:class:`~marvin.tools.spectrum.Spectrum` object):
            The model spectrum with only the emission lines. Includes ``mask``.
        emline_base (:class:`~marvin.tools.spectrum.Spectrum` object):
            The bitmask that only applies to the emission-line modeling.
            Includes ``mask``.

    """

    def __init__(self, *args, **kwargs):

        valid_kwargs = [
            'x', 'y', 'cube_filename', 'maps_filename', 'modelcube_filename',
            'mangaid', 'plateifu', 'cube', 'maps', 'modelcube', 'bintype',
            'template_kin', 'template_pop', 'drpall', 'drpver', 'dapver']

        assert len(args) == 0, 'Spaxel does not accept arguments, only keywords.'
        for kw in kwargs:
            assert kw in valid_kwargs, 'keyword {0} is not valid'.format(kw)

        self.cube = kwargs.pop('cube', True) or False
        self.maps = kwargs.pop('maps', True) or False
        self.modelcube = kwargs.pop('modelcube', True) or False

        if not self.cube and not self.maps and not self.modelcube:
            raise MarvinError('either cube, maps, or modelcube must be True or '
                              'a Marvin Cube, Maps, or ModelCube object must be specified.')

        self._drpver = kwargs.get('drpver', marvin.config.drpver)
        self._dapver = kwargs.get('dapver', marvin.config.dapver)

        self.bintype = marvin.tools.maps._get_bintype(
            self._dapver, bintype=kwargs.get('bintype', None))
        self.template_kin = marvin.tools.maps._get_template_kin(
            self._dapver, template_kin=kwargs.get('template_kin', None))

        self.plateifu = None
        self.mangaid = None

        if len(args) > 0:
            self.x = args[0]
            self.y = args[1]
        else:
            self.x = kwargs.pop('x', None)
            self.y = kwargs.pop('y', None)

        assert self.x is not None and self.y is not None, 'Spaxel requires x and y to initialise.'

        self.specres = None
        self.specresd = None
        self.spectrum = None
        self.properties = {}

        self.model_flux = None
        self.redcorr = None
        self.model = None
        self.emline = None
        self.emline_base = None
        self.stellar_continuum = None

        self.plateifu = kwargs.pop('plateifu', None)
        self.mangaid = kwargs.pop('mangaid', None)

        cube_filename = kwargs.pop('cube_filename', None)
        self._check_cube(cube_filename)

        maps_filename = kwargs.pop('maps_filename', None)
        self._check_maps(maps_filename)

        modelcube_filename = kwargs.pop('modelcube_filename', None)
        self._check_modelcube(modelcube_filename)

    def _check_cube(self, cube_filename):
        """Loads the cube and the spectrum."""

        # Checks that the cube is correct or load ones if cube == True.
        if not isinstance(self.cube, bool):
            assert isinstance(self.cube, marvin.tools.cube.Cube), \
                'cube is not an instance of marvin.tools.cube.Cube or a boolean.'
        elif self.cube is True:
            self.cube = marvin.tools.cube.Cube(filename=cube_filename, plateifu=self.plateifu,
                                               mangaid=self.mangaid, drpver=self._drpver)
        else:
            self.cube = None
            return

        if self.plateifu is not None:
            assert self.plateifu == self.cube.plateifu, \
                'input plateifu does not match the cube plateifu. '
        else:
            self.plateifu = self.cube.plateifu

        if self.mangaid is not None:
{'fraction_non_alphanumeric': 0.10085561497326204, 'fraction_numerical': 0.0027807486631016044, 'mean_word_length': 2.2970733427362484, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1438332', 'n_tokens_mistral': 5655, 'n_tokens_neox': 5361, 'n_words': 1782}