casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
test_ia_rebin.py
Go to the documentation of this file.
00001 ##########################################################################
00002 # imfit_test.py
00003 #
00004 # Copyright (C) 2008, 2009
00005 # Associated Universities, Inc. Washington DC, USA.
00006 #
00007 # This script is free software; you can redistribute it and/or modify it
00008 # under the terms of the GNU Library General Public License as published by
00009 # the Free Software Foundation; either version 2 of the License, or (at your
00010 # option) any later version.
00011 #
00012 # This library is distributed in the hope that it will be useful, but WITHOUT
00013 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00015 # License for more details.
00016 #
00017 # You should have received a copy of the GNU Library General Public License
00018 # along with this library; if not, write to the Free Software Foundation,
00019 # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00020 #
00021 # Correspondence concerning AIPS++ should be adressed as follows:
00022 #        Internet email: aips2-request@nrao.edu.
00023 #        Postal address: AIPS++ Project Office
00024 #                        National Radio Astronomy Observatory
00025 #                        520 Edgemont Road
00026 #                        Charlottesville, VA 22903-2475 USA
00027 #
00028 # <author>
00029 # Dave Mehringer
00030 # </author>
00031 #
00032 # <summary>
00033 # Test suite for the CASA tool method ia.rebin()
00034 # </summary>
00035 #
00036 # <reviewed reviwer="" date="" tests="" demos="">
00037 # </reviewed
00038 #
00039 # <prerequisite>
00040 # <ul>
00041 # </ul>
00042 # </prerequisite>
00043 #
00044 # <etymology>
00045 # Test for the ia.rebin() tool method
00046 # </etymology>
00047 #
00048 # <synopsis>
00049 # Test the ia.rebin() tool method
00050 # </synopsis> 
00051 #
00052 # <example>
00053 #
00054 # This test runs as part of the CASA python unit test suite and can be run from
00055 # the command line via eg
00056 # 
00057 # `echo $CASAPATH/bin/casapy | sed -e 's$ $/$'` --nologger --log2term -c `echo $CASAPATH | awk '{print $1}'`/code/xmlcasa/scripts/regressions/admin/runUnitTest.py test_ia_rebin[test1,test2,...]
00058 #
00059 # </example>
00060 #
00061 # <motivation>
00062 # To provide a test standard for the ia.rebin() tool method to ensure
00063 # coding changes do not break the associated bits 
00064 # </motivation>
00065 #
00066 
00067 ###########################################################################
00068 import shutil
00069 import casac
00070 from tasks import *
00071 from taskinit import *
00072 from __main__ import *
00073 import unittest
00074 
00075 def alleqnum(x,num,tolerance=0):
00076     if len(x.shape)==1:
00077         for i in range(x.shape[0]):
00078             if not (abs(x[i]-num) < tolerance):
00079                 print "x[",i,"]=", x[i]
00080                 return false
00081     if len(x.shape)==2:
00082         for i in range(x.shape[0]):
00083             for j in range(x.shape[1]):
00084                 if not (abs(x[i][j]-num) < tolerance):
00085                     print "x[",i,"][",j,"]=", x[i][j]
00086                     return false
00087     if len(x.shape)==3:
00088         for i in range(x.shape[0]):
00089             for j in range(x.shape[1]):
00090                 for k in range(x.shape[2]):
00091                     if not (abs(x[i][j][k]-num) < tolerance):
00092                         print "x[",i,"][",j,"][",k,"]=", x[i][j][k]
00093                         return false
00094     if len(x.shape)==4:
00095         for i in range(x.shape[0]):
00096             for j in range(x.shape[1]):
00097                 for k in range(x.shape[2]):
00098                     for l in range(x.shape[3]):
00099                         if not (abs(x[i][j][k][l]-num) < tolerance):
00100                             print "x[",i,"][",j,"][",k,"][",l,"]=", x[i][j][k]
00101                             return false
00102     if len(x.shape)>4:
00103         stop('unhandled array shape in alleq')
00104     return true
00105 
00106 
00107 class ia_rebin_test(unittest.TestCase):
00108     
00109     def setUp(self):
00110         self._myia = iatool()
00111     
00112     def tearDown(self):
00113         self._myia.done()
00114     
00115     def test_stretch(self):
00116         """ ia.rebin(): Test stretch parameter"""
00117         yy = iatool()
00118         mymask = "maskim"
00119         yy.fromshape(mymask, [200, 200, 1, 1])
00120         yy.addnoise()
00121         yy.done()
00122         shape = [200,200,1,20]
00123         yy.fromshape("", shape)
00124         yy.addnoise()
00125         self.assertRaises(
00126             Exception,
00127             yy.rebin, outfile="", bin=[2,2,1,1],
00128             mask=mymask + ">0", stretch=False
00129         )
00130         zz = yy.rebin(
00131             outfile="", bin=[2,2,1,1],
00132             mask=mymask + ">0", stretch=True
00133         )
00134         self.assertTrue(type(zz) == type(yy))
00135         yy.done()
00136         zz.done()
00137         
00138     def test_general(self):
00139         """ ia.rebin(): General tests"""
00140         # tests moved from imagetest_regression.py and modified
00141         
00142         myia = self._myia
00143         shp2 = [20,40]
00144         d2 = myia.makearray(1.0, [shp2[0], shp2[1]])
00145         #
00146         myim2 = myia.newimagefromarray(pixels=d2)
00147         self.assertTrue(myim2)
00148         
00149         try:
00150             myim2b = true
00151             myim2b = myim2.rebin(bin=[-100,2])
00152         except Exception, e:
00153             myim2b = false
00154         self.assertFalse(myim2b)
00155         
00156         myim2b = myim2.rebin("",bin=[2,2])
00157         self.assertTrue(myim2b)
00158         p = myim2b.getchunk()
00159         self.assertTrue(alleqnum(p,1.0,tolerance=0.0001))
00160         #
00161         ok = myim2.done() and myim2b.done()
00162         self.assertTrue(ok)
00163         
00164     def test_multibeam(self):
00165         """Test multiple beams"""
00166         myia = self._myia
00167         myia.fromshape("", [10, 10, 10])
00168         myia.setrestoringbeam(
00169             major="4arcsec", minor="2arcsec", pa="0deg",
00170             channel=0, polarization=0
00171         )
00172         rebin = myia.rebin("", [2, 2, 1])
00173         self.assertTrue(rebin)
00174         exception = False
00175         try:
00176             rebin = myia.rebin("", [2, 2, 2])
00177         except:
00178             exception = True
00179         self.assertTrue(exception)
00180         rebin.done()
00181             
00182 def suite():
00183     return [ia_rebin_test]