Hi,
Im trying to count bats flying through an infrared beam array. One of the experts here helped me a few months ago but now I am having a problem that is stumping me.
here is the original code that works (with two differnt patterns in array):
# this has been changed to operate under the assumption that the 1st beam
# opens and closes before next beam opens due to spacing between beams being increased.
BEGIN { OT=0 # Time of previous measurement
MAX=1 # Max num of seconds between valid events
DAY=""; # Current day
CA=0 ; CB=0
# Running total of bats leaving and entering
TOTALBATS=0;
# The highest TOTALBATS has ever been
MAXBATS=0;
# Length of the patterns
L=4
# Patterns to check against
# Block 1 unBlock 0 block 0 Unblock 0
A[0]="1,1"; A[1]="1,0"; A[2]="0,1"; A[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";
function print_daily(day,total,max,min,maxtime)
{
I=total; if(I<0) I=-I;
MX="no maximum"
if(maxtime > 0)
MX=sprintf("peak was at %s", strftime("%H:%M:%S",maxtime));
# printf("COUNT@%s COUNT %+d RET %d LEFT %d GUESS %d (%s)\n",
printf("Date @%s COUNT %+d IN %d OUT %d Estimate %d bats(%s)\n",
day, total, max, -min, I, MX) > "/dev/stderr";
# Reset daily counts
TOTALBATS=0; MAXBATS=0; MINBATS=0; MAXTIME=0;
MINTIME=0;
}
{ # 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);
# When the year, month, and/or day changes, time to print daily counts
if((DAY != $1 "-" $2 "-" $3) && (DAY != ""))
print_daily(DAY,TOTALBATS,MAXBATS,MINBATS,MAXTIME);
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;
for(N=0; N<L; N++)
{
if(A[N] != C[N]) FOUNDA=0;
if(B[N] != C[N]) FOUNDB=0;
}
# Count the events and mark the hour they occurred in
if(FOUNDA)
{
printf("A@%s-%s-%s %s:%s:%s\n",$1,$2,$3,$5,$6,$7);
CA++; AH[$5]++;
TOTALBATS++;
}
if(FOUNDB)
{
printf("B@%s-%s-%s %s:%s:%s\n",$1,$2,$3,$5,$6,$7);
CB++; BH[$5]++;
TOTALBATS--;
}
# Update our maximum daily counts
if(MAXBATS < TOTALBATS)
{
MAXBATS=TOTALBATS;
MAXTIME=T;
}
if(MINBATS > TOTALBATS)
{
MINBATS=TOTALBATS;
MINTIME=T;
}
}
}
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,TOTALBATS,MAXBATS,MINBATS,MAXTIME);
# Print the event counts
printf("A %2d\nB %2d\nT %2d\n", CA, CB, CA+CB) > "/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";
}
I have added two more patterns below to the array (C and D) and I need to add C to pattern A counts, and D to pattern B counts etc. how can I do this?
# additional Patterns to check against
# Block 1 Block 0 unblock 1 Unblock 0
A[0]="1,1"; A[1]="0,1"; A[2]="1,0"; A[3]="0,0";
# Block 1 Unblock 1 Block 0 Unblock 0
C[0]="1,1"; C[1]="1,0"; C[2]="0,1"; C[3]="0,0";
# Block 0 block 1 unBlock 0 unblock 1
B[0]="0,1"; B[1]="1,1"; B[2]="0,0"; B[3]="1,0";
# Block 0 Unblock 0 Block 1 Unblock 1
D[0]="0,1"; D[1]="0,0"; D[2]="1,1"; D[3]="1,0";
here is a small sample of the input file:
2011,07,19,Rx,00,10,28,pv,1,1
2011,07,19,Rx,00,10,28,pv,1,0
2011,07,19,Rx,00,10,28,pv,0,1
2011,07,19,Rx,00,10,28,pv,0,0
2011,07,19,Rx,00,10,28,pv,0,1
2011,07,19,Rx,00,10,28,pv,1,1
2011,07,19,Rx,00,10,28,pv,0,0
2011,07,19,Rx,00,10,28,pv,1,0
2011,07,19,Rx,00,10,29,pv,1,1
2011,07,19,Rx,00,10,29,pv,0,1
2011,07,19,Rx,00,10,29,pv,1,0
2011,07,19,Rx,00,10,29,pv,0,0
2011,07,19,Rx,00,36,57,pv,0,1
2011,07,19,Rx,00,36,57,pv,0,0
2011,07,19,Rx,00,36,57,pv,1,1
2011,07,19,Rx,00,36,57,pv,1,0
2011,07,19,Rx,00,37,10,pv,1,1
2011,07,19,Rx,00,37,10,pv,0,1
2011,07,19,Rx,00,37,10,pv,1,0
2011,07,19,Rx,00,37,10,pv,0,0
2011,07,19,Rx,00,41,31,pv,0,1
2011,07,19,Rx,00,41,31,pv,1,1
2011,07,19,Rx,00,41,31,pv,0,0
2011,07,19,Rx,00,41,31,pv,1,0
thanks