#!/usr/bin/perl
#this code is useful for merging omega velocity file (text format) at specific time (ms)
#command: perl omegavelmerge.pl file1.txt file2.txt time
my $fileaaa =$ARGV[0];
open(DATAaaa, $fileaaa) || die "Can't Open $fileaaa: $!\n";
my $filebbb =$ARGV[1];
my $waktu=$ARGV[2];
open(DATbbba, $filebbb) || die "Can't Open $filebbb: $!\n";
while (<DATAaaa>) {
chomp;
@saaa = split ; #split DATAaaa into lines
#####################################
if (/SPNT/) #grep lines contain VSTKT
{
push @inlinall, @saaa[3]; #push into inlinaaae
push @xlineall, @saaa[2];
}
################index SPNT###########
if (/VELF/)
{ #begin VELF
#####################################
$numelaaa=@saaa; #count no of elements in each line
if ($numelaaa==13)
{
push @cmpall,$saaa[1];
push @datumall,$saaa[2];
for ($j=1; $j<=10; $j++)
{
push @sxaaa,$saaa[2+$j];
}
}
elsif ($numelaaa==12)
{
for ($j=1; $j<=10; $j++)
{
push @sxaaa,$saaa[1+$j];
}
}
elsif ($numelaaa<=11)
{
for ($j=1; $j<=$numelaaa-1; $j++)
{
push @sxaaa,$saaa[$j];
}
}
} #end VELF
}
while (<DATbbba>) {
chomp;
@sbbb = split ; #split DATbbba into lines
#####################################
if (/SPNT/) #grep lines contain VSTKT
{
push @inlinbbb, @sbbb[2]; #push into inlinbbbe
push @xlinebbb, @sbbb[3];
}
################index SPNT###########
if (/VELF/)
{ #begin VELF
#####################################
$numelbbb=@sbbb; #count no of elements in each line
if ($numelbbb==13)
{
push @cmpbbb,$sbbb[1];
push @datumbbb,$sbbb[2];
for ($j=1; $j<=10; $j++)
{
push @sxbbb,$sbbb[2+$j];
}
}
elsif ($numelbbb==12)
{
for ($j=1; $j<=10; $j++)
{
push @sxbbb,$sbbb[1+$j];
}
}
elsif ($numelbbb<=11)
{
for ($j=1; $j<=$numelbbb-1; $j++)
{
push @sxbbb,$sbbb[$j];
}
}
} #end VELF
}
###########################################################
$numelaaa2=@sxaaa;
for ($j=0; $j<=($numelaaa2-1)/2; $j++)
{
if ($sxaaa[$j*2] < $waktu) #time you want to merge
{
push @timeaaa,$sxaaa[$j*2]; #select odd lines
push @velsaaa,$sxaaa[$j*2+1]; #select even lines
}
}
$numelaaa3=@timeaaa;
for ($j=0; $j<=$numelaaa3; $j++)
{
$dtaaa[$j]=$timeaaa[$j+1]-$timeaaa[$j]; #calculate timeaaa difference
push @dtxaaa, $dtaaa[$j]; #push
}
my (@indexaaa) = grep $dtxaaa[$_] < 0, 0 .. $#dtxaaa; #locate negative timeaaa difference i.e. new cmpaaa
$numelaaa4=@indexaaa;
for ($j=0; $j<=$numelaaa4-2; $j++)
{
$idxaaa[$j]=$indexaaa[$j+1]-$indexaaa[$j]; #calculate no of analysis points
push @idx2aaa,$idxaaa[$j];
}
push @idx3aaa, $indexaaa[0]+1, @idx2aaa; #calculate no of analysis points
$numelbbb2=@sxbbb;
for ($j=0; $j<=($numelbbb2-1)/2; $j++)
{
if ($sxbbb[$j*2] >= $waktu) #time you want to merge
{
push @timebbb,$sxbbb[$j*2]; #select odd lines
push @velsbbb,$sxbbb[$j*2+1]; #select even lines
}
}
$numelbbb3=@timebbb;
for ($j=0; $j<=$numelbbb3; $j++)
{
$dtbbb[$j]=$timebbb[$j+1]-$timebbb[$j]; #calculate timebbb difference
push @dtxbbb, $dtbbb[$j]; #push
}
my (@indexbbb) = grep $dtxbbb[$_] < 0, 0 .. $#dtxbbb; #locate negative timebbb difference i.e. new cmpbbb
$numelbbb4=@indexbbb;
for ($j=0; $j<=$numelbbb4-2; $j++)
{
$idxbbb[$j]=$indexbbb[$j+1]-$indexbbb[$j]; #calculate no of analysis points
push @idx2bbb,$idxbbb[$j];
}
push @idx3bbb, $indexbbb[0]+1, @idx2bbb; #calculate no of analysis points
$numelabidx=@idx3aaa;
for ($j=0; $j<=$numelabidx-1; $j++)
{
push @timeall, @timeaaa[$indexaaa[$j]-($idx3aaa[$j]-1)..$indexaaa[$j]], @timebbb[$indexbbb[$j]-($idx3bbb[$j]-1)..$indexbbb[$j]];
push @velsall, @velsaaa[$indexaaa[$j]-($idx3aaa[$j]-1)..$indexaaa[$j]], @velsbbb[$indexbbb[$j]-($idx3bbb[$j]-1)..$indexbbb[$j]];
}
$numelall3=@timeall;
for ($j=0; $j<=$numelall3; $j++)
{
$dtall[$j]=$timeall[$j+1]-$timeall[$j]; #calculate timeall difference
push @dtxall, $dtall[$j]; #push
}
my (@indexall) = grep $dtxall[$_] < 0, 0 .. $#dtxall; #locate negative timeall difference i.e. new cmpall
$numelall4=@indexall;
for ($j=0; $j<=$numelall4-2; $j++)
{
$idxall[$j]=$indexall[$j+1]-$indexall[$j]; #calculate no of analysis points
push @idx2all,$idxall[$j];
}
push @idx3all, $indexall[0]+1, @idx2all; #calculate no of analysis points
$numelall5=@idx3all;
for $m (0..$numelall5-1) #no cmpall
{
@timeall1=@timeall[$indexall[$m]-($idx3all[$m]-1)..$indexall[$m]];
@velsall1=@velsall[$indexall[$m]-($idx3all[$m]-1)..$indexall[$m]];
printf "%s", "SPNT";
printf "%11d",$cmpall[$m];
printf "%20d",$xlineall[$m];
printf "%10d",$inlinall[$m];
print "\n";
for $k (0..($idx3all[$m]-1)/5)
{
printf "%s", "VELF";
for $i (1..5)
{
if ($velsall1[$i+$k*5-1] > 0) #exist vel
{
if ($i == 1 && $k==0) #beginning of analysis points
{
printf "%11d",$cmpall[$m];
printf "%5d",$datumall[$m];
printf "%5d",$timeall1[$i+$k*5-1];
}
elsif ($i == 1 && $k==1 ) #row no 2 in omega format of each cmpall
{
printf "%21d",$timeall1[$i+$k*5-1];
}
elsif ($i == 1 ) #rest of analysis points
{
printf "%21d",$timeall1[$i+$k*5-1];
}
else
{
printf "%5d",$timeall1[$i+$k*5-1];
}
printf "%5d",$velsall1[$i+$k*5-1];
}
}
print "\n";
}
@timeall1=();
@velsall1=();
}
No comments:
Post a Comment