00001 import os
00002 import shutil
00003 import testhelper as th
00004 import numpy as np
00005 from __main__ import default
00006 from tasks import fluxscale
00007 from taskinit import *
00008 import unittest
00009
00010
00011 ''' Python unit tests for the fluxscale task
00012
00013 These tests will only verify if the fluxscale
00014 tables created for an MS and an MMS agree. These are
00015 not full unit tests for the fluxscale task.
00016 '''
00017
00018 datapath = os.environ.get('CASAPATH').split()[0] +\
00019 '/data/regression/unittest/fluxscale/'
00020
00021
00022
00023 testmms = False
00024 if os.environ.has_key('TEST_DATADIR'):
00025 DATADIR = str(os.environ.get('TEST_DATADIR'))+'/fluxscale/'
00026 if os.path.isdir(DATADIR):
00027 testmms = True
00028 datapath = DATADIR
00029 else:
00030 print 'WARN: directory '+DATADIR+' does not exist'
00031
00032 print 'fluxscale tests will use data from '+datapath
00033
00034
00035 class fluxscale1_test(unittest.TestCase):
00036
00037 def setUp(self):
00038
00039 self.prefix = 'ngc5921'
00040 self.msfile = self.prefix + '.ms'
00041 if testmms:
00042 self.msfile = self.prefix + '.mms'
00043
00044 self.gtable = self.prefix + '.ref1a.gcal'
00045 self.reffile = self.prefix + '.def.fcal'
00046 self.reffile2 = self.prefix + '.def.inc.fcal'
00047 self.tearDown()
00048
00049 fpath = os.path.join(datapath,self.msfile)
00050 if os.path.lexists(fpath):
00051 shutil.copytree(fpath, self.msfile, symlinks=True)
00052 fpath = os.path.join(datapath,self.gtable)
00053 shutil.copytree(fpath, self.gtable, symlinks=True)
00054 fpath = os.path.join(datapath,self.reffile)
00055 shutil.copytree(fpath, self.reffile, symlinks=True)
00056 fpath = os.path.join(datapath,self.reffile2)
00057 shutil.copytree(fpath, self.reffile2, symlinks=True)
00058 else:
00059 self.fail('Data does not exist -> '+fpath)
00060
00061 default('fluxscale')
00062
00063 def tearDown(self):
00064 shutil.rmtree(self.msfile, ignore_errors=True)
00065 os.system('rm -rf ngc5921*.fcal')
00066 os.system('rm -rf ngc5921*.gcal')
00067
00068
00069 def test_default(self):
00070 '''Fluxscale 1a: Create a flux table using field=0 as reference'''
00071
00072
00073 gtable = self.gtable
00074
00075
00076 outtable = self.msfile + '.fcal'
00077
00078 thisdict = fluxscale(vis=self.msfile, caltable=gtable, fluxtable=outtable, reference='1331*',
00079 transfer='1445*')
00080 self.assertTrue(os.path.exists(outtable))
00081
00082
00083 reference = self.reffile
00084
00085
00086 self.assertTrue(th.compTables(outtable, reference, ['WEIGHT']))
00087
00088
00089 refdict={'1': {'spidxerr': np.array([ 0., 0., 0.]), 'spidx': np.array([ 0., 0., 0.]), \
00090 'fluxdErr': np.array([0.00055571]), \
00091 'fieldName': '1445+09900002_0', 'numSol': np.array([54]), \
00092 'fluxd': np.array([0.16825763])}, \
00093 'freq': np.array([1.41266507e+09]), \
00094 'spwName': np.array(['none'], dtype='|S5'), \
00095 'spwID': np.array([0])}
00096
00097 diff_fluxd=abs(refdict['1']['fluxd'][0]-thisdict['1']['fluxd'][0])/refdict['1']['fluxd'][0]
00098 self.assertTrue(diff_fluxd<1.5e-8)
00099
00100
00101 def test_incremental(self):
00102 '''Fluxscale 1b: Create an incremental flux table using field=0 as reference'''
00103
00104 gtable = self.gtable
00105
00106
00107 outtable = self.msfile + '.inc.fcal'
00108
00109 thisdict = fluxscale(vis=self.msfile, caltable=gtable, fluxtable=outtable, reference='1331*',
00110 transfer='1445*', incremental=True)
00111 self.assertTrue(os.path.exists(outtable))
00112
00113
00114 reference = self.reffile2
00115
00116
00117 self.assertTrue(th.compTables(outtable, reference, ['WEIGHT']))
00118
00119
00120 class fluxscale2_test(unittest.TestCase):
00121
00122 def setUp(self):
00123
00124 prefix = 'ngc4826'
00125 self.msfile = prefix + '.ms'
00126 if testmms:
00127 self.msfile = prefix + '.mms'
00128
00129 self.gtable = prefix + '.spw.gcal'
00130 self.reffile = prefix + '.spw.fcal'
00131
00132 fpath = os.path.join(datapath,self.msfile)
00133 if os.path.lexists(fpath):
00134 shutil.copytree(fpath, self.msfile, symlinks=True)
00135 fpath = os.path.join(datapath,self.gtable)
00136 shutil.copytree(fpath, self.gtable, symlinks=True)
00137 fpath = os.path.join(datapath,self.reffile)
00138 shutil.copytree(fpath, self.reffile, symlinks=True)
00139 else:
00140 self.fail('Data does not exist -> '+fpath)
00141
00142 default('fluxscale')
00143
00144
00145 def tearDown(self):
00146 shutil.rmtree(self.msfile, ignore_errors=True)
00147 os.system('rm -rf ngc4826*.gcal')
00148 os.system('rm -rf ngc4826*.fcal')
00149
00150
00151 def test_spws(self):
00152 '''Fluxscale 2: Create a fluxscale table for an MS with many spws'''
00153
00154 gtable = self.gtable
00155
00156
00157 outtable = self.msfile + '.fcal'
00158
00159 thisdict = fluxscale(vis=self.msfile, caltable=gtable, fluxtable=outtable, reference='3C273-F0',
00160 transfer=['1310+323-F0'],refspwmap=[0,0])
00161 self.assertTrue(os.path.exists(outtable))
00162
00163
00164 reference = self.reffile
00165
00166
00167 self.assertTrue(th.compTables(outtable, reference, ['WEIGHT']))
00168
00169
00170 refdict={'1': {'spidxerr': np.array([ 0., 0., 0.]), 'spidx': np.array([ 0., 0., 0.]), \
00171 'fluxdErr': np.array([-1. , 0.04080052, -1. , -1. , -1. , -1. ]), \
00172 'fieldName': '1310+323-F0', 'numSol': np.array([-1, 8, -1, -1, -1, -1], dtype=np.int32), \
00173 'fluxd': np.array([-1. , 1.44578847, -1. , -1. , -1. , -1. ])}, \
00174 'freq': np.array([ 1.15138579e+11, 1.15217017e+11, -1.00000000e+00, -1.00000000e+00, -1.00000000e+00, \
00175 -1.00000000e+00]), 'spwName': np.array(['', '', '', '', '', ''], dtype='|S1'), \
00176 'spwID': np.array([0, 1, 2, 3, 4, 5], dtype=np.int32)}
00177 diff_fluxd=abs(refdict['1']['fluxd'][1]-thisdict['1']['fluxd'][1])/refdict['1']['fluxd'][1]
00178 self.assertTrue(diff_fluxd<1.e-8)
00179
00180 def suite():
00181 return [fluxscale1_test, fluxscale2_test]
00182
00183
00184
00185
00186
00187
00188