Skip to content
Snippets Groups Projects
sbccpl.F90 188 KiB
Newer Older
Guillaume Samson's avatar
Guillaume Samson committed

      IF(TRIM( sn_snd_ifrac%cldes )  == 'coupled') ssnd(jps_ficet)%laction = .TRUE.

      SELECT CASE ( TRIM( sn_snd_thick%cldes ) )
      CASE( 'none'         )       ! nothing to do
      CASE( 'ice and snow' )
         ssnd(jps_hice:jps_hsnw)%laction = .TRUE.
         IF( TRIM( sn_snd_thick%clcat ) == 'yes' ) THEN
            ssnd(jps_hice:jps_hsnw)%nct = nn_cats_cpl
         ENDIF
      CASE ( 'weighted ice and snow' )
         ssnd(jps_hice:jps_hsnw)%laction = .TRUE.
         IF( TRIM( sn_snd_thick%clcat ) == 'yes' ) ssnd(jps_hice:jps_hsnw)%nct = nn_cats_cpl
      CASE default   ;   CALL ctl_stop( 'sbc_cpl_init: wrong definition of sn_snd_thick%cldes' )
      END SELECT

      !                                                      ! ------------------------- !
      !                                                      !      Ice Meltponds        !
      !                                                      ! ------------------------- !
      ! Needed by Met Office
      ssnd(jps_a_p)%clname  = 'OPndFrc'
      ssnd(jps_ht_p)%clname = 'OPndTck'
      SELECT CASE ( TRIM( sn_snd_mpnd%cldes ) )
      CASE ( 'none' )
         ssnd(jps_a_p)%laction  = .FALSE.
         ssnd(jps_ht_p)%laction = .FALSE.
      CASE ( 'ice only' )
         ssnd(jps_a_p)%laction  = .TRUE.
         ssnd(jps_ht_p)%laction = .TRUE.
         IF( TRIM( sn_snd_mpnd%clcat ) == 'yes' ) THEN
            ssnd(jps_a_p)%nct  = nn_cats_cpl
            ssnd(jps_ht_p)%nct = nn_cats_cpl
         ELSE
            IF( nn_cats_cpl > 1 ) THEN
               CALL ctl_stop( 'sbc_cpl_init: use weighted ice option for sn_snd_mpnd%cldes if not exchanging category fields' )
            ENDIF
         ENDIF
      CASE ( 'weighted ice' )
         ssnd(jps_a_p)%laction  = .TRUE.
         ssnd(jps_ht_p)%laction = .TRUE.
         IF( TRIM( sn_snd_mpnd%clcat ) == 'yes' ) THEN
            ssnd(jps_a_p)%nct  = nn_cats_cpl
            ssnd(jps_ht_p)%nct = nn_cats_cpl
         ENDIF
      CASE default   ;   CALL ctl_stop( 'sbc_cpl_init: wrong definition of sn_snd_mpnd%cldes; '//sn_snd_mpnd%cldes )
      END SELECT

      !                                                      ! ------------------------- !
      !                                                      !      Surface current      !
      !                                                      ! ------------------------- !
      !        ocean currents              !            ice velocities
      ssnd(jps_ocx1)%clname = 'O_OCurx1'   ;   ssnd(jps_ivx1)%clname = 'O_IVelx1'
      ssnd(jps_ocy1)%clname = 'O_OCury1'   ;   ssnd(jps_ivy1)%clname = 'O_IVely1'
      ssnd(jps_ocz1)%clname = 'O_OCurz1'   ;   ssnd(jps_ivz1)%clname = 'O_IVelz1'
      ssnd(jps_ocxw)%clname = 'O_OCurxw'
      ssnd(jps_ocyw)%clname = 'O_OCuryw'
      !
      ssnd(jps_ocx1:jps_ivz1)%nsgn = -1.   ! vectors: change of the sign at the north fold

      IF( sn_snd_crt%clvgrd == 'U,V' ) THEN
         ssnd(jps_ocx1)%clgrid = 'U' ; ssnd(jps_ocy1)%clgrid = 'V'
      ELSE IF( sn_snd_crt%clvgrd /= 'T' ) THEN
         CALL ctl_stop( 'sn_snd_crt%clvgrd must be equal to T' )
      ENDIF
      ssnd(jps_ocx1:jps_ivz1)%laction = .TRUE.   ! default: all are send
      IF( TRIM( sn_snd_crt%clvref ) == 'spherical' )   ssnd( (/jps_ocz1, jps_ivz1/) )%laction = .FALSE.
      IF( TRIM( sn_snd_crt%clvor ) == 'eastward-northward' ) ssnd(jps_ocx1:jps_ivz1)%nsgn = 1.
      SELECT CASE( TRIM( sn_snd_crt%cldes ) )
      CASE( 'none'                 )   ;   ssnd(jps_ocx1:jps_ivz1)%laction = .FALSE.
      CASE( 'oce only'             )   ;   ssnd(jps_ivx1:jps_ivz1)%laction = .FALSE.
      CASE( 'weighted oce and ice' )   !   nothing to do
      CASE( 'mixed oce-ice'        )   ;   ssnd(jps_ivx1:jps_ivz1)%laction = .FALSE.
      CASE default   ;   CALL ctl_stop( 'sbc_cpl_init: wrong definition of sn_snd_crt%cldes' )
      END SELECT

      ssnd(jps_ocxw:jps_ocyw)%nsgn = -1.   ! vectors: change of the sign at the north fold

      IF( sn_snd_crtw%clvgrd == 'U,V' ) THEN
         ssnd(jps_ocxw)%clgrid = 'U' ; ssnd(jps_ocyw)%clgrid = 'V'
      ELSE IF( sn_snd_crtw%clvgrd /= 'T' ) THEN
         CALL ctl_stop( 'sn_snd_crtw%clvgrd must be equal to T' )
      ENDIF
      IF( TRIM( sn_snd_crtw%clvor ) == 'eastward-northward' ) ssnd(jps_ocxw:jps_ocyw)%nsgn = 1.
      SELECT CASE( TRIM( sn_snd_crtw%cldes ) )
         CASE( 'none'                 )   ; ssnd(jps_ocxw:jps_ocyw)%laction = .FALSE.
         CASE( 'oce only'             )   ; ssnd(jps_ocxw:jps_ocyw)%laction = .TRUE.
         CASE( 'weighted oce and ice' )   !   nothing to do
         CASE( 'mixed oce-ice'        )   ; ssnd(jps_ivx1:jps_ivz1)%laction = .FALSE.
         CASE default   ;   CALL ctl_stop( 'sbc_cpl_init: wrong definition of sn_snd_crtw%cldes' )
      END SELECT

      !                                                      ! ------------------------- !
      !                                                      !          CO2 flux         !
      !                                                      ! ------------------------- !
      ssnd(jps_co2)%clname = 'O_CO2FLX' ;  IF( TRIM(sn_snd_co2%cldes) == 'coupled' )    ssnd(jps_co2 )%laction = .TRUE.
      !

#if defined key_medusa 
      !                                                      ! ------------------------- !
      !                                                      !   MEDUSA output fields    !
      !                                                      ! ------------------------- !
      ! Surface dimethyl sulphide from Medusa
      ssnd(jps_bio_dms)%clname = 'OBioDMS'
      IF( TRIM(sn_snd_bio_dms%cldes) == 'medusa' )    ssnd(jps_bio_dms )%laction = .TRUE.

      ! Surface CO2 flux from Medusa
      ssnd(jps_bio_co2)%clname = 'OBioCO2'
      IF( TRIM(sn_snd_bio_co2%cldes) == 'medusa' )    ssnd(jps_bio_co2 )%laction = .TRUE.

      ! Surface chlorophyll from Medusa
      ssnd(jps_bio_chloro)%clname = 'OBioChlo'
      IF( TRIM(sn_snd_bio_chloro%cldes) == 'medusa' ) ssnd(jps_bio_chloro )%laction = .TRUE.
#endif


Guillaume Samson's avatar
Guillaume Samson committed
      !                                                      ! ------------------------- !
      !                                                      ! Sea surface freezing temp !
      !                                                      ! ------------------------- !
      ! needed by Met Office
      ssnd(jps_sstfrz)%clname = 'O_SSTFrz' ; IF( TRIM(sn_snd_sstfrz%cldes) == 'coupled' )  ssnd(jps_sstfrz)%laction = .TRUE.
      !
      !                                                      ! ------------------------- !
      !                                                      !    Ice conductivity       !
      !                                                      ! ------------------------- !
      ! needed by Met Office
      ! Note that ultimately we will move to passing an ocean effective conductivity as well so there
      ! will be some changes to the parts of the code which currently relate only to ice conductivity
      ssnd(jps_ttilyr )%clname = 'O_TtiLyr'
      SELECT CASE ( TRIM( sn_snd_ttilyr%cldes ) )
      CASE ( 'none' )
         ssnd(jps_ttilyr)%laction = .FALSE.
      CASE ( 'ice only' )
         ssnd(jps_ttilyr)%laction = .TRUE.
         IF( TRIM( sn_snd_ttilyr%clcat ) == 'yes' ) THEN
            ssnd(jps_ttilyr)%nct = nn_cats_cpl
         ELSE
            IF( nn_cats_cpl > 1 ) THEN
               CALL ctl_stop( 'sbc_cpl_init: use weighted ice option for sn_snd_ttilyr%cldes if not exchanging category fields' )
            ENDIF
         ENDIF
      CASE ( 'weighted ice' )
         ssnd(jps_ttilyr)%laction = .TRUE.
         IF( TRIM( sn_snd_ttilyr%clcat ) == 'yes' ) ssnd(jps_ttilyr)%nct = nn_cats_cpl
      CASE default   ;   CALL ctl_stop( 'sbc_cpl_init: wrong definition of sn_snd_ttilyr%cldes;'//sn_snd_ttilyr%cldes )
      END SELECT

      ssnd(jps_kice )%clname = 'OIceKn'
      SELECT CASE ( TRIM( sn_snd_cond%cldes ) )
      CASE ( 'none' )
         ssnd(jps_kice)%laction = .FALSE.
      CASE ( 'ice only' )
         ssnd(jps_kice)%laction = .TRUE.
         IF( TRIM( sn_snd_cond%clcat ) == 'yes' ) THEN
            ssnd(jps_kice)%nct = nn_cats_cpl
         ELSE
            IF( nn_cats_cpl > 1 ) THEN
               CALL ctl_stop( 'sbc_cpl_init: use weighted ice option for sn_snd_cond%cldes if not exchanging category fields' )
            ENDIF
         ENDIF
      CASE ( 'weighted ice' )
         ssnd(jps_kice)%laction = .TRUE.
         IF( TRIM( sn_snd_cond%clcat ) == 'yes' ) ssnd(jps_kice)%nct = nn_cats_cpl
      CASE default   ;   CALL ctl_stop( 'sbc_cpl_init: wrong definition of sn_snd_cond%cldes;'//sn_snd_cond%cldes )
      END SELECT
      !
      !                                                      ! ------------------------- !
      !                                                      !     Sea surface height    !
      !                                                      ! ------------------------- !
      ssnd(jps_wlev)%clname = 'O_Wlevel' ;  IF( TRIM(sn_snd_wlev%cldes) == 'coupled' )   ssnd(jps_wlev)%laction = .TRUE.

      !                                                      ! ------------------------------- !
      !                                                      !   OCE-SAS coupling - snd by opa !
      !                                                      ! ------------------------------- !
      ssnd(jps_ssh   )%clname = 'O_SSHght'
      ssnd(jps_soce  )%clname = 'O_SSSal'
      ssnd(jps_e3t1st)%clname = 'O_E3T1st'
      ssnd(jps_fraqsr)%clname = 'O_FraQsr'
      !
      IF( nn_components == jp_iam_oce ) THEN
         ssnd(:)%laction = .FALSE.   ! force default definition in case of opa <-> sas coupling
         ssnd( (/jps_toce, jps_soce, jps_ssh, jps_fraqsr, jps_ocx1, jps_ocy1/) )%laction = .TRUE.
         ssnd( jps_e3t1st )%laction = .NOT.ln_linssh
         ! vector definition: not used but cleaner...
         ssnd(jps_ocx1)%clgrid  = 'U'        ! oce components given at U-point
         ssnd(jps_ocy1)%clgrid  = 'V'        !           and           V-point
         sn_snd_crt%clvgrd = 'U,V'
         sn_snd_crt%clvor = 'local grid'
         sn_snd_crt%clvref = 'spherical'
         !
         IF(lwp) THEN                        ! control print
            WRITE(numout,*)
            WRITE(numout,*)'  sent fields to SAS component '
            WRITE(numout,*)'               sea surface temperature (T before, Celsius) '
            WRITE(numout,*)'               sea surface salinity '
            WRITE(numout,*)'               surface currents U,V on local grid and spherical coordinates'
            WRITE(numout,*)'               sea surface height '
            WRITE(numout,*)'               thickness of first ocean T level '
            WRITE(numout,*)'               fraction of solar net radiation absorbed in the first ocean level'
            WRITE(numout,*)
         ENDIF
      ENDIF
      !                                                      ! ------------------------------- !
      !                                                      !   OCE-SAS coupling - snd by sas !
      !                                                      ! ------------------------------- !
      ssnd(jps_sflx  )%clname = 'I_SFLX'
      ssnd(jps_fice2 )%clname = 'IIceFrc'
      ssnd(jps_qsroce)%clname = 'I_QsrOce'
      ssnd(jps_qnsoce)%clname = 'I_QnsOce'
      ssnd(jps_oemp  )%clname = 'IOEvaMPr'
      ssnd(jps_otx1  )%clname = 'I_OTaux1'
      ssnd(jps_oty1  )%clname = 'I_OTauy1'
      ssnd(jps_rnf   )%clname = 'I_Runoff'
      ssnd(jps_taum  )%clname = 'I_TauMod'
      !
      IF( nn_components == jp_iam_sas ) THEN
         IF( .NOT. ln_cpl ) ssnd(:)%laction = .FALSE.   ! force default definition in case of opa <-> sas coupling
         ssnd( (/jps_qsroce, jps_qnsoce, jps_oemp, jps_fice2, jps_sflx, jps_otx1, jps_oty1, jps_taum/) )%laction = .TRUE.
         !
         ! Change first letter to couple with atmosphere if already coupled with sea_ice
         ! this is nedeed as each variable name used in the namcouple must be unique:
         ! for example O_SSTSST sent by OCE to SAS and therefore S_SSTSST sent by SAS to the Atmosphere
         DO jn = 1, jpsnd
            IF( ssnd(jn)%clname(1:1) == "O" ) ssnd(jn)%clname = "S"//ssnd(jn)%clname(2:LEN(ssnd(jn)%clname))
         END DO
         !
         IF(lwp) THEN                        ! control print
            WRITE(numout,*)
            IF( .NOT. ln_cpl ) THEN
               WRITE(numout,*)'  sent fields to OCE component '
            ELSE
               WRITE(numout,*)'  Additional sent fields to OCE component : '
            ENDIF
            WRITE(numout,*)'                  ice cover '
            WRITE(numout,*)'                  oce only EMP  '
            WRITE(numout,*)'                  salt flux  '
            WRITE(numout,*)'                  mixed oce-ice solar flux  '
            WRITE(numout,*)'                  mixed oce-ice non solar flux  '
            WRITE(numout,*)'                  wind stress U,V components'
            WRITE(numout,*)'                  wind stress module'
         ENDIF
      ENDIF

      ! Initialise 1D river outflow scheme 
      nn_cpl_river = 1 
      IF ( TRIM( sn_rcv_rnf%cldes ) == 'coupled1d' ) CALL cpl_rnf_1d_init   ! Coupled runoff using 1D array
      
      ! =================================================== !
      ! Allocate all parts of frcv used for received fields !
      ! =================================================== !
      DO jn = 1, jprcv

         IF ( srcv(jn)%laction ) THEN 
            SELECT CASE( srcv(jn)%dimensions )
            !
            CASE( 0 )   ! Scalar field
               ALLOCATE( frcv(jn)%z3(1,1,1) )
               
            CASE( 1 )   ! 1D field
               ALLOCATE( frcv(jn)%z3(nn_cpl_river,1,1) )
               
            CASE DEFAULT  ! 2D (or pseudo 3D) field.
               ALLOCATE( frcv(jn)%z3(jpi,jpj,srcv(jn)%nct) )
            END SELECT
         END IF

      END DO
      ! Allocate taum part of frcv which is used even when not received as coupling field
      IF ( .NOT. srcv(jpr_taum)%laction ) ALLOCATE( frcv(jpr_taum)%z3(jpi,jpj,srcv(jpr_taum)%nct) )
      ! Allocate w10m part of frcv which is used even when not received as coupling field
      IF ( .NOT. srcv(jpr_w10m)%laction ) ALLOCATE( frcv(jpr_w10m)%z3(jpi,jpj,srcv(jpr_w10m)%nct) )
      ! Allocate jpr_otx1 part of frcv which is used even when not received as coupling field
      IF ( .NOT. srcv(jpr_otx1)%laction ) ALLOCATE( frcv(jpr_otx1)%z3(jpi,jpj,srcv(jpr_otx1)%nct) )
      IF ( .NOT. srcv(jpr_oty1)%laction ) ALLOCATE( frcv(jpr_oty1)%z3(jpi,jpj,srcv(jpr_oty1)%nct) )
      ! Allocate itx1 and ity1 as they are used in sbc_cpl_ice_tau even if srcv(jpr_itx1)%laction = .FALSE.
      IF( k_ice /= 0 ) THEN
         IF ( .NOT. srcv(jpr_itx1)%laction ) ALLOCATE( frcv(jpr_itx1)%z3(jpi,jpj,srcv(jpr_itx1)%nct) )
         IF ( .NOT. srcv(jpr_ity1)%laction ) ALLOCATE( frcv(jpr_ity1)%z3(jpi,jpj,srcv(jpr_ity1)%nct) )
      END IF

Guillaume Samson's avatar
Guillaume Samson committed
      !
      ! ================================ !
      !   initialisation of the coupler  !
      ! ================================ !

      ! There's no point initialising the coupler if we've accumulated any errors in 
      ! coupling field definitions or settings. 
      IF (nstop > 0) CALL ctl_stop( 'STOP', 'sbc_cpl_init: Errors encountered in coupled field definitions' )

Guillaume Samson's avatar
Guillaume Samson committed
      CALL cpl_define(jprcv, jpsnd, nn_cplmodel)

      IF(ln_usecplmask) THEN
         xcplmask(:,:,:) = 0.
         CALL iom_open( 'cplmask', inum )
         CALL iom_get( inum, jpdom_unknown, 'cplmask', xcplmask(1:jpi,1:jpj,1:nn_cplmodel),   &
            &          kstart = (/ mig(1),mjg(1),1 /), kcount = (/ jpi,jpj,nn_cplmodel /) )
         CALL iom_close( inum )
      ELSE
         xcplmask(:,:,:) = 1.
      ENDIF
      xcplmask(:,:,0) = 1. - SUM( xcplmask(:,:,1:nn_cplmodel), dim = 3 )
      !
      IF( nn_coupled_iceshelf_fluxes .gt. 0 ) THEN 
          ! Crude masks to separate the Antarctic and Greenland icesheets. Obviously something 
          ! more complicated could be done if required. 
          greenland_icesheet_mask = 0.0 
          WHERE( gphit >= 0.0 ) greenland_icesheet_mask = 1.0 
          antarctica_icesheet_mask = 0.0 
          WHERE( gphit < 0.0 ) antarctica_icesheet_mask = 1.0 
  
          IF( .not. ln_rstart ) THEN 
             greenland_icesheet_mass = 0.0  
             greenland_icesheet_mass_rate_of_change = 0.0  
             greenland_icesheet_timelapsed = 0.0 
             antarctica_icesheet_mass = 0.0  
             antarctica_icesheet_mass_rate_of_change = 0.0  
             antarctica_icesheet_timelapsed = 0.0 
          ENDIF 
 
      ENDIF 
      !
      IF (ln_timing) CALL timing_stop('sbc_cpl_init')
Guillaume Samson's avatar
Guillaume Samson committed
      !
   END SUBROUTINE sbc_cpl_init


   SUBROUTINE sbc_cpl_rcv( kt, k_fsbc, k_ice, Kbb, Kmm )
      !!----------------------------------------------------------------------
      !!             ***  ROUTINE sbc_cpl_rcv  ***
      !!
      !! ** Purpose :   provide the stress over the ocean and, if no sea-ice,
      !!                provide the ocean heat and freshwater fluxes.
      !!
      !! ** Method  : - Receive all the atmospheric fields (stored in frcv array). called at each time step.
      !!                OASIS controls if there is something do receive or not. nrcvinfo contains the info
      !!                to know if the field was really received or not
      !!
      !!              --> If ocean stress was really received:
      !!
      !!                  - transform the received ocean stress vector from the received
      !!                 referential and grid into an atmosphere-ocean stress in
      !!                 the (i,j) ocean referencial and at the ocean velocity point.
      !!                    The received stress are :
      !!                     - defined by 3 components (if cartesian coordinate)
      !!                            or by 2 components (if spherical)
      !!                     - oriented along geographical   coordinate (if eastward-northward)
      !!                            or  along the local grid coordinate (if local grid)
      !!                     - given at U- and V-point, resp.   if received on 2 grids
      !!                            or at T-point               if received on 1 grid
      !!                    Therefore and if necessary, they are successively
      !!                  processed in order to obtain them
      !!                     first  as  2 components on the sphere
      !!                     second as  2 components oriented along the local grid
      !!                     third  as  2 components on the U,V grid
      !!
      !!              -->
      !!
      !!              - In 'ocean only' case, non solar and solar ocean heat fluxes
      !!             and total ocean freshwater fluxes
      !!
      !! ** Method  :   receive all fields from the atmosphere and transform
      !!              them into ocean surface boundary condition fields
      !!
      !! ** Action  :   update  utau, vtau   ocean stress at U,V grid
      !!                        taum         wind stress module at T-point
      !!                        wndm         wind speed  module at T-point over free ocean or leads in presence of sea-ice
      !!                        qns          non solar heat fluxes including emp heat content    (ocean only case)
      !!                                     and the latent heat flux of solid precip. melting
      !!                        qsr          solar ocean heat fluxes   (ocean only case)
      !!                        emp          upward mass flux [evap. - precip. (- runoffs) (- calving)] (ocean only case)
      !!----------------------------------------------------------------------
      USE zdf_oce,  ONLY :   ln_zdfswm
      !
      INTEGER, INTENT(in) ::   kt          ! ocean model time step index
      INTEGER, INTENT(in) ::   k_fsbc      ! frequency of sbc (-> ice model) computation
      INTEGER, INTENT(in) ::   k_ice       ! ice management in the sbc (=0/1/2/3)
      INTEGER, INTENT(in) ::   Kbb, Kmm    ! ocean model time level indices
      !!
      LOGICAL  ::   llnewtx, llnewtau      ! update wind stress components and module??
      INTEGER  ::   ji, jj, jn             ! dummy loop indices
      INTEGER  ::   isec                   ! number of seconds since nit000 (assuming rdt did not change since nit000)
      INTEGER  ::   ikchoix
      REAL(wp), DIMENSION(jpi,jpj) ::   ztx2, zty2
Guillaume Samson's avatar
Guillaume Samson committed
      REAL(wp) ::   zcumulneg, zcumulpos   ! temporary scalars
      REAL(wp) ::   zcoef                  ! temporary scalar
      LOGICAL  ::   ll_wrtstp              ! write diagnostics?
Guillaume Samson's avatar
Guillaume Samson committed
      REAL(wp) ::   zrhoa  = 1.22          ! Air density kg/m3
      REAL(wp) ::   zcdrag = 1.5e-3        ! drag coefficient
      REAL(wp) ::   zgreenland_icesheet_mass_in, zantarctica_icesheet_mass_in 
      REAL(wp) ::   zgreenland_icesheet_mass_b, zantarctica_icesheet_mass_b 
      REAL(wp) ::   zmask_sum, zepsilon    
Guillaume Samson's avatar
Guillaume Samson committed
      REAL(wp) ::   zzx, zzy               ! temporary variables
      REAL(wp) ::   r1_grau                ! = 1.e0 / (grav * rho0)
Guillaume Samson's avatar
Guillaume Samson committed
      REAL(wp), DIMENSION(jpi,jpj) ::   ztx, zty, zmsk, zemp, zqns, zqsr, zcloud_fra
      !!----------------------------------------------------------------------
      !
      !
      IF (ln_timing) CALL timing_start('sbc_cpl_rcv')
      !
      ll_wrtstp  = ( MOD( kt, sn_cfctl%ptimincr ) == 0 ) .OR. ( kt == nitend )
            !
Guillaume Samson's avatar
Guillaume Samson committed
      IF( kt == nit000 ) THEN
      !   cannot be done in the init phase when we use agrif as cpl_freq requires that oasis_enddef is done
         ncpl_qsr_freq = cpl_freq( 'O_QsrOce' ) + cpl_freq( 'O_QsrMix' ) + cpl_freq( 'I_QsrOce' ) + cpl_freq( 'I_QsrMix' )
         IF( ln_dm2dc .AND. ncpl_qsr_freq /= 86400 )   &
            &   CALL ctl_stop( 'sbc_cpl_rcv: diurnal cycle reconstruction (ln_dm2dc) needs daily couping for solar radiation' )

         IF ( ln_wave .AND. nn_components == 0 ) THEN
            ncpl_qsr_freq = 1;
            WRITE(numout,*) 'ncpl_qsr_freq is set to 1 when coupling NEMO with wave (without SAS) '
         ENDIF
      ENDIF
      !
      IF( ln_mixcpl )   zmsk(:,:) = 1. - xcplmask(:,:,0)
      !
      !                                                      ! ======================================================= !
      !                                                      ! Receive all the atmos. fields (including ice information)
      !                                                      ! ======================================================= !
      isec = ( kt - nit000 ) * NINT( rn_Dt )                      ! date of exchanges
      DO jn = 1, jprcv                                          ! received fields sent by the atmosphere
        IF( srcv(jn)%laction ) THEN  
 
          IF ( srcv(jn)%dimensions <= 1 ) THEN 
            CALL cpl_rcv_1d( jn, isec, frcv(jn)%z3, SIZE(frcv(jn)%z3), nrcvinfo(jn) ) 
          ELSE 

            CALL cpl_rcv( jn, isec, frcv(jn)%z3, xcplmask(:,:,1:nn_cplmodel), nrcvinfo(jn) ) 
          END IF 

        END IF 
Guillaume Samson's avatar
Guillaume Samson committed
      END DO

      !                                                      ! ========================= !
      IF( srcv(jpr_otx1)%laction ) THEN                      !  ocean stress components  !
         !                                                   ! ========================= !
         ! define frcv(jpr_otx1)%z3(:,:,1) and frcv(jpr_oty1)%z3(:,:,1): stress at U/V point along model grid
         ! => need to be done only when we receive the field
         IF(  nrcvinfo(jpr_otx1) == OASIS_Rcv ) THEN
            !
            IF( TRIM( sn_rcv_tau%clvref ) == 'cartesian' ) THEN            ! 2 components on the sphere
               !                                                       ! (cartesian to spherical -> 3 to 2 components)
               !
               CALL geo2oce( frcv(jpr_otx1)%z3(:,:,1), frcv(jpr_oty1)%z3(:,:,1), frcv(jpr_otz1)%z3(:,:,1),   &
                  &          srcv(jpr_otx1)%clgrid, ztx, zty )
               frcv(jpr_otx1)%z3(:,:,1) = ztx(:,:)   ! overwrite 1st comp. on the 1st grid
               frcv(jpr_oty1)%z3(:,:,1) = zty(:,:)   ! overwrite 2nd comp. on the 1st grid
               !
               IF( srcv(jpr_otx2)%laction ) THEN
                  CALL geo2oce( frcv(jpr_otx2)%z3(:,:,1), frcv(jpr_oty2)%z3(:,:,1), frcv(jpr_otz2)%z3(:,:,1),   &
                     &          srcv(jpr_otx2)%clgrid, ztx, zty )
                  frcv(jpr_otx2)%z3(:,:,1) = ztx(:,:)   ! overwrite 1st comp. on the 2nd grid
                  frcv(jpr_oty2)%z3(:,:,1) = zty(:,:)   ! overwrite 2nd comp. on the 2nd grid
               ENDIF
               !
            ENDIF
            !
            IF( TRIM( sn_rcv_tau%clvor ) == 'eastward-northward' ) THEN   ! 2 components oriented along the local grid
               !                                                       ! (geographical to local grid -> rotate the components)
               IF( srcv(jpr_otx1)%clgrid == 'U' .AND. (.NOT. srcv(jpr_otx2)%laction) ) THEN
                  ! Temporary code for HadGEM3 - will be removed eventually.
                  ! Only applies when we have only taux on U grid and tauy on V grid

                  !RSRH these MUST be initialised because the halos are not explicitly set 
                  ! but they're passed to repcmo and used directly in calculations, so if 
                  ! they point at junk in memory then bad things will happen!
                  ! (You can prove this by running with preset NaNs). 
                  ztx(:,:)=0.0
                  zty(:,:)=0.0

                  DO_2D( 0, 0, 0, 0 )                                       
                          ztx(ji,jj)=0.25*vmask(ji,jj,1)                &
                             *(frcv(jpr_otx1)%z3(ji,jj,1)+frcv(jpr_otx1)%z3(ji-1,jj,1)    &
                             +frcv(jpr_otx1)%z3(ji,jj+1,1)+frcv(jpr_otx1)%z3(ji-1,jj+1,1))
                          zty(ji,jj)=0.25*umask(ji,jj,1)                &
                             *(frcv(jpr_oty1)%z3(ji,jj,1)+frcv(jpr_oty1)%z3(ji+1,jj,1)    &
                             +frcv(jpr_oty1)%z3(ji,jj-1,1)+frcv(jpr_oty1)%z3(ji+1,jj-1,1))
                  END_2D
	                       
                  ikchoix = 1
                  CALL repcmo (frcv(jpr_otx1)%z3(:,:,1),zty,ztx,frcv(jpr_oty1)%z3(:,:,1),ztx2,zty2,ikchoix)
                  CALL lbc_lnk ('jpr_otx1', ztx2,'U', -1. )
                  CALL lbc_lnk ('jpr_oty1', zty2,'V', -1. )
                  frcv(jpr_otx1)%z3(:,:,1)=ztx2(:,:)
                  frcv(jpr_oty1)%z3(:,:,1)=zty2(:,:)
Guillaume Samson's avatar
Guillaume Samson committed
               ELSE
                  CALL rot_rep( frcv(jpr_otx1)%z3(:,:,1), frcv(jpr_oty1)%z3(:,:,1), srcv(jpr_otx1)%clgrid, 'en->i', ztx )   
                  frcv(jpr_otx1)%z3(:,:,1) = ztx(:,:)      ! overwrite 1st component on the 1st grid
                  IF( srcv(jpr_otx2)%laction ) THEN
                     CALL rot_rep( frcv(jpr_otx2)%z3(:,:,1), frcv(jpr_oty2)%z3(:,:,1), srcv(jpr_otx2)%clgrid, 'en->j', zty )   
                  ELSE
                     CALL rot_rep( frcv(jpr_otx1)%z3(:,:,1), frcv(jpr_oty1)%z3(:,:,1), srcv(jpr_otx1)%clgrid, 'en->j', zty )
                  ENDIF
                  frcv(jpr_oty1)%z3(:,:,1) = zty(:,:)      ! overwrite 2nd component on the 2nd grid 
Guillaume Samson's avatar
Guillaume Samson committed
               ENDIF
            ENDIF
            !
            IF( srcv(jpr_otx1)%clgrid == 'T' ) THEN
               DO_2D( 0, 0, 0, 0 )                                        ! T ==> (U,V)
                  frcv(jpr_otx1)%z3(ji,jj,1) = 0.5 * ( frcv(jpr_otx1)%z3(ji+1,jj  ,1) + frcv(jpr_otx1)%z3(ji,jj,1) )
                  frcv(jpr_oty1)%z3(ji,jj,1) = 0.5 * ( frcv(jpr_oty1)%z3(ji  ,jj+1,1) + frcv(jpr_oty1)%z3(ji,jj,1) )
               END_2D
               CALL lbc_lnk( 'sbccpl', frcv(jpr_otx1)%z3(:,:,1), 'U',  -1.0_wp, frcv(jpr_oty1)%z3(:,:,1), 'V',  -1.0_wp )
            ENDIF
            llnewtx = .TRUE.
         ELSE
            llnewtx = .FALSE.
         ENDIF
         !                                                   ! ========================= !
      ELSE                                                   !   No dynamical coupling   !
         !                                                   ! ========================= !
         frcv(jpr_otx1)%z3(:,:,1) = 0.e0                               ! here simply set to zero
         frcv(jpr_oty1)%z3(:,:,1) = 0.e0                               ! an external read in a file can be added instead
         llnewtx = .TRUE.
         !
      ENDIF
      !                                                      ! ========================= !
      !                                                      !    wind stress module     !   (taum)
      !                                                      ! ========================= !
      IF( .NOT. srcv(jpr_taum)%laction ) THEN                    ! compute wind stress module from its components if not received
         ! => need to be done only when otx1 was changed
         IF( llnewtx ) THEN
            DO_2D( 0, 0, 0, 0 )
               zzx = frcv(jpr_otx1)%z3(ji-1,jj  ,1) + frcv(jpr_otx1)%z3(ji,jj,1)
               zzy = frcv(jpr_oty1)%z3(ji  ,jj-1,1) + frcv(jpr_oty1)%z3(ji,jj,1)
               frcv(jpr_taum)%z3(ji,jj,1) = 0.5 * SQRT( zzx * zzx + zzy * zzy )
            END_2D
            CALL lbc_lnk( 'sbccpl', frcv(jpr_taum)%z3(:,:,1), 'T', 1.0_wp )
            llnewtau = .TRUE.
         ELSE
            llnewtau = .FALSE.
         ENDIF
      ELSE
         llnewtau = nrcvinfo(jpr_taum) == OASIS_Rcv
         ! Stress module can be negative when received (interpolation problem)
         IF( llnewtau ) THEN
            frcv(jpr_taum)%z3(:,:,1) = MAX( 0._wp, frcv(jpr_taum)%z3(:,:,1) )
         ENDIF
      ENDIF
      !
      !                                                      ! ========================= !
      !                                                      !      10 m wind speed      !   (wndm)
      !                                                      ! ========================= !
      IF( .NOT. srcv(jpr_w10m)%laction ) THEN                    ! compute wind spreed from wind stress module if not received
         ! => need to be done only when taumod was changed
         IF( llnewtau ) THEN
            zcoef = 1. / ( zrhoa * zcdrag )
            DO_2D( nn_hls, nn_hls, nn_hls, nn_hls )
               frcv(jpr_w10m)%z3(ji,jj,1) = SQRT( frcv(jpr_taum)%z3(ji,jj,1) * zcoef )
            END_2D
         ENDIF
      ENDIF
!!$      !                                                      ! ========================= !
!!$      SELECT CASE( TRIM( sn_rcv_clouds%cldes ) )             !       cloud fraction      !
!!$      !                                                      ! ========================= !
!!$      cloud_fra(:,:) = frcv(jpr_clfra)*z3(:,:,1)
!!$      END SELECT
!!$
      zcloud_fra(:,:) = pp_cldf   ! should be real cloud fraction instead (as in the bulk) but needs to be read from atm.
      IF( ln_mixcpl ) THEN
         cloud_fra(:,:) = cloud_fra(:,:) * xcplmask(:,:,0) + zcloud_fra(:,:)* zmsk(:,:)
      ELSE
         cloud_fra(:,:) = zcloud_fra(:,:)
      ENDIF
      !                                                      ! ========================= !
      ! u(v)tau and taum will be modified by ice model
      ! -> need to be reset before each call of the ice/fsbc
      IF( MOD( kt-1, k_fsbc ) == 0 ) THEN
         !
         IF( ln_mixcpl ) THEN
            utau(:,:) = utau(:,:) * xcplmask(:,:,0) + frcv(jpr_otx1)%z3(:,:,1) * zmsk(:,:)
            vtau(:,:) = vtau(:,:) * xcplmask(:,:,0) + frcv(jpr_oty1)%z3(:,:,1) * zmsk(:,:)
            taum(:,:) = taum(:,:) * xcplmask(:,:,0) + frcv(jpr_taum)%z3(:,:,1) * zmsk(:,:)
            wndm(:,:) = wndm(:,:) * xcplmask(:,:,0) + frcv(jpr_w10m)%z3(:,:,1) * zmsk(:,:)
         ELSE
            utau(:,:) = frcv(jpr_otx1)%z3(:,:,1)
            vtau(:,:) = frcv(jpr_oty1)%z3(:,:,1)
            taum(:,:) = frcv(jpr_taum)%z3(:,:,1)
            wndm(:,:) = frcv(jpr_w10m)%z3(:,:,1)
         ENDIF
         CALL iom_put( "taum_oce", taum )   ! output wind stress module
         !
      ENDIF

#if defined key_medusa
      IF (ln_medusa) THEN
        IF( srcv(jpr_atm_pco2)%laction) PCO2a_in_cpl(:,:) = frcv(jpr_atm_pco2)%z3(:,:,1)
        IF( srcv(jpr_atm_dust)%laction) Dust_in_cpl(:,:) = frcv(jpr_atm_dust)%z3(:,:,1)
      ENDIF
#endif
Guillaume Samson's avatar
Guillaume Samson committed
      !                                                      ! ================== !
      !                                                      ! atmosph. CO2 (ppm) !
      !                                                      ! ================== !
      IF( srcv(jpr_co2)%laction )   atm_co2(:,:) = frcv(jpr_co2)%z3(:,:,1)
      !
      !                                                      ! ========================= !
      !                                                      ! Mean Sea Level Pressure   !   (taum)
      !                                                      ! ========================= !
      IF( srcv(jpr_mslp)%laction ) THEN                    ! UKMO SHELF effect of atmospheric pressure on SSH
          IF( kt /= nit000 )   ssh_ibb(:,:) = ssh_ib(:,:)    !* Swap of ssh_ib fields

          r1_grau = 1.e0 / (grav * rho0)               !* constant for optimization
          ssh_ib(:,:) = - ( frcv(jpr_mslp)%z3(:,:,1) - rpref ) * r1_grau    ! equivalent ssh (inverse barometer)
          apr   (:,:) =     frcv(jpr_mslp)%z3(:,:,1)                         !atmospheric pressure

          IF( kt == nit000 ) ssh_ibb(:,:) = ssh_ib(:,:)  ! correct this later (read from restart if possible)
      ENDIF
      !
      IF( ln_sdw ) THEN  ! Stokes Drift correction activated
      !                                                      ! ========================= !
      !                                                      !       Stokes drift u      !
      !                                                      ! ========================= !
         IF( srcv(jpr_sdrftx)%laction ) ut0sd(:,:) = frcv(jpr_sdrftx)%z3(:,:,1)
      !
      !                                                      ! ========================= !
      !                                                      !       Stokes drift v      !
      !                                                      ! ========================= !
         IF( srcv(jpr_sdrfty)%laction ) vt0sd(:,:) = frcv(jpr_sdrfty)%z3(:,:,1)
      !
      !                                                      ! ========================= !
      !                                                      !      Wave mean period     !
      !                                                      ! ========================= !
         IF( srcv(jpr_wper)%laction ) wmp(:,:) = frcv(jpr_wper)%z3(:,:,1)
      !
      !                                                      ! ========================= !
      !                                                      !  Significant wave height  !
      !                                                      ! ========================= !
         IF( srcv(jpr_hsig)%laction ) hsw(:,:) = frcv(jpr_hsig)%z3(:,:,1)
      !
      !                                                      ! ========================= !
      !                                                      !    Vertical mixing Qiao   !
      !                                                      ! ========================= !
         IF( srcv(jpr_wnum)%laction .AND. ln_zdfswm ) wnum(:,:) = frcv(jpr_wnum)%z3(:,:,1)

         ! Calculate the 3D Stokes drift both in coupled and not fully uncoupled mode
         IF( srcv(jpr_sdrftx)%laction .OR. srcv(jpr_sdrfty)%laction .OR. &
             srcv(jpr_wper)%laction .OR. srcv(jpr_hsig)%laction )   THEN
            CALL sbc_stokes( Kmm )
         ENDIF
      ENDIF
      !                                                      ! ========================= !
      !                                                      ! Stress adsorbed by waves  !
      !                                                      ! ========================= !
      IF( srcv(jpr_wstrf)%laction .AND. ln_tauoc )  tauoc_wave(:,:) = frcv(jpr_wstrf)%z3(:,:,1)
      !
      !                                                      ! ========================= !
      !                                                      !   Wave drag coefficient   !
      !                                                      ! ========================= !
      IF( srcv(jpr_wdrag)%laction .AND. ln_cdgw )   cdn_wave(:,:) = frcv(jpr_wdrag)%z3(:,:,1)
      !
      !                                                      ! ========================= !
      !                                                      !   Chranock coefficient    !
      !                                                      ! ========================= !
      IF( srcv(jpr_charn)%laction .AND. ln_charn )  charn(:,:) = frcv(jpr_charn)%z3(:,:,1)
      !
      !                                                      ! ========================= !
      !                                                      ! net wave-supported stress !
      !                                                      ! ========================= !
      IF( srcv(jpr_tawx)%laction .AND. ln_taw )     tawx(:,:) = frcv(jpr_tawx)%z3(:,:,1)
      IF( srcv(jpr_tawy)%laction .AND. ln_taw )     tawy(:,:) = frcv(jpr_tawy)%z3(:,:,1)
      !
      !                                                      ! ========================= !
      !                                                      !wave to ocean momentum flux!
      !                                                      ! ========================= !
      IF( srcv(jpr_twox)%laction .AND. ln_taw )     twox(:,:) = frcv(jpr_twox)%z3(:,:,1)
      IF( srcv(jpr_twoy)%laction .AND. ln_taw )     twoy(:,:) = frcv(jpr_twoy)%z3(:,:,1)
      !
      !                                                      ! ========================= !
      !                                                      !    wave TKE flux at sfc   !
      !                                                      ! ========================= !
      IF( srcv(jpr_phioc)%laction .AND. ln_phioc )     phioc(:,:) = frcv(jpr_phioc)%z3(:,:,1)
      !
      !                                                      ! ========================= !
      !                                                      !      Bernoulli head       !
      !                                                      ! ========================= !
      IF( srcv(jpr_bhd)%laction .AND. ln_bern_srfc )   bhd_wave(:,:) = frcv(jpr_bhd)%z3(:,:,1)
      !
      !                                                      ! ========================= !
      !                                                      !   Stokes transport u dir  !
      !                                                      ! ========================= !
      IF( srcv(jpr_tusd)%laction .AND. ln_breivikFV_2016 )    tusd(:,:) = frcv(jpr_tusd)%z3(:,:,1)
      !
      !                                                      ! ========================= !
      !                                                      !   Stokes transport v dir  !
      !                                                      ! ========================= !
      IF( srcv(jpr_tvsd)%laction .AND. ln_breivikFV_2016 )     tvsd(:,:) = frcv(jpr_tvsd)%z3(:,:,1)
      !
      !  Fields received by SAS when OASIS coupling
      !  (arrays no more filled at sbcssm stage)
      !                                                      ! ================== !
      !                                                      !        SSS         !
      !                                                      ! ================== !
      IF( srcv(jpr_soce)%laction ) THEN                      ! received by sas in case of opa <-> sas coupling
         sss_m(:,:) = frcv(jpr_soce)%z3(:,:,1)
         CALL iom_put( 'sss_m', sss_m )
      ENDIF
      !
      !                                                      ! ================== !
      !                                                      !        SST         !
      !                                                      ! ================== !
      IF( srcv(jpr_toce)%laction ) THEN                      ! received by sas in case of opa <-> sas coupling
         sst_m(:,:) = frcv(jpr_toce)%z3(:,:,1)
         IF( srcv(jpr_soce)%laction .AND. l_useCT ) THEN    ! make sure that sst_m is the potential temperature
            sst_m(:,:) = eos_pt_from_ct( sst_m(:,:), sss_m(:,:) )
         ENDIF
      ENDIF
      !                                                      ! ================== !
      !                                                      !        SSH         !
      !                                                      ! ================== !
      IF( srcv(jpr_ssh )%laction ) THEN                      ! received by sas in case of opa <-> sas coupling
         ssh_m(:,:) = frcv(jpr_ssh )%z3(:,:,1)
         CALL iom_put( 'ssh_m', ssh_m )
      ENDIF
      !                                                      ! ================== !
      !                                                      !  surface currents  !
      !                                                      ! ================== !
      IF( srcv(jpr_ocx1)%laction ) THEN                      ! received by sas in case of opa <-> sas coupling
         ssu_m(:,:) = frcv(jpr_ocx1)%z3(:,:,1)
         uu(:,:,1,Kbb) = ssu_m(:,:)                          ! will be used in icestp in the call of ice_forcing_tau
         uu(:,:,1,Kmm) = ssu_m(:,:)                          ! will be used in sbc_cpl_snd if atmosphere coupling
         CALL iom_put( 'ssu_m', ssu_m )
      ENDIF
      IF( srcv(jpr_ocy1)%laction ) THEN
         ssv_m(:,:) = frcv(jpr_ocy1)%z3(:,:,1)
         vv(:,:,1,Kbb) = ssv_m(:,:)                          ! will be used in icestp in the call of ice_forcing_tau
         vv(:,:,1,Kmm) = ssv_m(:,:)                          ! will be used in sbc_cpl_snd if atmosphere coupling
         CALL iom_put( 'ssv_m', ssv_m )
      ENDIF
      !                                                      ! ======================== !
      !                                                      !  first T level thickness !
      !                                                      ! ======================== !
      IF( srcv(jpr_e3t1st )%laction ) THEN                   ! received by sas in case of opa <-> sas coupling
         e3t_m(:,:) = frcv(jpr_e3t1st )%z3(:,:,1)
         CALL iom_put( 'e3t_m', e3t_m(:,:) )
      ENDIF
      !                                                      ! ================================ !
      !                                                      !  fraction of solar net radiation !
      !                                                      ! ================================ !
      IF( srcv(jpr_fraqsr)%laction ) THEN                    ! received by sas in case of opa <-> sas coupling
         frq_m(:,:) = frcv(jpr_fraqsr)%z3(:,:,1)
         CALL iom_put( 'frq_m', frq_m )
      ENDIF

      !                                                      ! ========================= !
      IF( k_ice <= 1 .AND. MOD( kt-1, k_fsbc ) == 0 ) THEN   !  heat & freshwater fluxes ! (Ocean only case)
         !                                                   ! ========================= !
         !
         !                                                       ! total freshwater fluxes over the ocean (emp)
         IF( srcv(jpr_oemp)%laction .OR. srcv(jpr_rain)%laction ) THEN
            SELECT CASE( TRIM( sn_rcv_emp%cldes ) )                                    ! evaporation - precipitation
            CASE( 'conservative' )
               zemp(:,:) = frcv(jpr_tevp)%z3(:,:,1) - ( frcv(jpr_rain)%z3(:,:,1) + frcv(jpr_snow)%z3(:,:,1) )
            CASE( 'oce only', 'oce and ice' )
               zemp(:,:) = frcv(jpr_oemp)%z3(:,:,1)
            CASE default
               CALL ctl_stop( 'sbc_cpl_rcv: wrong definition of sn_rcv_emp%cldes' )
            END SELECT
         ELSE
            zemp(:,:) = 0._wp
         ENDIF
         !
         !                                                        ! runoffs and calving (added in emp)
         IF( srcv(jpr_rnf)%laction )     rnf(:,:) = frcv(jpr_rnf)%z3(:,:,1)
         IF( srcv(jpr_cal)%laction )     zemp(:,:) = zemp(:,:) - frcv(jpr_cal)%z3(:,:,1)

         IF( srcv(jpr_icb)%laction )  THEN
             fwficb(:,:) = frcv(jpr_icb)%z3(:,:,1)
             rnf(:,:)    = rnf(:,:) + fwficb(:,:)   ! iceberg added to runfofs
         ENDIF
         !
         ! ice shelf fwf
         IF( srcv(jpr_isf)%laction )  THEN
            fwfisf_oasis(:,:) = frcv(jpr_isf)%z3(:,:,1)  ! fresh water flux from the isf to the ocean ( > 0 = melting )
         END IF

         IF( ln_mixcpl ) THEN   ;   emp(:,:) = emp(:,:) * xcplmask(:,:,0) + zemp(:,:) * zmsk(:,:)
         ELSE                   ;   emp(:,:) =                              zemp(:,:)
         ENDIF
         !
         !                                                       ! non solar heat flux over the ocean (qns)
         IF(      srcv(jpr_qnsoce)%laction ) THEN   ;   zqns(:,:) = frcv(jpr_qnsoce)%z3(:,:,1)
         ELSE IF( srcv(jpr_qnsmix)%laction ) THEN   ;   zqns(:,:) = frcv(jpr_qnsmix)%z3(:,:,1)
         ELSE                                       ;   zqns(:,:) = 0._wp
         ENDIF
         ! update qns over the free ocean with:
         IF( nn_components /= jp_iam_oce ) THEN
            zqns(:,:) =  zqns(:,:) - zemp(:,:) * sst_m(:,:) * rcp         ! remove heat content due to mass flux (assumed to be at SST)
            IF( srcv(jpr_snow  )%laction ) THEN
               zqns(:,:) = zqns(:,:) - frcv(jpr_snow)%z3(:,:,1) * rLfus   ! energy for melting solid precipitation over the free ocean
            ENDIF
         ENDIF
         !
         IF( srcv(jpr_icb)%laction )  zqns(:,:) = zqns(:,:) - frcv(jpr_icb)%z3(:,:,1) * rLfus ! remove heat content associated to iceberg melting
         !
         IF( ln_mixcpl ) THEN   
            qns(:,:) = qns(:,:) * xcplmask(:,:,0) + zqns(:,:) * zmsk(:,:)
         ELSE                   
            qns(:,:) =                              zqns(:,:)
Guillaume Samson's avatar
Guillaume Samson committed
         ENDIF

         !                                                       ! solar flux over the ocean          (qsr)
         IF     ( srcv(jpr_qsroce)%laction ) THEN   ;   zqsr(:,:) = frcv(jpr_qsroce)%z3(:,:,1)
         ELSE IF( srcv(jpr_qsrmix)%laction ) then   ;   zqsr(:,:) = frcv(jpr_qsrmix)%z3(:,:,1)
         ELSE                                       ;   zqsr(:,:) = 0._wp
         ENDIF
         IF( ln_dm2dc .AND. ln_cpl )   zqsr(:,:) = sbc_dcy( zqsr )   ! modify qsr to include the diurnal cycle
         IF( ln_mixcpl ) THEN   ;   qsr(:,:) = qsr(:,:) * xcplmask(:,:,0) + zqsr(:,:) * zmsk(:,:)
         ELSE                   ;   qsr(:,:) =                              zqsr(:,:)
         ENDIF
         !
         ! salt flux over the ocean (received by opa in case of opa <-> sas coupling)
         IF( srcv(jpr_sflx )%laction )   sfx(:,:) = frcv(jpr_sflx  )%z3(:,:,1)
         ! Ice cover  (received by opa in case of opa <-> sas coupling)
         IF( srcv(jpr_fice )%laction )   fr_i(:,:) = frcv(jpr_fice )%z3(:,:,1)
         !
      ENDIF

      zepsilon = rn_iceshelf_fluxes_tolerance

      IF( srcv(jpr_grnm)%laction .AND. nn_coupled_iceshelf_fluxes == 1 ) THEN
     
         ! This is a zero dimensional, single value field.
         zgreenland_icesheet_mass_in =  frcv(jpr_grnm)%z3(1,1,1)
           
         greenland_icesheet_timelapsed = greenland_icesheet_timelapsed + rdt         
         IF( ln_iceshelf_init_atmos .AND. kt == 1 ) THEN
            ! On the first timestep (of an NRUN) force the ocean to ignore the icesheet masses in the ocean restart
            ! and take them from the atmosphere to avoid problems with using inconsistent ocean and atmosphere restarts.
            zgreenland_icesheet_mass_b = zgreenland_icesheet_mass_in
            greenland_icesheet_mass = zgreenland_icesheet_mass_in
         ENDIF
         IF( ABS( zgreenland_icesheet_mass_in - greenland_icesheet_mass ) > zepsilon ) THEN
            zgreenland_icesheet_mass_b = greenland_icesheet_mass
           
            ! Only update the mass if it has increased.
            IF ( (zgreenland_icesheet_mass_in - greenland_icesheet_mass) > 0.0 ) THEN
               greenland_icesheet_mass = zgreenland_icesheet_mass_in
            ENDIF
           
            IF( zgreenland_icesheet_mass_b /= 0.0 ) &
           &     greenland_icesheet_mass_rate_of_change = ( greenland_icesheet_mass - zgreenland_icesheet_mass_b ) / greenland_icesheet_timelapsed 
            greenland_icesheet_timelapsed = 0.0_wp       
         ENDIF
         IF(lwp .AND. ll_wrtstp) THEN
            WRITE(numout,*) 'Greenland icesheet mass (kg) read in is ', zgreenland_icesheet_mass_in
            WRITE(numout,*) 'Greenland icesheet mass (kg) used is    ', greenland_icesheet_mass
            WRITE(numout,*) 'Greenland icesheet mass rate of change (kg/s) is ', greenland_icesheet_mass_rate_of_change
            WRITE(numout,*) 'Greenland icesheet seconds lapsed since last change is ', greenland_icesheet_timelapsed
         ENDIF
      ELSE IF ( nn_coupled_iceshelf_fluxes == 2 ) THEN
         greenland_icesheet_mass_rate_of_change = rn_greenland_total_fw_flux
      ENDIF
      !                                                        ! land ice masses : Antarctica
      IF( srcv(jpr_antm)%laction .AND. nn_coupled_iceshelf_fluxes == 1 ) THEN
         
         ! This is a zero dimensional, single value field.
         zantarctica_icesheet_mass_in = frcv(jpr_antm)%z3(1,1,1)
           
         antarctica_icesheet_timelapsed = antarctica_icesheet_timelapsed + rdt         
         IF( ln_iceshelf_init_atmos .AND. kt == 1 ) THEN
            ! On the first timestep (of an NRUN) force the ocean to ignore the icesheet masses in the ocean restart
            ! and take them from the atmosphere to avoid problems with using inconsistent ocean and atmosphere restarts.
            zantarctica_icesheet_mass_b = zantarctica_icesheet_mass_in
            antarctica_icesheet_mass = zantarctica_icesheet_mass_in
         ENDIF
         IF( ABS( zantarctica_icesheet_mass_in - antarctica_icesheet_mass ) > zepsilon ) THEN
            zantarctica_icesheet_mass_b = antarctica_icesheet_mass
           
            ! Only update the mass if it has increased.
            IF ( (zantarctica_icesheet_mass_in - antarctica_icesheet_mass) > 0.0 ) THEN
               antarctica_icesheet_mass = zantarctica_icesheet_mass_in
            END IF
           
            IF( zantarctica_icesheet_mass_b /= 0.0 ) &
          &      antarctica_icesheet_mass_rate_of_change = ( antarctica_icesheet_mass - zantarctica_icesheet_mass_b ) / antarctica_icesheet_timelapsed 
            antarctica_icesheet_timelapsed = 0.0_wp       
         ENDIF
         IF(lwp .AND. ll_wrtstp) THEN
            WRITE(numout,*) 'Antarctica icesheet mass (kg) read in is ', zantarctica_icesheet_mass_in
            WRITE(numout,*) 'Antarctica icesheet mass (kg) used is    ', antarctica_icesheet_mass
            WRITE(numout,*) 'Antarctica icesheet mass rate of change (kg/s) is ', antarctica_icesheet_mass_rate_of_change
            WRITE(numout,*) 'Antarctica icesheet seconds lapsed since last change is ', antarctica_icesheet_timelapsed
         ENDIF
      ELSE IF ( nn_coupled_iceshelf_fluxes == 2 ) THEN
         antarctica_icesheet_mass_rate_of_change = rn_antarctica_total_fw_flux
      ENDIF
Guillaume Samson's avatar
Guillaume Samson committed
      !
      IF (ln_timing) CALL timing_stop('sbc_cpl_rcv')
Guillaume Samson's avatar
Guillaume Samson committed
   END SUBROUTINE sbc_cpl_rcv


   SUBROUTINE sbc_cpl_ice_tau( p_taui, p_tauj )
      !!----------------------------------------------------------------------
      !!             ***  ROUTINE sbc_cpl_ice_tau  ***
      !!
      !! ** Purpose :   provide the stress over sea-ice in coupled mode
      !!
      !! ** Method  :   transform the received stress from the atmosphere into
      !!             an atmosphere-ice stress in the (i,j) ocean referencial
      !!             and at the velocity point of the sea-ice model:
      !!                'C'-grid : i- (j-) components given at U- (V-) point
      !!
      !!                The received stress are :
      !!                 - defined by 3 components (if cartesian coordinate)
      !!                        or by 2 components (if spherical)
      !!                 - oriented along geographical   coordinate (if eastward-northward)
      !!                        or  along the local grid coordinate (if local grid)
      !!                 - given at U- and V-point, resp.   if received on 2 grids
      !!                        or at a same point (T or I) if received on 1 grid
      !!                Therefore and if necessary, they are successively
      !!             processed in order to obtain them
      !!                 first  as  2 components on the sphere
      !!                 second as  2 components oriented along the local grid
      !!                 third  as  2 components on the ice grid point
      !!
      !!                Except in 'oce and ice' case, only one vector stress field
      !!             is received. It has already been processed in sbc_cpl_rcv
      !!             so that it is now defined as (i,j) components given at U-
      !!             and V-points, respectively.
      !!
      !! ** Action  :   return ptau_i, ptau_j, the stress over the ice
      !!----------------------------------------------------------------------
      REAL(wp), INTENT(inout), DIMENSION(:,:) ::   p_taui   ! i- & j-components of atmos-ice stress [N/m2]
      REAL(wp), INTENT(inout), DIMENSION(:,:) ::   p_tauj   ! at I-point (B-grid) or U & V-point (C-grid)
      !!
      INTEGER ::   ji, jj   ! dummy loop indices
      INTEGER ::   itx      ! index of taux over ice
      REAL(wp)                     ::   zztmp1, zztmp2
      REAL(wp), DIMENSION(jpi,jpj) ::   ztx, zty
      !!----------------------------------------------------------------------
      !
#if defined key_si3 || defined key_cice
      !
      IF( srcv(jpr_itx1)%laction ) THEN   ;   itx =  jpr_itx1
      ELSE                                ;   itx =  jpr_otx1
      ENDIF

      ! do something only if we just received the stress from atmosphere
      IF(  nrcvinfo(itx) == OASIS_Rcv ) THEN
         !                                                      ! ======================= !
         IF( srcv(jpr_itx1)%laction ) THEN                      !   ice stress received   !
            !                                                   ! ======================= !
            !
            IF( TRIM( sn_rcv_tau%clvref ) == 'cartesian' ) THEN            ! 2 components on the sphere
               !                                                       ! (cartesian to spherical -> 3 to 2 components)
               CALL geo2oce(  frcv(jpr_itx1)%z3(:,:,1), frcv(jpr_ity1)%z3(:,:,1), frcv(jpr_itz1)%z3(:,:,1),   &
                  &          srcv(jpr_itx1)%clgrid, ztx, zty )
               frcv(jpr_itx1)%z3(:,:,1) = ztx(:,:)   ! overwrite 1st comp. on the 1st grid
               frcv(jpr_ity1)%z3(:,:,1) = zty(:,:)   ! overwrite 2nd comp. on the 1st grid
               !
               IF( srcv(jpr_itx2)%laction ) THEN
                  CALL geo2oce( frcv(jpr_itx2)%z3(:,:,1), frcv(jpr_ity2)%z3(:,:,1), frcv(jpr_itz2)%z3(:,:,1),   &
                     &          srcv(jpr_itx2)%clgrid, ztx, zty )
                  frcv(jpr_itx2)%z3(:,:,1) = ztx(:,:)   ! overwrite 1st comp. on the 2nd grid
                  frcv(jpr_ity2)%z3(:,:,1) = zty(:,:)   ! overwrite 2nd comp. on the 2nd grid
               ENDIF
               !
            ENDIF
            !
            IF( TRIM( sn_rcv_tau%clvor ) == 'eastward-northward' ) THEN   ! 2 components oriented along the local grid
               !                                                       ! (geographical to local grid -> rotate the components)
               CALL rot_rep( frcv(jpr_itx1)%z3(:,:,1), frcv(jpr_ity1)%z3(:,:,1), srcv(jpr_itx1)%clgrid, 'en->i', ztx )
               IF( srcv(jpr_itx2)%laction ) THEN
                  CALL rot_rep( frcv(jpr_itx2)%z3(:,:,1), frcv(jpr_ity2)%z3(:,:,1), srcv(jpr_itx2)%clgrid, 'en->j', zty )
               ELSE
                  CALL rot_rep( frcv(jpr_itx1)%z3(:,:,1), frcv(jpr_ity1)%z3(:,:,1), srcv(jpr_itx1)%clgrid, 'en->j', zty )
               ENDIF
               frcv(jpr_itx1)%z3(:,:,1) = ztx(:,:)      ! overwrite 1st component on the 1st grid
               frcv(jpr_ity1)%z3(:,:,1) = zty(:,:)      ! overwrite 2nd component on the 1st grid
            ENDIF
            !                                                   ! ======================= !
         ELSE                                                   !     use ocean stress    !
            !                                                   ! ======================= !
            frcv(jpr_itx1)%z3(:,:,1) = frcv(jpr_otx1)%z3(:,:,1)
            frcv(jpr_ity1)%z3(:,:,1) = frcv(jpr_oty1)%z3(:,:,1)
            !
         ENDIF
         !                                                      ! ======================= !
         !                                                      !     put on ice grid     !
         !                                                      ! ======================= !
         !
         !                                                  j+1   j     -----V---F
         ! ice stress on ice velocity point                              !       |
         ! (C-grid ==>(U,V))                                      j      |   T   U
         !                                                               |       |
         !                                                   j    j-1   -I-------|
         !                                               (for I)         |       |
         !                                                              i-1  i   i
         !                                                               i      i+1 (for I)
         SELECT CASE ( srcv(jpr_itx1)%clgrid )
         CASE( 'U' )
            p_taui(:,:) = frcv(jpr_itx1)%z3(:,:,1)                   ! (U,V) ==> (U,V)
            p_tauj(:,:) = frcv(jpr_ity1)%z3(:,:,1)
         CASE( 'T' )
            DO_2D( 0, 0, 0, 0 )                    ! T ==> (U,V)
               ! take care of the land-sea mask to avoid "pollution" of coastal stress. p[uv]taui used in frazil and  rheology
               zztmp1 = 0.5_wp * ( 2. - umask(ji,jj,1) ) * MAX( tmask(ji,jj,1),tmask(ji+1,jj  ,1) )
               zztmp2 = 0.5_wp * ( 2. - vmask(ji,jj,1) ) * MAX( tmask(ji,jj,1),tmask(ji  ,jj+1,1) )
               p_taui(ji,jj) = zztmp1 * ( frcv(jpr_itx1)%z3(ji+1,jj  ,1) + frcv(jpr_itx1)%z3(ji,jj,1) )
               p_tauj(ji,jj) = zztmp2 * ( frcv(jpr_ity1)%z3(ji  ,jj+1,1) + frcv(jpr_ity1)%z3(ji,jj,1) )
            END_2D
            CALL lbc_lnk( 'sbccpl', p_taui, 'U',  -1._wp, p_tauj, 'V',  -1._wp )