% Aug. 18, 2011
%
% Load the main package.

require("sitar.sl");

% First define some simple plotting routines that I will use
% throughout.  

%%%%%%%

public define asm_xyhplot(xlab,ylab,name,append,xlo,xhi,y,yerr,
                             erron,linlog,plotit,pause)
{
   if(plotit)
   {
      variable id = open_plot(strtrans(name," ","")+append+".ps/vcps");
      resize(25,0.9);
   }
   label(xlab,ylab,strup(name));

   charsize(1.2); set_line_width(3);

   if(linlog){ xlog; } else { xlin; }

   hplot(xlo,xhi,y);

   if(erron)
   {
      linestyle(4);
      line_or_color(0);
      linestyle(4);
      set_line_width(2);
      ohplot(xlo,xhi,y+yerr);
      linestyle(4);
      ohplot(xlo,xhi,y-yerr);
      line_or_color(1);
      linestyle(1);
   }

   if(plotit) close_plot(id);
   if(pause)  plot_pause;

   return;
}

public define asm_xyplot(xlab,ylab,name,append,x,y,linlog,plotit,pause)
{
   if(plotit)
   {
      variable id = open_plot(strtrans(name," ","")+append+".ps/vcps");
      resize(25,0.9); 
   }
   label(xlab,ylab,strup(name));

   charsize(1.2); set_line_width(3);

   if(linlog) { xlog; } else { xlin; }

   plot(x,y);

   if(plotit) close_plot(id);
   if(pause)  plot_pause;

   return;
}
%%%%%%%


variable ev, rb, pf, ef;
variable fp, name, n, file;

% Open the file with the names of the ASM files.  NOTE: This list can
% change on the ASM web page, so it's safest to generate your own

fp = fopen("asm_files","r");

% Keep reading lines of the source name file, until it's empty

while(-1 != fgets(&file,fp))
{

% Strip off extraneous carriage returns from the read

   file = strtrim(file);

% Strip off the front and end of the file name to get the source name

   (name,n) = strreplace(file,"xa_","",1);
   (name,n) = strreplace(name,"_d1.lc","",-1);

% Read the data, then rebin it to 7 days binning

   ev = sitar_readasm(file;minchi2=1.3,mjd);
   rb = sitar_rebin_rate(ev.time,7.,ev.rate;minbin=7,delgap);

   xrange(); yrange();
   asm_xyhplot("Time (MJD)","ASM Rate (cps)",name,"_rate",
                  rb.bin_lo,rb.bin_hi,rb.rate,,0,0,1,0);

% Epoch fold with 500 logarithmic periods from 5 minutes to 500 days.
% Plot on logarithmic and linear x-axes

   ef = sitar_epfold_rate(ev.time,ev.rate,0.0037,500.;nsrch=500,loggrid);

   xrange(); yrange();
   asm_xyplot("Trial Period","L Statistic",name,"_eflg",
              ef.prds,ef.lstat,1,1,0);

   xrange(); yrange();
   asm_xyplot("Trial Period","L Statistic",name,"_efln",
              ef.prds,ef.lstat,0,1,0);
}

() = fclose(fp);



