1#!/usr/bin/perl
2
3# open(D, "./ntpclient -d -r < test2.dat|") || die;
4open(D, "awk '{\$3=\$3-800;print \$0}' ~/scratch/xfer/time/log.ir30gw.h | ./ntpclient -d -r|") || die;
5
6open(GC,"|gnuplot") || die;
7$oldfh=select(GC); $|=1; select($oldfh);
8print GC "$ARGV[0]\n";
9shift(@ARGV);
10
11$rawplot=0;
12
13sub closeout {
14	print LN "$maxxs $maxxo\n";
15	print LX "$maxxs $maxxo\n";
16	close MN;
17	close MX;
18	close RN;
19	close RX;
20	close LN;
21	close LX;
22	close LP;
23	if ($rawplot) { $cmd = "plot \"rawmax.dat\", \"rawmin.dat\"$xplots$nplots\n"; }
24	      # else    { $cmd = "plot \"max.dat\", \"min.dat\", \"rawmax.dat\", \"rawmin.dat\", \"loop.dat\"\n"; }
25	      else    { $cmd = "plot \"max.dat\", \"min.dat\", \"loop.dat\" with lines, 0.9*x>0.3?-(x-0.3)*6000:-x*600, 0.9*x<-0.3?-(x+0.3)*6000:-x*600\n"; }
26	
27	if ($use) {
28		print GC $cmd || die;
29		$user=<>;
30		print GC $user;
31	}
32	$use=0;
33	$nplots="";
34	$xplots="";
35}
36
37sub bound_write {
38	($FD, $slope, $offset) = @_;
39	if ($FD eq "MX" && $slope < $maxxs && $slope > $minxs) {
40		print MN "$slope $offset\n" || die;
41	}
42	if ($FD eq "MN" && $slope < $maxxs && $slope > $minxs) {
43		print MN "$slope $offset\n" || die;
44	}
45}
46$first=1;
47$use=0;
48$nplots="";
49$xplots="";
50$scale=0.001;    # make raw and envelope fit on same scale
51$scale=1.0  if ($rawplot);
52while(<D>) {
53	chomp();
54	@A=split();
55	if (/^contemplate/) {
56		if (!$first) { closeout(); }
57		$first=0;
58		open(MN, ">min.dat")     || die;
59		open(MX, ">max.dat")     || die;
60		open(RN, ">rawmin.dat")  || die;
61		open(RX, ">rawmax.dat")  || die;
62		open(LN, ">loopmin.dat") || die;
63		open(LX, ">loopmax.dat") || die;
64		open(LP, ">loop.dat")    || die;
65	}
66	if (/^minseg/) {$use=1; $nplots .= ", $A[2]*x+$A[4]"; print MN "$A[2] $A[4]\n" || die; }
67	if (/^maxseg/) {$use=1; $xplots .= ", $A[2]*x+$A[4]"; print MX "$A[2] $A[4]\n" || die; }
68	if (/^mincro/) {$use=1; $xplots .= ", $A[1]*x+$A[3] title 'mincross'"; $minxs=$A[1]; $minxo=$A[3]; print LN "$A[1] $A[3]\n"; print LX "$A[1] $A[3]\n";}
69	if (/^maxcro/) {$use=1; $nplots .= ", $A[1]*x+$A[3] title 'maxcross'"; $maxxs=$A[1]; $maxxo=$A[3]; }
70	if (/^interp/ && $A[2]>$minxs && $A[2]<$maxxs) {$use=1; print LN "$A[2] $A[3]\n"; print LX "$A[2] $A[4]\n";}
71	if (/^hist/)   { $st=$A[2]*$scale; print RN "$st $A[4]\n"; print RX "$st $A[5]\n";}
72	if (/^loop/)   { print LP "$A[1] $A[2]\n"; }
73	if (/^xontemplate/ || /^delta_f/ || /^find_df_center/) { print "$_\n"; }
74}
75closeout();
76