;+
; NAME:
;	WRT_BSDFIT_CFL
; PURPOSE:
;	Write control file for the BSDFIT F77 programme
; CALLING SEQUENCE:  
;	wrt_bsdfit_cfl,filename [,directory=directory] [,spectra=spectra]
; INPUTS:
;	filename	fileid of BSD file
; OPTIONAL INPUTS:
;	directory      	directory that BSD file is in
;	spectra        	spectra number within file to be used (default = all)
;	chans		channels to be processed (default = 2,3,4)
;	two_comp	if present, two component fit done
;
;       cfl_name        Name of CFL           (def = ppp.cfl)
;
; OUTPUTS:
;	None
; SIDE EFFECTS:
;       Produces a .CFL file on the current directory
;       Default name for this is "ppp.cfl"
; RESTRICTIONS:
;	Only some of the paramaters can be set - the rest are defaults
; PROCEDURE:
;	Uses inpuit to define some items. Rest are defaults.
;
; MODIFICATION HISTORY:
;	RDB  03-Jul-92
;	RDB  07-Jul-92	Translate env. variable $DIR_BCS_* in Unix
;	RDB  08-Jul-92	Added CHANS keyword
;	RDB  25-Jul-92	Version 2 of CFL
;	RDB  28-Oct-92  Added two_comp switch
;	RDB   6-Jun-93	Added cfl_name keyword option
;	RDB   7-Jun-93	Vesrion 3
;
;-
pro  wrt_bsdfit_cfl,filename,directory=directory,spectra=spectra, $
	chans=chans,two_comp=two_comp,debug=debug, $
	cfl_name=cfl_name


ver = 3			;7-Jun-93
dir = ''
atodat    = '$DIR_BCS_ATODAT:'
baldat    = '$DIR_BCS_BALDAT:'
overwrite = 'F'			;Ver 3.
if !version.os ne 'vms' then begin
   atodat = getenv('DIR_BCS_ATODAT') + '/'
   baldat = getenv('DIR_BCS_BALDAT') + '/'
   overwrite = 'T'
endif
process   = ['F','T','T','T']
range     = [0,0]
uncert    = 1
theofil   = 1
iprof     = 0
kmax      = 20
delchi    = 0.01
velcomp   = 1
usesec3   = 1
usesol    = [-1,-1,-1,-1]
lowsol    = [30,35,13,20]
hisol     = [220,217,112,115]
use_disp  = [-1,1,-1,-1]
disp      = [1.,3.65e-4,1.,1.]

break_file,filename,ddisk,ddir,filnam,ext
fileid = filnam+ext
if strlen(fileid) gt 11 and strmid(fileid,3,1) eq '9' then $
      fileid = strmid(fileid,3,11)      ;!!!!!!!!!

if keyword_set(directory) then dir = directory
if keyword_set(spectra)   then range = spectra
if keyword_set(chans) then begin
  process   = ['F','F','F','F']
  process(chans-1) = 'T'		;chans 1-4...
endif
if keyword_set(two_comp)  then velcomp = 3	;two component fit

if keyword_set(debug) then begin
   print,ver
   print,fileid
   print,dir
   print,atodat
   print,baldat
   print,process
   print,range
   print,uncert
   print,theofil
   print,iprof,kmax,delchi
   print,velcomp
   print,usesec3
   print,usesol
   print,lowsol
   print,hisol
   print,use_disp
   print,disp
   print,overwrite
endif

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

printf,lun,ver,         	format='(i2,t45,"Version No.  (IDL)")'
printf,lun,fileid,      	format='(a,t45,"File ID.")'
printf,lun,dir,                 format='(a,t45,"Directory of BSD file")'
printf,lun,atodat,		format='(a,t45,"Directory of Atomic Data")'
printf,lun,baldat,		format='(a,t45,"Directory of Ioniz. Balance Data")'
printf,lun,process,		format='(4a4,t45,"Which channels are prcessed (T=true)")'
printf,lun,range,               format='(2i5,t45,"Start /End Spectra")'
printf,lun,uncert,              format='(i2,t45,"Uncertainties")'
printf,lun,theofil,		format='(i2,t45,"Write theory spectra file")'
printf,lun,iprof,kmax,delchi,	format='(2i5,f9.2,t45,"IPROF, KMAX, DELCHI  (ignore, do not change)")'
printf,lun,velcomp,		format='(i2,t45,"Velocity component (0=single, 3=two-comp)")'
printf,lun,usesec3,		format='(i2,t45,"Use wavelength from SEC3.DAT files")'
printf,lun,usesol,		format='(4i5,t45,"Use limits below if > 0")'
printf,lun,lowsol,		format='(4i5,t45,"Lowest solar bin in channels 1,2,3,4")'
printf,lun,hisol,		format='(4i5,t45,"Highest solar bin in channels 1,2,3,4")'
printf,lun,use_disp,		format='(4i7,t45,"Use dispersion below if >0")'
printf,lun,disp,		format='(4e10.2,t45,"Dispersion to override any other val")'
printf,lun,overwrite,		format='(a4,t45,"Overwrite BTH/BFT files (F on VMS, T on UNIX)")'		;ver 3

free_lun,lun


print,'BSDFIT control file written: ',cfl_file

end
