# run romSpline to convert into reduced order spline, then assign final .h5 values # and write all data to .h5 file # handled independently for Magnitude/Argument vs. Pluss/Cross data, based on # unique needs for each format if (dataFormat == "MagArg"): # for m>0 strainAmp = np.array(strainVal1) strainPhase = np.array(strainVal2) # for m<0 strainAmp2 = np.array(strainVal1) strainPhase2 = np.array(strain2neg) elif (dataFormat == "PlusCross"): strainAmp = wfutils.amplitude_from_polarizations( strainVal1, strainVal2).data strainPhase = wfutils.phase_from_polarizations( strainVal1, strainVal2).data # for m<0 strainAmp2 = wfutils.amplitude_from_polarizations( strainVal1, strain2neg).data strainPhase2 = wfutils.phase_from_polarizations( strainVal1, strain2neg).data else: print "dataFormat is incorrect or is not specified. Edit the metadata file and try again." print ' key = %s' % (key) print ' fitting spline...' try: # when a mode has nothing but zeros for strain, we don't want to add it to the .h5 sAmpH = romSpline.ReducedOrderSpline(times, strainAmp,
Hplus_approx, hplus_NR, low_frequency_cutoff=30.0, psd=noise_psd, high_frequency_cutoff=upp_bound ) # Errors if errors_file is not None: dAmpbyAmp.resize(tlen) dphi.resize(tlen) # Convert waveform to amplitude/phase, add/subtract errors amp_NR = wfutils.amplitude_from_polarizations(hplus_NR, hcross_NR) amp_NR_deltaUpp = amp_NR + dAmpbyAmp * amp_NR amp_NR_deltaLow = amp_NR - dAmpbyAmp * amp_NR phi_NR = wfutils.phase_from_polarizations(hplus_NR, hcross_NR) phi_NR_deltaUpp = phi_NR + dphi phi_NR_deltaLow = phi_NR - dphi hplus_NR_deltaLow = pycbc.types.TimeSeries( np.real(amp_NR_deltaLow * np.exp(1j * phi_NR_deltaLow)), delta_t=hplus_NR.delta_t ) hplus_NR_deltaUpp = pycbc.types.TimeSeries( np.real(amp_NR_deltaUpp * np.exp(1j * phi_NR_deltaUpp)), delta_t=hplus_NR.delta_t ) match_deltaUpp, _ = pycbc.filter.match( hplus_NR, hplus_NR_deltaUpp, low_frequency_cutoff=30.0, psd=noise_psd, high_frequency_cutoff=upp_bound ) match_deltaLow, _ = pycbc.filter.match(
def build_catalog(simulations, mtotal=100.0, nTsamples=1024, delta_t=1./1024, noise_file=None): """ Build the data matrix. """ # Preallocation amp_cat = np.zeros(shape=(simulations.nsimulations, nTsamples)) phase_cat = np.zeros(shape=(simulations.nsimulations, nTsamples)) for s, sim in enumerate(simulations.simulations): print "Adding {0} to catalog ({1} of {2})".format(sim['wavefile'], s, simulations.nsimulations) # Extract waveform hpRaw, hcRaw = nrbu.get_wf_pols(sim['wavefile'], mtotal=mtotal, inclination=0.0, delta_t=delta_t, f_lower=30, distance=100) #hp, hc = nrbu.get_wf_pols(sim['wavefile'], mtotal=mtotal, # inclination=0.0, delta_t=delta_t, f_lower=30, distance=100) # zero-pad hp = pycbc.types.TimeSeries(np.zeros(nTsamples), delta_t=hpRaw.delta_t) hc = pycbc.types.TimeSeries(np.zeros(nTsamples), delta_t=hcRaw.delta_t) hp.data[:len(hpRaw)]=np.copy(hpRaw.data) hc.data[:len(hcRaw)]=np.copy(hcRaw.data) # Highpass #hp = pycbc.filter.highpass(hp, 30.0) #hc = pycbc.filter.highpass(hc, 30.0) # Whiten if noise_file is not None: Hp = hp.to_frequencyseries() Hc = hc.to_frequencyseries() noise_data = np.loadtxt(noise_file) noise_asd = np.exp(np.interp(Hp.sample_frequencies, noise_data[:,0], np.log(noise_data[:,1]))) print 'whitening' Hp.data /= noise_asd Hc.data /= noise_asd hp = Hp.to_timeseries() hc = Hc.to_timeseries() hp = pycbc.types.TimeSeries(hp[:len(hpRaw)],delta_t=hp.delta_t) hc = pycbc.types.TimeSeries(hc[:len(hcRaw)],delta_t=hc.delta_t) amp = wfutils.amplitude_from_polarizations(hp, hc) phase = wfutils.phase_from_polarizations(hp, hc) # Normalise to unit norm amp /= np.linalg.norm(amp) # --- Populate time series catalogue (align peak amp to center) peakidx = np.argmax(amp.data) # Apply some smoothing to the start / end to get rid of remaining # small-number junk ampthresh=1e-2 # After ringdown: amptmp = np.copy(amp.data) amptmp[:peakidx] = 1e10 postmerger = np.argwhere(amptmp < ampthresh*max(amp.data))[0] win = lal.CreateTukeyREAL8Window(int(len(phase)-postmerger), 0.1) window = 1-win.data.data window[0.5*len(window):]=0.0 phase.data[postmerger:] *= window amp.data[postmerger:] *= window # before waveform: # XXX: careful with this - we just want to smooth out junk, but we # don't really want to artificially truncate the waveforms in-band # premerger = np.argwhere(amp>ampthresh*max(amp.data))[0] # # win = lal.CreateTukeyREAL8Window(int(len(phase)-premerger), 0.1) # # window = win.data.data # window[0.5*len(window):]=1.0 #phase.data *= window #amp.data *= window # from matplotlib import pyplot as pl # pl.figure() # pl.plot(amp) # pl.show() # sys.exit() # POPULATE # right start = 0.5*nTsamples end = start+len(amp.data)-peakidx amp_cat[s, start:end] = np.copy(amp.data[peakidx:]) phase_cat[s, start:end] = np.copy(phase.data[peakidx:]) # left start = 0.5*nTsamples-peakidx end = 0.5*nTsamples amp_cat[s, start:end] = np.copy(amp.data[:peakidx]) phase_cat[s, start:end] = np.copy(phase.data[:peakidx]) return (amp_cat, phase_cat) def main(): print >> sys.stdout, sys.argv[0] print >> sys.stdout, __version__ print >> sys.stdout, __date__ return 0
fd.attrs.create('spin2y', 0.0) fd.attrs.create('coa_phase', 0.0) fd.attrs.create('mass1', mass1 / total_mass) fd.attrs.create('mass2', mass2 / total_mass) for m in range(-l, l + 1): if abs(m) != 2: HlmAmp = np.zeros(wavelen) HlmPhase = np.zeros(wavelen) else: key = "l=%d, m=%d" % (l, m) hplus = pycbc.types.TimeSeries(np.real(Hlm[key]), delta_t=delta_t) hcross = pycbc.types.TimeSeries(-1 * np.imag(Hlm[key]), delta_t=delta_t) massMpc = total_mass * lal.MRSUN_SI / (20.0e6) HlmAmp = massMpc * wfutils.amplitude_from_polarizations( hplus, hcross).data HlmPhase = wfutils.phase_from_polarizations(hplus, hcross).data sAmph = romSpline.ReducedOrderSpline(times_M, HlmAmp, verbose=True) sPhaseh = romSpline.ReducedOrderSpline(times_M, HlmPhase, verbose=True) gramp = fd.create_group('amp_l%d_m%d' % (l, m)) sAmph.write(gramp) grphase = fd.create_group('phase_l%d_m%d' % (l, m)) sPhaseh.write(grphase)
sPhaseH = romSpline.ReducedOrderSpline(times, strainPhase, rel=True, verbose=False) grAmp = fd.create_group('amp_l%d_m%d' % (l, m)) sAmpH.write(grAmp) grPhase = fd.create_group('phase_l%d_m%d' % (l, m)) sPhaseH.write(grPhase) print 'spline created' elif (dataFormat == "PlusCross"): strainAmp = wfutils.amplitude_from_polarizations( strainVal1, strainVal2 / massMpc).data strainPhase = wfutils.phase_from_polarizations( strainVal1, strainVal2 / massMpc).data print 'fitting spline...' sAmpH = romSpline.ReducedOrderSpline(times, strainAmp, rel=True, verbose=False) sPhaseH = romSpline.ReducedOrderSpline(times, strainPhase, rel=True, verbose=False) grAmp = fd.create_group('amp_l%d_m%d' % (l, m)) sAmpH.write(grAmp) grPhase = fd.create_group('phase_l%d_m%d' % (l, m))
def writeHybrid_h5(path_name_part, metadata, approx, sim_name, h1, h1_ts, h2, h2_ts, delta_t, ip2, fp2): solar_mass_mpc = MRSUN_SI / ((1 * 10**6) * PC_SI) grav_m1 = metadata[0][0] grav_m2 = metadata[0][1] baryon_m1 = metadata[1][0] baryon_m2 = metadata[1][1] m1 = metadata[2][0] m2 = metadata[2][1] s1x = metadata[3][0] s1y = metadata[3][1] s1z = metadata[3][2] s2x = metadata[3][3] s2y = metadata[3][4] s2z = metadata[3][5] LNhatx = metadata[4][0] LNhaty = metadata[4][1] LNhatz = metadata[4][2] n_hatx = 1.0 #metadata[5][0] n_haty = 0.0 #metadata[5][1] n_hatz = 0.0 #metadata[5][2] tidal_1 = metadata[6][0] tidal_2 = metadata[6][1] type_sim = metadata[7] f_low = metadata[8] EOS = metadata[9] f_low_num = metadata[10] M = 300 ## length of the windowing function total_mass = grav_m1 + grav_m2 ### used masses are the masses used in the characterization procedure ### hybridize numerical (h2) and analytical waveform (h1) at a particular match window length set by fp2 hybridPN_Num = hy.hybridize(h1, h1_ts, h2, h2_ts, match_i=0, match_f=fp2, delta_t=delta_t, M=M, info=1) shift_time = (hybridPN_Num[6], fp2) hh_freq = (hybridPN_Num[7], fp2) match = hybridPN_Num[0] ### path_name_part contains simulation details that must be passed into this function. path_name = path_name_part + 'fp2_' + str(fp2) + '.h5' f_low_M = f_low * (TWOPI * total_mass * MTSUN_SI) with h5py.File(path_name, 'w') as fd: mchirp, eta = pnutils.mass1_mass2_to_mchirp_eta(grav_m1, grav_m2) fd.attrs['type'] = 'Hybrid:%s' % type_sim fd.attrs['hgroup'] = 'Read_CSUF' fd.attrs['Format'] = 1 fd.attrs['Lmax'] = 2 fd.attrs['approx'] = approx fd.attrs['sim_name'] = sim_name fd.attrs['f_lower_at_1MSUN'] = f_low fd.attrs['eta'] = eta fd.attrs['spin1x'] = s1x fd.attrs['spin1y'] = s1y fd.attrs['spin1z'] = s1z fd.attrs['spin2x'] = s2x fd.attrs['spin2y'] = s2y fd.attrs['spin2z'] = s2z fd.attrs['LNhatx'] = LNhatx fd.attrs['LNhaty'] = LNhaty fd.attrs['LNhatz'] = LNhatz fd.attrs['nhatx'] = 1.0 #n_hatx fd.attrs['nhaty'] = 0.0 #n_haty fd.attrs['nhatz'] = 0.0 #n_hatz fd.attrs['mass1'] = m1 fd.attrs['mass2'] = m2 fd.attrs['grav_mass1'] = grav_m1 fd.attrs['grav_mass2'] = grav_m2 fd.attrs['baryon_mass1'] = baryon_m1 fd.attrs['baryon_mass2'] = baryon_m2 fd.attrs['lambda1'] = tidal_1 fd.attrs['lambda2'] = tidal_2 fd.attrs['fp1'] = len(h1) fd.attrs['ip2'] = ip2 fd.attrs['hybrid_match'] = match fd.attrs['shift_time'] = shift_time fd.attrs['hybridize_freq'] = hh_freq fd.attrs['EOS'] = EOS fd.attrs['f_low_num'] = f_low_num gramp = fd.create_group('amp_l2_m2') grphase = fd.create_group('phase_l2_m2') times = hybridPN_Num[5][0] hplus = hybridPN_Num[5][1] hcross = hybridPN_Num[5][2] massMpc = total_mass * solar_mass_mpc hplusMpc = pycbc.types.TimeSeries(hplus / massMpc, delta_t=delta_t) hcrossMpc = pycbc.types.TimeSeries(hcross / massMpc, delta_t=delta_t) times_M = times / (MTSUN_SI * total_mass) HlmAmp = wfutils.amplitude_from_polarizations(hplusMpc, hcrossMpc).data HlmPhase = wfutils.phase_from_polarizations(hplusMpc, hcrossMpc).data sAmph = romspline.ReducedOrderSpline(times_M, HlmAmp, rel=True, verbose=False) sPhaseh = romspline.ReducedOrderSpline(times_M, HlmPhase, rel=True, verbose=False) sAmph.write(gramp) sPhaseh.write(grphase) fd.close() ## this function returns the hybridization frequency (hh_freq) and the time shift (shift_time) associated with each hybrid. return (shift_time, hh_freq)