Saturday, January 17, 2015

Omega Velocity copy header from master (Perl)


#!/usr/bin/perl
# command perl velcphead.pl slave.txt master.txt

my $file1 =$ARGV[0];
open(DATA1, $file1) || die "Can't Open $file1: $!\n";
my $file2 =$ARGV[1];
open(DATA2, $file2) || die "Can't Open $file2: $!\n";

while (<DATA1>) {
chomp;
@s1 = split ; 
if (/SPNT/) #grep lines contain SPNT
{
push @cmp1, @s1[1]; 
}

if (/VELF/)
{  #begin VELF

$numel1=@s1;  #count no of elements in each line
if ($numel1==13)
{
push @datum1,$s1[2]; #just in case you need to grab cmp from slave.txt
for ($j=1; $j<=10; $j++)
{
push @sx1,$s1[2+$j];
}
}

elsif ($numel1==12)
{
for ($j=1; $j<=10; $j++)
{
push @sx1,$s1[1+$j];
}
}

elsif ($numel1<=11)
{
for ($j=1; $j<=$numel1-1; $j++)
{
push @sx1,$s1[$j];
}
}
} #end VELF
}


########  Extract header from master.txt.....................
while (<DATA2>) {
chomp;
@s2 = split ; 
if (/SPNT/)
{
push @cmp2, @s2[1]; 
push @inlin2, @s2[2];
push @xcoor2, @s2[3];
push @ycoor2, @s2[4];
push @xline2, @s2[5];
}
if (/VELF/)

$numel2=@s2; 
if ($numel2==13)
{
push @datum2,$s2[2];
}
}
}

########  Search for existing header from master.txt............
for my $i (0..$#cmp1)   #no row slave
{
for my $j (0..$#cmp2)   #no row master
{
if ($cmp1[$i] == $cmp2[$j] )
{
push @cmp12, $cmp2[$j];
push @datum12, $datum2[$j];
push @inlin12, $inlin2[$j];
push @xline12, $xline2[$j];
push @xcoor12, $xcoor2[$j];
push @ycoor12, $ycoor2[$j];
}
}
}

######### Harvesting time and vels to be col 1 and col 2..........
for ($j=0; $j<=($#sx1)/2; $j++)
{
push @time,$sx1[$j*2];   #select odd lines
push @vels,$sx1[$j*2+1]; #select even lines
}

######### Count number of elements in each cmp......................
for ($j=0; $j<=$#time+1; $j++)
{
$dt[$j]=$time[$j+1]-$time[$j];  #calculate time difference
push @dtx, $dt[$j]; 
}
my (@index) = grep $dtx[$_] < 0, 0 .. $#dtx;  #locate negative time difference i.e. new cmp

for ($j=0; $j<=$#index-1; $j++)
{
$idx[$j]=$index[$j+1]-$index[$j];#calculate no of analysis points
push @idx2,$idx[$j];
}
push @idx3, $index[0]+1, @idx2;  #calculate no of analysis points

##################################################################
###########      PRINTING  DEPT.                 #################
##################################################################

for $m (0..$#idx3)  #no cmp
{
@time1=@time[$index[$m]-($idx3[$m]-1)..$index[$m]];
@vels1=@vels[$index[$m]-($idx3[$m]-1)..$index[$m]];

printf "%s", "SPNT";
printf  "%11d",$cmp12[$m];
printf  "%10d",$inlin12[$m];
printf  "%10d",$xcoor12[$m];
printf  "%10d",$ycoor12[$m];
printf  "%10d",$xline12[$m];
print "\n";


for $k (0..$idx3[$m]/5)
{
printf "%s", "VELF";
for $i (1..5)
{
if ($vels1[$i+$k*5-1] > 0)   #exist vel
{

if ($i == 1 && $k==0) #beginning of analysis points
{
printf  "%11d",$cmp12[$m];
printf  "%5d",$datum12[$m];
printf  "%5d",$time1[$i+$k*5-1];
}

elsif ($i == 1 && $k==1 )  #row no 2 in omega format of each cmp
{
printf  "%21d",$time1[$i+$k*5-1];
}
elsif ($i == 1 ) #rest of analysis points
{
printf  "%21d",$time1[$i+$k*5-1];
}
else
{
printf  "%5d",$time1[$i+$k*5-1];
}
printf  "%5d",$vels1[$i+$k*5-1];
}
}
print "\n";
}
@time1=();
@vels1=();
}

No comments: