#!bin/bash
#slice_tar_file_to_bounds.sh
echo "CAUTION: Bounds are currently hard-wired"
echo "It can be calculated automatically, but requires both ogrinfo and cdo to be available"

# activate conda env which has cdo
# CAUTION: Does not contain rasterio or geopandas, so ogrinfo is not available
echo "run conda activate conda activate /storage/home/tm689/miniconda3/envs/ewses_env"

tar_file=$1 #/.tar.gz file from Met Office
bounds_file=$2 # shpfile
outputdir=$3
# TODO: Determine lonres and latres in the script
lonres=0.140625 # can be obtained from any nc in tar file
latres=0.09375 # can be obtained from any nc in tar file

# 1) Determine bounds of sub-region

bounds_str=$(ogrinfo -so -al $bounds_file | grep "Extent");
echo "bounds_str: $bounds_str"

coords=($(echo $bounds_str | grep -o -E '[0-9\.]+'));

# bounds of input file
blonmin=${coords[0]};
blatmin=${coords[1]};
blonmax=${coords[2]};
blatmax=${coords[3];}

# we need nc files to go beyond those bounds by at least one datapoint
lonmin=$(bc -l <<< "$blonmin - $lonres");
latmin=$(bc -l <<< "$blatmin - $latres");
lonmax=$(bc -l <<< "$blonmax + $lonres");
latmax=$(bc -l <<< "$blatmax + $latres");
# Hard-wired: Pre-determined for example Ethiopia region
lonmin=38.60
lonmax=40.43
latmin=8.10
latmax=8.86
echo "Bounds are $lonmin, $latmin, $lonmax, $latmax"

# 2) get original netcdf files from input tar file

originaldir=$outputdir/original;
mkdir $originaldir;

tar -zxvf $tar_file -C $originaldir;

# 3) Slice the original netcdf files

# define the output directory
# remove suffix
tar_fn=$(basename $tar_file);
tar_fn_small=${tar_fn%.tar.gz}_small;

outputdir_tar_contents=${outputdir}/${tar_fn_small}/;
mkdir $outputdir_tar_contents;

nc_fns=$(find $originaldir -type f -name "*.nc");

for fn in $nc_fns; do

    fn_small=${outputdir_tar_contents}$(basename ${fn%.nc})_small.nc;

    echo "cdo command: sellonlatbox,$lonmin,$lonmax,$latmin,$latmax $fn $fn_small";
    cdo "sellonlatbox,$lonmin,$lonmax,$latmin,$latmax" $fn $fn_small;

done;

# copy over any other files that need to be retained
fns_to_keep=$(find $originaldir -type f -name "sources.txt");
cp $fns_to_keep $outputdir_tar_contents;

# 4) Make the smaller tar file

out_fn=$tar_fn_small.tar.gz;
cd $outputdir;
tar -zcvf $out_fn ${tar_fn_small}/;
cd -;

echo "created ${out_fn}";

# clean up 

rm -rf $originaldir;
rm -rf $outputdir_tar_contents;

echo finished;
exit 0;