#!/usr/bin/ksh
#
# overplot_move.ksh 
#
# Moves overview plots from scratch directory to working directory 
# Based heavily on overplot.ksh by Tim Quinn
# 
#

#
## Process files produced by IDL run
#

    dir="/disks/themisdata/overplots"
 
    cd ${dir}/scratch/
    files=`ls`
    echo "ls complete"
    for file in $files 
    do
      if [[ -n `ls $file | grep "^20"` ]]
      then
        plotYear=$(echo $file | awk -F- '{print $1}')
        plotMonth=$(echo $file | awk -F- '{print $2}')
        plotDay=$(echo $file | awk -F- '{print $3}')    
      elif [[ -n `ls $file | grep "^th"` ]]
      then
        plotYear=$(echo $file | awk -F_ '{print substr($4,1,4)}')
        plotMonth=$(echo $file | awk -F_ '{print substr($4,5,2)}')
        plotDay=$(echo $file | awk -F_ '{print substr($4,7,2)}')
      else
        continue
      fi  
      fileSize=$(ls -l $file |awk '{print $1}')
      echo "Moving file: $file to ${dir}/${plotYear}/${plotMonth}/${plotDay}"
      mkdir -p ${dir}/${plotYear}/${plotMonth}/${plotDay}
      mv -f $file ${dir}/${plotYear}/${plotMonth}/${plotDay}
    
#      if [[ -n `ls $file | grep "^20"` ]]
#      then     
#        plotYear=$(echo $file | awk -F- '{print $1}')
#        plotMonth=$(echo $file | awk -F- '{print $2}')
#        plotDay=$(echo $file | awk -F- '{print $3}')
#        echo "Moving file: $file to ${dir}/${plotYear}/${plotMonth}/${plotDay}"
#        mkdir -p ${dir}/${plotYear}/${plotMonth}/${plotDay}
#        mv -f $file ${dir}/${plotYear}/${plotMonth}/${plotDay}
#      fi
      
#      if [[ -n `ls $file | grep "^th"` ]]
#      then
#        plotYear=$(echo $file | awk -F_ '{print substr($4,1,4)}')
#        plotMonth=$(echo $file | awk -F_ '{print substr($4,5,2)}')
#        plotDay=$(echo $file | awk -F_ '{print substr($4,7,2)}')
#        echo "Moving file: $file to ${dir}/${plotYear}/${plotMonth}/${plotDay}"    
#        mkdir -p ${dir}/${plotYear}/${plotMonth}/${plotDay}
#        mv -f $file ${dir}/${plotYear}/${plotMonth}/${plotDay}
#      fi

    done
  
    exit 0
