00001 import os
00002 import sys
00003 import shutil
00004 from __main__ import default
00005 from tasks import *
00006 from taskinit import *
00007 import unittest
00008 import sha
00009 import time
00010 import numpy
00011
00012 from sdsave import sdsave
00013 import asap as sd
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 class sdsave_unittest_base:
00026 """
00027 Base class for testing classes.
00028 Implements several methods to compare the results.
00029 """
00030 taskname='sdsave'
00031 datapath=os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/sdsave/'
00032 basefile='OrionS_rawACSmod_cal2123.asap'
00033 summaryStr = None
00034 firstSpec = None
00035 nrow = None
00036 ifno = None
00037 cycleno = None
00038 scanno = None
00039
00040 def _checkfile( self, name ):
00041 isthere=os.path.exists(name)
00042 self.assertEqual(isthere,True,
00043 msg='output file %s was not created because of the task failure'%(name))
00044
00045 def _setAttributes(self):
00046 """
00047 Set summary string from the original data.
00048 """
00049 tb.open(self.basefile)
00050
00051
00052
00053
00054 self.firstSpec=tb.getcell('SPECTRA',0)
00055 self.nrow=tb.nrows()
00056 self.scanno=tb.getcell('SCANNO',0)
00057 self.ifno=tb.getcell('IFNO',0)
00058 self.cycleno=tb.getcell('CYCLENO',0)
00059 self.npol=tb.getkeyword('nPol')
00060
00061 tb.close()
00062
00063
00064 def _compare(self,filename):
00065 """
00066 Compare results
00067
00068 - check number of rows
00069 - check first spectrum
00070 """
00071 [nrow,sp0] = self._get(filename)
00072
00073
00074 if nrow != self.nrow:
00075 return False
00076 if any((abs(self.firstSpec-sp0))>1.0e-6):
00077 return False
00078 return True
00079
00080 def _get(self,filename):
00081 """
00082 """
00083 n=None
00084 st=filename.split('.')
00085 extension=st[-1]
00086
00087 if extension == 'asap' or extension == 'ms' or extension == 'fits':
00088 self._checkfile(filename)
00089 s=sd.scantable(filename,False)
00090 n=s.nrow()
00091 sp=numpy.array(s._getspectrum(0))
00092 del s
00093 else:
00094 import commands
00095 wcout=commands.getoutput('ls '+st[0]+'*.txt'+' | wc')
00096 n=int(wcout.split()[0])*self.npol
00097 filein=st[0]+'_SCAN%d_CYCLE%d_IF%d.txt'%(self.scanno,self.cycleno,self.ifno)
00098 self._checkfile(filein)
00099 f=open(filein)
00100 sp=[]
00101 line = f.readline()
00102 while ( line != '' ):
00103 if line[0] != '#' and line[0] != 'x':
00104 lines = line.split()
00105 sp.append(float(lines[1]))
00106 line = f.readline()
00107 sp = numpy.array(sp)
00108 f.close()
00109 return [n,sp]
00110
00111
00112
00113
00114
00115 class sdsave_test0(unittest.TestCase,sdsave_unittest_base):
00116 """
00117 Test on data selection, data averaging...
00118 """
00119
00120 infile='OrionS_rawACSmod_cal2123.asap'
00121 prefix=sdsave_unittest_base.taskname+'Test0'
00122 outfile=prefix+'.asap'
00123
00124 def setUp(self):
00125 self.res=None
00126 if (not os.path.exists(self.infile)):
00127 shutil.copytree(self.datapath+self.infile, self.infile)
00128
00129 default(sdsave)
00130
00131 def tearDown(self):
00132 if (os.path.exists(self.infile)):
00133 shutil.rmtree(self.infile)
00134 os.system( 'rm -rf '+self.prefix+'*' )
00135
00136 def test000(self):
00137 """Test 000: Default parameters"""
00138
00139 self.res=sdsave()
00140 self.assertFalse(self.res)
00141
00142 def test001(self):
00143 """Test 001: Time averaging without weight"""
00144 try:
00145 self.res=sdsave(infile=self.infile,timeaverage=True,outfile=self.outfile)
00146 self.assertTrue(False,
00147 msg='The task must throw exception')
00148 except Exception, e:
00149 pos=str(e).find('Please specify weight type of time averaging')
00150 self.assertNotEqual(pos,-1,
00151 msg='Unexpected exception was thrown: %s'%(str(e)))
00152
00153 def test002(self):
00154 """Test 002: Polarization averaging without weight"""
00155 try:
00156 self.res=sdsave(infile=self.infile,polaverage=True,outfile=self.outfile)
00157 self.assertTrue(False,
00158 msg='The task must throw exception')
00159 except Exception, e:
00160 pos=str(e).find('Please specify weight type of polarization averaging')
00161 self.assertNotEqual(pos,-1,
00162 msg='Unexpected exception was thrown: %s'%(str(e)))
00163
00164
00165
00166
00167
00168 class sdsave_test1(unittest.TestCase,sdsave_unittest_base):
00169 """
00170 Read scantable data, write various types of format.
00171 """
00172
00173 infile='OrionS_rawACSmod_cal2123.asap'
00174 prefix=sdsave_unittest_base.taskname+'Test1'
00175 outfile0=prefix+'.asap'
00176 outfile1=prefix+'.ms'
00177 outfile2=prefix+'.fits'
00178 outfile3=prefix
00179
00180 def setUp(self):
00181 self.res=None
00182 if (not os.path.exists(self.infile)):
00183 shutil.copytree(self.datapath+self.infile, self.infile)
00184 if (not os.path.exists(self.basefile)):
00185 shutil.copytree(self.datapath+self.basefile, self.basefile)
00186
00187 default(sdsave)
00188 self._setAttributes()
00189
00190 def tearDown(self):
00191 if (os.path.exists(self.infile)):
00192 shutil.rmtree(self.infile)
00193 if (os.path.exists(self.basefile)):
00194 shutil.rmtree(self.basefile)
00195 os.system( 'rm -rf '+self.prefix+'*' )
00196
00197 def test100(self):
00198 """Test 100: test to read scantable and to write as scantable"""
00199 self.res=sdsave(infile=self.infile,outfile=self.outfile0,outform='ASAP')
00200 self.assertEqual(self.res,None)
00201 self.assertTrue(self._compare(self.outfile0))
00202
00203 def test101(self):
00204 """Test 101: test to read scantable and to write as MS"""
00205 self.res=sdsave(infile=self.infile,outfile=self.outfile1,outform='MS2')
00206 self.assertEqual(self.res,None)
00207 self.assertTrue(self._compare(self.outfile1))
00208
00209 def test102(self):
00210 """Test 102: test to read scantable and to write as SDFITS"""
00211 self.res=sdsave(infile=self.infile,outfile=self.outfile2,outform='SDFITS')
00212 self.assertEqual(self.res,None)
00213 self.assertTrue(self._compare(self.outfile2))
00214
00215 def test103(self):
00216 """Test 103: test to read scantable and to write as ASCII"""
00217 self.res=sdsave(infile=self.infile,outfile=self.outfile3,outform='ASCII')
00218 self.assertEqual(self.res,None)
00219 self.assertTrue(self._compare(self.outfile3))
00220
00221
00222
00223
00224
00225 class sdsave_test2(unittest.TestCase,sdsave_unittest_base):
00226 """
00227 Read MS data, write various types of format.
00228 """
00229
00230 infile='OrionS_rawACSmod_cal2123.ms'
00231 prefix=sdsave_unittest_base.taskname+'Test2'
00232 outfile0=prefix+'.asap'
00233 outfile1=prefix+'.ms'
00234 outfile2=prefix+'.fits'
00235 outfile3=prefix
00236
00237 def setUp(self):
00238 self.res=None
00239 if (not os.path.exists(self.infile)):
00240 shutil.copytree(self.datapath+self.infile, self.infile)
00241 if (not os.path.exists(self.basefile)):
00242 shutil.copytree(self.datapath+self.basefile, self.basefile)
00243
00244 default(sdsave)
00245 self._setAttributes()
00246 self.scanno=0
00247
00248 def tearDown(self):
00249 if (os.path.exists(self.infile)):
00250 shutil.rmtree(self.infile)
00251 if (os.path.exists(self.basefile)):
00252 shutil.rmtree(self.basefile)
00253 os.system( 'rm -rf '+self.prefix+'*' )
00254
00255 def test200(self):
00256 """Test 200: test to read MS and to write as scantable"""
00257 self.res=sdsave(infile=self.infile,outfile=self.outfile0,outform='ASAP')
00258 self.assertEqual(self.res,None)
00259 self.assertTrue(self._compare(self.outfile0))
00260
00261 def test201(self):
00262 """Test 201: test to read MS and to write as MS"""
00263 self.res=sdsave(infile=self.infile,outfile=self.outfile1,outform='MS2')
00264 self.assertEqual(self.res,None)
00265 self.assertTrue(self._compare(self.outfile1))
00266
00267 def test202(self):
00268 """Test 202: test to read MS and to write as SDFITS"""
00269 self.res=sdsave(infile=self.infile,outfile=self.outfile2,outform='SDFITS')
00270 self.assertEqual(self.res,None)
00271 self.assertTrue(self._compare(self.outfile2))
00272
00273 def test203(self):
00274 """Test 203: test to read MS and to write as ASCII"""
00275 self.res=sdsave(infile=self.infile,outfile=self.outfile3,outform='ASCII')
00276 self.assertEqual(self.res,None)
00277 self.assertTrue(self._compare(self.outfile3))
00278
00279 def test204(self):
00280 """Test 204: test failure case that unexisting antenna is specified"""
00281 try:
00282 self.res=sdsave(infile=self.infile,antenna='ROSWELL',outfile=self.outfile0,outform='ASAP')
00283 self.assertTrue(False,
00284 msg='The task must throw exception')
00285 except Exception, e:
00286
00287 pos=str(e).find('Failed to open file')
00288 self.assertNotEqual(pos,-1,
00289 msg='Unexpected exception was thrown: %s'%(str(e)))
00290
00291 def test205(self):
00292 """Test 205: test to read USB spectral window"""
00293 self.__spwtest()
00294
00295 def test206(self):
00296 """Test 206: test to read LSB spectral window"""
00297 tb.open('%s/SPECTRAL_WINDOW'%(self.infile),nomodify=False)
00298 chanw = tb.getcol('CHAN_WIDTH')
00299 chanf = tb.getcol('CHAN_FREQ')
00300 chanw *= -1.0
00301 chanf = numpy.flipud(chanf)
00302 tb.putcol('CHAN_WIDTH',chanw)
00303 tb.putcol('CHAN_FREQ',chanf)
00304 netsb = numpy.ones( tb.nrows(), int )
00305 tb.putcol('NET_SIDEBAND', netsb )
00306 tb.close()
00307 self.__spwtest()
00308
00309 def __spwtest(self):
00310 self.res=sdsave(infile=self.infile,outfile=self.outfile0,outform='ASAP')
00311 self.assertFalse(self.res,False)
00312 self.__compareIncrement( self.outfile0, self.infile )
00313 self.res=sdsave(infile=self.outfile0,outfile=self.outfile1,outform='MS2')
00314 self.assertFalse(self.res,False)
00315 self.__compareIncrement( self.outfile0, self.outfile1 )
00316
00317 def __compareIncrement(self,stdata,msdata):
00318 tb.open('%s/FREQUENCIES'%(stdata))
00319 incr=tb.getcol('INCREMENT')
00320 tb.close()
00321 tb.open('%s/SPECTRAL_WINDOW'%(msdata))
00322 chanw=tb.getcol('CHAN_WIDTH')
00323 tb.close()
00324 for i in xrange(len(incr)):
00325
00326 self.assertEqual(incr[i],chanw[0][i])
00327
00328
00329
00330
00331 class sdsave_test3(unittest.TestCase,sdsave_unittest_base):
00332 """
00333 Read ATNF SDFITS data, write various types of format.
00334 """
00335
00336 infile='OrionS_rawACSmod_cal2123.fits'
00337 prefix=sdsave_unittest_base.taskname+'Test3'
00338 outfile0=prefix+'.asap'
00339 outfile1=prefix+'.ms'
00340 outfile2=prefix+'.fits'
00341 outfile3=prefix
00342
00343 def setUp(self):
00344 self.res=None
00345 if (not os.path.exists(self.infile)):
00346 shutil.copy(self.datapath+self.infile, self.infile)
00347 if (not os.path.exists(self.basefile)):
00348 shutil.copytree(self.datapath+self.basefile, self.basefile)
00349
00350 default(sdsave)
00351 self._setAttributes()
00352 self.scanno=0
00353
00354 def tearDown(self):
00355 if (os.path.exists(self.infile)):
00356 os.system( 'rm -f '+self.infile )
00357 if (os.path.exists(self.basefile)):
00358 shutil.rmtree(self.basefile)
00359 os.system( 'rm -rf '+self.prefix+'*' )
00360
00361 def test300(self):
00362 """Test 300: test to read ATNF SDFITS and to write as scantable"""
00363 self.res=sdsave(infile=self.infile,outfile=self.outfile0,outform='ASAP')
00364 self.assertEqual(self.res,None)
00365 self.assertTrue(self._compare(self.outfile0))
00366
00367 def test301(self):
00368 """Test 301: test to read ATNF SDFITS and to write as MS"""
00369 self.res=sdsave(infile=self.infile,outfile=self.outfile1,outform='MS2')
00370 self.assertEqual(self.res,None)
00371 self.assertTrue(self._compare(self.outfile1))
00372
00373 def test302(self):
00374 """Test 302: test to read ATNF SDFITS and to write as SDFITS"""
00375 self.res=sdsave(infile=self.infile,outfile=self.outfile2,outform='SDFITS')
00376 self.assertEqual(self.res,None)
00377 self.assertTrue(self._compare(self.outfile2))
00378
00379 def test303(self):
00380 """Test 303: test to read ATNF SDFITS and to write as ASCII"""
00381 self.res=sdsave(infile=self.infile,outfile=self.outfile3,outform='ASCII')
00382 self.assertEqual(self.res,None)
00383 self.assertTrue(self._compare(self.outfile3))
00384
00385
00386
00387
00388
00389 class sdsave_test4(unittest.TestCase,sdsave_unittest_base):
00390 """
00391 Read GBT SDFITS data, write various types of format.
00392 """
00393
00394 infile='AGBT06A_sliced.fits'
00395 prefix=sdsave_unittest_base.taskname+'Test4'
00396 outfile0=prefix+'.asap'
00397 outfile1=prefix+'.ms'
00398 outfile2=prefix+'.fits'
00399 outfile3=prefix
00400
00401 def setUp(self):
00402 self.res=None
00403 if (not os.path.exists(self.infile)):
00404 shutil.copy(self.datapath+self.infile, self.infile)
00405
00406 default(sdsave)
00407
00408
00409 def tearDown(self):
00410 if (os.path.exists(self.infile)):
00411 os.system( 'rm -f '+self.infile )
00412 os.system( 'rm -rf '+self.prefix+'*' )
00413
00414 def test400(self):
00415 """Test 400: test to read GBT SDFITS and to write as scantable"""
00416 self.res=sdsave(infile=self.infile,outfile=self.outfile0,outform='ASAP')
00417 self.assertEqual(self.res,None)
00418
00419 self.assertTrue(self._compare())
00420
00421 def test401(self):
00422 """Test 401: test to read GBT SDFITS and to write as MS"""
00423 self.res=sdsave(infile=self.infile,outfile=self.outfile1,outform='MS2')
00424 self.assertEqual(self.res,None)
00425
00426
00427 def test402(self):
00428 """Test 402: test to read GBT SDFITS and to write as SDFITS"""
00429 self.res=sdsave(infile=self.infile,outfile=self.outfile2,outform='SDFITS')
00430 self.assertEqual(self.res,None)
00431
00432
00433 def test403(self):
00434 """Test 403: test to read GBT SDFITS and to write as ASCII"""
00435 self.res=sdsave(infile=self.infile,outfile=self.outfile3,outform='ASCII')
00436 self.assertEqual(self.res,None)
00437
00438
00439 def _compare(self,filename=''):
00440 """
00441 Check a few things for the data.
00442 """
00443 s=sd.scantable(self.infile,False)
00444 if ( s.nrow() != 48 ):
00445 return False
00446 if ( s.nif() != 6 ):
00447 return False
00448 if ( s.nchan(0) != 4096 ):
00449 return False
00450 if ( s.nchan(2) != 8192 ):
00451 return False
00452 if ( s.npol() != 1 ):
00453 return False
00454 return True
00455
00456
00457
00458
00459 class sdsave_test5(unittest.TestCase,sdsave_unittest_base):
00460 """
00461 Read NROFITS data, write various types of format.
00462 """
00463
00464 infile='B68test.nro'
00465 prefix=sdsave_unittest_base.taskname+'Test5'
00466 outfile0=prefix+'.asap'
00467 outfile1=prefix+'.ms'
00468 outfile2=prefix+'.fits'
00469 outfile3=prefix
00470
00471 def setUp(self):
00472 self.res=None
00473 if (not os.path.exists(self.infile)):
00474 shutil.copy(self.datapath+self.infile, self.infile)
00475
00476 default(sdsave)
00477
00478
00479 def tearDown(self):
00480 if (os.path.exists(self.infile)):
00481 os.system( 'rm -f '+self.infile )
00482 os.system( 'rm -rf '+self.prefix+'*' )
00483
00484 def test500(self):
00485 """Test 500: test to read NROFITS and to write as scantable"""
00486 self.res=sdsave(infile=self.infile,outfile=self.outfile0,outform='ASAP')
00487 self.assertEqual(self.res,None)
00488
00489 self.assertTrue(self._compare())
00490
00491 def test501(self):
00492 """Test 501: test to read NROFITS and to write as MS"""
00493 self.res=sdsave(infile=self.infile,outfile=self.outfile1,outform='MS2')
00494 self.assertEqual(self.res,None)
00495
00496
00497 def test502(self):
00498 """Test 502: test to read NROFITS and to write as SDFITS"""
00499 self.res=sdsave(infile=self.infile,outfile=self.outfile2,outform='SDFITS')
00500 self.assertEqual(self.res,None)
00501
00502
00503 def test503(self):
00504 """Test 503: test to read NROFITS and to write as ASCII"""
00505 self.res=sdsave(infile=self.infile,outfile=self.outfile3,outform='ASCII')
00506 self.assertEqual(self.res,None)
00507
00508
00509 def _compare(self,filename=''):
00510 """
00511 Check a few things for the data.
00512 """
00513 s=sd.scantable(self.infile,False)
00514 if ( s.nrow() != 36 ):
00515 return False
00516 if ( s.nif() != 4 ):
00517 return False
00518 if ( s.nchan() != 2048 ):
00519 return False
00520 if ( s.npol() != 1 ):
00521 return False
00522 return True
00523
00524
00525
00526
00527
00528 class sdsave_test6( unittest.TestCase, sdsave_unittest_base ):
00529 """
00530 Test getpt parameter
00531
00532 1) import MS to Scantable format with getpt=True
00533 1-1) check POINTING table keyword is missing
00534 1-2) export Scantable to MS format
00535 1-3) compare POINTING table
00536 2) import MS to Scantable format with getpt=False
00537 1-1) check POINTING table keyword exists
00538 1-2) export Scantable to MS format
00539 1-3) compare POINTING table
00540
00541 """
00542
00543 infile='OrionS_rawACSmod_cal2123.ms'
00544 prefix=sdsave_unittest_base.taskname+'Test6'
00545 outfile0=prefix+'.asap'
00546 outfile1=prefix+'.ms'
00547
00548 def setUp(self):
00549 self.res=None
00550 if (not os.path.exists(self.infile)):
00551 shutil.copytree(self.datapath+self.infile, self.infile)
00552
00553 default(sdsave)
00554
00555
00556 def tearDown(self):
00557 if (os.path.exists(self.infile)):
00558 os.system( 'rm -rf '+self.infile )
00559 os.system( 'rm -rf '+self.prefix+'*' )
00560
00561 def test600(self):
00562 """Test 600: test getpt=True"""
00563 self.res=sdsave(infile=self.infile,getpt=True,outfile=self.outfile0,outform='ASAP')
00564 self.assertEqual(self.res,None)
00565 self.assertFalse(self._pointingKeywordExists())
00566 self.res=sdsave(infile=self.outfile0,outfile=self.outfile1,outform='MS2')
00567 self.assertTrue(self._compare())
00568
00569 def test601(self):
00570 """Test 601: test getpt=False"""
00571 self.res=sdsave(infile=self.infile,getpt=False,outfile=self.outfile0,outform='ASAP')
00572 self.assertEqual(self.res,None)
00573 self.assertTrue(self._pointingKeywordExists())
00574 self.res=sdsave(infile=self.outfile0,outfile=self.outfile1,outform='MS2')
00575 self.assertTrue(self._compare())
00576
00577 def _pointingKeywordExists(self):
00578 _tb=tbtool()
00579 _tb.open(self.outfile0)
00580 keys=_tb.getkeywords()
00581 _tb.close()
00582 del _tb
00583 return 'POINTING' in keys
00584
00585 def _compare(self):
00586 ret = True
00587 _tb1=tbtool()
00588 _tb2=tbtool()
00589 _tb1.open(self.infile)
00590
00591 ptab1=_tb1.getkeyword('POINTING').lstrip('Table: ')
00592 _tb1.close()
00593 _tb1.open(ptab1)
00594 _tb2.open(self.outfile1)
00595
00596 ptab2=_tb2.getkeyword('POINTING').lstrip('Table: ')
00597 _tb2.close()
00598 _tb2.open(ptab1)
00599 badcols = []
00600 for col in _tb1.colnames():
00601 if not all(_tb1.getcol(col).flatten()==_tb2.getcol(col).flatten()):
00602 badcols.append( col )
00603 _tb1.close()
00604 _tb2.close()
00605 del _tb1, _tb2
00606 if len(badcols) != 0:
00607 print 'Bad column: %s'%(badcols)
00608 ret = False
00609 return ret
00610
00611
00612
00613
00614 class sdsave_test7( sdsave_unittest_base, unittest.TestCase ):
00615 """
00616 Unit tests for task sdsave. Test scantable available restfreq
00617
00618 The list of tests:
00619 test701-704 --- a value (float, int, quantity w/ and w/o a unit)
00620 test711-715 --- a list [length=1]
00621 (float, int, quantity w/ and w/o a unit, dict)
00622 test721-725 --- a list [length>1]
00623 (float, int, quantity w/ and w/o a unit, dict)
00624 test731-733 --- a bad quantity unit (a value, a list)
00625 """
00626
00627 infile = 'OrionS_rawACSmod_cal2123.asap'
00628 outname = sdsave_unittest_base.taskname+'_test'
00629 iflist = [1,2]
00630 frf = [45.301e9,44.075e9]
00631 irf = [45301000000,44075000000]
00632 qurf = ['45301.MHz','44.075GHz']
00633 qrf = [str(frf[0]), str(irf[1])]
00634 drf = [{'name': "IF1 Rest", 'value': frf[0]}, \
00635 {'name': "IF2 Rest", 'value': qurf[1]}]
00636 badq = ['45301.km','44.075bad']
00637
00638 def setUp( self ):
00639
00640 if os.path.exists(self.infile):
00641 shutil.rmtree(self.infile)
00642 shutil.copytree(self.datapath+self.infile, self.infile)
00643
00644 default(sdsave)
00645
00646 def tearDown( self ):
00647 if (os.path.exists(self.infile)):
00648 shutil.rmtree(self.infile)
00649
00650
00651
00652 def _check_restfreq( self, outfile, restfreq, iflist ):
00653 self._checkfile(outfile)
00654
00655 if not (type(restfreq) in (list, tuple, numpy.ndarray)):
00656 restfreq = [restfreq] * len(iflist)
00657 elif len(restfreq) == 1:
00658 restfreq = restfreq * len(iflist)
00659
00660 scan = sd.scantable(outfile, average = False)
00661 for i in range(len(restfreq)):
00662 ifno = iflist[i]
00663 rf = restfreq[i]
00664 if type(rf) == dict:
00665 rf = rf['value']
00666 if qa.isquantity(rf):
00667 rfv = qa.convert(rf,'Hz')['value']
00668 else:
00669 rfv = float(rf)
00670 scan.set_selection(ifs=[ifno])
00671 molid = scan.getmolnos()
00672 self.assertTrue(len(molid)==1,msg="IFNO=%d has multiple MOLECULE_IDs: %s" % (ifno,str(molid)))
00673 newrf = scan.get_restfreqs(molid[0])
00674
00675 self.assertEqual(newrf[0], rfv,\
00676 msg="Rest frequency differs(IFNO=%d): %f (expected: %f)" % (ifno, newrf[0], rfv))
00677 print "Rest frequency (IFNO=%d): %f (expected: %f)" % (ifno, newrf[0], rfv)
00678
00679
00680 def test701( self ):
00681 """Test 701: restfreq (a float value)"""
00682 tid = "701"
00683 infile = self.infile
00684 outfile = self.outname+tid
00685 iflist = self.iflist
00686 restfreq = self.frf[1]
00687
00688 print "Setting restfreq = %s (%s)" % (str(restfreq), str(type(restfreq)))
00689 result = sdsave(infile=infile,outfile=outfile,\
00690 scanaverage=False,timeaverage=False,polaverage=False,\
00691 iflist=iflist,restfreq=restfreq)
00692
00693 self.assertEqual(result,None)
00694 self.assertTrue(os.path.exists(outfile),msg="No output written")
00695 print "Testing rest frequencies of output scantable"
00696 self._check_restfreq(outfile, restfreq, iflist)
00697
00698 def test702( self ):
00699 """Test 702: restfreq (an int value)"""
00700 tid = "702"
00701 infile = self.infile
00702 outfile = self.outname+tid
00703 iflist = self.iflist
00704 restfreq = self.irf[1]
00705
00706 print "Setting restfreq = %s (%s)" % (str(restfreq), str(type(restfreq)))
00707 result = sdsave(infile=infile,outfile=outfile,\
00708 scanaverage=False,timeaverage=False,polaverage=False,\
00709 iflist=iflist,restfreq=restfreq)
00710
00711 self.assertEqual(result,None)
00712 self.assertTrue(os.path.exists(outfile),msg="No output written")
00713 print "Testing rest frequencies of output scantable"
00714 self._check_restfreq(outfile, restfreq, iflist)
00715
00716 def test703( self ):
00717 """Test 703: restfreq (a quantity with unit)"""
00718 tid = "703"
00719 infile = self.infile
00720 outfile = self.outname+tid
00721 iflist = self.iflist
00722 restfreq = self.qurf[1]
00723
00724 print "Setting restfreq = %s" % (str(restfreq))
00725 result = sdsave(infile=infile,outfile=outfile,\
00726 scanaverage=False,timeaverage=False,polaverage=False,\
00727 iflist=iflist,restfreq=restfreq)
00728
00729 self.assertEqual(result,None)
00730 self.assertTrue(os.path.exists(outfile),msg="No output written")
00731 print "Testing rest frequencies of output scantable"
00732 self._check_restfreq(outfile, restfreq, iflist)
00733
00734 def test704( self ):
00735 """Test 704: restfreq (a quantity withOUT unit)"""
00736 tid = "704"
00737 infile = self.infile
00738 outfile = self.outname+tid
00739 iflist = self.iflist
00740 restfreq = self.qrf[1]
00741
00742 print "Setting restfreq = %s (%s)" % (str(restfreq), str(type(restfreq)))
00743 result = sdsave(infile=infile,outfile=outfile,\
00744 scanaverage=False,timeaverage=False,polaverage=False,\
00745 iflist=iflist,restfreq=restfreq)
00746
00747 self.assertEqual(result,None)
00748 self.assertTrue(os.path.exists(outfile),msg="No output written")
00749 print "Testing rest frequencies of output scantable"
00750 self._check_restfreq(outfile, restfreq, iflist)
00751
00752 def test711( self ):
00753 """Test 711: restfreq (a list of float value [length = 1])"""
00754 tid = "711"
00755 infile = self.infile
00756 outfile = self.outname+tid
00757 iflist = self.iflist
00758 restfreq = [ self.frf[1] ]
00759
00760 print "Setting restfreq = %s" % (str(restfreq))
00761 result = sdsave(infile=infile,outfile=outfile,\
00762 scanaverage=False,timeaverage=False,polaverage=False,\
00763 iflist=iflist,restfreq=restfreq)
00764
00765 self.assertEqual(result,None)
00766 self.assertTrue(os.path.exists(outfile),msg="No output written")
00767 print "Testing rest frequencies of output scantable"
00768 self._check_restfreq(outfile, restfreq, iflist)
00769
00770 def test712( self ):
00771 """Test 712: restfreq (a list of int value [length = 1])"""
00772 tid = "712"
00773 infile = self.infile
00774 outfile = self.outname+tid
00775 iflist = self.iflist
00776 restfreq = [ self.irf[1] ]
00777
00778 print "Setting restfreq = %s" % (str(restfreq))
00779 result = sdsave(infile=infile,outfile=outfile,\
00780 scanaverage=False,timeaverage=False,polaverage=False,\
00781 iflist=iflist,restfreq=restfreq)
00782
00783 self.assertEqual(result,None)
00784 self.assertTrue(os.path.exists(outfile),msg="No output written")
00785 print "Testing rest frequencies of output scantable"
00786 self._check_restfreq(outfile, restfreq, iflist)
00787
00788 def test713( self ):
00789 """Test 713: restfreq (a list of quantity with unit [length = 1])"""
00790 tid = "713"
00791 infile = self.infile
00792 outfile = self.outname+tid
00793 iflist = self.iflist
00794 restfreq = [ self.qurf[1] ]
00795
00796 print "Setting restfreq = %s" % (str(restfreq))
00797 result = sdsave(infile=infile,outfile=outfile,\
00798 scanaverage=False,timeaverage=False,polaverage=False,\
00799 iflist=iflist,restfreq=restfreq)
00800
00801 self.assertEqual(result,None)
00802 self.assertTrue(os.path.exists(outfile),msg="No output written")
00803 print "Testing rest frequencies of output scantable"
00804 self._check_restfreq(outfile, restfreq, iflist)
00805
00806 def test714( self ):
00807 """Test 714: restfreq (a list of quantity withOUT unit [length = 1])"""
00808 tid = "714"
00809 infile = self.infile
00810 outfile = self.outname+tid
00811 iflist = self.iflist
00812 restfreq = [ self.qrf[1] ]
00813
00814 print "Setting restfreq = %s" % (str(restfreq))
00815 result = sdsave(infile=infile,outfile=outfile,\
00816 scanaverage=False,timeaverage=False,polaverage=False,\
00817 iflist=iflist,restfreq=restfreq)
00818
00819 self.assertEqual(result,None)
00820 self.assertTrue(os.path.exists(outfile),msg="No output written")
00821 print "Testing rest frequencies of output scantable"
00822 self._check_restfreq(outfile, restfreq, iflist)
00823
00824 def test715( self ):
00825 """Test 715: restfreq (a list of dictionary [length = 1])"""
00826 tid = "715"
00827 infile = self.infile
00828 outfile = self.outname+tid
00829 iflist = self.iflist
00830 restfreq = [ self.drf[1] ]
00831
00832 print "Setting restfreq = %s" % (str(restfreq))
00833 result = sdsave(infile=infile,outfile=outfile,\
00834 scanaverage=False,timeaverage=False,polaverage=False,\
00835 iflist=iflist,restfreq=restfreq)
00836
00837 self.assertEqual(result,None)
00838 self.assertTrue(os.path.exists(outfile),msg="No output written")
00839 print "Testing rest frequencies of output scantable"
00840 self._check_restfreq(outfile, restfreq, iflist)
00841
00842 def test721( self ):
00843 """Test 721: restfreq (a list of float value [length > 1])"""
00844 tid = "721"
00845 infile = self.infile
00846 outfile = self.outname+tid
00847 iflist = self.iflist
00848 restfreq = self.frf
00849
00850 print "Setting restfreq = %s" % (str(restfreq))
00851 result = sdsave(infile=infile,outfile=outfile,\
00852 scanaverage=False,timeaverage=False,polaverage=False,\
00853 iflist=iflist,restfreq=restfreq)
00854
00855 self.assertEqual(result,None)
00856 self.assertTrue(os.path.exists(outfile),msg="No output written")
00857 print "Testing rest frequencies of output scantable"
00858 self._check_restfreq(outfile, restfreq, iflist)
00859
00860 def test722( self ):
00861 """Test 722: restfreq (a list of int value [length > 1])"""
00862 tid = "722"
00863 infile = self.infile
00864 outfile = self.outname+tid
00865 iflist = self.iflist
00866 restfreq = self.irf
00867
00868 print "Setting restfreq = %s" % (str(restfreq))
00869 result = sdsave(infile=infile,outfile=outfile,\
00870 scanaverage=False,timeaverage=False,polaverage=False,\
00871 iflist=iflist,restfreq=restfreq)
00872
00873 self.assertEqual(result,None)
00874 self.assertTrue(os.path.exists(outfile),msg="No output written")
00875 print "Testing rest frequencies of output scantable"
00876 self._check_restfreq(outfile, restfreq, iflist)
00877
00878 def test723( self ):
00879 """Test 723: restfreq (a list of quantity with unit [length > 1])"""
00880 tid = "723"
00881 infile = self.infile
00882 outfile = self.outname+tid
00883 iflist = self.iflist
00884 restfreq = self.qurf
00885
00886 print "Setting restfreq = %s" % (str(restfreq))
00887 result = sdsave(infile=infile,outfile=outfile,\
00888 scanaverage=False,timeaverage=False,polaverage=False,\
00889 iflist=iflist,restfreq=restfreq)
00890
00891 self.assertEqual(result,None)
00892 self.assertTrue(os.path.exists(outfile),msg="No output written")
00893 print "Testing rest frequencies of output scantable"
00894 self._check_restfreq(outfile, restfreq, iflist)
00895
00896 def test724( self ):
00897 """Test 724: restfreq (a list of quantity withOUT unit [length > 1])"""
00898 tid = "724"
00899 infile = self.infile
00900 outfile = self.outname+tid
00901 iflist = self.iflist
00902 restfreq = self.qrf
00903
00904 print "Setting restfreq = %s" % (str(restfreq))
00905 result = sdsave(infile=infile,outfile=outfile,\
00906 scanaverage=False,timeaverage=False,polaverage=False,\
00907 iflist=iflist,restfreq=restfreq)
00908
00909 self.assertEqual(result,None)
00910 self.assertTrue(os.path.exists(outfile),msg="No output written")
00911 print "Testing rest frequencies of output scantable"
00912 self._check_restfreq(outfile, restfreq, iflist)
00913
00914 def test725( self ):
00915 """Test 725: restfreq (a list of dictionary [length > 1])"""
00916 tid = "725"
00917 infile = self.infile
00918 outfile = self.outname+tid
00919 iflist = self.iflist
00920 restfreq = self.drf
00921
00922 print "Setting restfreq = %s" % (str(restfreq))
00923 result = sdsave(infile=infile,outfile=outfile,\
00924 scanaverage=False,timeaverage=False,polaverage=False,\
00925 iflist=iflist,restfreq=restfreq)
00926
00927 self.assertEqual(result,None)
00928 self.assertTrue(os.path.exists(outfile),msg="No output written")
00929 print "Testing rest frequencies of output scantable"
00930 self._check_restfreq(outfile, restfreq[0], iflist)
00931
00932 def test731( self ):
00933 """Test 731: restfreq (a BAD quantity unit)"""
00934 tid = "731"
00935 infile = self.infile
00936 outfile = self.outname+tid
00937 iflist = self.iflist
00938 restfreq = self.badq[0]
00939
00940 print "Setting restfreq = %s" % (str(restfreq))
00941 try:
00942 result = sdsave(infile=infile,outfile=outfile,\
00943 scanaverage=False,timeaverage=False,polaverage=False,\
00944 iflist=iflist,restfreq=restfreq)
00945 self.assertTrue(False,
00946 msg='The task must throw exception')
00947 except Exception, e:
00948 pos=str(e).find('wrong unit of restfreq')
00949 self.assertNotEqual(pos,-1,
00950 msg='Unexpected exception was thrown: %s'%(str(e)))
00951
00952 def test732( self ):
00953 """Test 732: restfreq (a list of BAD quantity unit [length = 1])"""
00954 tid = "732"
00955 infile = self.infile
00956 outfile = self.outname+tid
00957 iflist = self.iflist
00958 restfreq = [ self.badq[1] ]
00959
00960 print "Setting restfreq = %s" % (str(restfreq))
00961 try:
00962 result = sdsave(infile=infile,outfile=outfile,\
00963 scanaverage=False,timeaverage=False,polaverage=False,\
00964 iflist=iflist,restfreq=restfreq)
00965 self.assertTrue(False,
00966 msg='The task must throw exception')
00967 except Exception, e:
00968 pos=str(e).find('Input value is not a quantity: ')
00969 self.assertNotEqual(pos,-1,
00970 msg='Unexpected exception was thrown: %s'%(str(e)))
00971
00972
00973 def test733( self ):
00974 """Test 733: restfreq (a list of BAD quantity unit [length > 1])"""
00975 tid = "733"
00976 infile = self.infile
00977 outfile = self.outname+tid
00978 iflist = self.iflist
00979 restfreq = self.badq
00980
00981 print "Setting restfreq = %s" % (str(restfreq))
00982 try:
00983 result = sdsave(infile=infile,outfile=outfile,\
00984 scanaverage=False,timeaverage=False,polaverage=False,\
00985 iflist=iflist,restfreq=restfreq)
00986
00987 self.assertTrue(False,
00988 msg='The task must throw exception')
00989 except Exception, e:
00990 pos=str(e).find('wrong unit of restfreq')
00991 self.assertNotEqual(pos,-1,
00992 msg='Unexpected exception was thrown: %s'%(str(e)))
00993
00994
00995
00996
00997
00998 class sdsave_storageTest( sdsave_unittest_base, unittest.TestCase ):
00999 """
01000 Unit tests for task sdsave. Test scantable sotrage and insitu
01001 parameters
01002
01003 The list of tests:
01004 testMT --- storage = 'memory', insitu = True
01005 testMF --- storage = 'memory', insitu = False
01006 testDT --- storage = 'disk', insitu = True
01007 testDF --- storage = 'disk', insitu = False
01008
01009 Note on handlings of disk storage:
01010 Task script restores MOLECULE_ID column.
01011
01012 Tested items:
01013 1. Number of rows in tables and list of IDs of output scantable.
01014 2. Units and coordinates of output scantable.
01015 3. units and coordinates of input scantables before/after run.
01016 """
01017
01018 infile = 'OrionS_rawACSmod_cal2123.asap'
01019 outname = sdsave_unittest_base.taskname+'_test'
01020 pollist = [1]
01021 iflist = [2]
01022 restfreq = [44.075e9]
01023
01024 refout = {"nRow": 8, "SCANNOS": [21,23], "POLNOS": pollist,\
01025 "IFNOS": iflist, "MOLNOS": [1], "RestFreq": restfreq}
01026
01027 def setUp( self ):
01028
01029 if os.path.exists(self.infile):
01030 shutil.rmtree(self.infile)
01031 shutil.copytree(self.datapath+self.infile, self.infile)
01032
01033 self.storage = sd.rcParams['scantable.storage']
01034 self.insitu = sd.rcParams['insitu']
01035
01036 default(sdsave)
01037
01038 def tearDown( self ):
01039
01040 sd.rcParams['scantable.storage'] = self.storage
01041 sd.rcParams['insitu'] = self.insitu
01042 if (os.path.exists(self.infile)):
01043 shutil.rmtree(self.infile)
01044
01045
01046
01047 def _get_scantable_params( self, scanname ):
01048 self._checkfile(scanname)
01049 res = {}
01050 testvals = ["scannos", "polnos", "ifnos", "molnos"]
01051 scan = sd.scantable(scanname,average=False)
01052 res['nRow'] = scan.nrow()
01053 for val in testvals:
01054 res[val.upper()] = getattr(scan,"get"+val)()
01055
01056 rflist = []
01057 for molno in res["MOLNOS"]:
01058 rflist.append(scan.get_restfreqs(molno)[0])
01059 res["RestFreq"] = rflist
01060 del scan
01061 return res
01062
01063 def _compare_scantable_params( self, test , refval):
01064 if type(test) == str:
01065 testval = self._get_scantable_params(test)
01066 elif type(test) == dict:
01067 testval = test
01068 else:
01069 msg = "Invalid test value (should be either dict or file name)."
01070 raise Exception, msg
01071
01072
01073 if not type(refval) == dict:
01074 raise Exception, "The reference data should be a dictionary"
01075 for key, rval in refval.iteritems():
01076 if not testval.has_key(key):
01077 raise KeyError, "Test data does not have key, '%s'" % key
01078 if type(rval) in [list, tuple, numpy.ndarray]:
01079 self.assertEqual(len(testval[key]), len(rval), \
01080 msg = "Number of elements in '%s' differs." % key)
01081 for i in range(len(rval)):
01082 rv = rval[i]
01083 if type(rv) == float:
01084 self.assertAlmostEqual(testval[key][i], rv, \
01085 msg = "%s[%d] differs: %s (expected: %s) "\
01086 % (key, i, str(testval[key][i]), str(rv)))
01087 else:
01088 self.assertEqual(testval[key][i], rv, \
01089 msg = "%s[%d] differs: %s (expected: %s) "\
01090 % (key, i, str(testval[key][i]), str(rv)))
01091 else:
01092 if type(rval) == float:
01093 self.assertAlmostEqual(testval[key], rval, \
01094 msg = "%s differs: %s (expected: %s)" \
01095 % (key, str(testval[key]), rval))
01096 else:
01097 self.assertEqual(testval[key], rval, \
01098 msg = "%s differs: %s (expected: %s)" \
01099 % (key, str(testval[key]), rval))
01100
01101
01102
01103 def testMT( self ):
01104 """Storage Test MT: sdsave on storage='memory' and insitu=T"""
01105 tid = "MT"
01106 infile = self.infile
01107 outfile = self.outname+tid
01108 iflist = self.iflist
01109 pollist = self.pollist
01110 restfreq = self.restfreq
01111
01112
01113 initval = self._get_scantable_params(infile)
01114
01115 sd.rcParams['scantable.storage'] = 'memory'
01116 sd.rcParams['insitu'] = True
01117 print "Running test with storage='%s' and insitu=%s" % \
01118 (sd.rcParams['scantable.storage'], str(sd.rcParams['insitu']))
01119 result = sdsave(infile=infile,outfile=outfile,\
01120 scanaverage=False,timeaverage=False,polaverage=False,\
01121 iflist=iflist,pollist=pollist,restfreq=restfreq)
01122
01123 self.assertEqual(result,None)
01124 self.assertTrue(os.path.exists(outfile),msg="No output written")
01125 print "Testing output scantable"
01126 self._compare_scantable_params(outfile,self.refout)
01127
01128 print "Comparing input scantable before/after run"
01129 self._compare_scantable_params(infile,initval)
01130
01131
01132 def testMF( self ):
01133 """Storage Test MF: sdsave on storage='memory' and insitu=F"""
01134 tid = "MF"
01135 infile = self.infile
01136 outfile = self.outname+tid
01137 iflist = self.iflist
01138 pollist = self.pollist
01139 restfreq = self.restfreq
01140
01141
01142 initval = self._get_scantable_params(infile)
01143
01144 sd.rcParams['scantable.storage'] = 'memory'
01145 sd.rcParams['insitu'] = False
01146 print "Running test with storage='%s' and insitu=%s" % \
01147 (sd.rcParams['scantable.storage'], str(sd.rcParams['insitu']))
01148 result = sdsave(infile=infile,outfile=outfile,\
01149 scanaverage=False,timeaverage=False,polaverage=False,\
01150 iflist=iflist,pollist=pollist,restfreq=restfreq)
01151
01152 self.assertEqual(result,None)
01153 self.assertTrue(os.path.exists(outfile),msg="No output written")
01154 print "Testing output scantable"
01155 self._compare_scantable_params(outfile,self.refout)
01156
01157 print "Comparing input scantable before/after run"
01158 self._compare_scantable_params(infile,initval)
01159
01160
01161 def testDT( self ):
01162 """Storage Test DT: sdsave on storage='disk' and insitu=T"""
01163 tid = "DT"
01164 infile = self.infile
01165 outfile = self.outname+tid
01166 iflist = self.iflist
01167 pollist = self.pollist
01168 restfreq = self.restfreq
01169
01170
01171 initval = self._get_scantable_params(infile)
01172
01173 sd.rcParams['scantable.storage'] = 'disk'
01174 sd.rcParams['insitu'] = True
01175 print "Running test with storage='%s' and insitu=%s" % \
01176 (sd.rcParams['scantable.storage'], str(sd.rcParams['insitu']))
01177 result = sdsave(infile=infile,outfile=outfile,\
01178 scanaverage=False,timeaverage=False,polaverage=False,\
01179 iflist=iflist,pollist=pollist,restfreq=restfreq)
01180
01181 self.assertEqual(result,None)
01182 self.assertTrue(os.path.exists(outfile),msg="No output written")
01183 print "Testing output scantable"
01184 self._compare_scantable_params(outfile,self.refout)
01185
01186 print "Comparing input scantable before/after run"
01187 self._compare_scantable_params(infile,initval)
01188
01189
01190 def testDF( self ):
01191 """Storage Test DF: sdsave on storage='disk' and insitu=F"""
01192 tid = "DF"
01193 infile = self.infile
01194 outfile = self.outname+tid
01195 iflist = self.iflist
01196 pollist = self.pollist
01197 restfreq = self.restfreq
01198
01199
01200 initval = self._get_scantable_params(infile)
01201
01202 sd.rcParams['scantable.storage'] = 'disk'
01203 sd.rcParams['insitu'] = False
01204 print "Running test with storage='%s' and insitu=%s" % \
01205 (sd.rcParams['scantable.storage'], str(sd.rcParams['insitu']))
01206 result = sdsave(infile=infile,outfile=outfile,\
01207 scanaverage=False,timeaverage=False,polaverage=False,\
01208 iflist=iflist,pollist=pollist,restfreq=restfreq)
01209
01210 self.assertEqual(result,None)
01211 self.assertTrue(os.path.exists(outfile),msg="No output written")
01212 print "Testing output scantable"
01213 self._compare_scantable_params(outfile,self.refout)
01214
01215 print "Comparing input scantable before/after run"
01216 self._compare_scantable_params(infile,initval)
01217
01218
01219
01220 def suite():
01221 return [sdsave_test0,sdsave_test1,sdsave_test2,
01222 sdsave_test3,sdsave_test4,sdsave_test5,
01223 sdsave_test6,sdsave_test7,sdsave_storageTest]