#!/bin/sh

cpl_freq_sec=$(( cpl_freq_hrs * 3600 ))

lag_atm_oce=${ifs_time_step_sec}
lag_oce_atm=${nem_time_step_sec}

case ${ifs_grid} in
    T159L*) atm_grid=A080
            ;;
    T255L*) atm_grid=A128
            ;;
    T799L*) atm_grid=A400
            ;;
    *)      echo "*EE* Unsupported IFS grid type: ${ifs_grid}" 1>&2
            exit 1
            ;;
esac

case ${nem_grid} in
    ORCA1L*)   oce_grid=O1t0
               ;;
    ORCA025L*) oce_grid=Ot25
               ;;
    *)         echo "*EE* Unsupported Nemo grid type: ${nem_grid}" 1>&2
               exit 1
               ;;
esac

cat << EOF
# ===============
 \$JOBNAME
    ECE3
 \$END
# ---------------
 \$CHANNEL
    MPI1 NOBSEND
    ${ifs_numproc} ${ifs_numproc}
    ${nem_numproc} ${nem_numproc}
 \$END
# ---------------
 \$NBMODEL
    2 ifsmod oceanx 99 99
 \$END
# ---------------
 \$NFIELDS
    20 20
 \$END
# ---------------
 \$SEQMODE
    1
 \$END
# ---------------
 \$RUNTIME
    ${leg_length_sec}
 \$END
# ---------------
 \$INIDATE
    ${leg_start_date_yyyymmdd}
 \$END
# ---------------
 \$MODINFO
    NOT
 \$END
# ---------------
 \$NLOGPRT
    2
 \$END
# ---------------
 \$CALTYPE
    1
 \$END
# ===============
 \$STRINGS
# ===============
# Fields send from Atmosphere to Ocean
# ===============
# --------------- Field 1: Stress 1 (ocean) ---------------
  TAUX_OCE O_OTaux1 50 ${cpl_freq_sec} 4 TAUX_OCE EXPORTED
  ${atm_grid} ${oce_grid} LAG=${lag_atm_oce}
  P  0  P  2
  CHECKIN SCRIPR CONSERV CHECKOUT
   INT=1
   GAUSWGT D SCALAR LATITUDE 90 9 2.0
   GLOBAL
   INT=1
# --------------- Field 2: Stress 2 (ocean)  ---------------
  TAUY_OCE O_OTauy1 51 ${cpl_freq_sec} 4 TAUY_OCE EXPORTED
  ${atm_grid} ${oce_grid} LAG=${lag_atm_oce}
  P  0  P  2
  CHECKIN SCRIPR CONSERV CHECKOUT
   INT=1
   GAUSWGT D SCALAR LATITUDE 90 9 2.0
   GLOBAL
   INT=1
# --------------- Field 3: Stress 1 (ice) ---------------
  TAUX_ICE O_ITaux1 52 ${cpl_freq_sec} 4 TAUX_ICE EXPORTED
  ${atm_grid} ${oce_grid} LAG=${lag_atm_oce}
  P  0  P  2
  CHECKIN SCRIPR CONSERV CHECKOUT
   INT=1
   GAUSWGT D SCALAR LATITUDE 90 9 2.0
   GLOBAL
   INT=1
# --------------- Field 4: Stress 2 (ice) ---------------
  TAUY_ICE O_ITauy1 53 ${cpl_freq_sec} 4 TAUY_ICE EXPORTED
  ${atm_grid} ${oce_grid} LAG=${lag_atm_oce}
  P  0  P  2
  CHECKIN SCRIPR CONSERV CHECKOUT
   INT=1
   GAUSWGT D SCALAR LATITUDE 90 9 2.0
   GLOBAL
   INT=1
# --------------- Field 5: Surface solar radiation (ocean)
  QS___OCE O_QsrOce 251 ${cpl_freq_sec} 4 QS___OCE EXPORTED
  ${atm_grid} ${oce_grid} LAG=${lag_atm_oce}
  P  0  P  2
  CHECKIN SCRIPR CONSERV CHECKOUT
   INT=1
   GAUSWGT D SCALAR LATITUDE 90 9 2.0
   GLOBAL
   INT=1
# --------------- Field 6: Surface solar radiation (ice)
  QS___ICE O_QsrIce 251 ${cpl_freq_sec} 4 QS___ICE EXPORTED
  ${atm_grid} ${oce_grid} LAG=${lag_atm_oce}
  P  0  P  2
  CHECKIN SCRIPR CONSERV CHECKOUT
   INT=1
   GAUSWGT D SCALAR LATITUDE 90 9 2.0
   GLOBAL
   INT=1
# --------------- Field 7: Non-solar heat flux (ocean)
  QNS__OCE O_QnsOce 249 ${cpl_freq_sec} 4 QNS__OCE EXPORTED
  ${atm_grid} ${oce_grid} LAG=${lag_atm_oce}
  P  0  P  2
  CHECKIN SCRIPR CONSERV CHECKOUT
   INT=1
   GAUSWGT D SCALAR LATITUDE 90 9 2.0
   GLOBAL
   INT=1
# --------------- Field 8: Non-solar heat flux (ice)
  QNS__ICE O_QnsIce 249 ${cpl_freq_sec} 4 QNS__ICE EXPORTED
  ${atm_grid} ${oce_grid} LAG=${lag_atm_oce}
  P  0  P  2
  CHECKIN SCRIPR CONSERV CHECKOUT
   INT=1
   GAUSWGT D SCALAR LATITUDE 90 9 2.0
   GLOBAL
   INT=1
# --------------- Field 9: Precip (rain)
  PRCP_LIQ OTotRain 292 ${cpl_freq_sec} 4 PRCP_LIQ EXPORTED
  ${atm_grid} ${oce_grid} LAG=${lag_atm_oce}
  P  0  P  2
  CHECKIN SCRIPR CONSERV CHECKOUT
   INT=1
   GAUSWGT D SCALAR LATITUDE 90 9 2.0
   GLOBAL
   INT=1
# --------------- Field 10: Precip (snow)
  PRCP_SOL OTotSnow 328 ${cpl_freq_sec} 4 PRCP_SOL EXPORTED
  ${atm_grid} ${oce_grid} LAG=${lag_atm_oce}
  P  0  P  2
  CHECKIN SCRIPR CONSERV CHECKOUT
   INT=1
   GAUSWGT D SCALAR LATITUDE 90 9 2.0
   GLOBAL
   INT=1
# --------------- Field 11: Evap (total)
  EVAP_TOT OTotEvap 452 ${cpl_freq_sec} 4 EVAP_TOT EXPORTED
  ${atm_grid} ${oce_grid} LAG=${lag_atm_oce}
  P  0  P  2
  CHECKIN SCRIPR CONSERV CHECKOUT
   INT=1
   GAUSWGT D SCALAR LATITUDE 90 9 2.0
   GLOBAL
   INT=1
# --------------- Field 12: Evap (ice)
  EVAP_ICE OIceEvap 41 ${cpl_freq_sec} 4 EVAP_ICE EXPORTED
  ${atm_grid} ${oce_grid} LAG=${lag_atm_oce}
  P  0  P  2
  CHECKIN SCRIPR CONSERV CHECKOUT
   INT=1
   GAUSWGT D SCALAR LATITUDE 90 9 2.0
   GLOBAL
   INT=1
# --------------- Field 13: DQNSDT (ice) ---------------
  DQDT_ICE O_dQnsdT 35 ${cpl_freq_sec} 3 DQDT_ICE EXPORTED
  ${atm_grid} ${oce_grid} LAG=${lag_atm_oce}
  P  0  P  2
  CHECKIN SCRIPR CHECKOUT
   INT=1
   GAUSWGT D SCALAR LATITUDE 90 9 2.0
   INT=1
# --------------- Field 14: RUNOFF ---------------
  RNF__OCE O_Runoff 32 ${cpl_freq_sec} 4 RNF__OCE EXPORTED
  ${atm_grid} ${oce_grid} LAG=${lag_atm_oce}
  P  0  P  2
  CHECKIN SCRIPR CONSERV CHECKOUT
   INT=1
   GAUSWGT D SCALAR LATITUDE 90 9 2.0
   GLOBAL
   INT=1
# ===============
# Fields send from Ocean to Atmosphere
# ===============
# --------------- Field 1: SST ---------------
  O_SSTSST A_SSTSST 1 ${cpl_freq_sec} 5 O_SSTSST EXPORTED
  ${oce_grid} ${atm_grid/A/L} LAG=${lag_oce_atm}
  P  2  P  0
  LOCTRANS CHECKIN SCRIPR MASKP CHECKOUT
   AVERAGE
   INT=1
   GAUSWGT LR SCALAR LATITUDE 90 9 2.0
   999999.
   INT=1
# --------------- Field 2: Ice temperature ---------------
  O_TepIce A_TepIce 34 ${cpl_freq_sec} 4 O_TepIce EXPORTED
  ${oce_grid} ${atm_grid/A/L} LAG=${lag_oce_atm}
  P  2  P  0
  LOCTRANS CHECKIN SCRIPR CHECKOUT
   AVERAGE
   INT=1
   GAUSWGT LR SCALAR LATITUDE 90 9 2.0
   INT=1
# --------------- Field 3: Ice fraction ---------------
  O_AlbIce A_AlbIce 31 ${cpl_freq_sec} 4 O_AlbIce EXPORTED
  ${oce_grid} ${atm_grid/A/L} LAG=${lag_oce_atm}
  P  2  P  0
  LOCTRANS CHECKIN SCRIPR CHECKOUT
   AVERAGE
   INT=1
   GAUSWGT LR SCALAR LATITUDE 90 9 2.0
   INT=1
# --------------- Field 4: Ice albedo ---------------
  OIceFrac AIceFrac 17 ${cpl_freq_sec} 4 OIceFrac EXPORTED
  ${oce_grid} ${atm_grid/A/L} LAG=${lag_oce_atm}
  P  2  P  0
  LOCTRANS CHECKIN SCRIPR CHECKOUT
   AVERAGE
   INT=1
   GAUSWGT LR SCALAR LATITUDE 90 9 2.0
   INT=1
# --------------- Field 5: Ice thickness ---------------
  O_IceTck A_IceTck 45 ${cpl_freq_sec} 4 O_IceTck EXPORTED
  ${oce_grid} ${atm_grid/A/L} LAG=${lag_oce_atm}
  P  2  P  0
  LOCTRANS CHECKIN SCRIPR CHECKOUT
   AVERAGE
   INT=1
   GAUSWGT LR SCALAR LATITUDE 90 9 2.0
   INT=1
# --------------- Field 6: Snow thickness (ice) ---------------
  O_SnwTck A_SnwTck 46 ${cpl_freq_sec} 4 O_SnwTck EXPORTED
  ${oce_grid} ${atm_grid/A/L} LAG=${lag_oce_atm}
  P  2  P  0
  LOCTRANS CHECKIN SCRIPR CHECKOUT
   AVERAGE
   INT=1
   GAUSWGT LR SCALAR LATITUDE 90 9 2.0
   INT=1
# ---------------
 \$END
# ===============
EOF
