return cart2[0,0],cart2[1,0],cart2[2,0]

# Rotate about z axis (phi=0,theta=0) ang in rad
def rotz(x,y,z,ang):
    arr1=[m.cos(ang),-1.0*m.sin(ang),0.0]
    arr2=[m.sin(ang),m.cos(ang),0.0]
    arr3=[0.0,0.0,1.0]
    Rz=np.matrix([arr1,arr2,arr3])
    cart=np.matrix([[x,y,z]])
    cart2=Rz*cart.transpose()
    return cart2[0,0],cart2[1,0],cart2[2,0]

#
# Plot search region
#
(retval,xc,yc,fc)=ps.sph2xyf(proj,ra,dec)
(xcw,ycw)=GetWorldXY(xc,yc,fc)
hx=[]
hy=[]
dang=m.pi/2.0-dec
for rax in ps.frange(0,2.0*m.pi,.001):
    decx=m.pi/2.0-rad
    (x0,y0,z0)=sph2rec(rax,decx)
    (x1,y1,z1)=roty(x0,y0,z0,dang)
    (x2,y2,z2)=rotz(x1,y1,z1,ra-m.pi)
    (ra1,dec1)=rec2sph(x2,y2,z2)
    (retval,hx0,hy0,hf)=ps.sph2xyf(proj,ra1,dec1)
    (hxw,hyw)=GetWorldXY(hx0,hy0,hf)
    hx.append(hxw)
    hy.append(hyw)
plt.plot([xcw],[ycw],'ro')
    arr3=[0.0,0.0,1.0]
    Rz=np.matrix([arr1,arr2,arr3])
    cart=np.matrix([[x,y,z]])
    cart2=Rz*cart.transpose()
    return cart2[0,0],cart2[1,0],cart2[2,0]

# Get nearest point on squid tile border to ra,dec
(rval,ra0,dec0)=ps.tile_nearest(proj,squid,ra,dec)
print "nearest lon={:f} lat={:f} (deg)".format(ra0/dd2r,dec0/dd2r)
rad=ps.sphdist(ra0,dec0,ra,dec)
print "sphdist={:f} (deg)".format(rad/dd2r)

# Get params for this squid
sk=ps.squid_getres(squid) # squid resolution parameter
(rval,sr,sd)=ps.squid2sph(proj,squid) # ra,dec of squid center
(rval,sx,sy,sf)=ps.sph2xyf(proj,sr,sd) # get x,y,face for squid center

# cone array
(rval,hxc,hyc,hfc)=ps.sph2xyf(proj,ra,dec) # x,y,face for search point
(rval,xp0,yp0,pf)=ps.sph2xyf(proj,ra0,dec0) # x,y,face for nearest point

# get array for search radius for plotting
hx=[]
hy=[]
dang=m.pi/2.0-dec
for rax in ps.frange(0,2.0*m.pi,.001):
    decx=m.pi/2.0-rad
    (x0,y0,z0)=sph2rec(rax,decx)
    (x1,y1,z1)=roty(x0,y0,z0,dang)
    (x2,y2,z2)=rotz(x1,y1,z1,ra-m.pi)
    (ra1,dec1)=rec2sph(x2,y2,z2)