pro plot_set_xyscan, tmap,sz,sx,sy,pk,trx,wran,cord,gost

; purpose:
;   sets everything needed for graphic display using plot_xyscan
;   since plot_xyscan requires speed we let anything that does not
;   need to be redone done here once. (JL)
; input:
;	tmap 
; outputs:
;	sz,sx,sy,pk,trx,wran,cord,gost


m=n_elements(tmap[*,0])

;---need to sett window here
xwt =790-245+4 & ywt =320-20 ; match those in plot_xyscan.pro
 window,1,xs=xwt,ys=ywt
 device,decompo=0
 !p.multi=[0,2,1]
 !p.charsize=1.4
 !p.font=-1
 loadct,5
;------- define rotation matrix
p=35.0 & b0=-20.0
p=p*!dtor & b0=b0*!dtor
axx =  COS(p)
axy =  SIN(p)
axz =  0.
ayx = -COS(b0)*SIN(p) ; although unnecessary
ayy =  COS(b0)*COS(p)
ayz =  SIN(b0)
azx =  SIN(b0)*SIN(p)
azy = -SIN(b0)*COS(p)
azz =  COS(b0)
trx = [[axx,axy,axz], [ayx,ayy,ayz], [azx,azy,azz]]
trx=transpose(trx) ; this is the IDL way
;------ set the scan point
tmax=max(tmap)
px=where(tmap eq tmax) mod m
py=where(tmap eq tmax) / m
pk=[px,py]
;------- define a map sz=map(sx,sy)
m=n_elements(tmap[*,0])
s=findgen(m)-m/2
sx=fltarr(m,m)
for i=0,m-1 do sx(i,*)=s(i)
sy=transpose(sx) 
; need to scale the vertical heights so that it fits in the window.
vfact= (float(m)/2.)/tmax
sz=tmap *vfact
trx[1,1]=vfact ; since trx[1,*] are unsed put it here
;------- rotate it merely to set window box size
px=pk[0] & py=pk[1]
xscan1=reform(sx(*,py)); 1,2,3 means x,y,z-components
xscan2=reform(sy(*,py))
xscan3=reform(sz(*,py))
xscan1_ = axx*xscan1 + axy*xscan2 + axz*xscan3
xscan3_ = azx*xscan1 + azy*xscan2 + azz*xscan3

yscan1=reform(sx(px,*)); 1,2,3 means x,y,z-components
yscan2=reform(sy(px,*))
yscan3=reform(sz(px,*))
yscan1_ = axx*yscan1 + axy*yscan2 + axz*yscan3
yscan3_ = azx*yscan1 + azy*yscan2 + azz*yscan3

;---set window size (field of view for the perspective view)
xsmin=min([xscan1_,yscan1_])
xsmax=max([xscan1_,yscan1_])
zsmin=min([xscan3_,yscan3_])*1.3
zsmax=max([xscan3_,yscan3_])*1.3
wran=[xsmin,xsmax,zsmin,zsmax]

;--create coordinate lines parallel to x & y axes & rotate them,
delta_m = fix(float(m)/8.)
xg=(findgen(9)-4.)*delta_m
xxg=fltarr(9,9)
for i=0,8 do xxg[i,*]=xg[i]
yyg=transpose(xxg)
zzg=yyg*0.
xxg_ = axx*xxg + axy*yyg + axz*zzg
zzg_ = azx*xxg + azy*yyg + azz*zzg
cord=fltarr(9,9,2)
cord[*,*,0]=xxg_
cord[*,*,1]=zzg_

;--miscelleneous things

gost=intarr(51,51) & gost[0,0]=255 & gost=bytscl(gost)

return
; returns sz,sx,sy,pk,trx,wran,cord,gost
end
