PRO Fsp_proc, cfile, ofile, tyspec, fit_pars, sc_par, ch_dta, $
              pfile=pfile, sdel=sdel, flux_corr=flux_corr, nsigmas=nsigmas, $
              noplot=noplot, a_fix=a_fix, erange=erange, mc_trials=mc_trials, $
              mc_counts=mc_counts, mc_fits=mc_fits, init_a=init_a

   COMMON Rsp, nch, nsc, esc, rsc
   
;set up appropriate data structures:
;one of these for each flare interval
   ff1 = {fitinfo, tyspec:0, ma:0, alabels:strarr(6), t:intarr(7), dt:0.0, $
           a:fltarr(6), sa:fltarr(6), ffx:intarr(6), chi2:0.0, $
           nch:0, fluxc:0.0, nsig:0.0, flags:bytarr(5)}
;one of these for each detector channel,
   ff2 = {scpar, e10:0.0, e20:0.0, eb0:0.0, bck:fltarr(2), sbck:fltarr(2), $
           tb:intarr(7,2), dtb:fltarr(2)}
;one of these for each detector channel,for each time interval
   ff3 = {chdta, y0:0.0, sy0:0.0, chok:0b, c0:0.0, b0:0.0, sb0:0.0, f0:0.0, sf0:0.0}
   
; Get ma and labels, ma is the number of fit parameters
   ma_labels, tyspec, ma, alabels

;first find number of dummy lines in the resp. file
   n_dummy = dummy_rd(cfile)
;open input file
   openr, four, cfile, /get_lun   
;If necessary, read the dummy lines
   IF(n_dummy GT 0) THEN BEGIN
      hdr = strarr(n_dummy)
      tstr = strarr(1)
      FOR j = 0, n_dummy-1 DO BEGIN
         readf, four, tstr
         hdr(j) = tstr(0)
      ENDFOR
   ENDIF

   detector = strarr(1)
; start inputting from the file
   readf, four, detector
   detector = detector(0)
   readf, four, n_ins
   n_ins = fix(n_ins)           ;number of detectors

   inpf = strarr(n_ins)
   readf, four, inpf            ;resp file names
; get the responses
   resp_str = multresponses(inpf)
   nc0 = resp_str.nc0
   
; input the background times
   tbck0 = intarr(7)
   readf, four, tbck0
   readf, four, dtb0
   bck0 = fltarr(nc0)       ;background
   sbck0 = bck0                  ;sigma(bck)
   readf, four, bck0
   readf, four, sbck0

   tbck1 = intarr(7)
   readf, four, tbck1
   readf, four, dtb1
   bck1 = fltarr(nc0)       ;background
   sbck1 = bck1                  ;sigma(bck)
   readf, four, bck1
   readf, four, sbck1

   readf, four, fluxc0          ;flux correction?
;do we have a flux correction, or do we use the input a flux correction?
   IF(KEYWORD_SET(flux_corr)) THEN fluxc = flux_corr ELSE fluxc = fluxc0

   readf, four, events      ;the no. of events in the file
   events = fix(events)

   IF(events EQ 0) THEN BEGIN
      dude, ' no events??'
      RETURN
   ENDIF

;number of sigmas above background?
   IF(KEYWORD_SET(nsigmas)) THEN nsig = float(nsigmas(0)) ELSE nsig = 3.0

;fill the structure sc_par
   sc_par = ff2
   sc_par.tb(*, 0) = tbck0
   sc_par.tb(*, 1) = tbck1
   sc_par.dtb(0) = dtb0
   sc_par.dtb(1) = dtb1
   sc_par = replicate(sc_par, nc0)
   sc_par.bck(0) = bck0
   sc_par.sbck(0) = sbck0
   sc_par.bck(1) = bck1
   sc_par.sbck(1) = sbck1
   
   fit_pars = replicate({fitinfo}, events)
   ch_dta = replicate({chdta}, nc0, events)
   
;Obtain fixed parameters if necessary
   IF(KEYWORD_SET(a_fix)) THEN fix_pars, events, a_fix, alabels, ma, a_0, ffx
   
   chok = bytarr(nc0)           ;switch to tell us which channel is good, 
                                ;will give us icch chok(j)=0 if we keep, 
                                ;chok(j)>0 if not
   y0 = fltarr(nc0)             ;count rate
   sy0 = y0         ;sigma(y0)
   flags = bytarr(5)
      
;now input and fit the individual events
   FOR nev = 0, events-1 DO BEGIN
      t = intarr(7)
      readf, four, t            ;event start time
      readf, four, dt           ;interval
      readf, four, y0           ;count rate
      readf, four, sy0          ;sigma(y0)
      readf, four, chok         ;flag for good channels
      readf, four, flags
      
;fill the output structures
      fit_pars(nev).t = t
      fit_pars(nev).dt = dt
      fit_pars(nev).flags = flags
      
;if needed fill the counts structure
      ch_dta(*, nev).y0 = y0
      ch_dta(*, nev).sy0 = sy0
      ch_dta(*, nev).chok = chok
   ENDFOR

;ok, now you have the appropriate structures, do the fit
   IF(KEYWORD_SET(mc_trials)) THEN BEGIN
      fsp_mc, detector, resp_str, tyspec, fit_pars, sc_par, ch_dta, $
        ofile = ofile, pfile = pfile, sdel = sdel, nsigmas = nsigmas, $
        a_fix = a_fix, flux_corr = fluxc, noplot = noplot, erange = erange, $
        n_trials = mc_trials, mc_counts = mc_counts, mc_fits = mc_fits, init_a = init_a
   ENDIF ELSE BEGIN
      fsp_11, detector, resp_str, tyspec, fit_pars, sc_par, ch_dta, $
        ofile = ofile, pfile = pfile, sdel = sdel, nsigmas = nsigmas, $
        a_fix = a_fix, flux_corr = fluxc, noplot = noplot, erange = erange, init_a=init_a
   ENDELSE
   
   RETURN
END