#!/usr/local/bin/perl 

use XML::Simple;

my $Usage=<<'End_of_Usage;';
Usage:
	plot_xml_series [-ps] [-xr 1998:2000.5] [-yr -9:9] XMLfile

	-ps  	send postscript output to a file
	-xr	1998:2000.5   set x range
	-yr 	-9:9	set y range

XMLfile may be compressed with compress or gzip (extension .Z or .gz).

End_of_Usage;

if ($#ARGV < 0) {
    die "$Usage\n";
}
$postscpt=0;
$keepps=0;
while ($ARGV[0] =~ /^-/) {
  if ($ARGV[0] =~ "-ps") {
    $postscrpt=1;
    shift @ARGV;
  } elsif ($ARGV[0] =~ "-xr") {
    shift @ARGV;
    $xr=shift @ARGV;
  } elsif ($ARGV[0] =~ "-yr") {
    shift @ARGV;
    $yr=shift @ARGV;
  } elsif ($ARGV[0] =~ "-H") {
    print "$Usage\n";
    exit 0;
  }
}
$tmpcomp=0;
if ($ARGV[0] =~ /[zZ]$/) {
  $XMLfname="tmp_read_series.$$";
  $done=`zcat $ARGV[0] > $XMLfname`;
  $tmpcomp=1;
} else {
  $XMLfname=$ARGV[0];
}


my $station = XMLin($XMLfname, searchpath=>'.', forcearray => ['location', 'velocity', 'repeatability']);

$nseries=@{$station->{series}};

$ev="no_event";
$files_open=0;
for ($i=0;$i<$nseries;$i++) {
  if (! ($station->{series}->[$i]->{event}->{name} =~ $ev)) {
    $lonf=$station->{name}.$station->{series}->[$i]->{event}->{name}.".east";
    $latf=$station->{name}.$station->{series}->[$i]->{event}->{name}.".north";
    $radf=$station->{name}.$station->{series}->[$i]->{event}->{name}.".up";
    if ($files_open) {
      close LON;
      close LAT;
      close RAD;
    }
    open LON, ">$lonf";
    open LAT, ">$latf";
    open HT,  ">$radf";
    $files_open=1;
    push @lonfiles, $lonf;
    push @latfiles, $latf;
    push @radfiles, $radf;
    $ev=$station->{series}->[$i]->{event}->{name};
  }
  $est=$station->{series}->[$i]->{east}->{estimate};
  print LON "$station->{series}->[$i]->{float_year} $est $station->{series}->[$i]->{east}->{uncertainty}\n";
  $est=$station->{series}->[$i]->{north}->{estimate}+3.0 ;
  print LAT "$station->{series}->[$i]->{float_year} $est $station->{series}->[$i]->{north}->{uncertainty}\n";
  $est=$station->{series}->[$i]->{up}->{estimate}-3.0 ;
  print HT "$station->{series}->[$i]->{float_year} $est $station->{series}->[$i]->{up}->{uncertainty}\n";
}
close LON;
close LAT;
close HT;

$gopt=" ";
if ($postscrpt) {
  $outf=$station->{name}.".ps";
#  $gopt= $gopt." -term 'postscript color' -o ".$outf." ";
  $gopt= $gopt." -o '".$outf."' -term 'postscript color' ";
}
if (defined $xr) {
  $gopt = $gopt." -xr $xr";
}
if (defined $yr) {
  $gopt = $gopt." -yr $yr";
}
print "gopt=$gopt\n";
$title = $station->{name}." ".$station->{info}->{analysis_version}."  chi^2/dof=".$station->{fit}->{chi2_per_dof};

#$done= `gnup -e @lonfiles @latfiles @radfiles -t "$title" -xr 1998:2000.5 -yr -9:9 -c "set grid" -ylabel cm -xlabel year $gopt`;
#print "gnup -e @lonfiles @latfiles @radfiles -t '",$title,"' -c 'set grid' -ylabel cm -xlabel year $gopt\n";
$done= `gnup -e @lonfiles @latfiles @radfiles -t "$title" -c "set grid" -ylabel cm -xlabel year $gopt`;


if ($tmpcomp) {
  unlink $XMLfname;
}

foreach $i (@lonfiles,@latfiles,@radfiles) {
  unlink $i;
}
