Code:
#this makes use of csv format file "output.txt"
# MUST compensate for positive and negative totals
# logger day starts at 4pm actual time which = midnight bat time (since this script operates
# midnight to midnight) thus 16 hours are added
BEGIN { OT=0 # Time of previous measurement
MAX=0 # Max num of seconds between valid events
DAY=""; # Current day
CA=0 ; CB=0 ; CX=0 ; CD=0 # var to "hold" in or "out" A, C are one direction, B, X are the other dir
# Running total of bats leaving and entering
TOTALBATS=0;
MAXBATS=0; # The highest TOTALBATS has ever been
maxtime=0; # time of peak event
# XX = highest negative vlaue of bats - converted to positive)
XX=0;
XXX=0;
# Length of the patterns
L=4
# Patterns to check against
# Block 1 unBlock 1 block 0 Unblock 0
A[0]="1,1"; A[1]="1,0"; A[2]="0,1"; A[3]="0,0";
# Block 1 Block 0 unblock 1 Unblock 0
X[0]="1,1"; X[1]="0,1"; X[2]="1,0"; X[3]="0,0";
# Block 0 Unblock 0 Block 1 Unblock 1
B[0]="0,1"; B[1]="0,0"; B[2]="1,1"; B[3]="1,0";
# Block 0 block 1 unBlock 0 unblock 1
D[0]="0,1"; D[1]="1,1"; D[2]="0,0"; D[3]="1,0";
}
# ========================================================
function print_daily(day,inbt,outbt,maxbt,maxtime)
{
I=total; if(I<0) I=-I;
MX="no maximum"
if(maxtime > 0)
MX=sprintf("Max was %s", strftime("%H:%M:%S",maxtime)); #strftime(format, timestamp)
CZ=(CA-CB); # GET ERROR
printf("Date,%s,IN>,%d,OUT>,%d,Max count>,%d,difference between start of count (0) and end of count = %d (%s)\n",
day, inbt, outbt, maxbt, CZ, MX) > "/dev/stderr";
# printf("==") > "/dev/stderr";
# Reset daily counts
TOTALBATS=0; MAXBATS=0; maxtime=0;
CA=0; CB=0; CZ=0;
}
# end function
#====================================================================
{ # Calculate timestamp from date string
T=mktime($1 " " $2 " " $3 " " $5 " " $6 " " $7);
# T+=(60*60*16); # Add sixteen hours
$1=strftime("%Y", T); # Put these back in the strings
$2=strftime("%m", T);
$3=strftime("%d", T);
$5=strftime("%H", T);
$6=strftime("%M", T);
$7=strftime("%S", T);
# print ($5 " " $6 " " $7) " MAXBATS "MAXBATS > "/dev/stderr"; #cp
# When the year, month, and/or day changes, time to print daily counts
if((DAY != $1 "-" $2 "-" $3) && (DAY != ""))
print_daily(DAY,CA,CB,MAXBATS,maxtime); #CP
DAY=$1 "-" $2 "-" $3;
if($8 == "pv") # Ignore anything but PV lines.
{
#===============================================================
# If too much time has passed since the last event, start over.
if((T-OT) > MAX) # Blank the array
for(N=0; N<(L-1); N++) C[N]="";
else # Shift elements toward the front
for(N=0; N<(L-1); N++) C[N]=C[N+1];
OT=T # Set prev time to this one.
C[L-1]=$9 "," $10; # Set the latest event in the array
# Search for events in the array.
FOUNDA=1; FOUNDB=1;
FOUNDX=1; FOUNDD=1;
for(N=0; N<L; N++)
{
if(A[N] != C[N]) FOUNDA=0;
if(B[N] != C[N]) FOUNDB=0;
if(X[N] != C[N]) FOUNDX=0;
if(D[N] != C[N]) FOUNDD=0;
}
# Count the events and mark the hour they occurred in
if(FOUNDA || FOUNDX)
{
CA++;
printf("A@%s-%s-%s %s:%s:%s\n",$1,$2,$3,$5,$6,$7);
AH[$5]++;
TOTALBATS--;
# print($5,$6,$7) > "/dev/stderr"; #cp
}
if(FOUNDB || FOUNDD)
{
CB++;
printf("B@%s-%s-%s %s:%s:%s\n",$1,$2,$3,$5,$6,$7);
BH[$5]++;
TOTALBATS++;
}
XXX=TOTALBATS;
if (TOTALBATS<0) #cp NEXT LINES ARE USED TO GET THE ABSOLUTE VALUE OF TOTALBATS
{ XXX=TOTALBATS*TOTALBATS; #cp MULTIPLY NEGATIVE BY NEGATIVE TO GET POSITIVE
XXX=sqrt(XXX); #cp SQUT IS INBUILT SQUARE ROOT FUNCTION: BACK TO # ORIGINAL NUMBER, NOW POSITIVE
}
# Update our maximum daily counts
if(MAXBATS < XXX)
{
MAXBATS=XXX;
maxtime=(T+=(60*60*16)); # +16 HOURS TO SHOW MAX ACTUAL TIME, NOT LOGGER TIME
# i.e. midnight is actually 4pm (thus plus 16 hours)
}
print "TOTALBATS "TOTALBATS " MAXBATS "MAXBATS" maxtime "strftime("%m/%d %H:%M:%S", maxtime)" time " strftime("%m/%d %H:%M:%S", T)> "/dev/stderr"; #cp
}
}
END {
# The final statistics will be printed to stderr, to easily
# seperate them from the event times printed to stdout.
# The last daily count
print_daily(DAY,CA,CB,MAXBATS,maxtime);
# Print the event counts
### printf("A %2d\nB %2d\nT %2d\n", CA, CB, CA+CB) > "/dev/stderr";
# printf("A %2d\nB %2d\nX %2d\nD %2d\nT %2d\n", CA, CB, CX, CD, CA+CB+CX+CD) > "/dev/stderr";
# Print a list of hours from 1-23
### STR="H";
### for(N=1; N<=23; N++) STR=STR sprintf(" %2d", N);;
### print STR > "/dev/stderr";
# Print hourly counts for event A
### STR="A";
### for(N=1; N<=23; N++)
### STR=STR sprintf(" %2d", AH[sprintf("%02d", N)]);
### print STR > "/dev/stderr";
# Hourly counts for event B
### STR="B";
### for(N=1; N<=23; N++)
### STR=STR sprintf(" %2d", BH[sprintf("%02d",N)]);
### print STR > "/dev/stderr";
}