;-----------------------------------------------------
;       nac_stray_light_1.pro = Scan stage to check stray light
;	R. Sterner, 2003 Jun 19
;	R. Sterner, 2003 Jun 24 --- Added automatic lamp wait.
;	H. Taylor,  2003 Jun 29 --- Modified from PSF to Stray Light
;	R. Sterner, 2003 Jun 29 --- Added missing items.
;-----------------------------------------------------

        pro nac_stray_light_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 file.
	if err ne 0 then stop

	bin = s.bin+0			; Binning.
	imager = s.imager+0		; Imager: NAC.

        exps = s.exps+0			; Exposures.
	num_exps = n_elements(exps)
	i_exps0 = s.exps_i<(num_exps-1)	; Start index (for restart).

	azi_center = s.azi_center	; Central values for reference.
	alt_center = s.alt_center
	trans_center = s.trans_center
	st_homed = s.stage_homed

	azi = makex(s.azi_1+0.,s.azi_2+0.,s.azi_step+0.)
	num_azi = n_elements(azi)
	i_azi0 = s.azi_i<(num_azi-1)	; Start index (for restart).

	alt = makex(s.alt_1+0.,s.alt_2+0.,s.alt_step+0.)
	num_alt = n_elements(alt)
	i_alt0 = s.alt_i<(num_alt-1)	; Start index (for restart).

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

	nbacks = s.nbacks+0		; Number of background images.
	back_flag = s.back_flag+0	; Do backgrounds?

	addcom = $
	 ['Stage azi scan center (abs)', $
	  'Stage alt scan center (abs)', $
	  'Stage trans scan center (abs)', $
	  'Stage homed?', $
	  'Azi relative', $
	  'Alt relative']

	nexps = num_exps-i_exps0
	nazi = num_azi-i_azi0
	nalt = num_alt-i_alt0
	ndim_indices, [num_exps, num_azi, num_alt], $
          [i_exps0, i_azi0, i_alt0], 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_stray_light_1'
	ocf_note,' '
	ocf_note,'Exposures = '+strtrim(nexps,2)
	ocf_note,'Steps in azi = '+strtrim(nazi,2)
	ocf_note,'Steps in alt = '+strtrim(nalt,2)
	ocf_note,'Sets of images = '+strtrim(num_sets,2)
	ocf_note,'Images in each set = '+strtrim(nimages,2)
	ocf_note,'Total number of images to take = '+strtrim(tot_num,2)
	ocf_note,' '
	ocf_note,s.txt0
	ocf_note,' '

	;-------  Set monochromator  ------------------------
	ocf_note,' '
	ocf_note,'Setting monochromator wavelength to 0'
	ocf_note,' '
	ocf_mono, wv=0

	;-------  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,'Resetting Az and El stage to center'
	ocf_stage, ax=1, azi_center, /abs
	ocf_stage, ax=2, alt_center, /abs

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

	;========================================================
	;	Stray Light scan
	;========================================================
	ocf_note,'NAC stray Light scan.',/time
	ocf_note,' '

	;-----  Set constant hand items  ----------------
	hand_struct,tag='MC_SRC',val='Incandescent'
	hand_struct,tag='MC_SPWR',val='21 W'
	hand_struct,tag='MC_GRNUM',val='1?'
	hand_struct,tag='MC_SI_L',val='5 mm approx?'
	hand_struct,tag='MC_SI_W',val='2.5 mm?'
	hand_struct,tag='MC_SO_L',val='5 mm approx?'
	hand_struct,tag='MC_SO_W',val='2.5 mm?'
	hand_struct,tag='MC_EXFIL',val='3'
	;-----  set interactive hand items  ----------------
	ocf_hand,'MC_ENFIL','Set Monochromator entrance filter',/enter
	ocf_hand,'RMLT_OFF','Turn room lights off',good='YES',bad='NO'
	ocf_hand,/dolist

	;========================================================
	;  Background reference images
	;========================================================
	if back_flag then begin
	  ocf_note,' '
	  ocf_note,'Taking NAC background reference images.'
	  ocf_note,' '
	  if tag_test(s,'txtd') then xmess,s.txtd,/wait
	  ;----------  Images  ---------------------
	  txt = strtrim(nbacks,2)
	  ocf_note,'Taking '+txt+' images ...'
	  c->reset
	  throw_away, c, 1			; Throwaway images.

	  for i_exps = i_exps0, num_exps-1 do begin
	    expv = exps(i_exps)
	    txt = strtrim(expv,2)
	    ocf_note,' '
	    ocf_note,'#########################################'
	    ocf_note,'Setting exposure to '+txt
	    c->set,exposure=expv
	    ocf_note,'#########################################'
	    ocf_note,' '

	    for i = 0, nbacks-1 do begin		; Images.
              c->snap,img,/show,/save, img_type=2
	    endfor ; i

	  endfor ; i_exps

	  s.back_flag = 0
	  txtputkey, 'setup_.txt',s
	endif
	i_exps0 = 0

	ocf_note,' '
	ocf_note,'Turning lamp on and waiting for warmup ...',/time
	ocf_note,' '
	xmess,'Turn on lamp and click OK',/wait
	ocf_wait,1*60,title='Waiting for lamp to warm up ...',xsize=400

	;-------  WAC Reference Image  -------------
	;------  Set imager and  binning  ----------------------
;	ocf_note,' '
;	ocf_note,'Taking WAC reference images:'
;	ocf_note,'Setting image binning to 1'
;	ocf_note,'Setting imager to WAC'
;	ocf_note,'Setting WAC filter to 7'
;	ocf_note,' '
;	ocf_note,'Manually translating stage to 96 to center WAC ...',/time
;	ocf_note,' '
;	xmess,['Set stage translation to 96 to center WAC', $
;		'Click OK when ready'],/wait
;	fw_move,7			; Actually set WAC filter.
;	c->set,fpu_bin=1,imager=0
;	ss = {azi_cen:azi_center, alt_cen:alt_center, $
;	    trn_cen:trans_center, st_homed:st_homed, $
;	    azi_rel:0, alt_rel:0}
;	
;	wac_exp = [ 110, 1989, 5500]
;	nwac=n_elements(wac_exp)
;	mc_ent = ['0','5']
;	for kkk=0,1 do begin		; MC entrance filter.
;	  val = mc_ent(kkk)
;	  ocf_hand,'MC_ENFIL','Set Monochromator entrance filter to '+val,$
;	    good=val,bad='-1'
;	  ocf_hand,/dolist
;
;	for jjj=0,nwac-1 do begin
;	   myexp = wac_exp[jjj]
;	   ocf_note,'Setting WAC exposure to '+strtrim(myexp,2)
;	   c->set,exposure=myexp
;	   c->reset
;	   throw_away, c, 1			; Throwaway images.
;	   ocf_note,'Taking 3 images ...'
;	   for iii = 0,2 do begin
;		c->snap,img,/show,/save, added=ss, acomm=addcom, img_type=0
;	   endfor ; iii
;	endfor ; jjj
;	endfor ; kkk
;
;	ocf_note,' '
;	ocf_note,'Manually translating stage to 18 to center NAC ...',/time
;	ocf_note,' '
	xmess,['Set stage translation to 18 to center NAC', $
		'Click OK when ready'],/wait

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


	;========================================================
	;  Take NAC at center position with MC entrance filter 5.
	;========================================================
	ocf_note,' '
	ocf_note,'Setting Monochromator entrance filter to 5'
	ocf_note,'to take NAC reference image at center of scan.'
	ocf_note,' '
	ocf_hand,'MC_ENFIL','Set Monochromator entrance filter to 5',$
	  good='5',bad='-1'
	ocf_hand,/dolist
	for i_exps = i_exps0, num_exps-1 do begin
	  expv = exps(i_exps)
	  txt = strtrim(expv,2)
	  ocf_note,' '
	  ocf_note,'#########################################'
	  ocf_note,'Setting exposure to '+txt
	  c->set,exposure=expv
	  ocf_note,'#########################################'
	  ocf_note,' '

	  for i = 0, nimages-1 do begin		; Images.
	    ss = {azi_cen:azi_center, alt_cen:alt_center, $
	      trn_cen:trans_center, st_homed:st_homed}
            c->snap,img,/show,/save, added=ss, acomm=addcom(0:3)
	  endfor ; i
	endfor;  i_exps
	i_exps0 = 0

	ocf_note,' '
	ocf_note,'Setting Monochromator entrance filter to 0'
	ocf_note,'for NAC stray ligth scan.'
	ocf_note,' '
	ocf_hand,'MC_ENFIL','Set Monochromator entrance filter to 0',$
	  good='0',bad='-1'
	ocf_hand,/dolist

	;========================================================
	;  Loops
	;========================================================
	alt_last = -999.

	;========================================================
	;  Azimuth loop start
	;========================================================
	for i_azi = i_azi0, num_azi-1 do begin
	  azr = azi(i_azi)
	  az = azr + azi_center
	  ocf_note,' '
	  ocf_note,'========================================='
	  ocf_note,'Setting azi to '+strtrim(az,2)+' abs, '+$
	    strtrim(azr,2)+' rel',/time
	  c->stop
	  ocf_stage, ax=1, az, /abs
	  c->reset
	  ocf_note,'========================================='
	  ocf_note,' '

	  ;-------  Alt loop  ------------------------
	  for i_alt = i_alt0, num_alt-1 do begin
	    alr = alt(i_alt)
	    al = alr + alt_center
	    if al ne alt_last then begin
	      ocf_note,'  -----------------------------------------'
	      ocf_note,'  Setting alt to '+strtrim(al,2)+' abs, '+$
	        strtrim(alr,2)+' rel', /time
	      c->stop
	      ocf_stage, ax=2, al, /abs
	      c->reset
	      throw_away, c, 1			; Throwaway images.
	      alt_last = al
	    endif


;########  Start exposures loop  #######
	    for i_exps = i_exps0, num_exps-1 do begin
	      expv = exps(i_exps)
	      txt = strtrim(expv,2)
	      ocf_note,' '
	      ocf_note,'#########################################'
	      ocf_note,'Setting exposure to '+txt
	      c->set,exposure=expv
	      ocf_note,'#########################################'
	      ocf_note,' '

	    ;----------  Images  ---------------------
	    txt = strtrim(nimages,2)
	    img_cnt = img_cnt + 1
	    txtcnt = strtrim(img_cnt,2)
	    ocf_note,'    '+txtcnt+'. Taking '+txt+' images ...'

	    for i = 0, nimages-1 do begin		; Images.
	      ss = {azi_cen:azi_center, alt_cen:alt_center, $
		    trn_cen:trans_center, st_homed:st_homed, $
		    azi_rel:azr, alt_rel:alr}
              c->snap,img,/show,/save, added=ss, acomm=addcom
	    endfor ; i

;####  End exposures loop
	    endfor ; i_exps

	    ;-----  Restart info  --------
	    s.azi_i = strtrim(i_azi,2)+'		; Azi start index.'
	    s.alt_i = strtrim(i_alt,2)+'		; Alt start index.'
	    s.exps_i = strtrim(i_exps,2)+'		; Exps start index.'
	    txtputkey, 'setup_.txt',s

	  endfor ; i_alt
	  i_alt0 = 0

	endfor ; i_azi
	i_azi0 = 0
	s.back_flag = back_flag
	txtputkey, 'setup_.txt',s


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