#!/bin/csh  
 
# Programa para:
# Preparar os dados de entrada do CFSR para o Modelo Eta (formato AVN binario)
# Dados do CFSR grade 0.5° x 0.5° 

set datainit = $1

set experimento = CFSR
set dirdata = /stornext/online1/eta/etac/dados_CFSR
set dirout_temp=AVN_temp_${experimento}
set dirout=AVN_${experimento}

mkdir $dirout_temp $dirout

foreach dado (/stornext/online1/eta/etac/dados_CFSR/CFSR_varias/pgbhnl.gdas.${datainit}*)

# Periodo para os dados 3d
set period=`echo $dado | cut -c63-79`
echo " "
echo $period
echo " "

g2ctl ${dirdata}/CFSR_varias/pgbhnl.gdas.${period}.grb2      > pgbhnl.gdas.${period}.grb2_2.ctl
g2ctl ${dirdata}/CFSR_q/pgbhnl.gdas.${period}.grb2           > qpgbhnl.gdas.${period}.grb2_2.ctl
g2ctl ${dirdata}/CFSR_pressao/pgbhnl.gdas.${period}.grb2     > ppgbhnl.gdas.${period}.grb2_2.ctl
g2ctl ${dirdata}/CFSR_pressaosup/ipvhnl.gdas.${period}.grb2  > ipvhnl.gdas.${period}.grb2_2.ctl

sed "s:${dirdata}/CFSR_varias/pgbhnl.gdas.${period}.grb2.idx:pgbhnl.gdas.${period}.grb2.idx:g" pgbhnl.gdas.${period}.grb2_2.ctl > pgbhnl.gdas.${period}.grb2.ctl
sed "s:${dirdata}/CFSR_q/pgbhnl.gdas.${period}.grb2.idx:qpgbhnl.gdas.${period}.grb2.idx:g" qpgbhnl.gdas.${period}.grb2_2.ctl > qpgbhnl.gdas.${period}.grb2.ctl
sed "s:${dirdata}/CFSR_pressao/pgbhnl.gdas.${period}.grb2.idx:ppgbhnl.gdas.${period}.grb2.idx:g" ppgbhnl.gdas.${period}.grb2_2.ctl > ppgbhnl.gdas.${period}.grb2.ctl
sed "s:${dirdata}/CFSR_pressaosup/ipvhnl.gdas.${period}.grb2.idx:ipvhnl.gdas.${period}.grb2.idx:g" ipvhnl.gdas.${period}.grb2_2.ctl > ipvhnl.gdas.${period}.grb2.ctl

rm -f pgbhnl.gdas.${period}.grb2_2.ctl qpgbhnl.gdas.${period}.grb2_2.ctl ppgbhnl.gdas.${period}.grb2_2.ctl ipvhnl.gdas.${period}.grb2_2.ctl

gribmap -i pgbhnl.gdas.${period}.grb2.ctl
gribmap -i qpgbhnl.gdas.${period}.grb2.ctl
gribmap -i ppgbhnl.gdas.${period}.grb2.ctl
gribmap -i ipvhnl.gdas.${period}.grb2.ctl


cat <<EOF> prep_data_AVN.gs
'reinit'

* HGTprs, RHprs, TMPprs, UGRDprs, VGRDprs
'open pgbhnl.gdas.${period}.grb2.ctl'
* SPFHprs
'open qpgbhnl.gdas.${period}.grb2.ctl'
* PRMSLmsl
'open ppgbhnl.gdas.${period}.grb2.ctl'
* PRESsfc
'open ipvhnl.gdas.${period}.grb2.ctl'
* Topo
'open topo.ctl'
* Condicoes de solo Erainterim
*' open solo/Soil_Moisture_Temperature_Jan.ctl'

'set t last'
'q dims'
linha=sublin(result,5)
tempofinal=subwrd(linha,9)
say 'tempofinal= ' tempofinal


tempo=1
while (tempo<=tempofinal)

'set t  'tempo

'q time'
data=subwrd(result,3)
hora=substr(data,1,2)
dia=substr(data,4,2)
mes=substr(data,6,3)
ano=substr(data,11,2)

if (mes=JAN); mm=01; endif

if (mm=01)
'open solo/Soil_Moisture_Temperature_Jan.ctl' 
endif

if (mm=10)
'open solo/Soil_Moisture_Temperature_Out.ctl' 
endif


say hora' 'dia' 'mm' 'ano

* Valores capturados do topo.ctl 
'set lon 240 360'
'set lat -60 20' 

'set fwrite 'ano%mm%dia%hora'0000.ETA_avn'
'set gxout fwrite'

nivel=1
while (nivel<=37)
'set z 'nivel
'd UGRDprs'
nivel=nivel+1
endwhile

nivel=1
while (nivel<=37)
'set z 'nivel
'd VGRDprs'
nivel=nivel+1
endwhile

nivel=1
while (nivel<=37)
'set z 'nivel
'd HGTprs'
nivel=nivel+1
endwhile

nivel=1
while (nivel<=37)
'set z 'nivel
'd SPFHprs.2'
nivel=nivel+1
endwhile

nivel=1
while (nivel<=37)
'set z 'nivel
'd TMPprs'
nivel=nivel+1
endwhile

'set z 1'

'd lterp(topo.5(t=1),UGRDprs(t=1))'

'd PRMSLmsl.3'

'd PRESsfc.4'

'd lterp(topo.5(t=1),UGRDprs(t=1))'


* Condicoes do solo:

'd lterp(STL10_7cm.6(t=1),UGRDprs(t=1))'

'd lterp(SWVL10_7cm.6(t=1),UGRDprs(t=1))'

'd lterp(STL27_28cm.6(t=1),UGRDprs(t=1))'

'd lterp(SWVL27_28cm.6(t=1),UGRDprs(t=1))'

'd lterp(STL328_100cm.6(t=1),UGRDprs(t=1))'

'd lterp(SWVL328_100cm.6(t=1),UGRDprs(t=1))'

'd lterp(STL4100_255cm.6(t=1),UGRDprs(t=1))'

'd lterp(SWVL4100_255cm.6(t=1),UGRDprs(t=1))'

'disable fwrite'

tempo=tempo+1

endwhile


'quit'
EOF

grads -bpc "run prep_data_AVN.gs"
rm -rf prep_data_AVN.gs

foreach arq (*0000.ETA_avn) 
cp $arq entrada
mv $arq ${dirout_temp}
set arq2=`echo $arq | cut -c1-20`
echo Convertendo o arquivo $arq2
./bin_AVN_CLUSTER.x
mv saida ${dirout}/$arq2
rm -rf entrada
end

rm -f pgbhnl.gdas.${period}.grb2.ctl qpgbhnl.gdas.${period}.grb2.ctl ppgbhnl.gdas.${period}.grb2.ctl ipvhnl.gdas.${period}.grb2.ctl
rm -f pgbhnl.gdas.${period}.grb2.idx qpgbhnl.gdas.${period}.grb2.idx ppgbhnl.gdas.${period}.grb2.idx ipvhnl.gdas.${period}.grb2.idx

end

exit

