pro mdi_comp_eff, data, out, limit, infil=infil, dset=dset
;+
;NAME:
;	mdi_comp_eff
;PURPOSE:
;	To simulate the compression efficiency of the MDI telemetry
;SAMPLE CALLING SEQUENCE:
;	mdi_comp_eff, img
;	mdi_comp_eff, img, out_struct, out_limit
;HISTORY:
;	Written 28-Mar-96 by M.Morrison
;-
;
if (keyword_set(infil)) then begin
    read_mdi, infil, dset, data, h, tfr=tfr
    dpc_str = string(sxpar(h, 'dpc'), format='(z8.8)')
    print, 'Data is for DPC ' + dpc_str + ' for ' + ccd_hdr_info(tfr, 'REFTIME_STR')
    print, 'Data is from ' + infil + '  Image #: ' + strtrim(dset,2)
    print, ' '
end
;
fd = deriv_arr(data)		;first differences
ndata = n_elements(fd)
;
p = histogram(fd) / float(ndata)
ss = where(p ne 0)
limit = - total( p(ss) * alog(p(ss)) ) / alog(2)
;
fd = abs(fd)
;
out = {k: 0, n0: 0L, n1: 0L, nsat: 0L, nbits: 0.0}
out = replicate(out, 10)
;
print,  '       K      #Diff      #Diff/2^K     #Diff       Avg#        #Sec in
print,  '    Value    < K bits     from 1-7   Saturated    of Bits     160K Telem
print, ' '
	;       6     1012468       36107           0      8.04788      51.5064
;
n = 14
for k=1,10 do begin
    fd2 = fd / (2L^k)		;portion above the K bits
    ss0 = where(fd2 eq 0, nss0)
    ss1 = where((fd2 le 7) and (fd2 gt 0), nss1)
    ss2 = where(fd2 gt 7, nss2)
    ;
    nbits = nss0*(k+1)
    if (nss1 ne 0) then nbits = nbits + total(fd2(ss1) + k+1)
    if (nss2 ne 0) then nbits = nbits + nss2 * (k+9 + (n-k))
    avg_nbits = nbits/float(ndata) + 1		;+1 for sign bit
    fmt = '(i8, 3i12, 2f12.3)'
    print, k, nss0, nss1, nss2, avg_nbits, avg_nbits*ndata/(160.*1024), format=fmt
    out(k-1).k		= k
    out(k-1).n0		= nss0
    out(k-1).n1		= nss1
    out(k-1).nsat	= nss2
    out(k-1).nbits	= avg_nbits
end
;
dummy = min(out.nbits, imin)
print, '>>>>>>>>> Best K for this data is: ', out(imin).k
print, '          Theoretical Limit is:    ', limit
end
