!+ Program to convert from densities of water vapour to q.
!
! Method:
!	The densities and temperature are read in and q is 
!	calculated using a standard formula.
!
! Current Owner of Code: J. M. Edwards
!
! History:
!	Version		Date		Comment
!	1.0		12-04-95	First Version under RCS
!					(J. M. Edwards)
!
! Description of Code:
!   FORTRAN 77 with extensions listed in documentation.
!
!- ---------------------------------------------------------------------
      PROGRAM DENTOQ
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/dimensions_field.ucf'
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/error.pcf'
      INCLUDE 'incl/physical_constants_1.ccf'
      INCLUDE 'incl/samson_pstar.ecf'
!
!     DECLARATION OF VARIABLES.
      CHARACTER
     &     FILE_DEN*80
!             NAME OF FILE WITH DENISTY
     &   , FILE_T*80
!             NAME OF TEMPERATURE FILE
     &   , FILE_MIX*80
!             NAME OF FILE WITH MIXING RATIO
     &   , TITLE_DUMMY*80
!             DUMMY TITLE
     &   , TITLE_MIX*80
!             OUTPUT TITLE
      INTEGER
     &     IERR
!             ERROR FLAG
     &   , N_PROFILE
!             NUMBER OF PROFILES
     &   , N_PROFILE_T
!             NUMBER OF TEMPERATURE PROFILES
     &   , N_LEVEL
!             NUMBER OF LEVELS
     &   , N_LEVEL_T
!             NUMBER OF TEMPERATURE LEVELS
     &   , I
!             LOOP VARIABLE
     &   , L
!             LOOP VARIABLE
      REAL*8
     &     P(NPD_PROFILE, NPD_LAYER+1)
!             PRESSURE LEVELS
     &   , T(NPD_PROFILE, NPD_LAYER+1)
!             TEMPERATURES
     &   , DENSITY(NPD_PROFILE, NPD_LAYER+1)
!             MASS DENSITY
     &   , MIX_RATIO(NPD_PROFILE, NPD_LAYER+1)
!             MIXING RATIO
     &   , RHO_DRY
!             DENISTY OF DRY AIR
     &   , PSTAR(NPD_PROFILE)
!             SURFACE PRESSURE
!
!     SUBROUTINES CALLED:
      EXTERNAL
     &     READ_SAMSON_FIELD, WRITE_SAMSON_FIELD
!
!
      DATA TITLE_MIX(1:43)
     &   /' Water Vapour Mass Mixing Ratio (converted)'/
      DATA PSTAR/NPD_PROFILE*PSTAR_DEFAULT/
!
!
!
      WRITE(IU_USER, '(/A)')
     &     'ENTER NAME OF FILE CONTAINING MASS DENSITIES.'
      READ(IU_STDIN, '(A)') FILE_DEN
      CALL READ_SAMSON_FIELD(IERR, .TRUE., .FALSE.
     &   , FILE_DEN, TITLE_DUMMY
     &   , N_PROFILE, N_LEVEL, P, PSTAR, DENSITY)
      N_LEVEL=N_LEVEL+1
      IF (IERR.NE.I_NORMAL) STOP
!
      WRITE(IU_USER, '(/A)')
     &     'ENTER NAME OF FILE CONTAINING TEMPERATURES.'
      READ(IU_STDIN, '(A)') FILE_T
      CALL READ_SAMSON_FIELD(IERR, .TRUE., .FALSE.
     &   , FILE_T, TITLE_DUMMY
     &   , N_PROFILE_T, N_LEVEL_T, P, PSTAR, T)
      N_LEVEL_T=N_LEVEL_T+1
      IF (IERR.NE.I_NORMAL) STOP
      IF (N_PROFILE.NE.N_PROFILE_T) THEN
         WRITE(IU_ERR, '(/A)') 
     &      '*** ERROR: NUMBER OF PROFILES DOES NOT MATCH.'
         IERR=I_ERR_FATAL
         STOP
      ENDIF
      IF (N_LEVEL.NE.N_LEVEL_T) THEN
         WRITE(IU_ERR, '(/A)') 
     &      '*** ERROR: NUMBER OF LEVELS DOES NOT MATCH.'
         IERR=I_ERR_FATAL
         STOP
      ENDIF
!
      DO I=1, N_LEVEL
         DO L=1, N_PROFILE
            RHO_DRY=P(L, I)/(R_GAS_DRY*T(L, I))
     &         -RATIO_MOLAR_WEIGHT*DENSITY(L, I)
            MIX_RATIO(L, I)=DENSITY(L, I)/(RHO_DRY+DENSITY(L, I))
         ENDDO
      ENDDO
!
      WRITE(IU_USER, '(/A)') 'ENTER NAME OF OUTPUT FILE.'
      READ(IU_STDIN, '(A)') FILE_MIX
      CALL WRITE_SAMSON_FIELD(IERR, .FALSE.
     &   , FILE_MIX, TITLE_MIX
     &   , N_PROFILE, N_LEVEL-1, P, PSTAR, MIX_RATIO)
      IF (IERR.NE.I_NORMAL) STOP
!
!
      STOP
      END
