#!/bin/sh
#
# 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
# http://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) 2010 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.
#
#
#
# Example client for the CDAS SPASEQL Web services.
#
# Arguments:
# $1 optional endpoint URL
# $2 optional dataset ResourceID
#
#set -x
#set -v
userAgent=spaseqlExample
#
# XSLT script to get the //FileDescription/Name/text() from a
# DataResult file.
#
getResultFileName_xslt=/tmp/getResultFileName.xslt
cat > $getResultFileName_xslt << EOF
EOF
#
# Extracts the result filenames (//FileDescription/Name/text()) from a
# DataResult file and echos the values to standard output. If the
# filename is a text file, the contents is also displayed.
#
# Arguments:
# $1 name of file containing a DataResult XML document
#
echoResultFilenames() {
resultFile=$1
echo "Result Filenames from ${resultFile}:"
for resultFileName in `xsltproc $getResultFileName_xslt $resultFile`
do
echo " ${resultFileName}"
ext=`echo $resultFileName | awk -F . '{print $NF}'`
if [ $ext = "txt" ]
then
echo " Contents of ${resultFileName}"
curl --silent $resultFileName
fi
done
}
#
# Makes an HTTP OPTIONS request and echos the results to standard-out.
#
# Arguments:
# $1 endpoint URL
#
getWadl() {
endpointUrl=$1/
echo "Invoking HTTP OPTIONS ${endpointUrl}"
curl --user-agent $userAgent --silent --request OPTIONS \
"${endpointUrl}" | xmllint --format -
echo
echo
}
#
# Gets the Parameters for the specified resource.
#
# Arguments:
# $1 endpoint URL
# $2 ResourceID
#
getParameters() {
endpoint=$1
resourceId=$2
getParametersFileName_spaseql=/tmp/getParamters$$.spaseql
cat > $getParametersFileName_spaseql << EOF
1.0spase://SMWG/Service/CDAWebspase://SMWG/Service/CDAWeb${resourceId}
EOF
echo "Submitting the following SPASEQL to get the Parameters for $resourceId:"
echo
cat $getParametersFileName_spaseql
echo
echo
echo "Response:"
echo
curl --user-agent $userAgent --silent \
--header "Content-Type: application/xml" \
--header "Accept: application/xml" \
--data-ascii "@${getParametersFileName_spaseql}" "$endpoint" | \
xmllint --format -
echo
echo
rm -f $getParametersFileName_spaseql
}
#
# Gets the data Inventory for the specified resource.
#
# Arguments:
# $1 endpoint URL
# $2 ResourceID
#
getInventory() {
endpoint=$1
resourceId=$2
getInventoryFileName_spaseql=/tmp/getInventory$$.spaseql
cat > $getInventoryFileName_spaseql << EOF
1.0spase://SMWG/Service/CDAWebspase://SMWG/Service/CDAWeb${resourceId}
EOF
echo "Submitting the following SPASEQL to get the Inventory for $resourceId:"
echo
cat $getInventoryFileName_spaseql
echo
echo
echo "Response:"
echo
curl --user-agent $userAgent --silent \
--header "Content-Type: application/xml" \
--header "Accept: application/xml" \
--data-ascii "@${getInventoryFileName_spaseql}" "$endpoint" | \
xmllint --format -
echo
echo
rm -f $getInventoryFileName_spaseql
}
#
# Appends an Expression/Parameter/Name element tree to the specified
# (XML) file.
#
# Arguments:
# $1 name of file to append to
# $2 Name text
#
appendExpressionParameterName() {
cat >> $1 << EOF
${2}
EOF
}
#
# Gets the Granule for the specified resource.
#
# Arguments:
# $1 endpoint URL
# $2 startDate
# $3 stopDate
# $4 format
# $5 ResourceID
# $6- parameter names
#
getGranule() {
endpoint=$1; shift
startDate=$1; shift
stopDate=$1; shift
format=$1; shift
resourceId=$1; shift
getGranuleFileName_spaseql=/tmp/getGranule$$.spaseql
cat > $getGranuleFileName_spaseql << EOF
1.0spase://SMWG/Service/CDAWebspase://SMWG/Service/CDAWeb${resourceId}
EOF
while [ $# -gt 0 ]
do
appendExpressionParameterName $getGranuleFileName_spaseql $1
shift
done
cat >> $getGranuleFileName_spaseql << EOF
${format}
EOF
resultFile=/tmp/getGranule$$.xml
echo
echo "Submitting the following SPASEQL to get the Granule for $resourceId:"
echo
cat $getGranuleFileName_spaseql
echo
echo "Response:"
echo
curl --user-agent $userAgent --silent \
--header "Content-Type: application/xml" \
--header "Accept: application/xml" \
--data-ascii "@${getGranuleFileName_spaseql}" \
--output $resultFile $endpoint
xmllint --format $resultFile
echo
echo
echoResultFilenames $resultFile
echo
echo
rm -f $resultFile
}
#
# Main part of example.
#
# default parameter values
#
#endpoint=http://cdaweb.gsfc.nasa.gov/WS/spaseql/1/Query
endpoint=http://cdaweb-dev.sci.gsfc.nasa.gov/WS/spaseql/1/Query
#endpoint=http://localhost:8082/WS/spaseql/1/Query
#endpoint=http://localhost:8084/WS/spaseql/1/Query
#resourceId=spase://VMO/NumericalData/Geotail/MGF/PT64S
resourceId=spase://VHO/NumericalData/ACE/MAG/L2_PT16S
#resourceId=spase://VMO/NumericalData/IMP8/MAG/PT15.36S
#resourceId=spase://VWO/NumericalData/IMAGE/RPI/DS.PT5M
startDate="2004-01-01T00:00:00Z"
stopDate="2004-01-01T01:00:00Z"
format=Text.ASCII
#format=CDF
#format=GIF
#format=PNG
#format=PDF
#format=Postscript
#parameters="IB IB_vector"
parameters="Magnitude"
#parameters="Frequency"
#parameters="B_Vector_GSE"
#traceOption="--header 'X-Jersy-Trace-Accept: true'"
case $# in
0)
;;
1)
endpoint=$1
;;
2)
endpoint=$1
resourceId=$2
;;
3)
echo "ERROR: missing stopDate parameter"
exit 1
;;
4)
endpoint=$1
resourceId=$2
startDate=$3
stopDate=$4
;;
5)
endpoint=$1
resourceId=$2
startDate=$3
stopDate=$4
format=$5
;;
*)
endpoint=$1
resourceId=$2
startDate=$3
stopDate=$4
format=$5
shift 5
parameters=$@
;;
esac
echo "SPASEQL Example"
echo
getWadl $endpoint
getParameters $endpoint $resourceId
getInventory $endpoint $resourceId
getGranule $endpoint $startDate $stopDate $format $resourceId $parameters
exit 0