#! /bin/csh -f
##
# Based on the latest formal version (updated 27 Jul 2015) distributed at JSOC website;
# Modified by K.H. so that type (or name) of primary keyword be specified.
# 
# Usage;
#   my_exportfile.csh 'hmi.M_45s[2015.01.15_14:00:00/3m]' T_REC   XXX@YYY.ZZZ
#   my_exportfile.csh 'hmi.Synoptic_Mr_720s[2140-2141]'   CAR_ROT XXX@YYY.ZZZ
##

set noglob

if ($#argv == 3) then
  set myds=$1
  set mypkey=$2
  set myadrs=$3
  echo $myds $mypkey $myadrs
else
  echo Three args required...
  exit
endif


##
# get desired recordset and encode for transmitting
# you can get the following url_escape script at http://jsoc.stanford.edu/ajax/
##
set ds=`url_escape.pl "$myds"`

##
# You MUST now include a valid registered email address.  You can use a prior exportdata "notify" email address
# or just make one via http://jsoc.stanford.edu/ajax/register_email.html
# Change the '$2' in the line below to your notify address if you want the script to not need a second argument.
##
set notify=`url_escape.pl "$myadrs"`

## for FITS without full headers use fastest method use the next 2 lines:
# set method=url_quick
# set protocol=as-is
##
# if, as is most common, you want full FITS headers or
# if you want to specify the filename format for the returned data or if you expect multiple
# files you should use the next two lines.

set method=url
set protocol="FITS"

##
# to specify filename formats add the "filenamefmt" command to the cmd line below.
# you will need to url_escape the filenamefmt. 

if ($mypkey == T_REC) then
  set mypkey="T_REC:A"
endif
set ffmt = `url_escape.pl '{seriesname}.{'$mypkey'}.{segment}'`
echo $ffmt
set op=exp_request

set cmd = "op=$op&ds=$ds&process=n=0|no_op&method=$method&format=txt&protocol=$protocol&filenamefmt=$ffmt&notify=$notify&requestor=none&sizeratio=1"

set JSOC="http://jsoc.stanford.edu"
set FETCH="$JSOC/cgi-bin/ajax/jsoc_fetch"

## Make a place to put the handshake information
set RESP = /tmp/jsoc_export.$$

wget -S -nv -O $RESP  $FETCH?"$cmd"
if ($?) then
  wget failed, response was: $RESP
  exit
endif

if ($method == "url") then
  set Status = "`grep status $RESP | sed -e 's/[{}]//g' -e 's/: / = /' -e 's/"status"/Status/' -e 's/,.*//'`"
  set $Status
  if ($Status == 4) then
    echo "Export request failed, status=" $Status
    cat $RESP
    echo "Command was " $cmd
    exit
  else if ($Status == 6) then
    echo "Problem with finding registered email address."
    cat $RESP
    exit
  endif

  set requestid = `grep requestid $RESP`
  if ($#requestid == 0) then
    echo no requestid found
    exit
  endif
  set $requestid

## wait for processing to complete, do status query with sleeps and initial sleep
  set qry = "op=exp_status&requestid=$requestid&format=txt"
  while (1)
    sleep 3
    wget  -S -O $RESP $FETCH?"$qry"
    if ($?) then
      wget failed, response was: $RESP
      exit
    endif
    set Status = "`grep status $RESP | sed -e 's/[{}]//g' -e 's/: / = /' -e 's/"status"/Status/' -e 's/,.*//'`"
    set $Status
    if ($Status == 0) break
    if ($Status == 6) then
        continue
    else if ($Status > 2) then
        echo Error in processing export request
        echo possibly bad recordset format or no data in SUMS
        cat $RESP
        exit
    endif
  end

else if ($method == "url_quick") then
  set Status = "`grep status $RESP | sed -e 's/[{}]//g' -e 's/: / = /' -e 's/"status"/Status/' -e 's/,.*//'`"
  set $Status
  if ($Status == 4) then
    echo "Export request failed, status=" $Status
    cat $RESP
    echo "Command was " $cmd
    exit
  else if ($Status == 6) then
    echo "Problem with finding registered email address."
    cat $RESP
    exit
  endif
endif

set dir = `grep dir $RESP`
set $dir
set count = `grep count $RESP`
set $count

##
# Now $RESP contains headers used above and table of count files.
# loop through files and fetch them.

while ($count > 0)
  set REC = `tail --lines=$count $RESP | head -1`
  set QUERY = $REC[1]
  set FILE = $REC[2]
  set URL = $JSOC$dir/$FILE
  echo Fetching $QUERY as `basename $FILE`
  wget -S -nv $URL
  if ($?) then
    wget failed, response was: $RESP
    exit
  endif
  @ count = $count - 1
end

##
# remove working file.  Parhaps useful to save with the data.
# rm $RESP
mv $RESP ./

## end of this file
