#!/usr/local/bin/perl -w
# SOAP Client to send Discover, GetDetails, and GetData messages to
# ACE SOAP server
# Version 9: June 12, 2006
#
# This software is Copyright (C) 2005 ACE Science Center,
# California Institute of Technology. The software is provided to you
# without charge, and with no warranty. You may give away copies of
# this software, including sources, provided that this notice is included
# in all the files.
###################################################

use diagnostics;
use LWP::UserAgent;
use Hash_Print;		
use Data::Dumper;
use SOAP::Lite 0.67
	proxy => "http://www.srl.caltech.edu/ACE/ASC/SOAP/ACE_SOAP_server.cgi",
	uri => "http://www.srl.caltech.edu/ACEServices";
# 	uri => "urn:ACEServices";

#use SOAP::Lite +trace; # For debugging
#use SOAP::Lite +trace => ['headers']; # For logging

my $response = ();

my $soap = new SOAP::Lite;

# goto GETDATA;

# Level 0 Discovery query - find out what kinds of data are available
# (instrument names, time-averages, description,...)

$response = $soap->ACEDiscover();
my $mdata0 = $response->result;

print "Results of Discovery (i.e. what products are available?) query: \n";

if ( fault() ) {
  printf STDERR "\n\n Aborting...\n\n";
  exit(1);
}


Hash_Print::hash_print( $mdata0, 0, );	# Traverse and print out "Discovered" hash

# print Dumper($mdata0);

my $Resource=$mdata0->{GeneralInformation}[0]->{ResourceID};

print "\n\nLet's learn some details about Product ",$Resource,"\n"; 

# Hang on the keyboard before continuing
print "Press ENTER to continue...";
my $linein = <STDIN>;

# Detailed query - learn enough to be able to present a user
# with the information she needs to construct a query to
# obtain actual data (or a URL to actual data) for a given Product.
# We use as an example, one of the Product_IDs from the Discovery query above.

$response = $soap->ACEGetDetails(
		SOAP::Data->name('ResourceID'=>$Resource));

print "\nResults of GetDetails query on ResourceID $Resource:\n";

my $mdata1 = $response->result;

if( fault() ) {
   print STDERR "\n\nAborting...\n\n";
   exit(1);
}

Hash_Print::hash_print( $mdata1, 0 );	# Traverse and print out "Details" hash

# print Dumper($mdata1);


# Hang on the keyboard
print "\n\nLet's request a URL to some data for Product ",$Resource,"\n"; 
print "Press ENTER to continue...";
$linein = <STDIN>;

# the client should now be able to present a user with enough information
# for her to compose a query for a specific data subset.

# An example of a data subset query is shown below. In real life,
# the instrument_name would be chosen from one of the InstrumentIDs
# found via the Discovery query. Same for the resolution.

# Each data item is requested via its <PhysicalParameter><Name> tag,
# which one should normally obtain from the response to the GetDetails query.

GETDATA:

my $datalist = ["Bgse_x","Bgse_y","Bgse_z"];
my $msghash = {
        "instrument_name" => "MAG",
        "resolution" => "1hr",
        "start" => "2002 1",
        "end" => "2002 10",
        "dataitem" => $datalist
};

$response = $soap->ACEGetData(
		SOAP::Data->name('DataQuery'=>$msghash));

# The old, ugly way
# $response = $soap->ACEGetData(
#		SOAP::Data->name('DataQuery' =>\SOAP::Data->value(
#			SOAP::Data->name("instrument_name" => "MAG"),
#			SOAP::Data->name("resolution" => "1hr"),
#			SOAP::Data->name("start" => "2002 1"),
#			SOAP::Data->name("end" => "2002 10"),
#			SOAP::Data->name("dataitem" => $datalist)
#			)));


my $dataURL = $response->result;

if( fault() ) {
   print STDERR "\n\nAborting...\n\n";
   exit(1);
}

if( $dataURL =~ /^ERROR/ ) {
  print "\nOops, an error occurred. The server returned:\n";
  print $dataURL,"\n";
  exit( 1 )
}

print "\nHere's the URL to the data we requested:\n";
print $dataURL,"\n";


# - - - - - - - - - - - - - - - - - - - -

sub fault {	#Check for a $response->fault condition & report it
  if ( $response->fault ) {	# returns True on fault
    my $code   = $response->faultcode;
    my $string = $response->faultstring;
    chomp $string;
    my $actor  = $response->faultactor;
    my $detail = $response->faultdetail;
    if ($code) { print STDERR "\nfaultcode   = '$code'"  ; }
    if ($string) { print STDERR "\nfaultstring = '$string'"; }
    if ($actor) { print STDERR "\nfaultactor  = '$actor'" ;  }
    if ($detail) { print STDERR "\nfaultdetail = '$detail'"; }
    return( 1 );
  } 
  return( 0 );
}
# - - - - - - - - - - - - - - - - - - - -
