pro even_light_figures, figure, straight_num = straight_num

;+
; NAME:
;	even_light_figures
; PURPOSE:
;	Draw stuff
; INCLUDE:
	@compile_opt.pro		; On error, return to caller
; CALLS
;	InitVar
; MODIFICATION HISTORY:
;	???-????, Paul Hick (UCSD/CASS; pphick@ucsd.edu)
;-

Initvar, figure, 2
InitVar, straight_num, /key			; For figure 3

origin = [0,0,0]
xunit  = [1,0,0]
yunit  = [0,1,0]
zunit  = [0,0,1]

chars = 1.5
dd = .11/3.

case figure of
1: begin
	r0 = [1,1.75,1]*1.25

	aperture = origin+[0,0.25,0]

	scale = 1.5

	phi_box = 30/!radeg
	rad_box = 1.*scale

	twin, /del
	twin, xsize=400, ysize=1.8*400

	setup3d, -r0+[0,.07,0], r0+[0,.07,0], /xyplane


; box with fibers

	r0 = rad_box*[ 0,1,0]
	r1 = rad_box*[ cos(!pi/2-phi_box),sin(!pi/2-phi_box),0]
	r2 = rad_box*[-cos(!pi/2-phi_box),sin(!pi/2-phi_box),0]
	plot3darc, aperture, r1, r2

	dr = [0,.2,0]*scale
	plot3dline, aperture+r1, aperture+r1+dr
	plot3dline, aperture+r2, aperture+r2+dr
	plot3dline, aperture+r1+dr, aperture+r2+dr

; direction of horizontal motion

	da = [.3,0,0]
	plot3dline, aperture+r0+.65*dr+da,aperture+r0+.65*dr-da, tiplen=.03, tipwid=.03

	plot3dline, aperture, aperture+1.85*yunit, linestyle=1, thick=2
	plot3dline, aperture+.75*yunit-[.5,.3,0], aperture+.75*yunit,	$
		labeltext='Optical Axis', labeldist=-1, labeloff=[.1,0.1,0], chars=chars,	$
		tiplen=.03, tipwid=.03

; marks for fibers along circle of box

	n = 20
	angle = (90+28.5*gridgen(n,range=[1,-1]))/!radeg

; mark 60 degree span of box

; bounds of aperture view towards box

	plot3dline, aperture, aperture+1.2*rad_box*[cos(angle[n-1]),sin(angle[n-1]),0], linestyle=2
	plot3dline, aperture, aperture+1.2*rad_box*[cos(angle[0  ]),sin(angle[0  ]),0], linestyle=2, labeltext='1 m',	$
		labeldist=-.8, labeloffset=[-.2,0], chars=chars

	plot3darc, aperture, .3*rad_box*[cos(angle[0  ]),sin(angle[0  ]),0],	$
						 .3*rad_box*[cos(angle[n-1]),sin(angle[n-1]),0],	$
						labeltext='57!9%!X', labelradius=1.1, chars=chars, labeloff=[.02,0,0]

	for i=0,n-1 do begin
		r = rad_box*[cos(angle[i]),sin(angle[i]),0]
		plot3dline, aperture+.975*r, aperture+1.025*r
	endfor

	i = 2
	ri = rad_box*[cos(angle[i]),sin(angle[i]),0]
	plot3dline, aperture+1.35*ri, aperture+1.04*ri, tiplen=.05, tipwid=.02,	$
		labeltext='optical fiber', labeldist=-.5, chars=chars

	i = 15
	ri = rad_box*[cos(angle[i]),sin(angle[i]),0]
	plot3dline, aperture, aperture+1.25*ri, linestyle=2

	rj = rad_box*[cos(angle[i+1]),sin(angle[i+1]),0]
	plot3dline, aperture, aperture+1.25*rj, linestyle=2

	plot3darc, aperture, 1.2*ri, 1.2*rj, labeltext='3!9%!X', labeloffset=[.05,.1,0], chars=chars

; aperture

	dr = [.04,0,0]
	plot3dline, aperture-dr, aperture-3*dr
	plot3dline, aperture+dr, aperture+3*dr, labeltext='aperture', labeldist=.4, chars=chars


	da = [.2,0,0]
;	plot3dline, aperture+r0+dr+da,aperture+r0+dr-da, tiplen=.03, tipwid=.03

	scale = 1./700

	ccd = [0,0,1280,600]*scale
	ccd_center = [640,1303,0]*scale
	ccd_fov_phi = (270+30*[-1,1])/!radeg
	ccd_fov_rad = [1153,1251]*scale


	center = [0,.0,0]

	x = [ccd[0],ccd[2],ccd[2],ccd[0],ccd[0]]-ccd_center[0]+center[0]
	y = [ccd[1],ccd[1],ccd[3],ccd[3],ccd[1]]-ccd_center[1]+center[1]
	z = replicate(0,5)

	plots, x, y, z

	plot3dtext, center, 'Center', chars=chars, labeloffset=[.25,-.05,0]
;	plot3dtext, [x[0],y[0],z[0]], '(0,0)', chars=chars, labeloffset=[0,-.15,0]

	r1 = [ cos(ccd_fov_phi[0]),sin(ccd_fov_phi[0]),0]
	r2 = [ cos(ccd_fov_phi[1]),sin(ccd_fov_phi[1]),0]
	n = 20
	angle = (270-28.5*gridgen(n,range=[-1,1]))/!radeg

; mark 60 degree span of fov

	plot3dline, center, 1.05*ccd_fov_rad[0]*[cos(angle[n-1]),sin(angle[n-1]),0], linestyle=2
	plot3dline, center, 1.05*ccd_fov_rad[0]*[cos(angle[  0]),sin(angle[  0]),0], linestyle=2
;	plot3dline, center, 1.05*ccd_fov_rad[0]*[cos(angle[  0]),sin(angle[  0]),0],	$
;		linestyle=2, labeltext='1200', chars=chars,	labeldist=-.7, labeloffset=[.2,0,0]

	plot3darc, center,	0.2*ccd_fov_rad[0]*[cos(angle[  0]),sin(angle[  0]),0],	$
						0.2*ccd_fov_rad[0]*[cos(angle[n-1]),sin(angle[n-1]),0],	$
		labeltext='57!9%!X', labelradius=1.4, chars=chars

; Boundaries field of view arc

	plot3darc, center, ccd_fov_rad[1]*r1, ccd_fov_rad[1]*r2
	plot3darc, center, ccd_fov_rad[0]*r1, ccd_fov_rad[0]*r2
	plot3dline, ccd_fov_rad[0]*r1, ccd_fov_rad[1]*r1
	plot3dline, ccd_fov_rad[0]*r2, ccd_fov_rad[1]*r2

; Azimuth 270 degrees

	rad = 0.5*total(ccd_fov_rad)

	plot3dline, center, center-2*yunit, linestyle=1, thick=2
	plot3dline, center-rad*yunit+[.3,-.3,0], center-rad*yunit,	$
		labeltext='Optical Axis', labeldist=-.5, labeloff=[.3,0.0,0], chars=chars,	$
		tiplen=.03, tipwid=.03

; Centroids of fibers

	for i=0,n-1 do begin
		r = center+rad*[cos(angle[i]),sin(angle[i]),0]
		plot3darc, r, 0.01*xunit, 0.01*yunit, 0, 2*!pi

		if straight_num then	$
			inum = 20-i		$
		else					$
			inum = n-1-2*i

		plot3dtext, 1.1*r, strcompress(inum, /rem)
	endfor

	i = 15
	ri = rad*[cos(angle[i]),sin(angle[i]),0]
	plot3dline, center, center+1.13*ri, linestyle=2

	rj = rad*[cos(angle[i+1]),sin(angle[i+1]),0]
	plot3dline, center, center+1.13*rj, linestyle=2

;	plot3darc, center, 1.1*ri, 1.1*rj, labeltext='3!9%!X', labeloffset=[-.1,-.0,0], chars=chars
	plot3darc, center, 0.9*ri, 0.9*rj, labeltext='3!9%!X', labeloffset=[-.1,-.0,0], chars=chars

	phi = (270+10*[-1,1])/!radeg

	r1 = [ cos(phi[0]),sin(phi[0]),0]
	r2 = [ cos(phi[1]),sin(phi[1]),0]
	dr = [ cos(phi[0]+3/!radeg),sin(phi[0]+3/!radeg),0]
	plot3darc, center, 0.87*ccd_fov_rad[1]*r1, 0.87*ccd_fov_rad[1]*r2, tiplen=.07, tipwid=.05

end
2: begin

	aperture = [0,0.25,0]

	aspect = 1.5
	scale  = 1.5

	phi_box = 30/!radeg
	rad_box = 1.*scale

	twin, /del
	twin, xsize=400, ysize=aspect*400

	r0 = [1,aspect,1]*1.25
	setup3d, -r0, r0, /xyplane

	dy = 0.07

	y = .9
	xyouts, /normal, .1,y, 'B!Dbright!N=P!D1!N+G*(P!D2!N+B!Dsource!N)',	$
		chars=chars*1.5
	y = y-dy
	xyouts, /normal, .1,y, 'B!Dfaint !N=P!D1!N+G*P!D2!N',	$
		chars=chars*1.5

	y = y-dy
	xyouts, /normal, .1,y, 'B!Dbright!N-B!Dfaint!N=G*B!Dsource!N',	$
		chars=chars*1.5

	scale = 1./700

	ccd = [0,0,1280,600]*scale
	ccd_center = [640,1303,0]*scale
	ccd_fov_phi = (270+30*[-1,1])/!radeg
	ccd_fov_rad = [1153,1251]*scale


	center = [0,.5,0]

	x = [ccd[0],ccd[2],ccd[2],ccd[0],ccd[0]]-ccd_center[0]+center[0]
	y = [ccd[1],ccd[1],ccd[3],ccd[3],ccd[1]]-ccd_center[1]+center[1]
	z = replicate(0,5)

	plots, x, y, z

	r1 = [ cos(ccd_fov_phi[0]),sin(ccd_fov_phi[0]),0]
	r2 = [ cos(ccd_fov_phi[1]),sin(ccd_fov_phi[1]),0]

	plot3dline, center, center+ccd_fov_rad[0]*r1, linestyle=2
	plot3dline, center, center+ccd_fov_rad[0]*r2, linestyle=2

; mark 60 degree span of fov

	plot3darc, center, 0.2*ccd_fov_rad[0]*r1, 0.2*ccd_fov_rad[0]*r2,	$
		labeltext='60!9%!X', labelradius=1.4, chars=chars

	plot3darc, center, ccd_fov_rad[1]*r1, ccd_fov_rad[1]*r2
	plot3darc, center, ccd_fov_rad[0]*r1, ccd_fov_rad[0]*r2
	plot3dline, center+ccd_fov_rad[0]*r1, center+ccd_fov_rad[1]*r1
	plot3dline, center+ccd_fov_rad[0]*r2, center+ccd_fov_rad[1]*r2

	n = 20
	angle = (270+28.5*gridgen(n,range=[1,-1]))/!radeg
	rad = 0.5*total(ccd_fov_rad)
	for i=0,n-1 do begin
		r = center+rad*[cos(angle[i]),sin(angle[i]),0]
		plot3darc, r, 0.01*xunit, 0.01*yunit, 0, 2*!pi
	endfor

	phi = (270+10*[-1,1])/!radeg

	back = [[50,30,0],[130,100,0]]*scale-(ccd_center-center)#[1,1]

	x = [back[0],back[3],back[3],back[0],back[0]]
	y = [back[1],back[1],back[4],back[4],back[1]]
	z = replicate(0,5)

	plots, x, y, z

	r0 = 0.5*total(back,2)+[.06,-.06,0]
	plot3dline, r0+[.5,-.2,0], r0, labeltext='faint',labeldist=-.7, chars=chars, tiplen=.04, tipwid=.04

	r1 = center+rad*[ cos(phi[0]),sin(phi[0]),0]
	r0 = r1+[.06,.1,0]
	plot3dline, r0+[.3,.4,0], r0, labeltext='bright',labeldist=-.55, chars=chars, tiplen=.04, tipwid=.04
end
3: begin
	r0 = [1,1,1]*1.

	twin, /del
	twin, xsize=400, ysize=400

	setup3d, -r0+[0,.07,0], r0+[0,.07,0], /xyplane

	scale = 1./700

	ccd = [0,0,1280,600]*scale
	ccd_center = [640,1303,0]*scale
	ccd_fov_phi = (270+30*[-1,1])/!radeg
	ccd_fov_rad = [1153,1251]*scale

	center = [0,1,0]

	x = [ccd[0],ccd[2],ccd[2],ccd[0],ccd[0]]-ccd_center[0]+center[0]
	y = [ccd[1],ccd[1],ccd[3],ccd[3],ccd[1]]-ccd_center[1]+center[1]
	z = replicate(0,5)

	plots, x, y, z

	plot3dtext, center, 'Center', chars=chars, labeloffset=[.2,-.05,0]

	n = 20
	angle = (270-28.5*gridgen(n,range=[-1,1]))/!radeg
	m = 21
	angle1 = (270-30.0*gridgen(m,range=[-1,1]))/!radeg

; mark 57 degree span of fov

	plot3dline, center, center+1.05*ccd_fov_rad[0]*[cos(angle[n-1]),sin(angle[n-1]),0], linestyle=2
	plot3dline, center, center+1.05*ccd_fov_rad[0]*[cos(angle[  0]),sin(angle[  0]),0], linestyle=2

; Boundaries field of view arc

	r1 = [ cos(ccd_fov_phi[0]),sin(ccd_fov_phi[0]),0]
	r2 = [ cos(ccd_fov_phi[1]),sin(ccd_fov_phi[1]),0]

	plot3darc, center, ccd_fov_rad[1]*r1, ccd_fov_rad[1]*r2
	plot3darc, center, ccd_fov_rad[0]*r1, ccd_fov_rad[0]*r2

; Azimuth 270 degrees

	rad = 0.5*total(ccd_fov_rad)

; Centroids of fibers

	for i=0,n-1 do begin
		dr = rad*[cos(angle[i]),sin(angle[i]),0]
		plot3darc, center+dr, 0.01*xunit, 0.01*yunit, 0, 2*!pi
		if straight_num then	$
			inum = 20-i		$
		else					$
			inum = n-1-2*i

		plot3dtext, center+1.1*dr, strcompress(inum, /rem)
	endfor

	for i=0,m-1 do begin
		r = [cos(angle1[i]),sin(angle1[i]),0]
		plot3dline, center+ccd_fov_rad[0]*r,center+ccd_fov_rad[1]*r
	endfor

end
4: begin

	zstd = -1
	for j=0,3 do begin
;		c = 'x'
		c = 'y'

		case j of
		0: status = flt_read('F:\fov\flt_1'+c+'\a_ring.dat', ring, /exp)
		1: status = flt_read('F:\fov\flt_2'+c+'\a_ring.dat', ring, /exp)
		2: status = flt_read('F:\fov\flt_3'+c+'\a_ring.dat', ring, /exp)
		3: status = flt_read('F:\fov\bham' +c+'\a_ring.dat', ring, /exp)
		endcase

		for i=2,40,2 do begin
			zval = ring[2+i,*]
			zstd = [zstd, stddev(zval)/mean(zval)]
		endfor
	endfor
	zstd = zstd[1:*]*100

	a = ((where(zstd gt .13) mod 20)+1)*2
	print,a
	i = 1+[0,20,40,60]
	print, ((i mod 20)+1)*2
	print, zstd[ i ]

	qbargraph, zstd, /histogram
end
else: message, /info, 'figure not available'
endcase

return  &  end
