#!/usr/bin/perl -w

#
# NOSA HEADER START
#
# The contents of this file are subject to the terms of the NASA Open 
# Source Agreement (NOSA), Version 1.3 only (the "Agreement").  You may 
# not use this file except in compliance with the Agreement.
#
# You can obtain a copy of the agreement at
#   docs/NASA_Open_Source_Agreement_1.3.txt
# or 
#   https://cdaweb.gsfc.nasa.gov/WebServices/NASA_Open_Source_Agreement_1.3.txt.
#
# See the Agreement for the specific language governing permissions
# and limitations under the Agreement.
#
# When distributing Covered Code, include this NOSA HEADER in each
# file and include the Agreement file at 
# docs/NASA_Open_Source_Agreement_1.3.txt.  If applicable, add the 
# following below this NOSA HEADER, with the fields enclosed by 
# brackets "[]" replaced with your own identifying information: 
# Portions Copyright [yyyy] [name of copyright owner]
#
# NOSA HEADER END
#
# Copyright (c) 2009-2017 United States Government as represented by the 
# National Aeronautics and Space Administration. No copyright is claimed 
# in the United States under Title 17, U.S.Code. All Other Rights Reserved.
#
# $Id: getCdasDataFile.pl,v 1.3 2017/02/28 15:12:06 bharris Exp $
#

use SOAP::Lite;
#use SOAP::Lite 'trace', 'debug';

if ($#ARGV < 2) {

    print "ERROR: missing argument(s)\n";
    print "USAGE: $0 dataset start-time endtime\n";
    print "  where dataset = a SPASE ResourceID or traditional CDAWeb dataset name\n";
    print "        start-time = YYYY-MM-DDTHH:MM:SS.000Z\n";
    print "        end-time = YYYY-MM-DDTHH:MM:SS.000Z\n\n";
    print "EXAMPLE: $0 spase://VMO/NumericalData/IMP8/MAG/PT15.36S 2000-06-09T00:00:00.000Z 2000-06-10T00:00:00.000Z\n";
    exit 1;
}

my $cdas = SOAP::Lite
  -> proxy('https://cdaweb.gsfc.nasa.gov/WS/sp_phys/jaxrpc')
  -> ns('http://cdaweb.gsfc.nasa.gov/WS/CDASWS', 'ans1');

my $agent = 'getCdasDataFile.pl ' . $cdas->transport->agent();

$cdas -> transport -> agent($agent);


my $datasetParam = SOAP::Data->type('string')->name('String_1')->value($ARGV[0]);
my $varResult = $cdas -> getDatasetVariables($datasetParam);

my @varNames;

unless ($varResult -> fault) {

    print "getDatasetVariables() returned:\n";

    my $vars = $varResult->valueof('//result');

    foreach $var (@$vars) {

        $varNames[++$#varNames] = @$var[0];

        foreach $item (@$var) {

            print "  $item";
        }
        print "\n";
   }
}
else {

    print join ',', $varResult -> faultcode, $varResult -> faultstring;
}


my $timeIntervals =
    SOAP::Data->type('soapenc:Array')->name('arrayOfTimeInterval_2' =>
        \SOAP::Data->type('tns:TimeInterval')->name('TimeInterval' => 
            \SOAP::Data->value(
                SOAP::Data->type('xsd:dateTime')->name('end')->value($ARGV[2]),
                SOAP::Data->type('xsd:dateTime')->name('start')->value($ARGV[1])
            )
        )
    ) -> attr( { 'xmlns:tns' => 'http://cdaweb.gsfc.nasa.gov/WS/types/CDASWS',
                 'soapenc:arrayType' => 'tns:TimeInterval[1]' } );

my $filesResult = $cdas -> getCdasDataFile($datasetParam, $timeIntervals);

unless ($filesResult -> fault) {

    print "getCdasDataFile() returned:\n";

    my $files = $filesResult->valueof('//urls');

    foreach $file (@$files) {

        print "  $file \n";
    }
}
else {

    print join ',', $filesResult -> faultcode, $filesResult -> faultstring;
    print "\n";
}

#
# get file with subset of variables
#
$#varNames--;

my $variables =
    SOAP::Data->type('soapenc:Array')->name('arrayOfString_3' =>
        \SOAP::Data->value(@varNames)
    )->attr( { 'soapenc:arrayType' => 'xsd:string[]' } );

$filesResult = 
    $cdas -> getCdasDataFile2($datasetParam, $timeIntervals, $variables);

unless ($filesResult -> fault) {

    print "getCdasDataFile2() returned:\n";

    my $files = $filesResult->valueof('//urls');

    foreach $file (@$files) {

        print "  $file \n";
    }
}
else {

    print join ',', $filesResult -> faultcode, $filesResult -> faultstring;
    print "\n";
}

