pro vect_ps,o_name=o_name,smooth=smooth,$	;,l_name,q_name,u_name
	calibration=calibration,swap=swap,judge=judge,$
	binxy=binxy,length=length

ll_name=pickfile(filter='l5*.dat')
qq_name=pickfile(filter='q5*.dat')
uu_name=pickfile(filter='u5*.dat')
len=strlen(ll_name)
l_name=strmid(ll_name,len-12,12)
q_name=strmid(qq_name,len-12,12)
u_name=strmid(uu_name,len-12,12)
len=12
;----------------------------------------------------------------------
; PURPOSE: Draw the VECTOR Magnetogram of HSOS
;----------------------------------------------------------------------
; KEYWORDS:
;	smooth: mode to smooth the raw data, default=4 pixels
;	calibration: calib. coefficient, default=2000.
;	swap: the high-low 8-bit conversion
;	judge: judge the direction of traverse field by potential field
;		(force-free is available, if make a minor modification)
;			default=1
;	o_name: the vector draw will be put into o_name
;	binxy: factor to rebin the size of bx(y) to get better drawing
;			default=6
;	length: one-ticklen == length (Gauss traverse strength)
;			default: 1000G/ticklen	
;----------------------------------------------------------------------

;----------------------------------------------------------------------
; Default:
	if n_elements(smooth) eq 0 then smooth=4
	if n_elements(calibration) eq 0 then calibration=2000.
	if n_elements(swap) eq 0 then swap=0	
	if n_elements(o_name) eq 0 then o_name=strmid(l_name,len-12,9)+'vps'
	if n_elements(binxy) eq 0 then binxy=6
	if n_elements(length) eq 0 then length=1000.
	if n_elements(judge) eq 0 then judge=1
	calibration=1000./calibration
		
;----------------------------------------------------------------------
;	INPUT: l(q,u)_name: l,q,u filename

;----------------------------------------------------------------------
;	Modification:
;		Oct 25, by Deng Y. Y.


;----------------------------------------------------------------------
;	Read Raw Magneogram And Preprocess
read_hr,ll_name,l,lh,swap=swap
read_hr,qq_name,q,qh,swap=swap
read_hr,uu_name,u,uh,swap=swap
xd=523
yd=363
l=l*calibration
q=q*calibration
u=u*calibration
ltmp=fltarr(512+7,512+7)
ltmp(4:515,4:515)=l
ltmp=smooth(ltmp,smooth)
l=ltmp(4:515,4:515)
ltmp(4:515,4:515)=q
ltmp=smooth(ltmp,smooth)
q=ltmp(4:515,4:515)
ltmp(4:515,4:515)=u
ltmp=smooth(ltmp,smooth)
u=ltmp(4:515,4:515)

l=congrid(l,xd/4,yd/4,/interp)
ll=congrid(l,xd/binxy,yd/binxy,/interp)
qq=congrid(q,xd/binxy,yd/binxy,/interp)
uu=congrid(u,xd/binxy,yd/binxy,/interp)

;----------------------------------------------------------------------
;	Calibrate the traverse field and get bx,by
tt=float(qq)*qq+float(uu)*uu
tt=sqrt(tt)
tt=93.7*sqrt(tt)
re=where(tt le 300)
if re(0) ne -1 then begin
tt(re)=0.
uu(re)=0.
qq(re)=1.
endif
angle=0.5*(-atan(uu,qq)+3.14159)
xsi=size(ll)
ysi=xsi(2)
xsi=xsi(1)
xstep=1./(xsi+1.)
ystep=1./(ysi+1.)
tt(0,*)=0
tt(*,0)=0
tt(xsi-1,*)=0
tt(*,ysi-1)=0
tlength=max(tt)/length
bx=tt*cos(angle)
by=tt*sin(angle)
	if judge ne 0 then begin
		free_field,ll,0,0,pbx,pby,pbz
		test=(((pbx*bx+pby*by) gt 0.0)-0.5)*2
		bx=bx*test
		by=by*test
	endif

!p.position=[0,0,1,1]
vi=[40.,80.,160.,320.,480,640.,800,960.,1280.,1600]
vii=[-reverse(vi),vi]
xyt=strarr(6)+' '
contour,l,level=vii,c_linestyle=vii le 0.,c_color=(vii le 0)*128+127,xst=5,$
	yst=5,c_thick=1
contour,findgen(xd,yd),xst=1,yst=1,xtickname=xyt,ytickname=xyt,/nodata,/noerase
velovect,bx,by,length=tlength,xst=5,yst=5,/noerase
option3=wmenu(['Do You Want to Create PS File?','Sure','No, Thanks'])


;----------------------------------------------------------------------
;	Create the PS File of Contour
;----------------------------------------------------------------------
if (option3 eq 1) then begin	

set_plot,'ps'
print,'vect.ps file was ceated'
o_name='vect.ps'
device,filename=o_name,/landscape,xsize=5.23*3.5,ysize=3.63*3.5,$
		xoffset=3.,bits_per_pixel=16

!p.position=[xstep,ystep,xstep*xsi,ystep*ysi]
contour,l,level=vii,c_linestyle=vii le 0.,c_color=128-(vii le 0)*128,xst=5,$
	yst=5,c_thick=1
contour,findgen(xsi,ysi),xst=1,yst=1,xtickname=xyt,ytickname=xyt,/nodata,/noerase
!p.position=[0,0,1,1]
velovect,bx,by,length=tlength,xst=5,yst=5,/noerase

xconst=1./float(174)
yconst=1./float(121)
tname=l_name+', '+q_name+', '+u_name
tname='HSOS Magnetogram: ('+strupcase(tname)+')'
xyouts,xconst*2.,yconst*148,size=1.7,'!6 '+tname,/normal
xyouts,xconst*20,yconst*138,size=1.,'!8 Content: !6'+'Vector Field',/normal
xyouts,xconst*20,yconst*133,size=1.,'!8 Date:    !6'+lh(1),/normal
xyouts,xconst*20,yconst*128,size=1.,'!8 Time:    !6'+lh(2)+'UT',/normal

xpos=strcompress(lh(4),/remove_all)
if xpos le 0 then begin 
strput,xpos,'S',0
endif else begin
xpos='N'+xpos
endelse
ypos=strcompress(lh(5),/remove_all)
if ypos le 0 then begin 
strput,ypos,'E',0
endif else begin
ypos='W'+ypos
endelse

xyouts,xconst*100,yconst*138,size=1.,'!8 Disc Coordinate:  !6'+xpos+', '+ypos,/normal
xyouts,xconst*100,yconst*133,size=1.,'!8 Integrated Frame: !6'+lh(10),/normal
xyouts,xconst*100,yconst*128,size=1.,'!8 Wave Length:      !6'+lh(0),/normal
xyouts,xconst*1,yconst*120,size=1.,'!8 FOV: !6 5.23 x 3.63 arcmin.',/normal

xi=185
yi=93
xyouts,xconst*(xi-12),yconst*95,size=1.1,'!6 Field Strength: !8 Gauss',/normal
xyouts,xconst*(xi-8),yconst*(yi-5),size=0.9,'!6 Contour: !8 Longitudinal',/normal
xyouts,xconst*xi,yconst*(yi-10),size=0.8,'!8 Positive: !6 Solid',/normal
xyouts,xconst*xi,yconst*(yi-15),size=0.8,'!8 Negative: !6 Dashed',/normal
for i=0,9 do xyouts,xconst*(xi+5),yconst*(yi-20-i*5),size=0.8,string(fix(vi(i))),/normal
xyouts,xconst*(xi-8),yconst*(yi-72),size=0.9,'!6 Arrow: !8 Transverse',/normal 
xyouts,xconst*xi,yconst*(yi-77),size=0.8,'!6 y-ticklength=1000G',/normal 


arrow,xconst*xi,yconst*120,xconst*195,yconst*120,/normal
arrow,xconst*xi,yconst*120,xconst*xi,yconst*130,/normal
xyouts,xconst*181.5,yconst*133,size=1.2,'!6 N',/normal
xyouts,xconst*195,yconst*119,size=1.2,'!6 W',/normal


device,/close
set_plot,'x'
endif
end
