#!/bin/ksh
set -x

idd=$(printf "%02d" $i)
Caldtg='/home/shian/Caldtg.ksh'
tmps=${uvdtg}
tmpe=${uvdtg}
while [ $tmps -le $tmpe ]
 do

dtg10=${tmps}
dtg_y4=`echo ${dtg10} | cut -c1-4`
dtg_m2=`echo ${dtg10} | cut -c5-6`
dtg_d2=`echo ${dtg10} | cut -c7-8`
dtg_h2=`echo ${dtg10} | cut -c9-10`
dtg=`echo ${dtg10} | cut -c5-6`

 NCL=/cluster/ncl-6.5.0/bin/ncl
 CDO=/cluster/gcc-4.8.5/tools/bin/cdo
 NCKS=/cluster/gcc-4.8.5/tools/bin/ncks


step6_fill()
{
 Var=$1
 infile=$2

 t1=`date +%s`
 level=( 2.00594
         6.01922
         10.0356
         14.0558
         18.0807
         22.1115
         26.1494
         30.1963
         34.254
         38.3253
         42.4132
         46.5216
         50.6554
         54.8204
         59.024
         63.2752
         67.585
         71.9673
         76.4389
         81.0207
         85.7385
         90.624
         95.7164
         101.064
         106.727
         112.778
         119.308
         126.43
         134.281
         143.032
         152.894
         164.125
         177.045
         192.051
         209.628
         230.378
         255.041
         284.534
         319.983
         362.782
         414.647
         477.697
         554.545
         648.417
         763.289
         904.071
         1076.81
         1288.99
         1549.81
         1870.65
         2265.52
         2751.73
         3350.63
         4088.53
         4997.94 )

   ${CDO} splitlevel ${infile} ${Var}
   ${CDO} -b 32 copy ${Var}${level[0]}.nc ${Var}1_c.nc
   ${CDO} fillmiss ${Var}1_c.nc ${Var}1_full.nc
   Str=${Var}'1_full.nc'
   ilev=2
   while [ ${ilev} -le 55 ]
   do
     ilevm1=$(( ${ilev}-1 ))
     levlev=${level[$ilevm1]}
     echo ${CDO} -b 32 copy ${Var}${levlev}.nc ${Var}${ilev}_c.nc
     ${CDO} -b 32 copy ${Var}${levlev}.nc ${Var}${ilev}_c.nc
     ${CDO} fillmiss ${Var}${ilev}_c.nc ${Var}${ilev}_f.nc
     ${CDO} ifthenelse ${Var}${ilev}_f.nc ${Var}${ilev}_f.nc ${Var}${ilevm1}_full.nc ${Var}${ilev}_full.nc
     Str=${Str}' '${Var}${ilev}_full.nc

     ilev=$(( $ilev + 1 ))
   done
#  ${CDO} copy ${Var}44_full.nc ${Var}45_full.nc
#  Str=${Str}' '${Var}45_full.nc

  echo ${Str}
  ${CDO} -O merge ${Str} full_${Var}_${dtg10}.nc
  rm -f ${Var}*.nc
  if [ -f full_${Var}_${dtg10}.nc ]; then
    t2=`date +%s`
    cost_time=$((${t2}-${t1}))
    echo ${cost_time}
    echo 'fillmiss' ${Var} 'ok!'
  fi

}

NWPDTGGLB_Pre=/data/public/HYCOM/shian/forecast/SST/T
NWPWRK_Pre=/data/public/HYCOM/shian/forecast/SST
MAINHYCOM1=${upth}
MAINHYCOM2=${vpth}
NWPBIN=/data/public/HYCOM/shian/forecast/SST
#====== Main
  if [ ! -d ${NWPDTGGLB_Pre}/${dtg}/${dtg_y4} ]; then
    mkdir -p ${NWPDTGGLB_Pre}/${dtg}/${dtg_y4}
  fi
 
  export  HYCOMWRK=${NWPWRK_Pre}/h_work
  if [ ! -s ${HYCOMWRK} ]; then mkdir -p ${HYCOMWRK} ; fi
  cd ${HYCOMWRK}

#========= check TIMCOM IC data & make IC ===========
  if [ ! -f ${NWPDTGGLB_Pre}/${dtg}/${dtg_y4}/full_T_${dtg10}.nc ]; then
    doTIMICr=1
    echo "$doTIMICr"
  else
    echo "${NWPDTGGLB_Pre}/${dtg}/${dtg_y4}/full_T_${dtg10}.nc exit"
    doTIMICr=0
  fi

  doICfHYCOM=0
  if [[ ${doTIMICr} -eq 1 ]]; then
    #=========================
    # 1. Check HYCOM ic
    #=========================
    # if [ ${dtg_h2} -eq 00 ]; then
    #   hycomicdtg=`${NWPBIN}/Caldtg.ksh ${dtg10} -12`
    # fi
    # if [ ${dtg_h2} -eq 12 ]; then
    #   hycomicdtg=`${NWPBIN}/Caldtg.ksh ${dtg10} -24`
    # fi

        hycomu3z=${MAINHYCOM1}

    if [ -s ${hycomu3z} ] ; then
        doICfHYCOM=1
    fi
  fi

  if [ ${doICfHYCOM} -eq 1 ]; then
    #=========================
    # 1. step1_SeparateVar.sh
    #=========================
    rm -f ${HYCOMWRK}/hycom_${dtg10}_*.nc
    ${NCKS} -v water_temp    ${hycomu3z} ${HYCOMWRK}/hycom_${dtg10}_u.nc

    ${CDO} chname,water_temp,T1    ${HYCOMWRK}/hycom_${dtg10}_u.nc   ${HYCOMWRK}/n_hycom_${dtg10}_u.nc

    rm -f ${HYCOMWRK}/hycom_${dtg10}_*.nc

    TIMarea=( 'r' )
    for area in ${TIMarea[@]}
    do
      if [ ${area} == 'r' ]; then
        doIC=${doTIMICr}
        export TCOM_FILE=~/timcom_grid_op.nc
      fi
   
      echo area=${area} doIC=${doIC}
      if [[ ${doIC} -eq 1 ]]; then  
        #=========================
        #=========================
        rm -f ${HYCOMWRK}/remap_hycom_${dtg10}_*.nc
        rm -f ${HYCOMWRK}/remap*.log
        ${CDO} -O remapnn,~/tar_grid.nc ${HYCOMWRK}/n_hycom_${dtg10}_u.nc   ${HYCOMWRK}/remap_hycom_${dtg10}_u.nc   >${HYCOMWRK}/remapU.log &
        wait
  
        #=========================
        # 4. step4_VerInterp.sh
        #=========================
        remapfile=( 'u' )
        for rfl in ${remapfile[@]}; do
          ${CDO} -O intlevel,2.00594472885132,6.0192232131958,10.0356044769287,14.0558137893677,18.0807456970215,22.1115036010742,26.1494483947754,30.1962604522705,34.2540092468262,38.3252563476562,42.4131507873535,46.5215835571289,50.6553573608398,54.8203887939453,59.023983001709,63.2751579284668,67.5850219726562,71.9672927856445,76.4388961791992,81.0207061767578,85.7384643554688,90.6239547729492,95.7163619995117,101.064033508301,106.726638793945,112.77774810791,119.308158874512,126.429878234863,134.281066894531,143.032196044922,152.893569946289,164.124603271484,177.045379638672,192.050735473633,209.627807617188,230.377548217773,255.041320800781,284.533782958984,319.983245849609,362.781799316406,414.646697998047,477.696563720703,554.545104980469,648.416748046875,763.289428710938,904.070617675781,1076.81457519531,1288.990234375,1549.81213378906,1870.64758300781,2265.5205078125,2751.73168945312,3350.62524414062,4088.53247070312,4997.9375  ${HYCOMWRK}/remap_hycom_${dtg10}_${rfl}.nc ${HYCOMWRK}/n_remap_hycom_${dtg10}_${rfl}.nc
        done
        #=========================
        # 5. run full horizontal value
        #=========================
        fileUU=${HYCOMWRK}/n_remap_hycom_${dtg10}_u.nc
        rm -f ${HYCOMWRK}/sfill*.log
        ${CDO} setmisstonn ${fileUU} nn_remap_hycom_${dtg10}_u.nc   >${HYCOMWRK}/sfillU.log &
        wait

        #=========================
        # 6. run full vertical value
        #=========================
        fileU=${HYCOMWRK}/nn_remap_hycom_${dtg10}_u.nc
#        rm -f ${HYCOMWRK}/full_*_${dtg10}.nc

        step6_fill T ${fileU}
   
        #=========================
        # 7. combine all file
        #=========================
        mv ${HYCOMWRK}/full_* ${NWPDTGGLB_Pre}/${dtg}/${dtg_y4}/  
#        rm -f ${HYCOMWRK}/tar_grid*
        rm -f ${HYCOMWRK}/remap_*
        rm -f ${HYCOMWRK}/n_remap*
        rm -f ${HYCOMWRK}/nn_remap*
#        rm -f ${HYCOMWRK}/full_*
        rm -f ${HYCOMWRK}/hycom_*

      fi #end if doIC
    done #end do area(g r)

    rm -f ${HYCOMWRK}/n_hycom_${dtg10}_*.nc
  fi  #end if ${doICfHYCOM}


   tmps=`${Caldtg} ${tmps} 24`
 done
 exit 0
