function [options, iniinfo] = zgn_inicheck( fcn, x0, uds, options, flag)
%
%  M-function zgn_inicheck checkes and eventually creates
%  the method options structure for NLSQ_RES and NLSQ_ERR
% 
  iniinfo = 0;
% monitor level and output file for zgn_inicheck
  mon = 0;
  luout = 1;
%
  def_options = [];
  def_options.ftol = 1.e-6;
  def_options.xtol = 1.e-6;
  def_options.kmax = 25;
  def_options.lambdamin = 1.e-4;
  def_options.lambda0 = 1.e-2;
  def_options.numdif = 1;
  def_options.printmon = 2;
  def_options.printsol = 0;
  def_options.fid = 1;
  def_options.iniscalx = 0;
  def_options.rescalx = 0;
  def_options.xthrsh = ones( length(x0), 1);

  if mon > 1
    if strcmp( flag, 'NLSQ_RES') == 1
      fprintf(luout,'  - checking structure OPTIONS for NLSQ_RES  \n');
    else
      fprintf(luout,'  - checking structure OPTIONS for NLSQ_ERR  \n');
    end
  end
%
  if isstruct(options) == 0
    if mon > 0     
      fprintf(luout,'   - OPTIONS is not a struct; create and set default optionss \n');
    end
    options = [];
    options.ftol = def_options.ftol;
    options.xtol = def_options.xtol;
    options.kmax = def_options.kmax;
    options.printmon = def_options.printmon;
    options.printsol = def_options.printsol;
    options.fid = def_options.fid;
    options.iniscalx = def_options.iniscalx;
    options.rescalx = def_options.rescalx;
    options.xthrsh = def_options.xthrsh;
    options.numdif = def_options.numdif;
    options.lambdamin = def_options.lambdamin;
    options.lambda0 = def_options.lambda0;
  end
   
  if strcmp( flag, 'NLSQ_RES') == 1
    if isfield(options,'ftol') == 0
      if mon > 0
        fprintf(luout,'   - OPTIONS.FTOL missing; setting default \n');
      end
      options.ftol = def_options.ftol;
    else
      if options.ftol > 1.e-2 | options.ftol < 1.e-10
        if mon > 0
          fprintf(luout,'   - OPTIONS.FTOL invalid; setting default \n');
        end
        options.ftol = def_options.ftol;
      else
        if mon > 1
          fprintf(luout,'   - OPTIONS.FTOL checked; O.K. \n');
        end
      end
    end
  end

  if strcmp( flag, 'NLSQ_ERR') == 1
    if isfield(options,'xtol') == 0
      if mon > 0
        fprintf(luout,'   - OPTIONS.XTOL missing; setting default \n');
      end
      options.xtol = def_options.xtol;
    else
      if options.xtol > 1.e-2 | options.xtol < 1.e-10
        if mon > 0
          fprintf(luout,'   - OPTIONS.XTOL invalid; setting default \n');
        end
        options.xtol = def_options.xtol;
      else
        if mon > 1
          fprintf(luout,'   - OPTIONS.XTOL checked; O.K. \n');
        end
      end
    end
  end

  if isfield(options,'kmax') == 0
    if mon > 0
      fprintf(luout,'   - OPTIONS.KMAX missing; setting default \n');
    end
    options.kmax = def_options.kmax;
  else
    if options.kmax > 1000 | options.kmax < 3
      if mon > 0
        fprintf(luout,'   - OPTIONS.KMAX invalid; setting default \n');
      end
      options.kmax = def_options.kmax;
    else
      if mon > 1
        fprintf(luout,'   - OPTIONS.KMAX checked; O.K. \n');
      end
    end
  end

  if isfield(options,'printmon') == 0
    if mon > 0
      fprintf(luout,'   - OPTIONS.PRINTMON missing; setting default \n');
    end
    options.printmon = def_options.printmon;
  else
    if options.printmon > 3 | options.printmon < 0
      if mon > 0
        fprintf(luout,'   - OPTIONS.PRINTMON invalid; setting default \n');
      end
      options.printmon = def_options.printmon;
    else
      if mon > 1
        fprintf(luout,'   - OPTIONS.PRINTMON checked; O.K. \n');
      end
    end
  end

  if isfield(options,'printsol') == 0
    if mon > 0
      fprintf(luout,'   - OPTIONS.PRINTSOL missing; setting default \n');
    end
    options.printsol = def_options.printsol;
  else
    if options.printsol > 3 | options.printsol < 0
      if mon > 0
        fprintf(luout,'   - OPTIONS.PRINTSOL invalid; setting default \n');
      end
      options.printsol = def_options.printsol;
    else
      if mon > 1
        fprintf(luout,'   - OPTIONS.PRINTSOL checked; O.K. \n');
      end
    end
  end

  if isfield(options,'fid') == 0
    if mon > 0
      fprintf(luout,'   - OPTIONS.FID missing; setting default \n');
    end
    options.fid = def_options.fid;
  else
    if mon > 1
      fprintf(luout,'   - OPTIONS.FID exists; no check for value \n');
    end
  end

  if isfield(options,'numdif') == 0
    if mon > 0
      fprintf(luout,'   - OPTIONS.NUMDIF missing; setting default \n');
    end
      options.numdif = def_options.numdif;
   else
     if options.numdif > 1 | options.numdif < 0
       if mon > 0
         fprintf(luout,'   - OPTIONS.NUMDIF invalid; setting default \n');
       end
       options.numdif = def_options.numdif;
    else
      if mon > 1
        fprintf(luout,'   - OPTIONS.NUMDIF checked; O.K. \n');
      end
    end
  end

  if isfield(options,'lambdamin') == 0
    if mon > 0
      fprintf(luout,'   - OPTIONS.LAMBDAMIN missing; setting default \n');
    end
    options.lambdamin = def_options.lambdamin;
  else
    if options.lambdamin > 0.1 | options.lambdamin < 1.e-8 
      if mon > 0
        fprintf(luout,'   - OPTIONS.LAMBDAMIN invalid; setting default \n');
      end
      options.lambdamin = def_options.lambdamin;
    else
      if mon > 1
        fprintf(luout,'   - OPTIONS.LAMBDAMIN checked; O.K. \n');
      end
    end
  end

  if isfield(options,'lambda0') == 0
    if mon > 0
      fprintf(luout,'   - OPTIONS.LAMBDA0 missing; setting default \n');
    end
    options.lambda0 = max(options.lambdamin,def_options.lambda0);
  else
    if options.lambda0 > 1 | options.lambda0 < options.lambdamin
      if mon > 0
        fprintf(luout,'   - OPTIONS.LAMBDA0 invalid; setting default \n');
      end
      options.lambda0 = max(options.lambdamin,def_options.lambda0);
    else
      if mon > 1
        fprintf(luout,'   - OPTIONS.LAMBDA0 checked; O.K. \n');
      end
    end
  end

  if isfield(options,'xthrsh') == 0
    if mon > 0
      fprintf(luout,'   - OPTIONS.XTHRSH missing; setting default \n');
    end
    options.xthrsh = def_options.xthrsh;
  else
    if all(options.xthrsh>0) == 0
      if mon > 1
        fprintf(luout,'   - OPTIONS.XTHRSH invalid; setting default \n');
      end
      options.xthrsh = def_options.xthrsh;
    else
      if mon > 1
        fprintf(luout,'   - OPTIONS.XTHRSH checked; O.K. \n');
      end
    end
  end
   
  if isfield(options,'iniscalx') == 0
    if mon > 0
      fprintf(luout,'   - OPTIONS.INISCALX missing; setting default \n');
    end
    options.iniscalx = def_options.iniscalx;
  else
    if options.iniscalx > 2 | options.iniscalx < 0
      if mon > 0
        fprintf(luout,'   - OPTIONS.INISCALX invalid; setting default \n');
      end
      options.iniscalx = def_options.iniscalx;
    else
      if mon > 1
        fprintf(luout,'   - OPTIONS.INISCALX checked; O.K. \n');
      end
    end
  end
   
  if isfield(options,'rescalx') == 0
    if mon > 0
      fprintf(luout,'   - OPTIONS.RESCALX missing; setting default \n');
    end
    options.rescalx = def_options.rescalx;
  else
    if options.rescalx > 2 | options.rescalx < 0
      if mon > 0
        fprintf(luout,'   - OPTIONS.RESCALX invalid; setting default \n');
      end
      options.rescalx = def_options.rescalx;
    else
      if mon > 1
        fprintf(luout,'   - OPTIONS.RESCALX checked; O.K. \n');
      end
    end
  end


% end of function zgn_inicheck 
%=============================

