This is one way to write a macro in perl.
Useful if you want to use perl to manipulate files in the directory, and then run sac commands on them. The sac is run all at once after the if/elsif statements are completed.
macro: A sequence of commands stored for later use in an application
# count number of files in directory
$dir = '.';
@files = <$dir/*>;
$count = @files;
print "numfiles: $count \n";
#######################
# to run sac commands, write to sac macro and run
open(scratch1,'>sacmacro');
# set initial counter
$x = 1;
foreach $fn (<*BHZ*.SAC>){
if ($x==1){
$fnout=$fn . '.merge';
print scratch1 "r $fn \n";
print "read first file: $fn \n";
$x++;
} elsif ($x < $count) {
print scratch1 "merge $fn \n";
print "merge $fn \n";
$x++;
} elsif ($x == $count) {
print scratch1 "merge $fn \n";
print scratch1 "w $fnout \n";
print "merge $fn \n";
}
} # ends the foreach
close(scratch1);
# now run the sac macro
# within the ` `, until EOF, all lines are executed in sac
`sac << EOF
m sacmacro
q
EOF`;
Monday, October 18, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment