3 # Copyright (C) 2019 Chad Hanna, Amit Reza, Leo Tsukada
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 2 of the License, or (at your
8 # option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 # Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 import numpy, scipy.interpolate
21 from lal
import series
22 from gstlal
import templates
23 from ligo.lw
import utils as ligolw_utils
26 parser = argparse.ArgumentParser(description =
'generate a dt dphi covariance matrix')
27 parser.add_argument(
'--psd-xml', help =
'XML containing HLV psd')
28 parser.add_argument(
'--H-snr', type =
float, help =
'H characteristic SNR')
29 parser.add_argument(
'--L-snr', type =
float, help =
'L characteristic SNR')
30 parser.add_argument(
'--V-snr', type =
float, help =
'V characteristic SNR')
31 parser.add_argument(
'--K-snr', type =
float, help =
'K characteristic SNR')
32 parser.add_argument(
'--m1', type =
float,
default = 1.4, help =
'primary component mass')
33 parser.add_argument(
'--m2', type =
float,
default = 1.4, help =
'secondary component mass')
34 parser.add_argument(
'--s1', type =
float,
default = 0., help =
'primary (z) spin')
35 parser.add_argument(
'--s2', type =
float,
default = 0., help =
'secondary (z) spin')
36 parser.add_argument(
'--flow', type =
float,
default = 10., help =
'Low frequency cut-off. Default 10 Hz')
37 parser.add_argument(
'--fhigh', type =
float,
default = 1024., help =
'High frequency cut-off. Default 1024 Hz')
38 parser.add_argument(
"--output", help =
"set the output h5 file, e.g., covmat.h5")
39 args = parser.parse_args()
41 if args.flow >= args.fhigh:
42 raise ValueError(
"flow cannot be greater than fhigh")
45 rho = {
"L1": args.L_snr,
"H1": args.H_snr,
"V1": args.V_snr,
"K1": args.K_snr}
47 instruments = (
"H1",
"L1",
"V1",
"K1")
48 snr = series.read_psd_xmldoc(ligolw_utils.load_filename(refpsd, verbose = True, contenthandler = series.PSDContentHandler))
51 f = numpy.arange(len(snr[ifo].data.data)) * snr[ifo].deltaF
52 psddict[ifo] = scipy.interpolate.interp1d(f, snr[ifo].data.data)
54 def moment(fmin, fmax, n, m1, m2, s1, s2, ifo, psddict = psddict, delta_f = 0.25):
55 farr = numpy.linspace(0, fmax, fmax / delta_f + delta_f)
56 h = templates.hplus_of_f(m1, m2, s1, s2, fmin, fmax, delta_f)
57 return templates.moment(farr, h, n, psddict[ifo])
65 sigsqf = moment(args.flow, args.fhigh, 2, args.m1, args.m2, args.s1, args.s2, ifo) - moment(args.flow, args.fhigh, 1, args.m1, args.m2, args.s1, args.s2, ifo)**2
66 sigsqtt[ifo] = (1. / (2 * 3.14 * rho[ifo] * sigsqf**.5)**2)
67 sigsqpp[ifo] = moment(args.flow, args.fhigh, 2, args.m1, args.m2, args.s1, args.s2, ifo) / (rho[ifo]**2 * sigsqf)
68 sigsqtp[ifo] = moment(args.flow, args.fhigh, 1, args.m1, args.m2, args.s1, args.s2, ifo) / (2 * 3.14 * rho[ifo]**2 * sigsqf)
69 sigsqdd[ifo] = 1. / rho[ifo]**2
76 # FIXME do an actual calculation
77 # transdd = {frozenset(("H1", "L1")): 1. / numpy.sqrt(sigsqdd["H1"] + sigsqdd["L1"]) , frozenset(("H1", "V1")): 1. / numpy.sqrt(sigsqdd["H1"] + sigsqdd["V1"]), frozenset(("L1", "V1")): 1. / numpy.sqrt(sigsqdd["L1"] + sigsqdd["V1"])}
78 # transdd = {frozenset(instrument_pair) : 1. / numpy.sqrt(sigsqdd[instrument_pair[0]] + sigsqdd[instrument_pair[1]]) for instrument_pair in TimePhaseSNR.instument_pairs(("H1", "L1", "V1", "K1"))}
80 pairs = [tuple(sorted(pair)) for pair in itertools.combinations(instruments, 2)]
83 m11 = sigsqtt[a] + sigsqtt[b]
84 m22 = sigsqpp[a] + sigsqpp[b]
85 m12 = m21 = sigsqtp[a] + sigsqtp[b]
86 mat = numpy.array([[m11, m12], [m21, m22]])
87 matinv = numpy.linalg.inv(mat)
88 cholesky_transpose = numpy.linalg.cholesky(matinv).T
89 transtt[frozenset(pair)] = cholesky_transpose[0,0]
90 transtp[frozenset(pair)] = cholesky_transpose[0,1]
91 transpt[frozenset(pair)] = cholesky_transpose[1,0]
92 transpp[frozenset(pair)] = cholesky_transpose[1,1]
93 transdd[frozenset(pair)] = 1. / numpy.sqrt(sigsqdd[a] + sigsqdd[b])
95 f = h5py.File(args.output, "w")
96 f.create_dataset("psd", data=args.psd_xml)
97 h5_snr = f.create_group("SNR")
98 for ifo, snr in rho.items():
99 h5_snr.create_dataset(ifo, data=snr)
100 h5_transtt = f.create_group("transtt")
101 h5_transtp = f.create_group("transtp")
102 h5_transpt = f.create_group("transpt")
103 h5_transpp = f.create_group("transpp")
104 h5_transdd = f.create_group("transdd")
105 for
group, mat in zip((h5_transtt, h5_transtp, h5_transpt, h5_transpp, h5_transdd), (transtt, transtp, transpt, transpp, transdd)):
106 for k,v in mat.items():
107 group.create_dataset(",".join(sorted(k)), data =
float(v))
110 for i in range(1, len(instruments) + 1):
111 for choice in itertools.combinations(instruments, i):
112 combos.add(tuple(sorted(choice)))
113 combos = tuple(sorted(list(combos)))
114 norm = dict((frozenset(k), 0.) for k in combos)
116 h5_norm = f.create_group(
"norm")
118 h5_norm.create_dataset(
",".join(sorted(combo)), data = 1.0)
122 print(
"transtt =", transtt)
123 print("transtp =", transtp)
124 print("transpt =", transpt)
125 print("transpp =", transpp)
126 print("transdd =", transdd)
127 print("norm = ", norm)
def group
group a list roughly according to the distribution in parts, e.g.