merge_enviro_hirlam_for_flexpart.sh

Modified 23.11.2022

#! /bin/sh

suffixmd="md"

suffixve="ve"

fctimenames=("*003" "*006")

prefix="HL"

LSM_filename="LSM"

# Optional orography file. If not included, see below.

ORO_filename="OROGRAPHY"

# Variables to get out of the ll file (file without a suffix),

# separated by a comma, all as one string.

varstofetch="var11,var33,var34,var11,var33,var34,var51,var153,var151,var51,var66,var81,var159,var51,var62,var63"

# Variables to get out of the md file, separated by a comma, all as one string.

varstofetchmd="var1,var6,var17,var67,var71"

# Variables to get out of the ve file, separated by a comma, all as one string.

varstofetchve="var11,var33,var34,var11,var33,var34"

# Variables to get out of the ve file of +00 because it's not available in the +03 file.

wvarstofetchve="var39"

for fctime in ${fctimenames[@]}; do

  for filename in $fctime; do

    year=${filename:2:4}

    month=${filename:6:2}

    day=${filename:8:2}

    hour=${filename:11:2}

    fctime=${filename:15:2}

    if [ $fctime -eq 03 ]; then

      wfilename=${filename:0:16}0

    else

      wfilename=$filename

    fi;

    outhour=$(( hour + fctime ))

    spacer=""

    if [ $outhour -lt 10 ]; then

      spacer="0"

    fi;

    if [ $outhour -eq 24 ]; then

      outhour="0"

      spacer="0"

      if [ ${day:0:1} -eq "0" ]; then

        day=${day:1}

      fi;

      day=$(( day + 1 ))

      if [ ${#day} -eq 1 ]; then

        day=$spacer$day

      fi;

      case "$month" in

        "01")

          if [ $day -gt 31 ]; then

            day="01"

            month="02"

          fi;

          ;;

        "02")

           if [ $year % 4 -eq 0 ]; then

             if [ $day -gt 29 ]; then

               day="01"

               month="03"

             fi;

           else

             if [ $day -gt 28 ]; then

               day="01"

               month="03"

             fi;

           fi;

          ;;

        "03")

          if [ $day -gt 31 ]; then

            day="01"

            month="04"

          fi;

          ;;

        "04")

          if [ $day -gt 30 ]; then

            day="01"

            month="05"

          fi;

          ;;

        "05")

          if [ $day -gt 31 ]; then

            day="01"

            month="06"

          fi;

          ;;

        "06")

          if [ $day -gt 30 ]; then

            day="01"

            month="07"

          fi;

          ;;

        "07")

          if [ $day -gt 31 ]; then

            day="01"

            month="08"

          fi;

          ;;

        "08")

          if [ $day -gt 31 ]; then

            day="01"

            month="09"

          fi;

          ;;

        "09")

          if [ $day -gt 30 ]; then

            day="01"

            month="10"

          fi;

          ;;

        "10")

          if [ $day -gt 31 ]; then

            day="01"

            month="11"

          fi;

          ;;

        "11")

          if [ $day -gt 30 ]; then

            day="01"

            month="12"

          fi;

          ;;

        "12")

          if [ $day -gt 31 ]; then

            year=$(( year + 1 ))

            day="01"

            month="01"

          fi;

          ;;

      esac

    fi;

    outfile=$prefix${year:2}$month$day$spacer$outhour

    echo Input files: $filename  $filename$suffixmd $filename$suffixve

    echo Output file: $outfile

    if [ -f $outfile ]; then

      rm -f $outfile

    fi;

     # The following merges the desired variables needed by FLEXPART

     # into one file.

    cdo -f grb1 select,name=$varstofetch $filename tmpfile__1

    cdo -f grb1 select,name=$varstofetchmd $filename$suffixmd tmpfile__2

    cdo -f grb1 select,name=$varstofetchve $filename$suffixve tmpfile__3

    cdo -f grb1 select,name=$wvarstofetchve $wfilename$suffixve tmpfile__4

    # If there IS an orography file to include, then use this line:

    cdo -f grb1 merge tmpfile__1 tmpfile__2 tmpfile__3 tmpfile__4 \

        $ORO_filename $LSM_filename $outfile

    # If there is NOT an orography file, then use this line:

    #cdo -f grb1 merge tmpfile__1 tmpfile__2 tmpfile__3 tmpfile__4 $LSM_filename $outfile

    rm -f tmpfile__1 tmpfile__2 tmpfile__3 tmpfile__4

   echo ----------------

  done

done