#!/bin/sh 
#
# AUTHOR - date
# ===========
# Simona Flavoni  - 02/2010 - LOCEAN
#
# DESCRIPTION
# ===========
# Graphics outputs files are written in my_IDL/PS/ps_ts/.
#
# A global document with all plots in Postscript, PDF or HTML file can be produced.
# 
# EXAMPLES
# ========
# $ ./time_series.sh -exp1 sEXP1 -exp2 sEXP2 -ystart year1 -yend year2 -format pdf
# 
# PROBLEMS
# ========
# no batch processing possible because of SAXO interactiviy Seb is working on it ...
#
# TODO
# ====
# portability on linux
# define overriding policy
# finalize ps and pdf production 
#
usage=" Usage : ${command} -exp1 exp1 -exp2 exp2 -ystart start -yend end -format pdf
 exp1 is your experience name
 exp2 is name of experience of reference 
 ystart is the first year from which you want to compute Time Series 
 yend is the last year for which you want to compute Time Series 
 example:  ./time_series.sh -exp1 core2000 -exp2 core2000 -ystart 1 -yend 2000 -format pdf "
#
minargcount=10
if [ ${#} -lt ${minargcount} ]; then
 echo "Not enought arguments"
 echo "${usage}"
 exit 1
fi
#
while [ ! -z "${1}" ] # ++ pb bash
do
 case ${1} in
     -exp1)	 exp1=${2}        shift  ;;
     -exp2)	 exp2=${2}        shift  ;;
     -ystart)	 start=${2}	  shift	 ;;
     -yend)	 end=${2}	  shift	 ;;
     -format)	 format=${2}	  shift	 ;;
     -h)	
	 echo "${usage}"
	 exit 0	
	 ;;
     *) # other choice
	 echo "${usage}"
	 exit 1
	 ;;
 esac
 shift # next flag
done
#
# ++ check parameters
set -u
#
# define output directory for POSTCRIPT files
#++ change directory
psdir=/Users/sflod/TOOLS/my_IDL/PS/CLIMATO/ts_ps
pdfdir=/Users/sflod/TOOLS/my_IDL/PS/CLIMATO/ts_pdf
htmldir=/Users/sflod/TOOLS/my_IDL/HTML
# test if ${psdir} already exists
if [ -d ${psdir} ]; then
 echo "${psdir} already exist"
 #+++exit 1
else
 mkdir -p ${psdir}
fi 
# test if ${pdfdir} already exists
if [ -d ${pdfdir} ]; then
 echo "${psdir} already exist"
 #+++exit 1
else
 mkdir -p ${pdfdir}
fi 

# memorize current IDL_STARTUP file
memo_idl_startup=${IDL_STARTUP}

# generation of IDL commands file to launch several std_plots commands
rm /tmp/time_series_multi.pro 2> /dev/null
cat << EOF >> /tmp/time_series_multi.pro
 psdir='${psdir}'
 std_ts,'${exp1}','${exp2}',${start},${end},/postscript
 exit
EOF
#
idl_command='/Applications/itt/idl64/bin/idl'  # !! only on my mac
#
# check if IDL available
type ${idl_command}
status_type=${?}
if [ ${status_type} -ne 0 ]
then
 echo "eee : idl not found"
 exit 1
fi
# debug
more /tmp/time_series_multi.pro
#
# launch IDL
${idl_command} /tmp/time_series_multi.pro
status_idl=${?}
# test if idl returns ok
# echo "iii : ${status_idl}"
#
# restore IDL_STARTUP file
IDL_STARTUP=${memo_idl_startup}
export IDL_STARTUP

# produce the final document
case ${format} in
    pdf)
	rm ${pdfdir}/all_ts.pdf 2> /dev/null
	echo "psdir: ${psdir}"
	for file in ${psdir}/*.ps ; do
	    echo "ps2pdf"
		ps2pdf -sPAPERSIZE=a4 ${file}
	done
	ls ${pdfdir}
	mv *.pdf ${pdfdir}/.
	texexec --pdfarrange --result=/tmp/all_ts.pdf ${pdfdir}/*.pdf
	rm -rf *.aux *.log /tmp/*.log
	mv /tmp/all_ts.pdf ${pdfdir}/all_ts.pdf
#commented because convert gives images of bad quality
#	convert ${psdir}/*.ps /tmp/all_ts.pdf
#       convert -resize 800x600 ${psdir}/${exp1}_${exp2}/ps/*.ps /tmp/all_ts.pdf
	;;
    html)
	rm /tmp/time_series.html 2> /dev/null
# check if convert available
	convert_command='/sw/bin/convert'  # !! only on my mac
	type ${convert_command}
	status_type=${?}
	if [ ${status_type} -ne 0 ]
	then
	    echo "eee : convert not found"
	    exit 1
	fi
# debug
	
	for file in `ls ${psdir}/*.ps | sed -e "s/\.ps//"` ; do
	    ${convert_command} -antialias  ${file}.ps  ${file}.png
	done
	 # creation of a HTML file
	cat << EOF > /tmp/time_series.html
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <hr>
       	</head>
	<body>
EOF
	cat /tmp/time_series.txt >>  /tmp/time_series.html
	cat << EOF >> /tmp/time_series.html
<hr/>
EOF
	
	cat << EOF >> /tmp/time_series.html

  </body>
  </html>
EOF
	more /tmp/time_series.html
	;;
    *)	
	echo " format ${format} not implemented"
	exit 1
	;;
esac

    #
#=====================
# prepare to put images on dods
# +++ to finish it
#LOGIN=xxxx
#Tag_Name=ORCA2_LIM2
#rsh ${LOGIN}@gaya.idris.fr exec /bin/ksh <<EOF
#       cd ${homegaya}/IGCM_OUT/${Tag_Name}/CORE2/CLIMA/ts_pdf/${exp1}
#       /usr/local/bin/dods_rm DODS/pub/${LOGIN}/${Tag_Name}/CORE2/CLIMA/ts_pdf/${exp1} > /dev/null 2>&1
#       /usr/local/bin/dods_cp ${exp1} DODS/pub/${LOGIN}/${Tag_Name}/CORE2/CLIMA/ts_pdf/${exp1} > /dev/null 2>&1
#EOF
#=====================
# end
exit 0
