#!/bin/bash -e
set -o nounset
set -o errexit


#################################################################################
## Post-process daily OSTIA (0.05x0.05deg)
##
## Ver1.0: Created by Mariko Koseki, 07/05/2025
## ver1.1: updated by Mariko, 24 Sep 2026
#################################################################################


## User input ##
if [ $# -eq 2 ] #check number of arguments
then 
  start_year=$1
  start_month=$2
  end_year=$1
  end_month=$2
elif [ $# -eq 4 ]
then 
  start_year=$1
  start_month=$2
  end_year=$3
  end_month=$4
else
  echo "usage: $0 <year> <month> " 
  echo "       or "
  echo "       $0 <year1> <month1> <yearn> monthn> "
  exit 0
fi

#GRIDDES=`pwd`/grid_360x180.txt


##--- Select dataset -----------------------------------------------------------##
# Dataset name: Global Ocean OSTIA Sea Surface Temperature and Sea Ice Reprocessed
#  Product ID: SST_GLO_SST_L4_REP_OBSERVATIONS_010_011
#  Dataset ID: METOFFICE-GLO-SST-L4-REP-OBS-SST
#  DOI: https://doi.org/10.48670/moi-00168
#  Spatial resolution: 0.05° × 0.05°
#  Period: 1 Oct 1981 to 31 May 2022

# Dataset name: Global Ocean OSTIA Sea Surface Temperature and Sea Ice Analysis
#  Product ID: SST_GLO_SST_L4_NRT_OBSERVATIONS_010_001
#  Dataset ID: METOFFICE-GLO-SST-L4-NRT-OBS-SST-V2
#  DOI: https://doi.org/10.48670/moi-00165
#  Spatial resolution: 0.05° × 0.05°
#  Period: 1 Jan 2007 to present


#dataset='nrt'
dataset='rep'


if [ ${dataset} = 'rep' ]
then
  datasetID='METOFFICE-GLO-SST-L4-REP-OBS-SST'
  data_path='SST_GLO_SST_L4_REP_OBSERVATIONS_010_011/METOFFICE-GLO-SST-L4-REP-OBS-SST_202003/'
  filename='120000-UKMO-L4_GHRSST-SSTfnd-OSTIA-GLOB_REP-v02.0-fv02.0.nc'

elif [ ${dataset} = 'nrt' ]
then
  datasetID='METOFFICE-GLO-SST-L4-NRT-OBS-SST-V2'
  data_path='SST_GLO_SST_L4_NRT_OBSERVATIONS_010_001/METOFFICE-GLO-SST-L4-NRT-OBS-SST-V2/'
  filename='120000-UKMO-L4_GHRSST-SSTfnd-OSTIA-GLOB-v02.0-fv02.0.nc'

else
  echo "no such data"
fi
 
echo "ID:" ${datasetID}
echo ""




#NTHREAD=32

process_day() {
    if [ -e ${year}${month}${day}${filename} ]
    then
	echo 'daily data: ' ${year}${month}${day}${filename} 'exists'
        
    else
	echo "no daily data"
        exit 0	

    fi

    ### Define dim as record dimension in output file ###
    #'''
    #if first time post-processing
    #'''
    echo "step0"
    #ncks -O --mk_rec_dmn time ${year}${month}${day}${filename} ${year}${month}${day}${filename}

    ### Copy file ###
    cp ${year}${month}${day}${filename} ${year}_${month}_${day}_hires.nc
 

    ### Rename variable ###
    echo "step1"
    ## sea_ice_fraction -> icec ###
    ncrename -v sea_ice_fraction,icec ${year}_${month}_${day}_hires.nc

    ## analysis_error -> err ###
    ncrename -v analysis_error,err ${year}_${month}_${day}_hires.nc

    ## analysed_sst -> sst ###
    ncrename -v analysed_sst,sst ${year}_${month}_${day}_hires.nc
    

    ### Unpack OSTIA packed variables ### #ver1.1
    echo "step1-1"
    ncpdq -O --unpack ${year}_${month}_${day}_hires.nc ${year}_${month}_${day}_hires.nc


    ### Delete old packed-data valid range ### #ver1.1
#    ncatted -a valid_min,sst,d,, -a valid_max,sst,d,,-a valid_min,err,d,, -a valid_max,err,d,,-a valid_min,icec,d,, -a valid_max,icec,d,,${year}_${month}_${day}_hires.nc
    ncatted -O -a valid_min,sst,d,, -a valid_max,sst,d,, ${year}_${month}_${day}_hires.nc
    ncatted -O -a valid_min,err,d,, -a valid_max,err,d,, ${year}_${month}_${day}_hires.nc
    ncatted -O -a valid_min,icec,d,, -a valid_max,icec,d,, ${year}_${month}_${day}_hires.nc


    ### change FillValue ###
    echo "step2"

    ## DELETE THIS STEP #ver1.1
#    ncap2 -O -s 'where(err<-32760) err=-999.' ${year}_${month}_${day}_hires.nc ${year}_${month}_${day}_hires.nc

    ## DELETE THIS STEP #ver1.1
#    ncatted -a _FillValue,,o,f,-999. -a missing_value,,o,f,-999. ${year}_${month}_${day}_hires.nc


    ### Delete FillValue and missing value for icec ###
    ## DELETE THIS STEP #ver1.1
#    ncatted -a _FillValue,icec,d,, -a missing_value,icec,d,, ${year}_${month}_${day}_hires.nc


    ### Change missing values to -999 ### #ver1.1
    ncatted -O -a _FillValue,sst,o,f,-999. -a missing_value,sst,o,f,-999. ${year}_${month}_${day}_hires.nc
    ncatted -O -a _FillValue,err,o,f,-999. -a missing_value,err,o,f,-999. ${year}_${month}_${day}_hires.nc


    ### Change icec FillValue to -999 ### #ver1.1
    ncatted -O -a _FillValue,icec,o,f,-999. ${year}_${month}_${day}_hires.nc

    ### Temporarily delete FillValue information for icec ### #ver1.1
    ncatted -O -a _FillValue,icec,d,, -a missing_value,icec,d,, ${year}_${month}_${day}_hires.nc


    ### Missing icec -> 0 where SST > -2 degC #ver1.1
    ### Convert icec from 0-1 to 0-100
    echo "check icec"
    cdo -s info -selname,icec ${year}_${month}_${day}_hires.nc
    ncap2 -O -s 'where(sst>271.15 && icec==-999.) icec=0.; where(icec>0.) icec=icec*100.;' ${year}_${month}_${day}_hires.nc ${year}_${month}_${day}_hires.nc

    ### Restore FillValue for icec ###
    ncatted -O -a _FillValue,icec,o,f,-999. -a missing_value,icec,o,f,-999. ${year}_${month}_${day}_hires.nc

    ### Check icec ###
    echo "check icec"
    cdo -s info -selname,icec ${year}_${month}_${day}_hires.nc



    ### Sea ice concentration ### #ver1.1
#    ncap2 -O -s 'where(sst>-2. && icec==-999.) icec=0.; where(icec>0.) icec=icec*100.;' ${year}_${month}_${day}_hires.nc ${year}_${month}_${day}_hires.nc
#    ncap2 -O -s 'where(sst>271.15 && icec==-999.) icec=0.; where(icec>0.) icec=icec*100.;' ${year}_${month}_${day}_hires.nc ${year}_${month}_${day}_hires.nc
#    ncap2 -O -s 'msk=mask_miss(icec); where(sst>271.15 && msk==0) icec=0.; where(icec>0.) icec=icec*100.;' ${year}_${month}_${day}_hires.nc ${year}_${month}_${day}_hires.nc


    ### Add FillValue and missing value for icec ###
#    ncatted -a _FillValue,icec,o,f,-999. -a missing_value,icec,o,f,-999. ${year}_${month}_${day}_hires.nc

    ### Add add_offset and scale_factor for sst and icec ###
    ## DELETE THIS STEP #ver1.1
#    ncatted -a add_offset,sst,o,f,0. -a scale_factor,sst,o,f,1. -a add_offset,icec,o,f,0. -a scale_factor,icec,o,f,1. ${year}_${month}_${day}_hires.nc


    ### change unit "kelvin" -> "Celsius" ###
    echo "step3"
    cdo -setattribute,sst@units="Celsius" -subc,273.15 -selname,sst ${year}_${month}_${day}_hires.nc temp_output_d_hires.nc

    ### err: K difference = degree C difference, so do NOT subtract 273.15 ###
    cdo -setattribute,err@units="Celsius" -selname,err ${year}_${month}_${day}_hires.nc err_output_d_hires.nc




    ### Append variables ###
    echo "step4"
    ncks -A -v sst temp_output_d_hires.nc ${year}_${month}_${day}_hires.nc
    ncks -A -v err err_output_d_hires.nc ${year}_${month}_${day}_hires.nc


    ### Delete eulaVlliF_ for sst and err ###
    ncatted -a eulaVlliF_,sst,d,, ${year}_${month}_${day}_hires.nc
    ncatted -a eulaVlliF_,err,d,, ${year}_${month}_${day}_hires.nc
    ncatted -a eulaVlliF_,icec,d,, ${year}_${month}_${day}_hires.nc

#    ncatted -O -a _FillValue,sst,o,f,-999. -a missing_value,sst,o,f,-999. ${year}_${month}_${day}_hires.nc
#    ncatted -O -a _FillValue,err,o,f,-999. -a missing_value,err,o,f,-999. ${year}_${month}_${day}_hires.nc


    ### Final missing value = -999 ###
    ncatted -O -a _FillValue,sst,o,f,-999. -a missing_value,sst,o,f,-999. ${year}_${month}_${day}_hires.nc
    ncatted -O -a _FillValue,err,o,f,-999. -a missing_value,err,o,f,-999. ${year}_${month}_${day}_hires.nc
    ncatted -O -a _FillValue,icec,o,f,-999. -a missing_value,icec,o,f,-999. ${year}_${month}_${day}_hires.nc




    ### Check output ### #ver1.1
    echo "check final output"
    cdo -s info -selname,sst ${year}_${month}_${day}_hires.nc
    cdo -s info -selname,icec ${year}_${month}_${day}_hires.nc
    cdo -s info -selname,err ${year}_${month}_${day}_hires.nc


#    ncdump -h ${year}_${month}_${day}_hires.nc | grep -E "_FillValue|missing_value"


    ### Move NetCDF file to monthly directory ### 
    mv ${year}_${month}_${day}_hires.nc ../../../../../../processed/daily/005deg/${year}_${month}_${day}.nc #ver1.1
    echo "done"
    echo "file has been moved to daily folder"
    echo " "

    ### Delete files ###
    rm -f temp_output_d_hires.nc
    rm -f err_output_d_hires.nc
}


## Move into original data ##
#TMPDIR='original/'${data_path}
TMPDIR='/nird/datalake/NS9039K/bcpu-data-repo/CMEMS/OSTIA/raw/'${dataset}'/'${data_path}   #ver1.1
cd $TMPDIR

ORGDIR=$(pwd)

## Read daily files ##
#COUNT=1
echo "Mapping files..."
for year in $(seq -f "%04g" $start_year $end_year)
do
  cd ${ORGDIR}/${year}

  PWDDIR=$(pwd)
  echo $PWDDIR

  for month in $(seq -f "%02g" $start_month $end_month)
  do
    cd ${ORGDIR}/${year}/${month}
    PWDDIR=$(pwd)
    echo $PWDDIR

    ndays=$(cal ${month} ${year} | awk 'NF {DAYS = $NF}; END {print DAYS}')
    echo $ndays

    
    for day in $(seq -f "%02g" 01 $ndays)
    do
      #if [[ ! -e ../../../../../daily/${year}_${month}_${day}.nc || -L ../../${year}_${month}_${day}.nc ]]
      if [[ ! -e ../../../../../../processed/daily/${year}_${month}_${day}.nc ]] #ver1.1
      then
	echo ""
	echo "create daily data"
	
        process_day
        #if [ $COUNT -eq $NTHREAD ]
        #then 
          #COUNT=0
          #wait
        #else
          #COUNT=$((COUNT+1))
        #fi
      fi 
    done
  done
done 
wait
echo "Completed!!" 
