From 77f5721f3e9165179095e3177892b58d9d453469 Mon Sep 17 00:00:00 2001
From: Leo Singer <leo.singer@ligo.org>
Date: Thu, 22 Feb 2018 10:14:48 -0500
Subject: [PATCH] Clean up get_detector_pair_axis

- Fix docstrings
- Add doctest
- Delay lalsimulation import
---
 .../lalinference/bayestar/postprocess.py      | 60 ++++++++++---------
 1 file changed, 31 insertions(+), 29 deletions(-)

diff --git a/lalinference/python/lalinference/bayestar/postprocess.py b/lalinference/python/lalinference/bayestar/postprocess.py
index 1878ecac18..51abfdb348 100644
--- a/lalinference/python/lalinference/bayestar/postprocess.py
+++ b/lalinference/python/lalinference/bayestar/postprocess.py
@@ -24,15 +24,15 @@ from __future__ import print_function
 import collections
 import pkg_resources
 
+from astropy import constants
 from astropy.coordinates import (CartesianRepresentation, SkyCoord,
                                  UnitSphericalRepresentation)
 from astropy import units as u
 from astropy.wcs import WCS
 import healpy as hp
-import lal
-import lalsimulation
 import numpy as np
 from scipy.interpolate import interp1d
+import six
 
 from .. import distance
 from .. import moc
@@ -568,21 +568,32 @@ def find_greedy_credible_levels(p, ranking=None):
     return cls.reshape(p.shape)
 
 
+def _get_detector_location(ifo):
+    if isinstance(ifo, six.string_types):
+        try:
+            import lalsimulation
+        except ImportError:
+            raise RuntimeError('Looking up detectors by name '
+                               'requires the lalsimulation package.')
+        ifo = lalsimulation.DetectorPrefixToLALDetector(ifo)
+    try:
+        ifo = ifo.location
+    except AttributeError:
+        pass
+    return ifo
+
+
 def get_detector_pair_axis(ifo1, ifo2, gmst):
     """Find the sky position where the line between two detectors pierces the
     celestial sphere.
 
     Parameters
     ----------
-
-    ifo1 : str or `~lal.Detector` or `~np.ndarray`
-        The first detector; either the name of the detector (e.g. `'H1'`), or a
+    ifo1, ifo2 : str or `~lal.Detector` or `numpy.ndarray`
+        The detector positions. Can be specifed by name (e.g. `'H1'`), or by
         `lal.Detector` object (e.g., as returned by
-        `lalsimulation.DetectorPrefixToLALDetector('H1')` or the geocentric
-        Cartesian position of the detection in meters.
-
-    ifo2 : str or `~lal.Detector` or `~np.ndarray`
-        The second detector; same as described above.
+        `lalsimulation.DetectorPrefixToLALDetector('H1')` or by the geocentric
+        Cartesian position in meters.
 
     gmst : float
         The Greenwich mean sidereal time in radians, as returned by
@@ -601,28 +612,19 @@ def get_detector_pair_axis(ifo1, ifo2, gmst):
 
     light_travel_time : float
         The light travel time from `ifo1` to `ifo2` in seconds.
-    """
 
-    # Get location of detectors if ifo1, ifo2 are LAL detector structs
-    try:
-        ifo1 = lalsimulation.DetectorPrefixToLALDetector(ifo1)
-    except TypeError:
-        pass
-    try:
-        ifo1 = ifo1.location
-    except AttributeError:
-        pass
-    try:
-        ifo2 = lalsimulation.DetectorPrefixToLALDetector(ifo2)
-    except TypeError:
-        pass
-    try:
-        ifo2 = ifo2.location
-    except AttributeError:
-        pass
+    Example
+    -------
+
+    >>> ret = get_detector_pair_axis('H1', 'L1', 41758.384193753656)
+    >>> print(*np.around(ret, 5))
+    5.94546 -0.47622 0.01001
+    """
+    ifo1 = _get_detector_location(ifo1)
+    ifo2 = _get_detector_location(ifo2)
 
     n = ifo2 - ifo1
-    light_travel_time = np.sqrt(np.sum(np.square(n))) / lal.C_SI
+    light_travel_time = np.sqrt(np.sum(np.square(n))) / constants.c.value
     (theta,), (phi,) = hp.vec2ang(n)
     pole_ra = (gmst + phi) % (2 * np.pi)
     pole_dec = 0.5 * np.pi - theta
-- 
GitLab