;+
; NAME:
;	WRT_MKBSD_CFL
;
; PURPOSE:
;	Creates .CFL file for F77 programme, MKBSD
;
; CALLING SEQUENCE:  
;	WRT_MKBSD_CFL,filnam,istart,istop
;
; INPUTS:
;	filnam		name of BDA file, including directory
;	istart		start time - 7 element array in external format
;	istop		stop time - 7 element array in external format
;
; KEYWORD INPUTS
;	integration	Integration time      (def = 24. sec)
;	nspectra	No. of Spectra        (def = 0)
;	chans		Channels              (def = 1,2,3,4)
;	count_level	Counter level         (def = 0)
;	count_chan	Counter channel       (def = 4)
;	offsets		Pointing offsets      (def = 0.,0.)
;	consistency	Pointing Consistency  (def = 0.1)
;	options		Programme options     (def = 1,1,0,0,0,0,0)
;
;	cfl_name	Name of CFL           (def = yyy.cfl)
;
;	debug		If set, prints input parameters
;
; OUTPUTS:
;	None
; SIDE EFFECTS:
;	Produces a .CFL file on the current directory
;	Default name for this is YYY.CFL
; RESTRICTIONS:
;	None

; PROCEDURE:
;	Creates .CFL file, suing defualts when parameter is not 
;	specified.
;
; MODIFICATION HISTORY:
;	RDB, 24-Mar-92
;	RDB, 05-Jun-92	Added CFL filename option
;	RDB  15-Dec-92	Version 4 of CFL file...
;	RDB   6-Jun-93	Added declaration of file written
;-

pro	wrt_mkbsd_cfl,fileid,istart,istop,  $
	  integration=integration,nspectra=nspectra,chans=chans,  $
          count_level=count_level,count_chan=count_chan,  $
	  offsets=offsets,consistency=consistency, $
	  options=options,debug=debug,cfl_name=cfl_name


;	version of the BSD CFL file
ver = 0.4		;????
;;name=''
;;ista=intarr(7)
;;istp=intarr(7)
;;int=0.

;	break file to fix wrongly coded fix by MKC
break_file,fileid,dsk,dir,filnam,ext

;	integration time
int = 24.
if keyword_set(integration) then int = integration

;	number of spectra, default to all ???
nspec=0
if keyword_set(nspectra) then nspec = nspectra


;	channels to analyse
if keyword_set(chans) then begin
  nchan = n_elements(chans)
endif else begin
  nchan=4
  chans = [1,2,3,4]
endelse


;	analysis options
cnt_lev=0
if keyword_set(count_level) then cnt_lev = count_level

cnt_chan=4
if keyword_set(count_chan) then cnt_chan = count_chan


;	pointing options
if keyword_set(offsets) then begin
  offset1 = 1
endif else begin
  offset1 = 0
  offsets = fltarr(2)
endelse

consist = 0.1 
if keyword_set(consistency) then consist = consistency


;	run options
if keyword_set(options) then begin
  nopt = n_elements(options)
endif else begin
  nopt = 7
  options = [1,1,0,0,0,0,0]
endelse

;	start and stop time (???)
ista = istart
istp = istop

if keyword_set(debug) then begin
  print,ver
  print,dsk+dir
  print,filnam+ext
  print,istart
  print,istop
  print,nspec
  print,nchan
  print,chans
  print,int
  print,cnt_lev
  print,cnt_chan
  print,offset1
  print,offsets
  print,consist
  print,nopt
  print,options
endif

;	default to YYY.CFL unless name supplied
cfl_file='yyy.cfl'
if keyword_set(cfl_name) then cfl_file = cfl_name
openw,lun,/get_lun,cfl_file

printf,lun,ver,		format='(f7.2,t55,"Version No.  (IDL)")'
printf,lun,dsk+dir,	format='(a,t55,"Directory of BDA File")'
printf,lun,filnam+ext,	format='(a,t55,"File ID.")'
printf,lun,ista,	format='(7i6,t55,"Start Time")'
printf,lun,istp,	format='(7i6,t55,"Stop  Time")'
printf,lun,nspec,	format='(i6,t55,"No. of Spectra")'
printf,lun,nchan,	format='(i6,t55,"No. of channels")'
printf,lun,chans,	format='('+string(nchan,'(i1)')+'i4,t55,"Channel No.s")'
printf,lun,int,		format='(f8.3,t55,"Integration Time")'
printf,lun,cnt_lev,	format='(i6,t55,"Count Level")'
printf,lun,cnt_chan,	format='(i6,t55,"Count Channel")'
printf,lun,offset1,	format='(i6,t55,"Flare Offset Entered")'
printf,lun,offsets,	format='(2e13.4,t55,"Flare Offsets")'
printf,lun,consist,	format='(f9.4,t55,"Pointing Consistency")'
printf,lun,nopt,	format='(i6,t55,"No. of Options")'
printf,lun,options,	format='('+string(nopt,'(i2)')+'i4,t55,"Options")'

free_lun,lun

print,'MKBSD control file written:  ',cfl_file

end
