def RDSI_4_VIP(cube, angle_list, verbose, **kwargs): dprint(cube.shape) # diff_cube = cube - kwargs['cube_ref'] if kwargs['thru']: diff_cube = cube - kwargs['cube_ref'][:, -2] LCcube = np.transpose(diff_cube,(1,2,0)) else: diff_cube = kwargs['full_target_cube'] - kwargs['cube_ref'] LCcube = np.transpose(diff_cube, (2, 3, 0, 1)) dprint(diff_cube.shape) print(LCcube.shape) # quicklook_im(LCcube[:,:,0]) LCcube = np.resize(LCcube,(LCcube.shape[0],LCcube.shape[1],LCcube.shape[2],1)) print(LCcube.shape) algo_dict = {'thresh': 0} Lmap = get_Dmap(LCcube, algo_dict['thresh'], binning=2) quicklook_im(Lmap, annos=['MKIDs'], title= r' $I_L / I^{*}$', mark_star=True) dprint(LCcube.shape) # Lmap = get_Dmap(LCcube, kwargs['thresh']) # # quicklook_im(Lmap) # normed_Lmap = Lmap#/kwargs['DSI_starphot'] # Lmap = np.transpose(Lmap) # quicklook_im(Lmap) return Lmap
def plot_DSI(xlocs, ylocs, LCmapFile, inspect=False): with open(LCmapFile, 'rb') as handle: LCmap = pickle.load(handle) total_map = np.sum(LCmap, axis=2) median_map = np.median(LCmap, axis=2) interval_map = np.sum(LCmap[:, :, :5], axis=2) # quicklook_im(total_map, logAmp=True, show=False) # quicklook_im(median_map, logAmp=True, show=False) # quicklook_im(interval_map, logAmp=True, show=False) yinspect = range(62, 68) xinspect = range(62, 68) # yes # if inspect: # plt.imshow(interval_map[yinspect[0]:yinspect[-1], xinspect[0]:xinspect[-1]]) # plt.show() if os.path.isfile(DSFile): with open(DSFile, 'rb') as handle: Dmap = pickle.load(handle) else: Dmap = get_Dmap(LCmap, xlocs, ylocs, xinspect, yinspect, inspect) with open(DSFile, 'wb') as handle: pickle.dump(Dmap, handle, protocol=pickle.HIGHEST_PROTOCOL) quicklook_im(Dmap, logAmp=True, show=False) # , vmax=25)# plt.show() return total_map, median_map, interval_map, Iratio, mIratio
def DISI(LCmap, thresh=3e-6, plot=True): '''Dark I_s imaging''' Is_maps = [] print(LCmap.shape) for i in range(10): SSD_maps = get_Iratio(LCmap[:,:,i*100:(i+1)*100]) Is_maps.append(SSD_maps[1]) Is_maps = np.array(Is_maps) Dmap = np.zeros_like((Is_maps[0])) nexps = Is_maps.shape[0] # print nexps for ie in range(nexps): # print ie exp = Is_maps[ie] darkmask = np.transpose(exp <= thresh) # quicklook_im(exp,logAmp=False) # quicklook_im(darkmask,logAmp=False) Dmap += darkmask if plot and ie % 1 == 0: # # quicklook_im(exp, logAmp=True, vmin=0.1) quicklook_im(Dmap,logAmp=True, vmin=1) # quicklook_im(Dmap) # Dmap /= np.max(Dmap) # quicklook_im(Dmap) # # quicklook_im(1-Dmap) # finite = Dmap!=0 # min_nonzero = np.min(Dmap[finite]) # Lmap = 1./(Dmap+min_nonzero/100) # scale = np.sum(LCmap[0])/np.sum(Lmap) # Lmap *= scale # # Lmap = np.max(Dmap)-Dmap if plot: quicklook_im(Dmap, logAmp=True, vmin=1) return Dmap
def get_unoccult_DSIpsf(plot=False, hyperFile='/SSDHyperUnOccult.pkl', thresh=1e-6): hypercube = phot.get_unoccult_hyper(hyperFile, numframes=1000) LCmap = np.transpose(hypercube[:, 0]) Dmap = get_Dmap(LCmap, thresh) if plot: quicklook_im(Dmap) return Dmap
def generate_maps2(): import random print 'Generating optic aberration maps using Proper' wfo = proper.prop_begin(tp.diam, 1., tp.grid_size, tp.beam_ratio) # rms_error = 5e-6#500.e-9 # RMS wavefront error in meters # c_freq = 0.005 # correlation frequency (cycles/meter) # high_power = 1. # high frewquency falloff (r^-high_power) rms_error = 2.5e-3 # 500.e-9 # RMS wavefront error in meters c_freq = 0.000005 # correlation frequency (cycles/meter) high_power = 1. # high frewquency falloff (r^-high_power) # tp.abertime = [0.5,2,10] # characteristic time for each aberation in secs tp.abertime = [ 100 ] # if beyond numframes then abertime will be auto set to duration of simulation abercubes = [] aber_cube = np.zeros((ap.numframes, tp.grid_size, tp.grid_size)) print 'here' perms = np.random.rand(ap.numframes, tp.grid_size, tp.grid_size) - 0.5 perms *= 1e-7 phase = 2 * np.pi * np.random.uniform(size=(tp.grid_size, tp.grid_size)) - np.pi aber_cube[0] = proper.prop_psd_errormap( wfo, rms_error, c_freq, high_power, MAP="prim_map", PHASE_HISTORY=phase) # FILE=td.aberdir+'/telzPrimary_Map.fits') print 'lol' # quicklook_im(aber_cube[0], logAmp=False) for a in range(1, ap.numframes): # quicklook_im(aber_cube[a], logAmp=False) perms = np.random.rand(tp.grid_size, tp.grid_size) - 0.5 perms *= 0.05 phase += perms # print phase[:5,:5] aber_cube[a] = proper.prop_psd_errormap(wfo, rms_error, c_freq, high_power, MAP="prim_map", PHASE_HISTORY=phase) plt.plot(aber_cube[:, 20, 20]) plt.show() for a in range(0, ap.numframes - 1, 100): quicklook_im(aber_cube[a], logAmp=False) if not os.path.isdir(iop.aberdir): os.mkdir(iop.aberdir) for f in range(0, ap.numframes, 1): # print 'saving frame #', f if f % 100 == 0: misc.progressBar(value=f, endvalue=ap.numframes) rawImageIO.saveFITS(aber_cube[f], '%stelz%f.fits' % (iop.aberdir, f * cp.frame_time))
def get_unoccult_psf(plot=False, hyperFile = '/IntHyperUnOccult.pkl', numframes=1000): # import copy # tp_orig = copy.copy(tp) # ap_orig = copy.copy(ap) # iop_orig = copy.copy(iop) # # tp.detector = 'ideal' # ap.companion = False # # tp.NCPA_type = 'Wave' # # iop.hyperFile = iop.datadir + '/IntHyperUnOccult.pkl' # tp.occulter_type = 'None' # num_exp = 1 # ap.exposure_time = 0.001 # 0.001 # ap.numframes = int(num_exp * ap.exposure_time / cp.frame_time) # tp.nwsamp = 1 # # Yup this is 'if' is necessary # hypercube = read.get_integ_hypercube() hypercube = get_unoccult_hyper(hyperFile, numframes=numframes) # PSF = hypercube[0,0] # PSF = (read.take_exposure(hypercube))[0,0] # PSF = (read.take_exposure(hypercube)) PSF = hypercube if plot: quicklook_im(PSF) # tp.__dict__ = tp_orig.__dict__ # ap.__dict__ = ap_orig.__dict__ # iop.__dict__ = iop_orig.__dict__ # # print tp.occulter_type return PSF
def get_unoccult_SSDpsf(plot=False, hyperFile='/SSDHyperUnOccult.pkl'): hypercube = phot.get_unoccult_hyper(hyperFile, numframes=1000) LCmap = np.transpose(hypercube[:, 0]) xlocs = list(range(LCmap.shape[0])) ylocs = list(range(LCmap.shape[1])) images = get_Iratio(LCmap, xlocs, ylocs, xinspect=None, yinspect=None, inspect=None) Iratio = images[2] if plot: quicklook_im(Iratio) return Iratio
def do_SDI(datacube, plot=False): wsamples = np.linspace(tp.band[0], tp.band[1], tp.w_bins) scale_list = tp.band[0] / wsamples # print scale_list from vip_hci import pca dprint((datacube.shape, scale_list.shape)) fr_pca1 = pca.pca(datacube, angle_list=np.zeros((len(scale_list))), scale_list=scale_list, mask_center_px=None) # fr_pca1 = fr_pca1[:,:-4] if plot: quicklook_im(fr_pca1) # dprint(fr_pca1.shape) return fr_pca1
def save_pix_IQ(wf, plot=False): with open(iop.IQpixel, 'a') as the_file: complex_map = proper.prop_shift_center(wf.wfarr) pix0 = complex_map[64, 64] pix1 = complex_map[100, 100] the_file.write('%f, %f, %f, %f\n' % (np.real(pix0), np.imag(pix0), np.real(pix1), np.imag(pix1))) if plot: quicklook_im(np.real(complex_map)) quicklook_wf(wf, show=True) quicklook_IQ(wf, show=True)
def get_Dmap(LCmap, xlocs, ylocs, xinspect, yinspect, inspect): Dmap = np.zeros((len(xlocs), len(ylocs))) nexps = LCmap.shape[2] print nexps for ie in range(nexps): print ie exp = LCmap[:, :, ie] # quicklook_im(exp, logAmp=True) darkmask = exp == 0 # quicklook_im(darkmask,logAmp=False) Dmap += darkmask if ie % 100 == 0: quicklook_im(Dmap, logAmp=False) quicklook_im(Dmap, logAmp=False) return Dmap
def array_response(plot=False): print('Assigning each pixel a phase responsivity between 0 and 1') dist = Distribution(gaussian(mp.g_mean, mp.g_sig, np.linspace(0, 1.2, mp.res_elements)), interpolation=True) response = dist(mp.array_size[0] * mp.array_size[1])[0]/float(mp.res_elements) if plot: plt.xlabel('Responsivity') plt.ylabel('#') plt.hist(response) plt.show() response = np.reshape(response, mp.array_size[::-1]) if plot: quicklook_im(response)#plt.imshow(response) # plt.show() return response
def plot_DSI(xlocs, ylocs, LCmapFile): with open(LCmapFile, 'rb') as handle: LCmap = pickle.load(handle) total_map = np.sum(LCmap, axis=2) median_map = np.median(LCmap, axis=2) interval_map = np.sum(LCmap[:, :, :5], axis=2) # quicklook_im(total_map, logAmp=True, show=False) # quicklook_im(median_map, logAmp=True, show=False) # quicklook_im(interval_map, logAmp=True, show=False) if os.path.isfile(iop.DSFile): with open(iop.DSFile, 'rb') as handle: Dmap = pickle.load(handle) else: Dmap = get_Dmap(LCmap) with open(iop.DSFile, 'wb') as handle: pickle.dump(Dmap, handle, protocol=pickle.HIGHEST_PROTOCOL) quicklook_im(Dmap, logAmp=True, show=True) # , vmax=25)#
def get_unoccult_perf_psf(plot=False, hyperFile='/IntHyperUnOccult.pkl'): import copy tp_orig = copy.copy(tp) ap_orig = copy.copy(ap) iop_orig = copy.copy(iop) tp.detector = 'ideal' ap.companion = False # tp.NCPA_type = 'Wave' iop.hyperFile = iop.datadir + '/perfIntHyperUnOccult.pkl' tp.occulter_type = 'None' num_exp = 1 ap.exposure_time = 0.001 # 0.001 ap.numframes = int(num_exp * ap.exposure_time / cp.frame_time) tp.use_atmos = False tp.nwsamp = 1 tp.CPA_type = None#'Quasi'# None tp.NCPA_type = None#'Wave'# #None tp.aber_params = {'CPA': False, 'NCPA': False, 'QuasiStatic': False, # or 'Static' 'Phase': False, 'Amp': False, 'n_surfs': 2} # Yup this is 'if' is necessary hypercube = read.get_integ_hypercube() # PSF = hypercube[0,0] PSF = (read.take_exposure(hypercube))[0,0] if plot: quicklook_im(PSF) tp.__dict__ = tp_orig.__dict__ ap.__dict__ = ap_orig.__dict__ iop.__dict__ = iop_orig.__dict__ # # print tp.occulter_type return PSF
def quick_processing(simple_hypercube_1, simple_hypercube_2, plot=True): diff_cube = simple_hypercube_1 - simple_hypercube_2 if plot: quicklook_im(np.mean(simple_hypercube_1[:,-2], axis=0)) quicklook_im(np.mean(simple_hypercube_2[:,-2], axis=0)) quicklook_im(np.mean(diff_cube[:,-2], axis=0)) Lmaps = np.zeros((diff_cube.shape[1], diff_cube.shape[2], diff_cube.shape[3])) rmaps = np.zeros_like(Lmaps) for iw in range(diff_cube.shape[1]): dprint((diff_cube.shape, iw)) LCcube = np.transpose(diff_cube[:, iw:iw + 1], (2, 3, 0, 1)) rmaps[iw] = Analysis.stats.get_skew(LCcube)#, xinspect = range(233,236), yinspect = range(233,236), inspect = True)#, xinspect = range(40,50), yinspect = range(40,50), inspect = True) # quicklook_im(rmaps[iw], logAmp=True) Lmaps[iw] = Analysis.stats.get_Dmap(LCcube, binning=10, plot=False, threshold=0.01) if plot: loop_frames(rmaps) loop_frames(Lmaps) SDI = Analysis.phot.do_SDI(rmaps*Lmaps) if plot: quicklook_im(SDI) return [np.mean(simple_hypercube_1[:,0], axis=0), np.mean(diff_cube[:,0], axis=0), Lmaps[0], SDI]
diff_cube = simple_hypercube_1[2:] - simple_hypercube_2[2:] # loop_frames(diff_cube[:,0], logAmp=False) # loop_frames(diff_cube[0,:], logAmp=False) # quicklook_im(np.mean(diff_cube[:,0],axis=0), logAmp=False) # quicklook_im(np.mean(diff_cube[:, 0], axis=0), logAmp=True) # quicklook_im(np.median(diff_cube[:, 0], axis=0), logAmp=True) # LCcube = np.transpose(diff_cube, (2, 3, 0, 1)) algo_dict = {'thresh': 0} Dmap = Analysis.stats.get_Dmap(LCcube, algo_dict['thresh'], binning=499, plot=True) quicklook_im(Dmap, annos=['MKIDs'], title=r' $I_L / I^{*}$', mark_star=True) # indep_images([np.mean(diff_cube[:, 0], axis=0) / star_phot, Dmap / star_phot], logAmp=True, # titles=[r' $I / I^{*}$', r' $I_L / I^{*}$'], annos=['Mean', 'MKIDs']) # # wsamples = np.linspace(tp.band[0], tp.band[1], tp.nwsamp) # # scale_list = tp.band[0] / wsamples # scale_list = wsamples / tp.band[0] # # angle_list = np.zeros((tp.nwsamp)) # print np.mean(diff_cube, axis=0).shape # static_psf = pca.pca(np.mean(diff_cube, axis=0), angle_list=angle_list, scale_list=scale_list, # mask_center_px=None, full_output=True) # # quicklook_im(pca.pca(np.mean(diff_cube,axis=0), angle_list=angle_list, scale_list=scale_list, # # mask_center_px=None))
if __name__ == '__main__': mkid = gpd.run()[0, :] compare_images(mkid[::2], vmax=200, logAmp=True, vmin=1, title=r'$I (cts)$', annos=[ 'MKIDs 800 nm', '940 nm', '1080 nm', '1220 nm', '1360 nm', '1500 nm' ]) quicklook_im(np.mean(mkid[5:-1], axis=0), anno='MEDIS J Band', vmax=400, axis=None, title=r'$I (cts)$', logAmp=True, label='e') fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(9, 3.8)) labels = ['Ideal', 'MKID'] # images = [ideal,mkid] titles = ['A.U.', 'cts'] vmaxs = [0.01, 100] for m, ax in enumerate(axes): im = ax.imshow(images[m], interpolation='none', origin='lower', cmap="YlGnBu_r", vmax=vmaxs[m]) #norm= LogNorm(),
def run_system(empty_lamda, grid_size, PASSVALUE): #'dm_disp':0 passpara = PASSVALUE['params'] ap.__dict__ = passpara[0].__dict__ tp.__dict__ = passpara[1].__dict__ iop.__dict__ = passpara[2].__dict__ # params.ap = passpara[0] # params.tp = passpara[1] # # ap = params.ap # tp = params.tp # print 'line 23', tp.occulter_type # print 'propagating frame:', PASSVALUE['iter'] wsamples = np.linspace(tp.band[0], tp.band[1], tp.nwsamp) / 1e9 # print wsamples datacube = [] # print proper.prop_get_sampling(wfp), proper.prop_get_nyquistsampling(wfp), proper.prop_get_fratio(wfp) # global phase_map, Imaps # Imaps = np.zeros((4,tp.grid_size,tp.grid_size)) # phase_map = np.zeros((tp.grid_size, tp.grid_size)) # wavefronts = np.empty((len(wsamples),1+len(ap.contrast)), dtype=object) for iw, w in enumerate(wsamples): # Define the wavefront beam_ratio = tp.beam_ratio * tp.band[0] / w * 1e-9 wfp = proper.prop_begin(tp.diam, w, tp.grid_size, beam_ratio) wfs = [wfp] names = ['primary'] if ap.companion: for id in range(len(ap.contrast)): wfc = proper.prop_begin(tp.diam, w, tp.grid_size, beam_ratio) wfs.append(wfc) names.append('companion_%i' % id) # proper.prop_circular_aperture(wfo, tp.diam / 2) # for iw, wf in enumerate([wfo, wfc]): wframes = np.zeros((tp.grid_size, tp.grid_size)) for iwf, wf in zip(names, wfs): # wavefronts[iw,iwf] = wf proper.prop_circular_aperture(wf, tp.diam / 2) # quicklook_wf(wf, show=True) if tp.use_atmos: tdm.add_atmos(wf, tp.f_lens, w, atmos_map=PASSVALUE['atmos_map']) if tp.rot_rate: tdm.rotate_atmos(wf, PASSVALUE['atmos_map']) # quicklook_wf(wf, show=True) # if tp.use_spiders: # tdm.add_spiders(wf, tp.diam) if tp.use_hex: tdm.add_hex(wf) proper.prop_define_entrance(wf) # normalizes the intensity if iwf[:9] == 'companion': tdm.offset_companion(wf, int(iwf[10:]), PASSVALUE['atmos_map']) # quicklook_wf(wf, show=True) if tp.use_apod: tdm.do_apod(wf, tp.grid_size, tp.beam_ratio, tp.apod_gaus) # quicklook_wf(wf, show=True) # obj_map = tdm.wfs_measurement(wfo)#, obj_map, tp.wfs_scale) proper.prop_propagate(wf, tp.f_lens) if tp.aber_params['CPA']: tdm.add_aber(wf, tp.f_lens, tp.aber_params, tp.aber_vals, PASSVALUE['iter'], Loc='CPA') # if tp.CPA_type == 'test': # tdm.add_single_speck(wf, PASSVALUE['iter'] ) # if tp.CPA_type == 'Static': # tdm.add_static(wf, tp.f_lens, loc = 'CPA') # if tp.CPA_type == 'Amp': # tdm.add_static(wf, tp.f_lens, loc = 'CPA', type='Amp') # if tp.CPA_type == 'Quasi': # tdm.add_quasi(wf, tp.f_lens, PASSVALUE['iter']) # rawImageIO.save_wf(wf, iop.datadir+'/beforeAO.pkl') # quicklook_wf(wf) # quicklook_im(obj_map, logAmp=False) proper.prop_propagate(wf, tp.f_lens) if tp.quick_ao: if iwf == 'primary': # and PASSVALUE['iter'] == 0: # quicklook_wf(wf, show=True) r0 = float(PASSVALUE['atmos_map'][-10:-5]) # dprint((r0, 'r0')) CPA_map = tdm.quick_wfs(wf, PASSVALUE['iter'], r0=r0) # , obj_map, tp.wfs_scale) # dprint('quick_ao') # quicklook_wf(wf, show=True) if tp.use_ao: tdm.quick_ao(wf, iwf, tp.f_lens, beam_ratio, PASSVALUE['iter'], CPA_map) # dprint('quick_ao') # quicklook_wf(wf, show=True) else: if tp.use_ao: tdm.adaptive_optics(wf, iwf, iw, tp.f_lens, beam_ratio, PASSVALUE['iter']) if iwf == 'primary': # and PASSVALUE['iter'] == 0: # quicklook_wf(wf, show=True) r0 = float(PASSVALUE['atmos_map'][-10:-5]) # dprint((r0, 'r0')) # if iw == np.ceil(tp.nwsamp/2): tdm.wfs_measurement(wf, PASSVALUE['iter'], iw, r0=r0) #, obj_map, tp.wfs_scale) proper.prop_propagate(wf, tp.f_lens) # rawImageIO.save_wf(wf, iop.datadir+'/loopAO_8act.pkl') # if iwf == 'primary': # quicklook_wf(wf, show=True) # if tp.active_modulate: # tdm.modulate(wf, w, PASSVALUE['iter']) # if iwf == 'primary': # quicklook_wf(wf, show=True) if tp.aber_params['NCPA']: tdm.add_aber(wf, tp.f_lens, tp.aber_params, tp.aber_vals, PASSVALUE['iter'], Loc='NCPA') # if tp.NCPA_type == 'Static': # tdm.add_static(wf, tp.f_lens, loc = 'NCPA') # if tp.NCPA_type == 'Wave': # tdm.add_IFS_ab(wf, tp.f_lens, w) # if tp.NCPA_type == 'Quasi': # tdm.add_quasi(wf, tp.f_lens, PASSVALUE['iter']) # quicklook_wf(wf, show=True) # if iwf == 'primary': # NCPA_phasemap = proper.prop_get_phase(wf) # quicklook_im(NCPA_phasemap, logAmp=False, show=False, colormap="jet", vmin=-3.14, vmax=3.14) # if iwf == 'primary': # global obj_map # r0 = float(PASSVALUE['atmos_map'][-10:-5]) # obj_map = tdm.wfs_measurement(wf, r0 = r0)#, obj_map, tp.wfs_scale) # # quicklook_im(obj_map, logAmp=False) proper.prop_propagate(wf, tp.f_lens) # spiders are introduced here for now since the phase unwrapping seems to ignore them and hence so does the DM # Check out http://scikit-image.org/docs/dev/auto_examples/filters/plot_phase_unwrap.html for masking argument if tp.use_spiders: tdm.add_spiders(wf, tp.diam) tdm.prop_mid_optics(wf, tp.f_lens) # if iwf == 'primary': # if PASSVALUE['iter']>ap.numframes-2 or PASSVALUE['iter']==0: # quicklook_wf(wf, show=True) # print proper.prop_get_sampling(wfp), proper.prop_get_sampling_arcsec(wfp), 'here' if tp.satelite_speck and iwf == 'primary': tdm.add_speckles(wf) # tp.variable = proper.prop_get_phase(wfo)[20,20] # print 'speck phase', tp.variable # import cPickle as pickle # dprint('just saved') # with open(iop.phase_ideal, 'wb') as handle: # pickle.dump(proper.prop_get_phase(wf), handle, protocol=pickle.HIGHEST_PROTOCOL) # exit() if tp.active_null and iwf == 'primary': FPWFS.active_null(wf, PASSVALUE['iter'], w) # if tp.speckle_kill and iwf == 'primary': # tdm.speckle_killer(wf) # tdm.speck_kill(wf) # iwf == 'primary': # parent_bright = aper_phot(proper.prop_get_amplitude(wf),0,8) # if iwf == 'primary' and iop.saveIQ: # save_pix_IQ(wf) # complex_map = proper.prop_shift_center(wf.wfarr) # complex_pix = complex_map[64, 64] # print complex_pix # if np.real(complex_pix) < 0.2: # quicklook_IQ(wf) # # if iwf == 'primary': # # print np.sum(proper.prop_get_amplitude(wf)), 'before', aper_phot(proper.prop_get_amplitude(wf),0,4) # quicklook_wf(wf, show=True, logAmp=True) # if iwf == 'primary': # quicklook_wf(wf, show=True) # if tp.active_modulate and PASSVALUE['iter'] >=8: # coronagraph(wf, tp.f_lens, tp.occulter_type, tp.occult_loc, tp.diam) # if not tp.active_modulate: coronagraph(wf, tp.f_lens, tp.occulter_type, tp.occult_loc, tp.diam) # dprint(proper.prop_get_sampling_arcsec(wf)) # exit() # tp.occult_factor = aper_phot(proper.prop_get_amplitude(wf),0,8)/parent_bright # if PASSVALUE['iter'] % 10 == 0: # with open(iop.logfile, 'a') as the_file: # the_file.write('\n', tp.occult_factor) # quicklook_wf(wf, show=True) if tp.occulter_type != 'None' and iwf == 'primary': #kludge for now until more sophisticated coronapraph has been installed wf.wfarr *= 0.1 # # print np.sum(proper.prop_get_amplitude(wf)), 'after', aper_phot(proper.prop_get_amplitude(wf), 0, 4) # quicklook_wf(wf, show=True) # print proper.prop_get_sampling(wfp), proper.prop_get_sampling_arcsec(wfp), 'here' # if iwf == 'primary': # quicklook_wf(wf, show=True) if tp.use_zern_ab: tdm.add_zern_ab(wf, tp.f_lens) (wframe, sampling) = proper.prop_end(wf) # dprint((np.sum(wframe), 'sum')) # wframe = proper.prop_get_amplitude(wf) # planet = np.roll(np.roll(wframe, 20, 1), 20, 0) * 0.1 # [92,92] # if ap.companion: # from scipy.ndimage.interpolation import shift # companion = shift(wframe, shift= np.array(ap.comp_loc[::-1])- np.array([tp.grid_size/2,tp.grid_size/2])) * ap.contrast # # planet = np.roll(wframe, 15, 0) * 0.1 # [92,92] # # wframe = (wframe + companion) # quicklook_im(wframe, logAmp=True) # '''test conserve=True on prop_magnify!''' # wframe = proper.prop_magnify(wframe, (w*1e9)/tp.band[0]) # wframe = tdm.scale_wframe(wframe, w, iwf) # print np.shape(wframe) quicklook_im(wframe) # quicklook_im(wframe) # mid = int(len(wframe)/2) # wframe = wframe[mid - tp.grid_size/2 : mid +tp.grid_size/2, mid - tp.grid_size/2 : mid +tp.grid_size/2] # if max(mp.array_size) < tp.grid_size: # # Photons seeded outside the array cannot have pixel phase uncertainty applied to them. Instead make both grids match in size # wframe = rawImageIO.resize_image(wframe, newsize=(max(mp.array_size),max(mp.array_size))) # dprint(np.sum(wframe)) # dprint(iwf) # if iwf == 'companion_0': wframes += wframe # if sp.show_wframe: # quicklook_im(wframes, logAmp=True, show=True) datacube.append(wframes) datacube = np.array(datacube) datacube = np.abs(datacube) # #normalize # datacube = np.transpose(np.transpose(datacube) / np.sum(datacube, axis=(1, 2)))/float(tp.nwsamp) # print 'Some pixels have negative values, possibly because of some Gaussian uncertainy you introduced. Taking abs for now.' # view_datacube(datacube) # # End # print type(wfo[0,0]), type(wfo) # # proper.prop_savestate(wfo) # # else: # # wfo = proper.prop_state(wfo) return (datacube, sampling)
def plot_LC_map(xlocs, ylocs, LCmapFile, inspect=False): with open(LCmapFile, 'rb') as handle: LCmap = pickle.load(handle) # print np.shape(LCmap) LCmap = LCmap[:, :, :] # plt.plot(LCmap[60,40]) # plt.figure() # LCmap = temp.downsample(LCmap, factor=10) # plt.plot(LCmap[60,40]) # plt.show() print np.shape(LCmap) # xinspect = range(35,45) # yinspect = range(85,95) total_map = np.sum(LCmap, axis=2) median_map = np.median(LCmap, axis=2) interval_map = np.sum(LCmap[:, :, :100], axis=2) # if inspect: # plt.imshow(median_map[yinspect[0]:yinspect[-1],xinspect[0]:xinspect[-1]]) # plt.show() quicklook_im(total_map, logAmp=True, show=False) quicklook_im(median_map, logAmp=True, show=False) quicklook_im(interval_map, logAmp=True, show=False) if os.path.isfile(IratioFile): with open(IratioFile, 'rb') as handle: Ic, Is, Iratio, mIratio = pickle.load(handle) print np.shape(Iratio) else: Iratio = np.zeros((len(xlocs), len(ylocs))) Ic = np.zeros((len(xlocs), len(ylocs))) Is = np.zeros((len(xlocs), len(ylocs))) mIratio = np.zeros((len(xlocs), len(ylocs))) for ix, xloc in enumerate(xlocs): for iy, yloc in enumerate(ylocs): if (ix * len(ylocs) + iy) % 100 == 0: misc.progressBar(value=(ix * len(ylocs) + iy), endvalue=len(xlocs) * len(ylocs)) ints = LCmap[ix, iy] ID = pipe.get_intensity_dist(ints) bincent = (ID['binsS'] + np.roll(ID['binsS'], 1)) / 2. bincent = np.array(bincent)[1:] # popt, _ = curve_fit(gaussian, ID['binsS'][:-1], ID['histS']) # plt.plot(ID['binsS'][:-1], gaussian(ID['binsS'][:-1], *popt), 'r--') # popt, _ = curve_fit(poisson, ID['binsS'][:-1], ID['histS']) # plt.plot(ID['binsS'][:-1], poisson(ID['binsS'][:-1], *popt), 'g--') # bincent = np.linspace(0,100000,len(bincent)) # gauss = gaussian2(bincent, 1000,np.mean(ints)) + 0.0001 * np.random.normal(size=bincent.size) # popt, _ = curve_fit(gaussian2, bincent, gauss, p0=[100,100]) # print popt # plt.plot(bincent, gauss) # plt.plot(bincent, gaussian2(bincent, *popt), 'g--') # print sum(gauss) # print np.mean(ints) guessIc = np.mean(ints) * 0.7 guessIs = np.mean(ints) * 0.3 # popt, _ = curve_fit(MR, bincent, gauss, p0=[guessIc,guessIs]) # print popt # plt.plot(bincent, MR(bincent, *popt), 'g--') # print sum(MR(bincent, *popt)) # popt, _ = curve_fit(func, bincent, ID['histS']) # plt.plot(bincent, func(bincent, *popt), 'r--') try: popt, _ = curve_fit(MR, bincent, ID['histS'], p0=[guessIc, guessIs]) Ic[ix, iy] = popt[0] Is[ix, iy] = popt[1] Iratio[ix, iy] = popt[0] / popt[1] m = (np.sum(ints) - (popt[0] + popt[0])) / ( np.sqrt(popt[1]**2 + 2 * popt[0] + popt[1]) * len(ints)) mIratio[ix, iy] = m**-1 * (Iratio[ix, iy]) except RuntimeError: pass # print np.shape(ints) # EI = np.sum(ints) # # print EI # EI2 = EI**2 # # print EI2 # var = np.var(ints) # # print var # Is[ix,iy] = EI-np.sqrt(EI2-var) # # print Is[ix,iy] # Ic[ix,iy] = EI-Is[ix,iy] # # print Ic[ix,iy] # Iratio[ix,iy] = Ic[ix,iy]/Is[ix,iy] # exit() if inspect == True: # and xloc in yinspect and yloc in xinspect: plt.figure() plt.plot(ints) print xloc, yloc plt.figure() plt.step(bincent, ID['histS']) plt.plot(bincent, MR(bincent, *popt), 'b--') print popt, popt[0] / popt[1] plt.show() with open(IratioFile, 'wb') as handle: pickle.dump([Ic, Is, Iratio, mIratio], handle, protocol=pickle.HIGHEST_PROTOCOL) quicklook_im(Ic, logAmp=True, show=False) #, vmax=25)# quicklook_im(Is, logAmp=True, show=False) #,vmax=5,)# quicklook_im(Iratio, logAmp=True, show=False) #,vmax=25,)# quicklook_im(mIratio, logAmp=True, show=False) #, vmax=5,)# quicklook_im(mIratio * Iratio, logAmp=True, show=False) #, vmax=500,)# plt.show() return total_map, median_map, interval_map, Iratio, mIratio
iop.hyperFile = iop.datadir + 'HR8799_MKIDs400sstar_realPs1w.pkl' # 5 # iop.hyperFile = iop.datadir + 'noWnoRollHyperWcomp1000cont_Aug_1stMKIDs2.pkl'#5 simple_hypercube_1 = read.get_integ_hypercube(plot=False) #/ap.numframes # loop_frames(simple_hypercube_1[:,0], logAmp=True) # # loop_frames(simple_hypercube_1[0,:], logAmp=True) # quicklook_im(np.sum(simple_hypercube_1[:100,0],axis=0), logAmp=True) ap.startframe = ap.numframes #+3010 ap.companion = False iop.hyperFile = iop.datadir + 'HR8799_2_MKIDs400sref_realPs1w.pkl' # 5 # iop.hyperFile = iop.datadir + 'HR8799_2_MKIDs5.pkl' # 5 # # iop.hyperFile = iop.datadir + 'noWnoRollHyperWcomp1000cont_Aug_2ndMKIDs2.pkl'#5 simple_hypercube_2 = read.get_integ_hypercube(plot=False) #/ap.numframes # diff_cube = simple_hypercube_1 - simple_hypercube_2 quicklook_im(np.sum(diff_cube[:, 0], axis=0), logAmp=True) # loop_frames(simple_hypercube_2[:,0], logAmp=True) # quicklook_im(np.sum(simple_hypercube_2[:100,0],axis=0), logAmp=True) # loop_frames(simple_hypercube_2[:,0], logAmp=True)q # diff_cube = simple_hypercube_1#-simple_hypercube_2 Analysis.stats.Dmap_quad(simple_hypercube_1, simple_hypercube_2, binning=2) # factor = 1000 # diff_cube = np.zeros(((simple_hypercube_1.shape[0]/factor)**2,1,mp.array_size[0],mp.array_size[1])) # k = 0 # for a in simple_hypercube_1[::factor,0]: # for b in simple_hypercube_2[::factor,0]: # # quicklook_im(a) # # quicklook_im(b) # diff_cube[k] = a - b # print k
# # SSD_maps[:2] /= star_phot # # SSD_maps[2] /= SSD_starphot # SSD_maps #/= star_phot #vmins = [2e-11, 2e-8, 1e-12], vmaxs = [5e-7, 1.5e-7, 1e-6] indep_images( SSD_maps, logAmp=True, titles=[r' $I_C / I^{*}$', r' $I_S / I^{*}$', r' $I_r / I^{*}$'], annos=['Deterministic', 'Random', 'Beam Ratio']) print 'here' # quicklook_im(bunch_hypercube[0,0]) ap.exposure_time = 1 stacked = read.take_exposure(bunch_hypercube) quicklook_im(stacked[0, 0]) from statsmodels.tsa.stattools import acovf, acf, ccf mask = Analysis.phot.aperture(64, 64, 1) aper_idx = np.where(mask) print aper_idx # quicklook_im(mask) star_corr = 0 # for x, y in np.transpose(aper_idx): # # for y in aper_idx[0]: # print x, y # corr, ljb, pvalue = acf(bunch_hypercube[:, 0, x, y], unbiased=False, qstat=True, nlags=len(range(5000))) # # plt.plot(range(4999), corr[:-1]) # # plt.show()
# Amplitude tp.detector = 'ideal'#'MKIDs' # tp.use_ao = True tp.use_atmos = False tp.NCPA_type = None#'Static' tp.CPA_type = 'Amp' # tp.CPA_type = 'test' # tp.CPA_type = None#'Static'#'test' ap.lods = [[2, 0]] # initial location (no rotation) tp.active_null = True tp.speckle_kill = True iop.hyperFile = iop.datadir + '/amp_abs2.pkl' # tp.active_modulate = True perfect_hypercube = read.get_integ_hypercube(plot=False) # /ap.numframes quicklook_im(np.sum(perfect_hypercube[:, 0], axis=0), axis=None, title=r' $I / I^{*}$', anno='Integration') loop_frames(perfect_hypercube[::20, 0]) # compare_images([perfect_hypercube[0,0],perfect_hypercube[10,0],perfect_hypercube[100,0],perfect_hypercube[1000,0]], logAmp=True) annos = ['Frame: 0', 'Frame: 20', 'Frame: 500', 'Frame: 2000'] compare_images( [perfect_hypercube[0, 0], perfect_hypercube[20, 0], perfect_hypercube[500, 0], perfect_hypercube[-1, 0]], logAmp=True, title='A.U', annos= annos) # Perfect tp.detector = 'ideal'#'MKIDs' # tp.use_ao = False tp.use_atmos = False tp.NCPA_type = None#'Static' tp.CPA_type = None#'Static' tp.active_null = False iop.hyperFile = iop.datadir + '/perfect.pkl'
# plt.show() # # method_out = eval_method(simple_hypercube[:,0], Analysis.stats.DSI_4_VIP,angle_list, algo_dict) # plotdata.append(method_out[0]) # maps.append(method_out[1]) # # # quicklook_im(method_out[1], axis=None, title=r' $I_r / I^{*}$', anno='DSI') conv_steps = 110 # DSI iop.hyperFile = iop.datadir + '/modulate.pkl' # tp.active_modulate = True simple_hypercube = read.get_integ_hypercube(plot=False)#/ap.numframes print 'star_phot', star_phot quicklook_im(np.sum(simple_hypercube[conv_steps:,0], axis=0)/star_phot, axis=None, title=r' $I / I^{*}$', anno='Integration') angle_list = np.zeros((len(simple_hypercube)-conv_steps)) # algo_dict = {'DSI_starphot': DSI_starphot, 'thresh': 1e-5} algo_dict = {'thresh': 1e-5} loop_frames(simple_hypercube[:,0]) plt.figure() # plt.plot(simple_hypercube[:,0,53,53]) # plt.plot(simple_hypercube[:, 0, 54, 54]) # plt.plot(simple_hypercube[:, 0, 53, 54]) # plt.plot(simple_hypercube[:, 0, 54, 53]) # plt.plot(simple_hypercube[:, 0, 74, 63]) # plt.plot(simple_hypercube[:, 0, 75, 63]) # plt.plot(simple_hypercube[:, 0, 74, 64]) # plt.plot(simple_hypercube[:, 0, 75, 64]) # plt.plot(simple_hypercube[:, 0, 74, 55])
def speck_killing_loop(wfo): #configfilename = 'speckle_null_config.ini' #config = ConfigObj(configfilename) configfilename = tp.FPWFSdir + 'speckle_null_config_Rupe.ini' hardwareconfigfile = tp.FPWFSdir + 'speckle_instruments.ini' configspecfile = tp.FPWFSdir + 'speckle_null_config.spec' print(configfilename) config = ConfigObj(configfilename, configspec=configspecfile) val = Validator() check = config.validate(val) #pharo = hardware.PHARO_COM('PHARO', # configfile = hardwareconfigfile) #p3k = hardware.P3K_COM('P3K_COM', configfile = hardwareconfigfile) camera = hardware.camera() ao = hardware.ao() apmask = False if not apmask: aperturemask = np.ones((66, 66)) if apmask: aperturemask = dm.annularmask(66, 12, 33) # nulled_field = None im_params = config['IM_PARAMS'] null_params = config['NULLING'] abc = config['INTENSITY_CAL']['abc'] use_centoffs = config['NULLING']['cent_off'] #bgds = flh.setup_bgd_dict(config) fake_bgds = { 'bkgd': np.zeros((tp.grid_size, tp.grid_size)), 'masterflat': np.ones((tp.grid_size, tp.grid_size)), 'badpix': np.zeros((tp.grid_size, tp.grid_size)) } print("WARNING: USING FAKE BGDS") bgds = fake_bgds.copy() # controlregion = pf.open(tp.FPWFSdir+config['CONTROLREGION']['filename'])[0].data # controlregion = controlregion[512-int(tp.grid_size/2):512+int(tp.grid_size/2), 512-int(tp.grid_size/2):512+int(tp.grid_size/2)] # controlregion = np.roll(controlregion, 15) # controlregion[:,0:70] = 0 # controlregion[:80] = 0 # controlregion[-80:] = 0 controlregion = np.zeros((tp.grid_size, tp.grid_size)) controlregion[50:80, 35:50] = 1 boarder = get_ctrlrgnBoarder(controlregion) quicklook_im(controlregion, logAmp=False) # plt.show(block=True) #Notes==>scale exptime in snr exp = config['INTENSITY_CAL']['exptime'] #Setup # initial_flatmap = ao.grab_current_flatmap() # initial_centoffs= ao.grab_current_centoffs() defaultim = np.ones((tp.grid_size, tp.grid_size)) vertsx = config['CONTROLREGION']['verticesx'] vertsy = config['CONTROLREGION']['verticesy'] # plt.ion() fig = plt.figure(figsize=(12, 12)) ax1 = plt.subplot2grid((4, 4), (0, 0), rowspan=2, colspan=2) ax2 = plt.subplot2grid((4, 4), (0, 2), rowspan=2, colspan=2) ax3 = plt.subplot2grid((4, 4), (3, 0)) ax4 = plt.subplot2grid((4, 4), (2, 2), rowspan=2, colspan=2) #ax5 = plt.subplot2grid((4,4),(3,2)) # ax1b =plt.subplot2grid((4,4),(0, 0), rowspan =2, colspan = 2) title = fig.suptitle('Speckle destruction') ax1.set_title('Image') ax2.set_title('Control region') ax3.set_title('RMS in region') ax4.set_title('Image') #ax5.set_title('Intensity') w1 = ax1.imshow(np.log10(np.abs(defaultim)) + boarder, origin='lower', interpolation='nearest') current_cmap = cm.get_cmap() current_cmap.set_bad(color='white') # w1b = ax1b.imshow(boarder*10, origin='lower', interpolation = 'none') # ax1.set_xlim(min(vertsx), max(vertsx)) # ax1.set_ylim(min(vertsy), max(vertsy)) #ax1.set_ylim(int(im_params['centery'])-25, int(im_params['centery']+50)) w2 = ax2.imshow(np.log(np.abs(controlregion * defaultim)), origin='lower', interpolation='nearest') # ax2.set_xlim(min(vertsx), max(vertsx)) # ax2.set_ylim(min(vertsy), max(vertsy)) w3 = ax3.plot([], []) ax3.set_xlim(0, 10) w4, = ax4.plot(np.abs(defaultim[64])) # w4 = ax4.imshow(np.log10(np.abs(defaultim))+boarder, origin='lower', interpolation = 'nearest') current_cmap = cm.get_cmap() current_cmap.set_bad(color='white') N_iterations = 10 itcounter = [] maxfluxes = [] meanfluxes = [] totalfluxes = [] rmsfluxes = [] #w5 = ax5.plot(np.arange(10)*0, np.arange(10)*0) # w4 = ax4.imshow(np.log(camera.take_src_return_imagedata(exptime =exp)[242:758, 242:758]), origin='lower', interpolation = 'nearest') plt.show() tstamp = time.strftime("%Y%m%d-%H%M%S").replace(' ', '_') cubeoutputdir = os.path.join(null_params['outputdir'], tstamp) if not os.path.exists(cubeoutputdir): os.makedirs(cubeoutputdir) print(configfilename) result_imagecube = output_imagecube(N_iterations, tp.grid_size, filepath=os.path.join( cubeoutputdir, 'test_' + tstamp + '.fits'), comment='fun', configfile=configfilename) clean_imagecube = output_imagecube(N_iterations, tp.grid_size, filepath=os.path.join( cubeoutputdir, 'test_clean_' + tstamp + '.fits'), comment='fun', configfile=configfilename) cal_imagecube = output_imagecube(4, tp.grid_size, filepath=os.path.join( cubeoutputdir, 'test_cals_' + tstamp + '.fits'), comment='fun', configfile=configfilename) cal_imagecube.update(controlregion) cal_imagecube.update(bgds['bkgd']) cal_imagecube.update(bgds['masterflat']) cal_imagecube.update(bgds['badpix']) ####MAIN LOOP STARTS HERE##### print("BEGINNING NULLING LOOP") for iteration in range(N_iterations): # quicklook_wf(wfo) itcounter.append(iteration) if use_centoffs == False: current_flatmap = ao.grab_current_flatmap() if use_centoffs == True: current_centoffs = ao.grab_current_centoffs() # quicklook_im(current_flatmap, logAmp=False, show=True) print("Taking image of speckle field") # if nulled_field != None: # raw_im = nulled_field # else: raw_im = camera.take_src_return_imagedata(wfo, exptime=exp) result_imagecube.update(raw_im) field_im = pre.equalize_image(raw_im, **bgds) clean_imagecube.update(field_im) field_ctrl = field_im * controlregion # print np.shape(field_im), np.shape(controlregion), np.shape(field_ctrl) # plt.figure() # plt.imshow(field_im) # plt.figure() # plt.imshow(controlregion) # plt.figure() # plt.imshow(field_ctrl) # plt.show(block=True) meanfluxes.append(np.mean(field_ctrl[field_ctrl > 0])) maxfluxes.append(np.max(field_ctrl[field_ctrl > 0])) totalfluxes.append(np.sum(field_ctrl)) rmsfluxes.append( np.std(field_ctrl[field_ctrl > 0]) / config['NULLING']['referenceval']) ax3.plot(itcounter, rmsfluxes) #w5 = plt.plot(itcounter, maxfluxes) #w5 = plt.plot(itcounter, totalfluxes) ax1.set_title('Iteration: ' + str(iteration) + ', Mean: ' + str(meanfluxes[iteration]) + ', Max: ' + str(maxfluxes[iteration])) w1.set_data(np.log(np.abs(field_ctrl)) + boarder) w1.autoscale() plt.draw() plt.pause(0.02) if iteration > 0: try: printstats(field_im, speckleslist) except: pass flh.writeout(current_flatmap, 'latestiteration.fits') # ans = raw_input('Do you want to run a speckle nulling iteration[Y/N]?') ans = 'Y' if ans == 'N': flatmapans = eval( input('Do you want to reload the' + 'flatmap/centoffs you started with[Y/N]?')) if flatmapans == 'Y': print("Reloading initial flatmap/centoffs") if use_centoffs == False: status = ao.load_new_flatmap((initial_flatmap)) if use_centoffs == True: status = ao.load_new_centoffs((initial_centoffs)) #ao.load_new_flatmap(initial_flatmap) break print(('Iteration ' + str(iteration) + ' total_intensity: ' + str(np.sum(field_ctrl)))) #return a list of points print("computing interesting bright spots") #note indices and coordinates are reversed ijofinterest = identify_bright_points(field_ctrl, controlregion) xyofinterest = [p[::-1] for p in ijofinterest] print(("computed ", str(len(xyofinterest)), " bright spots")) max_specks = config['DETECTION']['max_speckles'] #if len(xyofinterest)>50: # xyofinterest = xyofinterest[0:49] if len(xyofinterest) < max_specks: max_specks = len(xyofinterest) fps = filterpoints(xyofinterest, max=max_specks, rad=config['NULLING']['exclusionzone']) print(fps) print("creating speckle objects") speckleslist = [speckle(field_im, xy[0], xy[1], config) for xy in fps] speckleslist = [x for x in speckleslist if (x.intensity) > 0] #old way--filter the speckles #speckleslist =[speckle(field_im, xy[0], xy[1], config) for xy in xyofinterest] #speckleslist = filterspeckles(speckleslist, max = max_specks) phases = null_params['phases'] # phases = [-np.pi/2.,0,np.pi/2.,np.pi] for idx, phase in enumerate(phases): print(("Phase ", phase)) phaseflat = 0 allspeck_aps = 0 #put in sine waves at speckle locations for speck in speckleslist: #XXX # phaseflat= phaseflat+speck.generate_flatmap(phase) phaseflat = speck.generate_flatmap(phase) # quicklook_im(speck.generate_flatmap(phase), logAmp=False) allspeck_aps = allspeck_aps + speck.aperture print('here') ax2.set_title('Phase: ' + str(phase)) if idx == 0: # w1.set_data( (allspeck_aps*field_ctrl)-0.95*field_ctrl) w1.set_data( np.log(np.abs((allspeck_aps * field_im) - 0.95 * field_im)) + boarder) w1.autoscale() plt.draw() #w1.set_data(field_ctrl); w1.autoscale(); plt.draw() phaseflat = phaseflat * aperturemask # plt.figure() # plt.imshow(allspeck_aps) # plt.show() # ans = raw_input('press enter') wf_temp = copy.copy(wfo) if use_centoffs == False: status = ao.load_new_flatmap(current_flatmap + phaseflat, wf_temp) # if use_centoffs == True: # status = ao.load_new_centoffs(current_centoffs+ # fmf.convert_flatmap_centoffs(phaseflat)) tp.variable = proper.prop_get_phase(wf_temp)[20, 20] print(('speck phase', tp.variable, 'intensity', proper.prop_get_amplitude(wf_temp)[20, 20])) # plt.show(block=True) # quicklook_wf(wf_temp, show=True) phaseim = camera.take_src_return_imagedata(wf_temp, exptime=exp) # quicklook_im(phaseim, show=True) phaseim = pre.equalize_image(phaseim, **bgds) # quicklook_im(phaseim, show=True) w2.set_data(np.log(np.abs(phaseim * controlregion))) w2.autoscale() plt.draw() plt.pause(0.02) # w4.set_data(range(128), np.sum(np.eye(tp.grid_size)*proper.prop_get_amplitude(wf_temp),axis=1))#ax4.plot(range(128), proper.prop_get_amplitude(wf_temp)[20])#np.abs(field_im[20]))#+boarder) w4.set_data( list(range(128)), proper.prop_get_amplitude(wf_temp)[64] ) # ax4.plot(range(128), proper.prop_get_amplitude(wf_temp)[20])#np.abs(field_im[20]))#+boarder) ax4.set_xlim([0, 128]) ax4.set_ylim([0, 0.2]) print("recomputing intensities") for speck in speckleslist: phase_int = speck.recompute_intensity(phaseim) speck.phase_intensities[idx] = phase_int print((speck.phase_intensities)) time.sleep(3) # if use_centoffs == False: # ao.load_new_flatmap(current_flatmap, wf_temp) # # if use_centoffs == True: # # ao.load_new_centoffs(current_centoffs) if config['NULLING']['null_gain'] == False: defaultgain = config['NULLING']['default_flatmap_gain'] nullmap = generate_phase_nullmap(speckleslist, defaultgain, phases) nullmap = nullmap * aperturemask if use_centoffs == False: # ao.load_new_flatmap(current_flatmap + nullmap, wfo) ao.load_new_flatmap(nullmap, wfo) # FPWFS.quicklook_wf(wfo) # camera.take_src_return_imagedata(wfo, exptime=exp) # if use_centoffs == True: # ao.load_new_centoffs(current_centoffs+ fmf.convert_flatmap_centoffs(nullmap)) # ans = raw_input('press enter') if config['NULLING']['null_gain'] == True: ##NOW CALCULATE GAIN NULLS print("DETERMINING NULL GAINS") gains = config['NULLING']['amplitudegains'] for idx, gain in enumerate(gains): print("Checking optimal gains") nullmap = generate_phase_nullmap(speckleslist, gain, phases) nullmap = nullmap * aperturemask wf_temp = copy.copy(wfo) if use_centoffs == False: ao.load_new_flatmap(current_flatmap + nullmap, wf_temp) # if use_centoffs == True: # ao.load_new_centoffs(current_centoffs+ fmf.convert_flatmap_centoffs(nullmap)) ampim = camera.take_src_return_imagedata(wf_temp, exptime=exp) # quicklook_wf(wf_temp) ampim = pre.equalize_image(ampim, **bgds) w2.set_data(np.log(np.abs(ampim * controlregion))) ax2.set_title('Gain: ' + str(gain)) w2.autoscale() plt.draw() plt.pause(0.02) for speck in speckleslist: amp_int = speck.recompute_intensity(ampim) speck.gain_intensities[idx] = amp_int print((speck.gain_intensities)) w4.set_data( list(range(128)), proper.prop_get_amplitude(wf_temp)[64] ) #ax4.plot(range(128), proper.prop_get_amplitude(wf_temp)[20])#np.abs(field_im[20]))#+boarder) ax4.set_xlim([0, 128]) ax4.set_ylim([0, 0.2]) for speck in speckleslist: speck.compute_null_gain() supernullmap = generate_super_nullmap(speckleslist, phases) print( "Loading supernullmap now that optimal gains have been found!") supernullmap = supernullmap * aperturemask if use_centoffs == False: # ao.load_new_flatmap(current_flatmap + supernullmap, wfo) ao.load_new_flatmap(supernullmap, wfo) # FPWFS.quicklook_wf(wfo) # quicklook_im(supernullmap,logAmp=False, show=True) # camera.take_src_return_imagedata(wfo, exptime=exp) # if use_centoffs == True: # ao.load_new_centoffs(current_centoffs+ fmf.convert_flatmap_centoffs(supernullmap)) #ao.load_new_flatmap(supernullmap) # w3.set_data(nullmap) # plt.draw() # w4.set_data(np.log(np.abs(field_im))+boarder) # plt.draw() # w4.autoscale(); # quicklook_im(field_im, logAmp=False) quicklook_wf(wfo) plt.show(block=True) # ans = raw_input('press enter') # try: # check = raw_input("would you like to continue?: ") # except EOFError: # print ("Error: EOF or empty input!") # check = "" # print check plt.show(block=False)
def simple_telescope(wavelength, gridsize): # Define entrance aperture diameter and other quantities d_objective = 5.0 # objective diameter in meters fl_objective = 20.0 * d_objective # objective focal length in meters fl_eyepiece = 0.021 # eyepiece focal length fl_eye = 0.022 # human eye focal length beam_ratio = 0.3 # initial beam width/grid width # Define the wavefront wfo = proper.prop_begin(d_objective, wavelength, gridsize, beam_ratio) # print d_objective, wavelength, gridsize, beam_ratio # Define a circular aperture proper.prop_circular_aperture(wfo, d_objective / 2) # proper.prop_propagate(wfo, fl_objective) # proper.prop_propagate(wfo, fl_objective) # proper.prop_propagate(wfo, fl_objective) # plt.imshow(proper.prop_get_amplitude(wfo)) # plt.show() # Define entrance proper.prop_define_entrance(wfo) # plt.imshow(proper.prop_get_amplitude(wfo)) # plt.show() # proper.prop_propagate(wfo, fl_objective+fl_eyepiece, "eyepiece") # # plt.imshow(proper.prop_get_amplitude(wfo)) # # plt.show() # # proper.prop_propagate(wfo, fl_objective+fl_eyepiece, "eyepiece") # plt.imshow(proper.prop_get_amplitude(wfo)) # plt.show() # # proper.prop_propagate(wfo, fl_objective+fl_eyepiece, "eyepiece") # plt.imshow(proper.prop_get_amplitude(wfo)) # plt.show() # Define a lens proper.prop_lens(wfo, fl_objective, "objective") # plt.imshow(proper.prop_get_amplitude(wfo)) # plt.show() # Propagate the wavefront proper.prop_propagate(wfo, fl_objective + fl_eyepiece, "eyepiece") # plt.imshow(proper.prop_get_amplitude(wfo)) # plt.show() # Define another lens proper.prop_lens(wfo, fl_eyepiece, "eyepiece") # plt.imshow(proper.prop_get_amplitude(wfo)) # plt.show() exit_pupil_distance = fl_eyepiece / (1 - fl_eyepiece / (fl_objective + fl_eyepiece)) proper.prop_propagate(wfo, exit_pupil_distance, "exit pupil at eye lens") # quicklook_wf(wfo) # plt.imshow(proper.prop_get_amplitude(wfo)) # plt.show() proper.prop_lens(wfo, fl_eye, "eye") proper.prop_propagate(wfo, fl_eye, "retina") # plt.imshow(proper.prop_get_amplitude(wfo)) # plt.show() quicklook_wf(wfo) phase_map = proper.prop_get_phase(wfo) amp_map = proper.prop_get_amplitude(wfo) # quicklook_im(phase_map) amp_map[80:100, 80:100] = 0 quicklook_im(amp_map, logAmp=True) import numpy as np wfo.wfarr = proper.prop_shift_center(amp_map * np.cos(phase_map) + 1j * amp_map * np.sin(phase_map)) # quicklook_wf(wf_array[iw,0]) proper.prop_propagate(wfo, fl_eye, "retina") proper.prop_lens(wfo, fl_eye, "eye") quicklook_wf(wfo) # End (wfo, sampling) = proper.prop_end(wfo) return (wfo, sampling)
scale_list = tp.band[0] / wsamples print wsamples, tp.band, scale_list from Detector.analysis import make_SNR_plot, make_cont_plot from vip_hci import pca SDIs = [] for i in range(4): SDI = pca.pca(images[i], angle_list=np.zeros((len(images[i]))), scale_list=scale_list[::-1]**-1, mask_center_px=None) SDIs.append(SDI) SDIs = np.array(SDIs) all_curves = np.vstack((images[:, 4], SDIs)) for i in range(len(all_curves)): quicklook_im(all_curves[i], vmin=1) # make_cont_plot(SDIs, ['Stack', 'Med', 'Short', 'Ratio']) # make_cont_plot(images[:,0], ['Stack', 'Med', 'Short', 'Ratio']) make_cont_plot(all_curves, [ 'J-Stack', 'J-Median', 'J-Lucky', 'J-SSD', 'SDI-Stack', 'SDI-Median', 'SDI-Lucky', 'SDI-SSD' ]) # norms = [1,2000,20,1,1,2000,20,1] plt.show() # mp.obsfile = occultdirs[1] + '/' + 'r0varyObsfile_piston_colors.h5' # LCmapFile = os.path.join(mp.rootdir, mp.proc_dir, mp.date, occultdirs[1], 'LCvaryR0_piston_colors.pkl') # IratioFile = os.path.join(mp.rootdir, mp.proc_dir, mp.date, occultdirs[1], 'IrvaryR0_piston_colors.pkl') # # if not os.path.isfile(mp.datadir + mp.obsfile): # sp.num_processes = 20 # tp.occulter_type = None # gpd.run()
def plot_SSD(xlocs, ylocs, LCmapFile, inspect=False): with open(LCmapFile, 'rb') as handle: LCmap = pickle.load(handle) assert len(LCmap.shape) != 4 LCmap = LCmap[:, :, :] print np.shape(LCmap) # yinspect = range(35,45) # xinspect = range(85,95) # yinspect = range(62,68) # xinspect = range(62, 68) # yes # xinspect = range(95,102) # yinspect = range(26,36) # xinspect = range(20,26) # yinspect = range(102,110) # 108, 23 # xinspect = range(78, 82) # yinspect = range(66,70)# 66,80 # xinspect = range(20,28) # yinspect = range(38, 44) # 42, 23 total_map = np.sum(LCmap, axis=2) median_map = np.median(LCmap, axis=2) interval_map = np.sum(LCmap[:, :, :5], axis=2) if inspect: plt.imshow(interval_map[yinspect[0]:yinspect[-1], xinspect[0]:xinspect[-1]]) plt.show() quicklook_im(total_map, logAmp=True, show=False) quicklook_im(median_map, logAmp=True, show=False) quicklook_im(interval_map, logAmp=True, show=True) if os.path.isfile(IratioFile): with open(IratioFile, 'rb') as handle: Ic, Is, Iratio, mIratio = pickle.load(handle) print np.shape(Iratio) else: Ic, Is, Iratio, mIratio = get_Iratio(LCmap, xlocs, ylocs, xinspect, yinspect, inspect) with open(IratioFile, 'wb') as handle: pickle.dump([Ic, Is, Iratio, mIratio], handle, protocol=pickle.HIGHEST_PROTOCOL) quicklook_im(np.abs(Ic) + 1e-3, logAmp=True, show=False) # , vmax=25)# quicklook_im(Is, logAmp=True, show=False) # ,vmax=5,)# quicklook_im(Iratio, logAmp=True, show=False) # ,vmax=25,)# quicklook_im(mIratio, logAmp=True, show=False) # , vmax=5,)# quicklook_im(mIratio * Iratio, logAmp=True, show=False) # , vmax=500,)# plt.show() return total_map, median_map, interval_map, Iratio, mIratio
fname = folder + '1491886320_flat.h5' with h5py.File(fname, 'r') as hf: flats = [] for i in range(10): image = np.array(hf.get('Images/%i' % (1491886320 + i))) flats.append(image) flats = np.array(flats) # loop_frames(flats, vmin=0, vmax=40) # loop_frames(flats - darks) flat = np.median(flats[4:], axis=0) - np.median(darks, axis=0) # quicklook_im(flat, vmin=0, vmax=40) dprint('flat') flat[flat <= 0] = 1 flat[68:82, 31:44] = 1 quicklook_im(flat) quicklook_im(flat, logAmp=True) fname = folder + '1491896000.h5' # ob = ObsFile(fname) # print ob.photonTable, type(ob.photonTable) # for row in ob.photonTable: # for phot in row: # print phot[:2] # exit() with h5py.File(fname, 'r') as hf: images = [] for i in range(195): image = np.array(hf.get('Images/%i' % (1491896000 + i))) images.append(image)
def get_Dmap(LCmap, threshold=1, binning=10, plot=False, verb_output=False): dprint(LCmap.shape) # mask = phot.aperture(tp.grid_size/2,tp.grid_size/2,9) # mask = mask==0 #flip the zeros and ones # add_rc = np.ones((mp.array_size[0],mp.array_size[1])) # add row col of ones to mask # add_rc[:-1,:-1] = mask # mask=add_rc # mask = np.transpose(np.resize(mask,(8,ap.numframes,129,129))) # LCmap *= mask Dmap = np.zeros((LCmap.shape[0],LCmap.shape[1])) nexps = LCmap.shape[2] # print nexps intervals = list(range(0,nexps+1,binning)) print(intervals) Dmaps = [] for iw in range(LCmap.shape[3]): for ie in range(nexps/binning): # print ie, # print intervals[ie], intervals[ie + 1] exp = np.mean(LCmap[:,:,intervals[ie]:intervals[ie+1],iw], axis=2) # quicklook_im(exp,logAmp=False) # for x in range(exp.shape[0]): # for y in range(exp.shape[1]): # exp[x,y] = np.sum(exp[x-1:x+2,y-1:y+2])/9 # quicklook_im(exp,logAmp=False) # darkmask = np.logical_or(exp <= 0.5, exp >=1.5) # darkmask = np.logical_or(exp <= 1, exp >= 2) # darkmask = np.logical_or(exp <= 1, exp >= 15) # darkmask = exp <= 1e-10#0#, np.abs(exp)>2)#5e-6 # darkmask = np.logical_and(exp <= threshold, exp >= 0) # 0#, np.abs(exp)>2)#5e-6 darkmask = exp <= threshold#, exp >= 0) # 0#, np.abs(exp)>2)#5e-6 # quicklook_im(exp,logAmp=False) # quicklook_im(darkmask,logAmp=False) Dmap += darkmask if plot and ie % 10 == 0: print(ie, end=' ') print(intervals[ie], intervals[ie + 1]) quicklook_im(exp, logAmp=True)#, vmin=0.1) quicklook_im(Dmap,logAmp=True, vmin=1) if ie == 0 and iw ==0: # quicklook_im(Dmap, logAmp=True, vmin=1) Dmaps.append(exp) Dmaps.append(copy.deepcopy(Dmap)) if ie == 10 and iw ==0: # quicklook_im(Dmap, logAmp=True, vmin=1) Dmaps.append(copy.deepcopy(Dmap)) if ie == nexps/binning - 1 and iw ==0: # quicklook_im(Dmap, logAmp=True, vmin=1) Dmaps.append(copy.deepcopy(Dmap)) print(len(Dmaps)) # quicklook_im(Dmap) # Dmap /= np.max(Dmap) # quicklook_im(Dmap) # quicklook_im(1-Dmap) finite = Dmap!=0 min_nonzero = np.min(Dmap[finite]) # plt.hist(Dmap.flatten(), bins=25) # plt.show() print(min_nonzero) Lmap = 1./(Dmap+min_nonzero/100) # plt.hist(Lmap.flatten(), bins=100) # plt.show() # Lmap[Lmap == 100] = np.min(Lmap) # Lmap = np.max(Dmap) - Dmap # loc_max = np.argmax(LCmap, axis=(0,1,2)) # loc_max = np.unravel_index(LCmap.argmax(), LCmap.shape) # print 'loc_max', loc_max # # loc_max = np.array(loc_max) # from vip_hci import phot, pca # rawscaler = phot.contrcurve.aperture_flux(LCmap[loc_max[0]], [loc_max[1]], [loc_max[2]], 8, 1)[0] # print rawscaler # loc_max = np.unravel_index(Lmap.argmax(), Lmap.shape) # procscaler = phot.contrcurve.aperture_flux(Lmap, [loc_max[0]], [loc_max[1]], 8, 1)[0] # scale = rawscaler/procscaler # print rawscaler, procscaler, scale, 'scale' # plt.hist(Lmap.reshape(Lmap.shape[0]**2), bins = 500) # plt.figure() # plt.hist(LCmap[:,:,0].reshape(Lmap.shape[0]**2), bins=500) # plt.figure() # plt.hist(LCmap) # LCmap_max = np.sum(np.argsort(-Lmap.reshape(128**2))[:10]) # Lmap_max = np.sum(np.argsort(-np.median(LCmap, axis=2).reshape(128**2))[:10]) # print Lmap.shape, np.median(LCmap, axis=2).shape # print np.argsort(-Lmap.reshape(128**2))[:10], np.argsort(-np.median(LCmap, axis=2).reshape(128**2))[:10] # scale = float(LCmap_max)/Lmap_max # LCmap_max = float(np.max(LCmap[:,:,0])) # Lmap_max = np.max(LCmap) stacked = np.mean(LCmap[:,:,:,0], axis=2) LCmap_max = stacked[64,57] Lmap_max = Lmap[64,57] scale = 1.#LCmap_max/ Lmap_max # scale = np.max(LCmap[:,:,:])/np.max(Lmap) print(LCmap_max, Lmap_max, scale, 'scale') # quicklook_im(Lmap) # quicklook_im(stacked) # Lmap *= scale # Lmap = np.max(Dmap)-Dmap # plt.hist(Lmap.reshape(Lmap.shape[0]**2), bins = 500) # plt.show() if plot: quicklook_im(Lmap, logAmp=True, vmin=0.1) if verb_output: return Dmaps, Lmap else: return Lmap
simple_hypercube = read.get_integ_hypercube(plot=False) #/ap.numframes wsamples = np.linspace(tp.band[0], tp.band[1], tp.nwsamp) # scale_list = tp.band[0] / wsamples scale_list = wsamples / tp.band[0] angle_list = np.zeros((tp.nwsamp)) print np.mean(simple_hypercube, axis=0).shape static_psf = pca.pca(np.mean(simple_hypercube, axis=0), angle_list=angle_list, scale_list=scale_list, mask_center_px=None, full_output=True) # quicklook_im(pca.pca(np.mean(simple_hypercube,axis=0), angle_list=angle_list, scale_list=scale_list[::-1], # mask_center_px=None)) quicklook_im(np.sum(simple_hypercube, axis=(0, 1))) loop_frames(np.sum(simple_hypercube, axis=0)) # scale_list = np.linspace(scale_list[-1],scale_list[0],8) scale_list = tp.band[1] / wsamples # scale_list = scale_list[::-1] print scale_list # loop_frames(static_psf[0], logAmp=False) # loop_frames(static_psf[1], logAmp=False) # loop_frames(static_psf[2], logAmp=False) # loop_frames(static_psf[3], logAmp=False) static_psf = static_psf[1][0] import scipy.ndimage dprint(star_phot) static_psf = scipy.ndimage.zoom(static_psf, float(simple_hypercube.shape[-1]) / static_psf.shape[-1],
psf_template=psf_template, fwhm=10, pxscale=0.13, starphot=star_phot, algo=pca.pca, debug=True) # from vip_hci import pca # loop_frames(hypercube[:,0]) ADI = pca.pca( hypercube[:, 0], angle_list=-1 * np.arange(0, num_exp * tp.rot_rate * cp.frame_time, tp.rot_rate * cp.frame_time) ) #, scale_list=np.ones((len(hypercube[:,0]))), mask_center_px=None) quicklook_im(ADI) ADI = ADI.reshape(1, 128, 128) wsamples = np.linspace(tp.band[0], tp.band[1], tp.nwsamp) scale_list = tp.band[0] / wsamples print scale_list, scale_list[-1], 'SL', 1. / scale_list[-1] wframe = clipped_zoom(hypercube[0, 0], 1. / scale_list[-1]) quicklook_im(wframe) wframe = wframe.reshape(1, 128, 128) cube = np.vstack((hypercube[0, [0, -1]], wframe, SDI)) # cube = np.dstack((hypercube[0,0],wframe,hypercube[0,-1],SDI)).transpose() annos = [ '$\lambda_0$=860 nm', '$\lambda_8=$1250 nm', '$\lambda_0$ Scaled', 'SDI Residual' ] print 'change titles to bottom left text in figure white'