;-------  wac_sphere_2.pro = WAC sphere filter test 2  ----------
;	Howard Taylor, 2003 Jun 21
;	R. Sterner, 2003 Jun 22

	pro wac_sphere_2

	;========================================================
	;	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.

	ocf_log,/write,/screen,command='wac_sphere_2'
	ocf_note,' '

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

	;-------  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.

	lamps = s.lamps			; Lamp control bits and # images.
	num_lamps = n_elements(lamps)
	i_lamp0 = s.i_lamp0<(num_lamps-1)

        filter = s.filter		; Filters.
	num_filt = n_elements(filter)
	i_filt0 = s.filt_i<(num_filt-1)	; Start index (for restart).

	;------- Images  ---------
	nlamps = num_lamps-i_lamp0
	nfilt = num_filt-i_filt0
	ndim_indices, [num_lamps, num_filt],[i_lamp0, i_filt0], i_info
	num_sets = i_info.nl		; # sets left to do.
	img_cnt = 0			; Image counter.
	tot_images = 0			; Count images taken.

	ocf_note,' '
	ocf_note,'Lamps settings = '+strtrim(nlamps,2)
	ocf_note,'Filters = '+strtrim(num_filt,2)
	ocf_note,'Sets of images = '+strtrim(num_sets,2)+' x exposures'
	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

	;========================================================
	;  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

	  ltxt = lamps(i_lamp)			; Next lamp and # images.
	  new_lamps_txt = getwrd(ltxt,0)	; new lamp status (as text).
	  new_lamps = new_lamps_txt + 0		; new lamp status (numeric).
 	  nimages = getwrd(ltxt,1)		; number of images in 2nd item.
	  nimages = nimages+0			; number of images to numeric.

	  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

	  ;------  WAC filter loop  --------------
	  for i_filt = i_filt0, num_filt-1 do begin	; Loop through filters.
	    ftxt = filter(i_filt)		; Next filter # and exp list.
	    fwnumtxt = getwrd(ftxt,0)		; Filter number (as text).
	    fwnum = fwnumtxt + 0		; Filter number (numeric).
	    wordarray, getwrd(ftxt,1,99), exps	; Exposure list to array.
	    exps = exps+0			; Exposure array to numeric.
	    num_exps = n_elements(exps)		; Number of exposures.
	    ocf_note,' '
	    ocf_note,'========================================='
	    ocf_note,'|||||||||||||||||||||||||||||||||||||||||'
	    ocf_note,'Setting WAC filter to '+fwnumtxt
	    ocf_note,strtrim(num_exps,2)+' exposure'+plural(num_exps)+ $
	    ' for this filter.'
	    ocf_note,'|||||||||||||||||||||||||||||||||||||||||'
	    ocf_note,'========================================='
	    ocf_note,' '
	    c->stop
	    fw_move,fwnum			; Actually set WAC filter.
	    c->reset
	    repeat begin
	      c->snap, /nofits,head=shdr
	      hdr_fwnum = fw_res_to_num(shdr.fw_read)
	      diff = abs(fwnum-hdr_fwnum)
	      print,' Filter diff = ',diff
	    endrep until (diff lt 0.05)
	    throw_away, c, 1

	    ;-------  Exposures loop  --------------------
	    for i_exps = 0, num_exps-1 do begin	; Loop through exposures.
	      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
	      endfor ; i
	      tot_images = tot_images + nimages	; Count images just taken.
	      ;-----  Restart info  --------
	      s.i_lamp0 = strtrim(i_lamp,2)
	      s.filt_i = strtrim(i_filt,2)+'		; Filter start index.'
	      txtputkey, 'setup_.txt',s

	    endfor ; i_exps
	    i_exps0 = 0

	  endfor ; i_filt
	  i_filt0 = 0

	endfor ; i_lamp
	i_lamps0 = 0

	;========================================================
	;	Terminate
	;========================================================
	ocf_sphlights,0			; Ensure all lights are off
	ocf_note,' '
	ocf_note,'-------------------------------------'
	ocf_note,'  Run complete.'
	ocf_note,'  Total images taken: '+strtrim(tot_images,2)
	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
