program stifftst;  {reads stiffness test file and averages distance jumps
To use this program, the data columns must include CycleCount/n, Y_force,
A_dist-Y, B_dist-Y, and Status.  Further, status reporting must be 
modified in the Host ltposix.c to read:
data->quicktime.recent[recentIndex][kChannelTweezStatus] =
		(float) (data->instrument.runMode     * 1000 
		+ data->quicktime.byteFromTrapA[mode] * 100
		+ data->quicktime.byteFromTrapB[mode] * 10
		+ data->instrument.stiffTestHighA     * 2
		+ data->instrument.movingUpward       * 1  );
Also the new variable data->instrument.stiffTestHighA  must be defined 
in ltdata.h and set/reset in the TestStiffness function of ltcontrol.c 
by stating    data->instrument.stiffTestHighA = flip          }
uses crt;
const riseTime = 5; {number of lines before position in Hi/Lo pulse stabilizes}
var  i,j,k,row,L,column, Status,ii, jj,
     CycleCntCol,YforceCol,AdistYCol,BdistYCol,StatusCol:integer;
     fileName,dataLine,dataWord:string;
     inFile, outFile:text;
     APsdY, APsdSum,BPsdY,BPsdSum,Yforce,AdistY,BdistY,CycleCnt,CycleCntSum,
     YforceSum,AdistYSum,BdistYSum: real;
     pastStatus:array[1..riseTime] of integer;
     pulseAvg:array[0..100,0..5] of real;
                {CycleCnt,YforceAv, AdistAv, BdistAv, status, k}

procedure openInputFile;
begin
clrscr;
writeln;
writeln('What text file to open?  ".TXT" added automatically');
readln(filename);
assign(infile,filename+'.txt'); 
row:=0;
reset(infile);
end;

Procedure detectColumns; {finds columns with correct data for stiffness test}
begin
repeat readln(infile,dataLine) until length(dataLine) > 40;{skip introduction}
i:=0; column:=1; L:=length(dataLine); dataWord:=''; status:=0;
repeat
      if dataLine[i]=chr(9) then  begin inc(column); dataWord:=''; end;
      repeat
          inc(i);
          dataWord:=dataWord + dataLine[i];
      until (i=L) or (dataLine[i+1]=chr(9));
      {writeln(outFile,column,'  ',dataWord);}
      if dataWord='CycleCount/n' then CycleCntCol := column;
      if dataWord='Y_force' then YforceCol := column;
      if dataWord='A_dist-Y' then AdistYCol := column;
      if dataWord='B_dist-Y' then BdistYCol := column;
      if dataWord='Status' then StatusCol := column;
      inc(i);
until (i>L) or keypressed;    {done parsing row}
writeln;
if  CycleCntCol = 0 then writeln('CycleCount/n column missing');
if  YforceCol = 0 then writeln('Y_force column missing');
if  AdistYCol = 0 then writeln('A_dist-Y column missing');
if  BdistYCol = 0 then writeln('B_dist-Y column missing');
if  StatusCol = 0 then writeln('Status column missing');
if  (CycleCntCol * YforceCol * AdistYCol *  BdistYCol * StatusCol = 0)
    then begin
         writeln('program aborted.....press any key to exit');
         repeat until keypressed;
         halt;
         end;
end;

procedure openOutputFile;
begin
assign(outFile,filename+'.out');
rewrite(outFile);
writeln;
writeln('Writing to output file "'+filename+'.out"');
writeln; writeln; writeln('cycleTime =');
writeln(outFile,'CycleCount':11,'yForce(pN)':11,'distA(nm)':11,
      'distB(nm)':11,'HiPulses':11,'LoPulses':11);
end;

procedure averageTheLines; {average line by line and sort results into pulses}
begin
for j:= riseTime downto 2 do pastStatus[j]:=pastStatus[j-1];
pastStatus[1]:=status;
if status = pastStatus[riseTime]   {in steady part of pulse because we skipped 5 lines}   then begin
        YforceSum:=YforceSum+Yforce;
        AdistYSum:=AdistYSum+AdistY;
        BdistYSum:=BdistYSum+BdistY;
        inc(k);
        end
   else if k>0 then  {at start of new pulse, time to average previous pulse}
        begin
        pulseAvg[ii,0]:= CycleCnt;
        pulseAvg[ii,1]:=-YforceSum/k ;
        pulseAvg[ii,2]:= AdistYSum/k;
        pulseAvg[ii,3]:= BdistYSum/k;
        pulseAvg[ii,4]:= pastStatus[riseTime];
        pulseAvg[ii,5]:= k;
{        writeln(ii,pulseAvg[ii,0]:8:0,pulseAvg[ii,1]:8:0,
             pulseAvg[ii,2]:8:0,pulseAvg[ii,3]:8:0,'  k=',k,'  status=',pulseAvg[ii,4]:0:0);}
        inc(ii);
        k:=0 ;
        YforceSum:=0;
        AdistYSum:=0;
        BdistYSum:=0;
        end;
end;

procedure summarize;  {writes summary of contiguous pulse group}
var n,nLo,nHi,HiLoDigit:integer;
    sumForceHi, sumForceLo, sumAdistHi,sumAdistLo,sumBdistHi,sumBdistLo:real;
begin
 dec(ii); {gets rid of one extra in procedure above}
 sumForceHi:=0; sumForceLo:=0; sumAdistHi:=0;
 sumAdistLo:=0; sumBdistHi:=0; sumBdistLo:=0;
 CycleCntSum:=0;
 nHi:=0; nLo:=0; jj:=0;
for n:=1 to ii do if (pulseAvg[n,5]>20)  then
  begin
  HiLoDigit:=(round(pulseAvg[n,4]) mod 4) div 2; {pick out the "2" added to Status}
  if HiLoDigit = 0 then
       begin
       sumForceLo:=sumForceLo+pulseAvg[n,1];
       sumAdistLo:=sumAdistLo+pulseAvg[n,2];
       sumBdistLo:=sumBdistLo+pulseAvg[n,3];
       inc(nLo);
       end;
  if HiLoDigit = 1 then
       begin
       sumForceHi:=sumForceHi+pulseAvg[n,1];
       sumAdistHi:=sumAdistHi+pulseAvg[n,2];
       sumBdistHi:=sumBdistHi+pulseAvg[n,3];
       inc(nHi);
       end;
  CycleCntSum:=CycleCntSum + pulseAvg[n,0];
  inc(jj);
   end;
if ((jj>0) and (nHi>0) and (nLo>0)) then
   begin
   write (outFile,  CycleCntSum/jj:11:0);
   write (outFile,(sumForceHi/nHi+sumForceLo/nLo)/2:11:1);
   write (outFile,(sumAdistHi/nHi-sumAdistLo/nLo):11:1);
   write (outFile,(sumBdistLo/nLo-sumBdistHi/nHi):11:1);
   write(outFile,nHi:11,nLo:11);
   writeln(outFile);
   writeln(CycleCntSum/jj:11:0);   {to CRT screen}
   end;
ii:=0;
end;

begin  {main}
openInputFile;
detectColumns;
openOutputFile;
row:=0;
   repeat
   inc(row);
   repeat readln(infile,dataLine) until (length(dataLine)>20){skips "Skipped"}
          or EOF(infile) or keyPressed;
   i:=0; column:=1; L:=length(dataLine); dataWord:=''; status:=0;
      repeat
        if dataLine[i]=chr(9) then  begin inc(column); dataWord:=''; end;
        repeat
          inc(i);
          dataWord:=dataWord + dataLine[i];
        until (i=L) or (dataLine[i+1]=chr(9));
        if column = CycleCntCol then val(dataword,CycleCnt,j);
        if column = YforceCol then val(dataword,Yforce,j);
        if column = AdistYCol then val(dataword,AdistY,j);
        if column = BdistYCol then val(dataword,BdistY,j);
        if column = statusCol then val(dataword,Status,j);
        inc(i);
      until (i>L) or keypressed;    {done parsing row}
   if (status div 1000 = 4) and (ii<101) {runMode=4 for stiffness test}
      then averageTheLines
      else if ii >0 {prevent div/zero error}
           then summarize;
   until eof(infile) or keypressed;
close(outFile);
end.