import unittest
import numpy.testing as testing
import numpy as np
import fitsio
import tempfile
import os

from redmapper import ColorBackground
from redmapper import ColorBackgroundGenerator
from redmapper import Configuration

class ColorBackgroundTestCase(unittest.TestCase):
    """
    Tests for the redmapper.ColorBackground and
    redmapper.ColorBackgroundGenerator classes.
    """
    def runTest(self):
        """
        Run the ColorBackground and ColorBackgroundGenerator tests.
        """

        file_name = 'test_dr8_col_bkg.fit'
        file_path = 'data_for_tests'

        cbkg = ColorBackground('%s/%s' % (file_path, file_name))

        col1 = np.array([0.572300, 1.39560])
        col2 = np.array([0.7894, 0.9564])
        refmags = np.array([17.587, 18.956])

        refmagindex = np.array([258, 395])
        col1index = np.array([1, 17])
        col2index = np.array([15, 19])

        # These are new values that are based on improvements in the binning.
        idl_bkg1 = np.array([0.76778, 0.80049])
        idl_bkg2 = np.array([0.04012, 0.10077])
        idl_bkg12 = np.array([0.01085, 0.081])

        # Test color1
        py_outputs = cbkg.lookup_diagonal(1, col1, refmags)
        testing.assert_almost_equal(py_outputs, idl_bkg1, decimal=5)

        # Test color2
        py_outputs = cbkg.lookup_diagonal(2, col2, refmags)
        testing.assert_almost_equal(py_outputs, idl_bkg2, decimal=5)

        # Test off-diagonal
        py_outputs = cbkg.lookup_offdiag(1, 2, col1, col2, refmags)
        testing.assert_almost_equal(py_outputs, idl_bkg12, decimal=5)

        # And a test sigma_g with the usehdrarea=True
        cbkg2 = ColorBackground('%s/%s' % (file_path, file_name), usehdrarea=True)

        col1 = np.array([0.572300, 1.39560, 1.0])
        col2 = np.array([0.7894, 0.9564, 1.0])
        refmags = np.array([17.587, 18.956, 25.0])

        idl_sigma_g1 = np.array([127.698, 591.112, np.inf])
        idl_sigma_g2 = np.array([7.569, 82.8938, np.inf])

        # Test color1
        py_outputs = cbkg2.sigma_g_diagonal(1, col1, refmags)
        testing.assert_almost_equal(py_outputs, idl_sigma_g1, decimal=3)

        # Test color2
        py_outputs = cbkg2.sigma_g_diagonal(2, col2, refmags)
        testing.assert_almost_equal(py_outputs, idl_sigma_g2, decimal=3)


        #####################################################
        # Now a test of the generation of a color background
        conf_filename = 'testconfig.yaml'
        config = Configuration(file_path + "/" + conf_filename)

        tfile = tempfile.mkstemp()
        os.close(tfile[0])
        config.bkgfile_color = tfile[1]
        config.d.nside = 128
        config.d.hpix = [8421]
        config.border = 0.0

        cbg = ColorBackgroundGenerator(config, minrangecheck=5)
        # Need to set clobber=True because the tempfile was created
        cbg.run(clobber=True)

        fits = fitsio.FITS(config.bkgfile_color)

        # Make sure we have 11 extensions
        testing.assert_equal(len(fits), 11)

        # These tests are obsolete, but could be refactored
        # Check the 01_01 and 01_02
        # bkg11 = fits['01_01_REF'].read()
        # bkg11_compare = fitsio.read(file_path + "/test_dr8_bkg_zredc_sub.fits", ext='01_01_REF')
        # testing.assert_almost_equal(bkg11['BC'], bkg11_compare['BC'], 3)
        # testing.assert_almost_equal(bkg11['N'], bkg11_compare['N'], 3)

        # bkg12 = fits['01_02_REF'].read()
        # bkg12_compare = fitsio.read(file_path + "/test_dr8_bkg_zredc_sub.fits", ext='01_02_REF')

        # testing.assert_almost_equal(bkg12['BC'], bkg12_compare['BC'], 2)
        # testing.assert_almost_equal(bkg12['N'], bkg12_compare['N'], 4)

        # And delete the tempfile
        os.remove(config.bkgfile_color)

if __name__=='__main__':
    unittest.main()

[((109, 4, 109, 19), 'unittest.main', 'unittest.main', ({}, {}), '()', False, 'import unittest\n'), ((25, 15, 25, 64), 'redmapper.ColorBackground', 'ColorBackground', ({(25, 31, 25, 63): "'%s/%s' % (file_path, file_name)"}, {}), "('%s/%s' % (file_path, file_name))", False, 'from redmapper import ColorBackground\n'), ((27, 15, 27, 44), 'numpy.array', 'np.array', ({(27, 24, 27, 43): '[0.5723, 1.3956]'}, {}), '([0.5723, 1.3956])', True, 'import numpy as np\n'), ((28, 15, 28, 41), 'numpy.array', 'np.array', ({(28, 24, 28, 40): '[0.7894, 0.9564]'}, {}), '([0.7894, 0.9564])', True, 'import numpy as np\n'), ((29, 18, 29, 44), 'numpy.array', 'np.array', ({(29, 27, 29, 43): '[17.587, 18.956]'}, {}), '([17.587, 18.956])', True, 'import numpy as np\n'), ((31, 22, 31, 42), 'numpy.array', 'np.array', ({(31, 31, 31, 41): '[258, 395]'}, {}), '([258, 395])', True, 'import numpy as np\n'), ((32, 20, 32, 37), 'numpy.array', 'np.array', ({(32, 29, 32, 36): '[1, 17]'}, {}), '([1, 17])', True, 'import numpy as np\n'), ((33, 20, 33, 38), 'numpy.array', 'np.array', ({(33, 29, 33, 37): '[15, 19]'}, {}), '([15, 19])', True, 'import numpy as np\n'), ((36, 19, 36, 47), 'numpy.array', 'np.array', ({(36, 28, 36, 46): '[0.76778, 0.80049]'}, {}), '([0.76778, 0.80049])', True, 'import numpy as np\n'), ((37, 19, 37, 47), 'numpy.array', 'np.array', ({(37, 28, 37, 46): '[0.04012, 0.10077]'}, {}), '([0.04012, 0.10077])', True, 'import numpy as np\n'), ((38, 20, 38, 46), 'numpy.array', 'np.array', ({(38, 29, 38, 45): '[0.01085, 0.081]'}, {}), '([0.01085, 0.081])', True, 'import numpy as np\n'), ((42, 8, 42, 68), 'numpy.testing.assert_almost_equal', 'testing.assert_almost_equal', (), '', True, 'import numpy.testing as testing\n'), ((46, 8, 46, 68), 'numpy.testing.assert_almost_equal', 'testing.assert_almost_equal', (), '', True, 'import numpy.testing as testing\n'), ((50, 8, 50, 69), 'numpy.testing.assert_almost_equal', 'testing.assert_almost_equal', (), '', True, 'import numpy.testing as testing\n'), ((53, 16, 53, 82), 'redmapper.ColorBackground', 'ColorBackground', (), '', False, 'from redmapper import ColorBackground\n'), ((55, 15, 55, 49), 'numpy.array', 'np.array', ({(55, 24, 55, 48): '[0.5723, 1.3956, 1.0]'}, {}), '([0.5723, 1.3956, 1.0])', True, 'import numpy as np\n'), ((56, 15, 56, 46), 'numpy.array', 'np.array', ({(56, 24, 56, 45): '[0.7894, 0.9564, 1.0]'}, {}), '([0.7894, 0.9564, 1.0])', True, 'import numpy as np\n'), ((57, 18, 57, 50), 'numpy.array', 'np.array', ({(57, 27, 57, 49): '[17.587, 18.956, 25.0]'}, {}), '([17.587, 18.956, 25.0])', True, 'import numpy as np\n'), ((59, 23, 59, 59), 'numpy.array', 'np.array', ({(59, 32, 59, 58): '[127.698, 591.112, np.inf]'}, {}), '([127.698, 591.112, np.inf])', True, 'import numpy as np\n'), ((60, 23, 60, 57), 'numpy.array', 'np.array', ({(60, 32, 60, 56): '[7.569, 82.8938, np.inf]'}, {}), '([7.569, 82.8938, np.inf])', True, 'import numpy as np\n'), ((64, 8, 64, 72), 'numpy.testing.assert_almost_equal', 'testing.assert_almost_equal', (), '', True, 'import numpy.testing as testing\n'), ((68, 8, 68, 72), 'numpy.testing.assert_almost_equal', 'testing.assert_almost_equal', (), '', True, 'import numpy.testing as testing\n'), ((74, 17, 74, 63), 'redmapper.Configuration', 'Configuration', ({(74, 31, 74, 62): "file_path + '/' + conf_filename"}, {}), "(file_path + '/' + conf_filename)", False, 'from redmapper import Configuration\n'), ((76, 16, 76, 34), 'tempfile.mkstemp', 'tempfile.mkstemp', ({}, {}), '()', False, 'import tempfile\n'), ((77, 8, 77, 26), 'os.close', 'os.close', ({(77, 17, 77, 25): 'tfile[0]'}, {}), '(tfile[0])', False, 'import os\n'), ((83, 14, 83, 63), 'redmapper.ColorBackgroundGenerator', 'ColorBackgroundGenerator', (), '', False, 'from redmapper import ColorBackgroundGenerator\n'), ((87, 15, 87, 48), 'fitsio.FITS', 'fitsio.FITS', ({(87, 27, 87, 47): 'config.bkgfile_color'}, {}), '(config.bkgfile_color)', False, 'import fitsio\n'), ((106, 8, 106, 39), 'os.remove', 'os.remove', ({(106, 18, 106, 38): 'config.bkgfile_color'}, {}), '(config.bkgfile_color)', False, 'import os\n')]