casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
coordsystest_regression.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #                                                                            #
00003 # Test Name:                                                                 #
00004 #   coordsystest_regression.py                                               #
00005 # Rationale for Inclusion:                                                   #
00006 #   This is a Python translation of the Glish assay test coordsystest.g      #
00007 #   It tests many coordsys tool methods                                      #
00008 # Features Tested:                                                           #
00009 #   coordsys, type, done, id, ncoordinates                                   #
00010 #   coordinatetype, axiscoordinatetypes, summary                             #
00011 #   is_coordsys, coordsystools                                               #
00012 #   referencecode, setreferencecode                                          #
00013 #   restfrequency, setrestfrequency                                          #
00014 #   projection, setprojection                                                #
00015 #   torecord, fromrecord, copy                                               #
00016 #   setepoch, epoch, setobserver, observer                                   #
00017 #   settelescope, telescope, setparentname, parentname                       #
00018 #   setnames, names, setunits, units                                         #
00019 #   setreferencepixel, referencepixel                                        #
00020 #   setreferencevalue, referencevalue                                        #
00021 #   setincrement, increment                                                  #
00022 #   setlineartransform, lineartransform                                      #
00023 #   setstokes, stokes                                                        #
00024 #   findcoordinate, findaxis                                                 #
00025 #   toworld, toworldmany, topixel, topixelmany                               #
00026 #   naxes, axesmap                                                           #
00027 #   reorder                                                                  #
00028 #   frequencytovelocity, velocitytofrequency                                 #
00029 #   setreferencelocation                                                     #
00030 #   toabs, torel, toabsmany, torelmany                                       #
00031 #   convert, convertmany                                                     #
00032 #   setspectral                                                              #
00033 #   settabular                                                               #
00034 #   addcoordinate                                                            #
00035 #   toworld, topixel with reference conversion                               #
00036 #   setdirection                                                             #
00037 #   replace                                                                  #
00038 # Success/failure criteria:                                                  #
00039 #    Internally tests each method for pass/fail.  Throws an uncaught         #
00040 #    exception ending test when an unexpected failure occurs.                #
00041 #    All tests pass if script runs to completion.                            #
00042 #                                                                            #
00043 ##############################################################################
00044 #                                                                            #
00045 # Converted by RRusk 2007-11-08 from coordsystest.py                         #
00046 #                                                                            #
00047 ##############################################################################
00048 
00049 import time
00050 import re
00051 #
00052 # coordsystest.py: test casapy coordsys tool
00053 #
00054 
00055 #
00056 # Utils
00057 #
00058 def note(message, priority="INFO", origin="coordsystest", ntime=None, postcli='F'):
00059     #if not ntime:  #if (ntime==None):
00060     #    ntime=time.asctime()
00061     #print ntime, priority, origin, message
00062     if postcli: print message
00063     casalog.postLocally(message, priority, origin)
00064 ###
00065 def info(message):
00066     #note(message,origin='coordsystest')
00067     print message
00068     casalog.postLocally(message, priority="INFO", origin="coordsystest")
00069 
00070 def fail(message=""):
00071     casalog.postLocally(message, priority="SEVERE", origin='coordsystest')
00072     raise RuntimeError, message
00073 
00074 ###
00075 def stop(message=""):
00076     note(message ,priority='SEVERE', origin='coordsystest')
00077     raise RuntimeError, message
00078 
00079 ###
00080 def cleanup(dir):
00081     if (os.path.isdir(dir)):
00082         info("Cleaning up directory "+dir)
00083         def errFunc(raiser, problemPath, excInfo):
00084             #print raiser.__name__,'failed on',problemPath
00085             note(raiser.__name__+'failed on'+problemPath,"SEVERE")
00086             raise RuntimeError, "Cleanup of " + dir + " fails!"
00087         shutil.rmtree(dir,0,errFunc)
00088     return true
00089 
00090 def all(x,y,tolerance=0):
00091     if len(x) != len(y):
00092         return false
00093     for i in range(len(x)):
00094         if not (abs(x[i]-y[i]) <= tolerance):
00095             print "x["+str(i)+"]=", x[i]
00096             print "y["+str(i)+"]=", y[i]
00097             return false
00098     return true
00099 
00100 def alleq(x,y,tolerance=0):
00101     if x.size != y.size:
00102         print "x.size=", x.size
00103         print "y.size=", y.size
00104         return false
00105     if len(x.shape)==1:
00106         for i in range(len(x)):
00107             if not (abs(x[i]-y[i]) < tolerance):
00108                 print "x[",i,"]=", x[i]
00109                 print "y[",i,"]=", y[i]
00110                 return false
00111     if len(x.shape)==2:
00112         for i in range(len(x)):
00113             for j in range(len(x[i])):
00114                 if not (abs(x[i][j]-y[i][j]) < tolerance):
00115                     print "x[",i,"][",j,"]=", x[i][j]
00116                     print "y[",i,"][",j,"]=", y[i][j]
00117                     return false
00118     if len(x.shape)==3:
00119         for i in range(len(x)):
00120             for j in range(len(x[i])):
00121                 for k in range(len(x[i][j])):
00122                     if not (abs(x[i][j][k]-y[i][j][k]) < tolerance):
00123                         print "x[",i,"][",j,"][",k,"]=", x[i][j][k]
00124                         print "y[",i,"][",j,"][",k,"]=", y[i][j][k]
00125                         return false
00126     if len(x.shape)==4:
00127         for i in range(len(x)):
00128             for j in range(len(x[i])):
00129                 for k in range(len(x[i][j])):
00130                     for l in range(len(x[i][j][k])):
00131                         if not (abs(x[i][j][k][l]-y[i][j][k][l]) < tolerance):
00132                             print "x[",i,"][",j,"][",k,"][",l,"]=", x[i][j][k][l]
00133                             print "y[",i,"][",j,"][",k,"][",l,"]=", y[i][j][k][l]
00134                             return false
00135     if len(x.shape)>4:
00136         stop('unhandled array shape in alleq')
00137     return true
00138 
00139 
00140 def coordsystest():
00141     def test1():
00142         info('');
00143         info('');
00144         info('');
00145         info('Test 1 - coordsys, type, done, id, ncoordinates');
00146         info('       - coordinatetype, axiscoordinatetypes, summary');
00147         info('       - is_coordsys, coordsystools');
00148         #
00149         info('');
00150         info ('Testing coordsys constructor');
00151         info('');
00152         mycs = cs.newcoordsys()
00153         if not mycs:
00154             return stop('coordsys constructor 1 failed');
00155         #if not cs.iscoordsys(mycs): fail('is_coordsys 1 failed')
00156         #
00157         if mycs.ncoordinates()!=0: fail('ncoordinates 1 failed')
00158         if mycs.type()!='coordsys': fail('type 1 failed')
00159         #
00160         #id = mycs.id()
00161         #if not id: fail('id 1 failed')
00162         #ok = id.has_key('sequence') and id.has_key('pid')
00163         #ok = ok and id.has_key('time')
00164         #ok = ok and id.has_key('host')
00165         #ok = ok and id.has_key('agentid')
00166         #if not ok: fail(' id record has wrong fields')
00167         if not mycs.done(): fail()
00168         #
00169         mycs = cs.newcoordsys(direction=T);
00170         if not mycs: fail('coordsys constructor 2 failed')
00171         #if not cs.iscoordsys(mycs): fail('is_coordsys 2 failed')
00172         if mycs.ncoordinates()!=1: fail('ncoordinates 2 failed')
00173         if mycs.coordinatetype(0)!=['Direction']: fail('coordinatetype 1 failed')
00174         t1 = mycs.axiscoordinatetypes(T);
00175         t2 = mycs.axiscoordinatetypes(F);
00176         ok = t1[0]=='Direction' and t1[1]=='Direction'
00177         ok = ok and t2[0]=='Direction'
00178         ok = ok and t2[1]=='Direction'
00179         if not ok: fail('axiscoordinatetypes 1 failed')
00180         if not mycs.done(): fail()
00181         #
00182         mycs = cs.newcoordsys(spectral=T)
00183         if not mycs: fail('coordsys constructor 3 failed')
00184         #if not cs.iscoordsys(mycs): fail('is_coordsys 3 failed')
00185         if mycs.ncoordinates()!=1: fail('ncoordinates 3 failed')
00186         if mycs.coordinatetype(0)!=['Spectral']: fail('coordinatetype 2 failed')
00187         t1 = mycs.axiscoordinatetypes(T)
00188         t2 = mycs.axiscoordinatetypes(F)
00189         ok = (t1==['Spectral']) and (t2==['Spectral'])
00190         if not ok: fail('axiscoordinatetypes 2 failed')
00191         if not mycs.done(): fail()
00192         #
00193         mycs = cs.newcoordsys(stokes="I Q U V")
00194         if not mycs: fail('coordsys constructor 4 failed')
00195         #if not cs.iscoordsys(mycs): fail('is_coordsys 4 failed')
00196         if mycs.ncoordinates()!=1: fail('ncoordinates 4 failed')
00197         if mycs.coordinatetype(0)!=['Stokes']: fail('coordinatetype 3 failed')
00198         t1 = mycs.axiscoordinatetypes(T)
00199         t2 = mycs.axiscoordinatetypes(F)
00200         ok = t1==['Stokes'] and t2==['Stokes']
00201         if not ok: fail('axiscoordinatetypes 3 failed')
00202         if not mycs.done(): fail()
00203         #
00204         mycs = cs.newcoordsys(linear=3)
00205         if not mycs: fail('coordsys constructor 5 failed')
00206         #if not cs.iscoordsys(mycs): fail('is_coordsys 5 failed;'
00207         if mycs.ncoordinates()!=1: fail('ncoordinates 5 failed')
00208         if mycs.coordinatetype(0)!=['Linear']: fail('coordinatetype 4 failed')
00209         t1 = mycs.axiscoordinatetypes(T)
00210         t2 = mycs.axiscoordinatetypes(F)
00211         ok = t1[0]=='Linear' and t2[0]=='Linear'
00212         if not ok: fail('axiscoordinatetypes 4 failed')
00213         if not mycs.done(): fail()
00214         #
00215         mycs = cs.newcoordsys(tabular=T)
00216         if not mycs: fail('coordsys constructor 6 failed')
00217         #if not cs.iscoordsys(mycs): fail('is_coordsys 6 failed')
00218         if mycs.ncoordinates()!=1: fail('ncoordinates 6 failed')
00219         if mycs.coordinatetype(0)!=['Tabular']: fail('coordinatetype 6 failed')
00220         t1 = mycs.axiscoordinatetypes(T)
00221         t2 = mycs.axiscoordinatetypes(F)
00222         ok = t1==['Tabular'] and t2==['Tabular']
00223         if not ok: fail('axiscoordinatetypes 5 failed')
00224         if not mycs.done(): fail()
00225         #
00226         mycs = cs.newcoordsys(direction=T, spectral=T, stokes="I Q U V",
00227                               linear=1, tabular=T)
00228         if not mycs: fail('coordsys constructor 7 failed')
00229         #if not cs.iscoordsys(mycs): fail('is_coordsys 7 failed')
00230         if mycs.ncoordinates()!=5: fail('ncoordinates 7 failed')
00231         ok = mycs.coordinatetype(0)==['Direction']
00232         ok = ok and mycs.coordinatetype(1)==['Stokes']
00233         ok = ok and mycs.coordinatetype(2)==['Spectral']
00234         ok = ok and mycs.coordinatetype(3)==['Linear']
00235         ok = ok and mycs.coordinatetype(4)==['Tabular']
00236         if not ok: fail('coordinatetype 5 failed')
00237         t = mycs.coordinatetype()
00238         if not t: fail()
00239         if (t[0]!='Direction' or t[1]!='Stokes' or
00240             t[2]!='Spectral' or t[3]!='Linear' or
00241             t[4]!='Tabular'): fail('coordinatetype 6 failed')
00242         #
00243         t1 = mycs.axiscoordinatetypes(T)
00244         t2 = mycs.axiscoordinatetypes(F)
00245         ok = t1[0]=='Direction' and t1[1]=='Direction'
00246         ok = ok and (t1[2]=='Stokes' and t1[3]=='Spectral')
00247         ok = ok and (t1[4]=='Linear' and t1[5]=='Tabular')
00248         ok = ok and (t2[0]=='Direction' and t2[1]=='Direction')
00249         ok = ok and (t2[2]=='Stokes' and t2[3]=='Spectral')
00250         ok = ok and (t2[4]=='Linear' and t2[5]=='Tabular')
00251         if not ok: fail('axiscoordinatetypes 7 failed')
00252         #
00253         if not mycs.summary(): fail()
00254         if not mycs.summary(doppler='optical'): fail()
00255         if not mycs.summary(doppler='radio'): fail()
00256         #
00257         if not mycs.done(): fail()
00258         #
00259         cs1 = cs.newcoordsys()
00260         cs2 = cs.newcoordsys()
00261         #l = coordsystools()
00262         #ok = length(l)==2 and  l[0]=='cs1' and l[1]=='cs2'
00263         #if not ok:
00264         #    fail('coordsystools failed')
00265         #if not cs1.done(): fail()
00266         #if not cs2.done(): fail()
00267         ###
00268         return T
00269 
00270     def test2():
00271         info('')
00272         info('')
00273         info('')
00274         info('Test 2 - referencecode, setreferencecode')
00275         info('         restfrequency, setrestfrequency')
00276         info('         projection, setprojection')
00277         #
00278         # Frequency. Does not test reference value conversion
00279         # is correct
00280         #
00281         info('')
00282         info ('Testing referencecode')
00283         info('')
00284         mycs = cs.newcoordsys(direction=T, spectral=T)
00285         if not mycs: fail('coordsys constructor 1 failed')
00286         #
00287         d = me.frequency('LSRK')
00288         if not d: fail()
00289         list = me.listcodes(d)
00290         if not list: fail()
00291         #
00292         for i in list['normal']:
00293             if (i!='REST'):
00294                 ok = mycs.setreferencecode(type='spectral', value=i, adjust=T)
00295                 if not ok: fail()
00296                 if (mycs.referencecode(type='spectral')!=[i]):
00297                     fail('failed to recover spectral reference code '+i)
00298         #
00299         # Direction. Does not test reference value conversion
00300         # is correct
00301         #
00302         d = me.direction('J2000')
00303         if not d: fail()
00304         list = me.listcodes(d)
00305         if not list: fail()
00306         #
00307         for i in list['normal']:
00308             if not re.search("AZEL",i, re.IGNORECASE):
00309                 ok = mycs.setreferencecode(type='direction', value=i, adjust=F)
00310                 if not ok: fail()
00311                 if (mycs.referencecode(type='direction')!=[i]):
00312                     fail('failed to recover direction reference code '+i)
00313         if not mycs.done(): fail()
00314         #
00315         mycs = cs.newcoordsys(direction=T, spectral=T, linear=1)
00316         if not mycs: fail('coordsys constructor 2 failed')
00317         ok = mycs.setreferencecode(type='direction', value='B1950')
00318         if not ok: fail()
00319         ok = mycs.setreferencecode(type='spectral', value='BARY')
00320         if not ok: fail()
00321         #
00322         c = mycs.referencecode()
00323         ok = len(c)==3 and c[0]=='B1950' and c[1]=='BARY' and c[2]==''
00324         if not ok: fail('referencecode 3 failed')
00325         #
00326         try:
00327             ok = true
00328             note('Expect SEVERE error and Exception here')
00329             ok = mycs.setreferencecode(value='doggies')
00330         except Exception, e:
00331             note('Caught expected Exception')
00332             ok = false
00333         if ok: fail('setreferencecode unexpectedly did not fail')
00334         #
00335         # projection
00336         #
00337         info('')
00338         info ('Testing projection')
00339         info('')
00340         ok = mycs.setprojection('SIN', [1.0,2.0])
00341         if not ok: fail()
00342         p = mycs.projection()
00343         if not p: fail()
00344         ok = p['type']=='SIN' and len(p['parameters'])==2
00345         ok = ok and p['parameters'][0]==1.0
00346         ok = ok and p['parameters'][1]==2.0
00347         if not ok: fail('setprojection/projection 1 reflection failed')
00348         #
00349         p = mycs.projection('all')['types']
00350         if len(p)!=27: fail('projection 1 failed')
00351         for i in p:
00352             n = mycs.projection(i)
00353             if not n: fail()
00354         #
00355         try:
00356             ok = true
00357             note('Expect SEVERE error and Exception here')
00358             ok = mycs.setprojection('fish')
00359         except Exception, e:
00360             note('Caught expected Exception: '+str(e))
00361             ok = false
00362         if ok: fail('setprojection 1 unexpectedly did not fail')
00363         #
00364         # restfrequency
00365         #
00366         info('')
00367         info ('Testing restfrequency')
00368         info('')
00369         rf1 = qa.quantity('1.2GHz')
00370         if not rf1: fail()
00371         ok = mycs.setrestfrequency(rf1)
00372         if not ok: fail()
00373         rf2 = mycs.restfrequency()
00374         if not rf2: fail()
00375         rf2 = qa.convert(rf2,rf1['unit'])
00376         ok = abs(qa.getvalue(rf1)-qa.getvalue(rf2))<1.0e-6
00377         ok = ok and qa.getunit(rf1)==qa.getunit(rf2)
00378         if not ok: fail('setrestfrequency/restfrequency 1 reflection failed')
00379         #
00380         unit = qa.getunit(mycs.restfrequency())
00381         if not unit: fail()
00382         rf1 = 2.0
00383         ok = mycs.setrestfrequency(rf1)
00384         if not ok: fail()
00385         rf2 = mycs.restfrequency()
00386         if not rf2: fail()
00387         rf1 = qa.unit(rf1, unit)
00388         if not rf1: fail()
00389         ok = abs(qa.getvalue(rf1)-qa.getvalue(rf2))<1.0e-6
00390         ok = ok and qa.getunit(rf1)==qa.getunit(rf2)
00391         if not ok: fail('setrestfrequency/restfrequency 2 reflection failed')
00392         #
00393         rf1 = qa.quantity([1e9, 2e9], 'Hz')
00394         # Select second freq
00395         ok = mycs.setrestfrequency(value=rf1, which=1, append=F);
00396         rf2 = qa.convert(mycs.restfrequency(),qa.getunit(rf1))
00397         v1 = qa.getvalue(rf1)
00398         v2 = qa.getvalue(rf2)
00399         ok = abs(v1[0]-v2[1])<1e-6
00400         ok = ok and abs(v1[1]-v2[0])<1e-6
00401         ok = ok and qa.getunit(rf1)==qa.getunit(rf2)
00402         if not ok: fail('setrestfrequency/restfrequency 3 reflection failed')
00403         #
00404         rf1 = qa.quantity('1kg')
00405         try:
00406             ok = true
00407             note('Expect SEVERE error and Exception here')
00408             ok = mycs.setrestfrequency(rf1)
00409         except Exception, e:
00410             note('Caught expected Exception: '+str(e))
00411             ok = false
00412         if ok: fail('setrestfrequency 3 unexpectedly did not fail')
00413         if not mycs.done(): fail()
00414         #
00415         mycs = cs.newcoordsys(direction=T, spectral=F)
00416         try:
00417             note('Expect SEVERE error and Exception here')
00418             rf = mycs.restfrequency()
00419         except Exception, e:
00420             note('Caught expected Exception: '+str(e))
00421             rf = false
00422         if rf: fail('restfrequency unexpectedly did not fail')
00423         #
00424         rf1 = qa.quantity('1GHz')
00425         try:
00426             ok = true
00427             note('Expect SEVERE error and Exception here')
00428             ok = mycs.setrestfrequency(rf1)
00429         except Exception, e:
00430             note('Caught expected Exception: '+str(e))
00431             ok = false
00432         if ok: fail('setrestfrequency 4 unexpectedly did not fail')
00433         #
00434         if not mycs.done(): fail()
00435         #
00436         return T
00437 
00438     def test3():
00439         info('')
00440         info('')
00441         info('')
00442         info('Test 3 - torecord, fromrecord, copy')
00443         #
00444         mycs = cs.newcoordsys(direction=T, spectral=T, stokes="I Q U V",
00445                               linear=3)
00446         if not mycs: fail('coordsys constructor 1 failed')
00447         #
00448         r = mycs.torecord()
00449         if not r: fail('torecord 1 failed')
00450         ok = r.has_key('direction0') and r.has_key('stokes1')
00451         ok = ok and r.has_key('spectral2')
00452         ok = ok and r.has_key('linear3')
00453         if not ok: fail('torecord did not produce valid record')
00454         #
00455         cs2 = cs.newcoordsys(direction=F, spectral=F, stokes="", linear=0)
00456         if not cs2: fail('coordsys constructor 2 failed')
00457         ok = cs2.fromrecord(r)
00458         if not ok: fail('fromrecord 1 failed')
00459         #if not is_coordsys(cs2)) {
00460         # fail('fromrecord 1 did not produce a coordsys tool'
00461         #
00462         if not mycs.done(): fail()
00463         if not cs2.done(): fail()
00464         #
00465         mycs = cs.newcoordsys(direction=T)
00466         if not mycs: fail('coordsys constructor 3 failed')
00467         cs2 = mycs.copy()
00468         if not cs2: fail()
00469         ok = mycs.done()
00470         if (ok==false and cs2==false): fail('copy was a reference !')
00471         if not cs2.done(): fail;
00472         ###
00473         return T
00474 
00475     def test4():
00476         info('')
00477         info('')
00478         info('')
00479         info('Test 4 - setepoch, epoch, setobserver, observer')
00480         info('         settelescope, telescope, setparentname, parentname')
00481         #
00482         mycs = cs.newcoordsys(direction=T)
00483         if not mycs: fail('coordsys constructor 1 failed')
00484         #
00485         # Epoch
00486         #
00487         info('')
00488         info ('Testing epoch')
00489         info('')
00490         epoch1 = me.epoch('UTC', 'today')
00491         ok = mycs.setepoch(epoch1)
00492         if not ok: fail()
00493         epoch2 = mycs.epoch()
00494         #
00495         ok = abs(me.getvalue(epoch1)['m0']['value']-me.getvalue(epoch2)['m0']['value'])<1.0e-6
00496         ok = ok and me.getvalue(epoch1)['m0']['unit'] == me.getvalue(epoch2)['m0']['unit']
00497         ok = ok and me.gettype(epoch1) == me.gettype(epoch2)
00498         ok = ok and me.getref(epoch1) == me.getref(epoch2)
00499         if not ok:
00500             fail('setepoch/epoch reflection failed')
00501         #
00502         # Observer
00503         #
00504         info('')
00505         info ('Testing observer')
00506         info('')
00507         obs1 = 'Biggles'
00508         ok = mycs.setobserver(obs1)
00509         if not ok: fail()
00510         obs2 = mycs.observer()
00511         #
00512         ok = obs1==obs2
00513         if not ok:
00514             fail('setobserver/observer reflection failed')
00515         #
00516         # Telescope
00517         #
00518         info('')
00519         info ('Testing telescope')
00520         info('')
00521         tel1 = 'VLA'
00522         ok = mycs.settelescope(tel1)
00523         if not ok: fail()
00524         tel2 = mycs.telescope()
00525         #
00526         ok = tel1==tel2
00527         if not ok:
00528             fail('settelescope/telescope reflection failed')
00529         pos = me.observatory(mycs.telescope())
00530         if not me.ismeasure(pos): fail('telescope 1 failed')
00531         #
00532         # Parent name
00533         #
00534         info('')
00535         info ('Testing parentname')
00536         info('')
00537         pn1 = 'Biggles.image'
00538         ok = mycs.setparentname(pn1)
00539         if not ok: fail()
00540         pn2 = mycs.parentname()
00541         #
00542         ok = pn1==pn2
00543         if not ok:
00544             fail('setparentname/parentname reflection failed')
00545         #
00546         if not mycs.done(): fail()
00547         #
00548         ###
00549         return T
00550 
00551     def test5():
00552         info('')
00553         info('')
00554         info('')
00555         info('Test 5 - setnames, names, setunits, units')
00556         info('         setreferencepixel, referencepixel')
00557         info('         setreferencevalue, referencevalue')
00558         info('         setincrement, increment')
00559         info('         setlineartransform, lineartransform')
00560         info('         setstokes, stokes')
00561         #
00562         mycs = cs.newcoordsys(direction=T, spectral=T)
00563         if not mycs: fail('coordsys constructor 1 failed')
00564         #
00565         # Names
00566         #
00567         info('')
00568         info ('Testing names')
00569         info('')
00570         val1 = "a b c"
00571         ok = mycs.setnames(value=val1)
00572         if not ok: fail()
00573         val2 = mycs.names()
00574         #
00575         ok = val1.split()==val2
00576         if not ok: fail('setnames/names reflection 1 failed')
00577         #
00578         val2 = mycs.names('spec')
00579         ok = val2[0]==val1.split()[2]
00580         if not ok: fail('names 1 failed')
00581         #
00582         val1 = 'fish'
00583         ok = mycs.setnames(type='spec', value=val1)
00584         if not ok: fail()
00585         val2 = mycs.names('spec')
00586         ok = val2==[val1]
00587         if not ok: fail('setnames/names reflection 2 failed')
00588         if not mycs.done(): fail()
00589         #
00590         # Units
00591         #
00592         info('')
00593         info ('Testing units')
00594         info('')
00595         mycs = cs.newcoordsys(direction=T, spectral=T)
00596         if not mycs: fail('coordsys constructor 2 failed')
00597         val1 = "deg rad GHz"
00598         ok = mycs.setunits(value=val1)
00599         if not ok: fail()
00600         val2 = mycs.units()
00601         #
00602         ok = val1.split()==val2
00603         if not ok: fail('setunits/units 1 reflection failed')
00604         #
00605         try:
00606             ok = true
00607             note('Expect SEVERE error and Exception here')
00608             ok = mycs.setunits(value="Hz Hz Hz")
00609         except Exception, e:
00610             note('Caught expected Exception: '+str(e))
00611             ok = false
00612         if ok: fail('setunits 2 unexpectedly did not fail')
00613         try:
00614             ok = true
00615             note('Expect SEVERE error and Exception here')
00616             ok = mycs.setunits(value="m")
00617         except Exception, e:
00618             note('Caught expected Exception: '+str(e))
00619             ok = false
00620         if ok: fail('setunits 3 unexpectedly did not fail')
00621         #
00622         val1 = "deg rad GHz"
00623         ok = mycs.setunits(value=val1)
00624         if not ok: fail()
00625         val2 = mycs.units('spec')
00626         ok = val2[0]==val1.split()[2]
00627         if not ok: fail('units 1 failed')
00628         #
00629         val1 = 'kHz'
00630         ok = mycs.setunits(type='spec', value=val1)
00631         if not ok: fail()
00632         val2 = mycs.units('spec')
00633         ok = val2[0]==val1
00634         if not ok: fail('setunits/units reflection 2 failed')
00635         if not mycs.done(): fail()
00636         #
00637         mycs = cs.newcoordsys(direction=T, linear=2)
00638         if not mycs: fail('coordsys constructor 2b failed')
00639         val1 = "Hz kHz"
00640         ok = mycs.setunits(type='linear', value=val1, overwrite=T)
00641         if not ok: fail()
00642         val2 = mycs.units()
00643         ok = val1.split()[0]==val2[2] and val1.split()[1]==val2[3]
00644         if not ok: fail('setunits overwrite test failed')
00645         if not mycs.done(): fail;
00646         #
00647         # Reference pixel
00648         #
00649         info('')
00650         info ('Testing referencepixel')
00651         info('')
00652         mycs = cs.newcoordsys(direction=T, spectral=T)
00653         if not mycs: fail('coordsys constructor 3 failed')
00654         val1 = [0,1,2]
00655         ok = mycs.setreferencepixel(value=val1)
00656         if not ok: fail()
00657         val2 = mycs.referencepixel()['numeric']
00658         #
00659         ok = abs(val1[0]-val2[0])<1.0e-6
00660         ok = ok and abs(val1[1]-val2[1])<1.0e-6
00661         ok = ok and abs(val1[2]-val2[2])<1.0e-6
00662         if not ok:
00663             fail('setreferencepixel/referencepixel reflection failed')
00664         #
00665         val2 = mycs.referencepixel('dir')['numeric']
00666         if len(val2)!=2: fail()
00667         ok = abs(val2[0]-val1[0])<1.0e-6 and abs(val2[1]-val1[1])<1.0e-6
00668         if not ok: fail('referencepixel 1 failed')
00669         val2 = mycs.referencepixel('spec')['numeric']
00670         if len(val2)!=1: fail()
00671         ok = abs(val2-val1[2])<1.0e-6
00672         if not ok: fail('referencepixel 2 failed')
00673         #
00674         val1 = [0,0]
00675         ok = mycs.setreferencepixel(type='dir', value=val1)
00676         val2 = mycs.referencepixel('dir')['numeric']
00677         if len(val2)!=2: fail()
00678         ok = abs(val2[0]-val1[0])<1e-6 and abs(val2[1]-val1[1])<1e-6
00679         if not ok: fail('setreferencepixel 3 failed')
00680         #
00681         try:
00682             ok = true
00683             note('Expect SEVERE error and Exception here')
00684             ok = mycs.setreferencepixel (type='lin', value=[0,0])
00685         except Exception, e:
00686             note('Caught expected Exception: '+str(e))
00687             ok = false
00688         if ok: fail('setreferencepixel 1 unexpectedly did not fail')
00689         try:
00690             ok = true
00691             note('Expect SEVERE error and Exception here')
00692             ok = mycs.referencepixel('lin')
00693         except Exception, e:
00694             note('Caught expected Exception: '+str(e))
00695             ok = false
00696         if ok: fail('setreferencepixel 2 unexpectedly did not fail')
00697         if not mycs.done(): fail()
00698         #
00699         # linear transform
00700         #
00701         info('')
00702         info ('Testing lineartransform')
00703         info('')
00704         mycs = cs.newcoordsys(direction=T, spectral=T, stokes='IQ', tabular=T, linear=3)
00705         if not mycs: fail('coordsys constructor 3b failed')
00706         #
00707         val1 = ia.makearray(0,[2,2])
00708         val1[0,0] = cos(0.5)
00709         val1[0,1] = -sin(0.5)
00710         val1[1,0] = sin(0.5)
00711         val1[1,1] = cos(0.5)
00712         type = 'direction'
00713         ok = mycs.setlineartransform(value=val1, type=type)
00714         if not ok: fail()
00715         val2 = mycs.lineartransform(type=type)
00716         #
00717         ok = alleq(val1,val2,1.0e-6)
00718         if not ok:
00719             fail('direction setlineartransform/lineartransform reflection failed')
00720         ##
00721         val1 = ia.makearray(2,[1,1])
00722         type = 'spectral'
00723         ok = mycs.setlineartransform(value=val1, type=type)
00724         if not ok: fail()
00725         val2 = mycs.lineartransform(type=type)
00726         #
00727         ok = alleq(val1,val2,1.0e-6)
00728         if not ok:
00729             fail('spectral setlineartransform/lineartransform reflection failed')
00730         ##
00731         val1 = ia.makearray(2,[1,1])
00732         type = 'stokes'
00733         ok = mycs.setlineartransform(value=val1, type=type)
00734         if not ok: fail()
00735         val2 = mycs.lineartransform(type=type);  # Does not set ; returns T
00736         #
00737         ok = alleq(val1,val2,1.0e-6)
00738         if ok:
00739             fail('stokes setlineartransform/lineartransform reflection failed')
00740         ##
00741         val1 = ia.makearray(4,[1,1])
00742         type = 'tabular'
00743         ok = mycs.setlineartransform(value=val1, type=type)
00744         if not ok: fail()
00745         val2 = mycs.lineartransform(type=type)
00746         #
00747         ok = alleq(val1,val2,1.0e-6)
00748         if not ok:
00749             fail('tabular setlineartransform/lineartransform reflection failed')
00750         ##
00751         val1 = ia.makearray(0,[3,3])
00752         val1[0,0] = 2.0
00753         val1[1,1] = 3.0;
00754         val1[2,2] = 4.0
00755         type = 'linear'
00756         ok = mycs.setlineartransform(value=val1, type=type)
00757         if not ok: fail()
00758         val2 = mycs.lineartransform(type=type)
00759         #
00760         ok = alleq(val1,val2,1.0e-6)
00761         if not ok:
00762             fail('direction setlineartransform/lineartransform reflection failed')
00763         if not mycs.done(): fail()
00764         #
00765         # Reference value
00766         #
00767         info('')
00768         info ('Testing referencevalue')
00769         info('')
00770         mycs = cs.newcoordsys(direction=T)
00771         if not mycs: fail('coordsys constructor 4 failed')
00772         ok = mycs.setunits(value="rad rad")
00773         if not ok: fail()
00774         val1 = mycs.referencevalue(format='q')
00775         val1['quantity']['*1'] = qa.quantity('0.01rad')
00776         val1['quantity']['*2'] = qa.quantity('-0.01rad')
00777         ok = mycs.setreferencevalue(value=val1)
00778         if not ok: fail()
00779         val2 = mycs.referencevalue(format='q')
00780         if not val2: fail()
00781         #
00782         ok = abs(val1['quantity']['*1']['value']-val2['quantity']['*1']['value'])<1e-6
00783         ok = ok and abs(val1['quantity']['*2']['value']-val2['quantity']['*2']['value'])<1e-6
00784         ok = ok and val1['quantity']['*1']['unit']==val2['quantity']['*1']['unit']
00785         ok = ok and val1['quantity']['*2']['unit']==val2['quantity']['*2']['unit']
00786         if not ok:
00787             fail('setreferencevalue/referencevalue 1 reflection failed')
00788         if not mycs.done(): fail()
00789         #
00790         mycs = cs.newcoordsys(direction=T)
00791         if not mycs: fail('coordsys constructor 5 failed')
00792         units = mycs.units()
00793         if not units: fail()
00794         val1 = [1.0,2.0]
00795         ok = mycs.setreferencevalue(value=val1)
00796         if not ok: fail()
00797         val2 = mycs.referencevalue(format='q')
00798         if not val2: fail()
00799         ok = abs(val1[0]-val2['quantity']['*1']['value'])<1e-6
00800         ok = ok and abs(val1[1]-val2['quantity']['*2']['value'])<1e-6
00801         ok = ok and units[0]==val2['quantity']['*1']['unit']
00802         ok = ok and units[1]==val2['quantity']['*2']['unit']
00803         if not ok: fail('setreferencevalue/referencevalue 2 reflection failed')
00804         if not mycs.done(): fail()
00805         #
00806         mycs = cs.newcoordsys(spectral=T)
00807         if not mycs: fail('coordsys constructor 6 failed')
00808         try:
00809             ok = true
00810             note('Expect SEVERE error and Exception here')
00811             ok = mycs.setreferencevalue (value='i like doggies')
00812         except Exception, e:
00813             note('Caught expected Exception')
00814             ok = false
00815         if ok: fail('setreferencevalue unexpectedly did not fail')
00816         if not mycs.done(): fail()
00817         #
00818         mycs = cs.newcoordsys(direction=T, spectral=T)
00819         if not mycs: fail('coordsys constructor 7 failed')
00820         val1 = mycs.referencevalue(format='q')
00821         if not val1: fail()
00822         val2 = mycs.referencevalue(type='spec', format='q')
00823         if not val2: fail()
00824         ok = abs(val1['quantity']['*3']['value']-val2['quantity']['*1']['value'])<1e-6 and val1['quantity']['*3']['unit']==val2['quantity']['*1']['unit']
00825         if not ok: fail('referencevalue 1 failed')
00826         #
00827         val1 = [-10]
00828         ok = mycs.setreferencevalue(type='spec', value=val1)
00829         if not ok: fail()
00830         val2 = mycs.referencevalue(type='spec', format='n')
00831         if not val2: fail()
00832         ok = abs(val1[0]-val2['numeric'][0])<1e-6
00833         if not ok: fail('setreferencevalue 1 failed')
00834         #
00835         val1 = mycs.referencevalue(format='n')
00836         if not val1: fail()
00837         val2 = mycs.referencevalue(type='spec', format='n')
00838         if not val2: fail()
00839         ok = abs(val1['numeric'][2]-val2['numeric'][0])<1e-6
00840         if not ok: fail('referencevalue 2 failed')
00841         if not mycs.done(): fail()
00842         #
00843         # increment
00844         #
00845         info('')
00846         info ('Testing increment')
00847         info('')
00848         mycs = cs.newcoordsys(direction=T)
00849         if not mycs: fail('coordsys constructor 7 failed')
00850         ok = mycs.setunits(value="rad rad")
00851         if not ok: fail()
00852         val1 = mycs.increment(format='q')
00853         val1['quantity']['*1'] = qa.quantity('0.01rad')
00854         val1['quantity']['*2'] = qa.quantity('-0.01rad')
00855         ok = mycs.setincrement(value=val1)
00856         if not ok: fail()
00857         val2 = mycs.increment(format='q')
00858         if not val2: fail()
00859         #
00860         ok = abs(val1['quantity']['*1']['value']-val2['quantity']['*1']['value'])<1e-6
00861         ok = ok and abs(val1['quantity']['*2']['value']-val2['quantity']['*2']['value'])<1e-6
00862         ok = ok and val1['quantity']['*1']['unit']==val2['quantity']['*1']['unit']
00863         ok = ok and val1['quantity']['*2']['unit']==val2['quantity']['*2']['unit']
00864         if not ok:
00865             fail('setincrement/increment 1 reflection failed')
00866         if not mycs.done(): fail()
00867         #
00868         mycs = cs.newcoordsys(direction=T)
00869         if not mycs: fail('coordsys constructor 8 failed')
00870         units = mycs.units()
00871         if not units: fail()
00872         val1 = [1.0,2.0]
00873         ok = mycs.setincrement(value=val1)
00874         if not ok: fail()
00875         val2 = mycs.increment(format='q')
00876         if not val2: fail()
00877         ok = abs(val1[0]-val2['quantity']['*1']['value'])<1e-6
00878         ok = ok and abs(val1[1]-val2['quantity']['*2']['value'])<1e-6
00879         ok = ok and units[0]==val2['quantity']['*1']['unit']
00880         ok = ok and units[1]==val2['quantity']['*2']['unit']
00881         if not ok:
00882             fail('setincrement/increment 2 reflection failed')
00883         if not mycs.done(): fail()
00884         #
00885         mycs = cs.newcoordsys(spectral=T)
00886         if not mycs: fail('coordsys constructor 9 failed')
00887         try:
00888             ok = true
00889             note('Expect SEVERE error and Exception here')
00890             ok = mycs.setincrement(value='i like doggies')
00891         except Exception, e:
00892             note('Caught expected Exception')
00893             ok = false
00894         if ok: fail('setincrement 1 unexpectedly did not fail')
00895         if not mycs.done(): fail()
00896         #
00897         mycs = cs.newcoordsys(direction=T, spectral=T)
00898         if not mycs: fail('coordsys constructor 10 failed')
00899         val1 = [1.0, 2.0, 3.0]
00900         ok = mycs.setincrement(value=val1)
00901         if not ok: fail()
00902         val2 = mycs.increment(format='n', type='dir')
00903         ok = abs(val2['numeric'][0]-val1[0])<1e-6 and abs(val2['numeric'][1]-val1[1])<1e-6
00904         if not ok: fail('setincrement/increment 3 reflection failed')
00905         val2 = mycs.increment(type='spe',format='n')
00906         ok = abs(val2['numeric'][0]-val1[2])<1e-6
00907         if not ok: fail('setincrement/increment 4 reflection failed')
00908         try:
00909             note('Expect SEVERE error and Exception here')
00910             val2 = mycs.increment(type='lin', format='q')
00911         except Exception, e:
00912             note('Caught expected Exception')
00913             val2 = false
00914         if val2: fail('increment 2 unexpectedly did not fail')
00915         #
00916         val1 = [-10]
00917         ok = mycs.setincrement(type='spec', value=val1)
00918         if not ok: fail()
00919         val2 = mycs.increment(type='spec', format='n')
00920         if not val2: fail()
00921         ok = abs(val1[0]-val2['numeric'][0])<1e-6
00922         if not ok: fail('setincrement/increment 5 reflection failed')
00923         if not mycs.done(): fail;
00924         #
00925         # Stokes
00926         #
00927         mycs = cs.newcoordsys(stokes="I RL")
00928         if not mycs: fail()
00929         stokes = mycs.stokes()
00930         if not stokes: fail()
00931         if (stokes[0]!='I' and stokes[1]!='RL'):
00932             fail('stokes 1  recovered wrong values')
00933         ok = mycs.setstokes("XX V")
00934         if not ok: fail()
00935         stokes = mycs.stokes()
00936         if not stokes: fail()
00937         if (stokes[0]!='XX' and stokes[1]!='V'):
00938             fail('stokes 2 recovered wrong values')
00939         if not mycs.done(): fail;
00940         #
00941         mycs = cs.newcoordsys(direction=T)
00942         if not mycs: fail()
00943         try:
00944             stokes = true
00945             note('Expect SEVERE error and Exception here')
00946             stokes = mycs.stokes()
00947         except Exception, e:
00948             note('Caught expected Exception')
00949             stokes = false
00950         if stokes: fail('stokes 2 unexpectedly did not fail')
00951         try:
00952             ok = true
00953             note('Expect SEVERE error and Exception here')
00954             ok = mycs.setstokes("I V")
00955         except Exception, e:
00956             note('Caught expected Exception')
00957             ok = false
00958         if ok: fail('setstokes 2 unexpectedly did not fail')
00959         #
00960         if not mycs.done(): fail;
00961         #
00962         ###
00963         return T
00964 
00965     def test6():
00966         info('')
00967         info('')
00968         info('')
00969         info('Test 6 - findcoordinate, findaxis')
00970         #
00971         mycs = cs.newcoordsys()
00972         if not mycs: fail('coordsys constructor 1 failed')
00973 
00974         # findcoordinate
00975         #local pa, wa
00976         try:
00977             note('Expect SEVERE error and Exception here')
00978             ok = mycs.findcoordinate('fish', 1)
00979         except Exception, e:
00980             note('Caught expected Exception')
00981             ok = false
00982         if ok:
00983             return stop('findcoordinate 1 unexpectedly did not fail')
00984         ok = mycs.findcoordinate('dir', 20)
00985         if ok[0]:
00986             return stop('findcoordinate 2 unexpectedly did not fail')
00987         if not mycs.done(): fail()
00988         #
00989         mycs = cs.newcoordsys(direction=T, stokes="I V", spectral=T, linear=2)
00990         if not mycs: fail('coordsys constructor 1 failed')
00991         ok = mycs.findcoordinate('dir',0)
00992         if not ok or not ok[0]:
00993             return stop('findcoordinate 3 failed')
00994         pa = ok[1]
00995         wa = ok[2]
00996         if not (pa[0] == 0 and pa[1] == 1):
00997             return stop('find 3 pixel axes are wrong')
00998         if not (wa[0] == 0 and wa[1] == 1):
00999             return stop('find 3 world axes are wrong')
01000         #
01001         ok = mycs.findcoordinate('stokes',0)
01002         if not ok or not ok[0]:
01003             return stop('findcoordinate 4 failed')
01004         pa=ok[1]
01005         wa=ok[2]
01006         if not pa==2:
01007             return stop('findcoordinate 4 pixel axes are wrong')
01008         if not wa==2:
01009             return stop('findcoordinate 4 world axes are wrong')
01010         #
01011         ok = mycs.findcoordinate('spectral',0)
01012         if not ok or not ok[0]:
01013             return stop('findcoordinate 5 failed')
01014         pa=ok[1]
01015         wa=ok[2]
01016         if not pa==3:
01017             return stop('findcoordinate 5 pixel axes are wrong')
01018         if not wa==3:
01019             return stop('findcoordinate 5 world axes are wrong')
01020         #
01021         #
01022         ok = mycs.findcoordinate('linear',0)
01023         if not ok or not ok[0]:
01024             return stop('findcoordinate 6 failed')
01025         pa=ok[1]
01026         wa=ok[2]
01027         if not (pa[0]==4 and pa[1]==5):
01028             return stop('findcoordinate 6 pixel axes are wrong')
01029         if not (wa[0]==4 and wa[1]==5):
01030             return stop('findcoordinate 6 world axes are wrong')
01031         #
01032         if not mycs.done(): fail()
01033         #
01034         # findaxis
01035         #
01036         mycs = cs.newcoordsys(direction=T, linear=2)
01037         #local coord, axisincoord
01038         #
01039         ok = mycs.findaxis(T, 0)
01040         if not ok[0]: fail()
01041         coord=ok[1]
01042         axisincoord=ok[2]
01043         if (coord!=0 or axisincoord!=0):
01044             return stop('findaxis 0 values are wrong')
01045         #
01046         ok = mycs.findaxis(T, 1)
01047         if not ok[0]: fail()
01048         coord=ok[1]
01049         axisincoord=ok[2]
01050         if (coord!=0 or axisincoord!=1):
01051             return stop('findaxis 1 values are wrong')
01052         #
01053         ok = mycs.findaxis(T, 2)
01054         if not ok[0]: fail()
01055         coord=ok[1]
01056         axisincoord=ok[2]
01057         if (coord!=1 or axisincoord!=0):
01058             return stop('findaxis 2 values are wrong')
01059         #
01060         ok = mycs.findaxis(T, 3)
01061         if not ok[0]: fail()
01062         coord=ok[1]
01063         axisincoord=ok[2]
01064         if (coord!=1 or axisincoord!=1):
01065             return stop('findaxis 3 values are wrong')
01066         #
01067         try:
01068             note('Expect SEVERE error and Exception here')
01069             ok = mycs.findaxis(T, 4)
01070         except Exception, e:
01071             note('Caught expect Exception:' + str(e))
01072             ok = false
01073         if ok:
01074             return stop('findaxis 4 unexpectedly found the axis')
01075         #
01076         if not mycs.done(): fail()
01077 
01078         ###
01079         return T
01080  
01081     def test7():
01082         info('')
01083         info('')
01084         info('')
01085         info('Test 7 - toworld, toworldmany, topixel, topixelmany')
01086         #
01087         mycs = cs.newcoordsys(direction=T, spectral=T, stokes="I V", linear=2)
01088         if not mycs: fail('coordsys constructor 1 failed')
01089         #
01090         info('')
01091         info('Testing toworld')
01092         info('')
01093         #
01094         rp = mycs.referencepixel()['numeric']
01095         if len(rp)!=6: fail()
01096         rv = mycs.referencevalue(format='n')
01097         if not rv: fail()
01098         #d = abs(mycs.toworld(value=rp, format='n') - rv)
01099         #if not all(d<1e-6): fail('toworld 1 gives wrong values')
01100         if not alleq(mycs.toworld(value=rp, format='n')['numeric'],
01101                      rv['numeric'],tolerance=1e-6):
01102             fail('toworld 1 gives wrong values')
01103         #
01104         d = mycs.toworld(value=list(rp), format='q')
01105         u = mycs.units()
01106         if not u: fail()
01107         #
01108         if len(d['quantity'])!=len(rv['numeric']):
01109             fail('toworld 2 gives wrong number of quantities')
01110         for i in range(len(d['quantity'])):
01111             if abs(d['quantity']['*'+str(i+1)]['value']-rv['numeric'][i])>1e-6:
01112                 fail('toworld 2 gives wrong values')
01113             if d['quantity']['*'+str(i+1)]['unit'] != u[i]:
01114                 fail('toworld 2 gives wrong units')
01115         #
01116         q = mycs.toworld(value=rp, format='q')
01117         if not q: fail()
01118         m = mycs.toworld(value=rp, format='m')
01119         if not m: fail()
01120         m = m['measure']
01121         ok = m.has_key('direction') and m.has_key('spectral')
01122         ok = ok and m['spectral'].has_key('frequency')
01123         ok = ok and m['spectral'].has_key('opticalvelocity')
01124         ok = ok and m['spectral'].has_key('radiovelocity')
01125         ok = ok and m['spectral'].has_key('betavelocity')
01126         ok = ok and m.has_key('stokes')
01127         ok = ok and m.has_key('linear')
01128         if not ok: fail('toworld 3 gives wrong fields')
01129         d = m['direction']
01130         f = m['spectral']['frequency']
01131         l = m['linear']
01132         s = m['stokes']
01133         #
01134         v = me.getvalue(d)
01135         q['quantity']['*1'] = qa.convert(q['quantity']['*1'], v['m0']['unit'])
01136         q['quantity']['*2'] = qa.convert(q['quantity']['*2'], v['m1']['unit'])
01137         ok = abs(v['m0']['value']-q['quantity']['*1']['value'])<1e-6
01138         ok = ok and abs(v['m1']['value']-q['quantity']['*2']['value'])<1e-6
01139         ok = ok and v['m0']['unit']==q['quantity']['*1']['unit']
01140         ok = ok and v['m1']['unit']==q['quantity']['*2']['unit']
01141         if not ok: fail('toworld 3 gives wrong direction values')
01142         #
01143         v = me.getvalue(f)
01144         q['quantity']['*4'] = qa.convert(q['quantity']['*4'], v['m0']['unit'])
01145         ok = abs(v['m0']['value']-q['quantity']['*4']['value'])<1e-6
01146         ok = ok and v['m0']['unit']==q['quantity']['*4']['unit']
01147         if not ok: fail('toworld 3 gives wrong frequency values')
01148         #
01149         q['quantity']['*5'] = qa.convert(q['quantity']['*5'], l['*1']['unit'])
01150         q['quantity']['*6'] = qa.convert(q['quantity']['*6'], l['*2']['unit'])
01151         ok = abs(l['*1']['value']-q['quantity']['*5']['value'])<1e-6
01152         ok = ok and abs(l['*2']['value']-q['quantity']['*6']['value'])<1e-6
01153         ok = ok and l['*1']['unit']==q['quantity']['*5']['unit']
01154         ok = ok and l['*2']['unit']==q['quantity']['*6']['unit']
01155         if not ok: fail('toworld 3 gives wrong linear values')
01156         #
01157         # toworldmany - any is as good as any other
01158         #
01159         p = mycs.referencepixel()['numeric']
01160         w = mycs.referencevalue()
01161         rIn = ia.makearray(0, [len(p), 10])
01162         for i in range(10):
01163             for j in range(len(p)):
01164                 rIn[j,i] = p[j]
01165         rOut = mycs.toworldmany(rIn)
01166         if len(rOut['numeric'])!=len(rIn): fail()
01167         for i in range(10):
01168             for j in range(len(p)):
01169                 if not (rOut['numeric'][j,i] - w['numeric'][j]) < 1e-6:
01170                     fail('toworldmany 1 gives wrong values')
01171         #
01172         # topixel
01173         #
01174         info('')
01175         info('Testing topixel')
01176         info('')
01177         #
01178         tol = 1.0e-6
01179         rp = mycs.referencepixel()['numeric']
01180         if len(rp)!=6: fail()
01181         rv = mycs.referencevalue(format='n')
01182         if not rv: fail()
01183         p = mycs.topixel(value=rv)['numeric']
01184         if len(p)!=6: fail()
01185         if not all(p, rp, tol):
01186             fail('topixel 1 gives wrong values')
01187         #
01188         for format in ["n","q","m","s","nqms"]:
01189             for i in range(len(rp)): p[i] = rp[i]+1
01190             w = mycs.toworld(value=p, format=format)
01191             if not w: fail()
01192             #
01193             p2 = mycs.topixel(value=w)['numeric']
01194             if len(p2)!=6: fail()
01195             if not all(p, p2, tol):
01196                 s = 'toworld/topixel reflection failed for format "'+format+'"'
01197                 fail(s)
01198             #
01199             # topixelmany - any is as good as any other
01200             #
01201             n = 10
01202             p = mycs.referencepixel()['numeric']
01203             w = mycs.toworld(p, 'n')
01204             w = w['numeric']
01205             rIn = ia.makearray(0, [len(w), n])
01206             for i in range(n):
01207                 for j in range(len(w)):
01208                     rIn[j,i] = w[j]
01209             r2 = mycs.topixelmany(rIn)
01210             if len(r2['numeric'])!=len(rIn): fail()
01211             for i in range(n):
01212                 for j in range(len(w)):
01213                     if not abs((p[j]-r2['numeric'][j,i]) < 1e-6):
01214                         fail('topixelmany 1 gives wrong values')
01215             #
01216             if not mycs.done(): fail()
01217             ###
01218             return T
01219 
01220     def test8():
01221         info('')
01222         info('')
01223         info('')
01224         info('Test 8 - naxes, axesmap')
01225         #
01226         info('')
01227         info('Testing naxes')
01228         info('')
01229         mycs = cs.newcoordsys()
01230         if not mycs: fail('coordsys constructor 1 failed')
01231         n = mycs.naxes()
01232         if not n==0: fail('naxes 1 failed')
01233         if mycs.naxes()!=0: fail('naxes 1 gave wrong result')
01234         if not mycs.done(): fail()
01235         #
01236         mycs = cs.newcoordsys(direction=T, spectral=T, stokes="I V", linear=2)
01237         if not mycs: fail('coordsys constructor 2 failed')
01238         n = mycs.naxes()
01239         if not n: fail('naxes 2 failed')
01240         if mycs.naxes()!=6: fail('naxes 2 gave wrong result')
01241         #
01242         info('')
01243         info('Testing axesmap')
01244         info('')
01245         #
01246         # Since I have no way to reorder the world and pixel axes
01247         # from Glish, all I can do is check the maps are the
01248         # same presently
01249         #
01250         toworld = mycs.axesmap(toworld=T)
01251         print toworld
01252         if not len(toworld): fail()
01253         topixel = mycs.axesmap(toworld=F)
01254         if not len(topixel): fail()
01255         #
01256         idx = range(0,len(mycs.referencepixel()['numeric']))
01257         if not all(toworld,idx): fail('toworld map is wrong')
01258         if not all(topixel,idx): fail('topixel map is wrong')
01259         #
01260         if not mycs.done(): fail()
01261         ###
01262         return T
01263 
01264     def test9():
01265         info('')
01266         info('')
01267         info('')
01268         info('Test 9 - reorder')
01269         #
01270         info('')
01271         info('Testing reorder')
01272         info('')
01273         mycs = cs.newcoordsys(direction=T, spectral=T, stokes='I V', linear=1)
01274         if not mycs: fail('coordsys constructor 1 failed')
01275         #order = [4,3,2,1]
01276         order = [3,2,1, 0]
01277         ok = mycs.reorder(order)
01278         if not ok: fail('reorder 1 failed')
01279         ok = mycs.coordinatetype(0)==['Linear']
01280         ok = ok and mycs.coordinatetype(1)==['Spectral']
01281         ok = ok and mycs.coordinatetype(2)==['Stokes']
01282         ok = ok and mycs.coordinatetype(3)==['Direction']
01283         if not ok: fail('reorder reordered incorrectly')
01284         #
01285         try:
01286             note("Expect SEVERE error and Exception here")
01287             ok = mycs.reorder([1,2])
01288         except Exception, e:
01289             note("Caught expected Exception")
01290             ok = false
01291         if ok: fail('reorder 2 unexpectedly did not fail')
01292         #
01293         try:
01294             note('Expect SEVERE error and Exception here')
01295             ok = mycs.reorder([1,2,3,10])
01296         except Exception, e:
01297             note('Caught expected Exception')
01298             ok = false
01299         if ok: fail('reorder 3 unexpectedly did not fail')
01300         #
01301         if not mycs.done(): fail()
01302         ###
01303         return T
01304 
01305     def test10():
01306         info('')
01307         info('')
01308         info('')
01309         info('Test 10 - frequencytovelocity, velocitytofrequency')
01310         #
01311         mycs = cs.newcoordsys(spectral=T)
01312         if not mycs: fail('coordsys constructor 1 failed')
01313         #
01314         info('')
01315         info('Testing frequencytovelocity')
01316         info('')
01317 
01318         # Set rest freq to reference freq
01319         rv = mycs.referencevalue(format='n')
01320         if not rv: fail()
01321         restFreq = rv['numeric'][0]
01322         ok = mycs.setrestfrequency(restFreq)
01323         if not ok: fail()
01324 
01325         # Find radio velocity increment
01326         df = mycs.increment(format='n')
01327         c = qa.constants('c')['value']                          #m/s
01328         drv = -c * df['numeric'] / rv['numeric'][0] / 1000.0    #km/s
01329         #
01330         freq = rv['numeric'][0]
01331         freqUnit = mycs.units();
01332         print freqUnit
01333         vel = mycs.frequencytovelocity(value=freq, frequnit=freqUnit[0],
01334                                        doppler='radio', velunit='km/s')
01335         if (abs(vel) > 1e-6):
01336             fail('frequencytovelocity 1 got wrong values')
01337         freq2 = mycs.velocitytofrequency(value=vel, frequnit=freqUnit[0],
01338                                          doppler='optical', velunit='km/s')
01339         if (abs(freq2-freq) > 1e-6):
01340             fail('velocitytofrequency 1 got wrong values')
01341         ##
01342             vel = mycs.frequencytovelocity(value=freq, frequnit=freqUnit[0],
01343                                            doppler='optical', velunit='km/s')
01344         if (abs(vel) > 1e-6):
01345             fail('frequencytovelocity 2 got wrong values')
01346         #
01347         freq2 = mycs.velocitytofrequency(value=vel, frequnit=freqUnit[0],
01348                                          doppler='optical', velunit='km/s')
01349         if (abs(freq2-freq) > 1e-6):
01350             fail('velocitytofrequency 2 got wrong values')
01351         ##
01352         rp = mycs.referencepixel()['numeric']
01353         if rp!=0.0: fail()
01354         freq = mycs.toworld (value=rp+1, format='n')
01355         vel = mycs.frequencytovelocity(value=list(freq['numeric']),
01356                                        frequnit=freqUnit[0],
01357                                        doppler='radio', velunit='m/s')
01358         d = abs(vel - (1000.0*drv))
01359         if (d > 1e-6):
01360             fail('frequencytovelocity 3 got wrong values')
01361         freq2 = mycs.velocitytofrequency(value=vel, frequnit=freqUnit[0],
01362                                          doppler='radio', velunit='m/s')
01363         if (abs(freq2-freq['numeric']) > 1e-6):
01364             fail('velocitytofrequency 3 got wrong values')
01365         ##
01366         freq = [rv['numeric'][0], freq['numeric'][0]]
01367         vel = mycs.frequencytovelocity(value=freq, frequnit=freqUnit[0],
01368                                        doppler='radio', velunit='m/s')
01369         if (len(vel)!=2):
01370             fail('frequencytovelocity 4 returned wrong length vector')
01371         d1 = abs(vel[0] - 0.0)
01372         d2 = abs(vel[1] - (1000.0*drv))
01373         if (d1>1e-6 or d2>1e-6):
01374             fail('frequencytovelocity 4 got wrong values')
01375         freq2 = mycs.velocitytofrequency(value=vel, frequnit=freqUnit[0],
01376                                          doppler='radio', velunit='m/s')
01377         d1 = abs(freq[0] - freq2[0])
01378         d2 = abs(freq[1] - freq2[1])
01379         if (d1>1e-6 or d2>1e-6):
01380             fail('velocitytofrequency 4 got wrong values')
01381 
01382         # Forced errors
01383         try:
01384             note('Expect SEVERE error and Exception here')
01385             vel = true
01386             vel = mycs.frequencytovelocity(value=rv['numeric'][0],
01387                                            frequnit='Jy',
01388                                            doppler='radio', velunit='km/s')
01389         except Exception, e:
01390             note('Caught expected Exception')
01391             vel = false
01392         if vel: fail('frequencytovelocity 5 unexpectedly did not fail')
01393         try:
01394             note('Expect SEVERE error and Exception here')
01395             freq = true
01396             freq = mycs.velocitytofrequency(value=rv['numeric'][0],
01397                                             frequnit='Jy',
01398                                             doppler='radio', velunit='km/s')
01399         except Exception, e:
01400             note('Caught expected Exception')
01401             freq = false
01402         if freq: fail('velocitytofrequency 5 unexpectedly did not fail')
01403         ##
01404         try:
01405             note('Expect SEVERE error and Exception here')
01406             vel = true
01407             vel = mycs.frequencytovelocity(value=rv['numeric'][0],
01408                                            frequnit='GHz',
01409                                            doppler='radio', velunit='doggies')
01410         except Exception, e:
01411             note('Caught expected Exception: '+str(e))
01412             vel = false
01413         if vel: fail('frequencytovelocity 6 unexpectedly did not fail')
01414         try:
01415             note('Expect SEVERE error and Exception here')
01416             freq = true
01417             freq = mycs.velocitytofrequency(value=rv['numeric'][0],
01418                                             frequnit='GHz',
01419                                             doppler='radio', velunit='doggies')
01420         except Exception, e:
01421             note('Caught expected Exception: '+str(e))
01422             freq = false
01423         if freq:
01424             fail('velocitytofrequency 6 unexpectedly did not fail')
01425         #
01426         if not mycs.done(): fail()
01427         #
01428         mycs = cs.newcoordsys(direction=T, spectral=F)
01429         if not mycs: fail('coordsys constructor 2 failed')
01430         try:
01431             note('Expect SEVERE error and Exception here')
01432             vel = true
01433             vel = mycs.frequencytovelocity(value=[1.0], frequnit='Hz',
01434                                            doppler='radio', velunit='km/s')
01435         except Exception, e:
01436             note('Caught expected Exception')
01437             vel = false
01438         if vel:
01439             fail('frequencytovelocity 7 unexpectedly did not fail')
01440         if not mycs.done(): fail()
01441         ###
01442         return T
01443 
01444     def test11():
01445         info('')
01446         info('')
01447         info('')
01448         info('Test 11 - setreferencelocation')
01449         #
01450         mycs = cs.newcoordsys(linear=2, spectral=T)
01451         if not mycs: fail('coordsys constructor 1 failed')
01452         #
01453         p = [1.0, 1.0, 1.0]
01454         ok = mycs.setreferencepixel(value=p)
01455         if not ok: fail()
01456         rp = mycs.referencepixel()['numeric']
01457         if len(rp)!=3: fail()
01458         if not all(rp,p,1e-6): fail('setreferencepixel/referencepixel reflection failed')
01459         #
01460         shp = [101,101,10]
01461         inc = mycs.increment(format='n')['numeric']
01462         if len(inc)!=3: fail()
01463         w = mycs.toworld([1,1,1], 'n')['numeric']
01464         if len(w)!=3: fail()
01465         w += inc
01466         p = [51,51,5]  #p = ((shp-1)/2.0) + 1
01467         #
01468         ok = mycs.setreferencelocation (pixel=p, world=w, mask=[T,T,T])
01469         if not ok: fail()
01470         #
01471         rp = mycs.referencepixel()['numeric']
01472         if len(rp)!=3: fail()
01473         rv = mycs.referencevalue(format='n')['numeric']
01474         if len(rv)!=3: fail()
01475         #
01476         ok = abs(rv[0]-w[0])<1e-6 and abs(rv[1]-w[1])<1e-6
01477         ok = ok and abs(rv[2]-w[2])<1e-6
01478         if not ok: fail('setreferencelocation recovered wrong reference value')
01479         #
01480         ok = abs(rp[0]-p[0])<1e-6 and abs(rp[1]-p[1])<1e-6
01481         ok = ok and abs(rp[2]-p[2])<1e-6
01482         if not ok: fail('setreferencelocation recovered wrong reference pixel')
01483         #
01484         if not mycs.done(): fail;
01485         #
01486         ###
01487         return T
01488 
01489     def test12():
01490         info('')
01491         info('')
01492         info('')
01493         info('Test 12 - toabs, torel, toabsmany, torelmany')
01494         #
01495         mycs = cs.newcoordsys(direction=T, spectral=T, stokes="I V LL", linear=2)
01496         if not mycs: fail('coordsys constructor 1 failed')
01497         #
01498         info('')
01499         info('Testing torel/toabs on pixel coordinates')
01500         info('')
01501         #
01502         p = mycs.referencepixel()
01503         pn = p['numeric']  # pointer to numeric part of p
01504         for i in range(len(pn)): pn[i]=pn[i]+1
01505         if len(pn)!=6: fail()
01506         p2 = mycs.torel(p)
01507         if not p2: fail()
01508         p3 = mycs.toabs(p2)['numeric']
01509         if len(p3)==0: fail()
01510         d = abs(p3 - pn)
01511         for i in range(len(d)):
01512             if d[i]>1e-6 : fail('torel/toabs pixel reflection test 1 failed')
01513         #
01514         info('')
01515         info('Testing torel/toabs on world coordinates')
01516         info('')
01517         #
01518         p = mycs.referencepixel()['numeric']
01519         for i in range(len(p)): p[i] += 1
01520         if len(p)!=6: fail()
01521         #
01522         for f in ["n","q","s"]:
01523             w = mycs.toworld(p, format=f)
01524             if not w: fail()
01525             #
01526             w2 = mycs.torel(w)
01527             if not w2: fail()
01528             w3 = mycs.toabs(w2)
01529             if not w3: fail()
01530             #
01531             p2 = mycs.topixel(w3)['numeric']
01532             if len(p2)!=6: fail()
01533             if not all(p2,p,1e-6):
01534                 s = 'torel/toabs world reflection test 1 failed for format "'+f+'"'
01535                 fail(s)
01536         #
01537         p = mycs.referencepixel()
01538         if not p: fail()
01539         try:
01540             note("Expect SEVERE error and Exception here")
01541             p2 = mycs.toabs(p)
01542         except Exception, e:
01543             note("Caught expected Exception")
01544             p2 = false
01545         if p2: fail('toabs 1 unexpectedly did not fail')
01546         #
01547         p2 = mycs.torel(p)
01548         if not p2: fail()
01549         try:
01550             note('Expect SEVERE error and Exception here')
01551             p3 = mycs.torel(p2)
01552         except Exception, e:
01553             note('Caught expected Exception')
01554             p3 = false
01555         if p3: fail('torel 1 unexpectedly did not fail')
01556         #
01557         w = mycs.referencevalue()
01558         if not w: fail()
01559         try:
01560             note('Expect SEVERE error and Exception here')
01561             w2 = mycs.toabs(w)
01562         except:
01563             note('Caught expected exception')
01564             w2 = false
01565         if w2: fail('toabs 2 unexpectedly did not fail')
01566         #
01567         w2 = mycs.torel(w)
01568         if not w2: fail()
01569         try:
01570             note('Expect SEVERE error and Exception here')
01571             w3 = mycs.torel(w2)
01572         except Exception, e:
01573             note('Caught expected Exception')
01574             w3 = false
01575         if w3: fail('torel 2 unexpectedly did not fail')
01576         #
01577         # toabsmany, torelmany
01578         #
01579         info('')
01580         info('Testing toabsmany/torelmany')
01581         info('')
01582         p = mycs.referencepixel()['numeric']
01583         w = mycs.toworld(p, 'n')['numeric']
01584         n = 5
01585         pp = ia.makearray(0.0, [len(p), n])
01586         ww = ia.makearray(0.0, [len(w), n])
01587         for i in range(n):
01588             for j in range(len(p)):
01589                 pp[j,i] = p[i]
01590             for j in range(len(w)):
01591                 ww[j,i] = w[i]
01592         #
01593         relpix = mycs.torelmany(pp, F)
01594         if len(relpix['numeric'])!=len(p): fail()
01595         abspix = mycs.toabsmany(relpix, F)
01596         if len(abspix['numeric'])!=len(relpix['numeric']): fail()
01597         #
01598         relworld = mycs.torelmany(ww, T)
01599         if len(relworld['numeric'])!=len(w): fail()
01600         absworld = mycs.toabsmany(relworld, T)
01601         if len(absworld['numeric'])!=len(relworld['numeric']): fail()
01602         #
01603         for i in range(n):
01604             for j in range(len(p)):
01605                 if not abs(p[j]-abspix['numeric'][j,i])<1e-6:
01606                     fail('toabsmany/torelmany gives wrong values for pixels')
01607             #
01608             for j in range(len(w)):
01609                 #if not (abs(w[j])-absworld[j,i])<1e-6:
01610                 if not alleq(w[j],absworld['numeric'][j,i],1e-6):
01611                     fail('toabsmany/torelmany gives wrong values for world')
01612             #
01613         if not mycs.done(): fail()
01614         ###
01615         return T
01616  
01617 
01618     def test13():
01619         info('')
01620         info('')
01621         info('Test 13 - convert, convertmany')
01622         #
01623         mycs = cs.newcoordsys(direction=T, spectral=T, stokes="I V LL",
01624                               linear=2)
01625         if not mycs: fail('coordsys constructor 1 failed')
01626         tol = 1.0e-6
01627         n = mycs.naxes()
01628 
01629         ####################
01630         # abs pix to abs pix
01631         absin = n*[T]
01632         unitsin = n*['pix']
01633         coordin = mycs.referencepixel()['numeric'] # Make sure in range of stokes
01634         for i in range(len(coordin)):
01635             coordin[i] += 2
01636         absout = n*[T]
01637         unitsout = n*['pix']
01638         dopplerin = 'radio'
01639         dopplerout = 'radio'
01640         #
01641         p = mycs.convert(list(coordin), absin, dopplerin, unitsin,
01642                          absout, dopplerout, unitsout)
01643         if not len(p): fail()
01644         if not all(p,coordin,tol): fail('convert 1 gives wrong values')
01645 
01646         # abs pix to rel pix
01647         absout = n * [F]
01648         p = mycs.convert(list(coordin), absin, dopplerin, unitsin,
01649                          absout, dopplerout, unitsout)
01650         if not len(p): fail()
01651         #
01652         p2 = mycs.torel(coordin, F)['numeric']
01653         if len(p2)!=n: fail()
01654         if not all(p,p2,tol):
01655             fail('convert 2 gives wrong values')
01656 
01657         # rel pix to abs pix
01658         absin = n * [F]
01659         rp = mycs.referencepixel()['numeric']
01660         for i in range(len(rp)): rp[i] += 2
01661         coordin = list(mycs.torel(rp, F)['numeric'])
01662         if len(coordin)!=n: fail()
01663         absout = n * [T]
01664         #
01665         p = mycs.convert(coordin, absin, dopplerin, unitsin,
01666                          absout, dopplerout, unitsout)
01667         if not len(p): fail()
01668         #
01669         p2 = mycs.referencepixel()['numeric']
01670         if len(p2)!=n: fail()
01671         for i in range(len(p2)): p2[i] += 2
01672         if not all(p,p2,tol): fail('convert 3 gives wrong values')
01673 
01674         #######################
01675         # abs pix to abs world
01676         absin = n * [T]
01677         coordin = mycs.referencepixel()['numeric'] + 2
01678         if len(coordin)!=n: fail()
01679         unitsin = n * ['pix']
01680         absout = n * [T]
01681         unitsout = mycs.units()
01682         if not unitsout: fail()
01683         #
01684         p = mycs.convert(list(coordin), absin, dopplerin, unitsin,
01685                          absout, dopplerout, unitsout)
01686         if not len(p): fail()
01687         #
01688         p2 = mycs.referencepixel()['numeric'] + 2
01689         if len(p2)!=n: fail()
01690         w = mycs.toworld(p2)['numeric']
01691         if len(w)!=n: fail()
01692         if not all(p,w,tol): fail('convert 4 gives wrong values')
01693 
01694         # abs pix to rel world
01695         absin = n * [T]
01696         coordin = list(mycs.referencepixel()['numeric']+2)
01697         if len(coordin)!=n: fail()
01698         unitsin = n * ['pix']
01699         absout = n * [F]
01700         unitsout = mycs.units()
01701         if not unitsout: fail()
01702         #
01703         p = mycs.convert(coordin, absin, dopplerin, unitsin,
01704                          absout, dopplerout, unitsout)
01705         if not len(p): fail()
01706         #
01707         p2 = mycs.referencepixel()['numeric'] + 2
01708         if len(p2)!=n: fail()
01709         w = mycs.torel(mycs.toworld(p2),T)['numeric']
01710         if not all(p,w,tol): fail('convert 5 gives wrong values')
01711         
01712         # rel pix to abs world
01713         absin = n * [F]
01714         coordin = mycs.torel(mycs.referencepixel()['numeric']+2,F)
01715         if not coordin: fail()
01716         unitsin = n * ['pix']
01717         absout = n * [T]
01718         unitsout = mycs.units()
01719         if not unitsout: fail()
01720         #
01721         p = mycs.convert(list(coordin['numeric']), absin, dopplerin, unitsin,
01722                          absout, dopplerout, unitsout)
01723         if not len(p): fail()
01724         #
01725         p2 = mycs.referencepixel()['numeric']+2
01726         if len(p2)!=n: fail()
01727         w = mycs.toworld(p2)['numeric']
01728         if len(w)!=n: fail()
01729         if not all(p,w,tol): fail('convert 6 gives wrong values')
01730 
01731         # rel pix to rel world
01732         absin = n * [F]
01733         coordin = mycs.torel(mycs.referencepixel()['numeric']+2,F)
01734         if not coordin: fail()
01735         unitsin = n * ['pix']
01736         absout = n * [F]
01737         unitsout = mycs.units()
01738         #
01739         p = mycs.convert(list(coordin['numeric']), absin, dopplerin, unitsin,
01740                          absout, dopplerout, unitsout)
01741         if not len(p): fail()
01742         #
01743         p2 = mycs.referencepixel()['numeric']+2
01744         if len(p2)!=n: fail()
01745         w = mycs.torel(mycs.toworld(p2),T)['numeric']
01746         if len(w)!=6: fail()
01747         if not all(p,w,tol): fail('convert 7 gives wrong values')
01748 
01749         #######################
01750         # abs world to abs pix
01751         absin = n * [T]
01752         coordin = mycs.toworld(mycs.referencepixel()['numeric']+2)
01753         if not coordin: fail()
01754         unitsin = mycs.units()
01755         if not unitsout: fail()
01756         absout = n * [T]
01757         unitsout = n * ['pix']
01758         #
01759         p = mycs.convert(list(coordin['numeric']), absin, dopplerin, unitsin,
01760                          absout, dopplerout, unitsout)
01761         if not len(p): fail()
01762         #
01763         p2 = mycs.referencepixel()['numeric'] + 2
01764         if len(p2)!=n: fail()
01765         if not all(p,p2,tol): fail('convert 8 gives wrong values')
01766 
01767         # abs world to rel pix
01768         absin = n * [T]
01769         coordin = mycs.toworld(mycs.referencepixel()['numeric']+2)
01770         if not coordin: fail()
01771         unitsin = mycs.units()
01772         if not unitsin: fail()
01773         unitsout = n * ['pix']
01774         absout = n * [F]
01775         #
01776         p = mycs.convert(list(coordin['numeric']), absin, dopplerin, unitsin,
01777                          absout, dopplerout, unitsout)
01778         if not len(p): fail()
01779         #
01780         p2 = mycs.torel(mycs.referencepixel()['numeric']+2,F)['numeric']
01781         if len(p2)!=n: fail()
01782         if not all(p,p2,tol): fail('convert 9 gives wrong values')
01783 
01784         # rel world to abs pix
01785         absin = n * [F]
01786         coordin = mycs.torel(mycs.toworld(mycs.referencepixel()['numeric']+2),T)
01787         if not coordin: fail()
01788         unitsin = mycs.units()
01789         if not unitsin: fail()
01790         absout = n * [T]
01791         unitsout = n * ['pix']
01792         #
01793         p = mycs.convert(list(coordin['numeric']), absin, dopplerin, unitsin,
01794                          absout, dopplerout, unitsout)
01795         if not len(p): fail()
01796         #
01797         p2 = mycs.referencepixel()['numeric']+2
01798         if len(p2)!=n: fail()
01799         if not all(p,p2,tol): fail('convert 10 gives wrong values')
01800 
01801         # rel world to rel pix
01802         absin = n * [F]
01803         coordin = mycs.torel(mycs.toworld(mycs.referencepixel()['numeric']+2),T)
01804         if not coordin: fail()
01805         unitsin = mycs.units()
01806         absout = n * [F]
01807         unitsout = n * ['pix']
01808         #
01809         p = mycs.convert(list(coordin['numeric']), absin, dopplerin, unitsin,
01810                          absout, dopplerout, unitsout)
01811         if not len(p): fail()
01812         #
01813         p2 = mycs.torel(mycs.referencepixel()['numeric']+2,F)['numeric']
01814         if len(p2)!=n: fail()
01815         if not all(p, p2, tol): fail('convert 11 gives wrong values')
01816 
01817         # velocity
01818         ok = mycs.findcoordinate('spectral')
01819         if not ok[0]: fail()
01820         pa = ok[1]
01821         wa = ok[2]
01822         #
01823         sAxis = pa
01824         dopplerin = 'radio'
01825         dopplerout = 'optical'
01826         vRefIn = mycs.frequencytovelocity(
01827             value=mycs.referencevalue()['numeric'][sAxis],
01828             doppler=dopplerin,
01829             velunit='km/s')
01830         if not vRefIn: fail()
01831         vRefOut = mycs.frequencytovelocity(
01832             value=mycs.referencevalue()['numeric'][sAxis],
01833             doppler=dopplerout,
01834             velunit='km/s')
01835         if not vRefOut: fail()
01836 
01837         # absvel to absvel
01838         absin = n * [T]
01839         p = mycs.referencepixel()['numeric'] + 2
01840         if len(p)!=n: fail()
01841         coordin = mycs.toworld(p)
01842         if not coordin: fail()
01843         unitsin = mycs.units()
01844         if not unitsin: fail()
01845         absout = n * [T]
01846         unitsout = mycs.units()
01847         if not unitsout: fail()
01848         #
01849         w = coordin['numeric'][sAxis]
01850         vIn = mycs.frequencytovelocity(value=w, doppler=dopplerin,
01851                                        velunit='km/s')
01852         if not vIn: fail()
01853         vOut = mycs.frequencytovelocity(value=w, doppler=dopplerout,
01854                                         velunit='km/s')
01855         if not vOut: fail()
01856         #
01857         coordin['numeric'][sAxis] = vIn
01858         unitsin[sAxis] = 'km/s'
01859         unitsout[sAxis] = 'km/s'
01860         #
01861         p = mycs.convert(list(coordin['numeric']), absin, dopplerin, unitsin,
01862                          absout, dopplerout, unitsout)
01863         if not len(p): fail()
01864         #
01865         if not abs(p[sAxis]-vOut)<tol: fail('convert 12 gives wrong values')
01866 
01867         # absvel to relvel
01868         p = mycs.referencepixel()['numeric'] + 2
01869         if len(p)!=n: fail()
01870         coordin = mycs.toworld(p)['numeric']
01871         if len(coordin)!=n: fail()
01872         #
01873         w = coordin[sAxis]
01874         vIn = mycs.frequencytovelocity(value=w, doppler=dopplerin,
01875                                        velunit='km/s')
01876         if not vIn: fail()
01877         vOut = mycs.frequencytovelocity(value=w, doppler=dopplerout,
01878                                         velunit='km/s')
01879         if not vOut: fail()
01880         vOut -= vRefOut
01881         #
01882         coordin[sAxis] = vIn
01883         absin[sAxis] = T
01884         absout[sAxis] = F
01885         #
01886         p = mycs.convert(list(coordin), absin, dopplerin, unitsin,
01887                          absout, dopplerout, unitsout)
01888         if not len(p): fail()
01889         #
01890         d = abs(p[sAxis]-vOut)
01891         if not d<tol: fail('convert 13 gives wrong values')
01892 
01893         # absvel to absworld
01894         p = mycs.referencepixel()['numeric'] + 2
01895         if len(p)!=n: fail()
01896         coordin = mycs.toworld(p)['numeric']
01897         if len(coordin)!=n: fail()
01898         #
01899         w = coordin[sAxis]
01900         vIn = mycs.frequencytovelocity(value=w, doppler=dopplerin,
01901                                        velunit='km/s')
01902         if not vIn: fail()
01903         #
01904         coordin[sAxis] = vIn
01905         absin[sAxis] = T
01906         absout[sAxis] = T
01907         unitsout = mycs.units()
01908         #
01909         p = mycs.convert(list(coordin), absin, dopplerin, unitsin,
01910                          absout, dopplerout, unitsout)
01911         if not len(p): fail()
01912         #
01913         w = mycs.toworld(mycs.referencepixel()['numeric']+2)['numeric']
01914         if len(w)!=n: fail()
01915         d = abs(p[sAxis]-w[sAxis])
01916         if not d<tol: fail('convert 14 gives wrong values')
01917 
01918         # absvel to relworld
01919         p = mycs.referencepixel()['numeric'] + 2
01920         if len(p)!=n: fail()
01921         coordin = mycs.toworld(p)['numeric']
01922         if len(coordin)!=n: fail()
01923         #
01924         w = coordin[sAxis]
01925         vIn = mycs.frequencytovelocity(value=w, doppler=dopplerin,
01926                                        velunit='km/s')
01927         if not vIn: fail()
01928         #
01929         coordin[sAxis] = vIn
01930         absin[sAxis] = T
01931         absout[sAxis] = F
01932         unitsout = mycs.units()
01933         #
01934         p = mycs.convert(list(coordin), absin, dopplerin, unitsin,
01935                          absout, dopplerout, unitsout)
01936         if not len(p): fail()
01937         #
01938         w = mycs.torel(mycs.toworld(mycs.referencepixel()['numeric']+2),T)['numeric']
01939         if len(w)!=n: fail()
01940         d = abs(p[sAxis]-w[sAxis])
01941         if not d<tol: fail('convert 15 gives wrong values')
01942 
01943         # absvel to abspix
01944         p = mycs.referencepixel()['numeric'] + 2
01945         if len(p)!=n: fail()
01946         coordin = mycs.toworld(p)['numeric']
01947         if len(coordin)!=n: fail()
01948         #
01949         w = coordin[sAxis]
01950         vIn = mycs.frequencytovelocity(value=w, doppler=dopplerin,
01951                                     velunit='km/s')
01952         if not vIn: fail()
01953         #
01954         coordin[sAxis] = vIn
01955         absin[sAxis] = T
01956         absout[sAxis] = T
01957         unitsout[sAxis] = 'pix'
01958         #
01959         p = mycs.convert(list(coordin), absin, dopplerin, unitsin,
01960                          absout, dopplerout, unitsout)
01961         if not len(p): fail()
01962         #
01963         p2 = mycs.referencepixel()['numeric']+2
01964         if len(p2)!=n: fail()
01965         d = abs(p[sAxis]-p2[sAxis])
01966         if not d<tol: fail('convert 16 gives wrong values')
01967 
01968         # absvel to relpix
01969         p = mycs.referencepixel()['numeric'] + 2
01970         if len(p)!=n: fail()
01971         coordin = mycs.toworld(p)['numeric']
01972         if len(coordin)!=n: fail()
01973         #
01974         w = coordin[sAxis]
01975         vIn = mycs.frequencytovelocity(value=w, doppler=dopplerin,
01976                                        velunit='km/s')
01977         if not vIn: fail()
01978         #
01979         coordin[sAxis] = vIn
01980         absin[sAxis] = T
01981         absout[sAxis] = F
01982         unitsout[sAxis] = 'pix'
01983         #
01984         p = mycs.convert(list(coordin), absin, dopplerin, unitsin,
01985                          absout, dopplerout, unitsout)
01986         if not len(p): fail()
01987         #
01988         p2  = mycs.torel(mycs.referencepixel()['numeric']+2,F)['numeric']
01989         if len(p2)!=n: fail()
01990         d = abs(p[sAxis]-p2[sAxis])
01991         if not (d<tol): fail('convert 17 gives wrong values')
01992 
01993         # relvel to absvel
01994         p = mycs.referencepixel()['numeric'] + 2
01995         if len(p)!=n: fail()
01996         coordin = mycs.toworld(p)['numeric']
01997         if len(coordin)!=n: fail()
01998         #
01999         w = coordin[sAxis]
02000         vIn = mycs.frequencytovelocity(value=w, doppler=dopplerin,
02001                                        velunit='km/s')
02002         if not vIn: fail()
02003         vIn -= vRefIn
02004         vOut = mycs.frequencytovelocity(value=w, doppler=dopplerout,
02005                                         velunit='km/s')
02006         if not vOut: fail()
02007         #
02008         coordin[sAxis] = vIn
02009         absin[sAxis] = F
02010         absout[sAxis] = T
02011         unitsin[sAxis] = 'km/s'
02012         unitsout[sAxis] = 'km/s'
02013         #
02014         p = mycs.convert(list(coordin), absin, dopplerin, unitsin,
02015                          absout, dopplerout, unitsout)
02016         if not len(p): fail()
02017         #
02018         d = abs(p[sAxis]-vOut)
02019         if not (d<tol): fail('convert 18 gives wrong values')
02020 
02021         # absworld to absvel
02022         p = mycs.referencepixel()['numeric'] + 2
02023         if len(p)!=n: fail()
02024         coordin = mycs.toworld(p)['numeric']
02025         if len(coordin)!=n: fail()
02026         vOut = mycs.frequencytovelocity(value=coordin[sAxis],
02027                                         doppler=dopplerout,
02028                                         velunit='km/s')
02029         if not vOut: fail()
02030         #
02031         absin[sAxis] = T
02032         absout[sAxis] = T
02033         unitsin = mycs.units()
02034         unitsout[sAxis] = 'km/s'
02035         #
02036         p = mycs.convert(list(coordin), absin, dopplerin, unitsin,
02037                          absout, dopplerout, unitsout)
02038         if not len(p): fail()
02039         #
02040         d = abs(p[sAxis]-vOut)
02041         if not (d<tol): fail('convert 19 gives wrong values')
02042 
02043         # relworld to absvel
02044         p = mycs.referencepixel()['numeric'] + 2
02045         if len(p)!=n: fail()
02046         w = mycs.toworld(p)['numeric']
02047         if len(w)!=n: fail()
02048         coordin = mycs.torel(w,T)['numeric']
02049         if len(coordin)!=n: fail()
02050         vOut = mycs.frequencytovelocity(value=w[sAxis],
02051                                         doppler=dopplerout,
02052                                         velunit='km/s')
02053         if not vOut: fail()
02054         #
02055         absin[sAxis] = F
02056         absout[sAxis] = T
02057         unitsin = mycs.units()
02058         unitsout[sAxis] = 'km/s'
02059         #
02060         p = mycs.convert(list(coordin), absin, dopplerin, unitsin,
02061                          absout, dopplerout, unitsout)
02062         if not len(p): fail()
02063         #
02064         d = abs(p[sAxis]-vOut)
02065         if not (d<tol): fail('convert 20 gives wrong values')
02066 
02067         # abspix to absvel
02068         p = mycs.referencepixel()['numeric'] + 2
02069         if len(p)!=n: fail()
02070         w = mycs.toworld(p)['numeric']
02071         if len(w)!=n: fail()
02072         vOut = mycs.frequencytovelocity(value=w[sAxis],
02073                                         doppler=dopplerout,
02074                                         velunit='km/s')
02075         if not vOut: fail()
02076         #
02077         coordin = w
02078         coordin[sAxis] = p[sAxis]
02079         absin[sAxis] = T
02080         absout[sAxis] = T
02081         unitsin[sAxis] = 'pix'
02082         unitsout[sAxis] = 'km/s'
02083         #
02084         p = mycs.convert(list(coordin), absin, dopplerin, unitsin,
02085                          absout, dopplerout, unitsout)
02086         if not len(p): fail()
02087         #
02088         d = abs(p[sAxis]-vOut)
02089         if not (d<tol): fail('convert 21 gives wrong values')
02090 
02091         # relpix to absvel
02092         p = mycs.referencepixel()['numeric'] + 2
02093         if len(p)!=n: fail()
02094         w = mycs.toworld(p)['numeric']
02095         if len(w)!=n: fail()
02096         vOut = mycs.frequencytovelocity(value=w[sAxis],
02097                                         doppler=dopplerout,
02098                                         velunit='km/s')
02099         if not vOut: fail()
02100         #
02101         p = mycs.torel(p,F)['numeric']
02102         coordin[sAxis] = p[sAxis]
02103         absin[sAxis] = F
02104         absout[sAxis] = T
02105         unitsin[sAxis] = 'pix'
02106         unitsout[sAxis] = 'km/s'
02107         #
02108         p = mycs.convert(list(coordin), absin, dopplerin, unitsin,
02109                          absout, dopplerout, unitsout)
02110         if not len(p): fail()
02111         #
02112         d = abs(p[sAxis]-vOut)
02113         if not (d<tol): fail('convert 22 gives wrong values')
02114         mycs.done()
02115 
02116         # mixed
02117         mycs = cs.newcoordsys(direction=T, spectral=T, linear=1)
02118         if not mycs: fail()
02119         absPix = mycs.referencepixel()
02120         absPix['numeric'] += 4
02121         if len(absPix['numeric'])!=4: fail()
02122         relPix = mycs.torel(absPix, F)
02123         if len(relPix['numeric'])!=4: fail()
02124         absWorld = mycs.toworld(absPix)
02125         if not absWorld: fail()
02126         relWorld = mycs.torel(absWorld, T)
02127         if not relWorld: fail()
02128         n = mycs.naxes();
02129 
02130         # convertmany.  any test is as good as any other
02131         coordin = mycs.referencepixel()['numeric']
02132         if len(coordin)!=n: fail()
02133         absin = n * [T]
02134         unitsin = n * ['pix']
02135         dopplerin = 'radio'
02136         absout = n * [T]
02137         unitsout = mycs.units()
02138         if not unitsout: fail()
02139         dopplerout = 'radio'
02140         #
02141         coordout = mycs.convert(list(coordin), absin, dopplerin,
02142                                 unitsin, absout, dopplerout, unitsout)
02143         if not len(coordout): fail()
02144         #
02145         rIn = ia.makearray(0, [len(coordin), 10])
02146         for i in range(10):
02147             for j in range(len(coordin)):
02148                 rIn[j,i] = coordin[j]
02149         rOut = mycs.convertmany (rIn, absin, dopplerin, unitsin,
02150                                  absout, dopplerout, unitsout)
02151         if len(rOut)!=n: fail()
02152         for i in range(10):
02153             for j in range(len(coordin)):
02154                 d = rOut[j,i] - coordout[j]
02155                 if not (d<tol): fail('convertmany gives wrong values')
02156         #
02157         return T
02158 
02159     def test14():
02160         info('')
02161         info('')
02162         info('')
02163         info('Test 14 - setspectral')
02164         #
02165         info('')
02166         info('Testing setspectral')
02167         info('')
02168         #
02169         mycs = cs.newcoordsys(direction=T)
02170         if not mycs: fail('coordsys constructor 1 failed')
02171         try:
02172             note("Expect SEVERE error and Exception here")
02173             ok = mycs.setspectral(refcode='lsrk')
02174         except Exception, e:
02175             note("Caught expected Exception")
02176             ok = false
02177         if ok:
02178             fail ('setspectral 1 unexpectedly did not fail')
02179         mycs.done()
02180         #
02181         mycs = cs.newcoordsys(spectral=T)
02182         #
02183         rc = 'LSRK'
02184         ok = mycs.setspectral(refcode=rc)
02185         if not ok: fail()
02186         if mycs.referencecode('spectral') != [rc]:
02187             fail('setspectral/reference code test fails')
02188         #
02189         rf = qa.quantity('1.0GHz')
02190         ok = mycs.setspectral(restfreq=rf)
02191         if not ok: fail()
02192         rf2 = mycs.restfrequency()
02193         if not rf2: fail()
02194         rf3 = qa.convert(rf2, 'GHz')
02195         if not rf3: fail()
02196         if (qa.getvalue(rf3) != 1.0):
02197             fail('setspectral/restfrequency test fails')
02198         #
02199         fd = [1, 1.5, 2, 2.5, 3]
02200         fq = qa.quantity(fd, 'GHz')
02201         ok = mycs.setspectral(frequencies=fq)
02202         if not ok: fail()
02203         #
02204         doppler = 'optical'
02205         vunit = 'km/s'
02206         vd = mycs.frequencytovelocity(fd, 'GHz', doppler, vunit)
02207         vq = qa.quantity(vd, vunit)
02208         ok = mycs.setspectral(velocities=vq, doppler=doppler)
02209         if not ok: fail()
02210         #
02211         fd2 = mycs.velocitytofrequency(vd, 'GHz', doppler, vunit)
02212         if not all(fd2,fd,1e-6):
02213             fail('setspectral/freq/vel consistency test failed')
02214         #
02215         if not mycs.done(): fail()
02216 
02217     def test15():
02218         info('')
02219         info('')
02220         info('')
02221         info('Test 15 - settabular')
02222         #
02223         info('')
02224         info('Testing settabular')
02225         info('')
02226         #
02227         mycs = cs.newcoordsys(direction=T)
02228         if not mycs: fail('coordsys constructor 1 failed')
02229         try:
02230             note('Expect SEVERE error and Exception here')
02231             ok = mycs.settabular(pixel=[1,2], world=[1,2])
02232         except Exception, e:
02233             note('Caught expected Exception')
02234             ok = false
02235         if ok:
02236             fail ('settabular 1 unexpectedley did not fail')
02237         mycs.done()
02238         #
02239         mycs = cs.newcoordsys(tabular=T)
02240         #
02241         p = [0, 1, 2, 3, 4]
02242         w = [10, 20, 30, 40, 50]
02243         ok = mycs.settabular(pixel=p, world=w)
02244         if not ok: fail()
02245         #
02246         rv = mycs.referencevalue()['numeric']
02247         if not rv: fail()
02248         if (rv[0] != w[0]):  fail('settabular test 1 failed (refval)')
02249         #
02250         rp = mycs.referencepixel()['numeric']
02251         if rp!=0.0: fail()
02252         if (rp != p[0]):  fail('settabular test 1 failed (refpix)')
02253         #
02254         try:
02255             note('Expect SEVERE error and Exception here')
02256             ok = mycs.settabular(pixel=[0,1,2], world=[10,20])
02257         except Exception, e:
02258             note('Caught expected Exception')
02259             ok = false
02260         if ok: fail('settabular test 2 unexpectedly did not fail')
02261         #
02262         try:
02263             note('Expect SEVERE error and Exception here')
02264             ok = mycs.settabular(pixel=[0,1], world=[1,10,20])
02265         except Exception, e:
02266             note('Caught expected Exception')
02267             ok = false
02268         if ok: fail('settabular test 3 unexpectedly did not fail')
02269         #
02270         ok = mycs.settabular(pixel=[0,1,2], world=[1,10,20])
02271         if not ok: fail('settabular test 4 failed')
02272         try:
02273             note('Expect SEVERE error and Exception here')
02274             ok = mycs.settabular(pixel=[0,1,2,3])
02275         except Exception, e:
02276             note('Caught expected Exception')
02277             ok = false
02278         if ok: fail('settabular test 5 unexpectedly did not fail')
02279         try:
02280             note('Expect SEVERE error and Exception here')
02281             ok = mycs.settabular(world=[0,1,2,3])
02282         except Exception, e:
02283             note('Caught expected Exception')
02284             ok = false
02285         if ok: fail('settabular test 6 unexpectedly did not fail')
02286         #
02287         if not mycs.done(): fail()
02288 
02289     def test16():
02290         info('')
02291         info('')
02292         info('')
02293         info('Test 16 - addcoordinate')
02294         #
02295         info('')
02296         info('Testing addcoordinate')
02297         info('')
02298         #
02299         mycs = cs.newcoordsys()
02300         if not mycs: fail('coordsys constructor 1 failed')
02301         #
02302         ok = mycs.addcoordinate(direction=T, spectral=T, linear=2, tabular=T, stokes="I V")
02303         if not ok:
02304             fail ('addcoordinate failed')
02305         #
02306         n = mycs.ncoordinates()
02307         if (n != 5):
02308             fail ('addcoordinate gave wrong number of coordinates')
02309 
02310         # We don't know what order they will be in. This is annoying.
02311         types = mycs.coordinatetype()
02312         hasDir = F
02313         hasSpec = F
02314         hasLin = F
02315         hasTab = F
02316         hasStokes = F
02317         for i in range(n):
02318             if (types[i]=='Direction'):
02319                 hasDir = T
02320             else:
02321                 if (types[i]=='Spectral'):
02322                     hasSpec = T
02323                 else:
02324                     if (types[i]=='Linear'):
02325                         hasLin = T
02326                     else:
02327                         if (types[i]=='Tabular'):
02328                             hasTab = T
02329                         else:
02330                             if (types[i]=='Stokes'):
02331                                 hasStokes = T
02332         #
02333         ok = hasDir and hasSpec and hasLin and hasTab and hasStokes
02334         if not ok:
02335             fail('addcoordinate did not add correct types')
02336         #
02337         mycs.done()
02338 
02339     def test17():
02340         info('')
02341         info('')
02342         info('')
02343         info('Test 17 - toworld, topixel with reference conversion')
02344         #
02345         mycs = cs.newcoordsys(direction=T, spectral=T)
02346         if not mycs: fail()
02347         #
02348         v = mycs.units()
02349         v[0] = 'rad'
02350         v[1] = 'rad'
02351         v[2] = 'Hz'
02352         ok = mycs.setunits(v)
02353         if not ok: fail()
02354         #
02355         mycs.setrestfrequency(1.420405752E9)
02356         #
02357         ok = mycs.setreferencecode(value='J2000', type='direction', adjust=F)
02358         if not ok: fail;
02359         ok = mycs.setreferencecode(value='LSRK', type='spectral', adjust=F)
02360         if not ok: fail;
02361         #
02362         v = mycs.referencevalue()
02363         v[0] = 0.0
02364         v[1] = -0.5
02365         v[2] = 1.4e9
02366         ok = mycs.setreferencevalue(v)
02367         if not ok: fail()
02368         #
02369         v = list(mycs.referencepixel()['numeric'])
02370         v[0] = 101
02371         v[1] = 121
02372         v[2] = 10.5
02373         ok = mycs.setreferencepixel(v)
02374         if not ok: fail()
02375         #
02376         v = mycs.increment()
02377         v[0] = -1.0e-6
02378         v[1] =  2.0e-6
02379         v[2] =  4.0e6
02380         ok = mycs.setincrement(v)
02381         if not ok: fail()
02382         #
02383         v = mycs.units()
02384         v[0] = 'deg'
02385         v[1] = 'deg'
02386         ok = mycs.setunits(v)
02387         if not ok: fail()
02388 
02389         ok = mycs.setconversiontype(direction='GALACTIC', spectral='BARY')
02390         if not ok: fail()
02391         #local d,s
02392         d = mycs.conversiontype(type='direction')
02393         if not d: fail()
02394         s = mycs.conversiontype(type='spectral')
02395         if not s: fail()
02396         if (d != 'GALACTIC' or s != 'BARY'):
02397             fail('setconversiontype consistency test failed')
02398         #
02399         p = mycs.referencepixel()['numeric']
02400         for i in range(len(p)): p[i] += 10.0
02401         if len(p)!=3: fail()
02402         w = mycs.toworld(value=p, format='n')
02403         if not w: fail()
02404         p2 = mycs.topixel(value=w)['numeric']
02405         if len(p2)!=3: fail()
02406         #
02407         # Need to look into why i need such a large tolerance
02408         #
02409         tol = 1e-3
02410         if not all(p2,p,tol): fail('failed consistency test 1')
02411         ###
02412         return T
02413 
02414     def test18():
02415         info('')
02416         info('')
02417         info('')
02418         info('Test 18 - setdirection')
02419         #
02420         info('')
02421         info('Testing setdirection')
02422         info('')
02423         #
02424         mycs = cs.newcoordsys(direction=T)
02425         if not mycs: fail('coordsys constructor 1 failed')
02426 
02427         # Test 1
02428         refcode = 'GALACTIC'
02429         proj = 'CAR'
02430         projpar = []
02431         refpix = list(mycs.referencepixel()['numeric'])
02432         for i in range(len(refpix)): refpix[i] *= 1.1
02433         refval = mycs.referencevalue(format='n')['numeric']
02434         for i in range(len(refval)): refval[i] *= 1.1
02435         xform = ia.makearray(0.0, [2, 2]);
02436         xform[0,0] = 1.0
02437         xform[1,1] = 1.0
02438         ok = mycs.setdirection(refcode=refcode,
02439                                proj=proj, projpar=projpar,
02440                                refpix=refpix, refval=refval,
02441                                xform=xform)
02442         if not ok: fail()
02443         #
02444         if (proj != mycs.projection()['type']):
02445             fail('Projection was not updated')
02446         if (len(projpar) > 0):
02447             if (projpar != mycs.projection()['parameters']):
02448                 fail('Projection parameters were not updated')
02449         if not all(refpix, mycs.referencepixel()['numeric'], 1e-6):
02450             fail('Reference pixel was not updated')
02451         if not all(refval, mycs.referencevalue(format='n')['numeric'], 1e-6):
02452             fail('Reference value was not updated')
02453 
02454         # Test 2
02455         refcode = 'J2000'
02456         proj = 'SIN'
02457         projpar = [0,0]
02458         refval = "20.0deg -33deg"
02459         refval2 = [20,-33]
02460         ok = mycs.setdirection(refcode=refcode,
02461                                proj=proj, projpar=projpar,
02462                                refval=refval)
02463         if not ok: fail()
02464         #
02465         if (proj != mycs.projection()['type']):
02466             fail('Projection was not updated')
02467         if (len(projpar) > 0):
02468             if not all(projpar,mycs.projection()['parameters'],1e-6):
02469                 fail('Projection parameters were not updated')
02470         ok = mycs.setunits (value="deg deg")
02471         if not all (refval2, mycs.referencevalue(format='n')['numeric'], 1e-6):
02472             fail('Reference value was not updated')
02473         #
02474         if not mycs.done(): fail()
02475 
02476 
02477     def test19():
02478         info('')
02479         info('')
02480         info('')
02481         info('Test 19 - replace')
02482         #
02483         info('')
02484         info('Testing replace')
02485         info('')
02486         #
02487         mycs = cs.newcoordsys(direction=T, linear=1)
02488         if not mycs: fail('coordsys constructor 1 failed')
02489         #
02490         cs2 = cs.newcoordsys(linear=1)
02491         if not cs2: fail('coordsys constructor 2 failed')
02492         #
02493         try:
02494             note('Expect SEVERE error and Exception here')
02495             ok = mycs.replace(cs2.torecord(), whichin=0, whichout=0)
02496         except Exception, e:
02497             note('Caught expected Exception')
02498             ok = false
02499         if ok:
02500             fail('replace 1 unexpectedly did not fail')
02501         ok = cs2.done()
02502         #
02503         cs2 = cs.newcoordsys(spectral=T)
02504         ok = mycs.replace(cs2.torecord(), whichin=0, whichout=1)
02505         if not ok: fail()
02506         if mycs.coordinatetype(1) != ['Spectral']:
02507             fail('Replace 1 did not set correct coordinate type')
02508         #
02509         ok = cs2.done()
02510         ok = mycs.done()
02511 
02512 
02513     test1()
02514     test2()
02515     test3()
02516     test4()
02517     test5()
02518     test6()
02519     test7()
02520     test8()
02521     test9()
02522     test10()
02523     test11()
02524     test12() #check toabsmany/torelmany
02525     test13()
02526     test14()
02527     test15()
02528     test16()
02529     test17()
02530     test18()
02531     test19()
02532     
02533 
02534 Benchmarking = True
02535 if Benchmarking:
02536     startTime = time.time()
02537     regstate = False
02538     for i in range(100):
02539         coordsystest()
02540     endTime = time.time()
02541     regstate = True
02542 else:
02543     coordsystest()
02544 
02545 print ''
02546 print 'Regression PASSED'
02547 print ''
02548 
02549 #exit()