pro check4append, infil, dir_out, prefix, fileid_out
;
;+
;NAME:
;	check4append
;PURPOSE:
;	If a dataset had to be appended during the WRT1ORBIT procedure, it
;	needs to be checked to see if the data is in time order.
;	Should only be needed for SFR and BDA files.
;INPUT:
;	infil	- The input file name to be checked
;HISTORY:
;	Written 13-Mar-92 by M.Morrison
;-
;
rd_roadmap, infil, rmap
x = int2secarr(rmap)
if (n_elements(x) gt 1) then dx = deriv_arr(x) else dx = 0
qoutta_order = (dx lt 0)		;or equal to - equal to means piece together two images - TODO
if (not qoutta_order) then return
;
rd_pointer, infil, fpoint
rd_fheader, infil, fhead
rsiz = fpoint.vms_rec_size
progverno = fhead.progverno	;use REFORMAT version number
;
rd_qs, infil, qs
;
if (strupcase(strmid(prefix,0,1)) eq 'B') then begin
    rd_bda_dp, infil, dp_sync_out
    ndp_sync_out = n_elements(dp_sync_out)
    if (get_nbytes(dp_sync_out(0) lt 5) then ndp_sync_out = 0
end
;
get_lun, lun
print, 'Opening a new file: ', infil + 'sort'
open_da_file, lun, dir_out, prefix, fileid_out+'sort', rsiz, bytout, fpoint, qs
wrtqs, lun, fpoint, bytout, qs
;
dset_arr = indgen(n_elements(rmap))
dset_arr0 = dset_arr
nloop = 1

rd_fheader, infil, fheader
if (fheader.maxSamps*fheader.nDataSets ge 40e+6) then begin
    nloop = n_elements(dset_arr)
end

for iloop=1,nloop do begin		;for large FFI datasets, do them image by image to avoid a memory dump problem

	;TODO - case where two images need to be merged

	if (nloop ne 1) then dset_arr0 = dset_arr(iloop-1)

	case strupcase(strmid(prefix,0,1)) of
	    'B': rd_bda, infil, dset_arr0, index, data, roadmap
	    'H': rd_hda, infil, dset_arr0, index, data, roadmap
	    'S': rd_sda, infil, dset_arr0, index, data, roadmap
	    'W': rd_wda, infil, dset_arr0, index, data, roadmap
	    'C': rd_cba, infil, dset_arr0, index, data, roadmap			;,data2
	    'A': rd_ada, infil, dset_arr0, index, data, roadmap, hxa_scan
	endcase
	;
	xxx = int2secarr(index)
	if (n_elements(xxx) gt 1) then ss = sort(xxx) else ss = 0
	;
	for ii=0,n_elements(dset_arr0)-1 do begin
	    i = ss(ii)	;adjust so that it is time ordered
	    index(i).gen.nIndexByte = get_nbytes(index(i))	;needed because RD routine sometimes expands on the structure - SHIT

	    rdwrt, 'W', lun, bytout, rsiz, index(i), 1

	    case strupcase(strmid(prefix,0,1)) of
            'B': begin
		n = index(i).bcs.length
		nn = index(i).gen.ndatabyte		;case where there is padded data to be able to 
							;stop on a 16 byte boundary
		data0 = data(0:n-1,i)
		if (n ne nn) then data0 = [data0, bytarr(nn-n)]
		rdwrt, 'W', lun, bytout, rsiz, data0, 1
	     end
            'H': begin
		if (index(i).gen.ndatabyte ne 0) then rdwrt, 'W', lun, bytout, rsiz, data(*,*,*,i), 1
		;only write out if in flare mode
	     end
            'S': begin
		nx = index(i).sxt.shape_sav(0)
		ny = index(i).sxt.shape_sav(1)
		rdwrt, 'W', lun, bytout, rsiz, data(0:nx-1,0:ny-1,i), 1
	     end
            'W': rdwrt, 'W', lun, bytout, rsiz, data(i), 1
            'C': begin
		rdwrt, 'W', lun, bytout, rsiz, data(i), 1
		;;rdwrt, 'W', lun, bytout, rsiz, data2(*,*,i), 1
	     end
            'A': rdwrt, 'W', lun, bytout, rsiz, data(i), 1
	    endcase
	end
end
;
case strupcase(strmid(prefix,0,1)) of
    'B': wrtBCSMap, lun, fpoint, bytout, fhead, rsiz, dp_sync_out, ndp_sync_out
    'H': wrtHXTMap, lun, fpoint, bytout, fhead, rsiz  
    'S': wrtSXTMap, lun, fpoint, bytout, fhead, rsiz
    'W': wrtWBSMap, lun, fpoint, bytout, fhead, rsiz  
    'C': wrtCBAMap, lun, fpoint, bytout, fhead, rsiz
    'A': wrtATTMap, lun, fpoint, bytout, fhead, rsiz, hxa_scan, ihxa_scan
endcase
;
if (keyword_set(orbit_times)) then begin
    fhead.orb_st_time = orbit_times.orb_st_time
    fhead.orb_st_day  = orbit_times.orb_st_day
    fhead.orb_en_time = orbit_times.orb_en_time
    fhead.orb_en_day  = orbit_times.orb_en_day
end
wrt1p_fh, lun, fpoint, fhead, rsiz, progName, progVerNo, fileid_out, prefix, instr
;
free_lun, lun
;
spawn, 'rm -f ' + infil
spawn, 'mv ' + infil+'sort  ' + infil
end
