import unittest

import numpy as np
from desispec.sky import compute_sky
from desispec.resolution import Resolution

class TestSky(unittest.TestCase):
    
    #- Create unique test filename in a subdirectory
    def setUp(self):
        #- Create a fake sky
        self.nspec = 10
        self.wave = np.arange(4000, 4500)
        self.nwave = len(self.wave)
        self.flux = np.zeros(self.nwave)
        for i in range(0, self.nwave, 20):
            self.flux[i] = i
            
        self.ivar = np.ones(self.flux.shape)
                    
    def test_uniform_resolution(self):        
        #- Setup data for a Resolution matrix
        sigma = 4.0
        ndiag = 21
        xx = np.linspace(-(ndiag-1)/2.0, +(ndiag-1)/2.0, ndiag)
        Rdata = np.zeros( (self.nspec, ndiag, self.nwave) )
        for i in range(self.nspec):
            for j in range(self.nwave):
                kernel = np.exp(-xx**2/(2*sigma))
                kernel /= sum(kernel)
                Rdata[i,:,j] = kernel
                
        flux = np.zeros((self.nspec, self.nwave))
        for i in range(self.nspec):
            R = Resolution(Rdata[i])
            flux[i] = R.dot(self.flux)
                
        skyflux, skyivar, skymask = compute_sky(self.wave, flux, self.ivar, Rdata)
        self.assertEqual(len(skyflux.shape), 1)
        self.assertEqual(len(skyflux), self.nwave)
        self.assertEqual(len(skyflux), len(skyivar))
        self.assertEqual(len(skyflux), len(skymask))
        
    def runTest(self):
        pass
                
if __name__ == '__main__':
    unittest.main()
@@ -110,16 +110,30 @@
 lution%0A%0A
+import pylab%0A%0A
 class Te
@@ -1085,16 +1085,65 @@
 nwave))%0A
+        ivar = np.ones((self.nspec, self.nwave))%0A
         
@@ -1246,16 +1246,25 @@
 f.flux)%0A
+%0A        
         
@@ -1305,16 +1305,36 @@
  skymask
+, cskyflux, cskyivar
  = compu
@@ -1357,21 +1357,16 @@
 , flux, 
-self.
 ivar, Rd
@@ -1575,16 +1575,217 @@
 ymask))%0A
+        delta=flux%5B0%5D-cskyflux%0A        d=np.inner(delta,delta)%0A        self.assertAlmostEqual(d,0.)%0A        delta=flux%5B-1%5D-cskyflux%0A        d=np.inner(delta,delta)%0A        self.assertAlmostEqual(d,0.)%0A
         
@@ -1777,32 +1777,34 @@
 (d,0.)%0A        %0A
+%0A%0A
     def runTest(
@@ -1886,8 +1886,9 @@
 t.main()
+%0A

fix bugs in test_sky unit test, also checck that output sky is consistent with input