!+ Program to convert from dewpoints to q.
!
! Method:
!	The dewpoints are read in and an approprite routine
!	is called to determine the saturated mixing ratio.
!
! 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 DEWTOQ
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/dimensions_field.ucf'
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/error.pcf'
      INCLUDE 'incl/qsat_alg.pcf'
      INCLUDE 'incl/physical_constants_0.ccf'
      INCLUDE 'incl/samson_pstar.ecf'
!
!     DECLARATION OF VARIABLES.
!
      CHARACTER
     &     FILE_DEW*80
!             NAME OF FILE WITH DEWPOINT
     &   , FILE_MIX*80
!             NAME OF FILE WITH MIXING RATIO
     &   , TITLE_DUMMY*80
!             DUMMY TITLE
     &   , TITLE_MIX*80
!             OUTPUT TITLE
!
      LOGICAL
     &     LOCK_CODE
!             LOGICAL TO FORBID INTERACTIVE LOOPING
      EXTERNAL
     &     LOCK_CODE
!
      INTEGER
     &     IERR
!             ERROR FLAG
     &   , IOS
!             I/O ERROR FLAG
     &   , N_PROFILE
!             NUMBER OF PROFILES
     &   , N_LEVEL
!             NUMBER OF LEVELS
     &   , I_QSAT_ALG
!             ALGORITHM FOR CALCULATING SATURATED MIXING RATIO
     &   , I
!             LOOP VARIABLE
     &   , L
!             LOOP VARIABLE
!
      REAL*8
     &     P(NPD_PROFILE, NPD_LAYER+1)
!             PRESSURE LEVELS
     &   , DEWPOINT(NPD_PROFILE, NPD_LAYER+1)
!             DEWPOINT
     &   , MIX_RATIO(NPD_PROFILE, NPD_LAYER+1)
!             MIXING RATIO
     &   , PSTAR(NPD_PROFILE)
!             SURFACE PRESSURE
     &   , T_TRANSITION
!             TRANSITION TEMPERATURE FOR SATURATION W.R.T. WATER/ICE
!             IN THE GOFF-GRATSCH FORMULA.
!
!     SUBROUTINES CALLED:
      EXTERNAL
     &     READ_SAMSON_FIELD, WRITE_SAMSON_FIELD
     &   , QSAT, QSAT_GG, QSAT_GG_ICE
!
!
      DATA PSTAR/NPD_PROFILE*PSTAR_DEFAULT/
      DATA TITLE_MIX(1:43)
     &   /' Water Vapour Mass Mixing Ratio (converted)'/
!
!
!
      WRITE(IU_USER, '(/A)')
     &     'ENTER NAME OF FILE CONTAINING DEWPOINTS.'
      READ(IU_STDIN, '(A)') FILE_DEW
      CALL READ_SAMSON_FIELD(IERR, .TRUE., .FALSE.
     &   , FILE_DEW, TITLE_DUMMY
     &   , N_PROFILE, N_LEVEL, P, PSTAR, DEWPOINT)
      N_LEVEL=N_LEVEL+1
      IF (IERR.NE.I_NORMAL) STOP
!
!     DETERMINE ALGORITHM FOR CONVERSION OF DEWPOINTS.
      WRITE(IU_USER, '(/A)') 'ENTER THE ALGORITHM FOR CONVERSION.'
1     READ(IU_STDIN, *, IOSTAT=IOS) I_QSAT_ALG
      IF (IOS.NE.0) THEN
         WRITE(IU_ERR, '(/A)') '+++ UNRECOGNISED REPONSE.'
         IF (LOCK_CODE(.TRUE.)) THEN
            INCLUDE 'batch_error_main.finc'
         ELSE
            WRITE(IU_USER, '(/A)') 'PLEASE RE-ENTER.'
            GOTO 1
         ENDIF
      ENDIF
      IF ( (I_QSAT_ALG.NE.IP_UM_LOOK_UP).AND.
     &     (I_QSAT_ALG.NE.IP_GOFF_GRATSCH) ) THEN
         WRITE(IU_ERR, '(/A)') '+++ INVALID ALFGORITHM.'
         IF (LOCK_CODE(.TRUE.)) THEN
            INCLUDE 'batch_error_main.finc'
         ELSE
            WRITE(IU_USER, '(/A)') 'PLEASE RE-ENTER.'
            GOTO 1
         ENDIF
      ENDIF
!
      IF (I_QSAT_ALG.EQ.IP_UM_LOOK_UP) THEN
         DO I=1, N_LEVEL
            DO L=1, N_PROFILE
                CALL QSAT(MIX_RATIO(L, I), DEWPOINT(L, I), P(L, I), 1)
            ENDDO
         ENDDO
      ELSE IF (I_QSAT_ALG.EQ.IP_GOFF_GRATSCH) THEN
         WRITE(IU_USER, '(A)') 'ENTER TRANSITION TEMPERATURE FOR '
     &      //'SATURATION WITH RESPECT TO WATER OR ICE.'
         READ(IU_STDIN , *) T_TRANSITION
         DO I=1, N_LEVEL
            DO L=1, N_PROFILE
                IF (DEWPOINT(L, I).GE.T_TRANSITION) THEN
                   CALL QSAT_GG(MIX_RATIO(L, I)
     &                , DEWPOINT(L, I), P(L, I))
                ELSE
                   CALL QSAT_GG_ICE(MIX_RATIO(L, I)
     &                , DEWPOINT(L, I), P(L, I))
                ENDIF
            ENDDO
         ENDDO
      ENDIF
!
      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
