variable Multi_Format_Filename = "cmp/LETG_shell4_multi.txt";
variable Energy_Grid_File = "fcp/fcp_energy.dat";
variable Output_File = "fcp/LETG_shell4_multiout.txt";

variable Opt_Constants_File = "fcp/optical-constants.981023.tbl";

variable Order_Min = -25;
variable Order_Max = 25;

variable Efficiency;
variable Energy_Grid;
variable Energy_Grid_Npts;

% Read in a line from open file descriptor fp skipping comment lines.  Returns
% 0 if at end of file, 1 + line otherwise.
define read_line (fp)
{
   variable n;
   variable line;
   variable ch;
   
   forever
     {
	if (fgets (fp) <= 0)
	  return 0;
	
	line = ();
	line = strtrim (line);
	
	ch = line[0];
	
	if ((ch != '%') and (ch != ';') and (ch != '#') and (ch != 0))
	  return (line, 1);
	
     }
}

define open_file (file)
{
   variable fp = fopen (file, "r");
   
   if (fp == -1)
     error (strcat ("Unable to open ", file));
   
   return fp;
}


define compute_model_efficiencies (weight)
{
   variable i;
   variable order;
   variable eff;
   variable ord, ord_max;
   
   order = Order_Min;
   ord_max = Order_Max - Order_Min;
   
   _for (0, Energy_Grid_Npts - 1, 1)
     {
	i = ();
	
	_for (0, ord_max, 1)
	  {
	     ord = ();
	     order = Order_Min + ord;
	     
	     eff = compute_efficiency (Energy_Grid[i], order);

	     Efficiency [i,ord] = Efficiency [i,ord] + weight * eff;
	  }
     }
}

define compute_line_model (line)
{
   variable nfields;
   variable id, location, poly, cr, au, nvertices, weight;
   
   nfields = strchopr (line, '\t', 0);
   if ((nfields < 9) or (nfields mod 2))
     error ("Line has wrong number of fields.");
   
   id = ();
   location = ();
   
   () = fputs (Sprintf ("Processing %s (%s)\n", id, location, 2), stdout);
   () = fflush (stdout);
   
   weight = float ();
   Amplitude_Factor = float ();
   poly = float ();
   cr = float ();
   au = float ();
   
   nvertices = integer ();

   loop (2 * nvertices)
     {
	float ();
	_stk_roll (2 * nvertices);
     }
   _stk_reverse (2 * nvertices);
   
   set_plating_base (au, poly, cr, 3);
   define_grating_polygon (nvertices); %  values on stack
   
   %write_grating_model ("stdout", 0);
   
   compute_model_efficiencies (weight);
}


define get_energy_grid ()
{
   variable fp;
   variable npts;
   
   % Perform 2 passes.  Read file to determine size and then go back and read
   % data.
   fp = open_file (Energy_Grid_File);
   npts = 0; 
   while (read_line (fp))
     { 
	pop ();
	npts++;
     }
   
   () = fclose (fp);
   
   Energy_Grid_Npts = npts;
%   Energy_Grid = create_array ('f', npts, 1);
%   Efficiency = create_array ('f', npts, Order_Max - Order_Min + 1, 2);
   Energy_Grid = Double_Type[npts];
   Efficiency = Double_Type[npts, Order_Max - Order_Min + 1];
   
   fp = open_file (Energy_Grid_File);
   
   npts = 0;
   while (read_line (fp))
     {
	variable energy = float ();
	Energy_Grid [npts] = energy;
	npts++;
     }
   
   () = fclose (fp);
}


define write_results ()
{
   variable i, fp;
   variable ord;
   variable norders;
   
   fp = fopen (Output_File, "w");
   
   norders = Order_Max - Order_Min + 1;
   
   _for (0, Energy_Grid_Npts - 1, 1)
     {
	i = ();
	
	() = fputs (Sprintf ("%e", Energy_Grid[i], 1), fp);
	
	_for (0, norders - 1, 1)
	  {
	     ord = ();
	     () = fputs (Sprintf ("\t%e", Efficiency[i, ord], 1), fp);
	  }
	() = fputs ("\n", fp);
     }
   
   () = fclose (fp);
}


define main ()
{
   variable fp, line;
   
   get_energy_grid ();
   
   fp = open_file (Multi_Format_Filename);
   
   message ("Reading optical constants.");
   read_optical_constants (Opt_Constants_File);

   message ("Computing efficiencies.");
   
   while (read_line (fp))
     {
	line = ();
	compute_line_model (line);
     }
   
   () = fclose (fp);
   
   message ("Writing results.");
   write_results ();
}

main ();

   
   
