def test_lsf_COS(): gratings = ['G130M', 'G160M', 'G140L', 'G230L', 'G185M', 'G225M', 'G285M'] life_positions = ['1', '2', '3'] cen_waves_G160M = ['1577', '1589', '1600', '1611', '1623'] cen_waves_G130M = ['1291', '1300', '1309', '1318', '1327'] for grating in gratings: for lp in life_positions: instr_config = dict(name='COS', grating=grating, life_position=lp) if lp in ['2', '3']: if grating not in ['G130M', 'G160M']: continue if grating == 'G130M': cen_waves_aux = cen_waves_G130M elif grating == 'G160M': cen_waves_aux = cen_waves_G160M for cen_wave in cen_waves_aux: instr_config['cen_wave'] = cen_wave lsf = LSF(instr_config) print(lp, grating, cen_wave) elif lp == '1': lsf = LSF(instr_config) print(lp, grating)
def test_interpolate_to_wv0_wv0shortlong(plot=False): err_msg = 'Something is wrong with short wavelength handling in LSF.interpolate_to_wv0()' wv0 = 1105.0 * u.AA cos_dict = dict(name='COS', grating='G130M', life_position='1') lsf_cos = LSF(cos_dict) lsf_tab = lsf_cos.interpolate_to_wv0(wv0) assert lsf_tab[len(lsf_tab) // 2]['wv'] == wv0.value, err_msg assert lsf_tab[len(lsf_tab) // 2]['kernel'] == np.max( lsf_tab['kernel']), err_msg if plot: import matplotlib.pyplot as plt lsf_tab = lsf_cos.interpolate_to_wv0(wv0) plt.plot(lsf_tab['wv'] - wv0.value, lsf_tab['kernel'], '-') plt.show() err_msg = 'Something is wrong with long wavelength handling in LSF.interpolate_to_wv0()' wv0 = 1796.0 * u.AA cos_dict = dict(name='COS', grating='G160M', life_position='1') lsf_cos = LSF(cos_dict) lsf_tab = lsf_cos.interpolate_to_wv0(wv0) assert lsf_tab[len(lsf_tab) // 2]['wv'] == wv0.value, err_msg assert lsf_tab[len(lsf_tab) // 2]['kernel'] == np.max( lsf_tab['kernel']), err_msg if plot: import matplotlib.pyplot as plt lsf_tab = lsf_cos.interpolate_to_wv0(wv0) plt.plot(lsf_tab['wv'] - wv0.value, lsf_tab['kernel'], '-') plt.show() # test error outside range wv0 = 1300.0 * u.AA with pytest.raises(ValueError): lsf_tab = lsf_cos.interpolate_to_wv0(wv0) wv0 = 1900.0 * u.AA with pytest.raises(ValueError): lsf_tab = lsf_cos.interpolate_to_wv0(wv0)
def test_interpolate_to_wv0(plot=False, lp='2'): err_msg = 'Something is wrong with LSF.interpolate_to_wv0()' wv0 = 1160 * u.AA cos_dict = dict(name='COS', grating='G130M', life_position=lp, cen_wave='1309') stis_dict = dict(name='STIS', grating='G140L', slit='52x0.2') lsf_cos = LSF(cos_dict) lsf_stis = LSF(stis_dict) for lsf in [lsf_cos, lsf_stis]: lsf_tab = lsf.interpolate_to_wv0(wv0) assert lsf_tab[len(lsf_tab) // 2]['wv'] == wv0.value, err_msg assert lsf_tab[len(lsf_tab) // 2]['kernel'] == np.max(lsf_tab['kernel']), err_msg if plot: import matplotlib.pyplot as plt if lp == '3': wv_array = np.linspace(1200, 1400, 10) * u.AA else: wv_array = np.arange(1200, 1400, 10) * u.AA for wv in wv_array: lsf_tab = lsf_cos.interpolate_to_wv0(wv) plt.plot(lsf_tab['wv'] - wv.value, lsf_tab['kernel'], '-') plt.suptitle(lsf.name) # import pdb; pdb.set_trace() plt.show() # test last column interpolation lsf = LSF(dict(name='COS', grating='G130M', life_position='1')) tab = lsf.interpolate_to_wv0(1450 * u.AA)
def use_custom_lsf(self,FWHM='6.5',grating='G130M',life_position='1',cen_wave='1300A'): if FWHM=='COS': instr_config=dict(name='COS',grating=grating,life_position=life_position,cen_wave=cen_wave) coslsf=LSF(instr_config) s,data=coslsf.load_COS_data() #if 1150A 1200A 1250A 1300A 1350A 1400A 1450A kernel = CustomKernel(data[cen_wave].data) else: COS_kernel=np.double(FWHM)/2.355 #6.5 pixels #window_size_number_of_points=np.round(FWHM /(2.355*np.abs(velgrid[2]-velgrid[1]))) # Create kernel kernel = Gaussian1DKernel(stddev=COS_kernel) self.kernel=kernel
def test_interpolate_to_wv0(plot=False, lp='2'): err_msg = 'Something is wrong with LSF.interpolate_to_wv0()' wv0 = 1160 * u.AA cos_dict = dict(name='COS', grating='G130M', life_position=lp, cen_wave='1309') stis_dict = dict(name='STIS', grating='G140L', slit='52x0.2') stis_echelle_dict = dict(name='STIS', grating='E140M', slit='0.2x0.06') lsf_cos = LSF(cos_dict) lsf_stis = LSF(stis_dict) lsf_stis_echelle = LSF(stis_echelle_dict) for lsf in [lsf_cos, lsf_stis, lsf_stis_echelle]: lsf_tab = lsf.interpolate_to_wv0(wv0) assert lsf_tab[len(lsf_tab) // 2]['wv'] == wv0.value, err_msg assert lsf_tab[len(lsf_tab) // 2]['kernel'] == np.max( lsf_tab['kernel']), err_msg if plot: import matplotlib.pyplot as plt if lp == '3': wv_array = np.linspace(1200, 1400, 10) * u.AA else: wv_array = np.arange(1200, 1400, 10) * u.AA for wv in wv_array: lsf_tab = lsf_cos.interpolate_to_wv0(wv) plt.plot(lsf_tab['wv'] - wv.value, lsf_tab['kernel'], '-') plt.suptitle(lsf.name) # import pdb; pdb.set_trace() plt.show() # test last column interpolation lsf = LSF(dict(name='COS', grating='G130M', life_position='1')) tab = lsf.interpolate_to_wv0(1450 * u.AA)
def test_interpolate_to_wv_array(plot=False, lp='2'): err_msg = 'Something is wrong with LSF.interpolate_to_wv_array()' wv_array = np.arange(1600, 1601, 0.001) * u.AA wv_array = np.arange(1600, 1650, 0.001) * u.AA cen_waves = ['1577', '1589A', '1600', '1611', '1623'] colors = ['k', 'b', 'g', 'r', 'orange'] lsf_dict = dict() for i, cen_wave in enumerate(cen_waves): cos_dict_aux = dict(name='COS', grating='G160M', life_position=lp, cen_wave=cen_wave) lsf_dict[cen_wave] = LSF(cos_dict_aux) lsf_tab = lsf_dict[cen_wave].interpolate_to_wv_array(wv_array) assert isinstance(lsf_tab, Table), err_msg if plot: import matplotlib.pyplot as plt plt.plot(wv_array, lsf_tab['kernel'], '-', color=colors[i]) if plot: plt.show() # other tests lsf = LSF(dict(name='COS', grating='G130M', life_position='1')) wv_array = np.linspace(1210, 1211, 100) * u.AA # cubic tab = lsf.interpolate_to_wv_array(wv_array, kind='cubic', debug=True) # errors with pytest.raises(SyntaxError): tbl = lsf.interpolate_to_wv_array('bad_input') with pytest.raises(SyntaxError): x = np.array([[1, 2, 3], [4, 5, 6]]) tbl = lsf.interpolate_to_wv_array(x) # bad shape with pytest.raises(ValueError): tbl = lsf.interpolate_to_wv_array(np.array([1, 2]) * u.AA, kind='wrong_kind') with pytest.raises(ValueError): tbl = lsf.interpolate_to_wv_array(np.array([1, 2]) * u.AA, kind='cubic') # bad input wv_array
def test_interpolate_to_wv0_wv0shortlong(plot=False): err_msg = 'Something is wrong with short wavelength handling in LSF.interpolate_to_wv0()' wv0 = 1105.0 * u.AA cos_dict = dict(name='COS', grating='G130M', life_position='1') lsf_cos = LSF(cos_dict) lsf_tab = lsf_cos.interpolate_to_wv0(wv0) assert lsf_tab[len(lsf_tab) // 2]['wv'] == wv0.value, err_msg assert lsf_tab[len(lsf_tab) // 2]['kernel'] == np.max(lsf_tab['kernel']), err_msg if plot: import matplotlib.pyplot as plt lsf_tab = lsf_cos.interpolate_to_wv0(wv0) plt.plot(lsf_tab['wv'] - wv0.value, lsf_tab['kernel'], '-') plt.show() err_msg = 'Something is wrong with long wavelength handling in LSF.interpolate_to_wv0()' wv0 = 1796.0 * u.AA cos_dict = dict(name='COS', grating='G160M', life_position='1') lsf_cos = LSF(cos_dict) lsf_tab = lsf_cos.interpolate_to_wv0(wv0) assert lsf_tab[len(lsf_tab) // 2]['wv'] == wv0.value, err_msg assert lsf_tab[len(lsf_tab) // 2]['kernel'] == np.max(lsf_tab['kernel']), err_msg if plot: import matplotlib.pyplot as plt lsf_tab = lsf_cos.interpolate_to_wv0(wv0) plt.plot(lsf_tab['wv'] - wv0.value, lsf_tab['kernel'], '-') plt.show() # test error outside range wv0 = 1300.0 * u.AA with pytest.raises(ValueError): lsf_tab = lsf_cos.interpolate_to_wv0(wv0) wv0 = 1900.0 * u.AA with pytest.raises(ValueError): lsf_tab = lsf_cos.interpolate_to_wv0(wv0)
def test_interpolate_to_wv0(plot=False): err_msg = 'Something is wrong with LSF.interpolate_to_wv0()' wv0 = 1160*u.AA cos_dict = dict(name='COS',grating='G130M',life_position='2',cen_wave='1309') lsf_cos = LSF(cos_dict) lsf_tab = lsf_cos.interpolate_to_wv0(wv0) assert lsf_tab[len(lsf_tab)/2]['wv'] == wv0.value, err_msg assert lsf_tab[len(lsf_tab)/2]['kernel'] == np.max(lsf_tab['kernel']), err_msg if plot: import matplotlib.pyplot as plt wv_array = np.arange(1200,1400,10)*u.AA for wv in wv_array: lsf_tab = lsf_cos.interpolate_to_wv0(wv) plt.plot(lsf_tab['wv']-wv.value,lsf_tab['kernel'],'-') plt.show()
def get_lsfs(): lsfobjs=[] for i,inst in enumerate(cfg.instr): if inst in ['COS','STIS']: lsfobjs.append(LSF(dict(name=inst, grating=cfg.gratings[i], life_position=cfg.lps[i], cen_wave=cfg.cen_wave[i], slit=cfg.slits[i]))) elif inst in ['Gaussian','gaussian']: lsfobjs.append(LSF(dict(name=inst, pixel_scale=cfg.pixel_scales[i], FWHM=cfg.fwhms[i]))) cfg.lsfs=[] for fg in cfg.fgs: if isinstance(fg,int): lamobs=cfg.wave[fg] lsfmatch = jbg.wherebetween(lamobs, cfg.lsfranges[:, 0], cfg.lsfranges[:, 1]) lsf = lsfobjs[lsfmatch[0]].interpolate_to_wv_array(cfg.wave[cfg.fgs] * u.AA, kind='cubic') cfg.lsfs.append(lsf['kernel']) break else: lamobs=np.median(cfg.wave[fg]) lsfmatch = jbg.wherebetween(lamobs, cfg.lsfranges[:, 0], cfg.lsfranges[:, 1]) if len(fg) < 10: print("Line at {:.2f} AA is undersampling the LSF. Will increase number of pixels at either side to" " include at least 10.".format(lamobs)) if np.isnan(lamobs): import pdb;pdb.set_trace() # NT: this could happen when pixels with S/N<0 exist, i.e. pixels where flux is <0 (e.g. black lines). # For this reason, is better to remove the option to eliminate pixels based on S/N. n_more_side = int(np.ceil((10 - len(fg))/2 + 1)) inds_right = [np.max(fg) + ii + 1 for ii in range(n_more_side)] inds_left = [np.min(fg) - ii - 1 for ii in range(n_more_side)] inds_left.sort() fg = inds_left + fg.tolist() + inds_right fg = np.array(fg) print("New fg is: {}".format(fg)) try: lsf = lsfobjs[lsfmatch[0]].interpolate_to_wv_array(cfg.wave[fg] * u.AA, kind='cubic') except: import pdb; pdb.set_trace() # except: # QtCore.pyqtRemoveInputHook() # import pdb; pdb.set_trace() # QtCore.pyqtRestoreInputHook() cfg.lsfs.append(lsf['kernel'])
def test_lsf_STIS(): gratings = [ 'G750L', 'G140M', 'G140L', 'G230M', 'G230L', 'E140H', 'E140M', 'E230H', 'E230M' ] # G750M, G430M, G430L, G230LB G230MB still not fully implemented. #todo : implement G430M, G430L, G230LB, G230MB available_slits = { 'G140L': ['52x0.1', '52x0.2', '52x0.5', '52x2.0'], 'G140M': ['52x0.1', '52x0.2', '52x0.5', '52x2.0'], 'G230L': ['52x0.1', '52x0.2', '52x0.5', '52x2.0'], 'G230M': ['52x0.1', '52x0.2', '52x0.5', '52x2.0'], 'E140H': ['0.1x0.03', '0.2x0.09', '0.2x0.2', '6x0.2'], 'E140M': ['0.1x0.03', '0.2x0.06', '0.2x0.2', '6x0.2'], 'E230H': ['0.1x0.03', '0.1x0.09', '0.1x0.2', '6x0.2'], 'E230M': ['0.1x0.03', '0.2x0.06', '0.2x0.2', '6x0.2'], 'G430L': ['52x0.1', '52x0.2', '52x0.5', '52x2.0'], 'G750L': ['52x0.1', '52x0.2', '52x0.5', '52x2.0'] } for grating in gratings: # slits for slit in available_slits[grating]: instr_config = dict(name='STIS', grating=grating, slit=slit) lsf = LSF(instr_config)
def test_lsf_Gaussian(): fwhms = [0.4, 1.2, 1.6] pix_scales = [0.225, 0.01, 1.75] for ff in fwhms: # slits for ps in pix_scales: instr_config = dict(name='Gaussian', pixel_scale=ps, FWHM=ff) lsf = LSF(instr_config)
def test_lsf_COS(): gratings = ['G130M', 'G160M', 'G140L', 'G230L', 'G185M', 'G225M', 'G285M'] life_positions = ['1', '2', '3', '4'] cen_waves_G160M = ['1577', '1589', '1600', '1611', '1623'] cen_waves_G130M = ['1291', '1300', '1309', '1318', '1327'] cen_waves_G140L = ['1105', '1230', '1280'] for grating in gratings: for lp in life_positions: instr_config = dict(name='COS', grating=grating, life_position=lp) if lp in ['2', '3', '4']: if grating not in ['G130M', 'G160M', 'G140L']: continue if grating == 'G130M': cen_waves_aux = cen_waves_G130M if lp in ['3', '4']: # add the extra Cen Wave cen_waves_aux += ['1222'] elif grating == 'G160M': cen_waves_aux = cen_waves_G160M if lp == '4': # add the extra Cen Wave cen_waves_aux += ['1533'] elif grating == 'G140L': cen_waves_aux = cen_waves_G140L if (lp == '4'): cen_waves_aux = ['1105', '1280'] # 1230 not available for LP4 for cen_wave in cen_waves_aux: instr_config['cen_wave'] = cen_wave lsf = LSF(instr_config) print(lp, grating, cen_wave) elif lp == '1': lsf = LSF(instr_config) print(lp, grating)
def test_get_lsf(plot=False, lp='2'): err_msg = 'Something is wrong with LSF.get_lsf()' wv_array = np.arange(1250, 1251, 0.0001) * u.AA cen_waves = ['1291', '1300', '1309', '1318A', '1327A'] colors = ['k', 'b', 'g', 'r', 'orange'] lsf_dict = dict() for i, cen_wave in enumerate(cen_waves): cos_dict_aux = dict(name='COS', grating='G130M', life_position=lp, cen_wave=cen_wave) lsf_dict[cen_wave] = LSF(cos_dict_aux) lsf_kernel = lsf_dict[cen_wave].get_lsf(wv_array) assert isinstance(lsf_kernel, np.ndarray), err_msg if plot: import matplotlib.pyplot as plt plt.plot(wv_array, lsf_kernel, '-', color=colors[i]) # test for G140L grating wv_array = np.arange(1200, 1400, 0.1) * u.AA cen_waves = ['1105', '1280'] colors = ['k', 'b', 'g', 'r', 'orange'] lsf_dict = dict() for i, cen_wave in enumerate(cen_waves): cos_dict_aux = dict(name='COS', grating='G140L', life_position=lp, cen_wave=cen_wave) lsf_dict[cen_wave] = LSF(cos_dict_aux) lsf_kernel = lsf_dict[cen_wave].get_lsf(wv_array) assert isinstance(lsf_kernel, np.ndarray), err_msg if plot: import matplotlib.pyplot as plt plt.plot(wv_array, lsf_kernel, '-', color=colors[i]) if plot: plt.show()
def test_shift_to_wv0(plot=False): instr_dict1 = {'name': 'Gaussian', 'pixel_scale': 0.225, 'FWHM': 0.7} glsf1 = LSF(instr_config=instr_dict1) thislsf1 = glsf1.shift_to_wv0(3600. * u.AA) # should not work for multiple tabulated kernels cos_dict = dict(name='COS', grating='G130M', life_position='1') lsf_cos = LSF(cos_dict) with pytest.raises(ValueError): thislsfX = lsf_cos.interpolate_to_wv0(3600. * u.AA)
def test_lsf_init_errors(): with pytest.raises(TypeError): lsf = LSF('not_a_dict') with pytest.raises(SyntaxError): lsf = LSF(dict(wrong_key='xx')) with pytest.raises(NotImplementedError): lsf = LSF(dict(name='bad_instrument')) # for COS with pytest.raises(SyntaxError): lsf = LSF(dict(name='COS', not_grating_given='xx')) with pytest.raises(NotImplementedError): lsf = LSF(dict(name='COS', grating='not_implemented_grating')) with pytest.raises(SyntaxError): lsf = LSF(dict(name='COS', grating='G130M', not_life_pos_given='xx')) with pytest.raises(ValueError): lsf = LSF(dict(name='COS', grating='G130M', life_position='-1')) with pytest.raises(SyntaxError): lsf = LSF( dict(name='COS', grating='G130M', life_position='2', no_cen_wave_given='xx')) # for STIS with pytest.raises(SyntaxError): lsf = LSF(dict(name='STIS', not_grating_given='xx')) with pytest.raises(NotImplementedError): lsf = LSF(dict(name='STIS', grating='not_implemented_grating')) with pytest.raises(SyntaxError): lsf = LSF(dict(name='STIS', grating='G140L', not_slit_given='xx')) with pytest.raises(NotImplementedError): lsf = LSF(dict(name='STIS', grating='G140L', slit='bad_slit')) # for Gaussian with pytest.raises(KeyError): lsf = LSF(dict(name='Gaussian', not_ps_given=0.225, FWHM=0.4)) with pytest.raises(KeyError): lsf = LSF(dict(name='Gaussian', pixel_scale=0.225, not_fwhm_given=0.4))
def get_lsfs(): lsfobjs = [] for i, inst in enumerate(cfg.instr): if inst in ['COS', 'STIS']: lsfobjs.append( LSF( dict(name=inst, grating=cfg.gratings[i], life_position=cfg.lps[i], cen_wave=cfg.cen_wave[i], slit=cfg.slits[i]))) elif inst in ['Gaussian', 'gaussian']: lsfobjs.append( LSF( dict(name=inst, pixel_scale=cfg.pixel_scales[i], FWHM=cfg.fwhms[i]))) cfg.lsfs = [] fg_drop_list = [] for fg_e, fg in enumerate(cfg.fgs): if isinstance(fg, int): lamobs = cfg.wave[fg] lsfmatch = jbg.wherebetween(lamobs, cfg.lsfranges[:, 0], cfg.lsfranges[:, 1]) lsf = lsfobjs[lsfmatch[0]].interpolate_to_wv_array( cfg.wave[cfg.fgs] * u.AA, kind='cubic') cfg.lsfs.append(lsf['kernel']) break else: if multiple_gratings(cfg.wave[fg]): #Wave region straddles gratings, LSF solution unclear #not giving it an lsf, so also need to remove the wave region #from the wave region list fg_drop_list.append(fg_e) else: lamobs = np.median(cfg.wave[fg]) lsfmatch = jbg.wherebetween(lamobs, cfg.lsfranges[:, 0], cfg.lsfranges[:, 1]) if len(fg) < 10: print( "Line at {:.2f} AA is undersampling the LSF. Will increase number of pixels at either side to" " include at least 10.".format(lamobs)) if np.isnan(lamobs): import pdb pdb.set_trace() # NT: this could happen when pixels with S/N<0 exist, i.e. pixels where flux is <0 (e.g. black lines). # For this reason, is better to remove the option to eliminate pixels based on S/N. n_more_side = int(np.ceil((10 - len(fg)) / 2 + 1)) inds_right = [ np.max(fg) + ii + 1 for ii in range(n_more_side) ] inds_left = [ np.min(fg) - ii - 1 for ii in range(n_more_side) ] inds_left.sort() fg = inds_left + fg.tolist() + inds_right fg = np.array(fg) print("New fg is: {}".format(fg)) try: lsf = lsfobjs[lsfmatch[0]].interpolate_to_wv_array( cfg.wave[fg] * u.AA, kind='cubic') except: # import pdb; pdb.set_trace() print( "Generally when this happens, the line spans the two gratings." ) from IPython import embed embed() # except: # QtCore.pyqtRemoveInputHook() # import pdb; pdb.set_trace() # QtCore.pyqtRestoreInputHook() cfg.lsfs.append(lsf['kernel']) #remove wave regions that don't have an LSF solution #go backwards so that indices remain valid for drop_idx in fg_drop_list[::-1]: cfg.fgs.pop(drop_idx)