/*
      REAL FUNCTION p_depth(P,LAT)
C---------------------------------------------------------------------
C
C     This function calculates depth from pressure using Saunders and
C     Fofonoff's method.  (Deep-Sea Research, 1976,23,109-111)
C
C     Formula refitted for 1980 equation of state.
C
C     Input parameters:
C            P      -  Pressure in decibars
C            LAT    -  Latitude in degrees
C     Output value:
C            DEPTH  -  Depth in meters
C
C     Checkvalue: DEPTH = 9712.653 M for P=10000 decibars,
C                 latitude = 30 degrees
C                 Above for standard ocean: T=0 Deg. Celcius ;
C                 S = 35 (PSS-78)
C
C-------------------------------------------------------------------------------
*/

#include <stdio.h>
#include <math.h>
#include "ocean.h"

#if PROTOTYPE_ALLOWED
double p_depth(double P, double LAT)
#else
double p_depth(P, LAT)
double P, LAT;
#endif
{
     double X, GR, dd;
      X = sin(LAT/57.29578);
      X = X*X;
/*
     =====>  GR = Gravity variation with latitude:
             ANON (1970) Bulletin Geodesique
*/
      GR = 9.780318*(1.0+(5.2788E-3+2.36E-5*X)*X) + 1.092E-6*P;
      dd = (((-1.82E-15*P+2.279E-10)*P-2.2512E-5)*P+9.72659)*P;
      dd = dd/GR;
     return(dd);
}

#ifdef EXE
#if PROTOTYPE_ALLOWED
void main(int argc, char *argv[])
#else
void main(argc, argv)
int argc;
char *argv[];
#endif
{
   double P = 10000.0, LAT = 30.0;

   if (argc != 3)
   {
      printf("\n USAGE: depth <pressure> <latitude>");
      printf("\n ASSUMES: standard ocean T = 0.0 deg C and S = 35 (PSS-78)");
      printf("\n EXAMPLE: depth 10000.0 30.0\n");
   }
   else
   {
      P = atof(argv[1]);
      LAT = atof(argv[2]);
   }
   printf("\n depth = %f m at P = %g dbar, %g degrees latitude\n\n",
      p_depth(P, LAT), P, LAT);
}
#endif
