#!/usr/bin/perl #@ _COMMENT_ #Created by tech @ltimas on 2017-10-06 # # **************** Program Notes *********************************** # 1. Script reads data from file and dumps into array variable. Into a multi-dim array # 2. Smooths data in the 3rd column, temperature in degrees C. Adds it to the 4th column. # 3. Takes 1st derivative of the smoothed temperature data (4th column). Adds it to the 5th column. # 4. Smooths the first derivative data (5th column data). Adds it to the 6th column. # 5. Run Algorithm to count cycles. # Cycle Count Algorithm: # a. LOOP1: Read through array. If smoothed 1st derivative is +ve then 1, else 0. # b. LOOP2: Read through array. If change from +ve to -ve a zero is found. Record a 1, else 0. # c. LOOP3: Add up all zeros. # d. Calculate the cycles by dividing by two. # ********** NOTE: ** # ********** # ******************************************************************* #@ _Version_**** #V0.0 - Created Script for ltimas.com #@ _USE_ use strict; use warnings; use Date::Parse; #@ _USE_ #__GLOBAL VAR DECLARATION our @multiDat; our ($cycles, $zeros, $last, $noRows); our $last #___ my $filename = 'TestData.txt'; open(my $fh, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!"; ####### Read file handle into 1d array, loop through array row by row, finally join and print. chomp(my @data = <$fh>); close $fh; #read data into file then close file handle $noRows=@data; #print "$noRows"; our $index = 0; foreach (@data) { my @rowArray = split(' ',$data[$index]); $multiDat[$index][0]=$rowArray[0]; $multiDat[$index][1]=$rowArray[1]; $multiDat[$index][2]=$rowArray[2]; # print join(" ", "$multiDat[$index][0]","$multiDat[$index][1]","$multiDat[$index][2]"),"\n"; $index = ++$index; } ############################################ ####### Loop through all data records, and smooth temperature data. $last = $noRows-1; #the array length must be subtracted by 1 because 0 is a data row $index = 0; foreach (@data) { if ($index==0 ) { $multiDat[$index][3]= $multiDat[$index][2]; } elsif ($index!=0 && $index!=$last) { my $avgTemp = ($multiDat[$index-1][2] + $multiDat[$index][2] + $multiDat[$index+1][2])/3; $multiDat[$index][3]= $avgTemp; } elsif ($index==$last) { $multiDat[$index][3]= $multiDat[$index][2]; } $index = ++$index; } ############################################ ####### Loop through all data records, and calculate first derivative degC/Sec $index = 0; foreach (@data) { if ($index!=$last ) { my $d1 = join(" ", "$multiDat[$index][0]", "$multiDat[$index][1]", "\n"); my $t1 = str2time($d1); my $d2 = join(" ", "$multiDat[$index+1][0]", "$multiDat[$index+1][1]", "\n"); my $t2 = str2time($d2); my $tDiff = $t2 - $t1; my $slope = ($multiDat[$index+1][3] - $multiDat[$index][3]) / $tDiff; $multiDat[$index][4]= $slope; } elsif ($index==$last) { $multiDat[$index][4]=$multiDat[$index-1][4]+($multiDat[$index-1][4]-$multiDat[$index-2][4]); } $index=++$index; } ############################################ ####### Loop through all data records, and smooth first derivative degC/Sec $index = 0; foreach (@data) { if ($index==0 ) { $multiDat[$index][5]= $multiDat[$index][4]; } elsif ($index!=0 && $index!=$last) { my $avgDeriv = ($multiDat[$index-1][4] + $multiDat[$index][4] + $multiDat[$index+1][4])/3; $multiDat[$index][5]= $avgDeriv; } elsif ($index==$last) { $multiDat[$index][5]= $multiDat[$index][4]; } $index = $index +1; } ############################################ ######## Now we have a multidimensional array with all the data we need to count cycles. BEGIN CYCLE COUNTER Algorithm LOOP1 $index = 0; foreach (@data) { if ($multiDat[$index][5] > 0) { $multiDat[$index][6] = 1; } else { $multiDat[$index][6] = 0; } $index = ++$index; } # Now find zeros in 1st derivative data. BEGIN CYCLE COUNTER Algorithm LOOP2 $index = 0; $multiDat[0][7] = 0; #initize value for first record, assume cannot be a zero foreach (@data) { if ($index!=0) { #Only search for zeros if not first if ($multiDat[$index][6] == $multiDat[$index-1][6]) { $multiDat[$index][7] = 0; } else { $multiDat[$index][7] = 1; } } $index = ++$index; } # now print out data #$index = 0; #foreach (@data) { # print join(" ", "$multiDat[$index][0]","$multiDat[$index][1]","$multiDat[$index][2]","$multiDat[$index][3]","$multiDat[$index][4]","$multiDat[$index][5]","$multiDat[$index][6]","$multiDat[$index]###[7]"),"\n"; # $index = ++$index; #} # Now add up all zeros. BEGIN CYCLE COUNTER Algorithm LOOP3 $index = $zeros = 0; foreach (@data) { if ($multiDat[$index][7] == 1) { $zeros = ++$zeros; } $index = ++$index; } #Calculate Number of cycles $cycles = $zeros * 0.5; print "$cycles\n"; ###################################### END CYCLE COUNTER