SUBROUTINE CH_GP_TO_Z(PGP,PZ,KZ,KDIR,PLAT) 4
C
      IMPLICIT NONE
      INTEGER KZ,KDIR
      REAL*8 PGP(KZ),PZ(KZ)
      REAL  PLAT
C
#if defined (DOC)
*
*
**s/r CH_GP_TO_Z - Convert geopotential profile to geometric height profile
*               or vice versa.
*
*     AUTHOR: Y. Rochon *ARQX/AES Jan 1999
*     REVISIONS:
*
*     PURPOSE: Convert geopotential profile to/from geometric height
*              or geopotential height profile.
*
*              Geopential height and geometric height should approx
*              equal to each other in the troposphere and lower
*              stratosphere.
*
*              Geopotential height = geopotential/9.80665
*
*     ARGUMENTS
*
*       INPUT:
*
*
*          KDIR:      Flag (see PGP or PZ below)
*          KZ:        Dimension of arrays
*          PGP or PZ: Geopotentials (m^2/s^2; KDIR>0) and altitudes
*                     (m; KDIR<0) respectively.
*          PLAT:      Latitude. (rad.)
*          KDIR:      >0 for input of geopotential
*                        and =1 for output as geometric altitude (m)
*                            =2 for output as geopotential height (m)
*                     <0 for output of geopotential
*                        and =-1 for input as geometric altitude (m)
*                            =-2 for input as geopotential height (m)
*
*       OUTPUT:
*
*          PZ or PGP: Altitudes (m; KDIR>0) or geopotentials (m^2/s^2; KDIR<0)
*
*     OTHER PARAMETERS
*
*     ZPRAD - Planetocentric radius array. (km)
*     ZPLANET(1) - Rotation period. (sec)
*     ZPLANET(2) - Mass. (kg)
*     ZPLANET(3) - Equitorial radius. (km)
*     ZPLANET(4) - Polar radius. (km)
*     ZG  - Gravitational constant ( cm . km^{2} . s^{-2} . kg^{-1})
*     ZGRAVS - Gravitional acceleration
*
*
*     REFERENCES:
*
*     Bomford, W.A. (1971), 'Geodesy', Calrendon Press, Oxford.
*
*     Cook, A.H. (1959), 'Developments in dynamical geodesy',
*     Geophys, J. R. Astro. Soc., Vol. 2, pp. 222-240.
*
*     Heiskmanen, W.A. and H. Moritz (1967), 'Physical Geodesy',
*     W.H. Freeman and Compnay, San Francisco.
*
************************************************************************
#endif
#include "comlun.cdk"
#include "comcst.cdk"
C
      INTEGER I1,I2,I3,JLEV
      REAL*8 ZRAD,ZPRAD,ZPLAT
      REAL*8 ZPLANET(4)
      DATA ZPLANET/86200.0,5.977E24,6378.2,6356.8/
      REAL*8 ZJ2,ZJ4,ZG,ZSCALE,ZS,ZS2,ZC,ZC2,ZAP,ZAE
      REAL*8 ZF,ZRM,ZOMEGA,ZBARM,ZC1,ZC3,ZC33
      REAL*8 ZRM2,ZF2,ZOMEGA2
      REAL*8 ZSC,ZS4,ZP0,ZP2,ZP4,ZR2,ZPP2,ZPP4
      REAL*8 ZX,ZX2,ZC4,ZC5,ZC6,ZG1,ZG2,ZG3,ZG4
      REAL*8 ZGRAVS
C
      ZG=6.672E-15
C     cm TO km CONVERSION FACTOR
      ZSCALE=1.0E-5
C     RG=9.80665   !Acceleration due to gravity (m/s^2)
C
C     Set constants
C
C     ZPLAT=PLAT*3.1415926/180.0
      ZPLAT=PLAT
C
      IF (KDIR.EQ.-2) then
C
C        Geopotential height to geopotential
C
         DO I1=1,KZ
            PGP(I1)=PZ(I1)*RG
         END DO
      ELSE IF (KDIR.EQ.2) THEN
C
C        Geopotential to geopotential height
C
         DO I1=1,KZ
            PZ(I1)=PGP(I1)/RG
         END DO
      ELSE
C
C     To/from geometric altitude
C
C     ZPRAD (latitude dependent Earth radius) calculated here.
C
      ZS=SIN(ZPLAT)
      ZS2=ZS*ZS
      ZC=COS(ZPLAT)
      ZC2=ZC*ZC
      ZAP=ZPLANET(3)*ZPLANET(3)
      ZAE=ZPLANET(4)*ZPLANET(4)
      ZPRAD=SQRT(ZAP*ZAE/(ZAE*ZS2+ZAP*ZC2))
C
C     Set coeffecients for calc of g(z)
C
      ZF=(ZPLANET(3)-ZPLANET(4))/ZPLANET(3)
      ZRM=ZPLANET(3)*(1.0-ZF/3.0-ZF*ZF/5.)
      ZOMEGA=2.*3.1415927/ZPLANET(1)
      ZRM2=ZRM*ZRM
      ZF2=ZF*ZF
      ZOMEGA2=ZOMEGA*ZOMEGA
      ZBARM=ZOMEGA2*ZRM2*ZRM/ZG/ZPLANET(2)
      ZJ2=2.0*(ZF-ZBARM/2.0-ZF2/2.0+ZF*ZBARM/5.0)/3.0
      ZJ4=8.0*(2.5*ZF*ZBARM-3.5*ZF2)/35.0
      ZC1=ZOMEGA2*ZSCALE
      ZC2=ZG*ZPLANET(2)
      ZC3=ZJ2*ZRM2
      ZC33=ZJ4*ZRM2*ZRM2
C
      ZSC=ZS*ZC
      ZS4 = ZS2*ZS2
      ZP0 = 1.0
      ZP2 = (3.0*ZS2 - 1.0)/2.0
      ZP4=(35.0*ZS4-30.0*ZS2+3)/8.0
      ZR2=2.0/3.0*ZC1*(ZP0-ZP2)
      ZPP2 = 3.0*ZSC
      ZPP4 = (140.0*ZS2-60.0)*ZSC/8.0
C
      I1=KZ
      I2=1
      I3=-1
C
      IF (KDIR.EQ.1) THEN
         IF (PGP(1).LT.PGP(KZ)) THEN
            I1=1
            I2=KZ
            I3=1
         END IF
      ELSE
         IF (PZ(1).LT.PZ(KZ)) THEN
            I1=1
            I2=KZ
            I3=1
         END IF
      END IF
C
      IF (KDIR.EQ.1) THEN
         PZ(I1)=PGP(I1)/RG
      ELSE
C
         ZX=PZ(I1)/2./1000.+ZPRAD
         ZX2=ZX*ZX
C
C        Define d/dr of gravitationl potential
C
         ZC4=ZC2/ZX2
         ZC5=ZC3/ZX2
         ZC6=ZC33/ZX2/ZX2
         ZG1=ZC4*(-1.0+3.0*ZC5*ZP2+5.0*ZC6*ZP4)
C
C        Define d/dr of centrifugal potential
C
         ZG2=ZR2*ZX
C
C        Define d/rd(theta) of gravitational potential
C
         ZG3=-ZC4*(ZC5*ZPP2+ZC6*ZPP4)
C
C        Define d/rd(theta) of centrifugal potential
C
         ZG4=-ZC1*ZX*ZSC
C
C        Define graviational acceleration
C
         ZG1=ZG1+ZG2
         ZG1=ZG1*ZG1
         ZG2=ZG3+ZG4
         ZG2=ZG2*ZG2
         ZGRAVS=SQRT(ZG1+ZG2)/100.0
C
         PGP(I1)=PZ(I1)*ZGRAVS
      END IF
C
      DO JLEV=I1+I3,I2,I3
C
         IF (KDIR.EQ.1) THEN
C           Neglect difference between PGP/RG and PZ when calc.
C           grav. accel.
            ZX=(PGP(JLEV)+PGP(JLEV-I3))/2.0/1000./RG+ZPRAD
         ELSE
            ZX=(PZ(JLEV)+PZ(JLEV-I3))/2.0/1000.+ZPRAD
         END IF
C
         ZX2=ZX*ZX
C
C        Define d/dr of gravitationl potential
C
         ZC4=ZC2/ZX2
         ZC5=ZC3/ZX2
         ZC6=ZC33/ZX2/ZX2
         ZG1=ZC4*(-1.0+3.0*ZC5*ZP2+5.0*ZC6*ZP4)
C
C        Define d/dr of centrifugal potential
C
         ZG2=ZR2*ZX
C
C        Define d/rd(theta) of gravitational potential
C
         ZG3=-ZC4*(ZC5*ZPP2+ZC6*ZPP4)
C
C        Define d/rd(theta) of centrifugal potential
C
         ZG4=-ZC1*ZX*ZSC
C
C        Define graviational acceleration
C
         ZG1=ZG1+ZG2
         ZG1=ZG1*ZG1
         ZG2=ZG3+ZG4
         ZG2=ZG2*ZG2
         ZGRAVS=SQRT(ZG1+ZG2)/100.0
C
         IF (KDIR.EQ.1) THEN
            PZ(JLEV)=PZ(JLEV-I3)+
     1      (PGP(JLEV)-PGP(JLEV-I3))/ZGRAVS
         ELSE
            PGP(JLEV)=PGP(JLEV-I3)+
     1         ZGRAVS*(PZ(JLEV)-PZ(JLEV-I3))
         END IF
C
      END DO
C
      END IF
C
      RETURN
      END