  SUBROUTINE sflx_flux(temp)

!&E---------------------------------------------------------------------
!&E                 ***  ROUTINE sflx_flux  ***
!&E
!&E ** Purpose : call the routines computing exchanges at the air-sea interface
!&E              (momentum exchanges and heat fluxes)
!&E
!&E ** Description :
!&E
!&E ** Called by : step
!&E
!&E ** External calls : sflx_solar, sflx_turb, sflx_ir
!&E
!&E ** Used ij-arrays : temp,h0,tclim,sat,ventvent,rh,pa,fcc,
!&E                     rad,irlos,senslos,latlos,dirlos,dlatlos,dsenslos,hfrelax,
!&E                     rain,evap,
!&E                     cosfiu,sinfiu
!&E
!&E ** Modified variables : hfrelax,
!&E                         latlos,dlatlos,evap (sflx_turb),
!&E                         irlos,dirlos (sflx_ir),
!&E                         senslos,dsenslos (sflx_turb),
!&E                         rain (rain),
!&E                         tosx,tosy (sflx_turb)
!&E
!&E ** History :
!&E       !                        Original code
!&E       !  2010-07  (V. Garnier) Original code extracted from sflx_surf
!&E
!&E---------------------------------------------------------------------





























































!! * Modules used

   USE parameters, ONLY : kmax,rsh,rlg,limin,liminp1,limax,ljminp1,ljmax,ljmin,    &
                          valmanq,liminm1,limaxp2,ljminm1,ljmaxp2,pi,l_testcase,   &
                          num_testcase
   USE comvars2d,  ONLY : h0,cosfiu,sinfiu,ierrorlog,iwarnlog,t,l_obc_cycl_y,l_obc_cycl_x
   USE comfluxdo,  ONLY : evap,rain
   USE cometemp,   ONLY : l_rapflux,sat,ventvent,tclim,hfrelax,rh,fcc,l_meteoverif, &
                          l_sflx_solarcst
   USE cometdyn,   ONLY : l_sflx_rsolar, l_sflx_rir, l_sflx_rrain, l_sflx_rfcc,    &
                          l_sflx_rsat, l_sflx_rwind, l_sflx_rrh,   l_sflx_rpa, pa, &
                          file_meteo, tosx, tosy, cds, rhoair,                     &
                          windamp_duration,wind_veloc
   USE sflxatm,    ONLY : dirlos,dlatlos, dsenslos, rad, irlos, senslos, latlos,   &
                          drag, imeteo_exchtype, imeteo_dragtype
   USE sflxrad,    ONLY : sflx_ir, sflx_solar
   USE sflxturb,   ONLY : sflx_turb,cd
   USE toolmpi,ONLY : MASTER

   IMPLICIT NONE

!! * Arguments
   REAL(kind=rsh),DIMENSION(kmax,liminm1:limaxp2,ljminm1:ljmaxp2),INTENT(IN) :: temp

!! * Local declarations
   INTEGER         :: i,j,ncerror,IERR_MPI
   REAL(kind=rsh)  :: tdif
   REAL(kind=rsh)  :: windamp,tdays ! for test cases only

!!----------------------------------------------------------------------
!! * Executable part

   !PRINT*, 'enter SFLX_FLUX routine'

!$OMP PARALLEL DEFAULT(NONE) SHARED(l_rapflux,l_meteoverif,rain,evap    &
!$OMP& ,l_sflx_rsolar,l_sflx_rir,l_sflx_rrain,ventvent,pa,rh,sat,fcc    &
!$OMP& ,temp,hfrelax,dirlos,dlatlos,dsenslos,rad,irlos,senslos,latlos   &
!$OMP& ,tosx,tosy,l_sflx_solarcst,wind_veloc,windamp_duration,t         &

!$OMP& ,MASTER                                                          &

!$OMP& ,limin,liminp1,limax,ljmin,ljminp1,ljmax,h0,cosfiu,sinfiu        &
!$OMP& ,l_sflx_rfcc,l_sflx_rsat,l_sflx_rwind,l_sflx_rrh,tclim           &
!$OMP& ,cds,rhoair,imeteo_dragtype,imeteo_exchtype,drag                 &
!$OMP& ,l_obc_cycl_y,l_obc_cycl_x                                       &

!$OMP& ,l_sflx_rpa                                                      &


!$OMP& ,iwarnlog                                                        &

!$OMP& ,file_meteo,ierrorlog)                                           &
!$OMP& private(i,j,ncerror,IERR_MPI,tdif,windamp,tdays)

   l_rapflux=.FALSE.


! Estimates atmospherical fluxes
!-------------------------------

   IF (MASTER) THEN
! Check all needed meteorological variables have been read.
!$OMP SINGLE
   IF (.NOT.l_sflx_solarcst .AND. .NOT. l_meteoverif) THEN

     ncerror=0

! Check needed variables for the solar radiation.
     IF ((.NOT.l_sflx_rsolar).AND.(.NOT.l_sflx_rfcc)) THEN
       WRITE(ierrorlog,*) ' '
       WRITE(ierrorlog,*) 'ERROR: In SFLXFLUX.F90, cloud cover data are needed '
       WRITE(ierrorlog,*) 'to compute the solar radiation with non-homogenous conditions.'
       WRITE(ierrorlog,*) 'Set l_sflx_rfcc   = TRUE to read the cloud cover fraction,'
       WRITE(ierrorlog,*) 'or set l_sflx_rsolar = TRUE to read the solar radiation'
       WRITE(ierrorlog,*) 'from the meteorological file:'
       WRITE(ierrorlog,*) TRIM(file_meteo)
       WRITE(ierrorlog,*) 'Alternatively, set l_meteo_hom = TRUE to use'
       WRITE(ierrorlog,*) 'homogenous meteorological conditions'
       ncerror=ncerror+1
     ENDIF

! Check needed variables for the infra-red radiation.
     IF (.NOT.l_sflx_rir) THEN

       IF ((.NOT.l_sflx_rsat).OR.(.NOT.l_sflx_rfcc)) THEN
         WRITE(ierrorlog,*) ' '
         WRITE(ierrorlog,*) 'ERROR: In SFLXFLUX.F90, meteorological data are needed '
         WRITE(ierrorlog,*) 'to compute the infra-red radiation with non-homogenous conditions.'
         WRITE(ierrorlog,*) 'Set l_sflx_rsat = TRUE to read the air temperature'
         WRITE(ierrorlog,*) 'and l_sflx_rfcc = TRUE to read the cloud cover fraction,'
         WRITE(ierrorlog,*) 'or set l_sflx_rir = TRUE to read the infra-red radiation'
         WRITE(ierrorlog,*) 'from the meteorological file:'
         WRITE(ierrorlog,*) TRIM(file_meteo)
         WRITE(ierrorlog,*) 'Alternatively, set l_meteo_hom = TRUE to use'
         WRITE(ierrorlog,*) 'homogenous meteorological conditions'
         ncerror=ncerror+1
       ENDIF

     ENDIF

! Check needed variables for the turbulent fluxes.
     IF ((.NOT.l_sflx_rsat).OR.(.NOT.l_sflx_rwind).OR.(.NOT.l_sflx_rrh)) THEN
       WRITE(ierrorlog,*) ' '
       WRITE(ierrorlog,*) 'ERROR: In SFLXFLUX.F90, meteorological data are needed '
       WRITE(ierrorlog,*) 'to compute the turbulent fluxes with non-homogenous conditions.'
       WRITE(ierrorlog,*) 'Set l_sflx_rsat  = TRUE to read the air temperature,'
       WRITE(ierrorlog,*) '    l_sflx_rwind = TRUE to read the wind velocities,'
       WRITE(ierrorlog,*) 'and l_sflx_rrh   = TRUE to read the relative humidity'
       WRITE(ierrorlog,*) 'from the meteorological file:'
       WRITE(ierrorlog,*) TRIM(file_meteo)
       WRITE(ierrorlog,*) 'Alternatively, set l_meteo_hom = TRUE to use'
       WRITE(ierrorlog,*) 'homogenous meteorological conditions'
       ncerror=ncerror+1
     ENDIF

! Check needed variables for the turbulent fluxes.

     IF (.NOT.l_sflx_rpa) THEN
       WRITE(iwarnlog,*) ' '
       WRITE(iwarnlog,*) 'WARNING: In SFLXFLUX.F90, air pressure data are needed'
       WRITE(iwarnlog,*) 'to compute the turbulent fluxes with non-homogenous conditions'
       WRITE(iwarnlog,*) 'and with all formulations, except the formulation by Large.'
       WRITE(iwarnlog,*) 'Since l_sflx_rpa  = FALSE, the air pressure is constant and set to paref'
       WRITE(iwarnlog,*) 'Set l_sflx_rpa  = TRUE to read the air pressure'
       WRITE(iwarnlog,*) 'from the meteorological file:'
       WRITE(iwarnlog,*) TRIM(file_meteo)
     ENDIF






     IF (ncerror>=1) THEN
       WRITE(ierrorlog,*) 'Simulation stopped.'
       STOP
       CALL MPI_FINALIZE(IERR_MPI)
     ENDIF

     l_meteoverif=.true.

   ENDIF ! end of check on met. variables
!$OMP END SINGLE

   ENDIF


! ---------------------------
! Assign atmospherical fluxes
! ---------------------------

   IF (l_sflx_solarcst) THEN

! constant radiative flux
! no infra-red and turbulent flux
! Assign the wind stress (multiply by the correct drag coefficient)
!------------------------------------------------------------------



     IF (.NOT. l_testcase) THEN

       IF(imeteo_dragtype==0 .AND. imeteo_exchtype==0) THEN
!$OMP DO SCHEDULE(RUNTIME)
         DO j=ljmin,ljmax
           DO i = limin,limax
             tosx(i,j)=rhoair*cds*tosx(i,j)
             tosy(i,j)=rhoair*cds*tosy(i,j)
           END DO
         END DO
!$OMP END DO
       ELSE
!$OMP DO SCHEDULE(RUNTIME)
         DO j=ljmin,ljmax
           DO i = limin,limax
             tdif=sat(i,j)-temp(kmax,i,j)
             IF(tdif> 5.0_rsh) tdif=5.0_rsh
             IF(tdif<-5.0_rsh) tdif=-5.0_rsh
             tosx(i,j)=rhoair*cd(ventvent(i,j),tdif,drag,imeteo_dragtype,imeteo_exchtype)*tosx(i,j)
             tosy(i,j)=rhoair*cd(ventvent(i,j),tdif,drag,imeteo_dragtype,imeteo_exchtype)*tosy(i,j)
           END DO
         END DO
!$OMP END DO
       END IF

     ELSE ! test_case

       IF (t .LE. windamp_duration) THEN
! damp for the first windamp_duration
         tdays = t/86400.0_rlg
         windamp = SIGN(wind_veloc**2,wind_veloc) * SIN(pi*tdays/4.0_rsh)
       ELSE
         IF (num_testcase==4 .OR. num_testcase==26 .OR. num_testcase==22) THEN
           windamp = SIGN(wind_veloc**2,wind_veloc)
         ELSE
           windamp = 0.0_rsh
         END IF
       ENDIF
!$OMP DO SCHEDULE(RUNTIME)
       DO j=ljmin,ljmax
         DO i = limin,limax
           IF (l_obc_cycl_y) THEN
             tosy(i,j)=windamp
             tosx(i,j)=0.0_rsh
           ELSE IF (l_obc_cycl_x) THEN
             tosx(i,j)=windamp
             tosy(i,j)=0.0_rsh
           ELSE
             tosx(i,j)=windamp
             tosy(i,j)=windamp
           END IF
         END DO
       END DO
!$OMP END DO

     END IF ! ends test on l_test_case


   ELSE  ! variable heat fluxes

!$OMP DO SCHEDULE(RUNTIME)
     DO j=ljminp1,ljmax
       DO i=liminp1,limax
         IF (h0(i,j) > -valmanq) THEN

! incident solar heat flux
           IF (.NOT.l_sflx_rsolar) &
             CALL sflx_solar(i,j,fcc(i,j),sinfiu(i,j),cosfiu(i,j),rad(i,j))


! long-wave thermal flux (infra-red)
           IF (.NOT.l_sflx_rir)                                     & 
             CALL sflx_ir(temp(kmax,i,j),sat(i,j),rh(i,j),fcc(i,j), &
                            irlos(i,j),dirlos(i,j))

! latent and sensible fluxes
! Caution: For Fairall formulation, "rain" must be expressed in mm/hour.
           CALL sflx_turb(temp(kmax,i,j),sat(i,j),ventvent(i,j),pa(i,j),rh(i,j), &
                          rad(i,j),irlos(i,j),rain(i,j),                         &
                          senslos(i,j),latlos(i,j),evap(i,j),tosx(i,j),tosy(i,j))
         END IF
       END DO
     END DO
!$OMP END DO

     IF (.NOT.l_sflx_rrain) evap(:,:)=0.0_rsh

     IF (l_rapflux) THEN
!$OMP DO SCHEDULE(RUNTIME)
       DO j=ljmin,ljmax
         DO i=limin,limax
           hfrelax(i,j)=(dirlos(i,j)+dlatlos(i,j)+dsenslos(i,j))*(temp(kmax,i,j)-tclim(i,j))
         END DO
       END DO
!$OMP END DO
     END IF

   END IF   ! ends test on l_sflx_solarcst

!$OMP END PARALLEL

   !PRINT*, 'exit SFLX_FLUX routine'

END SUBROUTINE sflx_flux
