#!/usr/bin/bash
# Shebang line specifying the script should be run using the Bash shell

# Define base directories for processing
BASE_PATH="/home/study/S24_RaigoM"                    # Base directory for the project
TEMP_DIR="${BASE_PATH}/Temp"                          # Directory for temporary files
RESULT_DIR="${BASE_PATH}/Results"                     # Directory to store final results
DATA_DIR="/mnt/hpc/public/mko/CMEMS/BS_ice_conc"       # Directory where input data files are located

# Define options for the CDO (Climate Data Operators) commands
cdo_options='-L -O -f nc4 -z zip1'                    # 
# -L      : Enable large file support
# -O      : Overwrite existing files without prompting
# -f nc4  : Specify output format as NetCDF4
# -z zip1 : Enable compression with level 1

# Create necessary directories if they don't already exist
mkdir -p "$TEMP_DIR"       # Create temporary directory
mkdir -p "$RESULT_DIR"     # Create results directory

# Loop over the years from 1989 to 2019, 30 years
for year in {1989..2019}; do
  echo "Processing year: $year"  # Print current year being processed
  
  # Loop over the months from January (01) to December (12)
  for month in {01..12}; do
    # Merge multiple time slices for the current year and month into a single NetCDF file
    cdo $cdo_options mergetime ${DATA_DIR}/BS_SAT_SST.day.${year}${month}*.nc \
       ${TEMP_DIR}/temporary.${year}${month}.nc
    
    # Select the variable 'sea_ice_fraction' and subset the data spatially
    cdo $cdo_options selvar,sea_ice_fraction \
       -sellonlatbox,21,30,57,61 \
       ${TEMP_DIR}/temporary.${year}${month}.nc \
       ${TEMP_DIR}/ice_data.${year}${month}.nc
    
    # Remove the temporary merged file to save space
    rm -f ${TEMP_DIR}/temporary.${year}${month}.nc
  done
  
  # After processing all months, merge the monthly ice data into a yearly file
  cdo $cdo_options mergetime ${TEMP_DIR}/ice_data.${year}*.nc \
     ${RESULT_DIR}/yearly_ice_data.${year}.nc
done

# After processing all years, merge all yearly ice data into a combined file
cdo $cdo_options mergetime ${RESULT_DIR}/yearly_ice_data.*.nc \
   ${RESULT_DIR}/combined_ice_data.nc

# Calculate the annual sum of ice days where sea ice fraction is greater than or equal to 0
cdo $cdo_options yearsum -gtc,0 \
   ${RESULT_DIR}/combined_ice_data.nc \
   ${RESULT_DIR}/annual_ice_days.nc

# Compute the time average of annual ice days
cdo $cdo_options timavg \
   ${RESULT_DIR}/annual_ice_days.nc \
   ${RESULT_DIR}/average_ice_days.nc

# Spatial subset for the GOF (Goodness of Fit) region
cdo $cdo_options sellonlatbox,23.3,30,59,61 \
   ${RESULT_DIR}/combined_ice_data.nc \
   ${TEMP_DIR}/ice_GOF.nc

# Create a binary presence file where sea ice fraction is greater than or equal to 0
cdo $cdo_options -gtc,0 \
   ${TEMP_DIR}/ice_GOF.nc \
   ${TEMP_DIR}/presence_GOF.nc

# Calculate the field sum of presence (total GOF presence)
cdo $cdo_options fldsum \
   ${TEMP_DIR}/presence_GOF.nc \
   ${TEMP_DIR}/total_GOF.nc

# Compute the day-of-year mean for GOF total presence
cdo $cdo_options ydaymean \
   ${TEMP_DIR}/total_GOF.nc \
   ${RESULT_DIR}/mean_GOF.nc

# Compute the day-of-year standard deviation for GOF total presence
cdo $cdo_options ydaystd \
   ${TEMP_DIR}/total_GOF.nc \
   ${RESULT_DIR}/std_GOF.nc

# Spatial subset for the GOR (another region) area
cdo $cdo_options sellonlatbox,22,25,56.5,58.5 \
   ${RESULT_DIR}/combined_ice_data.nc \
   ${TEMP_DIR}/ice_GOR.nc

# Create a binary presence file where sea ice fraction is greater than or equal to 0 for GOR
cdo $cdo_options -gtc,0 \
   ${TEMP_DIR}/ice_GOR.nc \
   ${TEMP_DIR}/presence_GOR.nc

# Calculate the field sum of presence (total GOR presence)
cdo $cdo_options fldsum \
   ${TEMP_DIR}/presence_GOR.nc \
   ${TEMP_DIR}/total_GOR.nc

# Compute the day-of-year mean for GOR total presence
cdo $cdo_options ydaymean \
   ${TEMP_DIR}/total_GOR.nc \
   ${RESULT_DIR}/mean_GOR.nc

# Compute the day-of-year standard deviation for GOR total presence
cdo $cdo_options ydaystd \
   ${TEMP_DIR}/total_GOR.nc \
   ${RESULT_DIR}/std_GOR.nc

# Inform the user that temporary files are being deleted
echo "Deleting temporary directory..."

# Remove the temporary directory and all its contents
rm -rf "$TEMP_DIR"

# Inform the user that the script has completed successfully
echo "Script completed successfully."
