variable Init_Thickness = 0.45;
variable Init_Bar_Frac = 0.60;
variable Init_Poly_Thick = 0.50;
% File to fit X-GEF measurements to grating model using
% John Davis's code.

% Parameters to control the fitting
% IDL will pre-pend these to this file before execution

%variable Init_Thickness = 0.45;
%variable Init_Bar_Frac = 0.60;
%variable Init_Poly_Thick = 0.6;

% First thing to do is set up the geometry for the grating.
message ("Setting grating profile");
% 4 vertices
%define_grating_polygon ((0.5 - Init_Bar_Frac / 2. - 0.01), 0.001,
%			(0.5 - Init_Bar_Frac / 2.), Init_Thickness,
%			(0.5 + Init_Bar_Frac / 2.), Init_Thickness,
%			(0.01 + 0.5 + Init_Bar_Frac / 2.), 0.001,
%			4);	       %  4 vertices    

% 5 vertices
define_grating_polygon ((0.5 - Init_Bar_Frac / 2. - 0.01), 0.001,
			(0.5 - Init_Bar_Frac / 2.), Init_Thickness,
			(0.5 ), Init_Thickness,
			(0.5 + Init_Bar_Frac / 2.), Init_Thickness,
			(0.01 + 0.5 + Init_Bar_Frac / 2.), 0.001,
			5);	       %  5 vertices    

message ("Setting substrate layer values");
set_plating_base (0.0200,	       %  gold    
		  Init_Poly_Thick,     %  polyimide    
		  0.0050,		%  chromium    
		  3);			%  3 layers    

What_To_Fit[0]	=  0;
What_To_Fit[1]  =  0;	% Remove this line to fit polyimide
What_To_Fit[2]  =  0;


write_grating_model ("stderr", 0);

% Now read in optical constants
message ("Reading optical constants.");
read_optical_constants ("optical-constants.dat");

% Read in data from X-GEF test
read_efficiency_data (1.0, 	       %  scale to KeV
                      "pspc_effics_m2.dat", -2,
		      "pspc_effics_m1.dat", -1,
		      "pspc_effics_p1.dat", 1,
		      "pspc_effics_p2.dat", 2,
		       4);

find_best_fit ();

write_grating_model ("jfit_model.dat", 'c');

write_efficiencies (-2, 2, "jfit_effics.dat", 'c');

write_residuals (-1, "jfit_resid_m1.dat", 'c');  % energy, per-cent error
write_residuals (1, "jfit_resid_p1.dat", 'c');
write_residuals (-2, "jfit_resid_m2.dat", 'c');
write_residuals (2, "jfit_resid_p2.dat", 'c');

quit ();
