pro disp9embed, serial, qm2, hc=hc, sub10=sub10, qstop=qstop, correct=correct
;
;
;HISTORY:
;	Written Jan/Feb by M.Morrison
;V2.0	 8-Feb-94 (MDM) - Made use FITS files not IDL save files
;			- color bar label
;V2.1    9-Feb-94 (MDM) - Modified to not invert the color table when
;			  displaying.
;			- Modified to use linear color table, not
;			  HIST_EQUAL
;V3.0	18-Feb-94 (MDM) - CORRECTED EMBEDDING ERROR
;			  Modified to call EMBED9 with "qm2"
;			- Modified to use TV2 and XYOUTS2
;V3.01	23-Feb-94 (MDM) - Added /CORRECT option
;
;
progver = 'DISP9EMBED.PRO  Ver 3.01'
;
save_device = !d.name
save_color = !color
missing = 0
if (getenv('ys_batch') ne "") or (keyword_set(hc)) then set_plot, 'ps'
if (!d.name eq 'PS') then begin
    !color = 0
    missing=255
end
missing0 = -9999

embed9, serial, qm2, array, pos, out, out2, sub10=sub10, missing=missing0, correct=correct
;
m_arr = ['  M1', '  M2']
tit = 'Series: ' + strtrim(serial,2) + m_arr(qm2)
;
;
nout = 256
xc = cos(2*!pi*findgen(500)/499.)*33 * (nout/128) + nout/2
yc = sin(2*!pi*findgen(500)/499.)*33 * (nout/128) + nout/2
;
ss = where(out ne missing0)
imin = min(out(ss))
imax = max(out(ss))
idev = stdev(out(ss), iavg)
;
;;bimg = hist_equal(out)
;;bimg2 = hist_equal(out2)
bimg = bytscl(out, min=imin, max=imax)
bimg2 = bytscl(out2, min=imin, max=imax)
;
ss = where(out eq missing0) & if (ss(0) ne -1) then bimg(ss) = missing
ss = where(out2 eq missing0) & if (ss(0) ne -1) then bimg2(ss) = missing
;
case !d.name of
   'X': wdef, 1, nout*3, nout*4
   'Z': device, set_res=[nout*3, nout*4]
   'PS': tv2, nout*3, nout*4, /init
end
;
erase
for i=0,8 do begin
    x0 = (i mod 3)*nout
    y0 = (3-i/3)*nout
    tv2, rebin(bimg(*,*,i), nout, nout), x0, y0
    plots2, xc+x0, yc+y0, /dev, color=!color
end
tv2, rebin(bimg2, nout, nout, 9)
xyouts2, nout, 150, tit, siz=2, /dev
xyouts2, nout, 110, progver, /dev
xyouts2, nout,  90, 'Display Made: ' + !stime, /dev
if (keyword_set(correct)) then xyouts2, nout, 70, 'Angle Offset correction applied', /dev
;
mk_refbar, 2.5*nout, 0, imin, imax, /delta, average=iavg, dev=idev, fmt='(f8.3)'
;
!color = save_color
if (!d.name eq 'PS') then begin
    pprint
    set_plot, save_device
end
if (keyword_set(qstop)) then stop
end
