Skip to content

Commit

Permalink
Make detectors and ref time properties of the instance (to be continu…
Browse files Browse the repository at this point in the history
…ed…)
  • Loading branch information
titodalcanton committed Jul 31, 2024
1 parent 9626c00 commit 6613829
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions pycbc/tmpltbank/sky_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


class SkyGrid:
def __init__(self, ra, dec):
def __init__(self, ra, dec, detectors, ref_gps_time):
"""Initialize a sky grid from a list of RA/dec coordinates.
Parameters
Expand All @@ -20,15 +20,22 @@ def __init__(self, ra, dec):
Right ascensions for each point UNITS AND ANGULAR CONVENTION.
dec: iterable of floats
Declination for each point UNITS AND ANGULAR CONVENTION.
detectors: iterable of str
List of detector names associated with the sky grid, typically the
detectors for which the grid has been placed or is to be placed.
The detectors will be used when calculating the antenna pattern
functions and time delays from Earth center.
ref_gps_time: float
Reference GPS time associated with the sky grid. This will be used
when calculating the antenna pattern functions and time delays from
Earth center.
"""
# Question: should the list of detectors and reference times be part of
# the SkyGrid object? Or should they be given each time they are needed,
# independently? This decision will affect the `calculate_*()` methods
# down below.
# 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?
self.positions = np.vstack([ra, dec]).T
self.detectors = sorted(detectors)
self.ref_gps_time = ref_gps_time

def __len__(self):
"""Returns the number of points in the sky grid."""
Expand Down Expand Up @@ -61,6 +68,7 @@ def from_cli(cls, cli_parser, cli_args):
)
return cls.read_from_file(cli_args.sky_grid)
if cli_args.ra is not None and cli_args.dec is not None:
# FIXME where do we get the detectors and ref time from?
return cls([cli_args.ra], [cli_args.dec])
cli_parser.error(
'Please specify a sky grid via --sky-grid or a single sky '
Expand All @@ -73,44 +81,48 @@ def read_from_file(cls, path):
with h5py.File(path, 'r') as hf:
ra = hf['ra'][:]
dec = hf['dec'][:]
return cls(ra, dec)
detectors = hf.attrs['detectors']
ref_gps_time = hf.attrs['ref_gps_time']
return cls(ra, dec, detectors, ref_gps_time)

def write_to_file(self, path):
"""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

def calculate_antenna_patterns(self, detector_names, gps_time):
def calculate_antenna_patterns(self):
"""Calculate the antenna pattern functions at each point in the grid
for a list of GW detectors. Return a dict, keyed by detector name,
whose items are 2-dimensional Numpy arrays. The first dimension of
these arrays runs over the sky grid, and the second dimension runs over
the plus and cross polarizations.
for the list of GW detectors specified at instantiation. Return a dict,
keyed by detector name, whose items are 2-dimensional Numpy arrays.
The first dimension of these arrays runs over the sky grid, and the
second dimension runs over the plus and cross polarizations.
"""
result = {}
for det_name in detector_names:
for det_name in self.detectors:
det = Detector(det_name)
result[det_name] = np.empty((len(self), 2))
for i, (ra, dec) in enumerate(self):
result[det_name][i] = det.antenna_pattern(
ra, dec, 0, t_gps=gps_time
ra, dec, 0, t_gps=self.ref_gps_time
)
return result

def calculate_time_delays(self, detector_names, gps_time):
"""Calculate the time delays from the Earth center to a list of GW
detectors at each point in the grid. Return a dict, keyed by detector
name, whose items are 1-dimensional Numpy arrays containing the time
delays for each sky point.
def calculate_time_delays(self):
"""Calculate the time delays from the Earth center to each GW detector
specified at instantiation, for each point in the grid. Return a dict,
keyed by detector name, whose items are 1-dimensional Numpy arrays
containing the time delays for each sky point.
"""
result = {}
for det_name in detector_names:
for det_name in self.detectors:
det = Detector(det_name)
result[det_name] = np.empty(len(self))
for i, (ra, dec) in enumerate(self):
result[det_name][i] = det.time_delay_from_earth_center(
ra, dec, t_gps=gps_time
ra, dec, t_gps=self.ref_gps_time
)
return result

Expand Down

0 comments on commit 6613829

Please sign in to comment.