#!/bin/ksh  
VAR=TMAX

datai=20240101
dataf=20241231

cdo=/stornext/home/io/bin/cdo-1.8.2/bin/cdo
out=/share/ioper/tempo/SAMeT/CLIMATOLOGY/DAILY_AVERAGE/$VAR

cd /share/ioper/tempo/SAMeT/DAILY/${VAR}

if [ $VAR == "TMAX" ] ; then
  vv="tmax"
fi

while [ $datai -le $dataf ] ; do 
 arq_saida=SAMeT_CPTEC_${VAR}_00Z$(LANG=US date -d "${datai:0:8} 00:00 0 hours ago" +"%d%b"| tr 'A-Z' 'a-z').nc
 rm -f media_${VAR}_${datai}.nc 
 rm -f ${out}/$arq_saida
 echo $datai"  "$arq_saida
 ls -1 ????/${datai:4:2}/SAMeT_CPTEC_${VAR}_????${datai:4:2}${datai:6:2}.nc |grep -v ^2025 >lista_${datai}.txt
 $cdo ensmean $(cat lista_${datai}.txt) media_${VAR}_${datai}.nc

 $cdo delname,nobs media_${VAR}_${datai}.nc temp_${datai}.nc
 $cdo chname,$vv,t$vv temp_${datai}.nc ${out}/$arq_saida

# $cdo chname,$vv,t$vv -delname,nobs media_${VAR}_${datai}.nc ${out}/$arq_saida
 rm -f media_${VAR}_${datai}.nc  lista_${datai}.txt temp_${datai}.nc
 datai=`date -d "${datai:0:8} 00:00 24 hours" +"%Y%m%d"`
done
exit
