#!/bin/sh
set -x

cd $DATA/$fcstsys

grep "run success" $DATA/$fcstsys/$fcstsys'_fore_runsuccess.txt'
for=$?

grep "run success" $DATA/$fcstsys/$fcstsys'_now_runsuccess.txt'
now=$?

echo "grepnow $now"
echo "grepfor $for"

#test size for nowcast and forecast netcdf output files
fldtestsz=40000
statestsz=5000

#size of nowcast netcdf output files
nowstasz=`wc -c stations_nowcast.nc | awk '{print $1}'`
nowfieldsz=`wc -c fields_nowcast.nc | awk '{print $1}'`

if [ $cyc -eq '00' -o $cyc -eq '06' -o $cyc -eq '12' -o $cyc -eq '18' -a $for -eq 0 -a now -eq 0 ]
then

#size of forecast netcdf output files
forstasz=`wc -c stations_forecast.nc | awk '{print $1}'`
forfieldsz=`wc -c fields_forecast.nc | awk '{print $1}'`

if [ $forstasz -gt $statestsz -a $forfieldsz -gt $fldtestsz ];
then

export pgm=copy_${fcstsys}_forecast_output_to_archive_in_ush:nos_glofs_archive.sh
. ./prep_step

cp fields_forecast.nc $COMOUT/${RUN}.${fcstsys}.fields.forecast.${PDY}.t${cyc}z.nc
export err=$?; ./err_chk
statformove1=$err
cp  stations_forecast.nc $COMOUT/${RUN}.${fcstsys}.stations.forecast.${PDY}.t${cyc}z.nc
export err=$?; ./err_chk
statformove2=$err
fi

if [ $SENDDBN = YES ]
then
 
   $DBNROOT/bin/dbn_alert MODEL GLOFS_FCST_NETCDF $job $COMOUT/${RUN}.${fcstsys}.fields.forecast.${PDY}.t${cyc}z.nc
   $DBNROOT/bin/dbn_alert MODEL GLOFS_FCST_NETCDF $job $COMOUT/${RUN}.${fcstsys}.stations.forecast.${PDY}.t${cyc}z.nc
fi

if [ $nowstasz -gt $statestsz -a $nowfieldsz -gt $fldtestsz ];
then

export pgm=copy_${fcstsys}_nowcast_output_to_archive_in_ush:nos_glofs_archive.sh
. ./prep_step

cp fields_nowcast.nc $COMOUT/${RUN}.${fcstsys}.fields.nowcast.${PDY}.t${cyc}z.nc
export err=$?; ./err_chk
statnowmove1=$err
cp stations_nowcast.nc $COMOUT/${RUN}.${fcstsys}.stations.nowcast.${PDY}.t${cyc}z.nc
export err=$?; ./err_chk
statnowmove2=$err
fi

if [ $SENDDBN = YES ]
then
 
   $DBNROOT/bin/dbn_alert MODEL GLOFS_FCST_NETCDF $job $COMOUT/${RUN}.${fcstsys}.fields.nowcast.${PDY}.t${cyc}z.nc
   $DBNROOT/bin/dbn_alert MODEL GLOFS_FCST_NETCDF $job $COMOUT/${RUN}.${fcstsys}.stations.nowcast.${PDY}.t${cyc}z.nc
fi

   if [ $statformove1 -eq 0 -a $statformove2 -eq 0 -a $statnowmove1 -eq 0 -a $statnowmove2 -eq 0 ]
   then  
   echo "ARCHIVE DONE 100" >> $FCSTSYSLOG
   else
   echo "ARCHIVE DONE 0" >> $FCSTSYSLOG
   fi

if [ $SENDCOM = YES ]
then

   export pgm=copy_${fcstsys}_nos_cormsflags_to_archive_in_ush:nos_glofs_archive.sh
. ./prep_step

   cp $FCSTSYSLOG $COMOUT/${RUN}.${fcstsys}_corms_raw.${PDY}.t${cyc}z.txt
   export err=$?; ./err_chk
   
fi

if [ $SENDDBN = YES ]
then
   if [ $DBNROOT = DBNROOT_OFF ];
   then
      echo "DBNROOT not set, alerts will not be sent"
   else
       $DBNROOT/bin/dbn_alert MODEL GLOFS_FCST_TEXT $job $COMOUT/${RUN}.${fcstsys}_corms_raw.${PDY}.t${cyc}z.txt
   fi
fi

else

if [ $nowstasz -gt $statestsz -a $nowfieldsz -gt $fldtestsz ];
then

export pgm=copy_${fcstsys}_nowcast_output_to_archive_in_ush:nos_glofs_archive.sh
. ./prep_step

cp fields_nowcast.nc $COMOUT/${RUN}.${fcstsys}.fields.nowcast.${PDY}.t${cyc}z.nc
export err=$?; ./err_chk
statnowmove1=$err
cp stations_nowcast.nc $COMOUT/${RUN}.${fcstsys}.stations.nowcast.${PDY}.t${cyc}z.nc
export err=$?; ./err_chk
statnowmove2=$err
fi

if [ $SENDDBN = YES ]
then
   
   $DBNROOT/bin/dbn_alert MODEL GLOFS_FCST_NETCDF $job $COMOUT/${RUN}.${fcstsys}.fields.nowcast.${PDY}.t${cyc}z.nc
   $DBNROOT/bin/dbn_alert MODEL GLOFS_FCST_NETCDF $job $COMOUT/${RUN}.${fcstsys}.stations.nowcast.${PDY}.t${cyc}z.nc
fi

   if [ $statnowmove1 -eq 0 -a $statnowmove2 -eq 0 ]
   then
   echo "ARCHIVE DONE 100" >> $FCSTSYSLOG
   else
   echo "ARCHIVE DONE 0" >> $FCSTSYSLOG
   fi

export pgm=copy_${fcstsys}_nos_cormsflags_to_archive_in_ush:nos_glofs_archive.sh
. ./prep_step

cp  $FCSTSYSLOG $COMOUT/${RUN}.${fcstsys}_corms_raw.${PDY}.t${cyc}z.txt
export err=$?; ./err_chk

if [ $SENDDBN = YES ]
then
   if [ $DBNROOT = DBNROOT_OFF ];
   then
      echo "DBNROOT not set, alerts will not be sent"
   else
      $DBNROOT/bin/dbn_alert MODEL GLOFS_FCST_TEXT $job $COMOUT/${RUN}.${fcstsys}_corms_raw.${PDY}.t${cyc}z.txt
   fi
fi

fi
