;-------  nac_sphere_1.pro = NAC sphere test 1  ----------
;	R. Sterner, 2003 Jun 20
;	H. Taylor,  2003 Jun 21	    cam_align uses s.imager
;	H. Taylor,  2003 Jun 21	    updated to ignore filters for NAC

	pro nac_sphere_1

	;========================================================
	;	Initialize
	;========================================================
	;-----  CHECK to test, RUN to run  ---------
;	mode = 'CHECK'
	mode = 'RUN'
	ocf_init, mode=mode

        setmet                          ; Set GSE MET to JS.
        chk_timediff,td1                ; Check time diff.

	;-------  Values  --------------
	s = get_setup(err=err)		; Read setup.txt file.
	if err ne 0 then stop

	imager = s.imager+0		; Imager to use.
	bin = s.bin+0			; Binning mode.

	;------  Point camera toward sphere  ---------
	cam_align, imager, /window, azi=azi_cen, alt=alt_cen, trans=trn_cen

	lamps = s.lamps+0		; Lamp control bit patterns.
	num_lamps = n_elements(lamps)
	i_lamp0 = s.i_lamp0<(num_lamps-1)

	exps = s.exps+0			; Exposures array.
	num_exps = n_elements(exps)
	i_exps0 = s.i_exps0<(num_exps-1)

	nimages = s.nimages+0          ; Number of ensemble images to take.

	cutoff = s.cutoff+0		; Image brightness cutoff.
	x1 = s.x1+0			; Image indices of sub-area.
	x2 = s.x2+0
	y1 = s.y1+0
	y2 = s.y2+0
	subtxt = strtrim(x1,2)+','+strtrim(x2,2)+','+$
	  strtrim(y1,2)+','+strtrim(y2,2)

	addcom = $
	 ['Image brightness cutoff', $
	  'Sub-area indices: x1,x2,y1,y2.']
	ss = {cutoff:cutoff, subarea:subtxt}

	;------- Images  ---------
	nlamps = num_lamps-i_lamp0
	nexps = num_exps-i_exps0
	ndim_indices, [num_lamps, num_exps], [i_lamp0, i_exps0], i_info
	num_sets = i_info.nl		; # sets left to do.
	tot_num = num_sets*nimages
	img_cnt = 0			; Image counter.

	ocf_log,/write,/screen,command='nac_sphere_1'
	ocf_note,' '
	ocf_note,'Lamps settings = '+strtrim(nlamps,2)
	ocf_note,'Exposures = '+strtrim(num_exps,2)
	ocf_note,'Sets of images = '+strtrim(num_sets,2)
	ocf_note,'Images in each set = '+strtrim(nimages,2)
	ocf_note,'Max number of images = '+strtrim(tot_num,2)
	ocf_note,' '
	ocf_note,s.txt0
	ocf_note,' '
	if tag_test(s,'txt0') then xmess,s.txt0,/wait

	;-------  Set up camera object and init  ------------
	if mcam_get_obj(c) ne 0 then return

	erase					; This fixes IDL colors.
	wdelete

	;------  Set imager and  binning  ----------------------
	ocf_note,'Setting image binning to '+strtrim(bin,2)
	ocf_note,'Setting imager to '+strtrim(imager,2)
	c->stop
        c->set,fpu_bin=bin,imager=imager
	c->reset

	;-------  Start saturation values file  ----------
	file = 'saturation.txt'
	openw,lun,file,/get_lun

	;========================================================
	;  Image Loops
	;========================================================

	ocf_note,' '
	ocf_note,'Taking images.'
	ocf_note,' '

	last_lamps = 0				; Last lamps status.

	;--------  Lamps loop  -------------------
	for i_lamp= i_lamp0, num_lamps-1 do begin
	  new_lamps = lamps(i_lamp)		; Next lamps status.
	  ocf_sphlight_change, last_lamps, new_lamps, $
	    time=delay, text=txt
	  last_lamps = new_lamps		; Remember lamp status.
	  ocf_note,' '
	  ocf_note,'#########################################'
	  ocf_note,'#########################################'
	  ocf_note,'#########################################'
	  ocf_note,'Setting lamp to '+strtrim(new_lamps,2)
	  ocf_note,txt+' Waiting '+strtrim(delay,2)+' sec ...'
	  ocf_note,'#########################################'
	  ocf_note,'#########################################'
	  ocf_note,'#########################################'
	  ocf_note,' '
	  c->stop
	  ocf_sphlights, new_lamps		; Change sphere lights.
	  sphere_wait, delay, txt		; Wait.
	  c->reset


	    sat_flag = 0			;Image saturation flag.

	    ;-------  Exposures loop  --------------------
	    for i_exps = i_exps0, num_exps-1 do begin	; Loop through expos.
	      expv = exps(i_exps)		; Next exposure.
	      txt = strtrim(expv,2)		;   as text.
	      ocf_note,' '
	      ocf_note,'-----------------------------------------'
	      ocf_note,'Setting exposure to '+txt
	      c->set,exposure=expv		; Actually set exposure.
	      ocf_note,'-----------------------------------------'
	      ocf_note,' '

	      ;------- Images loop  ---------------------
	      txt = strtrim(nimages,2)
	      img_cnt = img_cnt + 1
	      txtcnt = strtrim(img_cnt,2)
	      ocf_note,'    '+txtcnt+'. Taking '+txt+' image'+ $
	        plural(nimages)+' ...'
	      for i = 0, nimages-1 do begin	; Images.
	        c->snap,img,/show,/save, added=ss, acomm=addcom
		sub = img(x1:x2,y1:y2)		; Grab sub-area.
	        mn = mean(sub)			; Sub-area mean.
	        ocf_note,' '
	        ocf_note,' Sub-area mean = '+strtrim(mn,2)
	        ocf_note,' '
	        if mn ge cutoff then begin
	          ocf_note,' '
	          ocf_note,' >>>===> Image brightness above cutoff.'
	          ocf_note,'         Skipping.'
	          sat_flag = 1			; Set image sat flag.
	          printf,lun,	' Saturated for: Sphere=',new_lamps,$
				'  Exposure=',expv
	          break				; Break out of img loop.
	        endif
	      endfor ; i
	      ;-----  Restart info  --------
	      s.i_lamp0 = strtrim(i_lamp,2)
	      s.i_exps0 = strtrim(i_exps,2)
	      txtputkey, 'setup_.txt',s

	      if sat_flag then break		; Break out of exp loop.

	    endfor ; i_exps
	    i_exps0 = 0

	endfor ; i_lamp
	i_lamps0 = 0

	;========================================================
	;	Terminate
	;========================================================
	free_lun, lun
	ocf_note,' '
	ocf_note,'-------------------------------------'
	ocf_note,'  Run complete.'
	ocf_note,'-------------------------------------'
	ocf_terminate

        obj_destroy, c                  ; Destroy object (to free fg).

        print,' Start and end time difference check'
        print,' JS_UT - MET: ',td1
        chk_timediff,td2                ; Check time diff.

        end
