%N zed %F $MIR/src/subs/zed.for
%D Zeeman fit of I spectrum to V spectrum.
%P nebk
%: zeeman
%B
      subroutine Zed(mode,ispect,vspect,m,n,a,b,siga,sigb,sigi,convrg)

      character mode*(*)
      integer m,n
      logical convrg
      real ispect(m,n),vspect(m,n),a,b,siga,sigb,sigi
  ---------------------------------------------------------------------
  This finds values of a and b, which are optimal in either a maximum
  likelihood or least squares sense, such that

    V = a*D*I + b*I

  One solution for a and b is found for the n given spectra.

  Here D is a matrix approximating a derivative operator.  The
  derivative formula used here is either:
    f(i) - f(i-1)
  or
    0.5*(f(i+1) - f(i-1))

  Inputs:
    ispect,vspect Measured I and V spectra.
    m          Number of data points in the I and V spectra.
    n          Number of spectra.
    mode       This consists of flags, which determine
               the algorithm to be used. Possible characters are:
                 '2'   Use two sided derivative estimate. Otherwise a
                       one sided derivative is used.
                 'm'   Use the maximum likelihood technique, otherwise
                       a least squares algorithm is used.
                 'l'   Fit for the leakage parameter as well.
                 'x'   Perform extra checks that we have converged to
                       the global minimum.  This slows down the
                       algorithm by about a factor of 3.  This should
                       be used in cases of poor signal to noise.
                  'd'  Debiased least squares.  This debiases alpha and
                       sigma alpha only.
               For example, mode='mlx2' means use max. likelihood,
               including leakage, use two sided derivative, and perform
               extra checks that we have converged to the global
               minimum.
  Output:
    a,b        Estimated a and b parameters.
    siga,sigb  Estimates of the error in a and b.
    sigi       Estimate of the rms noise.
    convrg     True if the procedure converged.  It is generally safe
               to ignore this.  Even when it indicates the algorithm
               has failed to converge, it actually means that it is
               essentially at the minimum, but not all conditions for
               convergence have yet to be satisfied.
