#!/bin/ksh

edatei=20230101
edatef=20231231

dir_in=/p/projetos/ioper/data/tempo/wwatch/xc50/ww3_v5.16/gl_0p25/brutos
dir_out=/pesq/share/oceanwave/dist/Barbara/modelo_ww3

edate=$edatei
while [ $edate -le $edatef ]; do
echo ${edate}
mkdir -p ${dir_out}/${edate:0:4}/${edate:4:2}/${edate:6:2}

cdo -f nc import_binary ${dir_in}/${edate:0:4}/${edate:4:2}/${edate:6:2}/00/WW_${edate}00.ctl ${dir_out}/${edate:0:4}/${edate:4:2}/${edate:6:2}/temp.nc
cdo select,name=uwnd,vwnd,hs -sellonlatbox,-70,-20,-55,-10 ${dir_out}/${edate:0:4}/${edate:4:2}/${edate:6:2}/temp.nc ${dir_out}/${edate:0:4}/${edate:4:2}/${edate:6:2}/WW_${edate}00_wind_hs.nc
cdo seltimestep,1/8 ${dir_out}/${edate:0:4}/${edate:4:2}/${edate:6:2}/WW_${edate}00_wind_hs.nc ${dir_out}/${edate:0:4}/${edate:4:2}/${edate:6:2}/WW_${edate}00_wind_hs_prev0-21h.nc
cdo seltimestep,9/16 ${dir_out}/${edate:0:4}/${edate:4:2}/${edate:6:2}/WW_${edate}00_wind_hs.nc ${dir_out}/${edate:0:4}/${edate:4:2}/${edate:6:2}/WW_${edate}00_wind_hs_prev24-45h.nc
cdo seltimestep,17/24 ${dir_out}/${edate:0:4}/${edate:4:2}/${edate:6:2}/WW_${edate}00_wind_hs.nc ${dir_out}/${edate:0:4}/${edate:4:2}/${edate:6:2}/WW_${edate}00_wind_hs_prev48-69h.nc
cdo seltimestep,25/32 ${dir_out}/${edate:0:4}/${edate:4:2}/${edate:6:2}/WW_${edate}00_wind_hs.nc ${dir_out}/${edate:0:4}/${edate:4:2}/${edate:6:2}/WW_${edate}00_wind_hs_prev72-93h.nc
cdo seltimestep,33/40 ${dir_out}/${edate:0:4}/${edate:4:2}/${edate:6:2}/WW_${edate}00_wind_hs.nc ${dir_out}/${edate:0:4}/${edate:4:2}/${edate:6:2}/WW_${edate}00_wind_hs_prev96-117h.nc
rm -f ${dir_out}/${edate:0:4}/${edate:4:2}/${edate:6:2}/temp.nc
rm -f ${dir_out}/${edate:0:4}/${edate:4:2}/${edate:6:2}/WW_${edate}00_wind_hs.nc

edate=$(date -u "+%Y%m%d" -d "${edate:0:8} 00:00 24 hour")
done

#  cdo select,name=uwnd,vwnd,hs WW_2021062500.nc WW_2021062500_hs_wind.nc\n
#  cdo -f nc import_binary binary/WW_2021062500.ctl netcdf/WW_2021062500.nc
