"""
io routines for sky

"""
import os
from astropy.io import fits


def write_sky(outfile,head,skyflux,skyivar,skymask,wave) :
    """
    write fiberflat
    """
    hdr = head
    hdr['EXTNAME'] = ('SKY', 'no dimension')
    fits.writeto(outfile,skyflux,header=hdr, clobber=True)
    
    hdr['EXTNAME'] = ('IVAR', 'no dimension')
    hdu = fits.ImageHDU(skyivar, header=hdr)
    fits.append(outfile, hdu.data, header=hdu.header)
    
    hdr['EXTNAME'] = ('MASK', 'no dimension')
    hdu = fits.ImageHDU(skymask, header=hdr)
    fits.append(outfile, hdu.data, header=hdu.header)
    
    hdr['EXTNAME'] = ('WAVELENGTH', '[Angstroms]')
    hdu = fits.ImageHDU(wave, header=hdr)
    fits.append(outfile, hdu.data, header=hdu.header)
    
    

@@ -113,16 +113,34 @@
 skymask,
+cskyflux,cskyivar,
 wave) :%0A
@@ -607,157 +607,994 @@
     
-hdr%5B'EXTNAME'%5D = ('WAVELENGTH', '%5BAngstroms%5D')%0A    hdu = fits.ImageHDU(wave, header=hdr)%0A    fits.append(outfile, hdu.data, header=hdu.header)%0A    %0A    
+%0A    hdr%5B'EXTNAME'%5D = ('CSKY', 'convolved sky at average resolution')%0A    hdu = fits.ImageHDU(cskyflux, header=hdr)%0A    fits.append(outfile, hdu.data, header=hdu.header)%0A    %0A    hdr%5B'EXTNAME'%5D = ('CIVAR', 'convolved sky inverse variance')%0A    hdu = fits.ImageHDU(cskyivar, header=hdr)%0A    fits.append(outfile, hdu.data, header=hdu.header)%0A    %0A    hdr%5B'EXTNAME'%5D = ('WAVELENGTH', '%5BAngstroms%5D')%0A    hdu = fits.ImageHDU(wave, header=hdr)%0A    fits.append(outfile, hdu.data, header=hdu.header)%0A    %0Adef read_sky(filename) :%0A%0A    %22%22%22%0A    read sky%0A    %22%22%22%0A    skyflux=fits.getdata(filename, 0).astype('float64')%0A    ivar=fits.getdata(filename, %22IVAR%22).astype('float64')%0A    mask=fits.getdata(filename, %22MASK%22).astype('int') # ??? SOMEONE CHECK THIS ???%0A    cskyflux=fits.getdata(filename, %22CSKY%22).astype('float64')%0A    civar=fits.getdata(filename, %22CIVAR%22).astype('float64')%0A    wave=fits.getdata(filename, %22WAVELENGTH%22).astype('float64')%0A    %0A    return skyflux,ivar,mask,cskyflux,civar,wave%0A
 %0A

add convolved sky and ivar