!+ Program to convert densities to mass fractions.
!
! Method:
!	Fields are read in as SAMSON files.
!	The density of air is calculated from T and q whence
!	the mixing ratio is calculated.
!
! 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 DENTOMIX
!
!
!
      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_DENSITY*80
!             NAME OF FILE WITH DENISTY
     &   , FILE_T*80
!             NAME OF TEMPERATURE FILE
     &   , FILE_Q*80
!             NAME OF HUMIDITY FILE
     &   , FILE_MIX*80
!             NAME OF FILE WITH MIXING RATIO
     &   , TITLE_DENSITY*80
!             TITLE OF FILE OF DENSITIES
     &   , TITLE_DUMMY*80
!             DUMMY TITLE
     &   , TITLE_MIX*80
!             OUTPUT TITLE
      INTEGER
     &     IERR
!             ERROR FLAG
     &   , N_LEVEL
!             NUMBER OF LEVELS
     &   , N_PROFILE
!             NUMBER OF PROFILES
     &   , N_PROFILE_T
!             NUMBER OF PROFILES
     &   , N_PROFILE_Q
!             NUMBER OF PROFILES
     &   , N_LEVEL_T
!             NUMBER OF TEMPERATURE LEVELS
     &   , N_LEVEL_Q
!             NUMBER OF HUMIDITY LEVELS
     &   , I
!             LOOP VARIABLE
     &   , L
!             LOOP VARIABLE
      REAL*8
     &     P(NPD_PROFILE, NPD_LAYER+1)
!             PRESSURE LEVELS
     &   , T(NPD_PROFILE, NPD_LAYER+1)
!             TEMPERATURES
     &   , Q(NPD_PROFILE, NPD_LAYER+1)
!             HUMIDITY MIXING RATIOS
     &   , DENSITY(NPD_PROFILE, NPD_LAYER+1)
!             MASS DENSITY
     &   , MIX_RATIO(NPD_PROFILE, NPD_LAYER+1)
!             MIXING RATIO
     &   , DENSITY_AIR
!             DENISTY OF AIR
     &   , PSTAR(NPD_PROFILE)
!             SURFACE PRESSURE
!
!     SUBROUTINES CALLED:
      EXTERNAL
     &     READ_SAMSON_FIELD, WRITE_SAMSON_FIELD
!
!
      DATA PSTAR/NPD_PROFILE*PSTAR_DEFAULT/
!
!
!
      WRITE(IU_USER, '(/A)')
     &     'ENTER NAME OF FILE CONTAINING MASS DENSITIES.'
      READ(IU_STDIN, '(A)') FILE_DENSITY
      CALL READ_SAMSON_FIELD(IERR, .TRUE., .FALSE.
     &   , FILE_DENSITY, TITLE_DENSITY
     &   , N_PROFILE, N_LEVEL, P, PSTAR, DENSITY)
      IF (IERR.NE.I_NORMAL) STOP
      N_LEVEL=N_LEVEL+1
!
      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)
      IF (IERR.NE.I_NORMAL) STOP
      N_LEVEL_T=N_LEVEL_T+1
      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
      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
!
      WRITE(IU_USER, '(/A)')
     &     'ENTER NAME OF FILE CONTAINING SPECIFIC HUMIDITY.'
      READ(IU_STDIN, '(A)') FILE_Q
      CALL READ_SAMSON_FIELD(IERR, .TRUE., .FALSE.
     &   , FILE_Q, TITLE_DUMMY
     &   , N_PROFILE_Q, N_LEVEL_Q, P, PSTAR, Q)
      IF (IERR.NE.I_NORMAL) STOP
      N_LEVEL_Q=N_LEVEL_Q+1
      IF (N_PROFILE.NE.N_PROFILE_Q) THEN
         WRITE(IU_ERR, '(A)') 
     &      '*** ERROR: NUMBER OF PROFILES DOES NOT MATCH.'
         IERR=I_ERR_FATAL
         STOP
      ENDIF
      IF (N_LEVEL.NE.N_LEVEL_Q) 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
            DENSITY_AIR=P(L, I)/(R_GAS_DRY*T(L, I)
     &         *(1.+(RATIO_MOLAR_WEIGHT-1.)*Q(L, I)))
            MIX_RATIO(L, I)=DENSITY(L, I)/DENSITY_AIR
         ENDDO
      ENDDO
!
      WRITE(IU_USER, '(/A)') 'ENTER NAME OF OUTPUT FILE.'
      READ(IU_STDIN, '(A)') FILE_MIX
      TITLE_MIX(1:80)=' Conversion from: '//TITLE_DENSITY(1: 62)
      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
