#!/bin/bash
#============================================================================================================================
# purpose: create files of orgorgraphy (orog)) and land-Sea-Fraftion(sftlf) for destination domain
#          will be done only if fields don't already exist
#
SWD=$1
Domain=$2
destDomain=$3
common_fx_file=$4
BaseDirSourcefx=$5
sftlf_file=$6
orog_file=$7
Ensemble=$8
BaseDirTarget=$9
#
#
# renaming to destination domain
outfile_orog=`echo "${orog_file}" | sed -e "s/$Domain/$destDomain/"`
outfile_sftlf=`echo "${sftlf_file}" | sed -e "s/$Domain/$destDomain/"`
#
# renaming to realizatin to "r0i0p0"
BaseDirTargetfx=`echo "${BaseDirTarget}" | sed -e "s/$Ensemble/r0i0p0/"`
BaseDirTargetfx=${BaseDirTargetfx}/fx
#
if [ ! -f ${BaseDirTargetfx}/orog/${outfile_orog} ] || [ ! -f ${BaseDirTargetfx}/sftlf/${outfile_sftlf} ]
then
#
 [ -d ${BaseDirTargetfx}/orog ] || mkdir -p ${BaseDirTargetfx}/orog
 [ -d ${BaseDirTargetfx}/sftlf ] || mkdir -p ${BaseDirTargetfx}/sftlf
#
# select orog and sftlf fields from common_fx_file of destination domain
# sftlf has to be given in %; therefore, multipication with 100 and renaming of units attribute
 cdo selvar,orog ${common_fx_file} orog_help.nc
 cdo mulc,100. -selvar,sftlf ${common_fx_file} sftlf_help.nc
 ncatted -A -a units,sftlf,o,c,"%" sftlf_help.nc
#
# delete all global attributes
 ncatted -A -a ,global,d,, orog_help.nc
 ncatted -A -a ,global,d,, sftlf_help.nc
#
# copy all global attribute from orog and sftlf files of source domain to files for destionation domain
 ncks -A -x ${BaseDirSourcefx}/orog/${orog_file} orog_help.nc
 ncks -A -x ${BaseDirSourcefx}/sftlf/${sftlf_file} sftlf_help.nc
#
# add tracking_id and creation_date 
 ncatted -h -a tracking_id,global,o,c,"`uuidgen -r`" orog_help.nc
 ncatted -h -a tracking_id,global,o,c,"`uuidgen -r`" sftlf_help.nc
 newdate=`date -u +%Y'-'%m'-'%d'T'%T'Z'`
 ncatted -h -a creation_date,global,o,c,"${newdate}" orog_help.nc
 ncatted -h -a creation_date,global,o,c,"${newdate}" sftlf_help.nc
#
# delete all history attributes
 ncatted -h -a history,global,d,x,x orog_help.nc
 ncatted -h -a history_of_appended_files,global,d,x,x orog_help.nc
 ncatted -h -a history,global,d,x,x sftlf_help.nc
 ncatted -h -a history_of_appended_files,global,d,x,x sftlf_help.nc
#
# copied global attributes contain name of the source domain; this has to be changed to the name of destination domain
# the procedure only works if the name appears only once in the global attributes
 GlobAttName=" "
 GlobAttName=$(ncdump -h orog_help.nc | grep  $Domain | cut -d':' -f2 | cut -d' ' -f1)
#
 if [ -n ${GlobAttName} ]
 then
  ncatted -h -O -a ${GlobAttName},global,o,c,"$destDomain" orog_help.nc
  ncatted -h -O -a ${GlobAttName},global,o,c,"$destDomain" sftlf_help.nc
 fi 
#
# move help files to final files
 mv orog_help.nc ${BaseDirTargetfx}/orog/${outfile_orog}
 mv sftlf_help.nc ${BaseDirTargetfx}/sftlf/${outfile_sftlf}
#
else
 echo
 echo orog  file for destination grid already exists:
 echo ${BaseDirTargetfx}/orog/${outfile_orog}
 echo
 echo sftlf file for destination grid already exists:
 echo ${BaseDirTargetfx}/sftlf/${outfile_sftlf}
 echo
fi
