pro fits_sci5k, sttim, entim, indir=indir, base_dir=base_dir0, $
			log_dir=log_dir, exe_dir=exe_dir, tmp_dir=tmp_dir, $
			lparl=lparl, sim=sim, nodelete=nodelete
;
;NAME:
;	fits_sci5k
;PURPOSE:
;	A front end to the temporary FITS file making from the SCI5K data
;	files.  
;SAMPLE CALLING SEQUENCE:
;	fits_sci5k, '13-Feb-96   14:00' , '13-Feb-96  18:00'
;	fits_sci5k, '13-Feb-96'
;RESTRICTION:
;	The calling time cannot span days (cannot cross the midnight boundary)
;HISTORY:
;	Written 25-Sep-95 by M.Morrison
;	13-Oct-95 (MDM) - Changed to point to /mdisw/dbase/cal/info for the
;			  DPC table by default
;			- Modified to point to file "dpc_table.txt" instead of
;			  "dpc_table"
;	 8-Nov-95 (MDM) - Modified to determine the DPC list to process by 
;			  looking at the raw index rather than from a fixed list.
;V2.0	13-Feb-96 (MDM) - Modified significantly to work on MDISAS and to use the new
;			    capabilities.
;V2.1	19-Feb-96 (MDM) - Added /LPARL option
;V2.2	14-Mar-96 (MDM) - Added "sim" option
;			- Added argument to the extract_dp map file (in_badpkt=)
;V2.3	 4-Apr-96 (MDM) - Adjusted the concatenation command used to make the
;			  temporary input file for SIM
;V2.4	17-Jun-96 (MDM) - MOdified the default executable directory
;			  (from /big3 and /md84 to /mdisw/STAGING)
;			- Delete the temp files and raw files created
;V2.5	29-Aug-96 (MDM) - V2.4 was never put on-line
;			- Incorporated changes "badpkts" now "misspkts"
;V2.6	30-Sep-96 (MDM) - Changed the SIM option functionality (now it is
;			  more like operations)
;V2.7	 2-Oct-96 (MDM) - Modified to use RD_SCI5K
;V2.71	16-Oct-96 (MDM) - Modified /lparl option to use /data13 not /small1
;	 2-Apr-97 (MDM) - Added fixed directory for SIM option
;V2.8	22-Nov-99 (RIB) - Changed /data13/data_eof/fits and /md84/fits 
;                         to $MDI_FITS_DIR
;V2.81	22-Dec-99 (RIB) - Removed portions of code that were commented out
;

fits_dir = getenv('MDI_FITS_DIR')
if (n_elements(tmp_dir) eq 0) then tmp_dir = concat_dir(fits_dir,'raw_tmp/')
if (n_elements(base_dir0) eq 0) then base_dir0 = concat_dir(fits_dir,'sci5k/')
if (n_elements(log_dir) eq 0) then log_dir = concat_dir(fits_dir,'logs/')
if (n_elements(exe_dir) eq 0) then exe_dir = '/mdisw/STAGING/bin/_sgi4'		;TODO - move to /mdisw/exe/CM/_sgi4
if (n_elements(table_dir) eq 0) then table_dir = getenv('MDI_CAL_INFO')
;
if (n_elements(sttim) eq 0) then begin
    sttim = anytim2ints(ut_time(), off=-1*60.*60)	;do the last hour
    entim = ut_time()
end
if (n_elements(entim) eq 0) then entim = anytim2ints(sttim, off=2*60.*60.)
;
tarr = anytim2ex(sttim)
dofm = long( int2secarr(tarr, '1-jan-93')/86400 )	;day of mission   1119 == 25-Jan-96
dofm_str = strtrim(dofm,2)
dofm_str2 = '[' + dofm_str + ']'
;
outfilnam = 'MDI_LRSC.' + dofm_str
tmp_outfil = concat_dir(tmp_dir, outfilnam)
print, 'Output file: ', tmp_outfil
;
;----------- Copy the data over
;
out = rd_sci5k(sttim, entim)
if (data_type(out) ne 8) then begin
    message, /info, 'No data was found.  Returning...'
    return
end
;
openw, lun, tmp_outfil, /get_lun            
for i=0L,n_elements(out)-1 do begin
    writeu, lun, bytarr(16)             ;EGSE header
    writeu, lun, bytarr(4)              ;tm flags
    writeu, lun, bytarr(4)              ;functional header
    writeu, lun, bytarr(2)              ;request ID
    writeu, lun, out(i)
end
free_lun, lun
;
;----------- Environment var setup
;
setenv,'ARCH=SGI4
setenv,'MACHINE=sgi4
;
;----------- Run the index task
;
paramfile = concat_dir(log_dir, 'index_sci5k.txt')
setenv,'mdi_sim=' + base_dir0 + '/{level}'
setenv,'PARAMFILE=' + paramfile
;
openw, lun, paramfile, /get_lun
printf, lun, 'in=' + tmp_dir
printf, lun, 'out=prog:mdi_sim,level:raw,series:sci5k[' + dofm_str + '] \'
printf, lun, 'format=Lockheed'
free_lun, lun
;
cmd = concat_dir(exe_dir, 'index_sci5k') + ' > ' + concat_dir(log_dir,  'index_sci5k.log')
print, cmd
spawn, cmd
;
;----------- Run the DP extractor
;
index_file = (file_list( concat_dir(base_dir0, 'raw'), 'MDI_LRSC.' + dofm_str +'.index'))(0)
if (index_file eq '') then begin
    print, 'No index file.  Ingest didnt work or there is no data.  Returning'
		stop
    return
end
mat = rd_tfile(index_file, 7)
if (n_elements(mat(0,*)) le 2) then begin
    print, 'No DPC index list??
    return
end
dpc=mat(2,2:*)		;drop the first two lines
dpc_list = dpc(uniq(dpc,sort(dpc)))
print, 'DPC_List to process: ', dpc_list
;
setenv,'lkhd_sci5k_raw=' + concat_dir(base_dir0, '{level}')
setenv,'lkhd_sci5k_lev0=' + concat_dir(base_dir0, '{level}/{series}')
paramfile = concat_dir(log_dir, 'extract_dp_sci5k.txt')
setenv,'PARAMFILE=' + paramfile
for i=0,n_elements(dpc_list)-1 do begin
    outdir = concat_dir(base_dir0,  'lev0/' + dpc_list(i))
    if (not file_exist(outdir)) then spawn, ['mkdir', outdir], /noshell
    openw, lun, paramfile, /get_lun
    printf, lun,'in=prog:lkhd_sci5k_raw,level:raw,series:sci5k' + dofm_str2 + ',sel:MDI_LRSC.'+dofm_str + '.segs'
    printf, lun,'in_index=prog:lkhd_sci5k_raw,level:raw,series:sci5k'+ dofm_str2 + ',sel:MDI_LRSC.'+dofm_str + '.index'
    printf, lun,'in_misspkt=prog:lkhd_sci5k_raw,level:raw,series:sci5k'+ dofm_str2 + ',sel:MDI_LRSC.'+dofm_str +'.misspkt.index'
    printf, lun, 'out=prog:lkhd_sci5k_lev0,level:lev0,series:' + dpc_list(i) + dofm_str2 
    printf, lun, 'dpc='+dpc_list(i)
    printf, lun, 'epoch=1993.01.01_00:00:00_TAI
    printf, lun, 'dpc_table=' + concat_dir(table_dir, 'dpc_table.txt')
    printf, lun, 'eof=yes'
    free_lun, lun

    cmd = concat_dir(exe_dir, 'extract_dp_sci5k') + '  > ' + concat_dir(log_dir,  'extract_dp_sci5k.' + dpc_list(i) + '.log')
    print, 'Now executing: ' + cmd
    spawn, cmd
end
;
if (not keyword_set(nodelete)) then begin
    spawn, 'rm -f ' + concat_dir(tmp_dir, '*')
    spawn, 'rm -f ' + concat_dir(base_dir0, 'raw/*')
end
;
end
