casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
test_split.py
Go to the documentation of this file.
00001 '''
00002 Unit tests for task split.
00003 
00004 Features tested:
00005   1. Are the POLARIZATION, DATA_DESCRIPTION, and (to some extent) the
00006      SPECTRAL_WINDOW tables correct with and without correlation selection?
00007   2. Are the data shapes and values correct with and without correlation
00008      selection?
00009   3. Are the WEIGHT and SIGMA shapes and values correct with and without
00010      correlation selection?
00011   4. Is a SOURCE table with bogus entries properly handled?
00012   5. Is the STATE table properly handled?
00013   6. Are generic subtables copied over?
00014   7. Are CHAN_WIDTH and RESOLUTION properly handled in SPECTRAL_WINDOW when
00015      channels are being selected and/or averaged?
00016   8. The finer points of spw:chan selection.
00017 
00018 Note: The time_then_chan_avg regression is a more "end-to-end" test of split.
00019 '''
00020 
00021 import inspect
00022 import os
00023 import numpy
00024 import re
00025 import sys
00026 import shutil
00027 from __main__ import default
00028 from recipes.listshapes import listshapes
00029 from tasks import *
00030 from taskinit import *
00031 import unittest
00032 
00033 datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/split/'
00034 
00035 # Pick up alternative data directory to run tests on MMSs
00036 testmms = False
00037 if os.environ.has_key('TEST_DATADIR'):   
00038     testmms = True
00039     DATADIR = str(os.environ.get('TEST_DATADIR'))
00040     if os.path.isdir(DATADIR):
00041         datapath = DATADIR+'/split/'
00042 
00043 print 'split tests will use data from '+datapath         
00044 
00045 
00046 def check_eq(val, expval, tol=None):
00047     """Checks that val matches expval within tol."""
00048     if type(val) == dict:
00049         for k in val:
00050             check_eq(val[k], expval[k], tol)
00051     else:
00052         try:
00053             if tol and hasattr(val, '__rsub__'):
00054                 are_eq = abs(val - expval) < tol
00055             else:
00056                 are_eq = val == expval
00057             if hasattr(are_eq, 'all'):
00058                 are_eq = are_eq.all()
00059             if not are_eq:
00060                 raise ValueError, '!='
00061         except ValueError:
00062             errmsg = "%r != %r" % (val, expval)
00063             if (len(errmsg) > 66): # 66 = 78 - len('ValueError: ')
00064                 errmsg = "\n%r\n!=\n%r" % (val, expval)
00065             raise ValueError, errmsg
00066         except Exception, e:
00067             print "Error comparing", val, "to", expval
00068             raise e
00069 
00070 def slurp_table(tabname):
00071     """
00072     Returns a dictionary containing the CASA table tabname.  The dictionary
00073     is arranged like:
00074 
00075     {'keywords': tb.getkeywords(),
00076      'cols': {colname0, {'desc': tb.getcoldesc(colname0),
00077                          'keywords': tb.getcolkeywords(colname0),
00078                          'data': tb.getcol(colname0)},
00079               colname1, {'desc': tb.getcoldesc(colname1),
00080                              'keywords': tb.getcolkeywords(colname1),
00081                          'data': tb.getcol(colname1)},
00082               ...}}
00083     """
00084     tb.open(tabname)
00085     retval = {'keywords': tb.getkeywords(),
00086               'cols': {}}
00087     cols = tb.colnames()
00088     for col in cols:
00089         entry = {'desc': tb.getcoldesc(col),
00090                  'keywords': tb.getcolkeywords(col)}
00091         if tb.isvarcol(col):
00092             entry['data'] = tb.getvarcol(col)
00093         else:
00094             entry['data'] = tb.getcol(col)
00095         retval['cols'][col] = entry
00096     tb.close()
00097     return retval
00098     
00099 def compare_tables(tabname, exptabname, tol=None):
00100     """
00101     Raises a ValueError if the contents of tabname are not the same as those
00102     of exptabname to within tol.
00103     """
00104     exptabdict = slurp_table(exptabname)
00105     tabdict = slurp_table(tabname)
00106 
00107     if set(tabdict['keywords']) != set(exptabdict['keywords']):
00108         raise ValueError, tabname + ' and ' + exptabname + ' have different keywords'
00109     if set(tabdict['cols'].keys()) != set(exptabdict['cols'].keys()):
00110         raise ValueError, tabname + ' and ' + exptabname + ' have different columns'
00111     for col, tabentry in tabdict['cols'].iteritems():
00112         if set(tabentry['keywords']) != set(exptabdict['cols'][col]['keywords']):
00113             raise ValueError, tabname + ' and ' + exptabname + ' have different keywords for column ' + col
00114 
00115         # Check everything in the description except the data manager.
00116         for thingy in tabentry['desc']:
00117             if thingy not in ('dataManagerGroup', 'dataManagerType'):
00118                 if tabentry['desc'][thingy] != exptabdict['cols'][col]['desc'][thingy]:
00119                     raise ValueError, thingy + ' differs in the descriptions of ' + col + ' in ' + tabname + ' and ' + exptabname
00120                 
00121         check_eq(tabentry['data'], exptabdict['cols'][col]['data'])
00122 
00123 
00124 
00125 class SplitChecker(unittest.TestCase):
00126     """
00127     Base class for unit test suites that do multiple tests per split run.
00128     """
00129     # Don't setup class variables here - the children would squabble over them.
00130     #
00131     # DON'T use numtests or tests_passed as (class) variables.  One of those is
00132     # a function elsewhere in the testing framework, and the name clash will
00133     # lead to a cryptic error.
00134     #
00135     # DO define a do_split(corrsel) method in each subclass to do the work and
00136     # record the results.  Any variables that it sets for use by the tests must
00137     # be class variables, i.e. prefixed by self.__class__..  The tests,
00138     # however, will refer to them as instance variables.  Example: usually
00139     # do_split() will set self.__class__.records, and the tests will use it as
00140     # self.records.  This quirk is a result of unittest.TestCase's preference
00141     # for starting from scratch, and tearing down afterwards, for each test.
00142     # That's exactly what SplitChecker is avoiding.
00143     
00144     def setUp(self):
00145         if self.need_to_initialize:
00146             self.initialize()
00147 
00148     def tearDown(self):
00149         """
00150         Will only clean things up if all the splits have run.
00151         """
00152         #print "self.n_tests_passed:", self.n_tests_passed
00153 
00154         # Check that do_split() ran for all of the corrsels.
00155         all_ran = True
00156         for corrsel in self.corrsels:
00157             if not self.records.get(corrsel):
00158                 all_ran = False
00159 
00160         if all_ran:
00161             #print "self.inpms:", self.inpms
00162             # if inpms is local...
00163             if self.inpms[0] != '/' and os.path.exists(self.inpms):
00164                 #print "rming", self.inpms
00165                 shutil.rmtree(self.inpms, ignore_errors=True)
00166 
00167             # Counting the number of tests that have run so far seems to often
00168             # work, but not always.  I think just keeping a class variable as a
00169             # counter is not thread safe.  Fortunately the only kind of test
00170             # that needs the output MS outside of do_split is
00171             # check_subtables().  Therefore, have check_subtables() remove the
00172             # output MS at its end.
00173             ## if self.n_tests_passed == self.n_tests:
00174             ##     # Remove any remaining output MSes.
00175             ##     for corrsel in self.corrsels:
00176             ##         oms = self.records.get(corrsel, {}).get('ms', '')
00177             ##         if os.path.exists(oms):
00178             ##             print "rming", oms
00179             ##             #shutil.rmtree(oms)
00180     
00181     def initialize(self):
00182         # The realization that need_to_initialize needs to be
00183         # a class variable more or less came from
00184         # http://www.gossamer-threads.com/lists/python/dev/776699
00185         self.__class__.need_to_initialize = False
00186                 
00187         inpms = self.inpms
00188     
00189         if not os.path.exists(inpms):
00190             # Copying is technically unnecessary for split, but self.inpms is
00191             # shared by other tests, so making it readonly might break them.
00192             # Make inpms an already existing path (i.e. datapath + inpms) to
00193             # disable this copy.
00194             shutil.copytree(datapath + inpms, inpms)
00195 
00196         if not os.path.exists(inpms):
00197             raise EnvironmentError, "Missing input MS: " + datapath + inpms
00198 
00199         for corrsel in self.corrsels:
00200             self.res = self.do_split(corrsel)
00201 
00202     def check_subtables(self, corrsel, expected):
00203         """
00204         Compares the shapes of self.records[corrsel]['ms']'s subtables
00205         to the ones listed in expected.
00206 
00207         Removes self.records[corrsel]['ms'] afterwards since nothing else
00208         needs it, and this is the most reliable way to clean up.
00209         """
00210         oms = self.records[corrsel]['ms']
00211         assert listshapes(mspat=oms)[oms] == set(expected)
00212         shutil.rmtree(oms)
00213 
00214 class split_test_tav(SplitChecker):
00215     need_to_initialize = True
00216     inpms = '../../0420+417/0420+417.ms'
00217     if datapath.count('unittest_mms')==1:
00218         inpms = '0420+417.ms'
00219         
00220     corrsels = ['', 'rr, ll', 'rl, lr', 'rr', 'll']
00221     records = {}
00222     #n_tests = 20
00223     #n_tests_passed = 0
00224     
00225     def do_split(self, corrsel):
00226         outms = 'tav' + re.sub(',\s*', '', corrsel) + '.ms'
00227         record = {'ms': outms}
00228 
00229         shutil.rmtree(outms, ignore_errors=True)
00230         try:
00231             print "\nTime averaging", self.inpms, corrsel
00232             splitran = split(self.inpms, outms, datacolumn='data',
00233                              field='', spw='', width=1, antenna='',
00234                              timebin='20s', timerange='',
00235                              scan='', array='', uvrange='',
00236                              correlation=corrsel, async=False)
00237             tb.open(outms)
00238             record['data']   = tb.getcell('DATA', 2)
00239             record['weight'] = tb.getcell('WEIGHT', 5)
00240             record['sigma']  = tb.getcell('SIGMA', 7)
00241             tb.close()
00242         except Exception, e:
00243             print "Error time averaging and reading", outms
00244             raise e
00245         self.__class__.records[corrsel] = record
00246         return splitran
00247 
00248     def test_sts(self):
00249         """Subtables, time avg. without correlation selection"""
00250         self.check_subtables('', [(4, 1)])
00251         #self.__class__.n_tests_passed += 1
00252 
00253     def test_sts_rrll(self):
00254         """Subtables, time avg. RR, LL"""
00255         self.check_subtables('rr, ll', [(2, 1)])
00256         #self.__class__.n_tests_passed += 1
00257         
00258     def test_sts_rllr(self):
00259         """Subtables, time avg. RL, LR"""
00260         self.check_subtables('rl, lr', [(2, 1)])
00261         #self.__class__.n_tests_passed += 1
00262         
00263     def test_sts_rr(self):
00264         """Subtables, time avg. RR"""
00265         self.check_subtables('rr', [(1, 1)])
00266         #self.__class__.n_tests_passed += 1
00267         
00268     def test_sts_ll(self):
00269         """Subtables, time avg. LL"""
00270         self.check_subtables('ll', [(1, 1)])
00271         #self.__class__.n_tests_passed += 1
00272 
00273     ## # split does not yet return a success value, and exceptions
00274     ## # are captured.
00275     ## # But at least on June 10 it correctly exited with an error
00276     ## # msg for correlation = 'rr, rl, ll'.
00277     ## def test_abort_on_rrrlll(self):
00278     ##     """
00279     ##     Cannot slice out RR, RL, LL
00280     ##     """
00281     ##     self.assertFalse(self.doSplit('rr, rl, ll'))
00282         
00283     def test_data(self):
00284         """DATA[2],   time avg. without correlation selection"""
00285         check_eq(self.records['']['data'],
00286                  numpy.array([[ 0.14428490-0.03145669j],
00287                               [-0.00379944+0.00710297j],
00288                               [-0.00381106-0.00066403j],
00289                               [ 0.14404297-0.04763794j]]),
00290                  0.0001)
00291         #self.__class__.n_tests_passed += 1
00292         
00293     def test_data_rrll(self):
00294         """DATA[2],   time avg. RR, LL"""
00295         check_eq(self.records['rr, ll']['data'],
00296                  numpy.array([[ 0.14428490-0.03145669j],
00297                               [ 0.14404297-0.04763794j]]),
00298                  0.0001)
00299         #self.__class__.n_tests_passed += 1
00300 
00301     def test_data_rllr(self):
00302         """DATA[2],   time avg. RL, LR"""
00303         check_eq(self.records['rl, lr']['data'],
00304                  numpy.array([[-0.00379944+0.00710297j],
00305                               [-0.00381106-0.00066403j]]),
00306                  0.0001)
00307         #self.__class__.n_tests_passed += 1
00308         
00309     def test_data_rr(self):
00310         """DATA[2],   time avg. RR"""
00311         check_eq(self.records['rr']['data'],
00312                  numpy.array([[ 0.14428490-0.03145669j]]),
00313                  0.0001)
00314         #self.__class__.n_tests_passed += 1
00315 
00316     def test_data_ll(self):
00317         """DATA[2],   time avg. LL"""
00318         check_eq(self.records['ll']['data'],
00319                  numpy.array([[ 0.14404297-0.04763794j]]),
00320                  0.0001)
00321         #self.__class__.n_tests_passed += 1
00322 
00323     def test_wt(self):
00324         """WEIGHT[5], time avg. without correlation selection"""
00325         check_eq(self.records['']['weight'],
00326                  numpy.array([143596.34375, 410221.34375,
00327                               122627.1640625, 349320.625]),
00328                  1.0)
00329         #self.__class__.n_tests_passed += 1
00330 
00331     def test_wt_rrll(self):
00332         """WEIGHT[5], time avg. RR, LL"""
00333         check_eq(self.records['rr, ll']['weight'],
00334                  numpy.array([143596.34375, 349320.625]),
00335                  1.0)
00336         #self.__class__.n_tests_passed += 1
00337 
00338     def test_wt_rllr(self):
00339         """WEIGHT[5], time avg. RL, LR"""
00340         check_eq(self.records['rl, lr']['weight'],
00341                  numpy.array([410221.34375, 122627.1640625]),
00342                  1.0)
00343 
00344     def test_wt_rr(self):
00345         """WEIGHT[5], time avg. RR"""
00346         check_eq(self.records['rr']['weight'],
00347                  numpy.array([143596.34375]),
00348                  1.0)
00349         #self.__class__.n_tests_passed += 1
00350 
00351     def test_wt_ll(self):
00352         """WEIGHT[5], time avg. LL"""
00353         check_eq(self.records['ll']['weight'],
00354                  numpy.array([349320.625]),
00355                  1.0)
00356         #self.__class__.n_tests_passed += 1
00357 
00358     def test_sigma(self):
00359         """SIGMA[7], time avg. without correlation selection"""
00360         check_eq(self.records['']['sigma'],
00361                  numpy.array([0.00168478, 0.00179394,
00362                               0.00182574, 0.00194404]),
00363                  0.0001)
00364         
00365     def test_sigma_rrll(self):
00366         """SIGMA[7], time avg. RR, LL"""
00367         check_eq(self.records['rr, ll']['sigma'],
00368                  numpy.array([0.00168478, 0.00194404]),
00369                  0.0001)
00370         #self.__class__.n_tests_passed += 1
00371         
00372     def test_sigma_rllr(self):
00373         """SIGMA[7], time avg. RL, LR"""
00374         check_eq(self.records['rl, lr']['sigma'],
00375                  numpy.array([0.00179394, 0.00182574]),
00376                  0.0001)
00377         #self.__class__.n_tests_passed += 1
00378         
00379     def test_sigma_rr(self):
00380         """SIGMA[7], time avg. RR"""
00381         check_eq(self.records['rr']['sigma'],
00382                  numpy.array([0.00168478]),
00383                  0.0001)
00384         
00385     def test_sigma_ll(self):
00386         """SIGMA[7], time avg. LL"""
00387         check_eq(self.records['ll']['sigma'],
00388                  numpy.array([0.00194404]),
00389                  0.0001)
00390         #self.__class__.n_tests_passed += 1
00391 
00392 class split_test_cav(SplitChecker):
00393     need_to_initialize = True
00394     corrsels = ['', 'rr', 'll']
00395     inpms = '../../viewertest/ctb80-vsm.ms'
00396     if datapath.count('unittest_mms')==1:
00397         inpms = 'ctb80-vsm.ms'
00398 
00399     records = {}
00400     #n_tests = 12
00401     #n_tests_passed = 0
00402     
00403     def do_split(self, corrsel):
00404         outms = 'cav' + re.sub(',\s*', '', corrsel) + '.ms'
00405         record = {'ms': outms}
00406 
00407         shutil.rmtree(outms, ignore_errors=True)
00408         try:
00409             print "\nChannel averaging", corrsel
00410             splitran = split(self.inpms, outms, datacolumn='data',
00411                              field='', spw='0:5~16', width=3,
00412                              antenna='',
00413                              timebin='', timerange='',
00414                              scan='', array='', uvrange='',
00415                              correlation=corrsel, async=False)
00416             tb.open(outms)
00417             record['data']   = tb.getcell('DATA', 2)
00418             record['weight'] = tb.getcell('WEIGHT', 5)
00419             record['sigma']  = tb.getcell('SIGMA', 7)
00420             tb.close()
00421         except Exception, e:
00422             print "Error channel averaging and reading", outms
00423             raise e
00424         self.records[corrsel] = record
00425         return splitran
00426 
00427     def test_sts(self):
00428         """Subtables, chan avg. without correlation selection"""
00429         self.check_subtables('', [(2, 4)])
00430         #self.__class__.n_tests_passed += 1
00431 
00432     def test_sts_rr(self):
00433         """Subtables, chan avg. RR"""
00434         self.check_subtables('rr', [(1, 4)])
00435         #self.__class__.n_tests_passed += 1
00436         
00437     def test_sts_ll(self):
00438         """Subtables, chan avg. LL"""
00439         self.check_subtables('ll', [(1, 4)])
00440         #self.__class__.n_tests_passed += 1
00441 
00442     def test_data(self):
00443         """DATA[2],   chan avg. without correlation selection"""
00444         check_eq(self.records['']['data'],
00445                  numpy.array([[16.795681-42.226387j, 20.5655-44.9874j,
00446                                26.801544-49.595020j, 21.4770-52.0462j],
00447                               [-2.919122-38.427235j, 13.3042-50.8492j,
00448                                 4.483857-43.986446j, 10.1733-19.4007j]]),
00449                  0.0005)
00450         #self.__class__.n_tests_passed += 1
00451         
00452     def test_data_rr(self):
00453         """DATA[2],   chan avg. RR"""
00454         check_eq(self.records['rr']['data'],
00455                  numpy.array([[16.79568-42.226387j, 20.5655-44.9874j,
00456                                26.80154-49.595020j, 21.4770-52.0462j]]),
00457                  0.0001)
00458         #self.__class__.n_tests_passed += 1
00459 
00460     def test_data_ll(self):
00461         """DATA[2],   chan avg. LL"""
00462         check_eq(self.records['ll']['data'],
00463                  numpy.array([[-2.919122-38.427235j, 13.3042-50.8492j,
00464                                 4.483857-43.986446j, 10.1733-19.4007j]]),
00465                  0.0001)
00466 
00467     def test_wt(self):
00468         """WEIGHT[5], chan avg. without correlation selection"""
00469         check_eq(self.records['']['weight'],
00470                  numpy.array([0.38709676, 0.38709676]), 0.001)
00471         #self.__class__.n_tests_passed += 1
00472 
00473     def test_wt_rr(self):
00474         """WEIGHT[5], chan avg. RR"""
00475         check_eq(self.records['rr']['weight'],
00476                  numpy.array([0.38709676]), 0.001)
00477 
00478     def test_wt_ll(self):
00479         """WEIGHT[5], chan avg. LL"""
00480         check_eq(self.records['ll']['weight'],
00481                  numpy.array([0.38709676]), 0.001)
00482         #self.__class__.n_tests_passed += 1
00483 
00484     def test_sigma(self):
00485         """SIGMA[7], chan avg. without correlation selection"""
00486         check_eq(self.records['']['sigma'],
00487                  numpy.array([0.57735026, 0.57735026]), 0.0001)
00488         
00489     def test_sigma_rr(self):
00490         """SIGMA[7], chan avg. RR"""
00491         check_eq(self.records['rr']['sigma'],
00492                  numpy.array([0.57735026]), 0.0001)
00493         
00494     def test_sigma_ll(self):
00495         """SIGMA[7], chan avg. LL"""
00496         check_eq(self.records['ll']['sigma'],
00497                  numpy.array([0.57735026]), 0.0001)
00498         #self.__class__.n_tests_passed += 1
00499 
00500 class split_test_cav5(SplitChecker):
00501     need_to_initialize = True
00502     corrsels = ['', 'll']
00503     inpms = '../../viewertest/ctb80-vsm.ms'
00504     if datapath.count('unittest_mms')==1:
00505         inpms = 'ctb80-vsm.ms'
00506 
00507     records = {}
00508     #n_tests = 12
00509     #n_tests_passed = 0
00510     
00511     def do_split(self, corrsel):
00512         outms = 'cav' + re.sub(',\s*', '', corrsel) + '.ms'
00513         record = {'ms': outms}
00514 
00515         shutil.rmtree(outms, ignore_errors=True)
00516         try:
00517             print "\nChannel averaging", corrsel
00518             splitran = split(self.inpms, outms, datacolumn='data',
00519                              field='', spw='0:5~16', width=5,
00520                              antenna='',
00521                              timebin='', timerange='',
00522                              scan='', array='', uvrange='',
00523                              correlation=corrsel, async=False)
00524             tb.open(outms)
00525             record['data']   = tb.getcell('DATA', 2)
00526             record['weight'] = tb.getcell('WEIGHT', 5)
00527             record['sigma']  = tb.getcell('SIGMA', 7)
00528             tb.close()
00529         except Exception, e:
00530             print "Error channel averaging and reading", outms
00531             raise e
00532         self.records[corrsel] = record
00533         return splitran
00534 
00535     def test_sts(self):
00536         """Subtables, chan avg. without correlation selection"""
00537         self.check_subtables('', [(2, 3)])
00538         #self.__class__.n_tests_passed += 1
00539 
00540     def test_sts_ll(self):
00541         """Subtables, chan avg. LL"""
00542         self.check_subtables('ll', [(1, 3)])
00543         #self.__class__.n_tests_passed += 1
00544 
00545     def test_data(self):
00546         """DATA[2],   chan avg. without correlation selection"""
00547         check_eq(self.records['']['data'],
00548                  numpy.array([[17.13964462-42.20331192j, 26.04414749-49.97922897j,
00549                                20.67210388-52.81464005j],
00550                               [ 5.80819368-43.6548233j,   6.72127867-44.33802414j,
00551                                10.60328102-11.62711906j]]),
00552                  0.0005)
00553         #self.__class__.n_tests_passed += 1
00554         
00555     def test_data_ll(self):
00556         """DATA[2],   chan avg. LL"""
00557         check_eq(self.records['ll']['data'],
00558                  numpy.array([[ 5.80819368-43.6548233j,  6.72127867-44.33802414j,
00559                                10.60328102-11.62711906j]]),
00560                  0.0001)
00561 
00562     def test_wt(self):
00563         """WEIGHT[5], chan avg. without correlation selection"""
00564         check_eq(self.records['']['weight'],
00565                  numpy.array([0.38709676, 0.38709676]),
00566                  0.001)
00567         #self.__class__.n_tests_passed += 1
00568 
00569     def test_wt_ll(self):
00570         """WEIGHT[5], chan avg. LL"""
00571         check_eq(self.records['ll']['weight'],
00572                  numpy.array([0.38709676]),
00573                  0.001)
00574         #self.__class__.n_tests_passed += 1
00575 
00576     def test_sigma(self):
00577         """SIGMA[7], chan avg. without correlation selection"""
00578         check_eq(self.records['']['sigma'],
00579                  numpy.array([0.5, 0.5]), 0.0001)
00580         
00581     def test_sigma_ll(self):
00582         """SIGMA[7], chan avg. LL"""
00583         check_eq(self.records['ll']['sigma'],
00584                  numpy.array([0.5]), 0.0001)
00585         #self.__class__.n_tests_passed += 1
00586 
00587 class split_test_cdsp(SplitChecker):
00588     need_to_initialize = True
00589     corrsels = ['cas-3307.ms', 'bogusCDSP.ms']  # MSes, not corr selections.
00590     inpms = corrsels[0]                         # This variable is not used.
00591     records = {}
00592     
00593     def initialize(self):
00594         # The realization that need_to_initialize needs to be
00595         # a class variable more or less came from
00596         # http://www.gossamer-threads.com/lists/python/dev/776699
00597         self.__class__.need_to_initialize = False
00598 
00599         for inpms in self.corrsels:
00600             if not os.path.exists(datapath + inpms):
00601                 raise EnvironmentError, "Missing input MS: " + datapath + inpms
00602             self.res = self.do_split(inpms)
00603 
00604     def do_split(self, corrsel):     # corrsel is really an input MS in
00605         outms = 'reind_' + corrsel   # this class.
00606         record = {'ms': outms}
00607 
00608         shutil.rmtree(outms, ignore_errors=True)
00609         try:
00610             print "\nRemapping CALDEVICE and SYSPOWER of", corrsel
00611             splitran = split(datapath + corrsel, outms, datacolumn='data',
00612                              field='', spw='0,2', width=1,
00613                              antenna='ea05,ea13&',
00614                              timebin='', timerange='',
00615                              scan='', array='', uvrange='',
00616                              correlation='', async=False)
00617             for st in ('CALDEVICE', 'SYSPOWER'):
00618                 record[st] = {}
00619                 tb.open(outms + '/' + st)
00620                 for c in ('ANTENNA_ID', 'SPECTRAL_WINDOW_ID'):
00621                     record[st][c]   = tb.getcol(c)
00622                 tb.close()
00623         except Exception, e:
00624             print "Error channel averaging and reading", outms
00625             raise e
00626         self.records[corrsel] = record
00627         return splitran
00628 
00629     def test_bogus_cd_antid(self):
00630         """ANTENNA_ID selection from a bad CALDEVICE"""
00631         # The resulting CALDEVICE is probably useless; the point is to ensure
00632         # that split ran to completion.
00633         check_eq(self.records['bogusCDSP.ms']['CALDEVICE']['ANTENNA_ID'],
00634                  numpy.array([0, 1, 0, 1]))
00635 
00636     def test_bogus_cd_spwid(self):
00637         """SPECTRAL_WINDOW_ID selection from a bad CALDEVICE"""
00638         # The resulting CALDEVICE is probably useless; the point is to ensure
00639         # that split ran to completion.
00640         check_eq(self.records['bogusCDSP.ms']['CALDEVICE']['SPECTRAL_WINDOW_ID'],
00641                  numpy.array([0, 0, 1, 1]))
00642 
00643     def test_bogus_cd_antid(self):
00644         """ANTENNA_ID selection from a bad SYSPOWER"""
00645         # The resulting SYSPOWER is probably useless; the point is to ensure
00646         # that split ran to completion.
00647         check_eq(self.records['bogusCDSP.ms']['SYSPOWER']['ANTENNA_ID'][89:97],
00648                  numpy.array([0, 0, 1, 0, 0, 1, 1, 1]))
00649 
00650     def test_bogus_cd_spwid(self):
00651         """SPECTRAL_WINDOW_ID selection from a bad SYSPOWER"""
00652         # The resulting SYSPOWER is probably useless; the point is to ensure
00653         # that split ran to completion.
00654         check_eq(self.records['bogusCDSP.ms']['SYSPOWER']['SPECTRAL_WINDOW_ID'][189:197],
00655                  numpy.array([0, 1, 0, 0, 0, 1, 1, 1]))
00656 
00657     def test_cd_antid(self):
00658         """ANTENNA_ID selection from CALDEVICE"""
00659         check_eq(self.records['cas-3307.ms']['CALDEVICE']['ANTENNA_ID'],
00660                  numpy.array([0, 1, 0, 1]))
00661 
00662     def test_cd_spwid(self):
00663         """SPECTRAL_WINDOW_ID selection from CALDEVICE"""
00664         check_eq(self.records['cas-3307.ms']['CALDEVICE']['SPECTRAL_WINDOW_ID'],
00665                  numpy.array([0, 0, 1, 1]))
00666 
00667     def test_cd_antid(self):
00668         """ANTENNA_ID selection from SYSPOWER"""
00669         # Purposely take a few from near the end.
00670         check_eq(self.records['cas-3307.ms']['SYSPOWER']['ANTENNA_ID'][-19:-6],
00671                  numpy.array([1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1]))
00672 
00673     def test_cd_spwid(self):
00674         """SPECTRAL_WINDOW_ID selection from SYSPOWER"""
00675         check_eq(self.records['cas-3307.ms']['SYSPOWER']['SPECTRAL_WINDOW_ID'][-18:-7],
00676                  numpy.array([0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1]))
00677 
00678 
00679 class split_test_cst(SplitChecker):
00680     """
00681     The main thing here is to not segfault even when the SOURCE table
00682     contains nonsense.
00683     """
00684     need_to_initialize = True
00685     corrsels = ['']
00686     inpms = datapath + 'crazySourceTable.ms' # read-only
00687     outms = 'filteredsrctab.ms'
00688     records = {}
00689 
00690     def initialize(self):
00691         # The realization that need_to_initialize needs to be
00692         # a class variable more or less came from
00693         # http://www.gossamer-threads.com/lists/python/dev/776699
00694         self.__class__.need_to_initialize = False
00695 
00696         if not os.path.isdir(self.inpms):
00697             raise EnvironmentError, "Missing input MS: " + self.inpms
00698         self.res = self.do_split(self.inpms)
00699 
00700     def do_split(self, inpms):
00701         shutil.rmtree(self.outms, ignore_errors=True)
00702         record = {}
00703         try:
00704             print "\nSplitting", inpms
00705             splitran = split(inpms, self.outms, datacolumn='data',
00706                              field='', spw='', width=1,
00707                              antenna='',
00708                              timebin='', timerange='',
00709                              scan='', array='', uvrange='',
00710                              correlation='',
00711                              observation='1~3,5',
00712                              async=False)
00713         except Exception, e:
00714             print "Error splitting to", self.outms
00715             raise e
00716         try:
00717             tb.open(self.outms + '/SOURCE')
00718             record['srcids'] = tb.getcol('SOURCE_ID')
00719             tb.close()
00720             tb.open(self.outms)
00721             #record['lastmainobsid'] = tb.getcell('OBSERVATION_ID', tb.nrows() - 1)
00722             tcol = tb.getcol('OBSERVATION_ID')
00723             tcol.sort()
00724             record['lastmainobsid'] = tcol[tb.nrows() - 1]
00725             tb.close()
00726             tb.open(self.outms + '/OBSERVATION')
00727             record['ebs'] = tb.getcol('SCHEDULE')[1]
00728             tb.close()
00729             shutil.rmtree(self.outms, ignore_errors=True)
00730         except Exception, e:
00731             print "Error getting results from", self.outms
00732             raise e
00733         self.records[inpms] = record
00734         return splitran
00735             
00736 
00737 #    def tearDown(self):
00738 #        shutil.rmtree(self.outms, ignore_errors=True)
00739         
00740     def test_cst(self):
00741         """
00742         Check that only the good part of a SOURCE subtable with some nonsense made it through
00743         """
00744         check_eq(self.records[self.inpms]['srcids'],
00745                  numpy.array([0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0,
00746                               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))
00747 
00748     def test_obs(self):
00749         """
00750         Selected right observation IDs?
00751         """
00752         check_eq(self.records[self.inpms]['ebs'],
00753                  numpy.array(['ExecBlock uid://A002/Xb4fac/X1',
00754                               'ExecBlock uid://A002/Xb4f4c/X1',
00755                               'ExecBlock uid://A002/Xb4eec/X1',
00756                               'ExecBlock uid://A002/Xb506c/X1']))
00757         check_eq(self.records[self.inpms]['lastmainobsid'], 2)
00758         
00759 
00760 class split_test_state(unittest.TestCase):
00761     """
00762     Checks the STATE subtable after selecting by intent.
00763     """
00764     inpms = datapath + 'doppler01fine-01.ms'
00765     locms = inpms.split(os.path.sep)[-1]
00766     outms = 'obstar.ms'
00767 
00768     def setUp(self):
00769         try:
00770             shutil.rmtree(self.outms, ignore_errors=True)
00771             os.symlink(self.inpms, self.locms)  # Paranoia
00772             splitran = split(self.locms, self.outms, datacolumn='data',
00773                              intent='OBSERVE_TARGET.UNSPECIFIED',
00774                              async=False)
00775         except Exception, e:
00776             print "Error splitting", self.locms, "to", self.outms
00777             raise e
00778 
00779     def tearDown(self):
00780         os.unlink(self.locms)
00781         shutil.rmtree(self.outms, ignore_errors=True)
00782 
00783     def test_state(self):
00784         """
00785         Is STATE correct after selecting by intent?
00786         """
00787         tb.open(self.outms + '/STATE')
00788         om = tb.getcol('OBS_MODE')
00789         tb.close()
00790         check_eq(om, numpy.array(['OBSERVE_TARGET.UNSPECIFIED']))
00791         tb.open(self.outms)
00792         mytime = tb.getcol('TIME')
00793         myrow = 0
00794         for i in xrange(len(mytime)):
00795             if mytime[i]==4785966752.5:
00796                 myrow = i
00797                 break
00798         rec = {}
00799         for c in ('ANTENNA1', 'ANTENNA2', 'DATA_DESC_ID', 'DATA',
00800                   'SCAN_NUMBER', 'STATE_ID', 'TIME'):
00801             rec[c] = tb.getcell(c, myrow)
00802         tb.close()
00803         # Row 1330 in inpms is the first one with STATE_ID 0.
00804         check_eq(rec, {'ANTENNA1': 0,
00805                        'ANTENNA2': 1,
00806                        'DATA': numpy.array([[287638.+0.j, 287638.+1.j,
00807                                              287638.+2.j, 287638.+3.j],
00808                                             [287638.+0.j, 287638.+1.j,
00809                                              287638.+2.j, 287638.+3.j]]),
00810                        'DATA_DESC_ID': 0,
00811                        'SCAN_NUMBER': 38,
00812                        'STATE_ID': 0,
00813                        'TIME': 4785966752.5})
00814 
00815 class split_test_cavcd(unittest.TestCase):
00816     """
00817     Checks that the CORRECTED_DATA column can be channel averaged.
00818     """
00819     inpms = '../../split/labelled_by_time+ichan.ms'    
00820     if datapath.count('unittest_mms')==1:
00821         inpms = 'labelled_by_time+ichan.ms'
00822 
00823     outms = 'cavcd.ms'
00824 
00825     def setUp(self):
00826         try:
00827             shutil.rmtree(self.outms, ignore_errors=True)
00828         
00829             if not os.path.exists(self.inpms):
00830                 # Copying is technically unnecessary for split,
00831                 # but self.inpms is shared by other tests, so making
00832                 # it readonly might break them.
00833                 shutil.copytree(datapath + self.inpms, self.inpms)
00834                 
00835             print "\n\tSplitting", self.inpms
00836             splitran = split(self.inpms, self.outms, datacolumn='corrected',
00837                              field='', spw='', width=4,
00838                              antenna='',
00839                              timebin='0s', timerange='',
00840                              scan='', array='', uvrange='',
00841                              correlation='', async=False)
00842         except Exception, e:
00843             print "Error splitting", self.inpms, "to", self.outms
00844             raise e
00845 
00846     def tearDown(self):
00847         shutil.rmtree(self.inpms, ignore_errors=True)
00848         shutil.rmtree(self.outms, ignore_errors=True)
00849 
00850     def test_cavcd(self):
00851         """
00852         Was the CORRECTED_DATA column channel averaged?
00853         """
00854         tb.open(self.outms)
00855         cod = tb.getcell('DATA', 0)
00856         tb.close()
00857         check_eq(cod.shape, (1, 2))
00858 
00859 class split_test_genericsubtables(unittest.TestCase):
00860     """
00861     Check copying generic subtables
00862     """
00863     inpms = datapath + '2554.ms'
00864     outms = 'musthavegenericsubtables.ms'
00865 
00866     def setUp(self):
00867         try:
00868             shutil.rmtree(self.outms, ignore_errors=True)
00869 
00870             #print "\n\tSplitting", self.inpms
00871             splitran = split(self.inpms, self.outms, datacolumn='data',
00872                              field='', spw='0', width=1,
00873                              antenna='',
00874                              timebin='0s', timerange='',
00875                              scan='', array='', uvrange='',
00876                              correlation='', async=False)
00877         except Exception, e:
00878             print "Error splitting", self.inpms, "to", self.outms
00879             raise e
00880 
00881     def tearDown(self):
00882         shutil.rmtree(self.outms, ignore_errors=True)
00883 
00884     def test_genericsubtables(self):
00885         """
00886         Can we copy generic subtables?
00887         """
00888         tb.open(self.outms)
00889         kws = tb.keywordnames()
00890         tb.close()
00891         # Just check a few, and order does not matter.  Include both "generic"
00892         # and "standard" (mandatory and optional) subtables.
00893         for subtab in ('ASDM_CALWVR', 'ASDM_CALDELAY', 'DATA_DESCRIPTION',
00894                        'POINTING', 'SYSCAL'):
00895             assert subtab in kws
00896  
00897 class split_test_singchan(unittest.TestCase):
00898     """
00899     Check selecting a single channel with the spw:chan syntax
00900     """
00901     # rename and make readonly when plotxy goes away.
00902     inpms = '../../viewertest/ctb80-vsm.ms'
00903     if datapath.count('unittest_mms')==1:
00904         inpms = 'ctb80-vsm.ms'
00905 
00906     outms = 'musthavesingchan.ms'
00907 
00908     def setUp(self):
00909         try:
00910             shutil.rmtree(self.outms, ignore_errors=True)
00911 
00912             if not os.path.exists(self.inpms):
00913                 # Copying is technically unnecessary for split,
00914                 # but self.inpms is shared by other tests, so making
00915                 # it readonly might break them.
00916                 shutil.copytree(datapath + self.inpms, self.inpms)
00917 
00918             print "\n\tSplitting", self.inpms
00919             splitran = split(self.inpms, self.outms, datacolumn='data',
00920                              field='', spw='0:25', width=1,
00921                              antenna='',
00922                              timebin='0s', timerange='',
00923                              scan='', array='', uvrange='',
00924                              correlation='', async=False)
00925         except Exception, e:
00926             print "Error splitting", self.inpms, "to", self.outms
00927             raise e
00928 
00929     def tearDown(self):
00930         # Leaves an empty viewertest dir in nosedir
00931         shutil.rmtree(self.inpms, ignore_errors=True)
00932         
00933         shutil.rmtree(self.outms, ignore_errors=True)
00934 
00935     def test_singchan(self):
00936         """
00937         Did we get the right channel?
00938         """
00939         tb.open(self.inpms)
00940         data_orig = tb.getcell('DATA', 3)
00941         tb.close()
00942         tb.open(self.outms)
00943         data_sp = tb.getcell('DATA', 3)
00944         tb.close()
00945         
00946         # For all correlations, compare output channel 0 to input channel 25.
00947         check_eq(data_sp[:,0], data_orig[:,25], 0.0001)
00948 
00949 class split_test_blankov(unittest.TestCase):
00950     """
00951     Check that outputvis == '' causes a prompt exit.
00952     """
00953     # rename and make readonly when plotxy goes away.
00954     inpms = '../../viewertest/ctb80-vsm.ms'
00955     if datapath.count('unittest_mms')==1:
00956         inpms = 'ctb80-vsm.ms'
00957 
00958     outms = ''
00959 
00960     def setUp(self):
00961         try:
00962             shutil.rmtree(self.outms, ignore_errors=True)
00963 
00964             if not os.path.exists(self.inpms):
00965                 # Copying is technically unnecessary for split,
00966                 # but self.inpms is shared by other tests, so making
00967                 # it readonly might break them.
00968                 shutil.copytree(datapath + self.inpms, self.inpms)
00969         except Exception, e:
00970             print "Error in rm -rf %s or cp -r %s" % (self.outms, self.inpms)
00971             raise e
00972 
00973     def tearDown(self):
00974         shutil.rmtree(self.inpms, ignore_errors=True)
00975         shutil.rmtree(self.outms, ignore_errors=True)
00976 
00977     def test_blankov(self):
00978         """
00979         Does outputvis == '' cause a prompt exit?
00980         """
00981         splitran = False
00982         original_throw_pref = False
00983         try:
00984             #print "\n\tSplitting", self.inpms
00985             myf = sys._getframe(len(inspect.stack()) - 1).f_globals
00986             # This allows distinguishing ValueError from other exceptions, and
00987             # quiets an expected error message.
00988             original_throw_pref = myf.get('__rethrow_casa_exceptions', False)
00989             myf['__rethrow_casa_exceptions'] = True
00990             splitran = split(self.inpms, self.outms, datacolumn='data',
00991                              field='', spw='0:25', width=1,
00992                              antenna='',
00993                              timebin='0s', timerange='',
00994                              scan='', array='', uvrange='',
00995                              correlation='', async=False)
00996         except ValueError:
00997             splitran = False
00998         except Exception, e:
00999             print "Unexpected but probably benign exception:", e
01000         myf['__rethrow_casa_exceptions'] = original_throw_pref
01001         assert not splitran
01002 
01003 class split_test_almapol(SplitChecker):
01004     """
01005     Check that correlations can be selected when WVR data is in spw 0,
01006     and that nonstandard columns in WEATHER are being copied.
01007     """
01008     need_to_initialize = True
01009     corrsels = ['xx,yy']
01010     inpms = datapath + 'ixxxyyxyy.ms'
01011     records = {}
01012 
01013     def do_split(self, corrsel):
01014         outms = 'xxyyspw1_3.ms'
01015         record = {'ms': outms}
01016 
01017         shutil.rmtree(outms, ignore_errors=True)
01018         splitran = False
01019         try:
01020             splitran = split(self.inpms, outms, datacolumn='data',
01021                              field='', spw='1~3', width=1,
01022                              antenna='',
01023                              timebin='0s', timerange='',
01024                              scan='', array='', uvrange='',
01025                              correlation=corrsel, async=False)
01026             tb.open(outms + '/WEATHER')
01027             record['nsid'] = {0: tb.getcell('NS_WX_STATION_ID', 0),
01028                               1: tb.getcell('NS_WX_STATION_ID', 1)}
01029             record['nspos'] = {0: tb.getcell('NS_WX_STATION_POSITION', 0),
01030                                1: tb.getcell('NS_WX_STATION_POSITION', 1)}
01031             tb.close()
01032         except Exception, e:
01033             print "Error selecting %s from %s:" % (corrsel, outms)
01034             raise e
01035         self.records[corrsel] = record
01036         return splitran
01037             
01038     def test_almapol(self):
01039         """Can we select corrs when WVR data is in spw 0?"""
01040         for corrsel in self.corrsels:
01041             assert os.path.isdir(self.records[corrsel]['ms'])
01042             shutil.rmtree(self.records[corrsel]['ms'], ignore_errors=True)
01043 
01044     def test_nsid(self):
01045         """Did NS_WX_STATION_ID get copied?"""
01046         for corrsel in self.corrsels:
01047             check_eq(self.records[corrsel]['nsid'][0], 8)
01048             check_eq(self.records[corrsel]['nsid'][1], 9)
01049             
01050     def test_nspos(self):
01051         """Did NS_WX_STATION_POS get copied?"""
01052         for corrsel in self.corrsels:
01053             check_eq(self.records[corrsel]['nspos'][0],
01054                      numpy.array([2225262.12, -5440307.30, -2480962.57]), 0.01)
01055             check_eq(self.records[corrsel]['nspos'][1],
01056                      numpy.array([2224782.10, -5440330.29, -2481339.08]), 0.01)
01057             
01058 
01059 class split_test_unorderedpolspw(SplitChecker):
01060     """
01061     Check spw selection from a tricky MS.
01062     """
01063     need_to_initialize = True
01064     inpms = datapath + 'unordered_polspw.ms'
01065     corrsels = ['']
01066     records = {}
01067     #n_tests = 2
01068     #n_tests_passed = 0
01069 
01070     def do_split(self, corrsel):
01071         outms = 'pss' + re.sub(',\s*', '', corrsel) + '.ms'
01072         record = {'ms': outms}
01073 
01074         shutil.rmtree(outms, ignore_errors=True)
01075         try:
01076             print "\nSelecting spws 1, 3, and 5."
01077             splitran = split(self.inpms, outms, datacolumn='data',
01078                              field='', spw='1,3,5', width=1, antenna='',
01079                              timebin='0s', timerange='18:32:40~18:33:20',
01080                              scan='', array='', uvrange='',
01081                              correlation=corrsel, async=False)
01082             tb.open(outms)
01083             record['data'] = tb.getcell('DATA', 2)
01084             tb.close()
01085         except Exception, e:
01086             print "Error selecting spws 1, 3, and 5 from", self.inpms
01087             raise e
01088         self.__class__.records[corrsel] = record
01089         return splitran
01090 
01091     def test_datashape(self):
01092         """Data shape"""
01093         assert self.records['']['data'].shape == (2, 128)
01094         #self.__class__.n_tests_passed += 1
01095 
01096     def test_subtables(self):
01097         """DATA_DESCRIPTION, SPECTRAL_WINDOW, and POLARIZATION shapes"""
01098         self.check_subtables('', [(2, 128)])
01099         #self.__class__.n_tests_passed += 1
01100 
01101 class split_test_sw_and_fc(SplitChecker):
01102     """
01103     Check SPECTRAL_WINDOW and FLAG_CMD with chan selection and averaging.
01104     """
01105     need_to_initialize = True
01106     inpms = datapath + '2562.ms'
01107     records = {}
01108 
01109     # records uses these as keys, so they MUST be tuples, not lists.
01110     # Each tuple is really (spw, width), but it's called corrsels for
01111     # compatibility with SplitChecker.
01112     corrsels = (('1:12~115', '1'), ('1', '3'))
01113 
01114     def do_split(self, spwwidth):
01115         outms = 'cw' + spwwidth[1] + '.ms'
01116         record = {'ms': outms}
01117 
01118         shutil.rmtree(outms, ignore_errors=True)
01119         try:
01120             print "\nChecking SPECTRAL_WINDOW and FLAG_CMD with width " + spwwidth[1] + '.'
01121             # Antenna selection added just so it's tested somewhere.
01122             splitran = split(self.inpms, outms, datacolumn='data',
01123                              field='', spw=spwwidth[0], width=spwwidth[1],
01124                              antenna='VA03,VA05&',               # Case sensitive
01125                              timebin='0s', timerange='',
01126                              scan='', array='', uvrange='',
01127                              correlation='', async=False)
01128             tb.open(outms + '/SPECTRAL_WINDOW')
01129             cf = tb.getcell('CHAN_FREQ', 0)
01130             record['nchan'] = cf.shape[0]
01131             record['cf0']   = cf[0]
01132             record['cf']    = cf[33]
01133             record['cflc']  = cf[-1]
01134             record['res']   = tb.getcell('RESOLUTION', 0)
01135             record['cw']    = tb.getcell('CHAN_WIDTH', 0)
01136             record['eb']    = tb.getcell('EFFECTIVE_BW', 0)
01137             record['tb']    = tb.getcell('TOTAL_BANDWIDTH', 0)
01138             record['rf']    = tb.getcell('REF_FREQUENCY', 0)
01139             tb.close()
01140             tb.open(outms + '/FLAG_CMD')
01141             record['fc'] = []
01142             for i in (0, 1, 2, 3, 4, 515, 516):
01143                 record['fc'].append(tb.getcell('COMMAND', i))
01144             tb.close()
01145             shutil.rmtree(outms, ignore_errors=True)
01146         except Exception, e:
01147             print "Error selecting spws 1, 3, and 5 from", self.inpms
01148             raise e
01149         self.__class__.records[spwwidth] = record
01150         return splitran
01151 
01152     def test_fc_noavg(self):
01153         """Updating of FLAG_CMD after selection, but no averaging."""
01154         check_eq(self.records[('1:12~115', '1')]['fc'],
01155                  ['',
01156                   "antenna='ea18' spw='0:28~43' timerange='2010/04/08/20:03:52.502~2010/04/08/20:03:55.504'",
01157                   "antenna='ea20' timerange='2010/04/08/20:03:56.804~2010/04/08/20:03:59.936'",
01158                   "antenna='ea17' spw='0:1~21' timerange='2010/04/08/20:04:50.614~2010/04/08/20:05:07.259'",
01159                   "antenna='ea22' spw='0:0~11' timerange='2010/04/08/20:04:50.614~2010/04/08/20:05:07.829'",
01160                   " antenna='ea17' spw='0:1~21' timerange='2010/04/08/20:04:50.614~2010/04/08/20:05:07.259,2010/04/08/20:04:50.917~2010/04/08/20:04:58.403,2010/04/08/20:06:01.627~2010/04/08/20:06:05.527,2010/04/08/20:06:16.444~2010/04/08/20:06:20.656,2010/04/08/20:06:36.308~2010/04/08/20:06:40.113,2010/04/08/20:06:56.059~2010/04/08/20:06:59.095,2010/04/08/20:07:16.302~2010/04/08/20:07:19.909,2010/04/08/20:07:36.027~2010/04/08/20:07:40.325,2010/04/08/20:07:56.374~2010/04/08/20:08:00.534,2010/04/08/20:08:16.436~2010/04/08/20:08:20.406,2010/04/08/20:08:35.928~2010/04/08/20:08:39.026,2010/04/08/20:08:56.301~2010/04/08/20:08:59.788,2010/04/08/20:09:16.035~2010/04/08/20:09:20.368,2010/04/08/20:09:36.382~2010/04/08/20:09:40.741,2010/04/08/20:09:56.591~2010/04/08/20:10:00.388,2010/04/08/20:10:16.083~2010/04/08/20:10:19.120,2010/04/08/20:10:36.085~2010/04/08/20:10:39.700,2010/04/08/20:10:49.701~2010/04/08/20:11:07.582,2010/04/08/20:10:49.900~2010/04/08/20:10:57.482,2010/04/08/20:10:50.401~2010/04/08/20:10:54.665'",
01161                   " antenna='ea22' spw='0:0~11' timerange='2010/04/08/20:04:50.614~2010/04/08/20:05:07.829,2010/04/08/20:04:51.020~2010/04/08/20:04:55.716,2010/04/08/20:06:01.661~2010/04/08/20:06:05.692,2010/04/08/20:06:16.392~2010/04/08/20:06:20.699,2010/04/08/20:06:36.403~2010/04/08/20:06:40.312,2010/04/08/20:06:55.903~2010/04/08/20:06:59.121,2010/04/08/20:07:16.181~2010/04/08/20:07:19.702,2010/04/08/20:07:35.915~2010/04/08/20:07:40.438,2010/04/08/20:07:56.297~2010/04/08/20:08:00.638,2010/04/08/20:08:16.445~2010/04/08/20:08:20.458,2010/04/08/20:08:36.006~2010/04/08/20:08:39.129,2010/04/08/20:08:56.129~2010/04/08/20:08:59.736,2010/04/08/20:09:16.044~2010/04/08/20:09:20.549,2010/04/08/20:09:36.374~2010/04/08/20:09:40.793,2010/04/08/20:09:56.479~2010/04/08/20:10:00.579,2010/04/08/20:10:15.781~2010/04/08/20:10:19.085,2010/04/08/20:10:36.093~2010/04/08/20:10:39.597,2010/04/08/20:10:49.805~2010/04/08/20:11:06.294,2010/04/08/20:10:49.995~2010/04/08/20:10:54.000,2010/04/08/20:10:50.298~2010/04/08/20:10:55.417'"])
01162 
01163     def test_rf_noavg(self):
01164         """REF_FREQUENCY after selection, but no averaging."""
01165         check_eq(self.records[('1:12~115', '1')]['rf'], 22141747338.809235)
01166 
01167     def test_nchan_noavg(self):
01168         """# of channels after selection, but no averaging."""
01169         check_eq(self.records[('1:12~115', '1')]['nchan'], 104)
01170 
01171     def test_res_noavg(self):
01172         """RESOLUTION after selection, but no averaging."""
01173         check_eq(self.records[('1:12~115', '1')]['res'], 14771.10564634, 1e-4)
01174 
01175     def test_cf0_noavg(self):
01176         """CHAN_FREQ[0] after selection, but no averaging."""
01177         check_eq(self.records[('1:12~115', '1')]['cf0'], 22141747338.809235, 1e-4)
01178 
01179     def test_cf_noavg(self):
01180         """CHAN_FREQ[33] after selection, but no averaging."""
01181         check_eq(self.records[('1:12~115', '1')]['cf'], 22142150187.145042, 1e-4)
01182 
01183     def test_cflc_noavg(self):
01184         """CHAN_FREQ[-1] after selection, but no averaging."""
01185         check_eq(self.records[('1:12~115', '1')]['cflc'], 22143004713.917973, 1e-4)
01186 
01187     def test_cw_noavg(self):
01188         """CHAN_WIDTH after selection, but no averaging."""
01189         check_eq(self.records[('1:12~115', '1')]['cw'], 12207.525327551524, 1e-4)
01190 
01191     def test_eb_noavg(self):
01192         """EFFECTIVE_BW after selection, but no averaging."""
01193         check_eq(self.records[('1:12~115', '1')]['eb'], 14771.10564634, 1e-4)
01194 
01195     def test_tb_noavg(self):
01196         """TOTAL_BANDWIDTH after selection, but no averaging."""
01197         check_eq(self.records[('1:12~115', '1')]['tb'], 1269582.6340653566, 1e-4)
01198 
01199     def test_nchan_wavg(self):
01200         """# of channels after averaging, but no selection."""
01201         check_eq(self.records[('1', '3')]['nchan'], 43)
01202 
01203     def test_rf_wavg(self):
01204         """REF_FREQUENCY after averaging, but no selection."""
01205         check_eq(self.records[('1', '3')]['rf'], 22141613056.030632)
01206 
01207     def test_res_wavg(self):
01208         """RESOLUTION after averaging and simple selection."""
01209         # The last one really is different (128 % 3 != 0), but the variation
01210         # of the rest is numerical jitter.
01211         check_eq(self.records[('1', '3')]['res'],
01212                  numpy.array([39186.15630552, 39186.1563017, 39186.15630552,
01213                               39186.1563017,  39186.1563017, 39186.1563017,
01214                               39186.1563017,  39186.1563055, 39186.1563017,
01215                               39186.15629789, 39186.1563055, 39186.15629789,
01216                               39186.1563017,  39186.1562941, 39186.15629789,
01217                               39186.1563017,  39186.1562979, 39186.15629789,
01218                               39186.15629789, 39186.1562979, 39186.15630552,
01219                               39186.1563017,  39186.1563055, 39186.1563017,
01220                               39186.1563017,  39186.1563055, 39186.15629789,
01221                               39186.15630552, 39186.1563017, 39186.1563017,
01222                               39186.1563017,  39186.1563017, 39186.15630552,
01223                               39186.1563017,  39186.1562979, 39186.15630552,
01224                               39186.1563017,  39186.1563055, 39186.15629789,
01225                               39186.1563017,  39186.1563055, 39186.15629789,
01226                               14771.10564634]), 1e-4)
01227 
01228     def test_cf0_wavg(self):
01229         """CHAN_FREQ[0] after averaging, but no selection."""
01230         check_eq(self.records[('1', '3')]['cf0'], 22141613056.030632, 1e-4)
01231 
01232     def test_cf_wavg(self):
01233         """CHAN_FREQ[33] after averaging, but no selection."""
01234         check_eq(self.records[('1', '3')]['cf'], 22142821601.038055, 1e-4)
01235 
01236     def test_cflc_wavg(self):
01237         """CHAN_FREQ[-1] after averaging, but no selection."""
01238         check_eq(self.records[('1', '3')]['cflc'], 22143138996.696575, 1e-4)
01239 
01240     def test_cw_wavg(self):
01241         """CHAN_WIDTH after averaging, but no selection."""
01242         # The last one really is different (128 % 3 != 0), but the variation
01243         # of the rest is numerical jitter.
01244         check_eq(self.records[('1', '3')]['cw'],
01245                  numpy.array([36622.57598673, 36622.57598292, 36622.57598673,
01246                               36622.57598292, 36622.57598292, 36622.57598292,
01247                               36622.57598292, 36622.57598673, 36622.57598292,
01248                               36622.5759791,  36622.57598673, 36622.5759791,
01249                               36622.57598292, 36622.57597529, 36622.5759791,
01250                               36622.57598292, 36622.5759791,  36622.5759791,
01251                               36622.5759791,  36622.5759791,  36622.57598673,
01252                               36622.57598292, 36622.57598673, 36622.57598292,
01253                               36622.57598292, 36622.57598673, 36622.5759791,
01254                               36622.57598673, 36622.57598292, 36622.57598292,
01255                               36622.57598292, 36622.57598292, 36622.57598673,
01256                               36622.57598292, 36622.5759791,  36622.57598673,
01257                               36622.57598292, 36622.57598673, 36622.5759791,
01258                               36622.57598292, 36622.57598673, 36622.5759791,
01259                               12207.52532755]), 1e-3)
01260 
01261     def test_eb_wavg(self):
01262         """EFFECTIVE_BW after averaging, but no selection."""
01263         # The last one really is different (128 % 3 != 0), but the variation
01264         # of the rest is numerical jitter.
01265         check_eq(self.records[('1', '3')]['eb'],
01266                  numpy.array([39186.15630552, 39186.1563017,  39186.15630552,
01267                               39186.1563017,  39186.1563017,  39186.1563017,
01268                               39186.1563017,  39186.15630552, 39186.1563017,
01269                               39186.15629789, 39186.15630552, 39186.15629789,
01270                               39186.1563017,  39186.15629407, 39186.15629789,
01271                               39186.1563017,  39186.15629789, 39186.15629789,
01272                               39186.15629789, 39186.15629789, 39186.15630552,
01273                               39186.1563017,  39186.15630552, 39186.1563017,
01274                               39186.1563017,  39186.15630552, 39186.15629789,
01275                               39186.15630552, 39186.1563017,  39186.1563017,
01276                               39186.1563017,  39186.1563017,  39186.15630552,
01277                               39186.1563017,  39186.15629789, 39186.15630552,
01278                               39186.1563017,  39186.15630552, 39186.15629789,
01279                               39186.1563017,  39186.15630552, 39186.15629789,
01280                               14771.10564634]), 1e-3)
01281 
01282     def test_tb_wavg(self):
01283         """Is TOTAL_BANDWIDTH conserved after averaging, but no selection?"""
01284         # The expected value comes from spw 1 of inpms.
01285         check_eq(self.records[('1', '3')]['tb'], 1550355.7165990437, 0.1)
01286 
01287     def test_fc_wavg(self):
01288         """Updating of FLAG_CMD after averaging, but simple selection."""
01289         check_eq(self.records[('1', '3')]['fc'],
01290                  ['',
01291                   "antenna='ea18' spw='0:13~18' timerange='2010/04/08/20:03:52.502~2010/04/08/20:03:55.504'",
01292                   "antenna='ea20' timerange='2010/04/08/20:03:56.804~2010/04/08/20:03:59.936'",
01293                   "antenna='ea17' spw='0:1~2;4~11' timerange='2010/04/08/20:04:50.614~2010/04/08/20:05:07.259'",
01294                   "antenna='ea22' spw='0:3~7' timerange='2010/04/08/20:04:50.614~2010/04/08/20:05:07.829'",
01295                   " antenna='ea17' spw='0:1~2;4~11' timerange='2010/04/08/20:04:50.614~2010/04/08/20:05:07.259,2010/04/08/20:04:50.917~2010/04/08/20:04:58.403,2010/04/08/20:06:01.627~2010/04/08/20:06:05.527,2010/04/08/20:06:16.444~2010/04/08/20:06:20.656,2010/04/08/20:06:36.308~2010/04/08/20:06:40.113,2010/04/08/20:06:56.059~2010/04/08/20:06:59.095,2010/04/08/20:07:16.302~2010/04/08/20:07:19.909,2010/04/08/20:07:36.027~2010/04/08/20:07:40.325,2010/04/08/20:07:56.374~2010/04/08/20:08:00.534,2010/04/08/20:08:16.436~2010/04/08/20:08:20.406,2010/04/08/20:08:35.928~2010/04/08/20:08:39.026,2010/04/08/20:08:56.301~2010/04/08/20:08:59.788,2010/04/08/20:09:16.035~2010/04/08/20:09:20.368,2010/04/08/20:09:36.382~2010/04/08/20:09:40.741,2010/04/08/20:09:56.591~2010/04/08/20:10:00.388,2010/04/08/20:10:16.083~2010/04/08/20:10:19.120,2010/04/08/20:10:36.085~2010/04/08/20:10:39.700,2010/04/08/20:10:49.701~2010/04/08/20:11:07.582,2010/04/08/20:10:49.900~2010/04/08/20:10:57.482,2010/04/08/20:10:50.401~2010/04/08/20:10:54.665'",
01296                   " antenna='ea22' spw='0:3~7' timerange='2010/04/08/20:04:50.614~2010/04/08/20:05:07.829,2010/04/08/20:04:51.020~2010/04/08/20:04:55.716,2010/04/08/20:06:01.661~2010/04/08/20:06:05.692,2010/04/08/20:06:16.392~2010/04/08/20:06:20.699,2010/04/08/20:06:36.403~2010/04/08/20:06:40.312,2010/04/08/20:06:55.903~2010/04/08/20:06:59.121,2010/04/08/20:07:16.181~2010/04/08/20:07:19.702,2010/04/08/20:07:35.915~2010/04/08/20:07:40.438,2010/04/08/20:07:56.297~2010/04/08/20:08:00.638,2010/04/08/20:08:16.445~2010/04/08/20:08:20.458,2010/04/08/20:08:36.006~2010/04/08/20:08:39.129,2010/04/08/20:08:56.129~2010/04/08/20:08:59.736,2010/04/08/20:09:16.044~2010/04/08/20:09:20.549,2010/04/08/20:09:36.374~2010/04/08/20:09:40.793,2010/04/08/20:09:56.479~2010/04/08/20:10:00.579,2010/04/08/20:10:15.781~2010/04/08/20:10:19.085,2010/04/08/20:10:36.093~2010/04/08/20:10:39.597,2010/04/08/20:10:49.805~2010/04/08/20:11:06.294,2010/04/08/20:10:49.995~2010/04/08/20:10:54.000,2010/04/08/20:10:50.298~2010/04/08/20:10:55.417'"])
01297 
01298 class split_test_optswc(SplitChecker):
01299     """
01300     Check propagation of SPECTRAL_WINDOW's optional columns
01301     """
01302     need_to_initialize = True
01303     inpms = datapath + 'optswc.ms'
01304     records = {}
01305     expcols = set(['MEAS_FREQ_REF', 'CHAN_FREQ',       'REF_FREQUENCY',
01306                    'CHAN_WIDTH',    'EFFECTIVE_BW',    'RESOLUTION',
01307                    'FLAG_ROW',      'FREQ_GROUP',      'FREQ_GROUP_NAME',
01308                    'IF_CONV_CHAIN', 'NAME',            'NET_SIDEBAND',
01309                    'NUM_CHAN',      'TOTAL_BANDWIDTH', 'BBC_NO',
01310                    'ASSOC_SPW_ID',  'ASSOC_NATURE'])
01311 
01312     # records uses these as keys, so they MUST be tuples, not lists.
01313     # Each tuple is really (spw, width), but it's called corrsels for
01314     # compatibility with SplitChecker.
01315     corrsels = (('1:12~115', '1'), ('', '3'))
01316 
01317     def do_split(self, spwwidth):
01318         outms = 'optswc_' + spwwidth[1] + '.ms'
01319         record = {'ms': outms}
01320 
01321         shutil.rmtree(outms, ignore_errors=True)
01322         try:
01323             print "\nChecking SPECTRAL_WINDOW's opt cols with width " + spwwidth[1] + '.'
01324             splitran = split(self.inpms, outms, datacolumn='data',
01325                              field='', spw=spwwidth[0], width=spwwidth[1], antenna='',
01326                              timebin='0s', timerange='',
01327                              scan='', array='', uvrange='',
01328                              correlation='', async=False)
01329             tb.open(outms + '/SPECTRAL_WINDOW')
01330             record['colnames'] = set(tb.colnames())
01331             record['bbc_no']   = tb.getcell('BBC_NO', 0)
01332             tb.close()
01333             shutil.rmtree(outms, ignore_errors=True)
01334         except Exception, e:
01335             print "Error selecting spws 1, 3, and 5 from", self.inpms
01336             raise e
01337         self.__class__.records[spwwidth] = record
01338         return splitran
01339 
01340     def test_rightcols_noavg(self):
01341         """List of SW cols after selection, but no averaging."""
01342         check_eq(self.records[('1:12~115', '1')]['colnames'],
01343                  self.expcols)
01344 
01345     def test_rightcols_wavg(self):
01346         """List of SW cols after averaging, but no selection."""
01347         check_eq(self.records[('', '3')]['colnames'],
01348                  self.expcols)
01349         
01350     def test_bbcno_noavg(self):
01351         """Can we get BBC1?"""
01352         check_eq(self.records[('1:12~115', '1')]['bbc_no'], 1)
01353 
01354     def test_bbcno_wavg(self):
01355         """Can we get any BBC if we average?"""
01356         check_eq(self.records[('', '3')]['bbc_no'], 0)
01357 
01358         
01359 class split_test_tav_then_cvel(SplitChecker):
01360     need_to_initialize = True
01361     # doppler01fine-01.ms was altered by
01362     # make_labelled_ms(vis, vis,
01363     #                  {'SCAN_NUMBER': 1.0,
01364     #                   'DATA_DESC_ID': 0.01,
01365     #                   'chan': complex(0, 1),
01366     #                   'STATE_ID': complex(0, 0.1),
01367     #                   'time': 100.0}, ow=True)
01368     inpms = datapath + 'doppler01fine-01.ms'
01369     corrsels = ['']
01370     records = {}
01371     #n_tests = 6
01372     #n_tests_passed = 0
01373     
01374     def do_split(self, corrsel):
01375         tavms = 'doppler01fine-01-10s.ms'
01376         cvms  = 'doppler01fine-01-10s-cvel.ms'
01377         record = {'tavms': tavms, 'cvms': cvms,
01378                   'tav': {},      'cv': False}
01379         self.__class__._cvel_err = False
01380 
01381         shutil.rmtree(tavms, ignore_errors=True)
01382         shutil.rmtree(cvms, ignore_errors=True)
01383         try:
01384             print "\nTime averaging", corrsel
01385             splitran = split(self.inpms, tavms, datacolumn='data',
01386                              field='', spw='', width=1, antenna='',
01387                              timebin='10s', timerange='',
01388                              scan='', array='', uvrange='',
01389                              correlation=corrsel, async=False)
01390             tb.open(tavms)
01391             for c in ['DATA', 'WEIGHT', 'INTERVAL', 'SCAN_NUMBER', 'STATE_ID', 'TIME']:
01392                 record['tav'][c] = {}
01393                 for r in [0, 4, 5, 6, 7, 90, 91]:
01394                     record['tav'][c][r] = tb.getcell(c, r)
01395             for c in ['SCAN_NUMBER', 'STATE_ID', 'TIME']:
01396                 record['tav'][c][123] = tb.getcell(c, 123)
01397             tb.close()
01398         except Exception, e:
01399             print "Error time averaging and reading", tavms
01400             raise e
01401         try:
01402             print "Running cvel"
01403             cvelran = cvel(tavms, cvms, passall=False, field='', spw='0~8',
01404                            selectdata=True, timerange='', scan="", array="",
01405                            mode="velocity", nchan=-1, start="-4km/s",
01406                            width="-1.28km/s", interpolation="linear",
01407                            phasecenter="", restfreq="6035.092MHz",
01408                            outframe="lsrk", veltype="radio", hanning=False)
01409         except Exception, e:
01410             print "Error running cvel:", e
01411             # Do NOT raise e: that would prevent the tav tests from running.
01412             # Use test_cv() to register a cvel error.
01413             self.__class__._cvel_err = True
01414         self.__class__.records = record
01415         shutil.rmtree(tavms, ignore_errors=True)
01416         # Don't remove cvms yet, its existence is tested.
01417         return splitran
01418 
01419     def test_tav_data(self):
01420         """Time averaged DATA"""
01421         check_eq(self.records['tav']['DATA'],
01422                  {0: numpy.array([[ 455.+0.10000001j,  455.+1.10000014j,
01423                                     455.+2.10000014j,  455.+3.10000014j],
01424                                   [ 455.+0.10000001j,  455.+1.10000014j,
01425                                     455.+2.10000014j,  455.+3.10000014j]]),
01426                   4: numpy.array([[4455.+0.10000001j, 4455.+1.10000014j,
01427                                    4455.+2.10000014j, 4455.+3.10000014j],
01428                                   [4455.+0.10000001j, 4455.+1.10000014j,
01429                                    4455.+2.10000014j, 4455.+3.10000014j]]),
01430                   5: numpy.array([[5405.+0.10000001j, 5405.+1.10000002j,
01431                                    5405.+2.10000014j, 5405.+3.10000014j],
01432                                   [5405.+0.10000001j, 5405.+1.10000002j,
01433                                    5405.+2.10000014j, 5405.+3.10000014j]]),
01434                   6: numpy.array([[6356.+0.10000002j, 6356.+1.10000014j,
01435                                    6356.+2.10000014j, 6356.+3.10000014j],
01436                                   [6356.+0.10000002j, 6356.+1.10000014j,
01437                                    6356.+2.10000014j, 6356.+3.10000014j]]),
01438                   7: numpy.array([[7356.+0.10000002j, 7356.+1.10000014j,
01439                                    7356.+2.10000014j, 7356.+3.10000014j],
01440                                   [7356.+0.10000002j, 7356.+1.10000014j,
01441                                    7356.+2.10000014j, 7356.+3.10000014j]]),
01442                  90: numpy.array([[162467.015625+0.j, 162467.015625+1.j,
01443                                    162467.015625+2.j, 162467.015625+3.j],
01444                                   [162467.015625+0.j, 162467.015625+1.j,
01445                                    162467.015625+2.j, 162467.015625+3.j]]),
01446                  91: numpy.array([[163467.015625+0.j, 163467.015625+1.j,
01447                                    163467.015625+2.j, 163467.015625+3.j],
01448                                   [163467.015625+0.j, 163467.015625+1.j,
01449                                    163467.015625+2.j, 163467.015625+3.j]])},
01450                  0.0001)
01451         #self.__class__.n_tests_passed += 1
01452 
01453     def test_tav_wt(self):
01454         """Time averaged WEIGHT"""
01455         check_eq(self.records['tav']['WEIGHT'],
01456                  {0: numpy.array([ 10.,  10.]),
01457                   4: numpy.array([ 10.,  10.]),
01458                   5: numpy.array([ 9.,  9.]),
01459                   6: numpy.array([ 10.,  10.]),
01460                   7: numpy.array([ 10.,  10.]),
01461                   90: numpy.array([ 10.,  10.]),
01462                   91: numpy.array([ 10.,  10.])}, 0.01)
01463         #self.__class__.n_tests_passed += 1
01464 
01465     def test_tav_int(self):
01466         """Time averaged INTERVAL"""
01467         check_eq(self.records['tav']['INTERVAL'],
01468                  {0: 10.0, 4: 10.0, 5: 9.0, 6: 10.0, 7: 10.0, 90: 10.0, 91: 10.0},
01469                  0.01)
01470         #self.__class__.n_tests_passed += 1
01471 
01472     def test_tav_state_id(self):
01473         """Time averaged STATE_ID"""
01474         check_eq(self.records['tav']['STATE_ID'],
01475                  {0: 1, 4: 1, 5: 1, 6: 1, 7: 1, 90: 0, 91: 0, 123: 0})
01476 
01477     def test_tav_scan(self):
01478         """Time averaged SCAN_NUMBER"""
01479         check_eq(self.records['tav']['SCAN_NUMBER'],
01480                  {0: 5, 4: 5, 5: 5, 6: 6, 7: 6, 90: 17, 91: 17, 123: 40})
01481 
01482     def test_tav_time(self):
01483         """Time averaged TIME"""
01484         check_eq(self.records['tav']['TIME'],
01485                  {0: 4785963881.0,
01486                   4: 4785963921.0,
01487                   5: 4785963930.5,
01488                   6: 4785963940.0,
01489                   7: 4785963950.0,
01490                   90: 4785965501.0,
01491                   91: 4785965511.0,
01492                   123: 4785966907.0})
01493 
01494     def test_cv(self):
01495         """cvel completed"""
01496         assert self._cvel_err == False and os.path.isdir(self.records['cvms'])
01497         shutil.rmtree(self.records['cvms'])
01498         #self.__class__.n_tests_passed += 1
01499 
01500 class split_test_wttosig(SplitChecker):
01501     """
01502     Check WEIGHT and SIGMA after various datacolumn selections and averagings.
01503     """
01504     need_to_initialize = True
01505     inpms = datapath + 'testwtsig.ms'
01506     records = {}
01507 
01508     # records uses these as keys, so they MUST be tuples, not lists.
01509     # Each tuple is really (datacolumn, width, timebin), but it's called corrsels for
01510     # compatibility with SplitChecker.
01511     corrsels = (('data',      '1', '0s'), # straight selection of DATA.
01512                 ('corrected', '1', '0s'), # straight CORRECTED -> DATA.
01513                 ('data', '2', '0s'),      # channel averaged DATA
01514                 ('data', '1', '60s'),     # time averaged DATA
01515                 ('corrected', '2', '0s'), # channel averaged CORRECTED -> DATA
01516                 ('corrected', '1', '60s')) # time averaged CORRECTED -> DATA
01517 
01518     def do_split(self, dcwtb):
01519         outms = 'wtsig_' + '_'.join(dcwtb) + '.ms'
01520         record = {'ms': outms}
01521 
01522         shutil.rmtree(outms, ignore_errors=True)
01523         try:
01524             print "\nChecking WEIGHT and SIGMA after %s." % (dcwtb,)
01525             splitran = split(self.inpms, outms, datacolumn=dcwtb[0],
01526                              field='', spw='', width=dcwtb[1], antenna='',
01527                              timebin=dcwtb[2], timerange='',
01528                              scan='', array='', uvrange='',
01529                              correlation='', async=False)
01530             tb.open(outms)
01531             record['sigma'] = tb.getcol('SIGMA')[:,0:5].transpose()
01532             record['wt']    = tb.getcol('WEIGHT')[:,0:5].transpose()
01533             tb.close()
01534             shutil.rmtree(outms, ignore_errors=True)
01535         except Exception, e:
01536             print "Error splitting %s from %s", (dcwtb, self.inpms)
01537             raise e
01538         self.__class__.records[dcwtb] = record
01539         return splitran
01540 
01541     def test_wt_straightselection(self):
01542         """WEIGHT after straight selection of DATA."""
01543         check_eq(self.records[('data', '1', '0s')]['wt'],
01544                  numpy.array([[1.,     4.,       9.,      16.],
01545                               [0.0625, 0.111111, 0.25,     1.],
01546                               [1.,     0.25,     0.111111, 0.0625],
01547                               [1.,     1.,       1.,       1.],
01548                               [1.,     1.,       1.,       1.]]), 0.001)
01549 
01550     def test_sig_straightselection(self):
01551         """SIGMA after straight selection of DATA."""
01552         check_eq(self.records[('data', '1', '0s')]['sigma'],
01553                  numpy.array([[4.,     3.,       2.,       1.],
01554                               [4.,     3.,       2.,       1.],
01555                               [1.,     2.,       3.,       4.],
01556                               [5.,     6.,       7.,       8.],
01557                               [1.,     1.,       1.,       1.]]), 0.001)
01558 
01559     def test_wt_corrtodata(self):
01560         """WEIGHT after straight CORRECTED -> DATA."""
01561         check_eq(self.records[('corrected', '1', '0s')]['wt'],
01562                  numpy.array([[1.,     4.,       9.,      16.],
01563                               [0.0625, 0.111111, 0.25,     1.],
01564                               [1.,     0.25,     0.111111, 0.0625],
01565                               [1.,     1.,       1.,       1.],
01566                               [1.,     1.,       1.,       1.]]), 0.001)
01567 
01568     def test_sig_corrtodata(self):
01569         """SIGMA after straight CORRECTED -> DATA."""
01570         check_eq(self.records[('corrected', '1', '0s')]['sigma'],
01571                  numpy.array([[1.,     0.5,      0.333333, 0.25],
01572                               [4.,     3.,       2.,       1.],
01573                               [1.,     2.,       3.,       4.],
01574                               [1.,     1.,       1.,       1.],
01575                               [1.,     1.,       1.,       1.]]), 0.001)
01576 
01577     def test_wt_cavdata(self):
01578         """WEIGHT after channel averaging DATA."""
01579         check_eq(self.records[('data', '2', '0s')]['wt'],
01580                  numpy.array([[1.,     4.,       9.,      16.],
01581                               [0.0625, 0.111111, 0.25,     1.],
01582                               [1.,     0.25,     0.111111, 0.0625],
01583                               [1.,     1.,       1.,       1.],
01584                               [1.,     1.,       1.,       1.]]), 0.001)
01585 
01586     def test_sig_cavdata(self):
01587         """SIGMA after channel averaging DATA."""
01588         check_eq(self.records[('data', '2', '0s')]['sigma'],
01589                  numpy.array([[ 2.82842708,  2.12132049,  1.41421354,  0.70710677],
01590                               [ 2.82842708,  2.12132049,  1.41421354,  0.70710677],
01591                               [ 0.70710677,  1.41421354,  2.12132049,  2.82842708],
01592                               [ 3.53553391,  4.24264097,  4.94974756,  5.65685415],
01593                               [ 0.70710677,  0.70710677,  0.70710677,  0.70710677]]),
01594                  0.001)
01595 
01596     def test_wt_tavdata(self):
01597         """WEIGHT after time averaging DATA."""
01598         check_eq(self.records[('data', '1', '60s')]['wt'],
01599                  numpy.array([[2.,     2.,       0.,       2.],
01600                               [4.,    16.,       0.,       1.],
01601                               [4.,     4.,       4.,       4.],
01602                               [2.,     2.,       2.,       2.],
01603                               [2.,     2.,       2.,       2.]]), 0.001)
01604 
01605     def test_sig_tavdata(self):
01606         """SIGMA after time averaging DATA."""
01607         check_eq(self.records[('data', '1', '60s')]['sigma'],
01608                  numpy.array([[3.5355, 4.2426,  -1.0,      5.6569],
01609                               [4.3011, 2.5495,  -1.0,      3.0414],
01610                               [17.2505, 0.7906, 17.2518,   0.4507],
01611                               [0.7071, 0.7071,   0.7071,   0.7071],
01612                               [0.7071, 0.7071,   0.7071,   0.7071]]), 0.001)
01613 
01614     def test_wt_cavcorr(self):
01615         """WEIGHT after channel averaging CORRECTED_DATA."""
01616         check_eq(self.records[('corrected', '2', '0s')]['wt'],
01617                  numpy.array([[1.,     4.,       9.,      16.],
01618                               [0.0625, 0.111111, 0.25,     1.],
01619                               [1.,     0.25,     0.111111, 0.0625],
01620                               [1.,     1.,       1.,       1.],
01621                               [1.,     1.,       1.,       1.]]), 0.001)
01622 
01623     def test_sig_cavcorr(self):
01624         """SIGMA after channel averaging CORRECTED_DATA."""
01625         check_eq(self.records[('corrected', '2', '0s')]['sigma'],
01626                  numpy.array([[1.,     0.5,      0.33333,  0.25],
01627                               [4.,     3.0,      2.,       1.],
01628                               [1.,     2.,       3.0000,   4.],
01629                               [1.,     1.,       1.,       1.],
01630                               [1.,     1.,       1.,       1.]]), 0.001)
01631 
01632     def test_wt_tavcorr(self):
01633         """WEIGHT after time averaging CORRECTED_DATA."""
01634         check_eq(self.records[('corrected', '1', '60s')]['wt'],
01635                  numpy.array([[2.,     2.,       0.,       2.],
01636                               [4.,    16.,       0.,       1.],
01637                               [4.,     4.,       4.,       4.],
01638                               [2.,     2.,       2.,       2.],
01639                               [2.,     2.,       2.,       2.]]), 0.001)
01640 
01641     def test_sig_tavcorr(self):
01642         """SIGMA after time averaging CORRECTED_DATA."""
01643         check_eq(self.records[('corrected', '1', '60s')]['sigma'],
01644                  numpy.array([[0.7071, 0.7071,  -1.0,      0.7071],
01645                               [0.5,    0.25,    -1.0,      1.0],
01646                               [0.5,    0.5,      0.5,      0.5],
01647                               [0.7071, 0.7071,   0.7071,   0.7071],
01648                               [0.7071, 0.7071,   0.7071,   0.7071]]), 0.001)
01649 
01650 class split_test_fc(SplitChecker):
01651     """
01652     Check FLAG_CATEGORY after various selections and averagings.
01653     """
01654     need_to_initialize = True
01655     inpms = datapath + 'hasfc.ms'
01656     records = {}
01657 
01658     # records uses these as keys, so they MUST be tuples, not lists.
01659     # Each tuple is really (datacolumn, width, timebin), but it's called corrsels for
01660     # compatibility with SplitChecker.
01661     corrsels = (('21:37:30~21:39:00', 1, '0s'),  # straight selection
01662                 ('',                  2, '0s'),  # channel averaged
01663                 ('',                  1, '20s')) # time averaged
01664 
01665     def do_split(self, trwtb):
01666         outms = 'fc.ms'
01667         record = {'ms': outms}
01668 
01669         shutil.rmtree(outms, ignore_errors=True)
01670         try:
01671             print "\nChecking FLAG_CATEGORY after %s." % (trwtb,)
01672             splitran = split(self.inpms, outms, datacolumn='data',
01673                              field='', spw='', width=trwtb[1], antenna='',
01674                              timebin=trwtb[2], timerange=trwtb[0],
01675                              scan='', array='', uvrange='',
01676                              correlation='', async=False)
01677             tb.open(outms)
01678             record['fc'] = tb.getcell('FLAG_CATEGORY', 5)[2]
01679             categories = tb.getcolkeyword('FLAG_CATEGORY', 'CATEGORY')
01680             tb.close()
01681             shutil.rmtree(outms, ignore_errors=True)
01682         except Exception, e:
01683             print "Error splitting %s from %s", (trwtb, self.inpms)
01684             raise e
01685         self.__class__.records[trwtb] = record
01686         self.__class__.records['categories'] = categories
01687         return splitran
01688 
01689     def test_fc_categories(self):
01690         """FLAG_CATEGORY's CATEGORY keyword"""
01691         check_eq(self.records['categories'],
01692                  numpy.array(['FLAG_CMD', 'ORIGINAL', 'USER']))
01693 
01694     def test_fc_straightselection(self):
01695         """FLAG_CATEGORY after straight selection"""
01696         check_eq(self.records[('21:37:30~21:39:00', 1, '0s')]['fc'],
01697                  numpy.array([[ True, False, False],
01698                               [ True, False, False],
01699                               [False, False, False],
01700                               [False, False, False],
01701                               [False, False, False],
01702                               [False, False, False],
01703                               [False, False, False],
01704                               [False, False, False],
01705                               [False, False, False],
01706                               [False, False, False],
01707                               [False, False, False],
01708                               [False, False, False],
01709                               [False, False, False],
01710                               [False, False, False],
01711                               [False, False, False],
01712                               [False, False, False],
01713                               [False, False, False],
01714                               [False, False, False],
01715                               [False, False, False],
01716                               [False, False, False],
01717                               [False, False, False],
01718                               [False, False, False],
01719                               [False, False, False],
01720                               [False, False, False],
01721                               [False, False, False],
01722                               [False, False, False],
01723                               [False, False, False],
01724                               [False, False, False],
01725                               [False, False, False],
01726                               [False, False, False],
01727                               [False, False, False],
01728                               [False, False, False],
01729                               [False, False, False],
01730                               [False, False, False],
01731                               [False, False, False],
01732                               [False, False, False],
01733                               [False, False, False],
01734                               [False, False, False],
01735                               [False, False, False],
01736                               [ True, False, False],
01737                               [ True, False, False]]))
01738 
01739     def test_fc_cav(self):
01740         """FLAG_CATEGORY after channel averaging"""
01741         check_eq(self.records[('', 2, '0s')]['fc'],
01742                  numpy.array([[ True, False, False],
01743                               [False, False, False],
01744                               [False, False, False],
01745                               [False, False, False],
01746                               [False, False, False],
01747                               [False, False, False],
01748                               [False, False, False],
01749                               [False, False, False],
01750                               [False, False, False],
01751                               [False, False, False],
01752                               [False, False, False],
01753                               [False, False, False],
01754                               [False, False, False],
01755                               [False, False, False],
01756                               [False, False, False],
01757                               [False, False, False],
01758                               [False, False, False],
01759                               [False, False, False],
01760                               [False, False, False],
01761                               [False, False, False],
01762                               [ True, False, False]]))
01763 
01764     def test_fc_tav(self):
01765         """FLAG_CATEGORY after time averaging"""
01766         check_eq(self.records[('', 1, '20s')]['fc'],
01767                  numpy.array([[ True, False, False],
01768                               [ True, False, False],
01769                               [False, False, False],
01770                               [False, False, False],
01771                               [False, False, False],
01772                               [False, False, False],
01773                               [False, False, False],
01774                               [False, False, False],
01775                               [False, False, False],
01776                               [False, False, False],
01777                               [False, False, False],
01778                               [False, False, False],
01779                               [False, False, False],
01780                               [False, False, False],
01781                               [False, False, False],
01782                               [False, False, False],
01783                               [False, False, False],
01784                               [False, False, False],
01785                               [False, False, False],
01786                               [False, False, False],
01787                               [False, False, False],
01788                               [False, False, False],
01789                               [False, False, False],
01790                               [False, False, False],
01791                               [False, False, False],
01792                               [False, False, False],
01793                               [False, False, False],
01794                               [False, False, False],
01795                               [False, False, False],
01796                               [False, False, False],
01797                               [False, False, False],
01798                               [False, False, False],
01799                               [False, False, False],
01800                               [False, False, False],
01801                               [False, False, False],
01802                               [False, False, False],
01803                               [False, False, False],
01804                               [False, False, False],
01805                               [False, False, False],
01806                               [ True, False, False],
01807                               [ True, False, False]]))
01808 
01809 
01810 
01811 def suite():
01812     return [split_test_tav, split_test_cav, split_test_cav5, split_test_cst,
01813             split_test_state, split_test_optswc, split_test_cdsp,
01814             split_test_singchan, split_test_unorderedpolspw, split_test_blankov,
01815             split_test_tav_then_cvel, split_test_genericsubtables,
01816             split_test_sw_and_fc, split_test_cavcd, split_test_almapol,
01817             split_test_wttosig, split_test_fc]
01818