%; Time-stamp: <2007-03-18 20:42:57 dph> 
%; Directory: ~dph/d6/Proc_data/LC_demo_44_Boo
%; File: Demo.sl
%; Author: D. Huenemoerder
%; Original version: 2003.12.18
%;====================================================================
% version: 2
%
% purpose: analyze 44 boo light curves; demonstrate light/phase curve
%          prototype functions.
%
%

require("aglc");

_auto_declare = 1;   % be lax about variable declarations


%% utility function to find the complement of a "where()" list:
%
private define notwhere(idx,npts)
{
    variable y = Integer_Type[ npts ] ;
    y[idx] = 1;
    return where( not y ) ;
}

% utility function to print an array of strings
% (for output of demo documentation);
%
private define mydoc(str_array)
{
    variable i;
    ()=printf("\n");
    for (i=0;i<length(str_array);i++)
	      () = printf("%% >>>  %s\n", str_array[i]);
    ()=printf("\n");
}

private variable vp = get_outer_viewport ; 
private define set_vp(x1, x2, y1, y2 )
{
    vp.xmin = x1 ;
    vp.xmax = x2 ;
    vp.ymin = y1 ;
    vp.ymax = y2 ;
    set_outer_viewport( vp ) ; 
}

% open a plot window:

putenv("PGPLOT_BACKGROUND=white");     % invert default for better view.
putenv("PGPLOT_FOREGROUND=black");

pw1 = open_plot("/xwin");
charsize(1.4);
resize(20,0.6);
erase;
%_pgsvp(0.1, 0.98, 0.15, 0.9);
set_vp(0.1, 0.98, 0.15, 0.9);
set_line_width(7);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

mydoc(["Load pha file, +-1st orders, group, combine, and plot:"]);
message("h = load_data(\"indir/pha2\");\n\n");

h = load_data("indir/pha2");   % 4-row file, for inspecting counts.
group_data(h[[0,1]],8);  % rebin by 8x.
group_data(h[[2,3]],4);  % rebin by 4x.

g_meg = combine_datasets(h[[2,3]]);        % dynamic summing of orders.
g_heg = combine_datasets(h[[0,1]]);

% define some convenient parameters:
%
dt = 200.0;
w1 = 1.7;
w2 = 8.0;
w3 = 13.0;
w4 = 26.0;

%%% plot the spectra; counts:
%
xrange(w1, w4); yrange(0);
title("44 Boo HETGS (binned 4,8)");
xlabel("Wavelength");  ylabel("Counts/bin");
message("hplot( get_combined(g_meg, &get_data_counts), 2);");
connect_points(1);
hplot( get_combined(g_meg, &get_data_counts), 1);
ohplot( get_combined(g_heg, &get_data_counts), 14);

y0 = max(get_combined(g_meg, &get_data_counts).value) * 0.4;
set_line_width(9);
oplot([w1,w2], [y0,y0], 4); 
oplot([w3,w4], [y0,y0], 2);
set_line_width(3);
color(4); xylabel(3,y0*1.05,"\"Hard\" band");
color(2); xylabel(17,y0*1.05,"\"Soft\" band");

plot_pause;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

mydoc(["Read events into a structure used by light curve functions",
       " evt1a  holds grating coordinates, and may be filtered.",
       " evt0   is unfiltered, and is used for exposure numbers from",
       " ALL exposed frames.\n"]);
message("s = aglc_read_events( f_evt1a, f_evt0 );\n\n");

% read the grating coord events and the exposure (unfiltered) events:
%  For this demo, these two files have been filtered to include
%  only the columns needed here (to make them smaller).
%  
f_evt1a = "indir/evt1a.tg";
f_evt0  = "indir/stat1.exp";

s = aglc_read_events( f_evt1a, f_evt0 );


%%% use both HEG and MEG, and orders -3 to 3 (except 0) for light curves:
%
g = ["H", "M"];             % grating types
o = [-3,-2,-1,1,2,3];       % orders

mydoc(["Compute light curves in full band, and soft and hard bands",
       "We could name the event files directly instead of the structure,",
       "but for reapeated use, the structure is much more efficient.\n",
       "NOTE: aglc returns a structure with grids, counts, rates, exposure.",
       "   Variables have been defined to generalize the inputs:",
       "   dt = time bin; w1-4 are wavelengths",
       "      (scalars were used here; can use arrays for multiple intervals);",
       "   g = [\"H\",\"M\"] for HEG and MEG photons;",
       "   o = [-3,-2,-1,1,2,3]  for orders."
      ] );

% compute and plot light curves in full band, and soft and hard bands:
%
multiplot(3,1);
charsize(1.2);
yrange(0,2.2); xrange(-1000);
set_line_width(5);
label("Time", "count_rate", "44 Boo, HETGS");

message("c= aglc(s, dt, w1, w4, g, o); % Full band\n");
c= aglc(s, dt, w1, w4, g, o);
message("hplot(  c.time_min,  c.time_max,  c.count_rate, 1);");
hplot(  c.time_min,  c.time_max,  c.count_rate, 1);
color(1); xylabel(5000, 1.5, "Full band");

message("\nch= aglc(s, dt, w1, w2, g, o); % Hard band\n");
ch=aglc(s, dt, w1, w2, g, o);
message("ohplot(ch.time_min, ch.time_max, ch.count_rate, 4);");
ohplot(ch.time_min, ch.time_max, ch.count_rate, 4);
color(4); xylabel(1800, 0.4, "Hard band");

message("\ncs= aglc(s, dt, w3, w4, g, o); % Soft band\n");
cs=aglc(s, dt, w3, w4, g, o);
message("ohplot(cs.time_min, cs.time_max, cs.count_rate, 2);");
ohplot(cs.time_min, cs.time_max, cs.count_rate, 2);
color(2); xylabel(50000, 0.35, "Soft band");

mydoc(["The light curve structure is..."]);
message("print(c);\n");
print(c);

plot_pause;


yrange;xrange(-1000);
ylabel("Hardness");

hplot( c.time_min, c.time_max, ch.count_rate/cs.count_rate, 15 );

mydoc(["Form a hardness-ratio by dividing hard counts by soft, and plot:"]);
message("hplot( c.time_min, c.time_max, ch.count_rate/cs.count_rate, 15 );\n");

plot_pause;

mydoc(["There are several ways to select regions in the curve.",
       "We will select mostly non-flaring times, filter the event structure,",
       "and make phased light curves from the filtered events.",
       "A GUI-selection can be done with the slgtk module and vwhere(), like this:",
       "   l_lo = vwhere(c.time_min, ch.count_rate/cs.count_rate);",
       "isis/pgplot cursor selection can be done like this:",
       "   (x1,x2,y1,y2) = cursor_box();\n",
       "Here, we'll apply pre-computed limits."]);

%%%%  mark/set regions of probably-not-flaring, based on hardness:
%
%%% for cursor interaction, uncomment and execute:
%
% (x1,x2,y1,y2) = cursor_box();
% (x3,x4,y1,y2) = cursor_box();
% (x5,x6,y1,y2) = cursor_box();
%() = printf("x1=%.0f; x2=%.0f; x3=%.0f; x4=%.0f; x5=%.0f; x6=%.0f;\n",
%             x1, x2, x3, x4, x5, x6);
%
% saved values from cursor interaction:
%
  x1=2632; x2=12844; x3=17133; x4=38851; x5=45047; x6=60910;

 l_lo = where( (c.time_min>x1 and c.time_min<x2)
            or (c.time_min>x3 and c.time_min<x4)
            or (c.time_min>x5 and c.time_min<x6) );

%% Or use vwhere() if available:
% l_lo = vwhere(c.time_min, ch.count_rate/cs.count_rate);

l_hi = notwhere(l_lo, length(c.time_min) );  % complement of "where"

%%% overplot the selections: 
%%%

message("oplot( ((c.time_min+c.time_max)[l_lo])/2, (ch.count_rate/cs.count_rate)[l_lo],2 );");
message("oplot( ((c.time_min+c.time_max)[l_hi])/2, (ch.count_rate/cs.count_rate)[l_hi],4 );\n");

connect_points(0);pointstyle(16);
oplot( ((c.time_min+c.time_max)[l_lo])/2, (ch.count_rate/cs.count_rate)[l_lo],2 );
oplot( ((c.time_min+c.time_max)[l_hi])/2, (ch.count_rate/cs.count_rate)[l_hi],4 );
connect_points(1);

color(4); xylabel( 14000, 1.6, "Flare selections");

plot_pause;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  make phased curves for different selections:
%
% set the epoch and period.
% date obs is 25apr2000, or ~jd 2451660.

%jd0 = 2451851.26598;        % epoch (from ref in brickhouse  2001ApJ...562L..75
% gherega, farkas, & horvath 1994 ibvs 4045 1
%
% jd0 = 2443604.5887;         % their linear fit
% pd = 0.26781785;           

jd0 = 2443604.5914;    %       their preferred quadratic ephem
pd =  0.26781662;
ephem = [jd0, pd ]; 

mydoc(["To make phased light curves, we have to set an ephemeris:"]);
message("ephem = [ 2443604.5914, 0.26781662 ];  % epoch in JD; period in days");

% define  phase grid parameters:
%
dphi = (dt/86400.) / pd ;
pgrid = [0.0, 1.0, dphi];   % min, max, step (this is not a grid array);

% make phased light curve for all diffracted events (standard extraction reg):

mydoc(["To make phased light curves for the hard and soft states, ",
       "we need to filter the event structures. aglc_filter() applies the",
       "selection using information in the light curve, c, (specifically,",
       "reverse indices from the histogram function), and copies the events",
       "counted in selected bins to a new structure:"]);

% filter event struct on time selection:

message("s_lo =  aglc_filter( c, s, l_lo );  %  filter low-state");
s_lo =  aglc_filter( c, s, l_lo );


mydoc(["Phased light curves are made with the function, aglc_phased(), which is",
       "similar to aglc().  Instead of taking a time bin, it takes",
       "phase grid parameters: pgrid = [min_phase, max_phase, phase_step]."]);


% make phased curves on hi/lo filtered times:

message("p_lo = aglc_phased( s_lo, [0.0, 1.0, 0.009], ephem, 1.7, 26, g, o);   % low state\n");
p_lo = aglc_phased( s_lo, pgrid, ephem, 1.7, 26, g, o);   % full band, non-flare


%%%% Plot phased light curves

mydoc(["The result is a structure similar to the light curve structure.",
       "Here are the fields:"]);
message("print(p_lo);");
print(p_lo);
plot_pause;

mydoc(["The exposure tag is computed from the histogram of exposure numbers",
       "phase folded.  Phase gaps (such as for the flare exclusions) show",
       "as low or no exposure (bottom plot), and are properly",
       "factored into the rates."]);

message("hplot(p_lo.phase_min, p_lo.phase_max, p_lo.count_rate, 4);");
message("hplot(p_lo.phase_min, p_lo.phase_max, p_lo.exposure/1000., 4);  % scaled exp");

%%%% "wrap" a bit to show continuity w/ phase:
multiplot(3,1);
%_pgsvp(0.15,0.98, 0.2, 0.92);
label("phase", "count rate", "44 Boo Flare-filtered phase-folded light curve (top); exposure function (bottom)");
set_line_width(7);
xrange(-0.2,1.2); yrange(0,1.);
hplot(p_lo.phase_min, p_lo.phase_max, p_lo.count_rate, 2);
set_line_width(3);
ohplot(p_lo.phase_min-1, p_lo.phase_max-1, p_lo.count_rate, 14);
ohplot(p_lo.phase_min+1, p_lo.phase_max+1, p_lo.count_rate, 14);

yrange(0); ylabel("exposure [ks]");
hplot(p_lo.phase_min, p_lo.phase_max, p_lo.exposure/1000., 2);
ohplot(p_lo.phase_min-1, p_lo.phase_max-1, p_lo.exposure/1000., 14);
ohplot(p_lo.phase_min+1, p_lo.phase_max+1, p_lo.exposure/1000., 14);

plot_pause;



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% another useful plot is vs rotation.  (unfolded phase)
%%%

mydoc(["Another useful quantity is the unfolded phase, or rotational cycle.",
       "Time conversion functions help here.  E.g.:"]);

message("jdmin = aglc_tobs_to_jd(c.time_min+c.timezero, c.mjdref); % time to jd");
message("rmin  = aglc_jd_to_rotnum( jdmin, jd0, pd );              % jd to rotation number");
message("rref  = int(min(rmin));                                   % compute zero-point");
message("rmin  = rmin - rref;                                      % remove zero-point");
message("hplot( rmin, rmax, c.count_rate, 1 );                     % plot the result\n");

jdmin = aglc_tobs_to_jd(c.time_min+c.timezero, c.mjdref);
jdmax = aglc_tobs_to_jd(c.time_max+c.timezero, c.mjdref);

rmin = aglc_jd_to_rotnum( jdmin, jd0, pd );
rmax = aglc_jd_to_rotnum( jdmax, jd0, pd );
rref = int(min(rmin));
rmin = rmin - rref;
rmax = rmax - rref;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%% PLOT  vs rotation:

multiplot(3,1);
set_line_width(5);
label("Unfolded Phase", "count rate", "44 Boo Unfolded phase light curves");
yrange(0); xrange();
hplot( rmin, rmax, c.count_rate, 1 );
ohplot( rmin, rmax, cs.count_rate, 2 );
ohplot( rmin, rmax, ch.count_rate, 4 );

%%% overplot the selections: 
%%%
yrange;xrange();
ylabel("Hardness");
hplot( rmin, rmax, ch.count_rate/cs.count_rate, 15 );
connect_points(0);pointstyle(16);
oplot( ((rmin+rmax)[l_lo])/2, (ch.count_rate/cs.count_rate)[l_lo],2 );
oplot( ((rmin+rmax)[l_hi])/2, (ch.count_rate/cs.count_rate)[l_hi],4 );
connect_points(1);

plot_pause;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
mydoc(["Another view is to plot rotation mod 1 to show the phased curve,",
       "but without accumulation of repeated phases into phase bins:"]);
message("plot( (rmin+rmax)[l_lo]/2 mod 1, c.count_rate[l_lo], 2 );\n");

%%%% plot folded unfolded phase modulo 1 (i.e., not histogrammed)
multiplot(3,1);
set_line_width(5);
connect_points(0);
pointstyle(16);

label("Phase", "count rate", "44 Boo  Unfolded phase, modulo 1");
yrange(0,2); xrange(-0.2,1.2);
plot( (rmin+rmax)[l_lo]/2 mod 1, c.count_rate[l_lo], 2 );
oplot( (rmin+rmax)[l_hi]/2 mod 1, c.count_rate[l_hi], 4 );

% overplot a bit of overlapped phase for continuity assessment:
oplot( ((rmin+rmax)[l_lo]/2 mod 1)-1, c.count_rate[l_lo], 14 );
oplot( ((rmin+rmax)[l_hi]/2 mod 1)-1, c.count_rate[l_hi], 15 );
oplot( ((rmin+rmax)[l_lo]/2 mod 1)+1, c.count_rate[l_lo], 14 );
oplot( ((rmin+rmax)[l_hi]/2 mod 1)+1, c.count_rate[l_hi], 15 );


%%% plot hardness, and overplot the selections: 
%%%
yrange(0,3);xrange(-0.2,1.2);
ylabel("Hardness");
connect_points(0);pointstyle(16);
plot( ((rmin+rmax)[l_lo])/2 mod 1, (ch.count_rate/cs.count_rate)[l_lo],2 );
oplot( ((rmin+rmax)[l_hi])/2 mod 1, (ch.count_rate/cs.count_rate)[l_hi],4 );

% and a bit of phase overlap:
oplot( (((rmin+rmax)[l_lo])/2 mod 1)-1, (ch.count_rate/cs.count_rate)[l_lo], 14 );
oplot( (((rmin+rmax)[l_hi])/2 mod 1)-1, (ch.count_rate/cs.count_rate)[l_hi], 15 );
oplot( (((rmin+rmax)[l_lo])/2 mod 1)+1, (ch.count_rate/cs.count_rate)[l_lo], 14 );
oplot( (((rmin+rmax)[l_hi])/2 mod 1)+1, (ch.count_rate/cs.count_rate)[l_hi], 15 );

connect_points(1);
multiplot(1);

plot_pause;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% repeat plots for hardcopy purposes:

private define plot_all()
{
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%% plot the spectra; counts:
    %
    charsize(1.2);
    set_line_width(7);
    xrange(w1, w4); yrange(0);
    title("44 Boo HETGS Counts spectrum, (binned MEG*4,HEG*8)");
    xlabel("Wavelength");  ylabel("Counts/bin");
    hplot( get_combined(g_meg, &get_data_counts), 1);
    ohplot( get_combined(g_heg, &get_data_counts), 14);

    y0 = max(get_combined(g_meg, &get_data_counts).value) * 0.4;
    set_line_width(9);
    oplot([w1,w2], [y0,y0], 4); 
    oplot([w3,w4], [y0,y0], 2);
    set_line_width(3);
    color(4); xylabel(3,y0*1.05,"\"Hard\" band");
    color(2); xylabel(17,y0*1.05,"\"Soft\" band");


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%% PLOT  vs rotation:

    multiplot(3,1);
    set_line_width(5);
    label("Unfolded Phase", "count rate", "44 Boo HETGS, count rate vs cycle");
    yrange(0); xrange();
    hplot( rmin, rmax, c.count_rate, 1 );
    ohplot( rmin, rmax, cs.count_rate, 2 );
    ohplot( rmin, rmax, ch.count_rate, 4 );

    %%% overplot the selections: 
    %%%
    yrange;xrange();
    ylabel("Hardness");
    hplot( rmin, rmax, ch.count_rate/cs.count_rate, 15 );
    connect_points(0);pointstyle(16);
    oplot( ((rmin+rmax)[l_lo])/2, (ch.count_rate/cs.count_rate)[l_lo],2 );
    oplot( ((rmin+rmax)[l_hi])/2, (ch.count_rate/cs.count_rate)[l_hi],4 );
    connect_points(1);

    multiplot(1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%% plot unfolded phase modulo 1 (i.e., not histogrammed)
    multiplot(3,1);
    set_line_width(5);
    connect_points(0);
    pointstyle(16);

    label("Phase", "count rate", "44 Boo  Count rate vs unfolded phase");
    yrange(0,2); xrange( -0.2, 1.2 );
    plot( (rmin+rmax)[l_lo]/2 mod 1, c.count_rate[l_lo], 2 );
    oplot( (rmin+rmax)[l_hi]/2 mod 1, c.count_rate[l_hi], 4 );

    oplot( ((rmin+rmax)[l_lo]/2 mod 1)-1, c.count_rate[l_lo], 14 );
    oplot( ((rmin+rmax)[l_hi]/2 mod 1)-1, c.count_rate[l_hi], 15 );
    oplot( ((rmin+rmax)[l_lo]/2 mod 1)+1, c.count_rate[l_lo], 14 );
    oplot( ((rmin+rmax)[l_hi]/2 mod 1)+1, c.count_rate[l_hi], 15 );


    %%% overplot the selections: 
    %%%
    yrange(0,3); xrange(-0.2,1.2);
    ylabel("Hardness");
    connect_points(0);pointstyle(16);
    plot( ((rmin+rmax)[l_lo])/2 mod 1, (ch.count_rate/cs.count_rate)[l_lo],2 );
    oplot( ((rmin+rmax)[l_hi])/2 mod 1, (ch.count_rate/cs.count_rate)[l_hi],4 );

    oplot( (((rmin+rmax)[l_lo])/2 mod 1)-1, (ch.count_rate/cs.count_rate)[l_lo], 14 );
    oplot( (((rmin+rmax)[l_hi])/2 mod 1)-1, (ch.count_rate/cs.count_rate)[l_hi], 15 );
    oplot( (((rmin+rmax)[l_lo])/2 mod 1)+1, (ch.count_rate/cs.count_rate)[l_lo], 14 );
    oplot( (((rmin+rmax)[l_hi])/2 mod 1)+1, (ch.count_rate/cs.count_rate)[l_hi], 15 );

    connect_points(1);
    multiplot(1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%% phased light curve
    %%%% "wrap" a bit to show continuity w/ phase:

    multiplot(3,1);
    label("phase", "count rate", "44 Boo Flare-filtered phase-folded light curve (top); exposure function (bottom)");
    set_line_width(7);
    xrange(-0.2,1.2); yrange(0, 1.);
    hplot(p_lo.phase_min, p_lo.phase_max, p_lo.count_rate, 2);
    set_line_width(3);
    ohplot(p_lo.phase_min-1, p_lo.phase_max-1, p_lo.count_rate, 14);
    ohplot(p_lo.phase_min+1, p_lo.phase_max+1, p_lo.count_rate, 14);

    yrange(0); ylabel("exposure [ks]");
    hplot(p_lo.phase_min, p_lo.phase_max, p_lo.exposure/1000., 2);
    ohplot(p_lo.phase_min-1, p_lo.phase_max-1, p_lo.exposure/1000., 14);
    ohplot(p_lo.phase_min+1, p_lo.phase_max+1, p_lo.exposure/1000., 14);

    multiplot(1);
}


mydoc(["Make a big summary plot of all the above, and also put into",
       "a postscript file, ./outdir/LC_Demo_44_Boo.ps"]);

pw3=open_plot("/xwin", 2,2); resize(20, 0.6);
plot_all;
plot_pause;
close_plot(pw3);

pid = open_plot("./outdir/LC_Demo_44_Boo.ps/cps", 2,2);
resize(10*2.54, 8.5/11.);
charsize(1.2);
%_pgsvp(0.1, 0.98, 0.2, 0.9); 
plot_all;
_pgiden;
close_plot(pid);
window(pw1);


% write curves to files:

mydoc( ["Write curves to FITS files:"]);

message( "aglc_write_curve( \"outdir/44b_c.fits\",  s.fevt_1a,    c, \"full band\");" ) ;
message( "aglc_write_curve( \"outdir/44b_ch.fits\", s.fevt_1a,   ch, \"hard band\");" ) ; 
message( "aglc_write_curve( \"outdir/44b_cs.fits\", s.fevt_1a,   cs, \"soft band\");" ) ; 
message( "aglc_write_curve( \"outdir/44b_pc.fits\", s.fevt_1a, p_lo, \"lo state\");" ) ; 

aglc_write_curve( "outdir/44b_c.fits",  s.fevt_1a,  c, "full band");
aglc_write_curve( "outdir/44b_ch.fits", s.fevt_1a, ch, "hard band");
aglc_write_curve( "outdir/44b_cs.fits", s.fevt_1a, cs, "soft band");
aglc_write_curve( "outdir/44b_pc.fits", s.fevt_1a, p_lo, "lo state");
