def sky_transmission_curve(wavels, delta_lambda, zenith_ang):
    '''Function that generates a full throughput curve combining
	sky transmission & sky extinction.

	Inputs:
		wavels: array of wavelengths for datacube
		delta_lambda: Resolution element [um]
		zenith_ang: zenith angle of observation [degrees]

	Outputs:
		cube_total_sky_trans: array of total throughput
			for each wavelength value in wavels
	'''
    #convert from zenith angle to airmass
    X = 1. / n.cos(n.radians(zenith_ang))
    inbuilt_airmasses = [1., 1.15, 1.41, 2.]

    #determine the closest data to the airmass value given and find it's location in the data file
    closest_X = min(inbuilt_airmasses, key=lambda x: abs(x - X))
    data_index = inbuilt_airmasses.index(closest_X) + 1

    #load sky transmission & extinction files, then reduce to the columns required
    sky_trans_all_X = n.genfromtxt(
        os.path.join(
            tppath,
            'ASM_throughput/transmission_0.15_angstroms_resolution.txt'))
    sky_trans = n.column_stack(
        (sky_trans_all_X[:, 0], sky_trans_all_X[:, data_index]))
    print len(sky_trans)
    sky_trans_all_X = []  #clean up unused data for sake of memory

    #Find start and end wavelength values of curves
    st_start_arg = n.where(sky_trans[:, 0] < wavels[0])[0][-1]
    st_end_arg = n.where(sky_trans[:, 0] > wavels[-1])[0][0]
    sky_trans_slice = sky_trans[st_start_arg:st_end_arg + 1, :]

    if type(delta_lambda) == n.ndarray:
        #Low resolution mode. Use low_res_mode function to generate sky transmission spectrum
        sky_trans_spec = low_res_spec(sky_trans, delta_lambda, True)
        sky_total_trans = sky_trans_spec[:, 1].copy()

    else:
        #Convolve sky transmission array with Gaussian LSF of
        #FWHM = sqrt(new_resolution**2-old_resolution**2)
        #to fold in spectrum for each spectral pixel.
        sigma = n.sqrt(delta_lambda**2 - 0.15E-4**2) / (2. *
                                                        n.sqrt(2. * n.log(2.)))
        conv_sky_trans = gauss_convolve(sky_trans_slice,
                                        sigma,
                                        lambda_space='Linear')

        interp_trans = s.interpolate.interp1d(conv_sky_trans[:, 0],
                                              conv_sky_trans[:, 1],
                                              kind='linear',
                                              bounds_error=False,
                                              fill_value=0)
        sky_total_trans = interp_trans(wavels)

    sky_total_trans.shape = (len(wavels), 1, 1)
    return sky_total_trans, wavels
def sky_background(wavels, delta_lambda, dit):
    '''Function that generates a sky background curve combining
    sky continuum, sky thermal emission and sky emission lines.
    
    Inputs:
        wavels: array of wavelengths for datacube
        delta_lambda: resolution element [um]
        dit: exposure time [s]. This determins how the sky emission
             line amplitudes vary through the exposure.
        
    Outputs:
        sky_bg_curve: array of total sky background
                  [units of photons/s/m^2/um/arcsec^2]
                  for each wavelength value in wavels
    '''

    #Load up all datafile using n.genfromtxt(...)
    #sky_em = n.genfromtxt(bgpath+'ASM_background/radiance_resolution_0.15_angstroms_MICRONS.txt')
    sky_em = n.genfromtxt(
        os.path.join(
            bgpath,
            'ASM_background/radiance_resolution_0.15_angstroms_MICRONS.txt'))

    #Cut down data to relevent region.
    se_start_arg = n.where(sky_em[:, 0] < wavels[0])[0][-1]
    se_end_arg = n.where(sky_em[:, 0] > wavels[-1])[0][0]
    sky_em_slice = sky_em[se_start_arg:se_end_arg + 1, :]

    if type(delta_lambda) == n.ndarray:
        #Low resolution mode. Use low_res_mode function to generate sky emission spectrum
        sky_em_spec = low_res_spec(sky_em, delta_lambda, False)
        binned_sky_em = sky_em_spec[:, 1].copy()

    else:
        #Convolve sky transmission array with Gaussian LSF of
        #FWHM = sqrt(new_resolution**2-old_resolution**2)
        #to fold in spectrum for each spectral pixel.
        input_disp = sky_em[1, 0] - sky_em[0, 0]
        sigma = n.sqrt(delta_lambda**2 -
                       input_disp**2) / (2. * n.sqrt(2. * n.log(2.)))

        conv_sky_em = gauss_convolve(sky_em_slice,
                                     sigma,
                                     lambda_space='Linear')
        ###new 30-05-14
        conv_sky_em_ph = conv_sky_em[:,
                                     1] * input_disp  #convert to units of photons/s/m^2/arcsec^2
        binned_sky_em_ph = frebin(
            conv_sky_em_ph.reshape(len(conv_sky_em_ph), 1), (1, len(wavels)),
            True)  #Total photons conserved
        binned_sky_em = binned_sky_em_ph / float(
            wavels[1] -
            wavels[0])  #reconvert back to photons/s/m^2/um/arcsec^2

    binned_sky_em.shape = (len(wavels), 1, 1)
    return binned_sky_em
def sky_background(wavels, delta_lambda, dit):
    '''Function that generates a sky background curve combining
    sky continuum, sky thermal emission and sky emission lines.
    
    Inputs:
        wavels: array of wavelengths for datacube
        delta_lambda: resolution element [um]
        dit: exposure time [s]. This determins how the sky emission
             line amplitudes vary through the exposure.
        
    Outputs:
        sky_bg_curve: array of total sky background
                  [units of photons/s/m^2/um/arcsec^2]
                  for each wavelength value in wavels
    '''
    
    #Load up all datafile using n.genfromtxt(...)
    #sky_em = n.genfromtxt(bgpath+'ASM_background/radiance_resolution_0.15_angstroms_MICRONS.txt')
    sky_em = n.genfromtxt(os.path.join(bgpath,'ASM_background/radiance_resolution_0.15_angstroms_MICRONS.txt'))
    
    #Cut down data to relevent region.
    se_start_arg = n.where(sky_em[:,0] < wavels[0])[0][-1]
    se_end_arg = n.where(sky_em[:,0] > wavels[-1])[0][0]
    sky_em_slice = sky_em[se_start_arg:se_end_arg+1,:]

    if type(delta_lambda) == n.ndarray:
        #Low resolution mode. Use low_res_mode function to generate sky emission spectrum
        sky_em_spec = low_res_spec(sky_em, delta_lambda, False)
        binned_sky_em = sky_em_spec[:,1].copy()

    else:
        #Convolve sky transmission array with Gaussian LSF of
        #FWHM = sqrt(new_resolution**2-old_resolution**2)
        #to fold in spectrum for each spectral pixel.
        input_disp = sky_em[1,0] - sky_em[0,0]
        sigma = n.sqrt(delta_lambda**2-input_disp**2)/(2.*n.sqrt(2.*n.log(2.)))
        
        conv_sky_em = gauss_convolve(sky_em_slice, sigma, lambda_space='Linear')
        ###new 30-05-14
        conv_sky_em_ph = conv_sky_em[:,1]*input_disp #convert to units of photons/s/m^2/arcsec^2
        binned_sky_em_ph = frebin(conv_sky_em_ph.reshape(len(conv_sky_em_ph),1), (1,len(wavels)), True) #Total photons conserved
        binned_sky_em = binned_sky_em_ph/float(wavels[1]-wavels[0]) #reconvert back to photons/s/m^2/um/arcsec^2
    
    binned_sky_em.shape = (len(wavels),1,1)
    return binned_sky_em
def sky_transmission_curve(wavels, delta_lambda):
    '''Function that generates a full throughput curve combining
    sky transmission & sky extinction.

    Inputs:
        wavels: array of wavelengths for datacube
        delta_lambda: Resolution element [um]

    Outputs:
        cube_total_sky_trans: array of total throughput
                     for each wavelength value in wavels
    '''

    #NEED TO IMPLEMENT DIFFERENT AIRMASS VALUES AT SOME POINT
    #Currently set at 1.15 to match PSFs - Zenith angle = 30deg

    #load sky transmission & extinction files
    sky_trans = n.genfromtxt(os.path.join(tppath,'ASM_throughput/transmission_resolution_0.15_angstroms_MICRONS.txt'))

    #Find start and end wavelength values of curves
    st_start_arg = n.where(sky_trans[:,0] < wavels[0])[0][-1]
    st_end_arg = n.where(sky_trans[:,0] > wavels[-1])[0][0]
    sky_trans_slice = sky_trans[st_start_arg:st_end_arg+1,:]

    if type(delta_lambda) == n.ndarray:
        #Low resolution mode. Use low_res_mode function to generate sky transmission spectrum
        sky_trans_spec = low_res_spec(sky_trans, delta_lambda, True)
        sky_total_trans = sky_trans_spec[:,1].copy()

    else:
        #Convolve sky transmission array with Gaussian LSF of
        #FWHM = sqrt(new_resolution**2-old_resolution**2)
        #to fold in spectrum for each spectral pixel.
        sigma = n.sqrt(delta_lambda**2-0.15E-4**2)/(2.*n.sqrt(2.*n.log(2.)))
        conv_sky_trans = gauss_convolve(sky_trans_slice, sigma, lambda_space='Linear')

        interp_trans = s.interpolate.interp1d(conv_sky_trans[:,0], conv_sky_trans[:,1],
                                              kind='linear', bounds_error=False, fill_value=0)
        sky_total_trans = interp_trans(wavels)

    sky_total_trans.shape = (len(wavels),1,1)
    return sky_total_trans, wavels