pro chk_embed9, serial, qm2, hc=hc, sub10=sub10, qstop=qstop, last_only=last_only, fit_result=fit_result, save=save
;
;
;
;HISTORY:
;       Written Jan/Feb by M.Morrison
;V1.0      Feb-94 (MDM) 
;V2.0	16-Feb-94 (MDM) - Modified to be a function and to use the
;			  FITS files
;			- Label sub box number on the multi plots
;			- Plot the relative offset from the position 0
;V3.00  18-Feb-94 (MDM) - CORRECTED EMBEDDING ERROR
;                         Modified to call EMBED9 with "qm2"
;V3.01	18-Feb-94 (MDM) - Modified plotting of relative offset from position 0
;V3.02	23-Feb-94 (MDM) - Added plotting of y position versus central wave - pos 0
;V3.03	24-Feb-94 (MDM) - Modified to plot relative offset from position 0 versus
;			  relative CCD position, not position on the Michelson.
;			  ** This modification was lost and had to be re-created **
;V3.04	25-Feb-94 (MDM) - Added /SAVE option to save the 6x6 binned average signal
;			  levels for each position in the overlap area.
;
save_device = !d.name
if (keyword_set(hc)) then begin
    set_plot,'ps'
    device, /land
end
;
progver = 'CHK_EMBED9.PRO  Ver 3.04'
;
embed9, serial, qm2, array, pos, out, out2, mposout, ccdposout, sub10=sub10
;
m_arr = ['  M1', '  M2']
tit = 'Series: ' + strtrim(serial,2) + m_arr(qm2)
;
ist = 64-24
ien = 64+24
nn = 8
nout = (ien-ist)/nn
chk_map = fltarr(nout, nout, 9)
for i=0,nout-1 do for j=0,nout-1 do begin
    ii = ist + i*nn
    jj = ist + j*nn
    for k=0,8 do begin
	temp = out(ii:ii+nn-1, jj:jj+nn-1, k)
	ss = where(temp ne 0, nss)
	;print, i, j, k, ii, jj, nss
	if (nss gt nn*nn/2) then chk_map(i,j,k) = total(temp(ss))/nss
	;if (nss eq nn*nn) then chk_map(i,j,k) = total(temp(ss))/nss
    end
end
;
ss = where((chk_map lt 10) and (chk_map ne 0))
if (ss(0) ne -1) then chk_map(ss) = chk_map(ss) + 200*(qm2+1)
;
if (keyword_set(save)) then begin
    item_arr = ['_m1chk9', '_m2chk9']
    filnam = 's' + string(serial,format='(i3.3)') + item_arr(qm2) + '.fits'
    outfil = concat_dir('$MDI_CAL_DATA', filnam)
    print, 'SAVING: ', outfil
    ;;wrt_fits, outfil, dummy_head, chk_map
    for k=0,8 do save_do1pos, head0, serial, k, sub10, q900, reform(chk_map(*,*,k)), item=item_arr(qm2), laspos=ccdposout(*,k)
end
;
if (keyword_set(last_only)) then goto, skip1
;
xarr = indgen(9)
imin = min(chk_map(where(chk_map ne 0)))
imax = max(chk_map)
iplot = 0
!p.multi = [0,3,3]
for i=0,nout-1 do for j=0,nout-1 do begin
    temp = chk_map(i,j,*)
    ss = where(temp ne 0, nss)
    tit00 = tit + ' Sub Box ' + strtrim(i,2) + ',' + strtrim(j,2)
    if (nss ge 2) then plot, xarr(ss), temp(ss), psym=-2, /ynozero, tit=tit00, yrange=[imin,imax], xrange=[0,8], $
						xtit = 'Pointing Pos', ytit='Central Wave'
    if ((iplot mod 9) eq 8) then begin & plottime & plottime,0,0,progver & pause & end
    iplot = iplot + 1
end
;
!p.multi = 0
xarr = indgen(nout, nout)
plot, xarr, xarr, /nodata, yrange=[imin,imax], tit=tit, xtit='Sub-Boxes', ytit='Central Wavelength'
for i=0,8 do begin
    temp = chk_map(*,*,i)
    ss = where(temp ne 0)
    oplot, xarr(ss), temp(ss), linestyle=i
end
plottime & plottime,0,0,progver & pause
;
!p.multi = 0
xarr = indgen(nout, nout)
plot, xarr, xarr, /nodata, yrange=[-20,20], tit=tit, xtit='Sub-Boxes', ytit='Central Wavelength'
for i=0,8 do begin
    temp = chk_map(*,*,i)
    temp0 = chk_map(*,*,0)
    ss = where(temp ne 0)
    oplot, xarr(ss), temp(ss)-temp0(ss), linestyle=i
end
plottime & plottime,0,0,progver & pause
;
;----------------------- Sinusoidal plot of central wavelength relative to position 0
skip1:
;
!p.multi = 0
xarr = indgen(9)
imin = -40
imax = 40
plot, xarr, xarr, /nodata, tit=tit, yrange=[imin,imax], xrange=[0,8], xtit = 'Pointing Pos', $
	ytit='Central Wave (mA) - Position 0 (Average plotted offset by 10)'

avg_map = fltarr(9)
avg_cnt = fltarr(9)
;;for i=0,8 do begin & temp = chk_map(*,*,i) & ss = where(temp ne 0, nss) & avg_map(i) = total(temp(ss))/nss & end

iimin = 99999
iimax = -99999
for i=0,nout-1 do for j=0,nout-1 do begin
    temp = chk_map(i,j,*)
    ss = where(temp ne 0, nss)
    if (nss ge 2) then oplot, xarr(ss), temp(ss)-temp(0), psym=-2
    avg_map(ss) = avg_map(ss)+(temp(ss)-temp(0))
    avg_cnt(ss) = avg_cnt(ss) + 1
    iimin = min([iimin, temp(ss)-temp(0)])
    iimax = max([iimax, temp(ss)-temp(0)])
end
avg_map2 = avg_map/avg_cnt
oplot, xarr, avg_map2+10, thick=3
str1 = string(iimin, iimax, iimax-iimin, format='(3f10.2)')
str2 = string(min(avg_map2), max(avg_map2), max(avg_map2)-min(avg_map2), format='(3f10.2)')
plottime, 0.1, 0.20, 'Raw Min/Max/Del' + str1
plottime, 0.1, 0.17, 'Avg Min/Max/Del' + str2
plottime & plottime,0,0,progver & pause
;
!p.multi = 0
xx = ccdposout(1,*)
yy = avg_map2
plot, xx, yy, psym=-2, xtit='Y Relative position on CCD (-1 to 1)', ytit='Central Wave - Position 0 Average', tit=tit
coeff = poly_fit(xx, yy, 1, yfit)
corr = yfit
oplot, xx, yfit, thick=3
plottime, .45, .80, 'Coeff(0)='+string(coeff(0))
plottime, .45, .77, 'Coeff(1)='+string(coeff(1))
plottime & plottime,0,0,progver & pause
str = string(serial) + string(qm2) + string(coeff(0)) + string(coeff(1))
if (keyword_set(fit_result)) then file_append, fit_result, str
;
return
;
xx = pos(1,*)
plot, xx, yy, psym=-2, xtit='Laser Obs Derived Position in 16x16 pixels', ytit='Central Wave - Position 0 Average', tit=tit
coeff = poly_fit(xx, yy, 1, yfit)
oplot, xx, yfit, thick=3
plottime, .45, .80, 'Coeff(0)='+string(coeff(0))
plottime, .45, .77, 'Coeff(1)='+string(coeff(1))
plottime & plottime,0,0,progver & pause
;
xarr = indgen(9)
imin = -8
imax = 8
iplot = 0
!p.multi = [0,3,3]
for i=0,nout-1 do for j=0,nout-1 do begin
    temp = chk_map(i,j,*)
    ss = where(temp ne 0, nss)
    tit00 = tit + ' Sub Box ' + strtrim(i,2) + ',' + strtrim(j,2)
    if (nss eq 9) then begin
	plot, xarr(ss), temp(ss)-temp(0)-corr, psym=-2, tit=tit00, yrange=[imin,imax], xrange=[0,8], $
						xtit = 'Pointing Pos', ytit='Central Wave'
	if ((iplot mod 9) eq 8) then begin & plottime & plottime,0,0,progver & pause & end
	iplot = iplot + 1
    end
end
;
if (keyword_set(qstop)) then stop
if (keyword_set(hc)) then begin
    pprint
    set_plot, save_device
end

end
