#----- Print Usage !! -----#
if test "$#" -ne 1
then echo " "
     echo " Print_Usage !!"  
     echo " "  
     echo " rotate.sh  staid"
     echo " rotate.sh  ORI   "
     echo " "  
     exit 0
fi

# Directories
BASE_DIR=/home/hfd
DATA_DIR=${BASE_DIR}/data

# Station ID
STID=$1

mkdir -p ${DATA_DIR}

# Check if dop_4frq_rspdx_noguchi_100Hz.py is running or not
TMP=`ps aux | grep dop_4frq_rspdx_noguchi_100Hz.py | grep -v grep | wc -l`

# If dop_4frq_rspdx_noguchi_100Hz.py is not running (i.e., TMP=0), then start the program
if [ $TMP -eq 0 ]
  then 

# Get Today
  YR=$(/bin/date +%y)
  MO=$(/bin/date +%m)
  DY=$(/bin/date +%d)
  HR=$(/bin/date +%H)
  MI=$(/bin/date +%M)
  SC=$(/bin/date +%S)
  CDATE=20${YR}${MO}${DY}

# Get start time of previous run
  STIME=`tail -1 ${BASE_DIR}/stime.txt`
  SDATE=`echo $STIME | cut -c 1-8`
  if [ ${SDATE} -ne ${CDATE} ]
    then STIME=${CDATE}_000000
  fi

# Copy the outputs from previous run to the files
  if [ -f ${DATA_DIR}/data_3925.bin ]
    then mkdir -p ${DATA_DIR}/20${YR}
         cp ${DATA_DIR}/data_3925.bin ${DATA_DIR}/20${YR}/${STIME}UT_${STID}_3925_DGL.bin
  fi
  if [ -f ${DATA_DIR}/data_5006.bin ]
    then mkdir -p ${DATA_DIR}/20${YR}
         cp ${DATA_DIR}/data_5006.bin ${DATA_DIR}/20${YR}/${STIME}UT_${STID}_5006_DGL.bin
  fi
  if [ -f ${DATA_DIR}/data_6055.bin ]
    then mkdir -p ${DATA_DIR}/20${YR}
         cp ${DATA_DIR}/data_6055.bin ${DATA_DIR}/20${YR}/${STIME}UT_${STID}_6055_DGL.bin
  fi
  if [ -f ${DATA_DIR}/data_8006.bin ]
    then mkdir -p ${DATA_DIR}/20${YR}
         cp ${DATA_DIR}/data_8006.bin ${DATA_DIR}/20${YR}/${STIME}UT_${STID}_8006_DGL.bin
  fi

# Erase the data in the output file
  cp /dev/null ${DATA_DIR}/data_3925.bin
  cp /dev/null ${DATA_DIR}/data_5006.bin
  cp /dev/null ${DATA_DIR}/data_6055.bin
  cp /dev/null ${DATA_DIR}/data_8006.bin

# Output the start time of the current run
  echo 20${YR}${MO}${DY}_${HR}${MI}${SC} >> ${BASE_DIR}/stime.txt

# Run the python code
#  echo 20${YR}${MO}${DY}_${HR}${MI}${SC}
#  /usr/bin/python3 ${BASE_DIR}/dop_4frq_nozaki_noguchi_100Hz.py
  echo 20${YR}${MO}${DY}_${HR}${MI}${SC} >> ${BASE_DIR}/noguchi.log
  /usr/bin/python3 ${BASE_DIR}/dop_4frq_rspdx_noguchi_100Hz.py >> ${BASE_DIR}/noguchi.log 2>&1

fi
