#! /usr/local/bin/perl pipe(read_1,write_1); # open pipe 1 pipe(read_2,write_2); # open pipe 2 # unbuffer all i/o on the pipes $old_h=select(read_1); $|=1; select(read_2); $|=1; select(write_1); $|=1; select(write_2); $|=1; select($old_h); $|=1; if ($pid=fork) { # the parent do not do anything here} # so close off the read_1 and write_2 # hence wirte to write_1 and get results off read_2 $old_h=select(read_2); $|=1; select(write_1); $|=1; select($old_h); close(read_1); close(write_2); } elsif (defined $pid) { # this is the child so... # close off read_2 and write_1 # redirect stdin to read_1 and stdout to write_2 # then start up the work code $old_h=select(read_1); $|=1; select(write_2); $|=1; select($old_h); close(read_2); close(write_1); close(STDOUT); open(STDOUT,">& write_2"); select(STDOUT); close(STDIN); open(STDIN,"<& read_1"); close(read_1); sleep(1); system("/data/02/steve/mag_stuff/calc_mag"); die "ending child\n"; } # read in cnss catalog # find the locations, magnitude, and amp lines # store the info for locataion and magnitude # amp lines calc the new amps. # for this test calculate ms and mb and see if # the averages is close to the preferred mag $amp_loc=45; # amp_type in $amp line $mag_loc=10; # mag_type in $mag line print write_1 "ARC LZ 6.654 0.0459 0.2 119.5 33.0\n"; $new_line=; print $new_line; while (<>) { # print "$_"; if (/^\$end/) { $t_mag/=$mag_count if ($mag_count!=0); printf "total mag = %4.2f pref mag = %4.2f\n",$t_mag,$pref_mag if ($found); $st_mag/=$sm_count if ($sm_count!=0); printf "BK avg mag = %4.2f\n",$st_mag; $mag_count=0; $t_mag=0; $st_mag=0; $sm_count=0; $found=0; $pref_mag=0; } elsif (/^\$mag/) { $c_mag=substr($_,5,5); $mag_count+=1; $t_mag+=$c_mag; if (/^\$magP/) { $pref_mag=$c_mag;} } elsif (/^\$loc/) { if (/NEI/) { ($lat,$lon,$depth)=unpack("x24a9a10a7",$_); $found=1; } } elsif (/^\$amp/) { ($station,$amp,$a_type,$freq)=unpack("x23a4x3a6x9a2x5a6",$_); if ($found) { if ($amp>0 && ($a_type eq "PZ" ||$a_type eq "LZ" )) { # print "$station $a_type $amp $freq $lat $lon $depth\n"; print write_1 "$station $a_type $amp $freq $lat $lon $depth\n"; $new_line=; # print $new_line; @n=split(/\s+/,$new_line); $s_mag=$n[$#n]; $st_mag+=$s_mag; $sm_count+=1; } else { print "Something wrong $station $a_type $amp $freq $lat $lon $depth\n"; } } } }