#! /bin/bash -x

inctime=/home/luiz.sapucci/bin/inctime

data=

for f in $(ls new_AS_3B42RT.*z.bin)
do

  yyyy=${f:14:4}
  mm=${f:19:2}
  dd=${f:22:2}
  hh=${f:25:2}

date_fmt(){

  if [ ${2} -eq 01 ]; then nmm=jan; fi
  if [ ${2} -eq 02 ]; then nmm=feb; fi
  if [ ${2} -eq 03 ]; then nmm=mar; fi
  if [ ${2} -eq 04 ]; then nmm=apr; fi
  if [ ${2} -eq 05 ]; then nmm=may; fi
  if [ ${2} -eq 06 ]; then nmm=jun; fi
  if [ ${2} -eq 07 ]; then nmm=jul; fi
  if [ ${2} -eq 08 ]; then nmm=aug; fi
  if [ ${2} -eq 09 ]; then nmm=sep; fi
  if [ ${2} -eq 10 ]; then nmm=oct; fi
  if [ ${2} -eq 11 ]; then nmm=nov; fi
  if [ ${2} -eq 12 ]; then nmm=dec; fi
 

  export datefmt=${4}:00Z${3}${nmm}${1}
}

  date_fmt ${yyyy} ${mm} ${dd} ${hh}

  fname_ctl=$(echo $f | sed "s,.bin,.ctl,g")

cat << EOF > ${fname_ctl}
dset ^${f}

options sequential                                                             
title Three Hourly TRMM and Other Satellite Rainfall (3B42RT)                   
undef -9.99e+08                                                                 

xdef  190 linear  277.375 0.25
ydef  246 linear  -49.875 0.25                                       

zdef 1 levels 1000                                                              

*tdef 1 linear 00:00Z5jan2016 3hr
tdef 1 linear ${datefmt} 3hr

* end_time 12:00Z12may2016 (this_is_comment_line)
vars 1                                                                          
r         0   99 Hourly Rain Rate (mm/hr)                                       
endvars    
EOF
done

exit 0
