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


#################################################################################
## Interpolate daily OSTIA (0.05x0.05deg) into daily low resolution data (1x1deg)
##
## Ver1.0: Created by Mariko Koseki, 07/05/2025
#################################################################################


## 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}


    ### Remapping: 0.05 degree -> 1 degree ###
    echo "step1"
    cdo -remapcon,$GRIDDES ${year}${month}${day}${filename} ${year}_${month}_${day}.nc


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

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

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

    ### change FillValue ###
    echo "step3"

    ncap2 -O -s 'where(err<-32760) err=-999.' ${year}_${month}_${day}.nc ${year}_${month}_${day}.nc

    ncatted -a _FillValue,,o,f,-999. -a missing_value,,o,f,-999. ${year}_${month}_${day}.nc


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

    ### Sea ice concentration ###
    ncap2 -O -s 'where(sst>-2. && icec==-999.) icec=0.; where(icec>0.) icec=icec*100.;' ${year}_${month}_${day}.nc ${year}_${month}_${day}.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}.nc

    ### Add add_offset and scale_factor for sst and icec ###
    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}.nc


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

    ### Append variables ###
    echo "step5"
    ncks -A -v sst temp_output_d.nc ${year}_${month}_${day}.nc
    ncks -A -v err err_output_d.nc ${year}_${month}_${day}.nc


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

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


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

    ### Delete files ###
    rm -f temp_output_d.nc
    rm -f err_output_d.nc
}


## Move into original data ##
TMPDIR='original/'${data_path}
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 ../../../../../daily/${year}_${month}_${day}.nc ]]
      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!!" 
