;-------------------------------------------------------------------
;	wacwavescan.pro = Do WAC wavelength scans
;	R. Sterner, 2003 Jun 26
;
;	Setup.txt contains filter info.  Each filter line contains
;	filter_num wavlen_1 wavlen_2 wavlen_step exp1 exp2 ...
;	     0        1        2        3          4    5  ...
;-------------------------------------------------------------------

	pro wacwavescan

xmess,'Check hand item values',/wait

	;========================================================
	;	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_note,'WAC wavelength scan',/time
	ocf_note,' '


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

	nimages = s.nimages+0		; # images.
	nbacks = s.nbacks+0		; # backgrounds.
	filt = s.filter			; Filter lines.
	imager = s.imager+0
	bin = s.bin+0
	backs_flag = s.backs_flag+0	; Take backgrounds? 0=no, 1=yes.
	x1 = s.x1+0	; Box for area total for plot.
	x2 = s.x2+0
	y1 = s.y1+0
	y2 = s.y2+0
	numpix = (x2-x1+1.)*(y2-y1+1.)

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

	window,xs=500,ys=300			; Set up plot window.
	erase					; This fixes IDL colors.
	win = !d.window				; Plot window.

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

	if backs_flag eq 0 then goto, skip	; Skip backgrounds?
	
	;----------------------------------------------------------
	;  Background images
	;----------------------------------------------------------
	if tag_test(s,'txt0') then ocf_note,s.txt0
	ocf_note,' '
	if tag_test(s,'txt0') then xmess,s.txt0,/wait
	ocf_note,' '
	ocf_note,' '
	ocf_note,'==================================='
	ocf_note,'==================================='
	ocf_note,'Taking background reference images.'
	ocf_note,'==================================='
	ocf_note,'==================================='
	ocf_note,' '
	c->stop
	if tag_test(s,'txtd') then ocf_note,s.txtd
	ocf_note,' '
	if tag_test(s,'txtd') then xmess,s.txtd,/wait
	;-----  WAC backgrounds  --------
	nwacfilt = n_elements(filt)	      ; # WAC filters (+ exposures).
	for i_filt=0, nwacfilt-1 do begin
	  ftxt = filt(i_filt)                 ; Next filter # and exp list.
	  fwnumtxt = getwrd(ftxt,0)           ; Filter number (as text).
	  fwnum = fwnumtxt + 0                ; Filter number (numeric).
	  wordarray, getwrd(ftxt,4,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, /time
          ocf_note,'|||||||||||||||||||||||||||||||||||||||||'
          ocf_note,'========================================='
          c->stop
          fw_move,fwnum                         ; Actually set WAC filter.
          c->reset
          ocf_mode, get_mode=mode
          if mode eq 'RUN' then begin
            repeat begin			; Wait until filter in place.
              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
          endif ; RUN

          ;-------  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,'Setting exposure to '+txt, /time
            c->stop
            c->set,exposure=expv                  ; Actually set exposure.
            c->reset
            ocf_note,'-----------------------------------------'
  
            ;------- Images loop  ---------------------
            txt = strtrim(nimages,2)
            ocf_note,'    Taking '+txt+' image'+ $
              plural(nimages)+' ...'
            for i = 0, nimages-1 do begin ; Images.
              c->snap,img,/show,/save,img_typ=typ, added=add, acomment=acmt
              mx = max(img)
              print,' WAC background max = '+strtrim(mx,2)+ $
                    '  Filter = '+strtrim(fwnum,2)+   $
                    '  Binning = '+strtrim(bin,2)+     $
                    '  Exposure = '+strtrim(expv,2)
            endfor ; i
          endfor ; i_exps
	endfor  ; i_filt
	
	;----------------------------------------------------------
	;  Scan images
	;----------------------------------------------------------
skip:
	print,' '
	print,' '
	print,' '
	ocf_note,' '
	ocf_note,' '
	ocf_note,'==================================='
	ocf_note,'==================================='
	ocf_note,'Taking wavelength scan images.'
	ocf_note,'==================================='
	ocf_note,'==================================='
	ocf_note,' '
	c->stop

	;----------------------------------------------------------
	;  Set monochromater values
	;----------------------------------------------------------
	;-----  Set constant hand items  ----------------
	hand_struct,tag='MC_SRC',val='Incandescent'
	hand_struct,tag='MC_SPWR',val='42 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='RMLT_OFF',val='OFF'
	;-----  Set interactive hand items  ----------------
	ocf_hand,'MC_ENFIL','Set Monochromator entrance ND filter',/enter
	ocf_hand,'MC_EXFIL','Set Monochromator exit ND filter',/enter
	ocf_hand,/dolist

	if tag_test(s,'txts') then ocf_note,s.txts
	ocf_note,' '
	if tag_test(s,'txts') then xmess,s.txts,/wait
	ocf_wait,60,title='Lamp warmup'
        c->reset

	;-----  WAC wavelength scans  --------
	nwacfilt = n_elements(filt)	      ; # WAC filters (+ exposures).
	for i_filt=0, nwacfilt-1 do begin
	  ftxt = filt(i_filt)                 ; Next filter # and exp list.
	  fwnumtxt = getwrd(ftxt,0)           ; Filter number (as text).
	  fwnum = fwnumtxt + 0                ; Filter number (numeric).
	  wv1 = getwrd(ftxt,1)+0	      ; Wavelength 1.
	  wv2 = getwrd(ftxt,2)+0	      ; Wavelength 2.
	  wvstep = getwrd(ftxt,3)+0	      ; Wavelength step.
	  wavlen = makex(wv1,wv2,wvstep)      ; Wavelength array.
	  wavlen = long(wavlen)
	  num_wavlen = n_elements(wavlen)     ; Number of wavelengths.
	  wordarray, getwrd(ftxt,4,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, /time
          ocf_note,'|||||||||||||||||||||||||||||||||||||||||'
          ocf_note,'========================================='
          ocf_note,' '
          c->stop
          fw_move,fwnum                       ; Actually set WAC filter.
          c->reset
          ocf_mode, get_mode=mode
          if mode eq 'RUN' then begin
            repeat begin		      ; Wait until filter in place.
              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
          endif ; RUN
	  ;------  Setup for plot  ------------------------
	  xx = transpose(wavlen)/10		; Angstroms to nm.
	  xx = rebin(xx,nimages,num_wavlen)	; Repeat each wavlen nimages x.
	  yy = fltarr(n_elements(xx))		; Save image box total here.
	  icnt = 0				; Image counter.
	  tt = 'Wavelength scan for filter '+fwnumtxt
	  tx = 'Wavelength (nm)'
	  ty = 'Relative strength'
	  ;------------------------------------------------

	  ;-------  Wavelength loop  -------------------
          ocf_note,'Number of wavelengths = '+strtrim(num_wavlen,2)
          ocf_note,' '
	  for i_wv = 0, num_wavlen-1 do begin	  ; Loop through wavelengths.

	    wv = wavlen(i_wv)			  ; Wavelength.
	    ocf_mono, wv=wv			  ; Set it.

          ;-------  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,'Setting exposure to '+txt, /time
            c->stop
            c->set,exposure=expv          ; Actually set exposure.
            c->reset
            ocf_note,'-----------------------------------------'
  
            ;------- Images loop  ---------------------
            txt = strtrim(nimages,2)
            ocf_note,'    Taking '+txt+' image'+ $
              plural(nimages)+' ...'
            for i = 0, nimages-1 do begin ; Images.
              c->snap,img,/show,/save,img_typ=typ, added=add, acomment=acmt
              mx = max(img)
              print,' WAC image max = '+strtrim(mx,2)+ $
                    '  Wavelen = '+strtrim(wv,2)+   $
                    '  Filter = '+strtrim(fwnum,2)+   $
                    '  Binning = '+strtrim(bin,2)+     $
                    '  Exposure = '+strtrim(expv,2)
	      ;--------- Plot data  ------------------------
	      if icnt eq 0 then begin			; First point?
		yy = yy + total(img(x1:x2,y1:y2))/numpix ; Fill array with 1st.
	      endif else begin
	        yy(icnt) = total(img(x1:x2,y1:y2))/numpix ; Add area total.
	      endelse
	      wset,win
	      plot,xx,yy,title=tt,xtitl=tx,ytitl=ty,chars=2,/ynoz
	      empty
	      icnt = icnt + 1		; Count image.
	      ;---------------------------------------------
            endfor ; i
          endfor ; i_exps

	  endfor ; i_wv

	endfor  ; i_filt

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