Reading a table data in Perl

In data analysis, we need to read the data in tabular format. We can do this easily and elegantly in Perl. (Refer to Using Perl for statistics – Giovanni Baiocchi for more)

Let us take a tabular format data in the file “longley.reg“.

The data looks like following:

X1 -52.99357014 129.54487 -.409 .6911
X2 .7107319907E-01 .30166400E-01 2.356 .0402
X3 -.4234658557 .41773654 -1.014 .3346
X4 -.5725686684 .27899087 -2.052 .0673
X5 -.4142035888 .32128496 -1.289 .2263
X6 48.41786562 17.689487 2.737 .0209

The first column is the string with two characters. The second, third, fourth and fifth column is the data of the floating type or exponential type format. The precision of this data is varying and somewhere very  high which might not be always required. We will try to round off to 3 digits after decimal point.

The Perl code for reading this data and saving it in another file (formatted_table.dat) is following:

#!/usr/bin/perl

open( TABLE, "longley.reg" );
$filename = 'formatted_table.dat';
open($h1, '>', $filename) or die "Could not open file '$filename' $!";

$prec = 3; # sets number of decimals
$width = 8; # sets the width of the field
while () {
chomp;
@line = split;
printf $h1 "%2s", $line[0]; # prints variable name
for ( $i = 1 ; $i <= $#line ; $i++ ) {
printf $h1 "%${width}.${prec}f", $line[$i]; # prints all other fields
}
print $h1 " \n"; 
}
close(TABLE);
close $h1;
print "Finished writing!\n";

The formatted output is following:

X1 -52.994 129.545  -0.409   0.691 
X2   0.071   0.030   2.356   0.040 
X3  -0.423   0.418  -1.014   0.335 
X4  -0.573   0.279  -2.052   0.067 
X5  -0.414   0.321  -1.289   0.226 
X6  48.418  17.689   2.737   0.021

This output is much more organized than the unformatted one.

Writing or Reading a text file (in Perl)

In data analysis, reading and writing a text file is essential as you can not store all your data in your program. Here, we see how can we write a text file, append to that text file and later read that file.

Writing a text file

#!/usr/bin/perl

$filename = 'filename1.txt';
open($h1, '>', $filename) or die "Could not open file '$filename' $!";
print $h1 qq[
This is my first file written by perl
This is the first line
This is the second line
];

close $h1;
print "Finished writing!\n";

Here, we wrote a set of lines in a text file named “filename1.txt”

Screenshot from 2016-10-30 14-51-30.png

Appending a file

#!/usr/bin/perl

$filename = 'filename1.txt';
open($h1, '>>', $filename) or die "Could not open file '$filename' $!";
print $h1 qq[This is the 3rd line
This is the 4th line
];

close $h1;
print "Finished writing!\n";

Here, we append few lines to the previous text file.

Screenshot from 2016-10-30 14-54-06.png

Reading a file

Reading a file is equally essential as writing a file and sometimes even more if we are just visualizing the data.

#!/usr/bin/perl

$filename = 'filename1.txt';
open(my $h1, '<:encoding(UTF-8)', $filename)
  or die "Could not open file '$filename' $!";
 
while (my $row = <$h1>) {
  chomp $row;
  print "$row\n";
}

Let us run the above script and see its output:

Screenshot from 2016-10-30 14-58-17.png

Calling SAC(Seismic Analysis Code) (in Perl)

For seismologists, using a SAC for sac data manipulation is essential (though there are few alternatives). Here, we see how can we call SAC from a perl script:

#!/usr/bin/perl
open(SAC, "| sac ") or die "Error opening sac\n";
print SAC qq[
echo on
*fg seismogram    #sample seismic signal in SAC's memory
fg sine 2 npts 2000 delta 0.01
*fg impulse npts 100 delta 0.01
bandpass bessel corner 0.1 0.3 npole 4
ppk
fft
plotsp am
save sine_fft.pdf
];
print SAC "quit\n";
close(SAC);

Example script to call SAC functions in perl

Let’s use Perl

Perl is a general purpose, high-level programming language. High-level language means, which you can use for wide variety of applications. It also has many modules available which can be used easily. It has made useful of great features from several other programming languages including C, shell script, awk, sed etc. This makes it even more efficient. It has most advanced feature of text data manipulations which makes it very useful in computational biology. It has also been used for a long time in Geophysics and there are many popular codes available in this language.

You can even use this language as a wrapper of your other group of programs which will make your program easy to use. The codes in Perl are very easy to read and understand which makes it very popular.

Let’s see some basics to get started with using Perl:

Any command can be called from the command prompt:

perl  -e           # Unix/Linux

screen-shot-2016-10-20-at-3-38-31-pm

—————————————————————————–

To change the line of  the output or the command equivalent to return command in perl :

print "\n"; #new line character
-----------------------------------------------------------------------------

Each command in perl should terminate with the semicolon, “;”.

—————————————————————————–

Commenting

Comment in a perl script starts with the “#” entry.

Multiline Comment can be done using

=begin comment
something
something=cut

—————————————————————————–

We can also input set of strings or text as follows:

print "Hello

          world\n";

screen-shot-2016-10-20-at-3-44-22-pm

—————————————————————————–

The perl script can be invoked by the first line “#!/usr/bin/perl” (if the perl executable is present in this path, else you can find the path to perl using the shell command “which perl“).

—————————————————————————–

You can even make an alert or warning sound in Perl (enter in perl script)

print "Alert or bell - \a";

Manipulating the Capital/Small letter size:

# Only W will become upper case.

$str = "\uwelcome to utpalworld.com!";

print "$str\n";

# Whole line will become capital.

$str = "\UWelcome to utpalworld.com!";

print "$str\n";

# A portion of line will become capital.

$str = "Welcome to \Uutpalworld\E.com!"; 

print "$str\n"; 

Following is a example perl script which can be run using the command:

screen-shot-2016-10-20-at-3-56-46-pm

Perl Example 1


Scalar Variables in Perl

$age = 25;             # An integer assignment

$name = "John Paul";   # A string 

$salary = 1445.50;     # A floating point

print "Age = $age\n";

print "Name = $name\n";

print "Salary = $salary\n";

print "\n";

Perl Example 2


Arrays in Perl

Perl Example 3

@ages = (25, 30, 40); 
@names = ("John Paul", "Lisa", "Kumar");

print "\$ages[0] = $ages[0]\n";
print "\$ages[1] = $ages[1]\n";
print "\$ages[2] = $ages[2]\n";
print "\$names[0] = $names[0]\n";
print "\$names[1] = $names[1]\n";
print "\$names[2] = $names[2]\n";

screen-shot-2016-10-20-at-4-08-40-pm

@var_10 = (1..10);
@var_20 = (10..20);
@var_abc = (a..z);

print "@var_10\n"; # Prints number from 1 to 10
print "@var_20\n"; # Prints number from 10 to 20
print "@var_abc\n"; # Prints number from a to z
print "Size: ",scalar @var_10,"\n"; #size of the array
print "\n";
@array = (1,2,3);
$array[50] = 4;

$size = @array;
$max_index = $#array; #maximum index

print "Size: $size\n";
print "Max Index: $max_index\n";
print "\n";

push/pop  & shift/unshift command:

Perl Example 4

#Adding and removing elements in the array
# create a simple array
@coins = ("Quarter","Dime","Nickel");
print "1. \@coins = @coins\n";

# add one element at the end of the array
push(@coins, "Penny");
print "2. \@coins = @coins\n";

# add one element at the beginning of the array
unshift(@coins, "Dollar");
print "3. \@coins = @coins\n";

# remove one element from the last of the array.
pop(@coins);
print "4. \@coins = @coins\n";

# remove one element from the beginning of the array.
shift(@coins);
print "5. \@coins = @coins\n";

print "\n";

screen-shot-2016-10-20-at-4-18-27-pm


Splice command for replacing arrays

#Replacing array elements
@nums = (1..20);
print "Before - @nums\n";

splice(@nums, 5, 5, 21..25); #splice(array,offset,length,replacement)
print "After - @nums\n";

print "\n";

screen-shot-2016-10-20-at-4-24-38-pm


We can also split the string into an array and vice-versa:

#transforming strings into arrays
# define Strings
$var_string = "Rain-Drops-On-Roses-And-Whiskers-On-Kittens";
$var_names = "Larry,David,Roger,Ken,Michael,Tom";

# transform above strings into arrays.
@string = split('-', $var_string);
@names = split(',', $var_names);

print "$string[3]\n"; # This will print Roses
print "$names[4]\n"; # This will print Michael

print "\n";

screen-shot-2016-10-20-at-4-28-53-pm

#transforming arrays to strings
# define Strings
$var_string = "Rain-Drops-On-Roses-And-Whiskers-On-Kittens";
$var_names = "Larry,David,Roger,Ken,Michael,Tom";

# transform above strings into arrays.
@string = split('-', $var_string);
@names = split(',', $var_names);

$string1 = join( '-', @string );
$string2 = join( ',', @names );

print "$string1\n";
print "$string2\n";

print "\n";

screen-shot-2016-10-20-at-4-29-38-pm


Sorting the elements of an array:

##sorting arrays
# define an array
@foods = qw(pizza steak chicken burgers);
print "Before: @foods\n";

# sort this array
@foods = sort(@foods);
print "After: @foods\n";

print "\n";

screen-shot-2016-10-20-at-4-31-19-pm


Merging two arrays:

##Merging arrays
@odd = (1,3,5);
@even = (2, 4, 6);

@numbers = (@odd, @even);

print "numbers = @numbers\n";
print "\n";

screen-shot-2016-10-20-at-4-32-56-pm


Hash Variables:

###HASH VARIABLES key/value pairs
%data = ('John Paul', 45, 'Lisa', 30, 'Kumar', 40);

print "\$data{'John Paul'} = $data{'John Paul'}\n";
print "\$data{'Lisa'} = $data{'Lisa'}\n";
print "\$data{'Kumar'} = $data{'Kumar'}\n";

print "\n";

screen-shot-2016-10-20-at-4-37-01-pm

For clarity, you can use => as an alias for , to indicate the key/value pairs as follows −

%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);

%data = (-JohnPaul => 45, -Lisa => 30, -Kumar => 40);

print "\n";

Extracting slices:

%data = (-JohnPaul => 45, -Lisa => 30, -Kumar => 40);

@array = @data{-JohnPaul, -Lisa};

print "Array : @array\n";

print "\n";

screen-shot-2016-10-20-at-4-41-34-pm

Extracting keys and values:

%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);

@names = keys %data;

print "$names[0]\n";
print "$names[1]\n";
print "$names[2]\n";

@ages = values %data;

print "$ages[0]\n";
print "$ages[1]\n";
print "$ages[2]\n";
print "\n";

Screen Shot 2016-10-20 at 4.43.28 PM.png


Adding/removing elements in Hashes:

%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
@keys = keys %data;
$size = @keys;
print "1 - Hash size: is $size\n";

# adding an element to the hash;
$data{'Ali'} = 55;
@keys = keys %data;
$size = @keys;
print "2 - Hash size: is $size\n";

# delete the same element from the hash;
delete $data{'Ali'};
@keys = keys %data;
$size = @keys;
print "3 - Hash size: is $size\n";

print "\n";

screen-shot-2016-10-20-at-4-48-18-pm


If/else loop and exist command:

%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);

if( exists($data{'Lisa'} ) ){
 print "Lisa is $data{'Lisa'} years old\n";
}
else{
 print "I don't know age of Lisa\n";
}

print "\n";

Screen Shot 2016-10-20 at 4.46.01 PM.png

Other Conditional operator

$name = "Ali";
$age = 10;

$status = ($age > 60 )? "A senior citizen" : "Not a senior citizen";

print "$name is - $status\n";

print "\n";

Screen Shot 2016-10-20 at 4.49.50 PM.png


For loop in Perl

for (my $i=0; $i <= 9; $i++) {
 print "$i\n";
}

screen-shot-2016-10-20-at-4-57-18-pm


Foreach loop in Perl

foreach my $i (0..9) {
 print "$i\n";
}

Screen Shot 2016-10-20 at 4.59.51 PM.png


While loop in Perl

$i=0;
while ($i<15) {
$i++;
print "Running while...\n";
print "i = $i\n";
}

Screen Shot 2016-10-20 at 5.02.49 PM.png


Math functions in Perl

use feature 'say';
say "EXP 1 = ", exp 1;
say "HEX 10 = ", hex 10;
say "OCT 10 = ", oct 10;
say "INT 6.45 = ", int(6.45);
say "LOG 2 = ", log 2;
say "Random between 0-10= ", int(rand(11));
say "SQRT 9= ", sqrt 9;
print "\n";

Screen Shot 2016-10-20 at 4.53.46 PM.png

-Utpal Kumar (IES, Academia Sinica)

Moment Tensor Calculations

Programs:
1. moment_tensor_calc2.pl –> it calculates the moment tensor matrix for given M0, strike, dip and rake
2. diagonalizing_matrix.pl –> this program diagonalizes the given matrix. It can be used to diagonalize the moment tensor matrix as well.
3. iso_dev_mt.pl –> it gives the isotropic and deviatoric part for a given moment tensor matrix
4. maj_min_dc.pl –> this gives the major and minor double couple as output.
5. dc_clvd_mt.pl –> this gives the double couple and clvd part as output.
6. focal_mech_info.pl –> this gives the auxiliary fault plane as output for a given main fault plane.
7. plot_focal_mech.sh –> this bash script plots the focal mechanism solution for a given strike, dip and rake. It makes use of the focal_mech_info.pl program.
Example Focal Mechanism Plot for:
Main Fault
Strike = 40 degrees
Dip = 50 degrees
Rake = 60 degrees
Click here for the plot
test

Continue reading Moment Tensor Calculations