#!/bin/bash

Date=`date +%y%m%d`
XDATE=20$Date\00
echo "XDATE=$XDATE"

### Define the time for each cycle and different days
#xymd=`echo $XDATE | cut -c1-8`
PDY=`echo $XDATE | cut -c1-8`
cyc=`echo $XDATE | cut -c9-10`

#export PDY=${xymd}
#export cyc=00
export WORKdir=/lfs/h2/emc/hur/save/jiayi.peng/CollectVortex
cd $WORKdir

export COMin=/lfs/h1/ops/para/com/hafs/v2.0/hfsa.${PDY}/${cyc}
#export COMout=/lfs/h2/emc/hur/save/jiayi.peng/HFSAvortex
export COMout=/lfs/h2/emc/hur/noscrub/jiayi.peng/HFSAvortex

export NDATEX=/apps/ops/prod/nco/core/prod_util.v2.0.13/exec/ndate
export CDATE=${PDY}${cyc}
ymd=$(echo ${CDATE} | cut -c1-8)
yr=$(echo ${CDATE} | cut -c1-4)
mn=$(echo ${CDATE} | cut -c5-6)
dy=$(echo ${CDATE} | cut -c7-8)
hh=$(echo ${CDATE} | cut -c9-10)

export CDATEp6=`$NDATEX +6 $CDATE`
ymdp6=$(echo ${CDATEp6} | cut -c1-8)
yrp6=$(echo ${CDATEp6} | cut -c1-4)
mnp6=$(echo ${CDATEp6} | cut -c5-6)
dyp6=$(echo ${CDATEp6} | cut -c7-8)
hhp6=$(echo ${CDATEp6} | cut -c9-10)

export CDATEp12=`$NDATEX +12 $CDATE`
ymdp12=$(echo ${CDATEp12} | cut -c1-8)
yrp12=$(echo ${CDATEp12} | cut -c1-4)
mnp12=$(echo ${CDATEp12} | cut -c5-6)
dyp12=$(echo ${CDATEp12} | cut -c7-8)
hhp12=$(echo ${CDATEp12} | cut -c9-10)

export numlist="01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 \
	        21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40"
#----Collect 06hrs forecast vortex
for num in $numlist; do
  if [ -d ${COMin}/${num}l.${CDATE}.RESTART ]; then
    mkdir -p ${COMout}/${num}l.${CDATE}.RESTART	  
    rsync -av ${COMin}/${num}l.${CDATE}.RESTART/${ymdp6}.${hhp6}0000.fv_core.res.nest02.tile2.nc ${COMout}/${num}l.${CDATE}.RESTART/.
    rsync -av ${COMin}/${num}l.${CDATE}.RESTART/${ymdp6}.${hhp6}0000.fv_srf_wnd.res.nest02.tile2.nc ${COMout}/${num}l.${CDATE}.RESTART/.
    rsync -av ${COMin}/${num}l.${CDATE}.RESTART/${ymdp6}.${hhp6}0000.fv_tracer.res.nest02.tile2.nc ${COMout}/${num}l.${CDATE}.RESTART/.
    rsync -av ${COMin}/${num}l.${CDATE}.RESTART/${ymdp6}.${hhp6}0000.sfc_data.nest02.tile2.nc ${COMout}/${num}l.${CDATE}.RESTART/.
    rsync -av ${COMin}/${num}l.${CDATE}.RESTART/grid_mspec.nest02_${yrp6}_${mnp6}_${dyp6}_${hhp6}.tile2.nc ${COMout}/${num}l.${CDATE}.RESTART/.    
  fi
done

#----Collect 00 and 12hrs forecast vortex
export tnumlist="1 2 3 4 5 6 7"
for num in $numlist; do
  echo "${num}L"	
  if [ -d ${COMout}/${num}l.${CDATE}.RESTART ]; then
    for tnum in $tnumlist; do 
      echo "tnum=${tnum}"	    
      if [ -s /lfs/f1/ops/para/tmp/hfsa${tnum}_${cyc}_para_v2.0.3/tmpvit ]; then
        grep "NHC  ${num}L" /lfs/f1/ops/para/tmp/hfsa${tnum}_${cyc}_para_v2.0.3/tmpvit > ${WORKdir}/tmpvit.${num}L.${tnum}	      
	grep "${ymd} ${hh}00" ${WORKdir}/tmpvit.${num}L.${tnum} > ${WORKdir}/tmpvit.${num}L.${tnum}.${CDATE}
        numrecs=`grep "${ymd} ${hh}00" ${WORKdir}/tmpvit.${num}L.${tnum} | wc -l` 
	if [ ${numrecs} -gt 0 ]; then
          export INTERCOMmerge=/lfs/f1/ops/para/tmp/hfsa${tnum}_${cyc}_para_v2.0.3/intercom/RESTART_analysis_merge		
	  echo "${INTERCOMmerge}"
          rsync -av ${INTERCOMmerge}/${ymd}.${hh}0000.fv_core.res.nest02.tile2.nc ${COMout}/${num}l.${CDATE}.RESTART/.
          rsync -av ${INTERCOMmerge}/${ymd}.${hh}0000.fv_srf_wnd.res.nest02.tile2.nc ${COMout}/${num}l.${CDATE}.RESTART/.
          rsync -av ${INTERCOMmerge}/${ymd}.${hh}0000.fv_tracer.res.nest02.tile2.nc ${COMout}/${num}l.${CDATE}.RESTART/.
          rsync -av ${INTERCOMmerge}/${ymd}.${hh}0000.sfc_data.nest02.tile2.nc ${COMout}/${num}l.${CDATE}.RESTART/.
	  rsync -av ${INTERCOMmerge}/grid_mspec.nest02_${yr}_${mn}_${dy}_${hh}.tile2.nc ${COMout}/${num}l.${CDATE}.RESTART/grid_mspec.nest02_${yr}_${mn}_${dy}_${hh}.tile2.nc
#          rsync -av ${INTERCOMmerge}/grid_spec.nest02.tile2.nc ${COMout}/${num}l.${CDATE}.RESTART/grid_mspec.nest02_${yr}_${mn}_${dy}_${hh}.tile2.nc

	  export INTERCOMrestart=/lfs/f1/ops/para/tmp/hfsa${tnum}_${cyc}_para_v2.0.3/intercom/RESTART
	  echo "${INTERCOMrestart}"
          rsync -av ${INTERCOMrestart}/${ymdp12}.${hhp12}0000.fv_core.res.nest02.tile2.nc ${COMout}/${num}l.${CDATE}.RESTART/.
          rsync -av ${INTERCOMrestart}/${ymdp12}.${hhp12}0000.fv_srf_wnd.res.nest02.tile2.nc ${COMout}/${num}l.${CDATE}.RESTART/.
          rsync -av ${INTERCOMrestart}/${ymdp12}.${hhp12}0000.fv_tracer.res.nest02.tile2.nc ${COMout}/${num}l.${CDATE}.RESTART/.
          rsync -av ${INTERCOMrestart}/${ymdp12}.${hhp12}0000.sfc_data.nest02.tile2.nc ${COMout}/${num}l.${CDATE}.RESTART/.
	  export INTERCOMforecast=/lfs/f1/ops/para/tmp/hfsa${tnum}_${cyc}_para_v2.0.3/intercom/forecast
	  echo "${INTERCOMforecast}"
	  rsync -av ${INTERCOMforecast}/grid_mspec.nest02_${yrp12}_${mnp12}_${dyp12}_${hhp12}.tile2.nc ${COMout}/${num}l.${CDATE}.RESTART/.
        fi
      fi
    done 
  fi
done
rm -f tmpvit.*
