#!/bin/csh -f
# ************************************************************************
# *                                                                      *
# *   This C-shell programs automatically downloads 3D series of         *
# *   displacements caused by atmospheric pressure loading from the      *
# *   Goddard atmospheric pressure loading service. It is assumed that   *
# *   the full atmospheric pressure loading time series have been        *
# *   downloaded at least once within last two months, and  get_aplo.csh *
# *   updates the series.                                                *
# *                                                                      *
# *   get_aplo.csh first checks the date of the last epoch of the        *
# *   atmospheric pressure loading series at remote host and compares it *
# *   with the date of the last epoch stored in the local file. If the   *
# *   dates are the same, get_aplo.csh quits immediately.                *
# *   Then it retrieves the time series in ephedisp format for last      *
# *   three months, unpacks them and convert them ti bindisp format.     *
# *                                                                      *
# *   get_aplo.csh is suitable to be called by cron. It checks whether   *
# *   another process get_aplo.csh is running. If yes, it terminates     *
# *   immediately.                                                       *
# *                                                                      *
# *   Dependencies:                                                      *
# *      wget  ( http://www.gnu.org/software/wget/wget.html )            *
# *      bzip2 ( http://sources.redhat.com/bzip2            )            *
# *      aplo  ( http://gemini.gsfc.nasa.gov/aplo/aplo_source.html )     *
# *                                                                      *
# *   Customization:                                                     *
# *   -------------                                                      *
# *                                                                      *
# *   loading_date_url_where -- URL which keeps the URL of the last      *
# *                             epoch of the atmospheric pressure        *
# *                             loading time series.                     *
# *   aplo_2m_url_where      -- URL which keeps the URL of the           *
# *                             compressed file with atmospheric         *
# *                             pressure loading time series.            *
# *                                                                      *
# *   TMP_DIR                -- Name of the directory for temporary      *
# *                             files                                    *
# *   aplo_eph_dir           -- Name of the local directory for          *
# *                             atmospheric pressure loading in EPHEDISP *
# *                             format.                                  *
# *   aplo_bds_dir           -- Name of the local directory for          *
# *                             atmospheric pressure loading in BINDISP  *
# *                             format.                                  *
# *   get_aplo_pid           -- File which keeps PID (process identifier)*
# *                             of the get_aplo process.                 *
# *   local_last_aplo_date   -- File which keeps the last date of the    *
# *                             atmospheric pressure loading in          *
# *                             $aplo_eph_dir directory.                 *
# *   local_last_aplo_update -- File which keeps the last date of        *
# *                             atmospheric pressure loading time series *
# *                             update.                                  *
# *                                                                      *
# *   wget_exe               -- Full path name of wget executable.       *
# *   bzip2_exe              -- Full path name of bzip2 executable.      *
# *   ephedisp_to_bin_exe    -- Full path name of ephedisp_to_bin        *
# *                             executable (program for conversion       *
# *                             series from ephedisp to bindisp format). *
# *                                                                      *
# *   max_tries              -- Maximal number if tries to retrieve the  *
# *                             data.                                    *
# *   sleep_sec              -- Sleep time in seconds between tries to   *
# *                             download the data.                       *
# *                                                                      *
# *  ### 30-AUG-2004  get_aplo.csh  v1.3 (c)  L. Petrov 29-MAR-2005  ### *
# *                                                                      *
# ************************************************************************
#
# === Beginning user customization ===
#     ----------------------------
#
set aplo_2m_url_where     = http://gemini.gsfc.nasa.gov/aplo/aplo_2m_eph_url.txt
set loading_date_url_where = http://gemini.gsfc.nasa.gov/aplo/loading_date_url.txt
#
set TMP_DIR = /tmp
set aplo_eph_dir = /lyra3/aplo_eph
set aplo_bds_dir = /lyra2/vlbi_oper/apriori/aplo_bds
set get_aplo_pid = /lyra2/vlbi_oper/apriori/get_aplo.pid
set local_last_aplo_date   = /lyra2/vlbi_oper/last_aplo_date.txt
set local_last_aplo_update = /lyra2/vlbi_oper/last_aplo_update.txt
#
set wget_exe  = /usr/local/bin/wget
set bzip2_exe = /usr/local/bin/bzip2
set ephedisp_to_bin_exe = /lyra4/aplo/bin/ephedisp_to_bin
#
set max_tries = 50   
set sleep_sec = 60   
#
# === No user customization beyond this line ===
#     --------------------------------------  
#
set temp_fil     = $TMP_DIR/get_aplo__$$
set aplo_2m_file = $TMP_DIR/service__$$__aplo_2m_eph.tar.bz2
set aplo_eph_prefix = ${aplo_eph_dir}/vsgd_
set wget_opts = "--tries=$max_tries --wait=$sleep_sec --continue "
#
# --- Various checks
#
# --- First check whether get_aplo is already running
#
if ( -f $get_aplo_pid ) then
     set get_aplo_old_pid = `cat $get_aplo_pid`
     /bin/kill -0 $get_aplo_old_pid >& /dev/null
     set status_old_get_aplo = $status
     if ( $status_old_get_aplo == 0 ) then
          echo "get_aplo is already running. Quitting"
          exit 0
     endif
endif
#
# --- Create the pid-file. It contains identifier of the current process
#
echo $$ > $get_aplo_pid 
#
# --- Check existence of some local files
#
if ( -f $local_last_aplo_date == 0 ) then
     echo "0000.00.00_00:00:00" > $local_last_aplo_date 
endif
if ( -f $local_last_aplo_update == 0 ) then
     echo "0000.00.00_00:00:00" > $local_last_aplo_update 
endif
if ( -d $aplo_eph_dir == 0 ) then
     echo "Directory $aplo_eph_dir was not found"
     rm -f $get_aplo_pid
     exit 1
endif
if ( -d $aplo_bds_dir == 0 ) then
     echo "Directory $aplo_bds_dir was not found"
     rm -f $get_aplo_pid
     exit 1
endif
#
# --- Learn the URL with the date of the last loading time epoch
#
$wget_exe $wget_opts -O $temp_fil $loading_date_url_where
set wget_status = $status
if ( $wget_status != 0 ) then
     echo "Error in retrieving  $loading_date_url_where"
     rm -f $get_aplo_pid
     exit 1
endif
set loading_date_url = `cat $temp_fil`
#
# --- Learn the date of the latest update
#
rm -f $temp_fil 
$wget_exe $wget_opts -O $temp_fil $loading_date_url
set wget_status = $status
if ( $wget_status != 0 ) then
     echo "Error in retrieving  $loading_date_url"
     rm -f $get_aplo_pid
     exit 1
endif
#
# --- Get the date of the latest epoch of the amtospheric pressure loading 
# --- from remote site and from local site
#
set remote_aplo_date = `cat $temp_fil | awk '{print $6}'`
set local_aplo_date  = `cat $local_last_aplo_date`
#
# --- Now check whether the latest date of remote pressure loading series
# --- is after the latest date of local pressure loading series
#
if ( $remote_aplo_date == $local_aplo_date ) then
#
# -- The remote atmospheric pressure loading update date is the same as
# -- the local date. Nothing to do anymore.
#
     rm -f $get_aplo_pid
     exit 0
endif
#
# --- Learn the URL with the last 3 months of the atmospheric pressure loading
# --- series
#
rm -f $temp_fil 
$wget_exe $wget_opts -O $temp_fil $aplo_2m_url_where 
set wget_status = $status
if ( $wget_status != 0 ) then
     echo "Error in retrieving  $aplo_2m_url_where"
     rm -f $get_aplo_pid
     exit 1
endif
#
set aplo_2m_url = `cat $temp_fil`
#
# --- Retrieve the atmospheric pressure loading series for last three months
#
$wget_exe $wget_opts -O $aplo_2m_file $aplo_2m_url
set wget_status = $status
if ( $wget_status != 0 ) then
     echo "Error in retrieving  $aplo_2m_url"
     rm -f $get_aplo_pid
     exit 1
endif
#
# --- Unpack the atmospheric pressure loading series
#
cd $aplo_eph_dir 
$bzip2_exe -d -c $aplo_2m_file | tar -xvf - 
#
# --- Convert the atmospheric pressure loading series from eph to bds format
#
$ephedisp_to_bin_exe $aplo_eph_prefix $aplo_bds_dir  begin end 1
set status_flag = $status
if ( $status_flag != 0 ) then
     echo "Error in ephedisp_to_bin. Status: $status_flag" >> $aplo_log
     rm -f $get_aplo_pid
     exit 1
endif
#
# --- Preserve the last date of the atmospheric pressure loading
#
rm -f $local_last_aplo_date
echo $local_aplo_date > $local_last_aplo_date
#
# --- Store the date of atmospheric pressure loading update
#
set    date_stamp     = `date "+%Y.%m.%d-%H:%M:%S" | tr "[a-z]" "[A-Z]"`
rm -f $local_last_aplo_update
echo $date_stamp       > $local_last_aplo_update
echo $remote_aplo_date > $local_last_aplo_date
#
# --- Remove temporary file
#
rm -f $temp_fil     
#
# --- Remove pid-file
#
rm -f $get_aplo_pid