From 9facef292dea3e3830347f8dfdac27e8cac30ae4 Mon Sep 17 00:00:00 2001
From: Hal Simpson <hasimpson@usgs.gov>
Date: Tue, 6 Jan 2015 09:05:41 -0700
Subject: [PATCH] added all the functions needed to convert to/from XYZ from
 HEDZ.  Changed get_mag_from_obs to use H,E,Z instead of H,D,Z.  Also added a
 function to go from HEZ to HEDZ, or any combo of E,D

---
 geomagio/StreamConverter.py | 114 ++++++++++++++++++++++++++++++++----
 1 file changed, 101 insertions(+), 13 deletions(-)

diff --git a/geomagio/StreamConverter.py b/geomagio/StreamConverter.py
index 4d1fda28..a5175144 100644
--- a/geomagio/StreamConverter.py
+++ b/geomagio/StreamConverter.py
@@ -13,7 +13,29 @@ import ChannelConverter
 
 
 def get_geo_from_mag(mag):
-    pass
+    """Convert a stream to geographic coordinate system.
+
+    Parameters
+    ----------
+    stream : obspy.core.Stream
+        stream containing observatory components H, D, Z, and F.
+
+    Returns
+    -------
+    obspy.core.Stream
+        new stream object containing geographic components X, Y, Z, and F.
+    """
+    h = mag.select(channel='H')[0]
+    d = mag.select(channel='D')[0]
+    z = mag.select(channel='Z')[0]
+    f = mag.select(channel='F')[0]
+    mag_h = h.data
+    mag_d = d.data
+    (geo_x, geo_y) = ChannelConverter.get_geo_from_mag(mag_h, mag_d)
+    return obspy.core.Stream((
+        __get_trace('X', h.stats, geo_x),
+        __get_trace('Y', d.stats, geo_y),
+        z, f))
 
 
 def get_geo_from_obs(obs):
@@ -33,7 +55,17 @@ def get_geo_from_obs(obs):
 
 
 def get_mag_from_geo(geo):
-    pass
+    x = geo.select(channel='X')[0]
+    y = geo.select(channel='Y')[0]
+    z = geo.select(channel='Z')[0]
+    f = geo.select(channel='F')[0]
+    geo_x = x.data
+    geo_y = y.data
+    (mag_h, mag_d) = ChannelConverter.get_mag_from_geo(geo_x, geo_y)
+    return obspy.core.Stream((
+            __get_trace('H', x.stats, mag_h),
+            __get_trace('D', y.stats, mag_d),
+            z, f))
 
 
 def get_mag_from_obs(obs):
@@ -50,22 +82,22 @@ def get_mag_from_obs(obs):
         new stream object containing magnetic components H, D, Z, and F.
     """
     h = obs.select(channel='H')[0]
-    d = __get_obs_d_from_obs(obs)
+    e = __get_obs_e_from_obs(obs)
     z = obs.select(channel='Z')[0]
     f = obs.select(channel='F')[0]
     obs_h = h.data
-    obs_d = d.data
+    obs_e = e.data
     d0 = ChannelConverter.get_radians_from_minutes(
-            numpy.float64(d.stats['DECBAS']) / 10)
-    (mag_h, mag_d) = ChannelConverter.get_mag_from_obs(obs_h, obs_d, d0)
+            numpy.float64(e.stats['DECBAS']) / 10)
+    (mag_h, mag_d) = ChannelConverter.get_mag_from_obs(obs_h, obs_e, d0)
     return obspy.core.Stream((
             __get_trace('H', h.stats, mag_h),
-            __get_trace('D', d.stats, mag_d),
+            __get_trace('D', e.stats, mag_d),
             z, f))
 
 
 def get_obs_from_geo(geo, include_e=False):
-    """Convert a stream to geographic coordinate system.
+    """Convert a stream to observatory coordinate system.
 
     Parameters
     ----------
@@ -79,15 +111,71 @@ def get_obs_from_geo(geo, include_e=False):
     obspy.core.Stream
         new stream object containing observatory components H, D, E, Z, and F.
     """
-    return get_obs_from_mag(get_mag_from_geo(geo))
+    return get_obs_from_mag(get_mag_from_geo(geo), include_e)
 
 
 def get_obs_from_mag(mag, include_e=False):
-    # TODO
-    obs = obspy.core.Stream()
+    """Convert a stream to magnetic observatory coordinate system.
+
+    Parameters
+    ----------
+    stream: obspy.core.Stream
+        stream containing magnetic components H, D, Z, and F.
+    include_e: boolean
+        whether to also include the observatory E component
+
+    Returns
+    -------
+    obspy.core.Stream
+        new stream object containing observatory components H, D, E, Z, and F
+    """
+    h = mag.select(channel='H')[0]
+    d = mag.select(channel='D')[0]
+    z = mag.select(channel='Z')[0]
+    f = mag.select(channel='F')[0]
+    mag_h = h.data
+    mag_d = d.data
+    d0 = ChannelConverter.get_radians_from_minutes(
+        numpy.float64(d.stats['DECBAS']) / 10)
+    (obs_h, obs_e) = ChannelConverter.get_obs_from_mag(mag_h, mag_d, d0)
+    obs_d = ChannelConverter.get_obs_d_from_obs(obs_h, obs_e)
+    traces = (
+        __get_trace('H', h.stats, obs_h),
+        __get_trace('D', d.stats, obs_d),
+        z, f)
+    if include_e:
+        traces = traces + (__get_trace('E', d.stats, obs_e),)
+    return obspy.core.Stream(traces)
+
+
+def get_obs_from_obs(obs, include_e=False, include_d=False):
+    """Fill in the observatory parameters as requested
+
+    Parameters
+    ----------
+    stream: obspy.core.Stream
+        stream containing the observatory components H, D of E, Z, and F.
+    include_e: boolean
+        whether to include the e component
+    include_d: boolean
+        whether to include the d component
+
+    Returns
+    -------
+    obspy.core.Stream
+        new stream object containing observatory components H, D, E, Z, and F
+    """
+    h = obs.select(channel='H')[0]
+    z = obs.select(channel='Z')[0]
+    f = obs.select(channel='F')[0]
+    traces = (h, z, f)
+    if include_d:
+        d = __get_obs_d_from_obs(obs)
+        traces = traces + (d, )
     if include_e:
-        obs += __get_obs_e_from_obs(obs)
-    return obs
+        e = __get_obs_e_from_obs(obs)
+        traces = traces + (e, )
+    return obspy.core.Stream(traces)
 
 
 def __get_trace(channel, stats, data):
-- 
GitLab