#!/bin/ksh -e
#1) Scale the tendency term (opottemptend) by the number of seconds in a year
#(31536000.)
#
#2) Introduce a correction to the vertical mixing (opottempdiff), to account for
#T-freeze and the missing KPP term (mostly at low latitudes). This can be done by
#using the following code, where
#
#! cor   - 3D difference between the tendency and RHS
#! tend  - 3D tendency  (opottemptend)
#! adv   - 3D net advection (opottemprmadvect)
#! dif_i - 3D iso diffusion (opottemppmdiff)
#! dif_z - 3D vertical mixing (opottempdiff)
#! tmask - 3D array of 0/1 land/ocean mask
#! lat   - 1D array of latitude
#
#        do k=1,km
#         do i=1,imt
#          do j=1,jmt
#
#         cor(i,j,k)    =  (tend(i,j,k)-
#      &         (adv(i,j,k)+dif_i(i,j,k)+dif_z(i,j,k)))*tmask(i,j,k)
#
#       if (k.le.18) then    ! upper ~ 250 m
#        if (k.eq.1.or.abs(lat(j)).lt.70.) then
#         dif_z(i,j,k)    =  dif_z(i,j,k) + cor(i,j,k)
#        endif
#       endif
#
#          enddo
#         enddo
#        enddo
#
# Run example: fafmip_otempdiff sc_dpd-002_4501_4505_gz data_description out

if [[ $# -ne 3 ]] ; then
  echo "Error in $0: invalid number of arguments $#"
  exit 1
fi

pfx=$1
desc=$2
out=$3

dirscr=/users/tor/acrn/esm/PRODUCTION/cmip5_scripts
$dirscr/fafmip_otemp $pfx $desc delt           tend
$dirscr/fafmip_otemp $pfx $desc tadx tady tadz adv
$dirscr/fafmip_otemp $pfx $desc tidx tidy tidz difi
$dirscr/fafmip_otemp $pfx $desc tvdz tsrc      difz

add ${pfx}_difi ${pfx}_difz sum2
add ${pfx}_adv  sum2 sum3
sub ${pfx}_tend sum3 cor

release desc ${pfx}_${out}
access  desc ${pfx}_${desc}

#get latitude 
echo "C*SELECT.  STEP         0         0    1     -900099999 NAME  LAT"|ccc select desc lat

addcor_spv ${pfx}_difz cor lat ${pfx}_${out} 



