!+ Program to calculate heating rates from net fluxes.
!
! Method:
!	From the net fluxes, the net flow of energy into a layer is
!	determined. From the pressures the mass within the layer
!	can be found. The heating rates are now calculated assuming
!	that the specific heat may be taken as that of dry air.
!	The heating rates are allocated pressurea at the mid-points
!	of layers.
!
! 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 NETTOHR
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/dimensions_field.ucf'
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/error.pcf'
      INCLUDE 'incl/physical_constants_0.ccf'
      INCLUDE 'incl/physical_constants_1.ccf'
      INCLUDE 'incl/samson_pstar.ecf'
!
!     DECLARATION OF VARIABLES.
      CHARACTER
     &     FILE_NET*80
!             NAME OF FILE OF NET FLUXES
     &   , FILE_HR*80
!             NAME OF OUTPUT FILE
     &   , TITLE_DUMMY*80
!             DUMMY TITLE
     &   , TITLE_HR*80
!             OUTPUT TITLE
      INTEGER
     &     IERR
!             ERROR FLAG
     &   , N_DUMMY
!             DUMMY NUMBER OF PROFILES
     &   , N_LAYER
!             NUMBER OF LAYERS
     &   , I
!             LOOP VARIABLE
      REAL*8
     &     P(NPD_PROFILE, 0: NPD_LAYER)
!             PRESSURE LEVELS
     &   , NET_FLUX(NPD_PROFILE
     &              , 0: NPD_LAYER)
!             NET FLUXES
     &   , P_LAYER(NPD_PROFILE
     &              , NPD_LAYER)
!             PRESSURES IN LAYERS
     &   , HEAT_RATE(NPD_PROFILE
     &              , NPD_LAYER)
!             HEATING RATES
     &   , PSTAR(NPD_PROFILE)
!             SURFACE PRESSURE
!
!     SUBROUTINES CALLED:
      EXTERNAL
     &     READ_SAMSON_FIELD, WRITE_SAMSON_FIELD
!
!
      DATA TITLE_HR(1:41)
     &   /' Heating Rates determined from Net Fluxes'/
      DATA PSTAR/NPD_PROFILE*PSTAR_DEFAULT/
!
!
!
      WRITE(IU_USER, '(/A)')
     &     'ENTER NAME OF FILE CONTAINING THE NET FLUXES.'
      READ(IU_STDIN, '(A)') FILE_NET
      CALL READ_SAMSON_FIELD(IERR, .TRUE., .FALSE.
     &   , FILE_NET, TITLE_DUMMY
     &   , N_DUMMY, N_LAYER, P, PSTAR, NET_FLUX)
      IF (IERR.NE.I_NORMAL) STOP
!
      DO I=1, N_LAYER
         P_LAYER(1, I)=0.5D+00*(P(1, I-1)+P(1, I))
         HEAT_RATE(1, I)=-GRAV_ACC*(NET_FLUX(1, I)-NET_FLUX(1, I-1))
     &      /(CP_AIR_DRY*(P(1, I)-P(1, I-1)))*SECONDS_PER_DAY
         print*,i,p(1,i),p(1,i-1),(P(1, I)-P(1, I-1)),p_layer(1,i),
     &        heat_rate(1,i)
      ENDDO
!
      WRITE(IU_USER, '(/A)') 'ENTER NAME OF FILE OF HEATING RATES.'
      READ(IU_STDIN, '(A)') FILE_HR
      CALL WRITE_SAMSON_FIELD(IERR, .TRUE.
     &   , FILE_HR, TITLE_HR
     &   , 1, N_LAYER, P_LAYER, PSTAR, HEAT_RATE)
      IF (IERR.NE.I_NORMAL) STOP
!
!
      STOP
      END
