#!/bin/bash
# shell script to monitor aim cips reprocessing
#   Bill Barrett
#   Jul 2016

# The following two variables are commands that will be executed while looping
#   through the various directories

# First from the list of files that have been found, lop off everything except
#   the orbit number, year, and day of year.
# Take something like
#   ./aimcips2_reprocessing/data/cips/north_2008/level_1a/py/ver_05.10/rev_01/cips_sci_1a_orbit_05631_2008-128_cam_py_v05.10_r01.nc.gz
# and reduce it to
#   05631_2008-128
getOrbitAndDate="awk -F \"orbit_\" '{print \$2}' | awk -F \"_cam\" '{print \$1}'"

# Get the file creation date via 'ls -og' and strip out everything but the pertinent information
# Take something like
#   -rw-r--r-- 1 11796198 2016-07-26 19:35:26.402166081 +0000 ./aimcips2_reprocessing/data/cips/south_2013/level_1a/px/ver_05.10/rev_01/cips_sci_1a_orbit_35021_2013-270_cam_px_v05.10_r01.nc.gz  
# and transform it to
#   2016-07-26 19:35:26 cips_sci_1a_orbit_35021_2013-270_cam_px_v05.10_r01.nc.gz
getDateAndFilename="xargs ls -og --time-style=full-iso | sed 's/\./ /' | awk '{print \$4\" \"\$5\" \"\$8}' | awk -F \"/\" '{print \$1\" \"\$10}' | sed 's/\. //'"

echo ""
# The code is running on aimcips2, ..3, and ..4
for node in aimcips2_reprocessing aimcips3_reprocessing aimcips4_reprocessing aimcips3B_reprocessing aimcips4B_reprocessing
do
  # Get the season and the year from the code that is currently running
  # This expects to find a line that looks like
  #   for i = 2013266L, 2014078L do begin
  # and will pull '2013266L' from that line
  yearDayOfYear=`grep "for i" ./${node}/test/idl/cips/data_production/level_1A/level_1a_test_driver.pro | awk '{print $4}'`
  # The first four characters are the year, the next three the day of year
  year=${yearDayOfYear:0:4}
  dayOfYear=${yearDayOfYear:4:3}
  # This assumes that processing will begin either from the spring or fall equinox.
  # It is 'south' if the start day of year is close the fall equinox.
  if [ $dayOfYear -ge 266 ]; then
    season="south"
  else
    season="north"
  fi
  echo "Checking ${node} for ${season} ${year}"

  # Find the files for the given node, season, and year
  # This will produce a string containing items like
  #   ./aimcips2_reprocessing/data/cips/north_2008/level_1a/py/ver_05.10/rev_01/cips_sci_1a_orbit_05631_2008-128_cam_py_v05.10_r01.nc.gz
  found=`find ./${node}/data/cips/${season}_${year}/level_1a -name "*\.gz" | grep "05\.10"`
  # If no files are found there is nothing more to do
  [ ${#found} -eq 0 ] && continue

  # First from the list of files that have been found, lop off everything except
  #   the orbit number, year, and day of year.
  # Second from the list of files that have been found, use 'ls -og' to get the pertinent information
  #   and lop off everything except except the file creation time and file name.
  # In both cases sort what is left and print the four most recent.
  for cmd in "$getOrbitAndDate" "$getDateAndFilename"
  do
    if [ "$cmd" == "$getOrbitAndDate" ]; then
      echo "Most recent orbits"
    else
      echo "File creation times"
    fi
    echo $found | awk '{ for(j = 1; j <= NF; j++){ print $j; } }' | \
      eval "$cmd" | sort | tail -4
  done

  echo ""
done

