pro mk_ff_load, stlin, enlin, base_addr, infil=infil, outdir=outdir, $
			img=img, prefix=prefix, reflin=reflin
;+
;NAME:
;	mk_ff_load
;PURPOSE:
;	To make the source files for the flat field upload
;SAMPLE CALLING SEQUENCE:
;	mk_ff_load, 0, 15, infil=infil, outdir=outdir
;	mk_ff_load, 0, 15, infil=infil, outdir='.'
;	for i=0,1023,16 do mk_ff_load, i, i+15, img=img
;	for i=0,1023,16 do mk_ff_load, i, i+15, img=img, outdir='/mdisw_bbs/cfl/flat/src'
;
;	for i=   0,511,16 do mk_ff_load, i, i+15, '50080000'x+i*1024L, img=img, $	;bottom 512 lines in top of page 10
;					reflin=i
;	for i=512,1023,16 do mk_ff_load, i, i+15, '50080000'x+(i-512)*1024L, img=img, $	;top 512 lines in top of page 10
;					reflin=i
;
;	for i=0,1023,16 do mk_ff_load, i, i+15, img=img, outdir='.', infil='/mdisw/dbase/cal/files/flat_005.fits', prefix='ld_ff2_'
;	for i=0,1023,16 do mk_ff_load, i, i+15, img=img, outdir='.', $
;				infil='/mdisw/dbase/cal/files/flat_003.fits', prefix='ld_ff1_'
;HISTORY:
;	Written 11-Apr-96 by M.Morrison
;V2.0	17-Apr-96 (MDM) - Modified to not make the loads be cropped.
;V3.0	18-Apr-96 (MDM) - Modified to transfer every 4 lines to the IP
;			  (not every 2)
;			- Modified to use a different portion of the DEP
;			  memory
;V3.1	18-Apr-96 (MDM) - Modified the masking radius to be 520 pixels
;			- Modified to have portion outside of the mask be
;			  set to 1.0
;V4.0	14-May-96 (MDM) - Modified to write to page 12 instead of page 10
;			- Also modified to load one less word because of
;			  DEP/IP conflict.
;			- Added /NOWARN to mk_mdi_load call
;V5.0	21-Nov-96 (MDM) - Ver 4.0 was never put online
;			- Revert back to load into page 10
;			- Added base_addr parameter
;			- Removed the "one less word" patch since it 
;			  left things out of sync
;V5.1	 7-Jul-97 (MDM) - Added PREFIX option
;V5.2	 8-Jul-97 (MDM) - Changed default to be the new flat field
;V5.3	10-Jul-97 (MDM) - Corrected major mistake in that the IP destination
;			  address was not being updated properly
;			- Added REFLINE
;-
progver = 'MK_FF_LOAD  Ver 5.3'
;
;if (n_elements(infil) eq 0) then infil = '/mdisw/dbase/cal/files/flat_field.fits'
if (n_elements(infil) eq 0) then infil = '/mdisw/dbase/cal/files/flat_005.fits'
if (n_elements(outdir) eq 0) then outdir = '/mdisw/cfl/flat/src'
if (n_elements(base_addr) eq 0) then base_addr = '50000000'x
if (n_elements(prefix) eq 0) then prefix = 'ld_ff2_'
if (n_elements(reflin) eq 0) then reflin = 0
;
if (n_elements(img) eq 0) then begin
    img = rfits(infil)
    ss = circle_mask(img, 512, 512, 'GT', 520)
    img(ss) = 1.0
end
;
outfilnam = prefix + string(stlin, format='(i4.4)') + 'to' + string(enlin, format='(i4.4)') + '.src'
outfil = concat_dir(outdir, outfilnam)
print, 'Creating: ' + outfil
;
descr = 'Load flat field lines ' + strtrim(stlin,2) + ' to ' + strtrim(enlin,2)
comment = ['Load created from input file: ' + infil, $
	progver]
;
nlines = 4
qfirst = 1
depaddr = 0
for iline0=stlin, enlin, nlines do begin
	lines = img(*, iline0:(iline0+nlines-1)<enlin)
	lines = reform(lines, n_elements(lines))
	;addr = '50000000'x + 1024L*iline0
	;addr = '60000000'x + 1024L*iline0
	;addr = base_addr + 1024L*(iline0-stlin)
	addr = base_addr + 1024L*(iline0 - reflin)	;MDM 10-Jul-97
	lines = round(lines*16384L)  >0 <32767
	npad = 8 - (n_elements(lines) mod 8)
	if (npad ne 8) then lines = [lines, intarr(npad)+16384]

	;;lines = lines(0:n_elements(lines)-2)		;drop the last word so that IP is told N+1 which will end up
							;being an even multiple of 8.

        if (depaddr + n_elements(lines)*2 gt 65536) then depaddr = 0          ;start over from the beginning

	mk_mdi_load, 'gen_list', addr, outfil, lines, depaddr, $
			descr=descr, append=(1-qfirst), comment=comment, /nowarn
	qfirst = 0
        depaddr = depaddr + n_elements(lines)*2
end
;
end
