#! /bin/sh

yrs=20
yre=30
ses=1
see=12


#vname=(   't'   'ts'      'dod'            'wiod'           )
#vnamej=(  '温度' '地面温度' 'ダストの光学的厚さ' '水氷雲の光学的厚さ' )
#addinfo=( '(時間-緯度分布, 経度-緯度分布, 緯度-気圧分布)' \
#          '(時間-緯度分布, 経度-緯度分布)'              \
#          '(時間-緯度分布, 経度-緯度分布)'              \
#          '(時間-緯度分布, 経度-緯度分布)' )


vname=(   't'   'pt'   'ss'   'u'                'ts'      'dod'            'wiod'           )
vnamej=(  '温度' '温位' '安定度' '東西風(tentative)' '地面温度' 'ダストの光学的厚さ' '水氷雲の光学的厚さ' )
addinfo=( '(時間-緯度分布, 経度-緯度分布, 緯度-気圧分布)' \
          '(緯度-気圧分布)' \
          '(緯度-気圧分布)' \
          '(緯度-気圧分布)' \
          '(時間-緯度分布, 経度-緯度分布)'              \
          '(時間-緯度分布, 経度-緯度分布)'              \
          '(時間-緯度分布, 経度-緯度分布)' )





#vname=(   't' )
#vnamej=(  '温度' )

#vname=(   'ts'      'dod'            'wiod' )
#vnamej=(  '地面温度' 'ダストの光学的厚さ' '水氷の光学的厚さ' )

lst=(     2         14 )
plev=(    3.70      1.36      0.50       0.18 \
          3.70      1.36      0.50       0.18 \
         -1.0      -1.0      -1.0       -1.0       -1.0        -1.0 )


imgdirbase="./figdir"

tmpfn="###tmpfile"

toprd="topindex.rd"
tophtml="topindex.htm"

topdir="/work01/yot/work/dataanalysis/chart/mkhtml"

cp /dev/null ${toprd}
cat topindex_top.rd >> ${toprd}

echo "#== 図およびデータ" >> ${toprd}
echo "== 図" >> ${toprd}



#echo "以下の図において年は火星年を表し, 1 年目は MGS が火星に到着した年 (1997/09/11) としています." >> ${toprd}
#echo " " >> ${toprd}


echo "以下の図において年は火星年 (Mars Year; MY) を表し, 1 年目は Clancy et al. (2000) に従って 1955 年 4 月 11 日としています." >> ${toprd}
echo " " >> ${toprd}
echo "また, 以下のページにおいて火星における季節は黄経 " >> ${toprd}
echo "(areocentric solar longitude, Ls) で表しており, " >> ${toprd}
echo "Ls = 0, 90, 180, 270 度はそれぞれ" >> ${toprd}
echo "北半球の春分, 夏至, 秋分, 冬至を表します." >> ${toprd}

#The Mars Year (MY) numbering scheme used here is that of Clancy et al. (2000).
# abstract by Lewis et al.



#echo "  注意！　現状では 2 年目と 5 年目のデータは上手くリンクされていません." >> ${toprd}

ivar=0
while test ${ivar} -lt ${#vname[*]}
  do 


  echo "*${vnamej[$ivar]} ${addinfo[$ivar]}" >> ${toprd}
  echo " " >> ${toprd}


  iyr=${yrs}
  while test ${iyr} -le ${yre}
    do 

    echo ${iyr} > ${tmpfn}
    wl=`cat ${tmpfn} | wc -c`
    rm ${tmpfn}
    wl=`expr ${wl} - 1`
    co=2
    fntail=""
    while test ${co} -gt ${wl}
      do
      fntail=${fntail}0
      co=`expr ${co} - 1`
    done
    fntail=${fntail}${iyr}
    cyr=${fntail}


    sw_exist=0

    ilst=0
    while test ${ilst} -lt ${#lst[*]}
      do 

      echo ${lst[$ilst]} > ${tmpfn}
      wl=`cat ${tmpfn} | wc -c`
      rm ${tmpfn}
      wl=`expr ${wl} - 1`
      co=2
      fntail=""
      while test ${co} -gt ${wl}
        do
        fntail=${fntail}0
        co=`expr ${co} - 1`
      done
      fntail=${fntail}${lst[$ilst]}
      clst=${fntail}

      imgdir=${imgdirbase}/my${cyr}/${vname[$ivar]}_${clst}lst
      if [ -d ${imgdir} ] ; then
        sw_exist=1
      fi

      ilst=`expr ${ilst} + 1`
    done


    if [ $sw_exist -ge 1 ] ; then


      ilst=0

      echo ${lst[$ilst]} > ${tmpfn}
      wl=`cat ${tmpfn} | wc -c`
      rm ${tmpfn}
      wl=`expr ${wl} - 1`
      co=2
      fntail=""
      while test ${co} -gt ${wl}
	do
	fntail=${fntail}0
	co=`expr ${co} - 1`
      done
      fntail=${fntail}${lst[$ilst]}
      clst=${fntail}

      imgdir0=${imgdirbase}/my${cyr}/${vname[$ivar]}_${clst}lst


      ilst=1

      echo ${lst[$ilst]} > ${tmpfn}
      wl=`cat ${tmpfn} | wc -c`
      rm ${tmpfn}
      wl=`expr ${wl} - 1`
      co=2
      fntail=""
      while test ${co} -gt ${wl}
	do
	fntail=${fntail}0
	co=`expr ${co} - 1`
      done
      fntail=${fntail}${lst[$ilst]}
      clst=${fntail}

      imgdir1=${imgdirbase}/my${cyr}/${vname[$ivar]}_${clst}lst


      if [ -d ${imgdir0} ] ; then
        if [ -d ${imgdir1} ] ; then
	  echo "  * MY ${iyr} : 地方時 ((<${lst[0]} 時|URL:${imgdir0}/index.htm>)) / ((<${lst[1]} 時|URL:${imgdir1}/index.htm>))" \
	      >> ${toprd}
	else
	  echo "  * MY ${iyr} : 地方時 ((<${lst[0]} 時|URL:${imgdir0}/index.htm>)) / ${lst[1]} 時" \
	      >> ${toprd}
	fi
      else
        if [ -d ${imgdir1} ] ; then
	  echo "  * MY ${iyr} : 地方時 ${lst[0]} 時 / ((<${lst[1]} 時|URL:${imgdir1}/index.htm>))" \
	      >> ${toprd}
	fi
      fi


      ilst=0
      while test ${ilst} -lt ${#lst[*]}
        do 

        echo ${lst[$ilst]} > ${tmpfn}
        wl=`cat ${tmpfn} | wc -c`
        rm ${tmpfn}
        wl=`expr ${wl} - 1`
        co=2
        fntail=""
        while test ${co} -gt ${wl}
          do
          fntail=${fntail}0
          co=`expr ${co} - 1`
        done
        fntail=${fntail}${lst[$ilst]}
        clst=${fntail}


        imgdir=${imgdirbase}/my${cyr}/${vname[$ivar]}_${clst}lst


        if [ -d ${imgdir} ] ; then

          if [ ${vname[$ivar]} = "t" ] ; then
            cp template_t_index.txt          template_index.txt
          else
            if [ ${vname[$ivar]} = "pt" ] ; then
              cp template_latponly_index.txt    template_index.txt
            else
              if [ ${vname[$ivar]} = "ss" ] ; then
                cp template_latponly_index.txt  template_index.txt
	      else
		if [ ${vname[$ivar]} = "u" ] ; then
		  cp template_latponly_index.txt  template_index.txt
		else
		  cp template_others_index.txt template_index.txt
		fi
              fi
            fi
          fi


          sed s:CYEAR:${cyr}:g template_index.txt \
	      | sed -e s:YEAR:${iyr}:g \
	      | sed -e s:CLST:${clst}:g \
	      | sed -e s:LST:${lst[$ilst]}:g \
	      | sed -e s:VARNAMEJ:${vnamej[$ivar]}:g \
	      | sed -e s:VARNAME:${vname[$ivar]}:g \
	      > index.txt

	  cp index.txt ${imgdir}
	  rm index.txt
	  cp dcmodel-thum-make.rb ${imgdir}
	  cd ${imgdir}
	  pwd
	  ruby dcmodel-thum-make.rb
	  cd ${topdir}

	fi

	ilst=`expr ${ilst} + 1`
      done

      echo " " >> ${toprd}

    fi


    iyr=`expr ${iyr} + 1`
  done

  echo " " >> ${toprd}
  echo " " >> ${toprd}
  echo " " >> ${toprd}

  ivar=`expr ${ivar} + 1`
done


cat topindex_bot.rd >> ${toprd}

#echo "((:<hr>:))" >> ${toprd}
#echo "This page was created on "`date +"%B %e, %G"`"." >> ${toprd}


rd2 -r rd/rd2html-ext-lib.rb --with-css=dcmodel.css --out-code=euc --ref-extension --native-inline --head-element --html-title="MGS-TES atmospheric observation" topindex.rd > ${tophtml}

#echo "--------------------------------------"
#echo "Creating animation file," ${gifdir}/MGS_anim.gif
#gifsicle --delay 100 --loopcount=0 ${gifdir}/dcl-????.gif > ${gifdir}/anim.gif
