;+
; NAME:
;	BCSPRO
; PURPOSE:
;	Runs the BCS F77 Programmes MKBSD, BSDCAL and BSDFIT
; CALLING SEQUENCE:  
;	.run BCSPRO
; INPUTS:
;	filename	Filename of dataset to be analysed by MKBSD.
;			- this should include the path.
;			  Default is name of current TEST_RD file
;	directory	Where the data is to be analysed (BSDCAL and BSDFIT only)
;			  Default is the current directory
;	start_time	Start time to be used in MKBSD run 
;			  Default is LH cursor from TEST_RD, option -44x
;	stop_time	Stop time to be used in MKBSD run
;			  Default is RH cursor from TEST_RD, option -44x
;	int_tim		Integration time in seconds
;			  Default is 24 seconds
;	chans		Channels to be analysed
;			  Default is 1,2,3,4
;	two_comp	Whether two component fit should be done
;			  Default is not
; OUTPUTS:
;	Creates the .cfl files for MKBSD, BSDCAL and BSDFIT on current directory
; OPTIONAL OUTPUTS:
;
; RESTRICTIONS:
;
; PROCEDURE:
;	Creates a .CFL file using the supplied filename, and start and
;	stop time. Spawns a command file to execute the required tasks.
; MODIFICATION HISTORY:
;	24-Mar-92  RDB
;	26-Mar-92  RDB	Coded UNIX part (not tested)
;	 3-Apr-92  RDB	Corrections to VMS part (/USER stuff)
;	 5-Jun-92  RDB	Corrected read of channel no's
;			Option to run wthout creating CFL
;			Corrected UNIX part - USED $DIR_BCS_EXE
;	16-Jun-92  RDB  Only initialize ITIME array if not set
;	03-Jul-92  RDB  Expended routine and renamed BCSPRO
;			Now runs all the F77 tasks.
;	06-Jul-92  RDB	Changes to filename and default directory used
;	25-Jul-92  RDB	Correct erro in UNIX part
;	28-Oct-92  RDB	Added two component option
;			Some change of blocking of code
;	12-Mar-93  RDB	Improved search for file (curr dir + exact dir)
;			Added message if not default times
;	 6-Jun-93  RDB	Corrected call to wrt_mkbsd_cfl so used ffil(0)
;			Changed all 3 wrt_*_cfl's to use supplied filename
;			New .cfl files are created with names of the form 
;			'xxx911115_2135.cfl'
;	27-Oct-93  RDB	All run_mkbsd scripts combined and spawn modified
;	29-Oct-93  RDB	Put in warning for use on MIPS
;	19-nov-93  JRL  Fixed the warning 
;-

yesnox,'* Create CFL files',ans_cfl

if !version.os eq 'RISC/os' then begin
  bell
  print,'  ***** WARNING  WARNING  WARNING  WARNING *****'
  print,' '
  print,'The BSDFIT F77 programme does not work on the Mips'
  print,' '
  print,'  ***** WARNING  WARNING  WARNING  WARNING *****'
  print,' '
endif

if ans_cfl then begin

print,' '
yesnox,'* Control file for MKBSD  ',ans_mkbsd
yesnox,'* Control file for BSDCAL ',ans_bsdcal
yesnox,'* Control file for BSDFIT ',ans_bsdfit
print,' '

;		get filename
filename = ''
if n_elements(infil) eq 0 then infil = ''
break_file,infil(0),ddisk,ddir,dfilnam,dext
dfileid = dfilnam + dext
print,'Default file is: ',dfileid
get_file:
read,'* Enter filename: ',filename
if strlen(filename) eq 0 then filename = dfileid

;		verify that the file exists before continuing
if ans_mkbsd then begin
  spath = [data_paths(),curdir()]		;data dirs + current dir
  break_file,filename,ddisk,ddir,dfilnam,dext
  if strlen(ddisk+ddir) ne 0 then begin		;exact dir specified
     spath = ddisk+ddir
     filename = dfilnam+dext
  endif
  ffil = file_list(spath, filename) 
  if strlen(ffil(0)) eq 0 then begin
     print,'*** File not found: ',filename
     goto,get_file
  endif else print,'  File has been found: ',ffil
  break_file,ffil(0),ddisk,ddir,dfilnam,dext
endif			;ans_mkbsd


;		get work directory
directory = ''
cd,curr=curr
if !version.os ne 'vms' then curr = curr + '/'	;add / to terminate directory
print,' '
print,'Default (current) directory for files is:  ',curr

ggfil = findfile('*'+dext)
if strlen(ggfil(0)) gt 0 then begin
   print,'The following files with ID  *'+dext+'  exist on this directory:'
   print,ggfil
endif

read,'* Enter Directory for file: ',directory
if strlen(directory) eq 0 then begin
  if !version.os eq 'vms' then directory = '[]' $		;curr $
  else directory = './'
endif


;	check if required times are in file ????
;;rd_fheader,filename,fheader,ndset
;; fheader.first_time,fheader.first_day
;; fheader.last_time,fheader.last_day

if ans_mkbsd then begin

;		get start and stop times
tstart = ''
tstop = ''

if n_elements(plotr) eq 2 then begin
   plotrr = [min(ssss),max(ssss)]
   int2ex,roadmap(plotrr).time,roadmap(plotrr).day,itime
endif else begin
   print,' '
   print,'** No default times found from YODAT **'
endelse

if n_elements(itime) eq 0 then itime = [[0,0,0,0,1,1,92],[0,30,0,0,1,1,92]]

print,' '
read,'* Enter Start Time  [def= "',fmt_tim(itime(*,0)),'"]: ',tstart
if strlen(tstart) eq 0 then  $
istart = itime(*,0)  $
else istart = timstr2ex(tstart)
;	default to date of file if none specified
for j=4,6 do if istart(j) eq 0 then istart(j) = itime(j,0)


read,'* Enter Stop Time   [def= "',fmt_tim(itime(*,1)),'"]: ',tstop
if strlen(tstop) eq 0 then  $
istop = itime(*,1)  $
else istop  = timstr2ex(tstop)
;	default to same date as start if none specified
for j=4,6 do if istop(j) eq 0 then istop(j) = istart(j)


;		get integration time
tint = ''
read,'* Enter Integration time  [def = "24.0"]: ',tint
if tint eq '' then  int_tim = 24.0  $
else  int_tim = float(tint)

endif		;ans_mkbsd


;		get channel numbers
tchan = ''
print,'** It is recomended to use all channels for the moment **'
read,'* Enter Channels       [def = "1,2,3,4"]: ',tchan
if tchan eq '' then  chans = 0  $
else chans = fix(str2arr(tchan))


;		get two component preference
if ans_bsdcal or ans_bsdfit then  $
	yesnox,'* Do two component spectral fit',two_comp

print,' '
print,' '

;	root .cfl name is 'yymmdd_hhmm.cfl'
cfl_root = strmid(dfilnam,3,6)+'_'+strmid(dext,1,4)+'.cfl'

cfl_file1 = 'mk' + cfl_root		;'yyy.cfl'
cfl_file2 = 'cal' + cfl_root		;'qqq.cfl'
cfl_file3 = 'fit' + cfl_root		;'ppp.cfl'


if ans_mkbsd  then begin
wrt_mkbsd_cfl,ffil(0),istart,istop,debug=debug,integration=int_tim, $
	chans=chans,cfl_name=cfl_file1
endif

if ans_bsdcal then begin
wrt_bsdcal_cfl,filename,directory=directory,chans=chans,  $
	two_comp=two_comp,debug=debug,cfl_name=cfl_file2
endif

if ans_bsdfit then begin
wrt_bsdfit_cfl,filename,directory=directory,chans=chans,  $
	two_comp=two_comp,debug=debug,cfl_name=cfl_file3
endif

endif else begin

;	not create .cfl files, could be run tasks, thus need .cfl names
if n_elements(cfl_root) eq 0 then cfl_root = ''
cfl_rootss = cfl_root
read,'* Enter name of existing CFL file [def="*'+cfl_root+'"]: ',cfl_root
if cfl_root eq "" then cfl_root = cfl_rootss

;	default to old file names if there is no root
if cfl_root ne "" then begin
   cfl_file1 = 'mk' + cfl_root		;'yyy.cfl'
   cfl_file2 = 'cal' + cfl_root		;'qqq.cfl'
   cfl_file3 = 'fit' + cfl_root		;'ppp.cfl'
endif else begin
   cfl_file1 = 'yyy.cfl'
   cfl_file2 = 'qqq.cfl'
   cfl_file3 = 'ppp.cfl'
   print,'** Defaulting to old style .cfl filenames **'
endelse

endelse


print,' '
print,' '
yesnox,'* Execute BCSPRO tasks:',ans
print,' '


if ans then begin

yesnox,'* Run MKBSD ',ans_mkbsd
yesnox,'* Run BSCAL ',ans_bsdcal
yesnox,'* Run BSDFIT',ans_bsdfit
print,' '

com_file = 'jjxxyy.com'

case !version.os of
'vms': begin

         openw,lun,/get_lun,com_file
         printf,lun,'$!
         printf,lun,'$!	Command file to execute BCS F77 programmes from IDL
         printf,lun,'$!
         printf,lun,'$!
         printf,lun,'$!   If IDL run from command file, fails unless you have this
         printf,lun,'$ if f$trnlnm("sys$input") .eqs. "SYS$COMMAND" then deass/user sys$input
         printf,lun,'$!
         if ans_mkbsd then begin
            printf,lun,'$!
            printf,lun,'$!	  Execute programme to Make the BSD file
            printf,lun,'$!	Control file for MKBSD is called ',cfl_file1
            printf,lun,'$ MKBSD
            printf,lun,cfl_file1
	    print,'Control file for MKBSD:  ',cfl_file1
         endif
         if ans_bsdcal then begin
            printf,lun,'$!
            printf,lun,'$!	  Execute BSD Calibration programme
            printf,lun,'$!	Control file for BSDCAL is called ',cfl_file2
            printf,lun,'$ BSDCAL
            printf,lun,cfl_file2
	    print,'Control file for BSDCAL: ',cfl_file2
         endif
         if ans_bsdfit then begin
            printf,lun,'$!
            printf,lun,'$!	  Execute BSD Fit programme
            printf,lun,'$!	Control file for BSDFIT is called ',cfl_file3
            printf,lun,'$ BSDFIT
            printf,lun,cfl_file3
	    print,'Control file for BSDFIT: ',cfl_file3
         endif
         printf,lun,'$!
         printf,lun,'$!
         free_lun,lun

         spawn,'@' + com_file
;         spawn,'del ' + com_file + ';0'

      end

;	'unix' or 'ultrix' or 'RISC/os' or 'sunos'
else: begin
;		use task_os to start correct task!!

         if ans_mkbsd then begin
            openw,lun,/get_lun,com_file
            printf,lun,cfl_file1
            free_lun,lun
;;            spawn,'$DIR_BCS_EXE/' + task_os('run_mkbsd') + ' < ' + com_file
;	run_mkbsd scripts combined into one on 28/Oct/93 to simplify 
;	definition of CALFILn environment variables
            spawn,'$DIR_BCS_EXE/run_mkbsd' + ' < ' + com_file
         endif
         if ans_bsdcal then begin
            openw,lun,/get_lun,com_file
            printf,lun,cfl_file2
            free_lun,lun
            spawn,'$DIR_BCS_EXE/' + task_os('bsdcal') + ' < ' + com_file
         endif
         if ans_bsdfit then begin
            openw,lun,/get_lun,com_file
            printf,lun,cfl_file3
            free_lun,lun
            spawn,'$DIR_BCS_EXE/' + task_os('bsdfit') + ' < ' + com_file
         endif

;         spawn,'rm ' + com_file

      end
endcase

endif

endit:
end
