;-----------------------------------------------------
;       darks_loop.pro = Do a dark image sequence loop
;	R. Sterner, 2003 Jul 15 from darks.pro
;
;	Take dark images.
;-----------------------------------------------------

        pro darks_loop

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

	spawn,'cht  Press keyboard key to quit'

	;-------  Set initial values  --------------
	s = get_setup(err=err)	; Read setup file (setup.txt or setup_.txt).
	if err ne 0 then stop

	if tag_test(s,'txt0') then xmess, s.txt0, /wait	 ; Startup instructions.

        exps = s.exps+0			; Exposures.
        nex = n_elements(exps)

        nimages = s.n_images+0		; Number of ensemble images to take.
	txt_n = strtrim(nimages, 2)+' images'

	n_loops = s.n_loops+0		; How many sets of darks?


	ocf_note,' '
	ocf_log,/write,/screen,command='Dark images'
	ocf_note,' '
	ocf_note,s.txt0
	ocf_note,' '

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

	c->stop				; Stop frame grabber.

	;---------  Set WAC filter  ----------------
	ocf_note,' '
	ocf_note,'  Changing to WAC filter #9 . . .'
	fw_move,9           ;  Select filter 9 for WAC throughout.
	ocf_note,' '
	ocf_note,'========================================='

	c->reset					; Reset frame grabber.

	;========================================================
	;	Dark images
	;========================================================

	set = 0L
	time_0 = systime(1)

	;=======  Continuous Loop  ==================================
	for i_loop = 1, n_loops do begin

	set = set + 1
	set_txt = strtrim(set,2)
	ocf_note,' '
	ocf_note,'=========================================='
	ocf_note,'   Image set '+set_txt
	ocf_note,'=========================================='
	ocf_note,' '

	;=======  Imager: 0: WAC, 1: NAC  ===========================
	for i_im = 0, 1 do begin		; Imagers.
	  txt_im_i = strtrim(i_im,2)		; Imager index as text.
	  txt_im = (['WAC','NAC'])(i_im)	; Imager name.
          c->set,imager=i_im			; Set camera to imager.
	  ocf_note,' '
	  ocf_note,'  Switching to '+txt_im

	  ;=======  Binning: 0: 1024x1024, 1: 512x512  ==============
	  for i_bn = 0, 1 do begin			; Binning.
	    txt_bn_i = strtrim(i_bn,2)			; Binning index as text.
	    txt_bn = (['1024','512'])(i_bn)
	    txt_bn = txt_bn +'X'+ txt_bn
            c->set,fpu_bin=i_bn				; Set camera binning.
	    ocf_note,' '
	    ocf_note,'    Set binning to '+txt_bn
	    ocf_note,'    Doing '+txt_n+' at each exposure . . .'

	    ;========  Exposure time (ms)  ==========================
            for i_ex = 0, nex-1 do begin		; Exposure
	      txt_ex = string(exps[i_ex])+' ms'
	      time_txt = 'Running '+strsec(systime(1)-time_0)
	      ocf_note,'      '+txt_im+', '+txt_bn+$
		', '+txt_ex+', '+txt_n+', Cycle '+set_txt+', '+time_txt
              c->set,exposure=exps[i_ex]		; Set camera expsoure.

	      ;=========  Images  ===================================
              for j = 1,nimages do begin		; Repeats.
                c->snap,img, /show, /save, img_type=1	; Take dark image.
	        print,' Image mean = '+strtrim(mean(img),2)
	        if get_kbrd(0) ne '' then begin
	          if xoption(['Continue','Quit']) eq 1 then goto, done
	        endif
              endfor  ; j

            endfor ; i_ex

	  endfor ; i_bn

	endfor ; i_im

	endfor  ; i_loop

	;========================================================
	;	Terminate
	;========================================================
done:
	ocf_note,'-------------------------------------'
	ocf_log,/write,/screen,command='  Run complete.',/full_time
	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
