From 2f815088f57d285c69f585e06ff06d7a17ee7a7a Mon Sep 17 00:00:00 2001 From: jacquot Date: Mon, 6 Jan 2025 17:46:35 +0100 Subject: [PATCH 1/9] Using SkyGrid functions to write the HDF5 file and calculate time delays --- bin/pycbc_make_sky_grid | 33 +++++++++++++++------------------ pycbc/tmpltbank/sky_grid.py | 8 +++++++- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/bin/pycbc_make_sky_grid b/bin/pycbc_make_sky_grid index ef2971f9399..3fba8808344 100644 --- a/bin/pycbc_make_sky_grid +++ b/bin/pycbc_make_sky_grid @@ -165,23 +165,20 @@ spher = cart_to_spher(rota) # Calculate the time delays between the Earth center # and each detector for each sky point -time_delays = [ - [ - detectors[i].time_delay_from_earth_center( - spher[j][0], spher[j][1], args.trigger_time +sky_grid = SkyGrid( + spher[:, 0], spher[:, 1], args.instruments, args.trigger_time ) - for j in range(len(spher)) - ] - for i in range(len(detectors)) -] +time_delays = sky_grid.calculate_time_delays() + +extra_attributes = { + 'trigger_ra': args.ra, + 'trigger_dec': args.dec, + 'sky_error': args.sky_error, + 'timing_uncertainty': args.timing_uncertainty + } + +extra_datasets = { + 'time_delays': time_delays + } -with HFile(args.output, 'w') as hf: - hf['ra'] = spher[:, 0] - hf['dec'] = spher[:, 1] - hf['trigger_ra'] = [args.ra] - hf['trigger_dec'] = [args.dec] - hf['sky_error'] = [args.sky_error] - hf['trigger_time'] = [args.trigger_time] - hf['timing_uncertainty'] = [args.timing_uncertainty] - hf['instruments'] = [d for d in args.instruments] - hf['time_delays'] = time_delays +sky_grid.write_to_file(args.output, extra_attributes, extra_datasets) diff --git a/pycbc/tmpltbank/sky_grid.py b/pycbc/tmpltbank/sky_grid.py index 9da627c7689..94ac9668073 100644 --- a/pycbc/tmpltbank/sky_grid.py +++ b/pycbc/tmpltbank/sky_grid.py @@ -91,13 +91,19 @@ def read_from_file(cls, path): ref_gps_time = hf.attrs['ref_gps_time'] return cls(ra, dec, detectors, ref_gps_time) - def write_to_file(self, path): + def write_to_file(self, path, extra_attrs, extra_datasets): """Writes a sky grid to an HDF5 file.""" with h5py.File(path, 'w') as hf: hf['ra'] = self.ras hf['dec'] = self.decs hf.attrs['detectors'] = self.detectors hf.attrs['ref_gps_time'] = self.ref_gps_time + if extra_attrs is not None: + for attribute in extra_attrs: + hf.attrs[attribute] = extra_attrs[attribute] + if extra_datasets is not None: + for datasets in extra_datasets: + hf[datasets] = extra_datasets[datasets] def calculate_antenna_patterns(self): """Calculate the antenna pattern functions at each point in the grid From 1e8bb5a808b981177120eacbcefc083a92532206 Mon Sep 17 00:00:00 2001 From: jacquot Date: Wed, 8 Jan 2025 14:10:57 +0100 Subject: [PATCH 2/9] Adding requested changes and changing the name of the instrument attribute into detectors (doesn't work) --- bin/pycbc_make_sky_grid | 26 +++++++++++--------------- bin/sky_grid.hdf5 | Bin 0 -> 14416 bytes pycbc/tmpltbank/sky_grid.py | 13 ++++++------- 3 files changed, 17 insertions(+), 22 deletions(-) create mode 100644 bin/sky_grid.hdf5 diff --git a/bin/pycbc_make_sky_grid b/bin/pycbc_make_sky_grid index 3fba8808344..023520f717c 100644 --- a/bin/pycbc_make_sky_grid +++ b/bin/pycbc_make_sky_grid @@ -58,11 +58,11 @@ parser.add_argument( "otherwise radians are assumed.", ) parser.add_argument( - '--instruments', + '--detectors', nargs="+", type=str, required=True, - help="List of instruments to analyze.", + help="List of detectors to analyze.", ) parser.add_argument( '--sky-error', @@ -92,11 +92,11 @@ args = parser.parse_args() pycbc.init_logging(args.verbose) -if len(args.instruments) == 1: +if len(args.detectors) == 1: parser.error('Can not make a sky grid for only one detector.') -args.instruments.sort() # Put the ifos in alphabetical order -detectors = args.instruments +args.detectors.sort() # Put the ifos in alphabetical order +detectors = args.detectors detectors = [Detector(d) for d in detectors] detector_pairs = list(itertools.combinations(detectors, 2)) @@ -166,19 +166,15 @@ spher = cart_to_spher(rota) # Calculate the time delays between the Earth center # and each detector for each sky point sky_grid = SkyGrid( - spher[:, 0], spher[:, 1], args.instruments, args.trigger_time + spher[:, 0], spher[:, 1], args.detectors, args.trigger_time ) time_delays = sky_grid.calculate_time_delays() extra_attributes = { - 'trigger_ra': args.ra, - 'trigger_dec': args.dec, - 'sky_error': args.sky_error, - 'timing_uncertainty': args.timing_uncertainty - } - -extra_datasets = { - 'time_delays': time_delays - } + 'trigger_ra': args.ra, + 'trigger_dec': args.dec, + 'sky_error': args.sky_error, + 'timing_uncertainty': args.timing_uncertainty +} sky_grid.write_to_file(args.output, extra_attributes, extra_datasets) diff --git a/bin/sky_grid.hdf5 b/bin/sky_grid.hdf5 new file mode 100644 index 0000000000000000000000000000000000000000..1687f793a4ccc03d0505706ff078d60c88878e77 GIT binary patch literal 14416 zcmeD5aB<`1lHy_j0S*oZ76t(@6Gr@pf&dGM2#gPtPk=HQp>zk7Ucm%mFfxE31A_!q zTo7tLy1I}cS62oGCI*=KFdC|jA%O?NYJduGm_ryIQ2r=2yhFgl)iD6%!wpbxPJq%D zDo_TEC`JZ0aQcN13_uE!k{KBxDV`l_FN|ge%7bJC73>)}z;X@@Cfp1r|Cu2QS%IP; zc_wBiumRi*3Sd6eI0j~D{z%J7ElJKuEiyKO=;MIe%gn@Nzyc8hSqZTM%uj%b(wGG0 zQ&5hEeea6)o8s)AA8Xb6mkz-S1JhQMeDjE2CF4}oAme-}muR(P*s zg(!Y66+oq55NOxI+7AlQb`>sDq1_e+4nvIQ`rvOh~j@{%?tii?rY_L#*8cG4-=bY#-P#}Yu#IEsy<7Wl zZuDreQ@gdFch(&mU;ciOv)bA-TR>m z2^~WHdMwpWdp6)TXQZ2w`Sm_nMtWP6j#KaXz= zoNPZuaYf;_gvs`f36_yd{U_UB$kG*Cx})FzLdBHTKQ{H-TYfD&{V=ZI-d{uG^pco< z`|?Ys)dB1J?d?-=H1pDvohtFKltJ?4R{#C~#Onh2N_wy_Fn`}R1x>&3&=?I5V2mTuhruun4*_u25&$N9 zNXmBrr*rsxH%30Oz!46xnf*bLP2i58Uw(1G5iWzP{D1{aQ6hwul9~)=loVyAr>7Rh z!^B~Hn0#?|WqfK;QGOBB0GND9W^O7-2uNq Date: Wed, 8 Jan 2025 15:55:50 +0100 Subject: [PATCH 3/9] Renaming the instruments argument as instruments and not detectors after discussion --- bin/pycbc_make_sky_grid | 18 +++++++++--------- bin/sky_grid.hdf5 | Bin 14416 -> 0 bytes pycbc/tmpltbank/sky_grid.py | 1 - 3 files changed, 9 insertions(+), 10 deletions(-) delete mode 100644 bin/sky_grid.hdf5 diff --git a/bin/pycbc_make_sky_grid b/bin/pycbc_make_sky_grid index 023520f717c..2aa2294547b 100644 --- a/bin/pycbc_make_sky_grid +++ b/bin/pycbc_make_sky_grid @@ -20,7 +20,7 @@ import pycbc from pycbc.detector import Detector from pycbc.io.hdf import HFile from pycbc.types import angle_as_radians - +from pycbc.tmpltbank import sky_grid def spher_to_cart(sky_points): """Convert spherical coordinates to cartesian coordinates.""" @@ -58,11 +58,11 @@ parser.add_argument( "otherwise radians are assumed.", ) parser.add_argument( - '--detectors', + '--instruments', nargs="+", type=str, required=True, - help="List of detectors to analyze.", + help="List of instuments to analyze.", ) parser.add_argument( '--sky-error', @@ -92,11 +92,11 @@ args = parser.parse_args() pycbc.init_logging(args.verbose) -if len(args.detectors) == 1: +if len(args.instruments) == 1: parser.error('Can not make a sky grid for only one detector.') -args.detectors.sort() # Put the ifos in alphabetical order -detectors = args.detectors +args.instruments.sort() # Put the ifos in alphabetical order +detectors = args.instruments detectors = [Detector(d) for d in detectors] detector_pairs = list(itertools.combinations(detectors, 2)) @@ -165,8 +165,8 @@ spher = cart_to_spher(rota) # Calculate the time delays between the Earth center # and each detector for each sky point -sky_grid = SkyGrid( - spher[:, 0], spher[:, 1], args.detectors, args.trigger_time +sky_grid = sky_grid.SkyGrid( + spher[:, 0], spher[:, 1], args.instruments, args.trigger_time ) time_delays = sky_grid.calculate_time_delays() @@ -177,4 +177,4 @@ extra_attributes = { 'timing_uncertainty': args.timing_uncertainty } -sky_grid.write_to_file(args.output, extra_attributes, extra_datasets) +sky_grid.write_to_file(args.output, extra_attributes) diff --git a/bin/sky_grid.hdf5 b/bin/sky_grid.hdf5 deleted file mode 100644 index 1687f793a4ccc03d0505706ff078d60c88878e77..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 14416 zcmeD5aB<`1lHy_j0S*oZ76t(@6Gr@pf&dGM2#gPtPk=HQp>zk7Ucm%mFfxE31A_!q zTo7tLy1I}cS62oGCI*=KFdC|jA%O?NYJduGm_ryIQ2r=2yhFgl)iD6%!wpbxPJq%D zDo_TEC`JZ0aQcN13_uE!k{KBxDV`l_FN|ge%7bJC73>)}z;X@@Cfp1r|Cu2QS%IP; zc_wBiumRi*3Sd6eI0j~D{z%J7ElJKuEiyKO=;MIe%gn@Nzyc8hSqZTM%uj%b(wGG0 zQ&5hEeea6)o8s)AA8Xb6mkz-S1JhQMeDjE2CF4}oAme-}muR(P*s zg(!Y66+oq55NOxI+7AlQb`>sDq1_e+4nvIQ`rvOh~j@{%?tii?rY_L#*8cG4-=bY#-P#}Yu#IEsy<7Wl zZuDreQ@gdFch(&mU;ciOv)bA-TR>m z2^~WHdMwpWdp6)TXQZ2w`Sm_nMtWP6j#KaXz= zoNPZuaYf;_gvs`f36_yd{U_UB$kG*Cx})FzLdBHTKQ{H-TYfD&{V=ZI-d{uG^pco< z`|?Ys)dB1J?d?-=H1pDvohtFKltJ?4R{#C~#Onh2N_wy_Fn`}R1x>&3&=?I5V2mTuhruun4*_u25&$N9 zNXmBrr*rsxH%30Oz!46xnf*bLP2i58Uw(1G5iWzP{D1{aQ6hwul9~)=loVyAr>7Rh z!^B~Hn0#?|WqfK;QGOBB0GND9W^O7-2uNq Date: Wed, 8 Jan 2025 17:01:49 +0100 Subject: [PATCH 4/9] Fixing indentations and name of variables, removing the calculation of the time delays for now --- bin/pycbc_make_sky_grid | 9 +++------ pycbc/tmpltbank/sky_grid.py | 4 ++-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/bin/pycbc_make_sky_grid b/bin/pycbc_make_sky_grid index 2aa2294547b..5ff1e86b05a 100644 --- a/bin/pycbc_make_sky_grid +++ b/bin/pycbc_make_sky_grid @@ -62,7 +62,7 @@ parser.add_argument( nargs="+", type=str, required=True, - help="List of instuments to analyze.", + help="List of instruments to analyze.", ) parser.add_argument( '--sky-error', @@ -163,12 +163,9 @@ rota = r.apply(cart) # Convert cartesian coordinates back to spherical coordinates spher = cart_to_spher(rota) -# Calculate the time delays between the Earth center -# and each detector for each sky point sky_grid = sky_grid.SkyGrid( - spher[:, 0], spher[:, 1], args.instruments, args.trigger_time - ) -time_delays = sky_grid.calculate_time_delays() + spher[:, 0], spher[:, 1], args.instruments, args.trigger_time +) extra_attributes = { 'trigger_ra': args.ra, diff --git a/pycbc/tmpltbank/sky_grid.py b/pycbc/tmpltbank/sky_grid.py index 0412ccab366..b5c142e88ed 100644 --- a/pycbc/tmpltbank/sky_grid.py +++ b/pycbc/tmpltbank/sky_grid.py @@ -100,8 +100,8 @@ def write_to_file(self, path, extra_attrs=None, extra_datasets=None): hf.attrs['ref_gps_time'] = self.ref_gps_time for attribute in (extra_attrs or {}): hf.attrs[attribute] = extra_attrs[attribute] - for datasets in (extra_datasets or {}): - hf[datasets] = extra_datasets[datasets] + for dataset in (extra_datasets or {}): + hf[dataset] = extra_datasets[dataset] def calculate_antenna_patterns(self): """Calculate the antenna pattern functions at each point in the grid From 6bb5ab2e0c21bfd0c92a93fb93c2ae9bf96cb65c Mon Sep 17 00:00:00 2001 From: jacquot Date: Wed, 8 Jan 2025 17:07:42 +0100 Subject: [PATCH 5/9] Adding extraline after imports --- bin/pycbc_make_sky_grid | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/pycbc_make_sky_grid b/bin/pycbc_make_sky_grid index 5ff1e86b05a..e99daddb0df 100644 --- a/bin/pycbc_make_sky_grid +++ b/bin/pycbc_make_sky_grid @@ -22,6 +22,7 @@ from pycbc.io.hdf import HFile from pycbc.types import angle_as_radians from pycbc.tmpltbank import sky_grid + def spher_to_cart(sky_points): """Convert spherical coordinates to cartesian coordinates.""" cart = np.zeros((len(sky_points), 3)) From b46cb9024e2d52ca767aa7b079a5af6a805eae8e Mon Sep 17 00:00:00 2001 From: jacquot Date: Thu, 9 Jan 2025 14:41:57 +0100 Subject: [PATCH 6/9] Modifying the the conversion of cartesian coordinates into spherical ones --- bin/pycbc_make_sky_grid | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/pycbc_make_sky_grid b/bin/pycbc_make_sky_grid index e99daddb0df..e59e6fc9f4f 100644 --- a/bin/pycbc_make_sky_grid +++ b/bin/pycbc_make_sky_grid @@ -37,6 +37,7 @@ def cart_to_spher(sky_points): spher = np.zeros((len(sky_points), 2)) spher[:, 0] = np.arctan2(sky_points[:, 1], sky_points[:, 0]) spher[:, 1] = np.arcsin(sky_points[:, 2]) + spher[spher[:, 0] <0, 0] += 2 * np.pi return spher From 6b653a41aece9dc1617cd656901510866b1d325f Mon Sep 17 00:00:00 2001 From: jacquot Date: Thu, 9 Jan 2025 15:01:59 +0100 Subject: [PATCH 7/9] Add sanity checks on RA and DEC --- pycbc/tmpltbank/sky_grid.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pycbc/tmpltbank/sky_grid.py b/pycbc/tmpltbank/sky_grid.py index b5c142e88ed..31b9fe9bdd6 100644 --- a/pycbc/tmpltbank/sky_grid.py +++ b/pycbc/tmpltbank/sky_grid.py @@ -34,6 +34,10 @@ def __init__(self, ra, dec, detectors, ref_gps_time): # We store the points in a 2D array internally, first dimension runs # over the list of points, second dimension is RA/dec. # Question: should we use Astropy sky positions instead? + if (self.ra < 0).any() or (self.ra > 2 * np.pi).any(): + raise ValueError('RA must be in the range [0,2π]') + if (self.dec < -np.pi/2).any() or (ra > np.pi/2).any(): + raise ValueError('DEC must be in the range [-π/2, π/2]') self.positions = np.vstack([ra, dec]).T self.detectors = sorted(detectors) self.ref_gps_time = ref_gps_time From 8da40551e30603eec01e50b36dbc37224134cba4 Mon Sep 17 00:00:00 2001 From: jacquot Date: Thu, 9 Jan 2025 16:21:54 +0100 Subject: [PATCH 8/9] Correcting checks on ra and dec and ensure that they are np arrays --- bin/pycbc_make_sky_grid | 1 - pycbc/tmpltbank/sky_grid.py | 7 ++++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/pycbc_make_sky_grid b/bin/pycbc_make_sky_grid index e59e6fc9f4f..663de80a43a 100644 --- a/bin/pycbc_make_sky_grid +++ b/bin/pycbc_make_sky_grid @@ -18,7 +18,6 @@ from scipy.spatial.transform import Rotation as R import pycbc from pycbc.detector import Detector -from pycbc.io.hdf import HFile from pycbc.types import angle_as_radians from pycbc.tmpltbank import sky_grid diff --git a/pycbc/tmpltbank/sky_grid.py b/pycbc/tmpltbank/sky_grid.py index 31b9fe9bdd6..224a585a883 100644 --- a/pycbc/tmpltbank/sky_grid.py +++ b/pycbc/tmpltbank/sky_grid.py @@ -8,7 +8,7 @@ import h5py from pycbc.detector import Detector - +from pycbc.conversion import ensurearray class SkyGrid: def __init__(self, ra, dec, detectors, ref_gps_time): @@ -34,9 +34,10 @@ def __init__(self, ra, dec, detectors, ref_gps_time): # We store the points in a 2D array internally, first dimension runs # over the list of points, second dimension is RA/dec. # Question: should we use Astropy sky positions instead? - if (self.ra < 0).any() or (self.ra > 2 * np.pi).any(): + ra, dec, _ = ensurearray(ra,dec) + if (ra < 0).any() or (ra > 2 * np.pi).any(): raise ValueError('RA must be in the range [0,2π]') - if (self.dec < -np.pi/2).any() or (ra > np.pi/2).any(): + if (dec < -np.pi/2).any() or (dec > np.pi/2).any(): raise ValueError('DEC must be in the range [-π/2, π/2]') self.positions = np.vstack([ra, dec]).T self.detectors = sorted(detectors) From b9367d4da3677babacc705bd8cdb05c0c12a3c04 Mon Sep 17 00:00:00 2001 From: jacquot Date: Thu, 9 Jan 2025 17:18:01 +0100 Subject: [PATCH 9/9] Changing the way of importing SkyGrid class and correcting the import of pycbc.conversions --- bin/pycbc_make_sky_grid | 4 ++-- pycbc/tmpltbank/sky_grid.py | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/bin/pycbc_make_sky_grid b/bin/pycbc_make_sky_grid index 663de80a43a..c4d8da61f2a 100644 --- a/bin/pycbc_make_sky_grid +++ b/bin/pycbc_make_sky_grid @@ -19,7 +19,7 @@ from scipy.spatial.transform import Rotation as R import pycbc from pycbc.detector import Detector from pycbc.types import angle_as_radians -from pycbc.tmpltbank import sky_grid +from pycbc.tmpltbank.sky_grid import SkyGrid def spher_to_cart(sky_points): @@ -164,7 +164,7 @@ rota = r.apply(cart) # Convert cartesian coordinates back to spherical coordinates spher = cart_to_spher(rota) -sky_grid = sky_grid.SkyGrid( +sky_grid = SkyGrid( spher[:, 0], spher[:, 1], args.instruments, args.trigger_time ) diff --git a/pycbc/tmpltbank/sky_grid.py b/pycbc/tmpltbank/sky_grid.py index 224a585a883..26f72483f2d 100644 --- a/pycbc/tmpltbank/sky_grid.py +++ b/pycbc/tmpltbank/sky_grid.py @@ -8,7 +8,8 @@ import h5py from pycbc.detector import Detector -from pycbc.conversion import ensurearray +from pycbc.conversions import ensurearray + class SkyGrid: def __init__(self, ra, dec, detectors, ref_gps_time):