;*******************************************************************************
;
; Procedure Disktran
; Rotate the magnetic field so that it is viewd normally and plot it
;
; Input:
;	1) Bixarraym: X (west) compotent of the magnetic field in image plane
;	2) Biyarraym: Y (east) component of the magnetic field in image plane
;	3) Bizarraym: Line of sight magnetic field
;	4) B0: The heliocentric lattitude of the disk center (in radiant)
;	5) P: The angle between the disk projection of the solar spin axis and
;		the north point. (eastward is positive) (in radiant)
;	6) lc: The heliocentric longitude of the center point of the scan
;		(in radiant)
;	7) bc: The heliocentric lattitude of the center point of the scan
;		(in radiant)
;
; Output:
;	1)Bdxarraym: X component of the photospheric magnetic field
;	2)Bdyarraym: Y component of the photospheric magnetic field
;	3)Bdzarraym: Z component (normal component) of the magnetic field
;	4)Pxd: The X coordinate on the photosphere for each pixel in the
;		above array
;	5)Pyd: The Y coordinate on the photosphere for each pixel in the
;		above array
;	6)A vector magnetogram show the magnetic field as if it is viewed
;		normally.
;
;*******************************************************************************
;
PRO Disktran, bixarraym,biyarraym,bizarraym,b0,p,lc,bc,bdxarraym,bdyarraym,$
bdzarraym,pxd,pyd,noplot=noplot,pix_size=pix_size
if n_elements(pix_size) EQ 1 then pix_size=double([pix_size(0),pix_size(0)]) $
else pix_size = double(pix_size)  ; pixel size in arc sec [xsize,ysize]
ss=size(bixarraym)
nx=ss(1)
ny=ss(2)
cxx=cos(p)*cos(lc)-sin(p)*sin(b0)*sin(lc)
cxy=(-1)*cos(p)*sin(bc)*sin(lc)-sin(p)*(cos(b0)*cos(bc)+sin(b0)* $
sin(bc)*cos(lc))
cyx=sin(p)*cos(lc)+cos(p)*sin(b0)*sin(lc)
cyy=(-1)*sin(p)*sin(bc)*sin(lc)+cos(p)*(cos(b0)*cos(bc)+ $
sin(b0)*sin(bc)*cos(lc))
axx=(-1)*sin(b0)*sin(p)*sin(lc)+ $
cos(p)*cos(lc)
axy=sin(b0)*cos(p)*sin(lc)+ $
sin(p)*cos(lc)
axz=(-1)*cos(b0)*sin(lc)
ayx=(-1)*sin(bc)*(sin(b0)*sin(p)*cos(lc) $
+cos(p)*sin(lc))-cos(bc)* $
cos(b0)*sin(p)
ayy=sin(bc)*(sin(b0)*cos(p)*cos(lc)- $
sin(p)*sin(lc))+cos(bc)* $
cos(b0)*cos(p)
ayz=(-1)*cos(b0)*sin(bc)*cos(lc)+ $
sin(b0)*cos(bc)
azx=cos(bc)*(sin(b0)*sin(p)*cos(lc)+ $
cos(p)*sin(lc))-sin(bc)* $
cos(b0)*sin(p)
azy=(-1)*cos(bc)*(sin(b0)*cos(p)*cos(lc) $
-sin(p)*sin(lc))+sin(bc)*cos(b0)*cos(p)
azz=cos(bc)*cos(b0)*cos(lc)+ $
sin(bc)*sin(b0)
bdxarraym=axx*bixarraym+axy*biyarraym+axz*bizarraym
bdyarraym=ayx*bixarraym+ayy*biyarraym+ayz*bizarraym
bdzarraym=azx*bixarraym+azy*biyarraym+azz*bizarraym
x0=nx*(pix_size(0)/min(pix_size))/2.
y0=ny*(pix_size(1)/min(pix_size))/2.
delt=cxx*cyy-cxy*cyx
xcoor=(indgen(nx)#replicate(1,ny))*pix_size(0)/min(pix_size)
ycoor=(replicate(1,nx)#indgen(ny))*pix_size(1)/min(pix_size)
dircos=cos(bc)*cos(lc)*cos(b0)+sin(bc)*sin(b0)
nmax=fix(sqrt(float(nx*nx+ny*ny))/dircos/2.)
pxd=(cyy*(xcoor-x0)-cxy*(ycoor-y0))/delt+nmax
pyd=(cxx*(ycoor-y0)-cyx*(xcoor-x0))/delt+nmax
xsize=max(pxd)-min(pxd)
ysize=max(pyd)-min(pyd)
if NOT keyword_set(noplot) then begin
   print,'Tell me the window number for plotting the'
   print,'normaly viewed magnetic field.'
   read,nw
   window,nw,xs=xsize*15,ys=ysize*15
   whitebg
   specialct,100,0,0,255
   specialct,200,0,255,0
   vfield2,bdxarraym,bdyarraym,pxd,pyd,thick=1,background=0,color=1,length=1.0
   lv=[25,50,100,200,400,800,1600,3200]
   lv=[-rotate(lv,2),lv]
   contour,bdzarraym,pxd,pyd,levels=lv,$
   c_color=(lv lt 0)*100+100,/noerase, xstyle=5,ystyle=5
endif
end