;+
;  Name:
;       FCRYSTAL
;  Purpose:
;
;       This function calculates the energy loss in the NaI crystal given
;        the light observed.  The light is assumed to be
;                       L  =  E * g_c(E)
;       where g_c and its derivative are provided. A simple Newton-Raphson
;       algorithm is used on the assumption that the relation is very well behaved. 
;
;  Category:
;       BATSE
;  Calling Sequence:
;       result = fcrystal( olight )
;   Inputs:
;       olight
;   Outputs:
;       The result is the derived energy loss in keV
;   Restrictions:
;   Adapted from measurements made by UCSD on BATSE SPEC detectors.
;
;   Common Blocks:
;    none
;
;  History:
;   Written for FORTRAN by DLB, 10/2/91, IDL2 6/3/92 RAS
;  
;-
function fcrystal, olight

      eps=1.e-5
;c Initial guess
      crystal = olight
;c
      for it = 1,20 do begin

         if (crystal le 0.) then begin
            crystal=1.
            return, crystal
         endif

         g_c,crystal,g_func,g_der
         de = (olight-crystal*(g_func)(0))/((g_func)(0)+crystal*(g_der)(0))
         crystal = crystal+de
         if (abs(de) lt eps*crystal) then return, crystal

      endfor
;c
      print,format=format,olight,crystal,de
      format="('  Fcrystal did not converge within 20 iterations',//," + $
           " ' Light=',e10.5,' Energy=',e10.5,' Last correction=',e10.5)"
;c
      return, 0.0
      end
