#!/bin/bash

# CDO options for efficient NetCDF processing
opt='-L -O -f nc4 -z zip1'

# Define years and months
years=$(seq 2000 2019)
months=$(seq 1 12)

# Create an output directory for intermediate and final files
mkdir -p fout

# Loop through years and months
for yy in $years; do
  echo "Processing year: $yy"
  for m in $months; do
    mm=$(printf '%02d' $m)  # Format month as two digits
    echo "Processing month: $mm"

    # Process files individually
    for file in /mnt/hpc/public/mko/CMEMS/BS_ice_conc/BS_SAT_SST.day.${yy}${mm}*.nc; do
      echo "Processing file: $file"
      output_file="fout/$(basename $file)"
      cdo $opt selvar,sea_ice_fraction -sellonlatbox,22,28,57,60 $file $output_file
    done

    # Merge monthly files into one
    echo "Merging files for $yy-$mm"
    cdo $opt mergetime fout/BS_SAT_SST.day.${yy}${mm}*.nc fout/ice_$yy$mm.nc

    # Apply time fixes
    cdo setreftime,1981-01-01,00:00:00,seconds fout/ice_$yy$mm.nc fout/ice_normalized_$yy$mm.nc
    cdo settaxis,${yy}-${mm}-01,00:00:00,1day fout/ice_normalized_$yy$mm.nc fout/ice_fixed_$yy$mm.nc
  done

  # Merge yearly files
  echo "Merging yearly files for $yy"
  cdo $opt mergetime fout/ice_fixed_$yy*.nc fout/ice_mmerge_$yy.nc
done

# Merge all yearly files into one
cdo $opt mergetime fout/ice_mmerge_*.nc fout/ice_ymerge_mmerge.nc