#!/bin/csh -fx
# ------------------------------
#PBS -W group_list=g0613
#
#PBS -N simcov
#PBS -l ncpus=4          
#PBS -l walltime=12:00:00 
#PBS -S /bin/csh
#PBS -j eo
#BSUB -J simcov
#BSUB -n 4
#BSUB -W 4:00
#BSUB -o simconv.%J
#BSUB -e simconv.%J
# ------------------------------
#
# driver script for simulating OSSE convent2ional data
#   Feburary  2008 : Errico: original job script working at palm
#  
#   Feb-13-2008 : Yang  add changes to make RE's original script
#       to run as a batch job at palm
#   Feb-14-2008 : Yang--- when ntimes is large the current way to sign arrays file_names_xx 
#                         does not work due to too many contents.
#                         Made a simple changes for the arrays (those arrays are used for dmget)--
#                         just do dmget for different month.  Due to the computing time used
#                         do simulation a week by week for the test period with starting 06z.
#                         Need to work on a flexible script later....
#-------------------------------------
# Experiment environment
# ----------------------
  setenv SIMHOME  /cxfsm/nobackup11/ryang/OSSE2/OSSE2/Simtest
# not necessary to add SIMROOT--except copy files
  setenv SIMROOT  /home0/ryang/GSimobs/bin
  setenv BUFRBIN  /home/meta/bin/Linux

  mkdir -p       /lscr1/$user/fvwork.$$
  setenv SIMWORK /lscr1/$user/fvwork.$$

   /bin/mkdir -p $SIMWORK            # create working directory

# Make sure files are acessible
# -----------------------------
  umask 022

  echo $SIMWORK > $SIMHOME/.SIMWORK  # record working directory

  cd $SIMWORK

#
# script to create simulated observations from the ECMWF NR for OSSE
# initial script written by Ronald Errico, Jing Guo, and Runhua Yang 
# USER SET VARIABLES

# set initial part of name for NR data files
set NR_path_data="/output/ryang/NATURE2005"

# set initial symoptic date time
set datetime1="2006010100"   
#set datetime1="2006010318"   

# set number of synoptic times to process ( set to >130 if to end of month)

@ ntimes = 20                

# set file paths to real observations
# only set initial part (before datetime) of bufr input file names 
# set equal to "none" means don't process

set BF_path_MASS="/archive/input/dao_ops/ops/flk/ncep_g5obs/bufr/PREPBUFR"
set BF_path_WIND="/archive/input/dao_ops/ops/flk/ncep_g5obs/bufr/PREPBUFR"

set BF_outp_WIND="/output/ryang/OSSE2005/SIMOBSNOERR/Wind"
set BF_outp_MASS="/output/ryang/OSSE2005/SIMOBSNOERR/Mass"

# END OF USER SET VARIABLES
# ----------------------------------------------------------------------


mkdir -p $BF_outp_WIND
mkdir -p $BF_outp_MASS

date
 
# later need to make the final data/file structure/directory
# little bit confusing Now!
# copy some required files to local directory

rm -f addtime.x sim_conv.x  ossegrid.txt
/bin/cp $SIMROOT/addtime.x addtime.x
/bin/cp $SIMROOT/sim_conv.x sim_conv.x
/bin/cp  /home0/rmerrico/OSSE2/Sim5/txt/ossegrid.txt .

# set some constants
set add3h=+03
set add6h=+06
set sub3h=-03
set sub6h=-06

# separate initial date and time parameters from initial datetime  
set result=`addtime.x $datetime1 +00`
set yyyy=`echo $result | cut -d" " -f1`
set mm=`echo $result | cut -d" " -f2`
set dd=`echo $result | cut -d" " -f3`
set hh=`echo $result | cut -d" " -f4`
set datetime="${yyyy}${mm}${dd}${hh}"
@ yy = $yyyy % 10 
set yy=`printf "%2.2d\n" $yy`

# reduce $ntimes if necessary so that all requested times are 
# during the same month

if ($ntimes > 1) then
  set datetime2 = "${datetime}"
  @ ilast = 1
  @ i = 2
  while ( $i <= $ntimes ) 
    set result=`addtime.x $datetime2 $add6h`
    set yyyy2=`echo $result | cut -d" " -f1`
    set mm2=`echo $result | cut -d" " -f2`
    set dd2=`echo $result | cut -d" " -f3`
    set hh2=`echo $result | cut -d" " -f4`
    set datetime2="${yyyy2}${mm2}${dd2}${hh2}"
    if ($mm2 == $mm) then
      @ ilast = $i
    endif
    @ i++
  end
  @ ntimes = $ilast
endif
echo 'adjusted ntimes', $ntimes

# ----------------------------------------------------------------------
# dmget all required NR data

echo "dmget all required NR data"

# Check if need NR data from last time of month before initial one 
# (This occurs if datetime1 is first time of month)

if ($dd == "01"  && $hh == "00") then 
  echo 'dmget of NR data from end of previous month'
  echo 'where I am??'
  set result=`addtime.x $datetime1 $sub3h`
  set yyyy2=`echo $result | cut -d" " -f1`
  set mm2=`echo $result | cut -d" " -f2`
  set dd2=`echo $result | cut -d" " -f3`
  set hh2=`echo $result | cut -d" " -f4`
  set datetime2="${yyyy2}${mm2}${dd2}${hh2}"
  dmget "${NR_path_data}/Y${yyyy2}/M${mm2}/pres_${datetime2}z.bin" 
  dmget "${NR_path_data}/Y${yyyy2}/M${mm2}/surf_${datetime2}z.bin" 
  if ($BF_path_WIND != "none") then
    dmget ${NR_path_data}/Y${yyyy2}/M${mm2}/udat_${datetime2}z.bin" 
    dmget ${NR_path_data}/Y${yyyy2}/M${mm2}/vdat_${datetime2}z.bin" 
  endif
  if ($BF_path_MASS != "none") then
    dmget ${NR_path_data}/Y${yyyy2}/M${mm2}/tdat_${datetime2}z.bin" 
    dmget ${NR_path_data}/Y${yyyy2}/M${mm2}/qdat_${datetime2}z.bin" 
  endif
endif  

# dmget all NR data files from request month

echo "dmget of all required NR files in requested month"
dmget ${NR_path_data}/Y${yyyy}/M${mm}/pres*bin
dmget ${NR_path_data}/Y${yyyy}/M${mm}/surf*bin

if ($BF_path_WIND != "none") then
  dmget ${NR_path_data}/Y${yyyy}/M${mm}/udat*bin
  dmget ${NR_path_data}/Y${yyyy}/M${mm}/vdat*bin
endif

if ($BF_path_MASS != "none") then
  dmget ${NR_path_data}/Y${yyyy}/M${mm}/tdat*bin
  dmget ${NR_path_data}/Y${yyyy}/M${mm}/qdat*bin
endif

# ----------------------------------------------------------------------
# dmget input BUFR data files
echo "dmget of required BUFR data"

if ( $BF_path_WIND != "none" ) then
  dmget ${BF_path_WIND}/Y${yyyy}/M${mm}/gdas1.${yy}${mm}*prepbufr
  /bin/ls -1  ${BF_path_WIND}/Y${yyyy}/M${mm}/gdas1.${yy}${mm}*prepbufr
endif

if ( $BF_path_MASS != "none"  && $BF_path_MASS != $BF_path_WIND ) then
  dmget ${BF_path_MASS}/Y${yyyy}/M${mm}/gdas1.${yy}${mm}*t*prepbufr
  /bin/ls -1  ${BF_path_MASS}/Y${yyyy}/M${mm}/gdas1.${yy}${mm}*prepbufr
endif

# ----------------------------------------------------------------------
# produce file names for first synoptic time - 3 hours 
echo "produce file names for first synoptic time - 3 hours"

set result=`addtime.x $datetime1 $sub3h`
set yyyy=`echo $result | cut -d" " -f1`
set mm=`echo $result | cut -d" " -f2`
set dd=`echo $result | cut -d" " -f3`
set hh=`echo $result | cut -d" " -f4`
set datetime="${yyyy}${mm}${dd}${hh}"
echo "datetime=" $datetime

set file_pres="${NR_path_data}/Y${yyyy}/M${mm}/pres_${datetime}z.bin"

set file_surf="${NR_path_data}/Y${yyyy}/M${mm}/surf_${datetime}z.bin"

if ($BF_path_WIND != "none") then
  set file_udat="${NR_path_data}/Y${yyyy}/M${mm}/udat_${datetime}z.bin"
  set file_vdat="${NR_path_data}/Y${yyyy}/M${mm}/vdat_${datetime}z.bin"
endif

if ($BF_path_MASS != "none") then
  set file_tdat="${NR_path_data}/Y${yyyy}/M${mm}/tdat_${datetime}z.bin"
  set file_qdat="${NR_path_data}/Y${yyyy}/M${mm}/qdat_${datetime}z.bin"
endif

# x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 
# loop over synoptic times

@ i = 1
@ ilast =  $ntimes  
echo '$ilast', ilast
while ( $i <= $ntimes ) 
  
# x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 
# link to required NR .bin files for current synoptic time and 3 hours earlier

  set result=`addtime.x $datetime $add3h`
  set yyyy=`echo $result | cut -d" " -f1`
  set mm=`echo $result | cut -d" " -f2`
  set dd=`echo $result | cut -d" " -f3`
  set hh=`echo $result | cut -d" " -f4`
  set datetime="${yyyy}${mm}${dd}${hh}"
  @ yy = $yyyy % 10 
  set yy=`printf "%2.2d\n" $yy`

  echo " "
  echo "Top of loop for ${datetime}"
  date

  rm -f pres_NR_01
  rm -f pres_NR_02
  ln -s $file_pres pres_NR_01
  set filename="${NR_path_data}/Y${yyyy}/M${mm}/pres_${datetime}z.bin"
  ln -s $filename pres_NR_02

  rm -f surf_NR_01
  rm -f surf_NR_02
  ln -s $file_surf surf_NR_01
  set filename="${NR_path_data}/Y${yyyy}/M${mm}/surf_${datetime}z.bin"
  ln -s $filename surf_NR_02

  if ($BF_path_WIND != "none") then
    rm -f udat_NR_01
    rm -f udat_NR_02
    rm -f vdat_NR_01
    rm -f vdat_NR_02
    rm -f file_WIND_bfr
    ln -s $file_udat udat_NR_01
    ln -s $file_vdat vdat_NR_01
    set filename="${NR_path_data}/Y${yyyy}/M${mm}/udat_${datetime}z.bin"
    ln -s $filename udat_NR_02
    set filename="${NR_path_data}/Y${yyyy}/M${mm}/vdat_${datetime}z.bin"
    ln -s $filename vdat_NR_02
    set filename="${BF_path_WIND}/Y${yyyy}/M${mm}/gdas1.${yy}${mm}${dd}.t${hh}z.prepbufr"
    ln -s $filename file_WIND_bfr
  endif

  if ($BF_path_MASS != "none") then
    rm -f tdat_NR_01
    rm -f tdat_NR_02
    rm -f qdat_NR_01
    rm -f qdat_NR_02
    rm -f file_MASS_bfr
    ln -s $file_tdat tdat_NR_01
    ln -s $file_qdat qdat_NR_01
    set filename="${NR_path_data}/Y${yyyy}/M${mm}/tdat_${datetime}z.bin"
    ln -s $filename tdat_NR_02
    set filename="${NR_path_data}/Y${yyyy}/M${mm}/qdat_${datetime}z.bin"
    ln -s $filename qdat_NR_02
    set filename="${BF_path_MASS}/Y${yyyy}/M${mm}/gdas1.${yy}${mm}${dd}.t${hh}z.prepbufr"
    ln -s $filename file_MASS_bfr
  endif

# x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 
# produce file names for first synoptic time + 3 hours 
# these will beome names for next synoptic time - 3 hours 
# link to required NR binary files for current synoptic time + 3 hours

  set result=`addtime.x $datetime $add3h`
  set yyyy=`echo $result | cut -d" " -f1`
  set mm=`echo $result | cut -d" " -f2`
  set dd=`echo $result | cut -d" " -f3`
  set hh=`echo $result | cut -d" " -f4`
  set datetime="${yyyy}${mm}${dd}${hh}"

  rm -f pres_NR_03
  set file_pres="${NR_path_data}/Y${yyyy}/M${mm}/pres_${datetime}z.bin"
  ln -s $file_pres pres_NR_03

  rm -f surf_NR_03
  set file_surf="${NR_path_data}/Y${yyyy}/M${mm}/surf_${datetime}z.bin"
  ln -s $file_surf surf_NR_03

  if ($BF_path_WIND != "none") then
    rm -f udat_NR_03
    rm -f vdat_NR_03
    set file_udat="${NR_path_data}/Y${yyyy}/M${mm}/udat_${datetime}z.bin"
    ln -s $file_udat udat_NR_03
    set file_vdat="${NR_path_data}/Y${yyyy}/M${mm}/vdat_${datetime}z.bin"
    ln -s $file_vdat vdat_NR_03
  endif

  if ($BF_path_MASS != "none") then
    rm -f tdat_NR_03
    rm -f qdat_NR_03
    set file_tdat="${NR_path_data}/Y${yyyy}/M${mm}/tdat_${datetime}z.bin"
    ln -s $file_tdat tdat_NR_03
    set file_qdat="${NR_path_data}/Y${yyyy}/M${mm}/qdat_${datetime}z.bin"
    ln -s $file_qdat qdat_NR_03
  endif
#
# x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 
# run interpolation program to create simulated observations

# first compute time info for synoptic time from time + 3 hours
  set result=`addtime.x $datetime $sub3h`
  set yyyy=`echo $result | cut -d" " -f1`
  set mm=`echo $result | cut -d" " -f2`
  set dd=`echo $result | cut -d" " -f3`
  set hh=`echo $result | cut -d" " -f4`
  @ yy = $yyyy % 10 
  set yy=`printf "%2.2d\n" $yy`
   echo 'generate data at time '$yyyy,$mm,$dd,$hh

  if ($BF_path_WIND != "none") then
    rm -f file_in_bfr.blk data_out.bf
    /home/meta/bin/Linux/block  file_WIND_bfr file_in_bfr.blk
    sim_conv.x WIND_ file_in_bfr.blk data_out.bufr
    /bin/mkdir -p ${BF_outp_WIND}/Y${yyyy}/M${mm}
    /bin/ls -l ${BF_outp_WIND}/Y${yyyy}/M${mm}

    set filename="${BF_outp_WIND}/Y${yyyy}/M${mm}/wind.${yy}${mm}${dd}.t${hh}z.prepbufr"
#    rm -f $filename
    cp data_out.bufr $filename
    rm -f data_out.bufr
  endif

  if ($BF_path_MASS != "none") then
    rm -f file_in_bfr.blk data_out.bf
    /home/meta/bin/Linux/block  file_MASS_bfr file_in_bfr.blk
    sim_conv.x MASS_ file_in_bfr.blk data_out.bufr
    /bin/mkdir -p ${BF_outp_MASS}/Y${yyyy}/M${mm}
    /bin/ls -l ${BF_outp_MASS}/Y${yyyy}/M${mm}
    set filename="${BF_outp_MASS}/Y${yyyy}/M${mm}/mass.${yy}${mm}${dd}.t${hh}z.prepbufr"
#    rm -f $filename
    cp data_out.bufr $filename
    rm -f data_out.bufr
  endif

@ i++
end

#date
# All done
echo "end script"
# --------
  exit 0
