print "%i UBLs to include"%len(ubls) A_filename = datadir + tag + '_%s%s_%i_%i.A'%(p, p, len(tlist)*len(ubls), 12*nside_beamweight**2) if os.path.isfile(A_filename) and not force_recompute: print "Reading A matrix from %s"%A_filename sys.stdout.flush() A[p] = np.fromfile(A_filename, dtype='complex64').reshape((len(ubls), len(tlist), 12*nside_beamweight**2))[:,tmask].reshape((len(ubls)*len(tlist[tmask]), 12*nside_beamweight**2)) else: beam_healpix = np.fromfile(datadir + 'mwa_curtin_beam_%s_nside%i_freq167.275_zenith_float32.dat'%(p, bnside), dtype='float32') #hpv.mollview(beam_healpix, title='beam %s'%p) #plt.show() vs = sv.Visibility_Simulator() vs.initial_zenith = np.array([0, lat_degree*np.pi/180])#self.zenithequ beam_heal_equ = np.array(sv.rotate_healpixmap(beam_healpix, 0, np.pi/2 - vs.initial_zenith[1], vs.initial_zenith[0])) print "Computing A matrix for %s pol..."%p sys.stdout.flush() timer = time.time() A[p] = np.empty((len(tlist)*len(ubls), 12*nside_beamweight**2), dtype='complex64') for i in range(12*nside_beamweight**2): dec, ra = hpf.pix2ang(nside_beamweight, i)#gives theta phi dec = np.pi/2 - dec print "\r%.1f%% completed, %f minutes left"%(100.*float(i)/(12.*nside_beamweight**2), (12.*nside_beamweight**2-i)/(i+1)*(float(time.time()-timer)/60.)), sys.stdout.flush() A[p][:, i] = np.array([vs.calculate_pointsource_visibility(ra, dec, d, freq, beam_heal_equ = beam_heal_equ, tlist = tlist) for d in ubls]).flatten() print "%f minutes used"%(float(time.time()-timer)/60.) sys.stdout.flush() A[p].tofile(A_filename)
Bl = nsideB * 3 - 1 beam_healpix = np.zeros(12 * nsideB**2, dtype='float32') if pol[0] != pol[-1]: raise Exception( 'ERROR: polarization string %s not supported in the code.' % pol) for i in range(12 * nsideB**2): beam_healpix[i] = aa[0].bm_response( sv.rotatez_matrix(-np.pi / 2).dot(hpf.pix2vec(nsideB, i)), pol[0] )[0] #in paper's bm_response convention, (x,y) = (0,1) points north. else: raise Exception('Cant tell if the iinfo is for PAPER or MITEoR') vs.initial_zenith = np.array([0, lat_degree * np.pi / 180]) #self.zenithequ beam_heal_equ = np.array( sv.rotate_healpixmap(beam_healpix, 0, np.pi / 2 - vs.initial_zenith[1], vs.initial_zenith[0])) timer = time.time() if all_ubl: ubls = infoubl else: ubls = [ ubl for ubl in infoubl if la.norm(ubl) < inclusion_thresh * (nside_target * 299.792458 / freq) ] print "%i UBLs to include" % len(ubls) if len(tlist) * len(ubls) < 12 * nside**2: for i in range(5): print 'Not enough degree of freedom! %i*%i<%i.' % ( len(tlist), len(ubls), 12 * nside**2)
vis_data[p] = np.fromfile(data_filename, dtype='complex64').reshape((nt, nUBL))[np.ix_(cal_time_mask, cal_ubl_mask)].transpose() ubls[p] = ubls[p][cal_ubl_mask] ubl_sort[p] = np.argsort(la.norm(ubls[p], axis=1)) print "%i UBLs to include"%len(ubls[p]) #beam beam_healpix={} beam_healpix['x'] = abs(local_beam(vis_freq)[0])**2 + abs(local_beam(vis_freq)[1])**2 beam_healpix['y'] = abs(local_beam(vis_freq)[2])**2 + abs(local_beam(vis_freq)[3])**2 ###simulate UNpolarized############## vs = sv.Visibility_Simulator() vs.initial_zenith = np.array([0, sa.lat]) # self.zenithequ beam_heal_equ_x = sv.rotate_healpixmap(beam_healpix['x'], 0, np.pi / 2 - vs.initial_zenith[1], vs.initial_zenith[0]) beam_heal_equ_y = sv.rotate_healpixmap(beam_healpix['y'], 0, np.pi / 2 - vs.initial_zenith[1], vs.initial_zenith[0]) print "Computing UNpolarized point sources matrix..." sys.stdout.flush() cal_sources = ['cyg', 'cas'] Apol = np.empty((np.sum(cal_ubl_mask), 2, np.sum(cal_time_mask), len(cal_sources)), dtype='complex64') timer = time.time() for n, source in enumerate(cal_sources): ra = southern_points[source]['body']._ra dec = southern_points[source]['body']._dec Apol[:, 0, :, n] = vs.calculate_pointsource_visibility(ra, dec, ubls[p], vis_freq, beam_heal_equ=beam_heal_equ_x, tlist=tlist[cal_time_mask]) Apol[:, 1, :, n] = vs.calculate_pointsource_visibility(ra, dec, ubls[p], vis_freq, beam_heal_equ=beam_heal_equ_y, tlist=tlist[cal_time_mask])
__author__ = 'omniscope' import numpy as np import simulate_visibilities.simulate_visibilities as sv import time bnside = 256 freqs = range(110, 200, 10) raw_beam_data = np.concatenate([np.fromfile('/home/omniscope/data/mwa_beam/healpix_%i_%s.bin' % (bnside, p), dtype='complex64').reshape( (len(freqs), 12 * bnside ** 2, 2)) for p in ['x', 'y']], axis=-1).transpose(0, 2, 1) #freq by 4 by pix sv.plot_jones(raw_beam_data[5]) vstest = sv.Visibility_Simulator() vstest.initial_zenith = np.array([0, np.pi/4]) tm = time.time() beam_heal_equ = np.array( [sv.rotate_healpixmap(beam_healpixi, 0, np.pi / 2 - vstest.initial_zenith[1], vstest.initial_zenith[0]) for beam_healpixi in raw_beam_data[5]]) print (time.time()-tm) / 60. sv.plot_jones(beam_heal_equ)
def get_A(): if os.path.isfile(A_path) and not force_recompute: print "Reading A matrix from %s" % A_path sys.stdout.flush() A = np.fromfile(A_path, dtype='complex64').reshape((len(used_common_ubls), 4, len(tlist), 4, valid_npix + 2 * len(used_common_ubls))) else: # beam beam_healpix = local_beam(freq) # hpv.mollview(beam_healpix, title='beam %s'%p) # plt.show() vs = sv.Visibility_Simulator() vs.initial_zenith = np.array([0, lat_degree * np.pi / 180]) # self.zenithequ beam_heal_equ = np.array( [sv.rotate_healpixmap(beam_healpixi, 0, np.pi / 2 - vs.initial_zenith[1], vs.initial_zenith[0]) for beam_healpixi in local_beam(freq)]) print "Computing A matrix..." sys.stdout.flush() A = np.empty((len(used_common_ubls), 4 * len(tlist), 4, valid_npix + 2 * len(used_common_ubls)), dtype='complex64') timer = time.time() for n, i in enumerate(np.arange(npix)[valid_pix_mask]): ra = phis[i] dec = np.pi / 2 - thetas[i] print "\r%.1f%% completed, %f minutes left" % ( 100. * float(n) / (valid_npix), float(valid_npix - n) / (n + 1) * (float(time.time() - timer) / 60.)), sys.stdout.flush() A[..., n] = vs.calculate_pol_pointsource_visibility(ra, dec, used_common_ubls, freq, beam_heal_equ=beam_heal_equ, tlist=tlist).dot( [[.5, .5, 0, 0], [0, 0, .5, .5j], [0, 0, .5, -.5j], [.5, -.5, 0, 0]]) for i in range(len(used_common_ubls)): for p in range(4): A[i, p * len(tlist): (p + 1) * len(tlist), p, valid_npix + 2 * i] = 1. A[i, p * len(tlist): (p + 1) * len(tlist), p, valid_npix + 2 * i + 1] = 1.j print "%f minutes used" % (float(time.time() - timer) / 60.) sys.stdout.flush() A.tofile(A_path) A.shape = (len(used_common_ubls), 4, len(tlist), 4, valid_npix + 2 * len(used_common_ubls)) tmask = np.ones_like(tlist).astype(bool) for p in ['x', 'y']: # tf mask file, 0 means flagged bad data try: tfm_filename = datadir + tag + '_%s%s_%i_%i.tfm' % (p, p, nt, nf) tfmlist = np.fromfile(tfm_filename, dtype='float32').reshape((nt, nf)) tmask = tmask & np.array(tfmlist[:, 0].astype('bool')) # print tmask except: print "No mask file found" # print freq, tlist # Merge A A.shape = (len(used_common_ubls) * 4 * len(tlist[tmask]), 4 * A.shape[-1]) try: return np.concatenate((np.real(A), np.imag(A))) except MemoryError: print "Not enough memory, concatenating A on disk ", A_path + 'tmpre', A_path + 'tmpim', sys.stdout.flush() Ashape = list(A.shape) Ashape[0] = Ashape[0] * 2 np.real(A).tofile(A_path + 'tmpre') np.imag(A).tofile(A_path + 'tmpim') del (A) os.system("cat %s >> %s" % (A_path + 'tmpim', A_path + 'tmpre')) os.system("rm %s" % (A_path + 'tmpim')) A = np.fromfile(A_path + 'tmpre', dtype='float32').reshape(Ashape) os.system("rm %s" % (A_path + 'tmpre')) print "done." sys.stdout.flush() return A
sys.stdout.flush() A[p] = np.fromfile(A_path, dtype='complex64').reshape((len(ubls), len(tlist), 12 * nside_beamweight ** 2))[ :, tmask].reshape((len(ubls) * len(tlist[tmask]), 12 * nside_beamweight ** 2)) else: # beam if p == 'x': beam_healpix = abs(local_beam(freq)[0]) ** 2 + abs(local_beam(freq)[1]) ** 2 elif p == 'y': beam_healpix = abs(local_beam(freq)[2]) ** 2 + abs(local_beam(freq)[3]) ** 2 # hpv.mollview(beam_healpix, title='beam %s'%p) # plt.show() vs = sv.Visibility_Simulator() vs.initial_zenith = np.array([0, lat_degree * np.pi / 180]) # self.zenithequ beam_heal_equ = np.array( sv.rotate_healpixmap(beam_healpix, 0, np.pi / 2 - vs.initial_zenith[1], vs.initial_zenith[0])) print "Computing A matrix for %s pol..." % p sys.stdout.flush() timer = time.time() A[p] = np.empty((len(tlist) * len(ubls), 12 * nside_beamweight ** 2), dtype='complex64') for i in range(12 * nside_beamweight ** 2): dec, ra = hpf.pix2ang(nside_beamweight, i) # gives theta phi dec = np.pi / 2 - dec print "\r%.1f%% completed, %f minutes left" % (100. * float(i) / (12. * nside_beamweight ** 2), (12. * nside_beamweight ** 2 - i) / (i + 1) * ( float(time.time() - timer) / 60.)), sys.stdout.flush() A[p][:, i] = np.array( [vs.calculate_pointsource_visibility(ra, dec, d, freq, beam_heal_equ=beam_heal_equ, tlist=tlist) for d in ubls]).flatten()
for i, u in enumerate(used_common_ubls): if u in ubls[p]: ubl_index[p][i] = np.argmin(la.norm(ubls[p] - u, axis=-1)) + 1 elif -u in ubls[p]: ubl_index[p][i] = - np.argmin(la.norm(ubls[p] + u, axis=-1)) - 1 else: raise Exception('Logical Error') print '>>>>>>Used nUBL = %i, nt = %i.'%(nUBL_used, nt_used) ####set up vs and beam vs = sv.Visibility_Simulator() vs.initial_zenith = np.array([0, lat_degree * np.pi / 180]) # self.zenithequ beam_heal_hor_x = local_beam_unpol(freq)[0] beam_heal_hor_y = local_beam_unpol(freq)[1] beam_heal_equ_x = sv.rotate_healpixmap(beam_heal_hor_x, 0, np.pi / 2 - vs.initial_zenith[1], vs.initial_zenith[0]) beam_heal_equ_y = sv.rotate_healpixmap(beam_heal_hor_y, 0, np.pi / 2 - vs.initial_zenith[1], vs.initial_zenith[0]) ####initial A A = {} for p in ['x', 'y']: pol = p + p # ubl file #// ubl_filename = datadir + tag + '_%s%s_%i_%i.ubl' % (p, p, nUBL, 3) ubls = np.array([[0,0,0]])#//np.fromfile(ubl_filename, dtype='float32').reshape((nUBL, 3)) #// print "%i UBLs to include, longest baseline is %i wavelengths" % (len(ubls), np.max(np.linalg.norm(ubls, axis=1)) / (C / freq)) A_path = datadir + tag + '_%s%s_%i_%i.A' % (p, p, nt_used * len(ubls), 12 * nside_beamweight ** 2) if os.path.isfile(A_path) and not force_recompute: print "Reading A matrix from %s" % A_path
A_filename = datadir + tag + '_%s%s_%i_%i.A'%(p, p, len(tlist)*len(ubls), 12*nside_beamweight**2) if os.path.isfile(A_filename) and not force_recompute: print "Reading A matrix from %s"%A_filename sys.stdout.flush() A[p] = np.fromfile(A_filename, dtype='complex64').reshape((len(ubls), len(tlist), 12*nside_beamweight**2))[:,tmask].reshape((len(ubls)*len(tlist[tmask]), 12*nside_beamweight**2)) else: #beam beam_healpix = local_beam[p](freq) #hpv.mollview(beam_healpix, title='beam %s'%p) #plt.show() vs = sv.Visibility_Simulator() vs.initial_zenith = np.array([0, lat_degree*np.pi/180])#self.zenithequ beam_heal_equ = np.array(sv.rotate_healpixmap(beam_healpix, 0, np.pi/2 - vs.initial_zenith[1], vs.initial_zenith[0])) print "Computing A matrix for %s pol..."%p sys.stdout.flush() timer = time.time() A[p] = np.empty((len(tlist)*len(ubls), 12*nside_beamweight**2), dtype='complex64') for i in range(12*nside_beamweight**2): dec, ra = hpf.pix2ang(nside_beamweight, i)#gives theta phi dec = np.pi/2 - dec print "\r%.1f%% completed, %f minutes left"%(100.*float(i)/(12.*nside_beamweight**2), (12.*nside_beamweight**2-i)/(i+1)*(float(time.time()-timer)/60.)), sys.stdout.flush() A[p][:, i] = np.array([vs.calculate_pointsource_visibility(ra, dec, d, freq, beam_heal_equ = beam_heal_equ, tlist = tlist) for d in ubls]).flatten() print "%f minutes used"%(float(time.time()-timer)/60.) sys.stdout.flush() A[p].tofile(A_filename)