Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions bin/live/pycbc_live_plot_combined_single_significance_fits
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ import matplotlib
matplotlib.use('agg')
from matplotlib import pyplot as plt

from lal import gpstime

import pycbc
from pycbc.live import strip_time
from pycbc.io.hdf import HFile


Expand Down Expand Up @@ -115,9 +114,10 @@ t = min_start
# Last tick will be the midnight after the last datapoint
while t < max_end + 86400:
# Strip off the time information, ticks are at midnight
time_dt = gpstime.gps_to_utc(t).date()
xtix_labels.append(time_dt.strftime("%Y-%m-%d"))
xtix.append(gpstime.utc_to_gps(time_dt).gpsSeconds)

time_midnight, str_date = strip_time(t)
xtix_labels.append(str_date)
xtix.append(time_midnight)
t += 86400

logging.info("Plotting fits information")
Expand Down
12 changes: 9 additions & 3 deletions bin/live/pycbc_live_plot_single_significance_fits
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import matplotlib
matplotlib.use('agg')
from matplotlib import pyplot as plt

from lal import gpstime
from astropy.time import Time

import pycbc
from pycbc.bin_utils import IrregularBins
Expand Down Expand Up @@ -81,8 +81,14 @@ with HFile(args.trigger_fits_file, 'r') as trfit_f:
fit_function = {ifo: trfit_f[ifo].attrs['fit_function'] for ifo in ifos}
start_time = trfit_f.attrs['fit_start_gps_time']
end_time = trfit_f.attrs['fit_end_gps_time']
start_str = gpstime.gps_to_utc(start_time).strftime("%Y-%m-%d %H:%M:%S")
end_str = gpstime.gps_to_utc(end_time).strftime("%Y-%m-%d %H:%M:%S")
start_str = Time(
start_time,
format='gps'
).to_datetime().strftime("%Y-%m-%d %H:%M:%S")
end_str = Time(
end_time,
format='gps'
).to_datetime().strftime("%Y-%m-%d %H:%M:%S")

# Get the triggers for each detector
# (This is ones which passed the cuts in the fitting code)
Expand Down
74 changes: 45 additions & 29 deletions bin/live/pycbc_live_supervise_collated_trigger_fits
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import numpy as np

from datetime import datetime, timedelta

import lal
from lal import gpstime
from astropy.time import Time

import pycbc
from pycbc.live import supervision as sv
Expand Down Expand Up @@ -44,16 +43,10 @@ def get_true_date(replay_day_dt, controls):
replay_start_time = int(controls['replay-start-time'])
true_start_time = int(controls['true-start-time'])
replay_duration = int(controls['replay-duration'])
rep_start_utc = lal.GPSToUTC(replay_start_time)[0:6]

dt_replay_start = datetime(
year=rep_start_utc[0],
month=rep_start_utc[1],
day=rep_start_utc[2],
hour=rep_start_utc[3],
minute=rep_start_utc[4],
second=rep_start_utc[5]
)
dt_replay_start = Time(
replay_start_time,
format='gps'
).to_datetime()

td = (replay_day_dt - dt_replay_start).total_seconds()

Expand All @@ -62,15 +55,11 @@ def get_true_date(replay_day_dt, controls):

# Add this on to the original start time to get the current time of
# the replay data
true_utc = lal.GPSToUTC(true_start_time)[0:6]
dt_true_start = datetime(
year=true_utc[0],
month=true_utc[1],
day=true_utc[2],
hour=true_utc[3],
minute=true_utc[4],
second=true_utc[5]
)
dt_true_start = Time(
true_start_time,
format='gps'
).to_datetime()

# Original time of the data being replayed right now
return dt_true_start + timedelta(seconds=time_since_replay)

Expand All @@ -91,8 +80,16 @@ def trigger_collation(
'pycbc_live_collate_triggers',
]
collate_args += sv.dict_to_args(collation_options)
gps_start = gpstime.utc_to_gps(day_dt).gpsSeconds
gps_end = gpstime.utc_to_gps(day_dt + timedelta(days=1)).gpsSeconds
gps_start = int(Time(
day_dt,
format='datetime',
scale='utc'
).gps)
gps_end = int(Time(
day_dt + timedelta(days=1),
format='datetime',
scale='utc'
).gps)

trig_merge_file = os.path.join(
output_dir,
Expand Down Expand Up @@ -326,8 +323,16 @@ def single_significance_fits(
)
daily_args = ['pycbc_live_single_significance_fits']

gps_start_time = gpstime.utc_to_gps(day_dt).gpsSeconds
gps_end_time = gpstime.utc_to_gps(day_dt + timedelta(days=1)).gpsSeconds
gps_start_time = int(Time(
day_dt,
format='datetime',
scale='utc'
).gps)
gps_end_time = int(Time(
day_dt + timedelta(days=1),
format='datetime',
scale='utc'
).gps)
daily_fit_options['gps-start-time'] = f'{gps_start_time:d}'
daily_fit_options['gps-end-time'] = f'{gps_end_time:d}'
daily_args += sv.dict_to_args(daily_fit_options)
Expand Down Expand Up @@ -497,10 +502,21 @@ def daily_dq_trigger_rates(

start_time = get_true_date(day_dt, daily_dq_controls)

gps_start_time = gpstime.utc_to_gps(start_time).gpsSeconds
gps_end_time = gpstime.utc_to_gps(start_time + timedelta(days=1)).gpsSeconds

offset = gpstime.utc_to_gps(day_dt).gpsSeconds - gps_start_time
gps_start_time = int(Time(
start_time,
format='datetime',
scale='utc'
).gps)
gps_end_time = int(Time(
start_time + timedelta(days=1),
format='datetime',
scale='utc'
).gps)
offset = int(Time(
day_dt,
format='datetime',
scale='utc'
).gps) - gps_start_time

ddtr_out_full = os.path.join(output_dir, ddtr_out_fname)
daily_dq_args = ['pycbc_live_collated_dq_trigger_rates']
Expand Down
100 changes: 100 additions & 0 deletions pycbc/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"""
Constants module for PyCBC.

This module provides constants from various packages (LAL, astropy, numpy)
based on a preferred order of availability. This helps minimize dependency bloat
by relying on smaller, more general packages (astropy/numpy) when the large
LALSuite dependency is not available.

The priority order for constant lookup is: LAL > Astropy / SciPy / NumPy
"""

import logging
import os

import numpy as np
from astropy import (
constants as aconstants,
units as aunits
)

from pycbc.libutils import import_optional

lal = import_optional('lal')

# We define a global logger for this module
logger = logging.getLogger('pycbc.constants')

# Get the environment variable which defines which constants to use
# Allowed values are 'default' or 'lal'
_CONSTANTS = os.environ.get('PYCBC_CONSTANT_SOURCE', 'default').lower()


# first, do mappings for constants where the value is directly in astropy/numpy
# All are in SI units
_DEFAULT_MAPPING = {
'C_SI': aconstants.c.value, # Speed of light
'G_SI': aconstants.G.value, # Gravitational constant
'MSUN_SI': aconstants.M_sun.value, # Mass of the Sun
'PC_SI': aconstants.pc.value, # Parsec
'REARTH_SI': aconstants.R_earth.value, # Earth equatorial radius
'YRJUL_SI': aunits.year.to(aunits.s), # years in seconds
'PI': np.pi,
'TWOPI': 2 * np.pi,
'GAMMA': np.euler_gamma,
'PI_4': np.pi / 4.,
'LN2': np.log(2.)
}

# We need to define some constants from astropy values
MSUN_SI = _DEFAULT_MAPPING['MSUN_SI']
C_SI = _DEFAULT_MAPPING['C_SI']
G_SI = _DEFAULT_MAPPING['G_SI']

MTSUN_SI = MSUN_SI * G_SI / (C_SI ** 3)
MRSUN_SI = MSUN_SI * G_SI / (C_SI * C_SI)

# Add in these hybrid constants
_DEFAULT_MAPPING.update({
'MTSUN_SI': MTSUN_SI,
'MRSUN_SI': MRSUN_SI
})

# Define the Constant Lookup Function
def get_constant(name):
"""
Retrieves a constant value by name from the preferred order of packages.

The order of preference is: Astropy + NumPy > LAL

Parameters
----------
name : str
The name of the constant to retrieve (e.g., 'C_SI' for the speed of light).

Returns
-------
float or object
The value of the constant.

Raises
------
NotImplementedError
If the constant is not found in any of the available packages.
"""


if _CONSTANTS == 'lal': # Allow lal, or LAL, or whatever
return getattr(lal, name)

elif name in _DEFAULT_MAPPING:
return _DEFAULT_MAPPING[name]

raise NotImplementedError(
'Contact the PyCBC team, this should never happen. You are '
'trying to use a constant which is not defined.'
)

# Expose the constants as attributes of this module:
for const_name in _DEFAULT_MAPPING:
globals()[const_name] = get_constant(const_name)
Loading
Loading