#! /usr/local/bin/perl -w 
#
# plot_tec_dump -- Make x vs. y plots (postscript) from tec_dump text data.
#
use strict;

my $usage = <<EOS;
plot_tec_dump  <tec_dump_file> <station> <sat> <x-value> <y-values ...> [<switches for gnup ...>]

x-value:  gps_time, utc_time, utc_hours, az, el, eflat, eflon, sflon, smlat, smlon, etc.
y-value:  stec, vtec, stec_gim, vtec_gim, stec_diff, etc.
sat:  number (##), "all" or "" for all satellites
switches:  -y 0:100 -x 0:90
EOS

die $usage unless @ARGV >= 5;

my %VALS = ( gps_time  => [ 1, "GPS Time", "sec"],
             stn       => [ 2, "Station", ""],
             sat       => [ 3, "Satellite", ""],
             az        => [ 4, "Azimuth", "deg"],
             el        => [ 5, "Elevation", "deg"],
             eflat     => [ 6, "Earth-fixed IPP Latitude", "deg"],
             eflon     => [ 7, "Earth-fixed IPP Longitude", "deg"],
             sflon     => [ 8, "Sun-fixed IPP Longitude", "deg"],
             refstn    => [ 9, "Reference station", ""],
             angle     => [10, "Angle from Ref stn", "deg"],
             dist      => [11, "Shell distance from Ref stn", "km"],
	     utc_hour  => [12, "UTC Hour", ""],
             date_time => [13, "GPS Date/Time", ""],
             stec      => [14, "Obs Slant TEC", "TECU"],
             vtec      => [15, "Obs Vertical TEC", "TECU"],
             stec_gim  => [16, "GIM Slant TEC", "TECU"],
             vtec_gim  => [17, "GIM Vertical TEC", "TECU"],
             stec_diff => [18, "Slant TEC Residual", "TECU"],
             stec_err  => [19, "Obs Slant TEC Error", "TECU"],
             elev_map  => [20, "Elevation Mapping Ratio", ""],
             sat_bias  => [21, "Sat Bias", "TECU"],
             stn_bias  => [22, "Station Bias", "TECU"],
             smlat     => [23, "Solar-magnetic IPP Latitude", "deg"],
             smlon     => [24, "Solar-magnetic IPP Longitude", "deg"],
             rcvx      => [25, "Receiver X Coordinate", "km"],
             rcvy      => [26, "Receiver Y Coordinate", "km"],
             rcvz      => [27, "Receiver Z Coordinate", "km"],
             trnx      => [28, "Transmitter X Coordinate", "km"],
             trny      => [29, "Transmitter Y Coordinate", "km"],
             trnz      => [30, "Transmitter Z Coordinate", "km"],
             utc_time  => [31, "UTC Time", "sec"],
             utc_date_time => [32, "UTC Date/Time", ""],
             stec_p    => [33, "Obs Slant TEC from Pseudorange", "TECU"]
	     stec_gaim => [34, "GAIM Slant TEC", "TECU"],
             stec_gaim_m_obs => [35, "GAIM STEC minus Obs", "TECU"]
           );

my $tec_dump_file = shift @ARGV;
my $stn = uc shift @ARGV;
$stn = "ALL" if $stn eq "" || $stn =~ /^a/i;
my $sat = shift @ARGV;
$sat = "" if $sat =~ /^a/i;

my $xval = lc shift @ARGV;
my $vals = join " ", keys %VALS;
die "plot_tec_dump: Bad x-value, $xval.  Must be one of:\n$vals\n" if !defined $VALS{$xval};
my $yval = lc shift @ARGV;
die "plot_tec_dump: Bad y-value, $yval.  Must be one of:\n$vals\n" if !defined $VALS{$yval};

my @yvals;
push @yvals, $yval;
while ( defined $ARGV[0] && $ARGV[0] !~ /^-/ ) {
    push @yvals, lc shift @ARGV;
    die "plot_tec_dump: Bad y-value, $yvals[$#yvals].  Must be one of:\n$vals\n" if !defined $VALS{$yvals[$#yvals]};
}
my $yvals = join ",", @yvals;

my $key = $stn;
$key = "GPS" if $key eq "ALL";
if ( $sat ne "" ) {
    $key .= ' GPS' . sprintf "%2.2d", $sat;
}

my @i_yvals;
foreach my $v ( @yvals ) {
    push @i_yvals, $VALS{$v}->[0];
}
my $i_yvals = join " ", @i_yvals;
my $i_xval = $VALS{$xval}->[0];
my $ylabel = build_label($yval);
my $xlabel = build_label($xval);
my $title  = build_title($yval, $xval);
my $sat_str = $sat ne "" ? "_GPS$sat" : "";
my $str = `head -2 $tec_dump_file`;
my ($header, $data_line) = split "\n", $str;
my $tmp = (split ' ', $data_line)[31];
my $date = substr($tmp,0,4) . '/' . substr($tmp,4,2) . '/' . substr($tmp,6,2);
my $short_date = substr($tmp,2,2) . substr($tmp,4,2) . substr($tmp,6,2);
$title .= " for $stn${sat_str} on $date";
my $extra_args = join " ", @ARGV;
my $path = `pwd`;  chomp $path;

my $out_file;
if ( @yvals == 1 ) {
    $out_file = "$path/plt${short_date}_$stn${sat_str}_${yval}_vs_$xval";
    print STDERR
    qq(grep '$key' $tec_dump_file | cl $i_xval $i_yvals | gnup -t "$title" -yl "$ylabel" -xl "$xlabel" -o $out_file.ps $extra_args\n);
    `grep '$key' $tec_dump_file | cl $i_xval $i_yvals | gnup -t "$title" -yl "$ylabel" -xl "$xlabel" -o $out_file.ps $extra_args`;
} else {
    $out_file = "$path/plt${short_date}_$stn${sat_str}_${yvals[0]}_${yvals[1]}_vs_$xval";
    print STDERR
    qq(grep '$key' $tec_dump_file | cl $i_xval $i_yvals | gnupem $yvals '-t "$title" -yl "$ylabel" -xl "$xlabel" -o $out_file.ps $extra_args'\n);
    `grep '$key' $tec_dump_file | cl $i_xval $i_yvals | gnupem $yvals '-t "$title" -yl "$ylabel" -xl "$xlabel" -o $out_file.ps $extra_args'`;

}

print STDERR
"ghostscript $out_file.ps\n";
`ghostscript $out_file.ps\n`;
exit;

if ( ($str = `which ghostview`) !~ /no ghostview/i) {
    print STDERR
    "ghostview $out_file.ps\n";
    `ghostview $out_file.ps\n`;
} elsif ( ($str = `which ghostscript`) !~ /no ghostscript/i) {
    print STDERR
    "ghostscript $out_file.ps\n";
    `ghostscript $out_file.ps\n`;
} elsif ( ($str = `which convert`) !~ /no convert/i) {
    print STDERR
    "convert -rotate 90 $out_file.ps $out_file.gif\n";
    `convert -rotate 90 $out_file.ps $out_file.gif`;
    if ( ($str = `which xview`) !~ /no xview/i) {
	print STDERR
        "xview $out_file.gif\n";
	`xview $out_file.gif`;
    } elsif ( ($str = `which xv`) !~ /no xv/i) {
	print STDERR
        "xv $out_file.gif\n";
	`xv $out_file.gif`;
    } else {
	print STDERR "plot_tec_dump: Error, no xview or xv available.";
    }
} else {
    print STDERR "plot_tec_dump: Error, no ghostview, ghostscript, or convert available.";
}


# Subroutines follow.

sub build_label {
    my($val) = @_;

    my $name = $VALS{$val}->[1];
    my $unit = $VALS{$val}->[2];
    my $label = $name;
    $label .= " (" . $unit . ")" if $unit ne "";
}

sub build_title {
    my($yval, $xval) = @_;

    my $yname = $VALS{$yval}->[1];
    my $xname = $VALS{$xval}->[1];
    my $title = "$yname versus $xname";
}


             
