#!/usr/bin/perl

use Getopt::Std;

    &getopts ('D:');
    $ymd = ($opt_D eq "m");
    $present = "3000,001,00:00:00";

# Extract channel summary from RESP files.

printf ("%-4.4s %-3.3s %-3.3s %-2.2s %11.11s %19.19s %19.19s %10.10s %10.10s %6.6s %5.5s %7.7s %7.7s  %s\n",
	"Stat", "Net", "Cha", "Lo", "Rate","Start time  ", "End time  ", "Lat", "Lon", "Elev", "Depth", "Dip", "Azimuth","Instrument");
printf ("%s\n", "-" x 132);

while (<>) {
	$line = $_;
	($type, $key, $value) = $line =~ /(\w+)\s+([^:]+):\s+([^\n]+)/;
	$station = $value, next if ($type eq "B050F03");
	$network = $value, next if ($type eq "B050F16");
	$lat     = $value, next if ($type eq "B052F10");
	$lon     = $value, next if ($type eq "B052F11");
	$elev    = $value, next if ($type eq "B052F12");
	$depth   = $value, next if ($type eq "B052F13");
	$azimuth = $value, next if ($type eq "B052F14");
	$dip     = $value, next if ($type eq "B052F15");
	$rate	 = $value, next if ($type eq "B052F18");
	$location= $value, next if ($type eq "B052F03");
	$channel = $value, next if ($type eq "B052F04");
	$start   = $value, next if ($type eq "B052F22");
	($instr) = $value =~ /\s?\S+\s+(\S+.*)$/, next if ($type eq "B052F06");
	if ($type eq "B052F23") {
		$end     = $value;
	        $end = $present if (! ($end =~ /^[0-9,\:\.\/]+$/));
		$location = "--" if ($location =~ /^\s*$/);
		if ($ymd) {
			chomp ($start = `caldate -f %Y/%m/%d,%H:%M:%S $start`);
			chomp ($end   = `caldate -f %Y/%m/%d,%H:%M:%S $end`);
		}
		printf ("%-5.5s %-2.2s %-3.3s %-2.2s %11.7f %-19.19s %-19.19s %10.5f %10.5f %6.1f %5.1f %7.1f %7.1f  %s\n",
			$station, $network, $channel, $location, 
			$rate, $start, $end,
			$lat, $lon, $elev, $depth, $dip, $azimuth, $instr),
	}
}
