From d02b77e6b1fe58664237ece1c61a87c0d9121600 Mon Sep 17 00:00:00 2001
From: Jeremy Fee <jmfee@usgs.gov>
Date: Wed, 27 Jan 2016 14:35:29 -0700
Subject: [PATCH] Validate requested output channels, and raise more meaningful
 exception

---
 geomagio/edge/EdgeFactory.py        | 7 +++----
 geomagio/iaga2002/IAGA2002Writer.py | 7 ++++++-
 geomagio/pcdcp/PCDCPWriter.py       | 8 +++++++-
 3 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/geomagio/edge/EdgeFactory.py b/geomagio/edge/EdgeFactory.py
index 7b92a33ea..307275a9c 100644
--- a/geomagio/edge/EdgeFactory.py
+++ b/geomagio/edge/EdgeFactory.py
@@ -17,7 +17,7 @@ import obspy.core
 from datetime import datetime
 from obspy import earthworm
 from obspy.core import UTCDateTime
-from .. import ChannelConverter
+from .. import ChannelConverter, TimeseriesUtility
 from ..TimeseriesFactory import TimeseriesFactory
 from ..TimeseriesFactoryException import TimeseriesFactoryException
 from ..ObservatoryMetadata import ObservatoryMetadata
@@ -183,12 +183,11 @@ class EdgeFactory(TimeseriesFactory):
 
         if (starttime is None or endtime is None):
             starttime, endtime = self._get_stream_start_end_times(timeseries)
-
         for channel in channels:
             if timeseries.select(channel=channel).count() == 0:
                 raise TimeseriesFactoryException(
-                    'Missing channel "%s" for output' % channel)
-
+                    'Missing channel "%s" for output, available channels %s' %
+                    (channel, str(TimeseriesUtility.get_channels(timeseries))))
         for channel in channels:
             self._put_channel(timeseries, observatory, channel, type,
                     interval, starttime, endtime)
diff --git a/geomagio/iaga2002/IAGA2002Writer.py b/geomagio/iaga2002/IAGA2002Writer.py
index 56c510f3d..cd070075a 100644
--- a/geomagio/iaga2002/IAGA2002Writer.py
+++ b/geomagio/iaga2002/IAGA2002Writer.py
@@ -3,7 +3,7 @@ from cStringIO import StringIO
 from datetime import datetime
 import numpy
 import textwrap
-from .. import ChannelConverter
+from .. import ChannelConverter, TimeseriesUtility
 from ..TimeseriesFactoryException import TimeseriesFactoryException
 from ..Util import create_empty_trace
 import IAGA2002Parser
@@ -30,6 +30,11 @@ class IAGA2002Writer(object):
         channels: array_like
             channels to be written from timeseries object
         """
+        for channel in channels:
+            if timeseries.select(channel=channel).count() == 0:
+                raise TimeseriesFactoryException(
+                    'Missing channel "%s" for output, available channels %s' %
+                    (channel, str(TimeseriesUtility.get_channels(timeseries))))
         stats = timeseries[0].stats
         if len(channels) != 4:
             self._pad_to_four_channels(timeseries, channels)
diff --git a/geomagio/pcdcp/PCDCPWriter.py b/geomagio/pcdcp/PCDCPWriter.py
index e26c29075..3042df76e 100644
--- a/geomagio/pcdcp/PCDCPWriter.py
+++ b/geomagio/pcdcp/PCDCPWriter.py
@@ -3,7 +3,8 @@ import numpy
 import PCDCPParser
 from cStringIO import StringIO
 from datetime import datetime
-from geomagio import ChannelConverter
+from .. import ChannelConverter, TimeseriesUtility
+from ..TimeseriesFactoryException import TimeseriesFactoryException
 from obspy.core import Stream
 
 
@@ -26,6 +27,11 @@ class PCDCPWriter(object):
             channels : array_like
                 Channels to be written from timeseries object.
         """
+        for channel in channels:
+            if timeseries.select(channel=channel).count() == 0:
+                raise TimeseriesFactoryException(
+                    'Missing channel "%s" for output, available channels %s' %
+                    (channel, str(TimeseriesUtility.get_channels(timeseries))))
         stats = timeseries[0].stats
         out.write(self._format_header(stats))
         out.write(self._format_data(timeseries, channels))
-- 
GitLab