#/bin/bash
#
# plots the progress of the simulations as a bar plot

expdir="/scratch/snx3000/rhand/experiments/PALAEO-RA"

setlist="HIPPO_set_1850_1 HIPPO_set_1850_2 HIPPO_set_1850_3 HIPPO_set_1850_4 HIPPO_set_1420_1"
setlist="HIPPO_set_1420_2"

declare -A runlist
runlist[HIPPO_set_1850_1]="HIPPO_r00?i1850p1f12?? HIPPO_r010i1850p1f12??"
runlist[HIPPO_set_1850_2]="HIPPO_r01[1-9]i1850p1f12?? HIPPO_r020i1850p1f12??"
runlist[HIPPO_set_1850_3]=HIPPO_r0??i1850p1f1203?
runlist[HIPPO_set_1850_4]=HIPPO_r0??i1850p1f1242?
runlist[HIPPO_set_1420_1]=HIPPO_r0??i1420p1f11??
runlist[HIPPO_set_1420_2]=HIPPO_r0??i1420p1f13??


if [ -e runlist.dat ]; then rm runlist.dat; fi
if [ -e yearlist.dat ]; then rm yearlist.dat; fi

for set in ${setlist}; do
   for expid in ${runlist[${set}]}; do

      echo -e "${expid/HIPPO_r/}" >> runlist.dat
		find ${expid}/means -type f -name "*.nc" -printf "%f\n" | sort | tail -1 | sed -e "s/${expid}_//" -e "s/\_.*//" >> yearlist.dat

   done # expid
done # set

cat << __EOR__ | sed -e "s/   //" > plot.py
   
   import numpy as np
   import matplotlib.pyplot as plt
   
   runlist = open("runlist.dat", "r")
   yearlist = open("yearlist.dat", "r")   

   dpi = 200
   
   fig, ax = plt.subplots()
   
   runs = []
   years = []

   for run in runlist:
     runs.append(run.rstrip('\n'))

   for year in yearlist:
     years.append(int(year))

   y_pos = np.arange(len(runs))
   print(y_pos)

   print(runs)
   print(len(runs))

   print(years)
   print(len(years))
   
   ax.barh(y_pos , years , align='center')
   ax.set_yticks(y_pos)
   ax.set_yticklabels(runs)
   ax.invert_yaxis()  # labels read top-to-bottom

   ax.set(xlim=[1420, 1781], xlabel='progress (year)', ylabel='run')   

   yearlist.close()
   runlist.close()
   
   plt.savefig('progress.png', bbox_inches='tight', dpi=dpi)

__EOR__

python3 plot.py

exit 0
