casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
test_setjy.py
Go to the documentation of this file.
00001 import shutil
00002 import unittest
00003 import os
00004 import filecmp
00005 import numpy
00006 from tasks import *
00007 from taskinit import *
00008 from __main__ import default
00009 """
00010 Unit tests for task setjy.
00011 
00012 Features tested:
00013   1. Does setjy(modimage=modelimu, fluxdensity=0) NOT scale the model image's
00014      flux density?
00015   2. Does setjy(modimage=modelimu) scale the model image's flux density?
00016   3. Solar system (Uranus) flux density calibration.
00017 """
00018 
00019 datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/'
00020 
00021 class SetjyUnitTestBase(unittest.TestCase):
00022 
00023     def setUpMS(self,MS):
00024 
00025         self.inpms = MS
00026 
00027         # Create working area
00028         if not os.path.exists('unittest/setjy'):
00029             print "\nCreate working area..."
00030             os.system('mkdir -p unittest/setjy')
00031 
00032         # Create a new fresh copy of the MS
00033         print "\nCreate a new local copy of the MS..."
00034         os.system('cp -rf ' + os.environ.get('CASAPATH').split()[0] + "/data/regression/" + self.inpms + ' unittest/setjy/')
00035 
00036     def resetMS(self):
00037 
00038         if os.path.exists(self.inpms):
00039             print "\nRemove local copy of MS from previous test..."
00040             ret = os.system('rm -rf unittest/setjy/*')
00041 
00042     def get_last_history_line(self,vis, origin='setjy::imager::setjy()',
00043                               nback=0, maxnback=20, hint=''):
00044         """
00045         Finding the right history line is a bit tricky...it helps to filter
00046         by origin and read from the back to remain unaffected by changes
00047         elsewhere.
00048         
00049         This reads up to maxnback lines with origin origin until it finds one
00050         including hint in the message, going backwards from nback lines from the
00051         end.
00052         
00053         Returns 'JUNK' on failure.
00054         """
00055         retline = 'JUNK'
00056         try:
00057             tblocal = tbtool()
00058             tblocal.open(vis + '/HISTORY')
00059             st = tblocal.query('ORIGIN == "%s"' % origin, columns='MESSAGE')
00060             nstrows = st.nrows()
00061             startrow = st.nrows() - 1 - nback
00062             # don't go back more than selected rows
00063             if maxnback > nstrows:
00064                 maxnback = nstrows - 1
00065             stoprow = startrow - maxnback
00066             print hint
00067             for linenum in xrange(startrow, stoprow - 1, -1):
00068                 curline = st.getcell('MESSAGE', linenum)
00069                 if hint in curline:
00070                     retline = curline
00071                     break
00072             st.close()
00073             tblocal.close()
00074         except Exception, e:
00075             print "\nError getting last history line"
00076             tblocal.close()
00077             raise e
00078 
00079         return retline
00080 
00081     def check_history(self,histline, items):
00082         isok = True
00083         for item in items:
00084             if item not in histline:
00085                 isok = False
00086                 break
00087         if not isok:
00088             errmsg = "%s not found in %s.\n" % (items, histline)
00089             errmsg += "It could be that a change to HISTORY caused the wrong line to be selected."
00090             raise AssertionError, errmsg
00091         return isok
00092 
00093     def check_eq(self,val, expval, tol=None):
00094         """Checks that val matches expval within tol."""
00095         if type(val) == dict:
00096             for k in val:
00097                 check_eq(val[k], expval[k], tol)
00098         else:
00099             try:
00100                 if tol and hasattr(val, '__rsub__'):
00101                     are_eq = abs(val - expval) < tol
00102                 else:
00103                     are_eq = val == expval
00104                 if hasattr(are_eq, 'all'):
00105                     are_eq = are_eq.all()
00106                 if not are_eq:
00107                     raise ValueError, '!='
00108             except ValueError:
00109                 errmsg = "%r != %r" % (val, expval)
00110                 if (len(errmsg) > 66): # 66 = 78 - len('ValueError: ')
00111                     errmsg = "\n%r\n!=\n%r" % (val, expval)
00112                 raise ValueError, errmsg
00113             except Exception, e:
00114                 print "Error comparing", val, "to", expval
00115                 raise e
00116 
00117 
00118 class test_SingleObservation(SetjyUnitTestBase):
00119     """Test single observation MS"""
00120 
00121     def setUp(self):
00122         self.setUpMS("unittest/setjy/2528.ms")         # Uranus
00123 
00124     def tearDown(self):
00125         self.resetMS()
00126 
00127     def test1_SingleObservationOldModel(self):
00128         """ Test vs an MS with one single observation using the Butler-JPL-Horizons 2010 model"""
00129 
00130         os.system("mv " + self.inpms + " " + self.inpms + ".test1")
00131         self.inpms += ".test1"
00132         record = {}
00133 
00134         tblocal = tbtool()
00135         tblocal.open(self.inpms)
00136         cols = tblocal.colnames()
00137         tblocal.close()
00138         if 'MODEL_DATA' in cols:
00139             raise ValueError, "The input MS, " + self.inpms + " already has a MODEL_DATA col" + str(cols)
00140 
00141         try:
00142             print "\nRunning setjy(field='Uranus')."
00143             sjran = setjy(vis=self.inpms, field='Uranus', spw='', modimage='',
00144                           scalebychan=False, fluxdensity=-1,
00145                           standard='Butler-JPL-Horizons 2010', usescratch=True)
00146         except Exception, e:
00147             print "\nError running setjy(field='Uranus')"
00148             raise e
00149 
00150         try:
00151             tblocal.open(self.inpms)
00152             cols = tblocal.colnames()
00153             if 'MODEL_DATA' not in cols:
00154                 raise AssertionError, "setjy(field='Uranus') did not add a MODEL_DATA column"
00155             else:
00156                 record['wvr'] = tblocal.getcell('MODEL_DATA', 0)
00157                 record['auto3'] = tblocal.getcell('MODEL_DATA', 10)
00158                 record['long3'] = tblocal.getcell('MODEL_DATA', 11)
00159                 record['auto4'] = tblocal.getcell('MODEL_DATA', 2)
00160                 record['med4'] = tblocal.getcell('MODEL_DATA', 4)
00161                 record['long4'] = tblocal.getcell('MODEL_DATA', 3)
00162                 tblocal.close()
00163                 record['history'] = self.get_last_history_line(self.inpms, hint='Uranus')
00164                 self.result = record
00165         except AssertionError, e:
00166             print "\nError accesing MODEL_DATA"
00167             tblocal.close()
00168             raise e
00169 
00170         """Flux density in HISTORY (Uranus)?"""
00171         self.check_history(self.result['history'], ["Uranus", "V=0] Jy"])
00172         """WVR spw"""
00173         self.check_eq(self.result['wvr'], numpy.array([[26.40653229+0.j,26.40653229+0.j]]),0.0001)
00174         """Zero spacing of spw 3"""
00175         self.check_eq(self.result['auto3'], numpy.array([[65.80638885+0.j],[65.80638885+0.j]]),0.0001)
00176         """Long spacing of spw 3"""
00177         self.check_eq(self.result['long3'], numpy.array([[4.76111794+0.j],[4.76111794+0.j]]),0.0001)
00178         """Zero spacing of spw 4"""
00179         self.check_eq(self.result['auto4'], numpy.array([[69.33396912+0.j],[69.33396912+0.j]]),0.0001)
00180         """Medium spacing of spw 4"""
00181         self.check_eq(self.result['med4'], numpy.array([[38.01076126+0.j],[38.01076126+0.j]]),0.0001)
00182         """Long spacing of spw 4"""
00183         self.check_eq(self.result['long4'], numpy.array([[2.83933783+0.j],[2.83933783+0.j]]),0.0001)
00184 
00185         return sjran
00186 
00187     def test2_SingleObservationSelectByChan(self):
00188         """ Test vs an MS with one single observation using the Butler-JPL-Horizons 2010 model and selecting by channel"""
00189 
00190         os.system("mv " + self.inpms + " " + self.inpms + ".test2")
00191         self.inpms += ".test2"
00192         record = {}
00193 
00194         tblocal = tbtool()
00195         tblocal.open(self.inpms)
00196         cols = tblocal.colnames()
00197         tblocal.close()
00198         if 'MODEL_DATA' in cols:
00199             raise ValueError, "The input MS, " + self.inpms + " already has a MODEL_DATA col" + str(cols)
00200 
00201         try:
00202             print "\nRunning setjy(field='Uranus')."
00203             sjran = setjy(vis=self.inpms, field='Uranus', spw='', modimage='',
00204                           scalebychan=True, fluxdensity=-1,
00205                           standard='Butler-JPL-Horizons 2010', usescratch=True)
00206         except Exception, e:
00207             print "\nError running setjy(field='Uranus')"
00208             raise e
00209         try:
00210             tblocal.open(self.inpms)
00211             cols = tblocal.colnames()
00212             if 'MODEL_DATA' not in cols:
00213                 raise AssertionError, "setjy(field='Uranus') did not add a MODEL_DATA column"
00214             else:
00215                 record['wvr'] = tblocal.getcell('MODEL_DATA', 0)
00216                 record['auto1'] = tblocal.getcell('MODEL_DATA', 18)
00217                 record['long1'] = tblocal.getcell('MODEL_DATA', 19)
00218                 record['auto4'] = tblocal.getcell('MODEL_DATA', 2)
00219                 record['long4'] = tblocal.getcell('MODEL_DATA', 3)
00220                 tblocal.close()
00221                 record['history'] = self.get_last_history_line(self.inpms, hint="V=0] Jy")
00222                 self.result = record
00223         except AssertionError, e:
00224             print "\nError accesing MODEL_DATA"
00225             tblocal.close()
00226             raise e
00227 
00228         """Flux density in HISTORY (scalebychan)?"""
00229         self.check_history(self.result['history'], ["Uranus", "V=0] Jy"])
00230         """WVR spw with scalebychan"""
00231         self.check_eq(self.result['wvr'], numpy.array([[25.93320656+0.j,
00232                                                         26.88228607+0.j]]),
00233                  0.003)
00234         """Zero spacing of spw 1 with scalebychan"""
00235         # 8 (decreasing freq!) chans, XX & YY.
00236         self.check_eq(self.result['auto1'],
00237                  numpy.array([[65.49415588+0.j, 65.42105865+0.j,
00238                                65.34798431+0.j, 65.27491760+0.j,
00239                                65.20187378+0.j, 65.12883759+0.j,
00240                                65.05581665+0.j, 64.98281097+0.j],
00241                               [65.49415588+0.j, 65.42105865+0.j,
00242                                65.34798431+0.j, 65.27491760+0.j,
00243                                65.20187378+0.j, 65.12883759+0.j,
00244                                65.05581665+0.j, 64.98281097+0.j]]),0.0001)
00245         """Long spacing of spw 1 with scalebychan"""
00246         self.check_eq(self.result['long1'],
00247                  numpy.array([[4.92902184+0.j, 4.96826363+0.j,
00248                                5.00747252+0.j, 5.04664850+0.j,
00249                                5.08579159+0.j, 5.12490082+0.j,
00250                                5.16397619+0.j, 5.20301771+0.j],
00251                               [4.92902184+0.j, 4.96826363+0.j,
00252                                5.00747252+0.j, 5.04664850+0.j,
00253                                5.08579159+0.j, 5.12490082+0.j,
00254                                5.16397619+0.j, 5.20301771+0.j]]),0.0001)
00255         # spw 4 only has 1 chan, so it should be the same as without scalebychan.
00256         """Zero spacing of spw 4 with scalebychan"""
00257         self.check_eq(self.result['auto4'], numpy.array([[69.33396912+0.j],[69.33396912+0.j]]),0.0001)
00258         """Long spacing of spw 4 with scalebychan"""
00259         self.check_eq(self.result['long4'], numpy.array([[2.83933783+0.j],[2.83933783+0.j]]),0.0001)
00260 
00261         return sjran
00262 
00263     def test3_SingleObservationNewModel(self):
00264         """ Test vs an MS with one single observation using the Butler-JPL-Horizons 2012 model"""
00265 
00266         # print out some values for debugging
00267         debug=False
00268 
00269         os.system("mv " + self.inpms + " " + self.inpms + ".test3")
00270         self.inpms += ".test3"
00271         record = {}
00272 
00273         tblocal = tbtool()
00274         tblocal.open(self.inpms)
00275         cols = tblocal.colnames()
00276         tblocal.close()
00277         if 'MODEL_DATA' in cols:
00278             raise ValueError, "The input MS, " + self.inpms + " already has a MODEL_DATA col" + str(cols)
00279 
00280         try:
00281             print "\nRunning setjy(field='Uranus')."
00282             sjran = setjy(vis=self.inpms, field='Uranus', spw='', modimage='',
00283                           scalebychan=False, fluxdensity=-1,
00284                           standard='Butler-JPL-Horizons 2012', usescratch=True)
00285         except Exception, e:
00286             print "\nError running setjy(field='Uranus')"
00287             raise e
00288         try:
00289             tblocal.open(self.inpms)
00290             cols = tblocal.colnames()
00291             if 'MODEL_DATA' not in cols:
00292                 raise AssertionError, "setjy(field='Uranus') did not add a MODEL_DATA column"
00293             else:
00294                 record['wvr'] = tblocal.getcell('MODEL_DATA', 0)
00295                 record['auto3'] = tblocal.getcell('MODEL_DATA', 10)
00296                 record['long3'] = tblocal.getcell('MODEL_DATA', 11)
00297                 record['auto4'] = tblocal.getcell('MODEL_DATA', 2)
00298                 record['med4'] = tblocal.getcell('MODEL_DATA', 4)
00299                 record['long4'] = tblocal.getcell('MODEL_DATA', 3)
00300                 tblocal.close()
00301                 record['history'] = self.get_last_history_line(self.inpms, origin='setjy', hint='Uranus')
00302                 self.result = record
00303         except AssertionError, e:
00304             print "\nError accesing MODEL_DATA"
00305             tblocal.close()
00306             raise e
00307 
00308         if debug:
00309           print "self.result['history']=",self.result['history']
00310           print "self.result['wvr']=",self.result['wvr']
00311           print "self.result['auto3']=",self.result['auto3']
00312           print "self.result['auto4']=",self.result['auto4']
00313 
00314         """Flux density in HISTORY (Uranus)?"""
00315         self.check_history(self.result['history'], ["Uranus:", "V=0.0] Jy"])
00316         """WVR spw"""
00317         #self.check_eq(self.result['wvr'], numpy.array([[ 25.33798409+0.j,25.33798409+0.j]]),0.0001)
00318         # new value after code and ephemeris data update 2012-10-03
00319         self.check_eq(self.result['wvr'], numpy.array([[ 25.33490372+0.j, 25.33490372+0.j]]),0.0001)
00320         """Zero spacing of spw 3"""
00321         #self.check_eq(self.result['auto3'], numpy.array([[ 66.72530365+0.j],[ 66.72530365+0.j]]),0.0001)
00322         # new value after code and ephemeris data update 2012-10-03
00323         self.check_eq(self.result['auto3'], numpy.array([[ 66.71941376+0.j], [ 66.71941376+0.j]]),0.0001)
00324         """Zero spacing of spw 4"""
00325         #self.check_eq(self.result['auto4'], numpy.array([[ 70.40153503+0.j],[ 70.40153503+0.j]]),0.0001)
00326         # new value after code and ephemeris data update 2012-10-03
00327         self.check_eq(self.result['auto4'], numpy.array([[ 70.39561462+0.j], [ 70.39561462+0.j]]), 0.0001)
00328 
00329         return sjran
00330 
00331 
00332 class test_MultipleObservations(SetjyUnitTestBase):
00333     """Test multiple observation MS (CAS-3320)"""
00334 
00335     def setUp(self):
00336         self.setUpMS("unittest/setjy/multiobs.ms")         # Titan
00337 
00338     def tearDown(self):
00339         self.resetMS()
00340 
00341     def test1_MultipleObservationOldModel(self):
00342         """ Test vs an MS with multiple observations using the Butler-JPL-Horizons 2010 model"""
00343 
00344         os.system("mv " + self.inpms + " " + self.inpms + ".test1")
00345         self.inpms += ".test1"
00346         record = {}
00347 
00348         tblocal = tbtool()
00349         tblocal.open(self.inpms)
00350         cols = tblocal.colnames()
00351         tblocal.close()
00352         if 'MODEL_DATA' in cols:
00353             raise ValueError, "The input MS, " + self.inpms + " already has a MODEL_DATA col" + str(cols)
00354 
00355         try:
00356             print "\nRunning setjy(field='Uranus')."
00357             sjran = setjy(self.inpms, field='Titan', spw='',
00358                           selectdata=True, observation=1, 
00359                           modimage='',
00360                           scalebychan=False, fluxdensity=-1,
00361                           standard='Butler-JPL-Horizons 2010', usescratch=True)
00362         except Exception, e:
00363             print "\nError running setjy(field='Uranus')"
00364             raise e
00365 
00366         try:
00367             tblocal.open(self.inpms)
00368             cols = tblocal.colnames()
00369             if 'MODEL_DATA' not in cols:
00370                 raise AssertionError, "setjy(field='Uranus') did not add a MODEL_DATA column"
00371             else:
00372                 record[0] = tblocal.getcell('MODEL_DATA', 0)[0, 0]
00373                 record[1] = tblocal.getcell('MODEL_DATA', 666)[0]
00374                 record[2] = tblocal.getcell('MODEL_DATA', 950)[0, 0]
00375                 tblocal.close()
00376                 self.result = record
00377         except AssertionError, e:
00378             print "\nError accesing MODEL_DATA"
00379             tblocal.close()
00380             raise e
00381 
00382         """Was obsID 0 left alone?"""
00383         self.check_eq(self.result[0], 1.0+0.0j, 0.003)
00384         """Was obsID 1 set?"""
00385         self.check_eq(self.result[1],
00386                  numpy.array([1.40439999+0.j, 1.40436542+0.j,
00387                               1.40433097+0.j, 1.40429640+0.j]), 0.003)
00388         """Was obsID 2 left alone?"""
00389         self.check_eq(self.result[2], 1.0+0.0j, 0.003)
00390 
00391     def test2_MultipleObservationOldModel(self):
00392         """ Test vs an MS with multiple observations using the Butler-JPL-Horizons 2012 model"""
00393 
00394         os.system("mv " + self.inpms + " " + self.inpms + ".test2")
00395         self.inpms += ".test2"
00396         record = {}
00397 
00398         tblocal = tbtool()
00399         tblocal.open(self.inpms)
00400         cols = tblocal.colnames()
00401         tblocal.close()
00402         if 'MODEL_DATA' in cols:
00403             raise ValueError, "The input MS, " + self.inpms + " already has a MODEL_DATA col" + str(cols)
00404 
00405         try:
00406             print "\nRunning setjy(field='Uranus')."
00407             sjran = setjy(self.inpms, field='Titan', spw='',
00408                           selectdata=True, observation=1, 
00409                           modimage='',
00410                           scalebychan=False, fluxdensity=-1,
00411                           standard='Butler-JPL-Horizons 2012', usescratch=True)
00412         except Exception, e:
00413             print "\nError running setjy(field='Uranus')"
00414             raise e
00415 
00416         try:
00417             tblocal.open(self.inpms)
00418             cols = tblocal.colnames()
00419             if 'MODEL_DATA' not in cols:
00420                 raise AssertionError, "setjy(field='Uranus') did not add a MODEL_DATA column"
00421             else:
00422                 record[0] = tblocal.getcell('MODEL_DATA', 0)[0, 0]
00423                 record[1] = tblocal.getcell('MODEL_DATA', 666)[0]
00424                 record[1] = tblocal.getcell('MODEL_DATA', 671)[0]
00425                 record[2] = tblocal.getcell('MODEL_DATA', 950)[0, 0]
00426                 tblocal.close()
00427                 self.result = record
00428         except AssertionError, e:
00429             print "\nError accesing MODEL_DATA"
00430             tblocal.close()
00431             raise e
00432 
00433         """Was obsID 0 left alone?"""
00434         self.check_eq(self.result[0], 1.0+0.0j, 0.003)
00435         """Was obsID 1 set?"""
00436         self.check_eq(self.result[1],
00437         #        numpy.array([ 1.21551239-0.33617234j,  1.19003308-0.41755155j,
00438         #                      1.15911222-0.49702403j,  1.12289071-0.57422638j]),
00439                 numpy.array([ 1.26114714+0.j,  1.26116526+0.j, 1.26118350+0.j,  1.26120162+0.j]),
00440                               0.003)
00441         """Was obsID 2 left alone?"""
00442         self.check_eq(self.result[2], 1.0+0.0j, 0.003)
00443 
00444 
00445 class test_ModImage(SetjyUnitTestBase):
00446 
00447     def setUp(self):
00448 
00449         self.inpuvf = datapath + '/ATST2/NGC1333/N1333_1.UVFITS'
00450         self.inpms = 'unittest/setjy/n1333_1.ms'
00451         self.field = '0542+498_1'
00452         self.modelim = '3C147_U.im'
00453         self.result = {}
00454 
00455         if not os.path.exists(self.inpuvf):
00456             raise EnvironmentError, "Missing input UVFITS file: " + datapath + self.inpuvf
00457 
00458         try:
00459             print "Importing", self.inpuvf, "to an MS."
00460             importuvfits(fitsfile=self.inpuvf, vis=self.inpms,antnamescheme="new")
00461         except Exception, e:
00462             print "importuvfits error:"
00463             raise e
00464     
00465     def test1_UBandModelQithQBandMS(self):
00466         """ Test U-Band model with W-Band data to see impact of flus density scale """
00467 
00468         # The MS is in Q band, so deliberately choose the U band model so that the structure
00469         # is not too far off, but whether or not its flux density is scaled makes a difference.
00470 
00471         for use_standard in [False, True]:
00472             self.result[use_standard] = self.run_setjy(use_standard)
00473 
00474         self.result['fluxdens'] = self.run_setjy(False, 1234.0)
00475         self.result['spix'] = self.run_setjy(False,1234.0 * (43.42064/35.0)**0.7,-0.7,"35.0GHz")
00476 
00477         """Flux density in HISTORY (standard)?"""
00478         # Don't bother checking it without scaling - it won't be there and isn't interesting.
00479         self.check_history(self.result[True]['history'],["Scaling spw 1's model image to I ="])
00480         """Flux density in HISTORY (fluxdensity)?"""
00481         self.check_history(self.result['fluxdens']['history'],["Scaling spw 1's model image to I ="])
00482         """Flux density in HISTORY (fluxdensity)?"""
00483         self.check_history(self.result['fluxdens']['history'],["Scaling spw 1's model image to I ="])
00484         """Flux density in HISTORY (spix)?"""
00485         self.check_history(self.result['spix']['history'],["Scaling spw 1's model image to I ="])
00486         """modimage != '' and fluxdensity == 0 -> no scaling?"""
00487         self.check_eq(self.result[False]['short'], 2.712631, 0.05)
00488         self.check_eq(self.result[False]['long'],  2.4080808, 0.05)
00489         """modimage != '' and default fluxdensity -> scaling?"""
00490         self.check_eq(self.result[True]['short'], 0.911185, 0.025)
00491         self.check_eq(self.result[True]['long'],  0.808885, 0.025)
00492         """modimage != '' and fluxdensity > 0"""
00493         self.check_eq(self.result['fluxdens']['short'], 1233.7, 0.05)
00494         self.check_eq(self.result['fluxdens']['long'],  1095.2, 0.05)
00495         """modimage != '', fluxdensity > 0, and spix = -0.7"""
00496         self.check_eq(self.result['spix']['short'], 1233.7, 0.5)
00497         self.check_eq(self.result['spix']['long'],  1095.2, 0.5)
00498 
00499         return True
00500 
00501     def run_setjy(self, use_standard, fluxdens=0, spix=0, reffreq="1GHz"):
00502         record = {'setjyran': False}
00503         try:
00504             if use_standard:
00505                 record['setjyran'] = setjy(vis=self.inpms, field=self.field,
00506                                            modimage=self.modelim,
00507                                            scalebychan=False,
00508                                            standard='Perley-Taylor 99',
00509                                            usescratch=True,
00510                                            async=False)
00511             else:
00512                 record['setjyran'] = setjy(vis=self.inpms, field=self.field,
00513                                            modimage=self.modelim,
00514                                            scalebychan=False,
00515                                            fluxdensity=fluxdens,
00516                                            spix=spix, reffreq=reffreq,
00517                                            usescratch=True,
00518                                            async=False)
00519             record['history'] = self.get_last_history_line(self.inpms,
00520                                                            origin='imager::setjy()',
00521                                                            hint='model image to I')
00522             ms.open(self.inpms)
00523             record['short'] = ms.statistics(column='MODEL',
00524                                             complex_value='amp',
00525                                             field='0542+498_1',
00526                                             baseline='2&9',
00527                                             time='2003/05/02/19:53:30.0',
00528                                             correlation='rr')['MODEL']['mean']
00529             record['long']  = ms.statistics(column='MODEL',
00530                                             complex_value='amp',
00531                                             field='0542+498_1',
00532                                             baseline='21&24',
00533                                             time='2003/05/02/19:53:30.0',
00534                                             correlation='ll')['MODEL']['mean']
00535             ms.close()
00536         except Exception, e:
00537             print "Error from setjy or ms.statistics()"
00538             raise e
00539 
00540         return record
00541     
00542 class test_inputs(SetjyUnitTestBase):
00543     """Test input parameter checking"""
00544     def setUp(self):
00545         self.setUpMS("unittest/setjy/2528.ms")         # Uranus
00546 
00547     def tearDown(self):
00548         self.resetMS()
00549 
00550     def test_vischeck(self):
00551         """ Test input vis check"""
00552         self.inpms='wrong.ms'
00553         if os.path.exists(self.inpms):
00554             shutil.rmtree(self.inpms) 
00555 
00556 
00557         # test by temporarily setting __rethrow_casa_exceptions
00558         sjran=None
00559         try:
00560             myf = sys._getframe(len(inspect.stack()) - 1).f_globals
00561             original_rethrow_setting=myf.get('__rethrow_casa_exceptions',False)
00562             myf['__rethrow_casa_exceptions']=True
00563             print "\nRunning setjy with a non-existant vis"
00564             sjran = setjy(vis=self.inpms,listmodels=False)
00565         except Exception, setjyUTerr:
00566             msg = setjyUTerr.message
00567             self.assertNotEqual(msg.find("%s does not exist" % self.inpms), -1,
00568                                 'wrong type of exception is thrown')
00569         finally:
00570             # put back original rethrow setting
00571             myf['__rethrow_casa_exceptions']=original_rethrow_setting
00572         self.assertEqual(sjran,None,"Failed to raise exception.") 
00573      
00574     def test_listmodels(self):
00575         """ Test listmodels mode """
00576         self.inpms=''
00577         print "\nRunning setjy in listmodels mode ...."
00578         sjran = setjy(vis=self.inpms,listmodels=True)
00579         self.assertTrue(sjran)
00580       
00581         
00582   
00583 def suite():
00584     return [test_SingleObservation,test_MultipleObservations,test_ModImage, test_inputs]