function main(args)

'reinit'

var=subwrd(args,1)
out=subwrd(args,2)

say out

'open ../DAILY/'var'/SAMeT_CPTEC_'var'.ctl'


'set stat on'
meses="JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC"
meses="jan feb mar apr may jun jul aug sep oct nov dec"
  dff=" 31  28 31   30  31  30  31  31  30  31  30  31"
mm=1
while(mm<=12)
 mes=subwrd(meses,mm)
 df=subwrd(dff,mm)
 dd=1
 while(dd<=df)
   ano=2000
   'pTMP=lat*0'
   cont=0
   while(ano<=2024)
     if (dd<10)
      time=00Z0%dd%mes%ano
      ttime=00Z0%dd%mes
     else
      time=00Z%dd%mes%ano
      ttime=00Z%dd%mes
     endif
     'set gxout stat'
     'd 'var'(time='time')'
     stat=sublin(result,7)
     ValidCount=subwrd(stat,8)
     if (ValidCount>0) 
      'pTMP='var'(time='time')+pTMP'
       cont=cont+1
      say mes"  "dd"  "ano"  "time"  "ValidCount"  "cont
     endif
     ano=ano+1
   endwhile
   'set time 'ttime%2020
   'T'var'=pTMP/'cont
   'set sdfwrite -nc4 -3dt 'out'/SAMeT_CPTEC_'var'_'%ttime'.nc'
   'sdfwrite  T'var
   dd=dd+1
 endwhile 
 mm=mm+1
endwhile


*calculo para o dia 29 fevereiro
'ppTMP='var'(time=00Z29feb2000)+'var'(time=00Z29feb2004)+'var'(time=00Z29feb2008)+'var'(time=00Z29feb2012)+'var'(time=00Z29feb2016)+'var'(time=00Z29feb2020)+'var'(time=00Z29feb2024)'
'set time 00Z29feb2020'
'T'var'=ppTMP/6'
'set sdfwrite -nc4 -3dt 'out'/SAMeT_CPTEC_'var'_00Z29feb.nc'
'sdfwrite  T'var
'quit'
return











