Tuesday, December 1, 2020

Table of Contents

Seismic Fundamental
AAA
Aliasing
Amplitude
Angle mute
Angle stack
Anisotropic NMO
Anisotropic parameters
Anisotropy
Anisotropy classification
Artificial Neural Network
Attenuation
Auto correlation
Bowtie
Brute Stack
CDP
Converted Waves
Crooked Line
Curvelet Travsform
Dip Moveout
Dephasing
Detectability and Fidelity
Diffraction
Dogleg
Fermat principle
First Break
Frequency
Fresnel Zone
Gain
Groundroll
Higher order NMO
Hockey Stick
Huygens principle
Interval velocity
Prism wave
P wave velocity
Love wave
Migration
Migration aperture
Mistie
Multiple
NMO
Near offset far offset
Noise and data
Non zero apex
P wave
Polarity
PSDM
PSTM
Preserve Amplitude
Prewhitening
Q Migration
RMS Velocity
Ray Tracing
Reflectivity
Seismic Resolution
SELMA
SRME
Seismic record
Seismic reference datum
Seismic refraction
Seismic Refraction and ray tracing
Seismic tomography
Seismic trace
Seismic multicomponent
Semblance
Shear wave
Shear wave splitting
Slant Stack
Source signature
Spectral balancing
Spectral broadening
Spectral decomposition
Spectral ratio method
Spike
Stacking
Static correction
Super gather
Sweep vibroseis
TWT
Tau-p
Time to depth conversion
True amplitude recovery
Tuning thickness
Tuning Cube
Velocity Analysis
Velocity Dispersion
Vertical seismic resolution
Vp/Vs
Water column static
Wavelet
Wavelet polarity onshore
Zero crossing


Seismic Advanced
Anisotropic PSDM
Controlled Beam Migration
Full wave inversion
Reverse time migration


Signal Processing
Bandwidth
Convolution
Cross Correlation
dB/Octave
DC Component
Deconvolution
Dynamic range and BIT
Fourier Transform
Filtering
Gabor Transform
Hilbert Transform

Main lobe side lobe
Minimum phase zero phase
Multi Taper Method
Nyquist frequency


Seismic Acquisition, Well Logging, Drilling and Non Seismic
2D Marine Acquisition
3D Land survey
3D Seismic acquisition QC
4D Seismic
Checkshot survey
Gamma ray log
Horizontal Seismic Profiling
Pore Pressure Prediction
Remote Resistivity Mapping
Resistivity Log
Rich Azimuth Seismic 
Seismoelectric 
Kelly Bushing
Marine Analog Survey
Marine Geohazard survey
Neutron porosity
VSP
Wide Azimuth Seismic


Seismic Interpretation and Petroleum Geology
Bottom Simulating Reflector
Coal Bed Methane Seismic
Curvature
Envelope
Instantaneous Frequency 1
Instantaneous Frequency 2
Instantaneous phase 1
Instantaneous phase 2
Optical Stacking
Perigram
Perigram x Cosines phase
Petroleum System
Reflection Strength
Seismic Attribute
Seismic Facies 
Texture Attribute 
Unconformity
Velocity sag
Volume Assessment
Well seismic tie


Seismic Processing with Seismic UNIX (Basic)
1. Seismic Unix Installation
2. Data Download, Segy2SU, QC
3. Kill traces and gain
4. Groundroll Removal
5. Bandpass Filter and Decon
6. Geometry and CMP Sorting
7. NMO and Brute Stack
8. Velocity Analysis and Stack
9. Statics
10. Migration (STOLT)


Seismic Processing with Seismic UNIX (Advanced)
Attribute Seismic
Continuous Wavelet Transform
FX Decon
Interactive velocity analysis 
Mute Design
PreStack Time Migration
PreStack Depth Migration
PostStack Depth Migration
Radon Demultiple
Ray Tracing
Super gather 
Synthetic Gathers Generator
Velocity Medeling
VSP Processing
Waveform modeling


Reservoir Characterization
AVA
AVFVO
AVO Attributes
AVO Azimuth
AVO Classification
Acoustic Impedance
Anisotropic AVO
Aki and Richards Approximation
Coherence
Continuous Wavelet Transform
Elastic Impedance
Extended Elastic Impedance
Gassmann Fluid Substitution
Faust Equation
Intercept and Gradient AVO
Inversion Maximum Likelihood
Inversion Sparse Spike
Lambda Mu Rho
Mobil Approximation
Poisson Impedance
Recursive Inversion 
Reflection Impedance
Rock density
Rock Physic Analysis
Seismic Inversion
Seismic Multiattribute
Shuey Approximation
Smith and Gidlow 
Thinbed reflectivity inversion
Tuning Cube
Zoeppritz Elastic Impedance
Zoeppritz Equation


Computational Geophysics
Bresenham Algorithm
Madagascar
Marquardt Inversion
Matlab for horizon snap
SVD 1
SVD 2
SVDLib C
Seislab
SEGY2ASCII Matlab


Perl
Introduction to Perl
Hello World
Bit Reverse
Call GNUPLOT from Perl
Compare files 
Left of right justify
Matrix Multiplication (subroutine) 
Matrix Transpose
Print n-times according to integer in a specific column
Read text file and print on console
Read and write matrix file
Select Columns
Select rows 
Sort by key field
Sort by key fields
DEC2IEEE


Perl for Geoscientists
Ascii to SEGY
Automatic First Break Picks
Boundary Detection 
Compare two files and print closest values
Compare two files and search match columns
Copy header from another omega velocity file
Convert CGG to Omega velocity format
Discrete Fourier Transform 
Edit omega velocity file
Merge two omega velocity files
SEGY Header Dumper trace by trace
SEGY Header Dumper min-max 
SEGY Header Dumper user input 
SEGY to Ascii 
SEGD (Revision 1) to Ascii 
Spectrum and RAAC Editor (GUI) 
Smoothing with convolution
SPS File Generator
Text to SEGY header
XPS File Generator


C Language for Geoscientists
Hello World
Fahrenheit to Celsius Conversion
Progress indicator
Read input from keyboard entry and print on console
Read input from file perform operation and print on console
Read file perform operation and output file
Addition function
Temperature conversion function
Matrix addition function with keyboard entry
Matrix addition function load file
Matrix multiplication
Matrix transpose
Matrix identity
Matrix Inverse
Inverse Matrix with Marquardt method
Matrix operation with malloc
Matrix transpose with malloc
Fast Fourier Transform (FFT)
Format
SEGY Header Dumper



Miscellaneous
Animated GIF
AWK 1
AWK 2
AWK 3
BP Velocity Benchmark
Data Marine download
GNU Plot  
GNU Plot transparent filled polygon
IBM and IEEE
Promax 1
Promax 2
Promax 3
Promax 4
SEGY
Unix for geoscientists
VI

Saturday, May 16, 2015

Spectrum and RAAC Editor (Perl-GUI)

Kode Perl di bawah ini merupakan kode untuk melakukan edit Spectrum atau RAAC dengan Graphical User Interface (GUI).

Untuk GUI saya menggunakan Perl/Tk, yang pada Ubuntu bisa diinstall dengan perintah:

sudo apt-get install perl-tk
 
Terdapat dua format untuk RAAC yakni Time-RAAC dan Offset RAAC yakni Text Omega dengan posisi Time, Offset dan RAAC pada lokasi tertentu. Program ini memilki kemampuan membaca dan menulis dengan format tersebut (lihat gambar di bawah ini:

 
Format Omega Time-RAAC

.  
Format Offset-RAAC

Sedangkan Spectrum merupakan kolom:

Format Spectrum: kolom pertama (frekuensi), kedua (amplitudo), ketiga (fasa). 

Berikut adalah tampilan GUI untuk melakukan edit spectrum. Kurva merah adalah spectrum input, mouse pick (titik-titik magenta) merupakan guide untuk interpolasi. Antara mouse-pick tersebut di-interpolasi dengan metoda spline atau linear dengan moving average smoothing. Banyaknya sample diantara dua mouse-pick ditentukan dengan spline-steps.


GUI dengan Spline


GUI dengan Linear Interpolation dan Moving Average Smoothing



Berikut adalah Kode Perl GUI di atas:

#!/usr/bin/perl
use Tk;
use Tk::ProgressBar;
my $mw = MainWindow->new( -title => 'RAAC and Spectrum Editor Ver 1.0 by Agus Abdullah, PhD ' );

$mw->withdraw();
$mw->transient();
$mw->Popup();
my $titiks = [];
push(@$titiks, []);
my $menu_bar = $mw->Menu;
my $vw = $mw->vrootwidth/2;
my $vh = $mw->vrootheight/2;

### Progress Bar
my $percent_done=0;
my $Progress = $mw->ProgressBar(-width =>5,-from => 0,-to => 100,-blocks => 50,    -colors => [0, 'green', 50, 'yellow' , 80, 'red',],-variable => \$percent_done)->pack(-fill => 'x');

### canvas
$mw->configure( -menu => $menu_bar );
my $canvas = $mw->Canvas(-width => $vw+100, -height => $vh+100, -background => "white");


### Menu
my $file_mb = $menu_bar->cascade( -label => '~File', -tearoff => 0 );
$file_mb->command( -label => 'Open T-RAAC', -underline => 0, -command => \&f_open );
$file_mb->command( -label => 'Open O-RAAC', -underline => 0, -command => \&f_open_oraac);
$file_mb->command( -label => 'Open Spectrum', -underline => 0, -command => \&f_open_spec);
$file_mb->separator;
$file_mb->command( -label => 'Exit', -underline => 1, -command => sub{exit} );
$canvas->Tk::bind("<Button-1>", [ \&append_point, Ev('x'), Ev('y') ]);

my $help_mb = $menu_bar->cascade( -label => '~About', -tearoff => 0 );
$help_mb->command( -label => 'About', -underline => 0, -command => \&f_help );

### button
my $lab = $mw -> Label(-text=>"Spline Steps: ");
my $scl = $mw->Entry( -width => 8, -textvariable => '200');
my $spline = $mw->Button(-width => 8, -height => 1,-text => 'Run Spline',-command => \&fspline);
my $lab2 = $mw -> Label(-text=>"Smooth Window: ");
my $scl2 = $mw->Entry( -width => 8, -textvariable => '20');
my $spline2 = $mw->Button(-width => 8, -height => 1,-text => 'Run Linear',-command => \&flinear);
my $clear = $mw->Button(-width => 8, -height => 1,-text => 'Re-Pick',-command => \&fclear);
my $export1 = $mw->Button(-width => 15, -height => 1,-text => 'Print Time-RAAC',-command => \&fexport1);
my $export2 = $mw->Button(-width => 15, -height => 1,-text => 'Print Offset-RAAC',-command => \&fexport2);
my $export3 = $mw->Button(-width => 15, -height => 1,-text => 'Print Spectum',-command => \&fexport3);
my $exit = $mw->Button(-width => 8, -height => 1,-text => 'Exit',-command => [$mw => 'destroy']);

### packing
$canvas->pack;
$lab->pack(-side=>'left');
$scl->pack(-side=>'left');
$spline->pack(-side=>'left');
$lab2->pack(-side=>'left');
$scl2->pack(-side=>'left');
$spline2->pack(-side=>'left');
$clear->pack(-side=>'left');
$export1->pack(-side=>'left');
$export2->pack(-side=>'left');
$export3->pack(-side=>'left');
$exit->pack(-side=>'left');

MainLoop;

### pick points
sub f_help {
$mw-> messageBox(-title => 'About', -message => 'RAAC and Spectrum Editor Ver 1.00 contact: Agus Abdullah, aguabdullah@kockw.com for help ', -type=>'ok');
}


### read time raac format input
sub f_open {
### delete vars if any
@x=();
@y=();
@b=();
@sesu1=();
@spl=();
@xclick=();
@yclick=();
$ax2=();
$ax=();
$ay=();
$nopoints=();
$ax2o=();
$ayo=();
@xori=();
@yori=();
$percent_done=();
@data2=();
$xori2=();
@hasil=();
@data1=();
$dx=();
$dz=();
@dza=();
@dzt=();
@dzt2=();
@dzt3=();
@time=();
@raac=();
$kadal=();
$dhor=();
$dxy=();
@bx1=();
@row=();
@spec=();
@xspl=();
@yspl=();
@a1=();
@a2=();
@data=();
@avea=();
@xtra=();
@fbave=();
@yspl2=();

$junk=$canvas->createText(10,10, -text => a,-fill=>'white');
@items = $canvas->find('enclosed', $canvas->bbox('all') );
for($i=0;$i<=$#items;$i++)
{
  $canvas->delete($items[$i]);
}

my $filename = $mw->getOpenFile( -title => 'Open File:', -defaultextension => '.txt', -initialdir => '.' );
open (FILE, $filename); 
while (<FILE>) {
chomp;
if (/RACC/)
{
$s=$_;
$time1=substr($s,11,4);
$raac1=substr($s,15,6);
$time2=substr($s,26,4);
$raac2=substr($s,30,6);
$time3=substr($s,41,4);
$raac3=substr($s,45,6);
$time4=substr($s,56,4);
$raac4=substr($s,60,6);
push @x,$time1,$time2,$time3,$time4;
push @y,$raac1,$raac2,$raac3,$raac4;
}
}

### convert x,y into canvas pixel coordinates
for my $i (0..$#x-1)
{
$x1[$i]=(($x[$i]-min(@x))*($vw/(max(@x)-min(@x))))+50;
$y1[$i]=(($y[$i]-max(@y))*($vh/(min(@y)-max(@y))))+50;
push @b, $x1[$i], $y1[$i];
push @bx1,$x1[$i];
}
close( FILE );


### plot time raac input
$canvas->createLine(\@b, -fill=> 'red');  #plot curve
$canvas->createLine(40,20, $vw+80,20,$vw+80,$vh+80, 40, $vh+80, 40,20, -fill=> 'grey'); #plot box

$dhor=(max(@x)-min(@x))/10;
for my $i (0..10)
{
$canvas->createLine((($dhor*$i-min(@x))*($vw/(max(@x)-min(@x))))+50,20,(($dhor*$i-min(@x))*($vw/(max(@x)-min(@x))))+50,$vh+80, -fill=> 'grey', -dash => '-'); #plot grid
$canvas->createText((($dhor*$i-min(@x))*($vw/(max(@x)-min(@x))))+50,10, -text => sprintf("%.0f", $dhor*$i)); #plot box grid
}

$dxy=(max(@y)-min(@y))/5;
for my $i (0..5)
{
$canvas->createLine(40, ((min(@y)+($i*$dxy)-max(@y))*($vh/(min(@y)-max(@y))))+50, $vw+80, ((min(@y)+($i*$dxy)-max(@y))*($vh/(min(@y)-max(@y))))+50, -fill=> 'grey', -dash => '-');
$canvas->createText(20,((min(@y)+($i*$dxy)-max(@y))*($vh/(min(@y)-max(@y))))+50 ,-text => sprintf("%.2f", min(@y)+($i*$dxy)) ); #plot grid
}
print 'Time RAAC data loaded...Start to pick using Mouse Button 1 !',"\n";
}


### read offset raac format input
sub f_open_oraac {
### delete vars if any
@x=();
@y=();
@b=();
@sesu1=();
@spl=();
@xclick=();
@yclick=();
$ax2=();
$ax=();
$ay=();
$nopoints=();
$ax2o=();
$ayo=();
@xori=();
@yori=();
$percent_done=();
@data2=();
$xori2=();
@hasil=();
@data1=();
$dx=();
$dz=();
@dza=();
@dzt=();
@dzt2=();
@dzt3=();
@time=();
@raac=();
$kadal=();
$dhor=();
$dxy=();
@bx1=();
@row=();
@spec=();
@xspl=();
@yspl=();
@a1=();
@a2=();
@data=();
@avea=();
@xtra=();
@fbave=();
@yspl2=();
$junk=$canvas->createText(10,10, -text => a,-fill=>'white');
@items = $canvas->find('enclosed', $canvas->bbox('all') );

for($i=0;$i<=$#items;$i++)
{
  $canvas->delete($items[$i]);
}

my $filename = $mw->getOpenFile( -title => 'Open File:', -defaultextension => '.txt', -initialdir => '.' );
open (FILE, $filename); 
while (<FILE>) {
chomp;
if (/RACF/)
{
$s=$_;
$offset=substr($s,36,4);
push @x, $offset;
}

if (/RACC/)
{
$s=$_;
$raac=substr($s,15,6);
$sesu=substr($s,11,4); ### what the hack is this?
push @y, $raac;
push @sesu1, $sesu;
}

}
###### desired for line plot
for my $i (0..$#x-1)
{
$x1[$i]=(($x[$i]-min(@x))*($vw/(max(@x)-min(@x))))+50;
$y1[$i]=(($y[$i]-max(@y))*($vh/(min(@y)-max(@y))))+50;
push @b, $x1[$i], $y1[$i];
push @bx1,$x1[$i];
}
close( FILE );

### plot offset raac input
$canvas->createLine(\@b, -fill=> 'red');
$canvas->createLine(40,20, $vw+80,20,$vw+80,$vh+80, 40, $vh+80, 40,20, -fill=> 'grey'); #plot box

$dhor=(max(@x)-min(@x))/10;
for my $i (0..10)
{
$canvas->createLine((($dhor*$i-min(@x))*($vw/(max(@x)-min(@x))))+50,20,(($dhor*$i-min(@x))*($vw/(max(@x)-min(@x))))+50,$vh+80, -fill=> 'grey', -dash => '-'); #plot grid
$canvas->createText((($dhor*$i-min(@x))*($vw/(max(@x)-min(@x))))+50,10, -text => sprintf("%.0f", $dhor*$i)); #plot box grid
}

$dxy=(max(@y)-min(@y))/5;
for my $i (0..5)
{
$canvas->createLine(40, ((min(@y)+($i*$dxy)-max(@y))*($vh/(min(@y)-max(@y))))+50, $vw+80, ((min(@y)+($i*$dxy)-max(@y))*($vh/(min(@y)-max(@y))))+50, -fill=> 'grey', -dash => '-');
$canvas->createText(20,((min(@y)+($i*$dxy)-max(@y))*($vh/(min(@y)-max(@y))))+50 ,-text => sprintf("%.2f", min(@y)+($i*$dxy)) ); #plot grid
}
print 'Offset RAAC data loaded...Start to pick using Mouse Button 1 !',"\n";
}


### read spectrum
sub f_open_spec {
### delete vars if any
@x=();
@y=();
@b=();
@sesu1=();
@spl=();
@xclick=();
@yclick=();
$ax2=();
$ax=();
$ay=();
$nopoints=();
$ax2o=();
$ayo=();
@xori=();
@yori=();
$percent_done=();
@data2=();
$xori2=();
@hasil=();
@data1=();
$dx=();
$dz=();
@dza=();
@dzt=();
@dzt2=();
@dzt3=();
@time=();
@raac=();
$kadal=();
$dhor=();
$dxy=();
@bx1=();
@row=();
@spec=();
@xspl=();
@yspl=();
@a1=();
@a2=();
@data=();
@avea=();
@xtra=();
@fbave=();
@yspl2=();

$junk=$canvas->createText(10,10, -text => a,-fill=>'white');
@items = $canvas->find('enclosed', $canvas->bbox('all') );
for($i=0;$i<=$#items;$i++)
{
  $canvas->delete($items[$i]);
}

my $filename = $mw->getOpenFile( -title => 'Open File:', -defaultextension => '.txt', -initialdir => '.' );
open (FILE, $filename); 
while( <FILE> ) {
chomp;
my @row = split;
push @spec, \@row;
}
close( FILE );


for my $i (0..$#spec)
{
push @x, $spec[$i][0];
push @y, $spec[$i][1];
}

### convert x,y into canvas pixel coordinates
for my $i (0..$#x-1)
{
$x1[$i]=(($x[$i]-min(@x))*($vw/(max(@x)-min(@x))))+50;
$y1[$i]=(($y[$i]-max(@y))*($vh/(min(@y)-max(@y))))+50;
push @b, $x1[$i], $y1[$i];
push @bx1,$x1[$i];
}
close( FILE );


### plot time raac input
$canvas->createLine(\@b, -fill=> 'red');  #plot curve
$canvas->createLine(40,20, $vw+80,20,$vw+80,$vh+80, 40, $vh+80, 40,20, -fill=> 'grey'); #plot box

$dhor=(max(@x)-min(@x))/10;
for my $i (0..10)
{
$canvas->createLine((($dhor*$i-min(@x))*($vw/(max(@x)-min(@x))))+50,20,(($dhor*$i-min(@x))*($vw/(max(@x)-min(@x))))+50,$vh+80, -fill=> 'grey', -dash => '-'); #plot grid
$canvas->createText((($dhor*$i-min(@x))*($vw/(max(@x)-min(@x))))+50,10, -text => sprintf("%.0f", $dhor*$i)); #plot box grid
}

$dxy=(max(@y)-min(@y))/5;
for my $i (0..5)
{
$canvas->createLine(40, ((min(@y)+($i*$dxy)-max(@y))*($vh/(min(@y)-max(@y))))+50, $vw+80, ((min(@y)+($i*$dxy)-max(@y))*($vh/(min(@y)-max(@y))))+50, -fill=> 'grey', -dash => '-');
$canvas->createText(20,((min(@y)+($i*$dxy)-max(@y))*($vh/(min(@y)-max(@y))))+50 ,-text => sprintf("%.2f", min(@y)+($i*$dxy)) ); #plot grid
}
print 'Spectrum data loaded...Start to pick using Mouse Button 1 !',"\n";
}


### pick points
sub append_point {
    my ($canvas, $x, $y) = @_;
    my ($canvx, $canvy) = ($canvas->canvasx($x), $canvas->canvasy($y));
    $canvas->createOval($canvx-3, $canvy-3, $canvx+3, $canvy+3, -tags => 'point', -fill => 'magenta');
    push @xclick,$canvx;
    push @yclick,$canvy;
}



### spline
sub fspline {
$ax2=();
$ax=();
$ay=();
@spl=();
$ax2o=();
$ayo=();
@xori=();
@yori=();
$percent_done=();
@xspl=();
@yspl=();
@a1=();
@a2=();
@data=();
@avea=();
@xtra=();
@fbave=();
@yspl2=();
####



$canvas->delete($line2);

if ( min(@xclick) > min(@bx1))
{
print 'Splining is in progress be patient...However X coord of the first pick must be less than the minimum of X coord input, Re-pick',"\n";
}elsif ( max(@xclick) < max(@bx1))
{
print 'Splining is in progress be patient...However X coord of the last pick must be greater than the maximum of X coord input, Re-pick!',"\n";
} else
{
print 'Splining is in progress be patient...picks are OK',"\n";
}

$dnum=$scl->get();
$nopoints=$#xclick+1;
for($i=0;$i<$nopoints;$i++)
{
  $a[$i] = $yclick[$i];
}

for($i=0;$i<$nopoints;$i++)
{
  $h[$i] = $xclick[$i+1]-$xclick[$i];
}

for($i=1;$i<$nopoints-1;$i++)
{
  $al[$i] = ($yclick[$i+1]-$yclick[$i])*3/$h[$i] - ($yclick[$i]-$yclick[$i-1])*3/$h[$i-1];
}

$l[0] = 1;
$mu[0] = 0;
$z[0] = 0;

for($i=1;$i<$nopoints-1;$i++)
{
 $l[$i] = 2*($xclick[$i+1]-$xclick[$i-1])-$h[$i-1]*$mu[$i-1];
 $mu[$i] = $h[$i]/$l[$i];
 $z[$i] = ($al[$i]-$h[$i-1]*$z[$i-1])/$l[$i];
}

$l[$nopoints-1] = 1;
$z[$nopoints-1] = 0;
$c[$nopoints-1] = 0;

for($j=$nopoints-1;$j>=0;$j--)
{
  $c[$j] = $z[$j]-$mu[$j]*$c[$j+1];
  $b[$j] = ($yclick[$j+1]-$yclick[$j])/$h[$j]-$h[$j]*($c[$j+1]+2*$c[$j])/3;
  $d[$j] = ($c[$j+1]-$c[$j])/(3*$h[$j]);
}

$i=0;
for($i=0;$i<$nopoints-1;$i++)
{
  for($j=0;$j<$dnum;$j++)
  {
    $ax2 = ($xclick[$i+1]-$xclick[$i])/$dnum*$j+$xclick[$i];
    $ax = $ax2-$xclick[$i];
    $ay = $a[$i]+$b[$i]*$ax+$c[$i]*$ax**2+$d[$i]*$ax**3;
    push @spl,$ax2,$ay;
    $ax2o=(($ax2-50)/($vw/(max(@x)-min(@x))))+min(@x);
    $ayo=(($ay-50)/($vh/(min(@y)-max(@y))))+max(@y);
    push @xori,$ax2o;
    push @yori,$ayo;
  }
    $percent_done = $i/(($nopoints-2)/100);
    $mw->update;
}

$line2 = $canvas->createLine(\@spl, -fill=> 'blue');
print 'Splining is  done!',"\n";

}



### linear interp
sub flinear {
####### clear memory
$percent_done=();

$a=();
$h=();
$al=();
$l=();
$mu=();
$z=();
$c=();
$b=();
$d=();
$ax2=();
$ax=();
$ay=();
@xspl=();
@yspl=();
@avea=();
$ave=();
@fbave=();
$fbave=();
@data=();
@a1=();
@a2=();
@xtra=();
@yspl2=();
@spl=();
$ax2o=();
$ayo=();
@xori=();
@yori=();

#############

$canvas->delete($line2);

if ( min(@xclick) > min(@bx1))
{
print 'Splining is in progress be patient...However X coord of the first pick must be less than the minimum of X coord input, Re-pick',"\n";
}elsif ( max(@xclick) < max(@bx1))
{
print 'Splining is in progress be patient...However X coord of the last pick must be greater than the maximum of X coord input, Re-pick!',"\n";
} else
{
print 'Splining is in progress be patient...picks are OK',"\n";
}

$dnum=$scl->get();
$nopoints=$#xclick+1;
for($i=0;$i<$nopoints;$i++)
{
  $a[$i] = $yclick[$i];
}

for($i=0;$i<$nopoints;$i++)
{
  $h[$i] = $xclick[$i+1]-$xclick[$i];
}

for($i=1;$i<$nopoints-1;$i++)
{
  $al[$i] = ($yclick[$i+1]-$yclick[$i])*3/$h[$i] - ($yclick[$i]-$yclick[$i-1])*3/$h[$i-1];
}



$l[0] = 0;
$mu[0] = 0;
$z[0] = 0;



for($i=1;$i<$nopoints-1;$i++)
{
 $l[$i] = 2*($xclick[$i+1]-$xclick[$i-1])-$h[$i-1]*$mu[$i-1];
 $mu[$i] = $h[$i]/$l[$i];
 $z[$i] = ($al[$i]-$h[$i-1]*$z[$i-1])/$l[$i];
$l[$i] =0;
$mu[$i] = 0;
 $z[$i]=0;

}

$l[$nopoints-1] = 0;
$z[$nopoints-1] = 0;
$c[$nopoints-1] = 0;

for($j=$nopoints-1;$j>=0;$j--)
{
  $c[$j] = $z[$j]-$mu[$j]*$c[$j+1];
  $b[$j] = ($yclick[$j+1]-$yclick[$j])/$h[$j]-$h[$j]*($c[$j+1]+2*$c[$j])/3;
  $d[$j] = ($c[$j+1]-$c[$j])/(3*$h[$j]);
}

$i=0;
for($i=0;$i<$nopoints-1;$i++)
{
  for($j=0;$j<$dnum;$j++)
  {
    $ax2 = ($xclick[$i+1]-$xclick[$i])/$dnum*$j+$xclick[$i];
    $ax = $ax2-$xclick[$i];
    $ay = $a[$i]+$b[$i]*$ax+$c[$i]*$ax**2+$d[$i]*$ax**3;
    push @xspl,$ax2;
    push @yspl,$ay;
  }
    $percent_done = $i/(($nopoints-2)/100);
    $mw->update;
}





$window=$scl2->get();

#### padding
for ( $i = 0; $i < $window; $i++ )
{
push @a1,$yspl[0];
push @a2,$yspl[$#yspl];
}

push @data,@a1,@yspl,@a2;



##### moving average ####################
for ($i=0; $i<=$#data; $i++)
{
for ($k=0; $k<=$window; $k++)
{
push @{$fbave[$i]},$data[$i+$k];
}
$ave[$i]=mean(@{$fbave[$i]});
push @avea, $ave[$i];
}
push @xtra, $_ for 1..($window/2);
push @xtra,@avea;


for ($i=$window; $i<=$#data-$window; $i++)
{
push @yspl2, $xtra[$i];
}


for ($i=0; $i<=$#xspl; $i++)
{

push @spl,$xspl[$i],$yspl2[$i];

$ax2o[$i]=(($xspl[$i]-50)/($vw/(max(@x)-min(@x))))+min(@x);
$ayo[$i]=(($yspl2[$i]-50)/($vh/(min(@y)-max(@y))))+max(@y);
push @xori,$ax2o[$i];
push @yori,$ayo[$i];
}



$line2 = $canvas->createLine(\@spl, -fill=> 'green');
print 'Splining is  done!',"\n";
}


### min max subroutine
sub max {
    splice(@_, ($_[0] > $_[1]) ? 1 : 0, 1);
    return ($#_ == 0) ? $_[0] : max(@_);
}

sub min {
    splice(@_, ($_[0] > $_[1]) ? 0 : 1, 1);
    return ($#_ == 0) ? $_[0] : min(@_);
}


### clear for re-pick
sub fclear {
@items = $canvas->find('enclosed', $canvas->bbox('all') );
for($i=36;$i<=$#items;$i++)
{
$canvas->delete($items[$i]);
}

### delete vars
@spl=();
@xclick=();
@yclick=();
$ax2=();
$ax=();
$ay=();
$nopoints=();
$ax2o=();
$ayo=();
@xori=();
@yori=();
$percent_done=();
@data2=();
$xori2=();
@hasil=();
@data1=();
$dx=();
$dz=();
@dza=();
@dzt=();
@dzt2=();
@dzt3=();
@time=();
@raac=();
$kadal=();
$dhor=();
$dxy=();
@row=();
@spec=();
@xspl=();
@yspl=();
@a1=();
@a2=();
@data=();
@avea=();
@xtra=();
@fbave=();
@yspl2=();
}


### export time raac
sub fexport1{
print 'Printing...!',"\n";
@time=();
@raac=();
@data2=();
@dzt=();
@dzt2=();
@dzt3=();
$kadal=();
@hasil=();

for my $i (0..$#xori)
{
$xori2[$i]= int($xori[$i]) + ($xori[$i] != int($xori[$i]));  ## rounded up
push @{$hasil[$i]},$xori2[$i],$yori[$i];
}

### find uniq values
my %seen;
my @data2 = grep { !$seen{$_->[0]}++ } @hasil;



### find match X coords
for my $i (0..$#x)
{
push @{$data1[$i]},$x[$i],$y[$i];
}
for my $i (0..$#data1)   #no row slave
{
for my $j (0..$#data2)   #no row master
{
$dx[$i]=($data1[$i][0]-$data2[$j][0])*($data1[$i][0]-$data2[$j][0]);  #key1 match
$dz[$i]=$dx[$i]; #distance
push @dza,$dz[$i];
}
push @dzt,$dza[minindex(\@dza)];
push @dzt2,$data2[minindex(\@dza)][0];  #get data col 1
push @dzt3,$data2[minindex(\@dza)][1];  #get data col 2
@dza=();
}



for my $i (0..$#data1-1)
{
push @time, $data1[$i][0];
push @raac, $dzt3[$i];
}

###Printing Department  ;)
$kadal=($#time-1)/4;
my $mn = int($kadal) + ($kadal != int($kadal));
for $k (0..$mn)
{
printf "%s", "RACC";
{
for $i (1..4)
{
if ($raac[$i+$k*4-1] > 0)   # search for exist raac
{


if ($i == 1 && $k==0)
{
printf  "%11d",$time[$i+$k*4-1];
}

elsif ($i == 1 && $k==1 )  #
{
printf  "%11d",$time[$i+$k*4-1];
}

elsif ($i == 1 )
{
printf  "%11d",$time[$i+$k*4-1];
}

else
{
printf  "%9d",$time[$i+$k*4-1];
}


if ($raac[$i+$k*4-1] < 1)
{
printf  "%6.4f",$raac[$i+$k*4-1];
}else
{
printf  "%6.3f",$raac[$i+$k*4-1];
}
}
}
print "\n";
}

}
}


sub mean { # mean of values in an array
  my $sum = 0 ;
  foreach $x (@_) {
    $sum += $x ;
  }
  return $sum/@_ ;
}

#### export offset raac
sub fexport2{
@time=();
@raac=();
@data2=();
@dzt=();
@dzt2=();
@dzt3=();
$kadal=();
@hasil=();

print 'Printing...!',"\n";

for my $i (0..$#xori)
{
$xori2[$i]= int($xori[$i]) + ($xori[$i] != int($xori[$i])); ## round up
push @{$hasil[$i]},$xori2[$i],$yori[$i];
}
####find uniq values
my %seen;
my @data2 = grep { !$seen{$_->[0]}++ } @hasil;


###find match X
for my $i (0..$#x)
{
push @{$data1[$i]},$x[$i],$y[$i];
}


for my $i (0..$#data1)   #no row slave
{
for my $j (0..$#data2)   #no row master
{
$dx[$i]=($data1[$i][0]-$data2[$j][0])*($data1[$i][0]-$data2[$j][0]);  #key1 match
$dz[$i]=$dx[$i]; #distance
push @dza,$dz[$i];
}
push @dzt,$dza[minindex(\@dza)];
push @dzt2,$data2[minindex(\@dza)][0];  #get data col 1
push @dzt3,$data2[minindex(\@dza)][1];  #get data col 2
@dza=();
}


for my $i (0..$#data1)
{
push @time, $data1[$i][0];
push @raac, $dzt3[$i];
}


###Printing Department
for $i (0..$#time)
{
printf "%s", "RACF SOURCE_DETECT_DIST";
printf  "%17d",$time[$i];
printf  "%5s","1";
printf  "%5s","1";
print "\n";
printf "%s", "RACC";
printf  "%11d",$sesu1[$i];


if ($raac[$i] < 1)
{
printf  "%5.4f",$raac[$i];
}else
{
printf  "%6.3f",$raac[$i];
}
print "\n";
}
}


### export spectrum
sub fexport3{
$mn=();
print 'Print Spectrum After Spline...',"\n";
my $mn = int($#xori/$#x) + (($#xori/$#x) != int($#xori/$#x));
for ($i = 0; $i <= $#xori; $i += $mn)
{
if ($xori[$i] < 0)
{
}else
{
printf  "%7.2f",$xori[$i],"\t";
printf  "%10.3f",$yori[$i],"\t";
printf  "%5.0f",'0',"\t";
print  "\n";
}
}
}

sub minindex {
  my( $aref, $idx_min ) = ( shift, 0 );
  $aref->[$idx_min] < $aref->[$_] or $idx_min = $_ for 1 .. $#{$aref};
  return $idx_min;
}