#!/bin/bash
# 
# Extracts 6-hourly u-wind, v-wind and potential temperature from raw data
# 
# ralf.hand@unibe.ch, 2024-06-24

set -ex

case $(hostname -s) in
   climstor) export ModE_dir=/tank33/ERC_PALAEO_ARCHIVE
             export ModE_rawdir=/tank33/ERC_PALAEO_ARCHIVE/ModE-Sim/outdata
             export workdir=/tank33/ERC_PALAEO_ARCHIVE/outdata ;;
   climcal4) export ModE_dir=/mnt/climstor/ERC_PALAEO
             export ModE_rawdir=/mnt/climstor/ERC_PALAEO
             # export workdir=/scratch3/rhand/test-EP ;;
             export workdir=/mnt/climstor/ERC_PALAEO_ARCHIVE/outdata ;;
esac # hostname


export overviewfile=${ModE_dir}/ModE-Sim/docu/ModE-Sim_overview.csv
export outdir_base=${workdir}

# export outdir_base=/tank/exports/ERC_PALAEO/ModE-Sim/outdata/

export col_orig_set=$(head -1 ${overviewfile} | awk -F ";" -v b="orig_set" '{for (i=1;i<=NF;i++) { if ($i == b) { print i } }}')
export col_orig_member=$(head -1 ${overviewfile} | awk -F ";" -v b="orig_member" '{for (i=1;i<=NF;i++) { if ($i == b) { print i } }}')
export col_orig_sel_fyear=$(head -1 ${overviewfile} | awk -F ";" -v b="orig_sel_fyear" '{for (i=1;i<=NF;i++) { if ($i == b) { print i } }}')
export col_orig_sel_lyear=$(head -1 ${overviewfile} | awk -F ";" -v b="orig_sel_lyear" '{for (i=1;i<=NF;i++) { if ($i == b) { print i } }}')
export col_final_fyear=$(head -1 ${overviewfile} | awk -F ";" -v b="final_fyear" '{for (i=1;i<=NF;i++) { if ($i == b) { print i } }}')
export col_final_lyear=$(head -1 ${overviewfile} | awk -F ";" -v b="final_lyear" '{for (i=1;i<=NF;i++) { if ($i == b) { print i } }}')

###################
# define function #
###################

function extract_ep {

   set -ex

   module load cdo/1.9.10
   cdo=cdo
   
   memb_year=${1}

   final_member=$(echo ${memb_year} | awk -F "_" '{print $1}')

   orig_set=$(grep "${final_set};${final_member}" ${overviewfile} | awk -F ';' "NR==1 {print \$$(echo ${col_orig_set})}")
   orig_member=$(grep "${final_set};${final_member}" ${overviewfile} | awk -F ';' "NR==1 {print \$$(echo ${col_orig_member})}")

   outdir=${outdir_base}/set_${final_set}/abs/${final_member}/by_year

   year=$(echo ${memb_year} | awk -F "_" '{print $2}')

   echo ${final_member} ${year}

   block=$(echo ${year} | cut -c 1-3 )

   case ${final_set} in

      1850-1|1850-2)
               cd ${ModE_rawdir}/set_${final_set}/rawdata/${orig_member}
               infile_base=${orig_member}_
               ;;

      H1850-1|H-1870-1)

               cd ${ModE_rawdir}/set_${final_set}/abs/${final_member}/rawdata
               infile_base=ModE-Sim_set_${final_set}_${final_member}_
               ;;

   esac # final_set

   $cdo after ${infile_base}${year}??.01_echam ${outdir}/ModE-Sim_set_${final_set}_${final_member}_${year}_6h.alllevs.grb << __EOF__
   &select
     code = 130, 131, 132, 239 
     level = -1 
     type = 70
     format = 1
     mean = 0
   /
__EOF__
  
   cd ${workdir}
#   rm ${orig_member}/rawdata/${orig_member}_${year}??.01_echam ${orig_member}/rawdata/${orig_member}_${year}??.01_accw   

}

export -f extract_ep


##############
# do the job #
##############

   export final_set=1850-2
   # export memb_yearlist="m022_1852 m022_1858 m023_1977 m031_1968 m033_1971 m034_1948 m035_1887 m036_1990"
   export memb_yearlist="m029_1850 m029_1851 m029_1852 m029_1853 m029_1854 m029_1855 m029_1856 m029_1857 m029_1858 m029_1859 m029_1860 m029_1861 m029_1862 m029_1863 m029_1864 m029_1865 m029_1866 m029_1867 m029_1868 m029_1869 m029_1870 m029_1871 m029_1872 m029_1873 m029_1874 m029_1875 m029_1876 m029_1877 m029_1878 m029_1879 m029_1880 m029_1881 m029_1882 m029_1883 m029_1884 m029_1885 m029_1886 m029_1887 m029_1888 m029_1889 m029_1890 m029_1891 m029_1892 m029_1893 m029_1894 m029_1895 m029_1896 m029_1897 m029_1898 m029_1899 m029_1900 m029_1901 m029_1902 m029_1903 m029_1904 m029_1905 m029_1906 m029_1907 m029_1908 m029_1909 m029_1910 m029_1911 m029_1912 m029_1913 m029_1914 m029_1915 m029_1916 m029_1917 m029_1918 m029_1919 m029_1920 m029_1921 m029_1922 m029_1923 m029_1924 m029_1925 m029_1926 m029_1927 m029_1928 m029_1929 m029_1930 m029_1931 m029_1932 m029_1933 m029_1934 m029_1935 m029_1936 m029_1937 m029_1938 m029_1939 m029_1940 m029_1941 m029_1942 m029_1943 m029_1944 m029_1945 m029_1946 m029_1947 m029_1948 m029_1949 m029_1950 m029_1951 m029_1952 m029_1953 m029_1954 m029_1955 m029_1956 m029_1957 m029_161 m029_1862 m029_1863"

   export memb_yearlist="m029_1857"   

   n_orig_sets=$(grep "^${final_set};${final_member}" ${overviewfile} | wc -l)
    
#   for i in $(seq 1 ${n_orig_sets}); do
   for i in 1; do

      echo ${memb_yearlist} | tr " " "\n" | xargs -n1 -P14 -I% bash -c "extract_ep %"

   done # i

exit 0
