pro raw_list2pixmap, infil, outdir=outdir, output=out, display=display, $
	table_start=table_start, rowlen=rowlen
;+
;NAME:
;	raw_list2pixmap,
;PURPOSE:
;	To convert the raw list definition into pixel map format
;SAMPLE CALLING SEQUENCE:
;	raw_list2pixmap, '/mdisw/dbase/ops/lists/list_crop.fits'
;	raw_list2pixmap, '/mdisw/dbase/ops/lists/list_limb.fits', /display
;	raw_list2pixmap, '/mdisw/dbase/ops/lists/loi3b.fits', /display, output=out
;	raw_list2pixmap, '/mdisw/dbase/ops/lists/list_vwbin.fits', /display, output=out
;HISTORY:
;	Written 8-Sep-95 by M.Morrison
;	Ver 2.0 12-Sep-95 (MDM) - Modified to save the data as LONARR rather than FLOAT
;				  and did not save as 3-D since the weighting is all 100%
;	Ver 3.0  6-Oct-95 (MDM) - Modified to work properly when the list does not have
;				  any padding at the end
;				- Modified to save number of raw CCD pixels put into
;				  a single IRBIN bin
;	Ver 3.1	 6-Nov-95 (MDM) - Added TABLE_START keyword input
;	Ver 3.2	13-Mar-96 (MDM) - Modified to use an offset of 5 for VWBIN (instead of 6)
;	Ver 4.0	23-May-96 (MDM) - Modified to make the normalization value take the
;				  scale and shift factors (in addition to the number of
;				  pixels
;	Ver 4.1	 3-Nov-97 (MDM) - Added ROWLEN (used only in CROP)
;-
;
progver = 'Ver 4.1
;
if (n_elements(rowlen) eq 0) then rowlen = 1024
;
swap_os=['vms','ultrix','OSF']                  ; list of swap machines
chk=where(!version.os eq swap_os,nscount)       ; current OS in noswap list?
qswap = nscount eq 0                            ; assign boolean
;
if (not file_exist(infil)) then begin
    message, /info, infil + ' does not exist
    return
end
;
break_file, infil, dsk_log, dir, filnam
if (n_elements(outdir) eq 0) then outdir = dir
outfil = concat_dir(outdir, filnam + '.fits')
outfil = str_replace(outfil, 'list_', 'pixmap_')
if (strpos(outfil, 'pixmap_') eq -1) then outfil = concat_dir(outdir, 'pixmap_' + filnam + '.fits')
;
list = rfits(infil)
list = unsign(list, 16)
nximg = list(1)
nyimg = list(1)
;
case list(0) of
    414: begin          ;vwbin instruction
	;;if (n_elements(table_start) eq 0) then table_start = '7000'x
	if (n_elements(table_start) eq 0) then table_start = '40000'x		;MDM 13-Mar-96

        n = list(2)
        ss = indgen(n)
	nn = 10		;10 words per entry
        xvaddr = list(ss*nn+16+1) * '10000'x  + list(ss*nn+16+0)	- table_start
        nxbin = list(ss*nn+16+2)
        yvaddr = list(ss*nn+16+4) * '10000'x  + list(ss*nn+16+3)	- table_start
        nybin = list(ss*nn+16+5)
        addr = list(ss*nn+16+7) * '10000'x  + list(ss*nn+16+6)		- table_start
        nbin = list(ss*nn+16+8)
        shift = list(ss*nn+16+9)

	ntot = total(nbin)
	pix_addr = lonarr(ntot)
	ist = 0
	for i=0,n-1 do begin
	    ss = indgen(nbin(i))
	    pix_addr(ist) = list( addr(i)+ss*2+1 ) * '10000'x + list( addr(i)+ss*2 ) + $
				nxbin(i)/2 + nybin(i)/2*1024	;move to the center of the bin
	    ist = ist + nbin(i)
	end

;;	img = bytarr(1024,1024)
;;	img(pix_addr) = 255
;;	tvscl, rebin(img, 512,512)

;	xmat = pix_addr mod 1024
;	ymat = pix_addr / 1024
;	box_size = intarr(ntot)
;	for i=0,ntot-1 do begin
;	    diff = sqrt( (xmat-xmat(i))^2 + (ymat-ymat(i))^2 )
;	    ss = where(diff ne 0)
;	    box_size(i) = min(diff(ss))			;size of the full side of the non-overlapping box
;	end
;;;	box_size = intarr(ntot) + 6
	box_size = intarr(ntot) + 5		;MDM changed 13-Mar-96 (for xxxx6003)

	img = bytarr(1024,1024)
	ntot2 = total( box_size^2 )
	out = lonarr(ntot2, 2)
	ist = 0L
	for i=0L,ntot-1 do begin
	    n2 = box_size(i)^2
	    half_side = fix(box_size(i)/2)
	    ss = indgen(n2)
	    out(ist:ist+n2-1,0) = i
	    out(ist:ist+n2-1,1) = pix_addr(i)  + (ss mod box_size(i))-half_side + (ss/box_size(i) - half_side)*1024L
	    ist = ist + n2
	end


;;	img( out(*,1) ) = out(*,0)
;;	print,img(500:515,500:515)
;;	tvscl, rebin(img, 512, 512)

         end
    416: begin          ;rwbin instruction
	if (n_elements(table_start) eq 0) then table_start = '11000'x


        n = list(2)
        ss = indgen(n)
	nn = 7		; words per entry
        addr = list(ss*nn+16+1) * '10000'x  + list(ss*nn+16+0)		- table_start
        nbin = list(ss*nn+16+2)
        nxbin = list(ss*nn+16+3)
        nybin = list(ss*nn+16+4)
        shift = list(ss*nn+16+5)
        scale = list(ss*nn+16+6)

	ntot = total(nbin)
	pix_addr = lonarr(ntot)
	ist = 0
	for i=0,n-1 do begin
	    ss = indgen(nbin(i))
	    pix_addr(ist) = list( addr(i)+ss*2+1 ) * '10000'x + list( addr(i)+ss*2 ) + $
				nxbin(i)/2 + nybin(i)/2*1024	;move to the center of the bin
	    ist = ist + nbin(i)
	end

;;	img = bytarr(1024,1024)
;;	img(pix_addr) = 255
;;	tvscl, rebin(img, 512,512)

	box_size = intarr(ntot) + nxbin(0)		;TODO - fix up

;;	img = bytarr(1024,1024)
	ntot2 = total( box_size^2 )
	out = lonarr(ntot2, 2)
	ist = 0L
	for i=0L,ntot-1 do begin
	    n2 = box_size(i)^2
	    half_side = fix(box_size(i)/2)
	    ss = indgen(n2)
	    out(ist:ist+n2-1,0) = i
	    out(ist:ist+n2-1,1) = pix_addr(i)  + (ss mod box_size(i))-half_side + (ss/box_size(i) - half_side)*1024L
	    ist = ist + n2
	end


;;	img( out(*,1) ) = out(*,0)
;;	print,img(500:515,500:515)
;;	tvscl, rebin(img, 512, 512)

         end
    420: begin		;irbin
	n = list(3)>list(2)
	ss = indgen(n)
	addr = list(ss*5+16+1) * '10000'x  + list(ss*5+16+0)
	nbin = list(ss*5+16+2)
	xlen = list(ss*5+16+3)
	ylen = list(ss*5+16+4)
	;
	;out = lonarr(1024*1024L, 2)
	out = lonarr(1024*1024L, 3)
	nraw_pix = 0L
	ndp_pix = 0
	for i=0,n-1 do begin
	    nbin0 = nbin(i)
	    for j=0,nbin0-1 do begin
		    addr0 = addr(i)
		    ist = list(addr0+1+j*6) * '10000'x + list(addr0+j*6)	;data address
		    x0 = ist mod nximg
		    y0 = ist / nyimg
		    ;
		    ;                Shift Value     Bits
		    ;                hex     Dec
		    ;                --------------  -----
		    ;                0000    0       32-47
		    ;                0001    1       31-46
		    ;                ...
		    ;                000E    14      18-33
		    ;                000F    15      17-32
		    ;
		    ;                8000    32768   16-31
 		    ;                8001    32769   15-30
		    ;                ...
		    ;                800E    32782    2-17
		    ;                800F    32783    1-16
		    ;                8010(*) 32784    0-15 (OR) 16-31 ??
		    ;
		    ; dp_value = total(  ccd_pixels * scale_v * scale_shift )
		    ; avg_ccd_pixel = dp_value / npixels / scale_v / scale_shift
		    ;
		    shift_v = list(addr0+4+j*6)
		    scale_v = list(addr0+5+j*6)
		    shift_v = unsign(shift_v, 16)
		    scale_shift = 1.0
		    if (shift_v gt 32768) then scale_shift = 1.0 / 2.^(shift_v-32768)	;normalize to value '8000'x
		    if (shift_v lt 32768) then scale_shift = 2.^(16-shift_v)
		    ;
		    skip_take_addr = list(addr0+3+j*6) * '10000'x + list(addr0+2+j*6)
		    xlen0 = xlen(i)
		    ylen0 = ylen(i)
		    skip_take = fix(list(skip_take_addr:skip_take_addr+ylen0-1))
		    if (qswap) then dec2sun, skip_take
		    skip_take = byte(fix(skip_take), 0, 2, ylen0)
		    ;pimg = img(x0:x0+xlen0-1, y0:y0+ylen0-1)		;grab what is there because of box overlap
		    pimg = bytarr(xlen0, ylen0)
		    off = 0
		    for k=0,ylen0-1 do begin
			off = off + skip_take(0,k)	;move forward the skip number
			ix0 = off
			ix1 = off + skip_take(1,k)-1
			off = off + skip_take(1,k)	;move forward the take number
			if (skip_take(1,k) ne 0) then pimg( ix0:ix1 ) = 1
			if (keyword_set(qprint)) then print, j, k, skip_take(0,k), skip_take(1,k), off, ix0, ix1, xlen0, ylen0, xlen0*ylen0
		    end
		    ;
		    ss = where(pimg ne 0, nss)
		    ien = nraw_pix + nss-1
		    out(nraw_pix:ien, 0) = ndp_pix
		    out(nraw_pix:ien, 1) = reform(y0*nximg + (ss/xlen0)*nximg + (ss mod xlen0)+x0, nss)
		    ;;out(nraw_pix:ien, 2) = 100.00
		    out(nraw_pix:ien, 2) = nss * scale_v * scale_shift
		    ndp_pix = ndp_pix + 1
		    nraw_pix = nraw_pix + nss
	    end
	end
	out = out(0:nraw_pix-1, *)		;trim it back to the pixels used
     end
    376: begin		;crop or limb
		n = list(2)
		ss = indgen(n)
		;
		addr = list(ss*3+16+1) * '10000'x  + list(ss*3+16+0)
		narr = list(ss*3+16+2) + 1	;MDM added +1 19-Sep-95
		ndp_pix = total(narr)
		out = lonarr(ndp_pix,2)
		out(*,0) = lindgen(ndp_pix)		;data product pixel number (one to one mapping)
		;;out(*,2) = 100.00			;100% contribution into that pixel
		;
		ist = 0
		for i=0,n-1 do begin
		    n0 = narr(i)
		    ss2 = indgen(n0) + addr(i)
		    out(ist:ist+n0-1, 1) = ss2
		    ist = ist + n0
		end
	end
    else: begin
		print, 'RAW_LIST2PIXMAP: Sorry - this opcode is not encoded yet. 
		return
	end

endcase
;
head = [mk_fits_head(out), 'END                                                                             ']
sxaddpar, head, 'INFILE', infil
sxaddpar, head, 'CREATED', !stime
sxaddpar, head, 'PROGRAM', 'RAW_LIST2PIXMAP'
sxaddpar, head, 'PROG_VER', progver
wrt_fits, outfil, head, out
;
if (keyword_set(display)) then begin
    bimg = bytarr(rowlen,rowlen)
    bimg( out(*,1) ) = out(*,0)
    bimg = rebin(bimg, 512, 512)
    tvscl, bimg
end
;
end
;----------------------------
pro all_raw_list2pixmap
;
;
;
raw_list2pixmap, '/mdisw/dbase/ops/lists/list_crop.fits', /display
raw_list2pixmap, '/mdisw/dbase/ops/lists/list_limb.fits', /display
raw_list2pixmap, '/mdisw/dbase/ops/lists/list_loi3b.fits', /display, output=out
raw_list2pixmap, '/mdisw/dbase/ops/lists/list_vwbin.fits', /display, output=out
raw_list2pixmap, '/mdisw/dbase/ops/lists/list_rwbin.fits', /display, output=out
;
end
