<commit_before>from __future__ import print_function
import numpy as np
import os
from astropy.io import fits
from astropy import wcs
from marvin import config
from marvin.api.api import Interaction


class Cube(object):

    def __init__(self, filename=None, plateifu=None, mangaid=None):
        args = [filename, plateifu, mangaid]
        errmsg = 'Enter filename, plateifu, or mangaid!'
        assert any(args), errmsg
        assert sum([bool(arg) for arg in args]) == 1, errmsg

        self.filename = filename
        self.mangaid = mangaid
        self.plateifu = plateifu

        self._useDB = None

        # convert from mangaid to plateifu
        # print warning if multiple plateifus correspond to one mangaid
        # FIX
        mangaid_to_plateifu = {'1-209232': '8485-1901'}
        if self.mangaid:
            self.plateifu = mangaid_to_plateifu[self.mangaid]

        if config.mode == 'local':
            if self.filename:
                self._openFile()
            else:
                try:
                    # self._getCubeFromPlateIFU()
                    # self._getCubeFromMangaID()
                    raise RuntimeError('Failed to connect to database.')
                except RuntimeError:
                    # convert from plateifu to filename (sdss_access)
                    # self.filename = plateifu_to_filename(self.plateifu)
                    plateifu_to_filename = {'8485-1901': 'test.fits'}
                    self.filename = plateifu_to_filename[self.plateifu]
                    try:
                        self._openFile()
                    except FileNotFoundError:
                        if config.download:
                            # download file via sdsssync
                            # self._openFile()
                            pass
                        else:
                            fnferr_msg = ('Failed to find file locally. Try downloading file.')
                            raise FileNotFoundError(fnferr_msg)
        elif config.mode == 'remote':
            if self.filename:
                raise FileNotFoundError('Cannot open {} remotely.'.format(self.filename))
            else:
                # use API
                pass

    def getSpectrum(self, x=None, y=None, ra=None, dec=None, ext='flux'):
        """Returns the appropriate spectrum for a certain spaxel.

        Parameters
        ----------
        x, y : int
            The spaxel coordinates relative to the centre of the cube.

        ra, dec : float
            The coordinates of the spaxel to return. The closest spaxel to
            those coordinates will be returned.

        ext : str
            The extension of the cube to use, either `'flux'`, `'ivar'`, or
            `'mask'`.

        Returns
        -------
        result : np.array
            An array with the spectrum for the input coordinates.

        """

        # Checks that we have the correct set of inputs.
        if x or y:
            assert not ra and not dec, 'Either use (x, y) or (ra, dec)'
            assert x and y, 'Specify both x and y'
            inputMode = 'pix'
        elif ra or dec:
            assert not x and not y, 'Either use (x, y) or (ra, dec)'
            assert ra and dec, 'Specify both ra and dec'
            inputMode = 'sky'
        else:
            raise ValueError('You need to specify either (x, y) or (ra, dec)')

        assert isinstance(ext, basestring)
        ext = ext.lower()
        assert ext in ['flux', 'ivar', 'mask'], 'ext needs to be either \'flux\', \'ivar\', or \'mask\''

        if config.mode == 'local':

            if not self._useDB:

                cubeExt = self._hdu[ext.upper()]
                cubeShape = cubeExt.data.shape

                if inputMode == 'sky':
                    cubeWCS = wcs.WCS(cubeExt.header)
                    xCube, yCube, __ = cubeWCS.wcs_world2pix([[ra, dec, 1.]], 1)[0]
                else:
                    yMid, xMid = np.array(cubeShape[1:]) / 2.
                    xCube = int(xMid + x)
                    yCube = int(yMid - y)

                assert xCube > 0 and yCube > 0, 'pixel coordinates outside cube'
                assert (xCube < cubeShape[2] - 1 and yCube < cubeShape[1] - 1), 'pixel coordinates outside cube'

                return cubeExt.data[:, np.round(yCube), np.round(xCube)]

            else:

                raise NotImplementedError('getSpectrum from DB not yet implemented')

        else:

            raise NotImplementedError('getSpectrum over API not yet implemented')

        # ''' currently: x,y array indices
        # ideally: x,y in arcsecond relative to cube center '''
        # # spectrum = Spectrum(x,y)
        # if config.mode == 'file':
        #     ''' local (client has a file) '''
        #     shape = self.flux.shape
        #     assert len(shape) == 3, 'Dimensions of flux not = 3'
        #     assert x < shape[2] and y < shape[1], 'Input x,y coordinates greater than flux dimensions'
        #     return self.flux[:, y, x]
        # elif config.mode == 'db':
        #     ''' db means local (client) has db '''
        #     return self._cube.spaxels[0].flux
        # else:
        #     ''' local (client) has nothing '''
        #     route = 'cubes/{0}/spectra/x={1}/y={2}/'.format(self.mangaid, x, y)
        #     results = Interaction(route, request_type='get')
        #     return results.getData(astype=np.array)

    def _openFile(self):

        if not os.path.exists(self.filename):
            raise ValueError('filename {0} cannot be found'.format(self.filename))

        self._useDB = False

    @property
    def flux(self):
        if not self._useDB:
            return self._hdu['FLUX'].data
        else:
            return None

    def _getCubeFromDB(self):<commit_msg>removed old code<commit_after>from __future__ import print_function
import numpy as np
import os
from astropy.io import fits
from astropy import wcs
from marvin import config
from marvin.api.api import Interaction


class Cube(object):

    def __init__(self, filename=None, plateifu=None, mangaid=None):
        args = [filename, plateifu, mangaid]
        errmsg = 'Enter filename, plateifu, or mangaid!'
        assert any(args), errmsg
        assert sum([bool(arg) for arg in args]) == 1, errmsg

        self.filename = filename
        self.mangaid = mangaid
        self.plateifu = plateifu

        self._useDB = None

        # convert from mangaid to plateifu
        # print warning if multiple plateifus correspond to one mangaid
        # FIX
        mangaid_to_plateifu = {'1-209232': '8485-1901'}
        if self.mangaid:
            self.plateifu = mangaid_to_plateifu[self.mangaid]

        if config.mode == 'local':
            if self.filename:
                self._openFile()
            else:
                try:
                    # self._getCubeFromPlateIFU()
                    # self._getCubeFromMangaID()
                    raise RuntimeError('Failed to connect to database.')
                except RuntimeError:
                    # convert from plateifu to filename (sdss_access)
                    # self.filename = plateifu_to_filename(self.plateifu)
                    plateifu_to_filename = {'8485-1901': 'test.fits'}
                    self.filename = plateifu_to_filename[self.plateifu]
                    try:
                        self._openFile()
                    except FileNotFoundError:
                        if config.download:
                            # download file via sdsssync
                            # self._openFile()
                            pass
                        else:
                            fnferr_msg = ('Failed to find file locally. Try downloading file.')
                            raise FileNotFoundError(fnferr_msg)
        elif config.mode == 'remote':
            if self.filename:
                raise FileNotFoundError('Cannot open {} remotely.'.format(self.filename))
            else:
                # use API
                pass

    def getSpectrum(self, x=None, y=None, ra=None, dec=None, ext='flux'):
        """Returns the appropriate spectrum for a certain spaxel.

        Parameters
        ----------
        x, y : int
            The spaxel coordinates relative to the centre of the cube.

        ra, dec : float
            The coordinates of the spaxel to return. The closest spaxel to
            those coordinates will be returned.

        ext : str
            The extension of the cube to use, either `'flux'`, `'ivar'`, or
            `'mask'`.

        Returns
        -------
        result : np.array
            An array with the spectrum for the input coordinates.

        """

        # Checks that we have the correct set of inputs.
        if x or y:
            assert not ra and not dec, 'Either use (x, y) or (ra, dec)'
            assert x and y, 'Specify both x and y'
            inputMode = 'pix'
        elif ra or dec:
            assert not x and not y, 'Either use (x, y) or (ra, dec)'
            assert ra and dec, 'Specify both ra and dec'
            inputMode = 'sky'
        else:
            raise ValueError('You need to specify either (x, y) or (ra, dec)')

        assert isinstance(ext, basestring)
        ext = ext.lower()
        assert ext in ['flux', 'ivar', 'mask'], 'ext needs to be either \'flux\', \'ivar\', or \'mask\''

        if config.mode == 'local':

            if not self._useDB:

                cubeExt = self._hdu[ext.upper()]
                cubeShape = cubeExt.data.shape

                if inputMode == 'sky':
                    cubeWCS = wcs.WCS(cubeExt.header)
                    xCube, yCube, __ = cubeWCS.wcs_world2pix([[ra, dec, 1.]], 1)[0]
                else:
                    yMid, xMid = np.array(cubeShape[1:]) / 2.
                    xCube = int(xMid + x)
                    yCube = int(yMid - y)

                assert xCube > 0 and yCube > 0, 'pixel coordinates outside cube'
                assert (xCube < cubeShape[2] - 1 and yCube < cubeShape[1] - 1), 'pixel coordinates outside cube'

                return cubeExt.data[:, np.round(yCube), np.round(xCube)]

            else:

                raise NotImplementedError('getSpectrum from DB not yet implemented')

        else:

            raise NotImplementedError('getSpectrum over API not yet implemented')

    def _openFile(self):

        if not os.path.exists(self.filename):
            raise ValueError('filename {0} cannot be found'.format(self.filename))

        self._useDB = False

    @property
    def flux(self):
        if not self._useDB:
            return self._hdu['FLUX'].data
        else:
            return None

    def _getCubeFromDB(self):
{'fraction_non_alphanumeric': 0.09598733823666325, 'fraction_numerical': 0.007634298482450424, 'mean_word_length': 1.5692418081798614, 'pattern_counts': {'":': 0, '<': 9, '<?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': '745963', 'n_tokens_mistral': 3134, 'n_tokens_neox': 2881, 'n_words': 923}