/*
C---------------------------------------------------------------------
C
C                            Function = ATG
                             From UNESCO.FOR
C
C     This function calculates the Adiabatic Temperature Gradient in
C     degrees C per decibar.
C
C     Input parameters:
C
C            S      -  Salinity (pss-78)
C            T      -  Temperature in degrees C (IPTS-68)
C            P      -  Pressure in decibars
C     Output value:
C            ATG    -  Adiabatic temperature gradient in Deg C/Decibar
C
C     Checkvalue: ATG=3.255976E-4 C/dbar for S=40 (PSS-78), T=40 deg C,
C                 P=10000 DECIBARS
C
C     Reference: Bryden,H.,1973,Deep-Sea Research,20,401-408
C
C------------------------------------------------------------------------------
*/
#include <stdio.h>
#include <math.h>
#include "ocean.h"

#if PROTOTYPE_ALLOWED
double ATG(double S, double T, double P)
#else
double ATG(S, T, P)
double S, T, P;
#endif
{
     double DS, x;
      DS = S - 35.0;
      x = (((-2.1687E-16*T+1.8676E-14)*T-4.6206E-13)*P
            + ((2.7759E-12*T-1.1351E-10)*DS+((-5.4481E-14*T
            + 8.733E-12)*T-6.7795E-10)*T+1.8741E-8))*P
            + (-4.2393E-8*T+1.8932E-6)*DS
            + ((6.6228E-10*T-6.836E-8)*T+8.5258E-6)*T+3.5803E-5;
     return(x);
}

#ifdef EXE
#if PROTOTYPE_ALLOWED
void main(int argc, char *argv[])
#else
void main(argc, argv)
int argc;
char *argv[];  /* test for ATG */
#endif
{
     double S = 40;
     double T = 40;
     double P = 10000;

   if (argc != 4)
   {
      printf("\n USAGE: atg <salinity> <temperature> <pressure>");
      printf("\n EXAMPLE: atg 40.0 40.0 10000.0\n");
   }
   else
   {
      S = atof(argv[1]);
      T = atof(argv[2]);
      P = atof(argv[3]);
   }
   printf("\n The adiabatic temperature gradient is %E C/dbar", ATG(S,T,P));
   printf("\n when S = %g (PSS-78), T = %g deg C, and P = %g dbar.\n\n", S, T, P);
}
#endif
