From d55efcee3a2e8d521ef5aa51d8c1df69e42ea04c Mon Sep 17 00:00:00 2001
From: David Rafferty <rafferty@strw.leidenuniv.nl>
Date: Mon, 9 Sep 2013 14:59:58 +0000
Subject: [PATCH] Task #3403: Fix to 2D images with incorrect spectral units.

---
 CEP/PyBDSM/src/python/collapse.py  | 4 ++++
 CEP/PyBDSM/src/python/functions.py | 6 ++++--
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/CEP/PyBDSM/src/python/collapse.py b/CEP/PyBDSM/src/python/collapse.py
index 8fdfe4a65b5..62d78d6df2f 100644
--- a/CEP/PyBDSM/src/python/collapse.py
+++ b/CEP/PyBDSM/src/python/collapse.py
@@ -146,6 +146,7 @@ class Op_collapse(Op):
 
       if img.opts.blank_limit is not None or check_low:
           import scipy
+          import sys
           if check_low:
               threshold = 1e-5
           else:
@@ -153,7 +154,10 @@ class Op_collapse(Op):
               mylogger.userinfo(mylog, "Blanking pixels with values "
                   "below %.1e Jy/beam" % (threshold,))
           bad = (abs(image) < threshold)
+          original_stdout = sys.stdout  # keep a reference to STDOUT
+          sys.stdout = func.NullDevice()  # redirect the real STDOUT
           count = scipy.signal.convolve2d(bad, N.ones((3, 3)), mode='same')
+          sys.stdout = original_stdout  # turn STDOUT back on
           mask_low = (count >= 5)
           if check_low:
               nlow = len(N.where(mask_low)[0])
diff --git a/CEP/PyBDSM/src/python/functions.py b/CEP/PyBDSM/src/python/functions.py
index c1d633280c8..d88f0c6622f 100755
--- a/CEP/PyBDSM/src/python/functions.py
+++ b/CEP/PyBDSM/src/python/functions.py
@@ -1209,8 +1209,10 @@ def read_image_from_file(filename, img, indir, quiet=False):
 
     # Check for incorrect spectral units. For example, "M/S" is not
     # recognized by PyWCS as velocity ("S" is actually Siemens, not
-    # seconds).
-    for i in range(naxis):
+    # seconds). Note that we check CUNIT3 and CUNIT4 even if the
+    # image has only 2 axes, as the header may still have these
+    # entries.
+    for i in range(4):
         key_val_raw = hdr.get('CUNIT' + str(i+1))
         if key_val_raw != None:
             if 'M/S' in key_val_raw or 'm/S' in key_val_raw or 'M/s' in key_val_raw:
-- 
GitLab