pro sav_bsc, outfil, index, data, qs, dir=dir, append=append, skipmap=skipmap, $
        progverno=progverno, progname=progname, fhead_input=fhead_input
;
;+
;NAME:
;	sav_bsc
;PURPOSE:
;	To save an BCS processed spectra (BSC) index and data
;CALLING SEQUENCE:
;	sav_bsc, outfil, index, data
;	sav_bsc, 'hxi_15nov_flare', index, data, /append
;INPUT:
;	outfil	- The name of the file to create or append the
;		  the data.  NOTE: IDL "findfile" has a problem
;		  with filename that use the ~ to specify the
;		  directory.  This means that the /append option
;		  will not work with file name using ~.
;	index	- An array of index structures
;	data	- A BSC data structure.  It is possible to save only
;		  the index by not passing data, or by passing
;		  "byte(0)".  If data is passed, it should be an
;		  array the same length as index.
;OPTIONAL KEYWORD INPUT:
;	dir	- The output directory (option not to have to
;		  specify it in the filename)
;	append	- If set, append the dataset to an existing file
;		  If the file does not exist, it will create a new
;		  file.
;	skipmap	- If set, do not generate the roadmap for the file.
;		  This would be used when writing out a large number
;		  of datasets and using the /APPEND
;		  option.  The final call writing out the datasets
;		  will have to not have this switch set in order to
;		  have the roadmap created and the file be readable.
;       progname - The name of the program that is calling SAV_SDA
;       progverno - The version number of the program that is calling SAV_SDA
;       fhead_input - The file header of the input files to the program
;                 that is calling SAV_SDA so that the REFORMAT version
;                 number is saved in the output file header
;HISTORY:
;	Written 13-Nov-92 91 by M.Morrison
;	 16-Dec-92 (MDM) - Changed to break up BSC index into two parts
;	 		   GEN and BSC
;Ver 1.10  3-Feb-93 (MDM) - Added DATA.FLUX option
;Ver 1.11  8-Nov-93 (MDM) - Added .FLUX_FIT2
;Ver 1.12 26-Jan-94 (MDM) - Added /BLOCK switch to openu command
;-
;
progName1 = 'SAV_BSC.PRO'
progVerNo1 = 1.120*1000
progName2 = progName1
progVerNo2 = progVerNo1
;
if (keyword_set(progverno)) then progverno1 = progverno
if (keyword_set(progname)) then progname1 = progname
;
ref_struct, ref
rsiz = ref.BCS_Fil_Rec_Siz
;
break_file, outfil, dsk_log, dir0, filnam, ext
outfil0 = filnam + ext
if (not keyword_set(dir)) then dir = dir0
fileid = strmid(filnam, 3, 6) + ext
prefix = strupcase(strmid(filnam, 0, 3))
;
get_lun, lun
;
ff = findfile(dir+outfil0)
qexist = 0
if (ff(0) ne '') then qexist = 1
if (qexist and keyword_set(append)) then begin
    rd_pointer, dir+outfil0, fpoint
    rd_fheader, dir+outfil0, fhead
    bytout = fpoint.map_section		;start writting at end of datasection (overwrite roadmap - regenerate)
    openu, lun, dir+outfil0, /block
end else begin
    open_da_file, lun, dir, '', outfil0, rsiz, bytout, fpoint, qs
    gen_struct, file_header=fhead	;define "fhead" because if don't call "wrtBSCMap"
					;then it is undefined for "wrt1p_fh"
    if (keyword_set(fhead_input)) then begin
        fhead.refverno = fhead_input.refverno
        fhead.ref2verno = fhead_input.ref2verno
    end
end
;
qdata = (get_nbytes(data) gt 1)
;
n = n_elements(index)
for i=0,n-1 do begin
    index0 = index(i)
    if (qdata) then begin			;have true data to save
	data0 = data(i)		;data is a structure
	;
	for itag=0,n_tags(data0)-1 do begin
	    siz = size(data0.(itag))
	    nx0 = siz(1)
	    typ = siz( siz(0)+1 )
	    if (itag eq 0) then nx = nx0 else if (nx ne nx0) then begin
		help, /st, data0
		message, 'All data arrays in DATA structure must be same size'
	    end
	    if (typ ne 4) then begin
		help, /st, data0
		message, 'BSC DATA structures must be REAL*4 data type.  Stopping...'
	    end
	end
	ndatabyte = get_nbytes(data0)
	if ((ndatabyte mod rsiz) ne 0) then ndatabyte = ndatabyte + (rsiz - (ndatabyte mod rsiz))

	dataRecTypes = 0
	if (tag_index(data0, 'counts') ne -1)    then dataRecTypes = dataRecTypes + 1	;set bit 0
	if (tag_index(data0, 'bin') ne -1)       then dataRecTypes = dataRecTypes + 2	;set bit 1
	if (tag_index(data0, 'error') ne -1)     then dataRecTypes = dataRecTypes + 4	;set bit 2
	if (tag_index(data0, 'fit') ne -1)       then dataRecTypes = dataRecTypes + 8	;set bit 3
	if (tag_index(data0, 'wave') ne -1)      then dataRecTypes = dataRecTypes + 16	;set bit 4
	if (tag_index(data0, 'flux') ne -1)      then dataRecTypes = dataRecTypes + 32	;set bit 5
	if (tag_index(data0, 'wave_fit') ne -1)  then dataRecTypes = dataRecTypes + 64	;set bit 6
	if (tag_index(data0, 'flux_fit') ne -1)  then dataRecTypes = dataRecTypes + 128	;set bit 7
	if (tag_index(data0, 'flux_fit2') ne -1) then dataRecTypes = dataRecTypes + 256	;set bit 8
	bits, dataRecTypes, bitarr
	if (total(bitarr) ne n_tags(data0)) then begin
	    help, /st, data0
	    print, 'Valid struture tags are: COUNTS, BIN, ERROR, FIT, WAVE, FLUX, WAVE_FIT, FLUX_FIT, FLUX_FIT2'
	    message, 'Unrecognized data structure.  Stopping'
	end
    end else begin
	nx = 0
	typ = 0
	ndatabyte = 0
	dataRecTypes = 0
    end
    ;
    if (index0.bsc.length ne nx) then begin
	message, 'Number of bins as described in index (length) does not match data passed in', /info
	message, 'Index says' + string(index0.bsc.length, format='(i4)'), /info
	message, 'Data says ' + string(nx, format='(i4)'), /info
	message, 'Setting index to match data', /info

	index0.bsc.length	= nx
    end
    index0.gen.nIndexByte	= get_nbytes(index0)
    index0.gen.nDataByte	= ndatabyte
    ;;;index0.data_word_type	= typ
    index0.bsc.dataRecTypes	= dataRecTypes

    rdwrt, 'W', lun, bytout, rsiz, Index0, 1
    if (ndatabyte gt 0) then rdwrt, 'W', lun, bytout, rsiz, data0, 1
end
;
file_type = 'BSC'
;
fpoint.map_section = bytout
if (not keyword_set(skipmap)) then begin
    wrtBSCMap, lun, fpoint, bytout, fhead, rsiz
end
wrt1p_fh, lun, fpoint, fhead, rsiz, progName1, progVerNo1, fileid, file_type, 'BCS', progname2=progname2, progverno2=progverno2
;
free_lun, lun
end
