import astropy
import datetime
import numpy as np
from edibles.utils.edibles_spectrum import EdiblesSpectrum


def testEdiblesSpectrum(filename="tests/HD170740_w860_redl_20140915_O12.fits"):

    # Spectrum information
    sp = EdiblesSpectrum(filename=filename, fully_featured=True, noDATADIR=True)
    assert isinstance(sp.header, astropy.io.fits.header.Header)
    assert isinstance(sp.target, str)
    assert isinstance(sp.date, str)
    assert isinstance(sp.datetime, datetime.datetime)
    assert isinstance(sp.v_bary, float)
    assert isinstance(sp.wave_units, str)
    assert isinstance(sp.flux_units, str)

    # Raw
    assert isinstance(sp.raw_wave, np.ndarray)
    assert isinstance(sp.raw_bary_wave, np.ndarray)
    assert isinstance(sp.raw_flux, np.ndarray)
    assert len(sp.raw_wave) == len(sp.raw_bary_wave)
    assert len(sp.raw_wave) == len(sp.raw_flux)

    assert isinstance(sp.raw_grid, np.ndarray)
    assert len(sp.raw_grid) == 200443  # print(len(sp.raw_grid))

    assert isinstance(sp.raw_sky_wave, np.ndarray)
    assert isinstance(sp.raw_sky_flux, np.ndarray)
    assert len(sp.raw_sky_wave) == len(sp.raw_sky_flux)

    assert isinstance(sp.wave, np.ndarray)
    assert isinstance(sp.bary_wave, np.ndarray)
    assert isinstance(sp.flux, np.ndarray)

    # getSpectrum
    xmin = 7660
    xmax = 7680
    sp.getSpectrum(xmin=xmin, xmax=xmax)
    assert xmin == sp.xmin
    assert xmax == sp.xmax
    assert isinstance(sp.wave, np.ndarray)
    assert isinstance(sp.flux, np.ndarray)
    assert len(sp.wave) == len(sp.flux)
    assert np.min(sp.wave) > sp.xmin
    assert np.max(sp.wave) < sp.xmax

    assert isinstance(sp.bary_wave, np.ndarray)
    assert isinstance(sp.bary_flux, np.ndarray)
    assert len(sp.bary_wave) == len(sp.bary_flux)
    assert np.min(sp.bary_wave) > sp.xmin
    assert np.max(sp.bary_wave) < sp.xmax

    assert isinstance(sp.grid, np.ndarray)
    assert isinstance(sp.interp_flux, np.ndarray)
    assert isinstance(sp.interp_bary_flux, np.ndarray)
    assert len(sp.grid) == len(sp.interp_flux)
    assert len(sp.grid) == len(sp.interp_bary_flux)
    assert np.min(sp.grid) > sp.xmin
    assert np.max(sp.grid) < sp.xmax

    assert isinstance(sp.sky_wave, np.ndarray)
    assert isinstance(sp.sky_flux, np.ndarray)
    assert len(sp.sky_wave) == len(sp.sky_flux)
    assert np.min(sp.sky_wave) > sp.xmin
    assert np.max(sp.sky_wave) < sp.xmax

    # shift
    zoom_xmin = 7661
    zoom_xmax = 7679
    shift = 0.05
    sp.shift(shift=shift, zoom_xmin=zoom_xmin, zoom_xmax=zoom_xmax)

    assert isinstance(sp.wave, np.ndarray)
    assert isinstance(sp.flux, np.ndarray)
    assert len(sp.wave) == len(sp.flux)
    assert np.min(sp.wave) > sp.xmin
    assert np.max(sp.wave) < sp.xmax

    assert isinstance(sp.bary_wave, np.ndarray)
    assert isinstance(sp.bary_flux, np.ndarray)
    assert len(sp.bary_wave) == len(sp.bary_flux)
    assert np.min(sp.bary_wave) > sp.xmin
    assert np.max(sp.bary_wave) < sp.xmax

    assert isinstance(sp.grid, np.ndarray)
    assert isinstance(sp.interp_flux, np.ndarray)
    assert isinstance(sp.interp_bary_flux, np.ndarray)
    assert len(sp.grid) == len(sp.interp_flux)
    assert len(sp.grid) == len(sp.interp_bary_flux)
    assert np.min(sp.grid) > sp.xmin
    assert np.max(sp.grid) < sp.xmax

    assert isinstance(sp.sky_wave, np.ndarray)
    assert isinstance(sp.sky_flux, np.ndarray)
    assert len(sp.sky_wave) == len(sp.sky_flux)
    assert np.min(sp.sky_wave) > sp.xmin
    assert np.max(sp.sky_wave) < sp.xmax


if __name__ == "__main__":

    filename = "HD170740_w860_redl_20140915_O12.fits"
    testEdiblesSpectrum(filename=filename)

[((10, 9, 10, 80), 'edibles.utils.edibles_spectrum.EdiblesSpectrum', 'EdiblesSpectrum', (), '', False, 'from edibles.utils.edibles_spectrum import EdiblesSpectrum\n'), ((46, 11, 46, 26), 'numpy.min', 'np.min', ({(46, 18, 46, 25): 'sp.wave'}, {}), '(sp.wave)', True, 'import numpy as np\n'), ((47, 11, 47, 26), 'numpy.max', 'np.max', ({(47, 18, 47, 25): 'sp.wave'}, {}), '(sp.wave)', True, 'import numpy as np\n'), ((52, 11, 52, 31), 'numpy.min', 'np.min', ({(52, 18, 52, 30): 'sp.bary_wave'}, {}), '(sp.bary_wave)', True, 'import numpy as np\n'), ((53, 11, 53, 31), 'numpy.max', 'np.max', ({(53, 18, 53, 30): 'sp.bary_wave'}, {}), '(sp.bary_wave)', True, 'import numpy as np\n'), ((60, 11, 60, 26), 'numpy.min', 'np.min', ({(60, 18, 60, 25): 'sp.grid'}, {}), '(sp.grid)', True, 'import numpy as np\n'), ((61, 11, 61, 26), 'numpy.max', 'np.max', ({(61, 18, 61, 25): 'sp.grid'}, {}), '(sp.grid)', True, 'import numpy as np\n'), ((66, 11, 66, 30), 'numpy.min', 'np.min', ({(66, 18, 66, 29): 'sp.sky_wave'}, {}), '(sp.sky_wave)', True, 'import numpy as np\n'), ((67, 11, 67, 30), 'numpy.max', 'np.max', ({(67, 18, 67, 29): 'sp.sky_wave'}, {}), '(sp.sky_wave)', True, 'import numpy as np\n'), ((78, 11, 78, 26), 'numpy.min', 'np.min', ({(78, 18, 78, 25): 'sp.wave'}, {}), '(sp.wave)', True, 'import numpy as np\n'), ((79, 11, 79, 26), 'numpy.max', 'np.max', ({(79, 18, 79, 25): 'sp.wave'}, {}), '(sp.wave)', True, 'import numpy as np\n'), ((84, 11, 84, 31), 'numpy.min', 'np.min', ({(84, 18, 84, 30): 'sp.bary_wave'}, {}), '(sp.bary_wave)', True, 'import numpy as np\n'), ((85, 11, 85, 31), 'numpy.max', 'np.max', ({(85, 18, 85, 30): 'sp.bary_wave'}, {}), '(sp.bary_wave)', True, 'import numpy as np\n'), ((92, 11, 92, 26), 'numpy.min', 'np.min', ({(92, 18, 92, 25): 'sp.grid'}, {}), '(sp.grid)', True, 'import numpy as np\n'), ((93, 11, 93, 26), 'numpy.max', 'np.max', ({(93, 18, 93, 25): 'sp.grid'}, {}), '(sp.grid)', True, 'import numpy as np\n'), ((98, 11, 98, 30), 'numpy.min', 'np.min', ({(98, 18, 98, 29): 'sp.sky_wave'}, {}), '(sp.sky_wave)', True, 'import numpy as np\n'), ((99, 11, 99, 30), 'numpy.max', 'np.max', ({(99, 18, 99, 29): 'sp.sky_wave'}, {}), '(sp.sky_wave)', True, 'import numpy as np\n')]