require ("xfig");
private variable MLam_Max = 20.0;
variable Const_keV_A = 12.3984185734;
private variable E_Si_K = 1.739;
private variable Lam_Si_K = Const_keV_A/E_Si_K;

private variable Scale_X = 0.5;
private variable Scale_Y = 3.0;

private define svector (x, y)
{
   return vector (Scale_X*x, Scale_Y*y, 0*x);
}

define line_segment (x0, y0, x1, y1)
{
   return xfig_new_polyline (svector([x0,x1], [y0,y1]));
}

private define gaussian (x, x0, amp, sigma)
{
   x -= x0;
   x /= sigma;
   return amp * exp (-x*x);
}

private define rmf_profile (m, lam, sigma)
{
   variable mlam = m*lam;
   variable amp_primary = 3.0;
   variable amp_escape = 2;
   variable amp_fluor = 1;
   variable en = Const_keV_A/lam;
   variable x0_primary = en;
   variable x0_escape = en - E_Si_K;
   variable x0_fluor = E_Si_K;

   variable x = [_min(x0_fluor,x0_escape) - 3*sigma: x0_primary+2*sigma: #100];

   variable y = gaussian (x, x0_primary, amp_primary, sigma);
   y += gaussian (x, x0_escape, amp_escape, sigma);
   y += gaussian (x, x0_fluor, amp_fluor, sigma);

   x = abs(mlam) * x/Const_keV_A;
   y += mlam;
   return xfig_new_polyline (svector (y,x));
}

define sloped_line (x0, y0, m, xlim, ylim)
{
   variable x1 = xlim;
   variable y1 = y0 + m*(x1-x0);

   if (((y0 - ylim) * (y1 - ylim)) < 0)
     {
	y1 = ylim;
	x1 = x0 + (y1-y0)/double(m);
     }
   
   return line_segment (x0, y0, x1, y1);
}

define set_line_thickness_and_color (obj, width, color)
{
   obj.set_pen_color (obj, color);
   obj.set_thickness (obj, width);
}

define slsh_main ()
{
   variable obj, text;
   variable fp = xfig_create_file ("osort.png");
   
   % Create the axis
   obj = line_segment (-1.2*MLam_Max, 0, 1.2*MLam_Max, 0);
   obj.set_pen_color ("black");
   obj.set_thickness (2);
   obj.render (fp);
   obj = line_segment (0, 0, 0, 3.5);
   obj.set_thickness (2);
   obj.set_pen_color ("black");
   obj.render (fp);

   text = xfig_new_text ("\Large\[|m\lambda|\frac{E}{hc}\]"R);
   xfig_justify_object (text, svector (0, 3.5), vector(-0.5,0,0));
   text.render (fp);

   text = xfig_new_text ("\Large $m\lambda$"R);
   xfig_justify_object (text, svector (1.1*MLam_Max,0), vector(0.5,0.75,0));
   text.render (fp);

   text = xfig_new_text ("\Large $m\lambda=0$"R);
   xfig_justify_object (text, svector (0,0), vector(0,0.75,0));
   text.render(fp);
   variable slope = 1.0/Lam_Si_K;

   variable m;
   _for m (1, 3, 1)
     {
	obj = sloped_line (-MLam_Max, m, 0, MLam_Max, 0);
	obj.set_pen_color ("green3");
	obj.set_thickness (3);
	obj.render(fp);

	text = xfig_new_text ("\Large m=-$m"$R);
	xfig_justify_object (text, svector (-MLam_Max, m), vector(0.5,0,0));
	text.render (fp);
	text = xfig_new_text ("\Large m=$m"$R);
	xfig_justify_object (text, svector (MLam_Max, m), vector(-0.5,0,0));
	text.render (fp);

	% Escape lines
	obj = sloped_line (0, m, -slope, MLam_Max, 0);
	obj.set_thickness (3);
	obj.set_pen_color ("magenta3");
	obj.render(fp);
	obj = sloped_line (0, m, slope, -MLam_Max, 0);
	obj.set_thickness (3);
	obj.set_pen_color ("magenta3");
	obj.render(fp);
     }

   % Fluorescence
   obj = sloped_line (0, 0, slope, MLam_Max, 1);
   obj.set_thickness (3);
   obj.set_pen_color ("blue");
   obj.render(fp);
   obj = sloped_line (0, 0, -slope, -MLam_Max, 1);
   obj.set_thickness (3);
   obj.set_pen_color ("blue");
   obj.render(fp);

   obj = sloped_line (-Lam_Si_K, 1, -slope, -MLam_Max, 3);
   obj.set_thickness (3);
   obj.set_pen_color ("blue");
   obj.set_line_style (1);
   obj.render(fp);

   obj = sloped_line (Lam_Si_K, 1, slope, MLam_Max, 3);
   obj.set_thickness (3);
   obj.set_pen_color ("blue");
   obj.set_line_style (1);
   obj.render(fp);

   variable x0, y0, x1, y1;

   % Label for Escape events with arrows
   x0 = 15.0; y0 = 1.5;
   text = xfig_new_text ("\Large\bf\noindent Escape\\Events"R);
   xfig_justify_object (text, svector (x0,y0), vector(-0.5,0,0));
   xfig_render_object (text, fp);
   
   x1 = 9; y1 = 1.75;
   obj = xfig_new_polyline_with_arrow (svector([x0,x1],[y0,y1]), 0.2, 0.4);
   obj.set_thickness (2);
   obj.set_pen_color ("black");
   obj.render(fp);

   x1 = 5.5; y1 = 1.25;
   obj = xfig_new_polyline_with_arrow (svector([x0,x1],[y0,y1]), 0.2, 0.4);
   obj.set_thickness (2);
   obj.set_pen_color ("black");
   obj.render(fp);

   x1 = 5.5; y1 = 0.25;
   obj = xfig_new_polyline_with_arrow (svector([x0,x1],[y0,y1]), 0.2, 0.4);
   obj.set_thickness (2);
   obj.set_pen_color ("black");
   obj.render(fp);

   % Label for Escape events with arrows
   x0 = 5; y0 = -0.3;
   text = xfig_new_text ("\Large\bf\noindent Fluorescence Events"R);
   xfig_justify_object (text, svector (x0,y0), vector(-0.5,0.5,0));
   xfig_render_object (text, fp);
   
   x1 = 1.7; y1 = 0.2;
   obj = xfig_new_polyline_with_arrow (svector([x0,x1],[y0,y1]), 0.2, 0.4);
   obj.set_thickness (2);
   obj.set_pen_color ("black");
   obj.render(fp);

   obj = rmf_profile (-3, 5.0, 0.2);
   obj.set_thickness (2);
   obj.set_pen_color ("black");
   obj.set_area_fill (30);
   obj.set_fill_color ("blue");
   obj.render(fp);

   obj = rmf_profile (-1, 5.0, 0.2);
   obj.set_thickness (2);
   obj.set_pen_color ("black");
   obj.set_area_fill (30);
   obj.set_fill_color ("blue");
   obj.render(fp);
   
   x0 = -18; y0 = -0.3;
   text = xfig_new_text ("\Large\bf\noindent RMF profiles"R);
   xfig_justify_object (text, svector (x0,y0), vector(0,0.5,0));
   xfig_render_object (text, fp);
   
   x1 = -5; y1 = 0.4;
   obj = xfig_new_polyline_with_arrow (svector([x0,x1],[y0,y1]), 0.2, 0.4);
   obj.set_thickness (2);
   obj.set_pen_color ("black");
   obj.render(fp);

   x1 = -15; y1 = 0.7;
   obj = xfig_new_polyline_with_arrow (svector([x0,x1],[y0,y1]), 0.2, 0.4);
   obj.set_thickness (2);
   obj.set_pen_color ("black");
   obj.render(fp);

   x0 = 14; y0 = 3.2;
   text = xfig_new_text ("\noindent \Large $m\lambda$ = \verb|TG_MLAM|\\$E$ = \verb|ENERGY|"R);
   xfig_justify_object (text, svector (x0,y0), vector(-0.5,-0.5,0));
   text.render (fp);
   
   xfig_close_file (fp);
}
