#!/usr/local/bin/perl -w # # mega2.p, derived from whata.p and mega.p # reads two nfiles # and finds matching events (same c) so that we can plot their time diffs # to measure 'a', and also so that we can chop the file into megabursts. # # mega2.p n=What_a/n530 o=What_a/n2.$tag > What_a/d530 $file1 = "What_a/n.18B" ; $file2 = "What_a/n.19B" ; $ccol = 5 ; # which col c is found in. (in nfile) $changed = 20.0 ; # value beyond which delta t is judged to # have changed $history = 6 ; $n = "no_old_nfile" ; # where to read original n file. $o = "no_new_nfile" ; # where to write the modified n file. $d = "dfile" ; # where to write the time differences $out = 1 ; # whether to write the modified n file $verbose = 0 ; eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; $ccol -- ; # zero offset! $tcol = $ccol + 1 ; # column with time in it $bcol = $ccol - 1 ; # the burst number column. for ( $h = 0 ; $h <= $history ; $h ++ ) { $dt[$h] = 0.0 ; $c[$h] = 0 ; $b[$h] = 0 ; } $megaburst = 0 ; open ( B1 , "<$file1" ) ; open ( B2 , "<$file2" ) ; open ( D , ">$d" ) ; $c1 = 0 ; $c2 = 0 ; $b1 = 0 ; $b2 = 0 ; $ldiff = 0 ; &read1 ; &read2 ; $l = 0 ; while ( $c1 && $c2 ) { if ( $c1 == $c2 ) { $l ++ ; printf D "%8.3g %8.3g %8.3g %4d\n", $t1-$t2, $t1, $t2, $c1 ; $dt[0] = $t1-$t2 ; $c[0] = $c1 ; $b[0] = $burst1 ; # look for a change $same = 0 ; $diff = 0 ; for ( $h = $history ; $h >= 1 ; $h -- ) { $ddt = $dt[$h] - $dt[0] ; if ( abs($ddt) <= $changed ) { # then it is the same as a piece of history $same ++ ; } else { $diff ++ ; # } } if ( $diff >= $same ) { # this is an outlier, or a change. # check most recent line with an outlier / change if ( $ldiff && ( $ldiff == $l - 1 ) ) { if ( !$ldiff2 || ( $ldiff2 != $l - 2 ) ) { print STDERR "new block, burst $b[1],\t line $ldiff.\t c=$c1, \tcpast=$c[$history]..$c[2],$c[1].\n" ; $megaburst ++ ; $blist[$megaburst] = $b[1] ; } $ldiff2 = $ldiff ; } $ldiff = $l ; } # shuffle history for ( $h = $history ; $h >= 1 ; $h -- ) { $dt[$h] = $dt[$h-1] ; $c[$h] = $c[$h-1] ; $b[$h] = $b[$h-1] ; } &read1 ; &read2 ; } elsif ( $c1 > $c2 ) { while ( $c2 && ($c1 > $c2) ) { &read2 ; } } else { while ( $c1 && ( $c1 < $c2 ) ) { &read1 ; } } } print STDERR "wrote $l lines to the time-difference file, read $b1 and $b2 lines from the antenna-specific files\n" ; sub read1 { if(defined($_ = )) { $b1 ++ ; # s/^\s+//g ; @a = split ; $c1 = $a[$ccol] ; $t1 = $a[$tcol] ; $burst1 = $a[$bcol] ; } else { $c1 = 0 ; } return $c1 ; } sub read2 { if(defined($_ = )) { $b2 ++ ; # s/^\s+//g ; @a = split ; $c2 = $a[$ccol] ; $t2 = $a[$tcol] ; } else { $c2 = 0 ; } return $c2 ; } # =============================================== # # this is the end of the whata.p part. # # =============================================== if (!$out) { exit(0) ; } if ( $verbose ) { printf STDERR "New burst numbers\n" ; foreach $bbb ( 1..$megaburst ) { printf STDERR "$blist[$bbb] upwards -> $bbb\n" ; } } # carry out the substitutions of b in nfile open ( N , "<$n" ) ; open ( NO , ">$o" ) ; if (!defined(N) || !defined(NO)) { exit(0) ; } $p = 0 ; while( defined($_ = ) ) { s/^\s+//g ; @a = split ; $b = $a[$bcol] ; $bbb = 1 ; while ( ($bbb < $megaburst) && ($b >= $blist[$bbb+1]) ) { $bbb ++ ; } $a[$bcol] = $bbb ; print NO join ( "\t" , @a ) , "\n" ; $p ++ ; } print STDERR "\n grouped bursts together \n" ; print STDERR "-B $megaburst -N $p \n" ;