;-----------------------------------------------------
;       nac_pinhole_scan.pro = Scan stage to move pinhole.
;	R. Sterner, 2003 Jun 19
;	R. Sterner, 2003 Jun 24 --- Added automatic lamp wait.
;-----------------------------------------------------

        pro nac_pinhole_scan_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_pinhole_scan'
	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 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

	;========================================================
	;	Pinhole scan
	;========================================================
	ocf_note,'Pinhole scan.'
	ocf_note,' '

	;-----  Set constant hand items  ----------------
	hand_struct,tag='MC_SRC',val='Incandescent'
	;-----  Set interactive hand items  ----------------
	ocf_hand,'MC_SPWR','Set lamp to 42 w',good='42 W',bad='-'
	ocf_hand,'RMLT_OFF','Turn room lights off',good='YES',bad='NO'
	ocf_hand,/dolist

	;========================================================
	;  Loops
	;========================================================

	alt_last = -999.

	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,' '

	  ;========================================================
	  ;  Background reference images
	  ;========================================================
	  if back_flag then begin
	    ocf_note,' '
	    ocf_note,'Taking 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 = 0, nbacks-1 do begin		; Images.
              c->snap,img,/show,/save, img_type=2
	    endfor ; i
	    s.back_flag = 0
	    txtputkey, 'setup_.txt',s

	    xmess,'Turn on lamp and click OK',/wait
	    ocf_wait,5*60,title='Waiting for lamp to warm up ...',xsize=400
	  endif

	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'
	  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'
	      c->stop
	      ocf_stage, ax=2, al, /abs
	      c->reset
	      throw_away, c, 1			; Throwaway images.
	      alt_last = al
	    endif

	    ;----------  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
	    ;-----  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
	back_flag = 1			; Do background for next exposure.
	s.back_flag = back_flag
	txtputkey, 'setup_.txt',s

	endfor ; i_exps
	i_exps0 = 0

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