Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow units for --ra and --dec, improve external trigger info table #4965

Merged
merged 2 commits into from
Dec 4, 2024
Merged
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
12 changes: 8 additions & 4 deletions bin/pycbc_multi_inspiral
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ from pycbc import (
from pycbc.events import ranking, coherent as coh, EventManagerCoherent
from pycbc.io.hdf import HFile
from pycbc.filter import MatchedFilterControl
from pycbc.types import zeros, float32, complex64
from pycbc.types import zeros, float32, complex64, angle_as_radians
from pycbc.vetoes import sgchisq
from pycbc.tmpltbank.sky_grid import SkyGrid

Expand Down Expand Up @@ -214,11 +214,15 @@ parser.add_argument(
)
parser.add_argument(
"--ra",
type=float,
help="Right ascension of sky point to search (radians).",
type=angle_as_radians,
help="Right ascension of a single sky point to search. Use the rad or deg "
"suffix to specify units, otherwise radians are assumed."
)
parser.add_argument(
"--dec", type=float, help="Declination of sky point to search (radians)."
"--dec",
type=angle_as_radians,
help="Declination of a single sky point to search. Use the rad or deg "
"suffix to specify units, otherwise radians are assumed."
)
parser.add_argument(
"--sky-grid",
Expand Down
70 changes: 40 additions & 30 deletions bin/pygrb/pycbc_pygrb_grb_info_table
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@
import sys
import argparse
from datetime import datetime
import numpy
import math

import lal

from pycbc import add_common_pycbc_options, init_logging
import pycbc.version
import pycbc.results
from pycbc.detector import Detector
from pycbc.detector import Detector, ppdets
from pycbc.results.pygrb_postprocessing_utils import get_antenna_dist_factor
from pycbc.types import angle_as_radians


__author__ = "Francesco Pannarale <[email protected]>"
__version__ = pycbc.version.git_verbose_msg
Expand All @@ -48,15 +50,18 @@ add_common_pycbc_options(parser)
parser.add_argument("--trigger-time", type=int,
required=True,
help="GPS time of the GRB.")
parser.add_argument("--ra", type=float,
parser.add_argument("--ra", type=angle_as_radians,
required=True,
help="Right ascension (radians) of the GRB.")
parser.add_argument("--dec", type=float,
help="Right ascension of the GRB. Use the rad or deg "
"suffix to specify units, otherwise radians are assumed.")
parser.add_argument("--dec", type=angle_as_radians,
required=True,
help="Declination (radians) of the GRB.")
parser.add_argument("--sky-error", type=float,
default=0, required=False,
help="Sky-localisation error (radians) of the GRB.")
help="Declination of the GRB. Use the rad or deg suffix "
"to specify units, otherwise radians are assumed.")
parser.add_argument("--sky-error", type=angle_as_radians,
default=0,
help="Sky-localisation error of the GRB. Use the rad or "
"deg suffix to specify units, otherwise radians are assumed.")
parser.add_argument("--ifos", action="store", nargs='+',
default=None, required=True,
help="List containing the active IFOs.")
Expand All @@ -74,40 +79,45 @@ data = [[]]
data[0].append(str(opts.trigger_time))
headers.append('GPS Time')

utc_time = datetime(*lal.GPSToUTC(opts.trigger_time)[0:6]).strftime("%B %d %Y, %H:%M:%S UTC")
utc_time = datetime(*lal.GPSToUTC(opts.trigger_time)[0:6]).strftime("%Y-%m-%d %H:%M:%S")
data[0].append(utc_time)
headers.append('Coordinated Universal Time')
headers.append('UTC Time')

data[0].append(str(numpy.rad2deg(opts.ra)))
data[0].append(f'{math.degrees(opts.ra):.3f}')
headers.append('R.A. (deg)')

data[0].append(str(numpy.rad2deg(opts.dec)))
data[0].append(f'{math.degrees(opts.dec):.3f}')
headers.append('Dec (deg)')

data[0].append(str(opts.sky_error))
headers.append('Sky Error')
data[0].append(f'{math.degrees(opts.sky_error):.3f}')
headers.append('Sky Error (deg)')

data[0].append(''.join(opts.ifos))
data[0].append(ppdets(opts.ifos, ''))
headers.append('IFOs')

for ifo in opts.ifos:
antenna = Detector(ifo)
factor = get_antenna_dist_factor(antenna,
opts.ra,
opts.dec,
float(opts.trigger_time))
data[0].append('%.3f' % factor)
factor = get_antenna_dist_factor(
antenna, opts.ra, opts.dec, float(opts.trigger_time)
)
data[0].append(f'{factor:.3f}')
headers.append(ifo + ' Antenna Factor')

html = pycbc.results.dq.redirect_javascript + \
str(pycbc.results.static_table(data, headers))

title = 'GRB Summary Information'
caption = 'Parameters of the GRB. The reported antenna factors are the '
caption += 'dist / eff distance as defined by (4.3) in '
caption += 'https://arxiv.org/abs/0705.1514.'

pycbc.results.save_fig_with_metadata(html, opts.output_file, {},
cmd = ' '.join(sys.argv),
title = title,
caption = caption)
title = 'External Trigger Summary Information'
caption = (
'Parameters of the external trigger. The reported antenna factors are the '
'dist / eff distance as defined by Eq (4.3) in '
'https://arxiv.org/abs/0705.1514.'
)

pycbc.results.save_fig_with_metadata(
html,
opts.output_file,
{},
cmd=' '.join(sys.argv),
title=title,
caption=caption
)
34 changes: 34 additions & 0 deletions pycbc/types/optparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

"""
This modules contains extensions for use with argparse
"""

import copy
import warnings
import argparse
import re
import math
from collections import defaultdict


class DictWithDefaultReturn(defaultdict):
default_set = False
ifo_set = False
Expand Down Expand Up @@ -531,3 +537,31 @@ def nonnegative_int(s):
To be used as type in argparse arguments.
"""
return _nonnegative_type(s, dtype=int)

def angle_as_radians(s):
"""
Interpret argument as a string defining an angle, which will be converted
to radians and returned as float. The format can be either "<value><unit>"
(e.g. 12deg, 1rad), "<value> <unit>" (e.g. 12 deg, 1 rad) or just
"<value>", in which case the unit will be assumed to be radians.

To be used as type in argparse arguments.
"""
# if `s` converts to a float then there is no unit, so assume radians
try:
value = float(s)
warnings.warn(
f'Angle units not specified for {value}, assuming radians'
)
return value
except:
pass
# looks like we have units, so do some parsing
rematch = re.match('([0-9.e+-]+) *(deg|rad)', s)
value = float(rematch.group(1))
unit = rematch.group(2)
if unit == 'deg':
return math.radians(value)
if unit == 'rad':
return value
raise argparse.ArgumentTypeError(f'Unknown unit {unit}')
Loading