#!/usr/bin/perl -w
# Copyright (c) 2001 TMAP, NOAA
# ALL RIGHTS RESERVED
#
# Please read the full copyright notice in the file COPYRIGHT
# included with this code distribution
use LWP::UserAgent;
use URI::URL;
use URI::Escape;
use strict;
use Getopt::Long;
my %Opts;
my (%Ranges, $Outfile, $Format);
sub issueRequest {
my ($urlstr, $xml) = @_;
my $hdrs = new HTTP::Headers;
my $url = new URI::URL($urlstr);
die "Bad URL $url" if ! $url;
my $req = new HTTP::Request('POST', $url, $hdrs);
$req->content_type('application/x-www-form-urlencoded');
$req->content($xml);
my $ua = new LWP::UserAgent;
my ($resp, $loc);
while(1){
$resp = $ua->request($req);
$loc = $resp->header('Location');
last if ! $loc;
$url = new URI::URL($loc);
$req = new HTTP::Request('GET', $url, $hdrs);
}
my $result = $resp->content;
if ($result =~ // && $result !~ /LAS SERVER OK/){
print STDERR "The request failed with the following message:\n";
print STDERR $result;
exit 1;
}
if (defined($Outfile)){
print $Outfile $result;
} else {
print $result;
}
}
sub getXMLArgs {
my $str = "";
# Random number to avoid cacheing
$str = '' . rand() . '';
$str .= "long" if $Opts{l};
$str .= "$ARGV[0]" if $ARGV[0];
$str .= "$ARGV[1]" if $ARGV[1];
$str;
}
sub genXML {
my $xmlMid;
my $rank = 0;
foreach my $key (keys %Ranges){
my ($lo,$hi,$extra) = split(':', $Ranges{$key});
if ($extra){
die "Invalid range:", $Ranges{$key},
"\nOnly one ':' is allowed in a range";
}
next if ! defined($lo);
if (!$hi){
$xmlMid .= qq{};
} else {
$xmlMid .= qq{};
$rank++;
}
}
my $xmlTail = qq{};
my $op = 'data';
if ($Format =~ /ps|html/){
if ($rank == 1){
$op = 'line';
} elsif ($rank == 2){
$op = 'shade';
} else {
die "Can only plot 1 or 2d data";
}
$op .= '_ps' if $Format eq 'ps';
}
my $xmlHead=qq{$Format.5};
my $xml = $xmlHead . $xmlMid . $xmlTail;
return uri_escape('xml=' . $xml);
}
sub usage {
print STDERR "Usage: lasget.pl [-x xrange ] [-y yrange] [-z zrange] \n";
print STDERR " [-t trange] [-f format] [-o file] \n";
print STDERR " url dataset variable\n";
print STDERR " -x xrange x range of data (-120, -120:80)\n";
print STDERR " -y yrange y range of data (0, 0:90)\n";
print STDERR " -z zrange z range of data (0, 0:20)\n";
print STDERR " -t trange time range of data (1992-Dec-10, 1992-Jan-1:1992-Dec-1)\n";
print STDERR " -o file output file. Defaults to stdout\n";
print STDERR " -f format output format asc|cdf|txt|ps|html\n";
print STDERR " url URL of LAS server\n";
print STDERR " dataset name of dataset\n";
print STDERR " variable name of variable\n";
exit 1;
}
# TODO -- arg error checking
use Getopt::Std;
getopts('x:y:z:t:o:f:', \%Opts);
$Format = $Opts{f};
$Format = 'cdf' if ! $Format;
if ($Format !~ /asc|cdf|txt|ps|html/){
die "Unknown format: $Format\n";
}
if ($Opts{o}){
my $file = $Opts{o};
open OUTFILE, ">$file" || die "Can't open output file $file\n";
$Outfile = \*OUTFILE;
}
foreach my $range (qw(x y z t)){
$Ranges{$range} = $Opts{$range} if $Opts{$range};
}
if ($#ARGV != 2){
usage;
}
issueRequest($ARGV[0], genXML);