#!/bin/ksh

#rodar na aux02

export LANG=US

MONTHLY_ACCUMULATED=0
YEAR_ACCUMULATED=0
MONTHLY_AVERAGE=0
MONTHLY_AVERAGE_YEARLY=0
MONTHLY_ACCUMULATED_YEARLY=1
DAILY_AVERAGE=0

if [ $DAILY_AVERAGE -eq 1 ] ; then
  grads -blc "run DAILY_AVERAGE.gs"
  mv MERGE_CPTEC_12Z*.nc /share/ioper/tempo/MERGE/GPM/CLIMATOLOGY/DAILY_AVERAGE
fi

if [ $MONTHLY_ACCUMULATED -eq 1 ] ; then
 for mes in $(echo "jan feb mar apr may jun jul aug sep oct nov dec") ; do 
  df=$(cal $mes 2019 |  xargs -n 1 |tail -1) 
  echo $mes"  "$df
  grads -blc "run MONTHLY_ACCUMULATED.gs $mes $df"
  mv MERGE_CPTEC_acum_${mes}.nc /share/ioper/tempo/MERGE/GPM/CLIMATOLOGY/MONTHLY_ACCUMULATED
 done
fi

if [ $YEAR_ACCUMULATED -eq 1 ] ; then
 for ano in $(seq 2024 2024) ; do
   echo $ano
   grads -blc "YEAR_ACCUMULATED.gs $ano"
   mv MERGE_CPTEC_acum_$ano.nc /share/ioper/tempo/MERGE/GPM/CLIMATOLOGY/YEAR_ACCUMULATED
 done
fi

if [ $MONTHLY_AVERAGE -eq 1 ] ; then
 for mes in $(echo "jan feb mar apr may jun jul aug sep oct nov dec") ; do 
  df=$(cal $mes 2019 |  xargs -n 1 |tail -1) 
  echo $mes"  "$df
  grads -blc "run MONTHLY_AVERAGE.gs $mes $df"
  mv MERGE_CPTEC_mean_${mes}.nc /share/ioper/tempo/MERGE/GPM/CLIMATOLOGY/MONTHLY_AVERAGE
 done
fi

if [ $MONTHLY_ACCUMULATED_YEARLY -eq 1 ] ; then
gribmap -i /share/ioper/tempo/MERGE/GPM/DAILY/MERGE_CPTEC_2025.ctl
#  for ano in $(seq 2000 2023 ) ; do 
#  for mes in $(echo "jan feb mar apr may jun jul aug sep oct nov dec ") ; do 
  for ano in $(seq 2025 2025 ) ; do 
#  for mes in $(echo "jan feb mar apr  ") ; do 
  for mes in $(echo "apr ") ; do 
    df=$(cal $mes 2019 |  xargs -n 1 |tail -1)
    echo $ano"  "$mes"  "$df
    grads -blc "run MONTHLY_ACCUMULATED_YEARLY.gs $df $mes $ano"
    set -x 
    mv MERGE_CPTEC_acum_${mes}_${ano}.nc  /share/ioper/tempo/MERGE/GPM/CLIMATOLOGY/MONTHLY_ACCUMULATED_YEARLY
  done 
 done 
fi

if [ $MONTHLY_AVERAGE_YEARLY -eq 1 ] ; then
gribmap -i /share/ioper/tempo/MERGE/GPM/DAILY/MERGE_CPTEC_2024.ctl
# for ano in $(seq 2000 2023) ; do 
 for ano in $(seq 2024 2024) ; do 
#  for mes in $(echo "jan feb mar apr may jun jul aug sep oct nov dec") ; do 
  for mes in $(echo "jul aug sep oct ") ; do 
    df=$(cal $mes 2019 |  xargs -n 1 |tail -1)
    echo $ano"  "$mes"  "$df
    grads -blc "run MONTHLY_AVERAGE_YEARLY.gs $df $mes $ano"
    mv MERGE_CPTEC_mean_${mes}_${ano}.nc /share/ioper/tempo/MERGE/GPM/CLIMATOLOGY/MONTHLY_AVERAGE_YEARLY/
  done 
 done 
fi





