#!/bin/csh

set Run_Date=${1}

set dir_in=/scratchout/grupos/eta/home/etaclim/sazonal/worketa/eta/227X351X38_oper_kieee/binctl/${Run_Date}
set dir_scr=/scratchin/grupos/eta/home/etaclim/sazonal/worketa/eta/227X351X38_oper_kieee/scripts
set dir_out=/stornext/online1/eta/etaclim/climatologia_nova/BMJ_ZHAO/media_mensal

mkdir -p ${dir_out}/${Run_Date}
rm -f ${Run_Date}.bin 
date

set mes=`/scratchin/grupos/eta/home/etaclim/sazonal/worketa/util/caldate.3.0 ${Run_Date} + 30d  "mmm"`
set ano=`/scratchin/grupos/eta/home/etaclim/sazonal/worketa/util/caldate.3.0 ${Run_Date} + 30d  "yyyy"`
#echo $mes $ano
#exit

cat ${dir_scr}/Etasa40km_media.ctl | sed -e "s:date_in:${Run_Date}:g" | sed -e "s:data:${mes}${ano}:g" > ${dir_out}/${Run_Date}/media_eta40km${Run_Date}.ctl

set tempo=30
while ( ${tempo} <= 120 ) 

set M1=`/scratchin/grupos/eta/home/etaclim/sazonal/worketa/util/caldate.3.0 ${Run_Date} + ${tempo}d  "yyyymm"`

echo $M1 $tempo

rm -f fcst2.txt qtd_arq2.txt media2.x

foreach arq (`find . -name `${dir_in}/Etasa40km${Run_Date}+${M1}*.bin` | xargs -i -t | echo`)

echo $arq >> fcst2.txt

end

set qtd_arq=`more fcst2.txt |wc -l `
echo $qtd_arq > qtd_arq2.txt

pgf90 -o media2.x media_lendo_bin2.f90
media2.x
cat fort.93 >> ${dir_out}/${Run_Date}/${Run_Date}.bin
rm -f fort.93
@ tempo = ${tempo} + 30
end
rm -f fcst2.txt qtd_arq2.txt media2.x
date
exit
