 #!/bin/bash -x
 path1="/lustre_xc50/paulo_kubota/operacao/grb2"

echo ${path1}
cd ${path1}
  grb2ctl="/lustre_xc50/paulo_kubota/operacao/grb2/g2ctl"

#for ii in $(  ls -lF | grep / | awk '{print $9}' | head -2 | awk '{print substr($1,1,10)}' ); do
for ii in $(  ls -lF | grep / | awk '{print $9}' | awk '{print substr($1,1,10)}' ); do
#for ii in $(  ls -lF | grep / | awk '{print $9}'| tail -10 | head -1 | awk '{print substr($1,1,10)}' ); do
  DATA=${ii}
  year=`echo ${DATA} | awk '{print substr($1,1,4)}'`
  mon=`echo ${DATA}  | awk '{print substr($1,5,2)}'`
  day=`echo ${DATA}  | awk '{print substr($1,7,2)}'`
  hour=`echo ${DATA} | awk '{print substr($1,9,2)}'`

  iyear=`expr ${year} / 1`
  imon=`expr ${mon} / 1`
  iday=`expr ${day} / 1`
  ihour=`expr ${hour} / 1`

  cmon=${imon}
  if [[ ${imon} -lt 10 ]]; then
    cmon=0${imon}
  fi
  cday=${iday}
  if [[ ${iday} -lt 10 ]]; then
    cday=0${iday}
  fi

  cd ${path1}/${iyear}${cmon}${cday}${hour}/

  ln -s ${path1}/${iyear}${cmon}${cday}${hour}/GPOSNMC${iyear}${cmon}${cday}${hour}${iyear}${cmon}${cday}${hour}P.icn.TQ0666L064.grb2        ${path1}/${iyear}${cmon}${cday}${hour}/GPOSNMC${iyear}${cmon}${cday}${hour}${iyear}${cmon}${cday}${hour}P.fct.TQ0666L064.grb2
  for ii in $(ls ${path1}/${iyear}${cmon}${cday}${hour}/*.grb2); do 
     dir_path=`dirname ${ii}`
     file_grb2=`basename ${ii} `
     file_idx=`basename ${ii} | awk '{print substr($1,1,43)}'`
     file_ctl=`basename ${ii} | awk '{print substr($1,1,43)".ctl"}'`
     ${grb2ctl}   ${file_grb2} > ${file_ctl}
     sed s/"index ^${file_idx}.grb2.idx"/"index ^${file_idx}.idx"/g   ${dir_path}/${file_ctl} > ${path1}/tmp0.txt
     cp ${path1}/tmp0.txt  ${dir_path}/${file_ctl}
     /lustre_xc50/paulo_kubota/lib/grads-1.9b4/bin//gribmap  -i  ${file_ctl}  
  done
  tmpdate=` grep -i tdef  GPOSNMC${iyear}${cmon}${cday}${hour}${iyear}${cmon}${cday}06P.fct.TQ0666L064.ctl | awk '{print $4}'`
  tmp=` echo ${tmpdate} | awk '{print substr($1,3,12)}'`
  cdate=${hour}${tmp}
  sed s/"dset ^GPOSNMC${iyear}${cmon}${cday}${hour}${iyear}${cmon}${cday}06P.fct.TQ0666L064.grb2"/"dset ^GPOSNMC${iyear}${cmon}${cday}${hour}%y4%m2%d2%h2P.fct.TQ0666L064.grb2"/g ${dir_path}/GPOSNMC${iyear}${cmon}${cday}${hour}${iyear}${cmon}${cday}06P.fct.TQ0666L064.ctl > ${path1}/tmp1.txt
  sed s/"index ^GPOSNMC${iyear}${cmon}${cday}${hour}${iyear}${cmon}${cday}06P.fct.TQ0666L064.idx"/"index ^GPOSNMC${iyear}${cmon}${cday}${hour}P.fct.TQ0666L064.idx"/g ${path1}/tmp1.txt > ${path1}/tmp2.txt
  sed s/"options pascals"/"options pascals template"/g ${path1}/tmp2.txt > ${path1}/tmp3.txt
  sed s/"* produced by g2ctl v0.1.8"/"options pascals template"/g ${path1}/tmp3.txt > ${path1}/tmp4.txt
  sed s/"tdef 1 linear ${tmpdate} 1mo"/"tdef 41 linear ${cdate} 6hr"/g ${path1}/tmp4.txt > ${dir_path}/GPOSNMC${iyear}${cmon}${cday}${hour}P.fct.TQ0666L064.ctl

  /lustre_xc50/paulo_kubota/lib/grads-1.9b4/bin//gribmap -v -i ${dir_path}/GPOSNMC${iyear}${cmon}${cday}${hour}P.fct.TQ0666L064.ctl
  chmod -R 755  ${dir_path}

  rm -f ${path1}/tmp*.txt

done

exit
