Index: NEMO/branches/2020/r13916_ticket2377/src/OCE/SBC/sbcfwb.F90
===================================================================
--- NEMO/branches/2020/r13916_ticket2377/src/OCE/SBC/sbcfwb.F90	(revision 13953)
+++ NEMO/branches/2020/r13916_ticket2377/src/OCE/SBC/sbcfwb.F90	(revision 13954)
@@ -24,4 +24,5 @@
    !
    USE in_out_manager ! I/O manager
+   USE iom            ! IOM
    USE lib_mpp        ! distribued memory computing library
    USE timing         ! Timing
@@ -34,7 +35,7 @@
    PUBLIC   sbc_fwb    ! routine called by step
 
-   REAL(wp) ::   a_fwb_b   ! annual domain averaged freshwater budget
-   REAL(wp) ::   a_fwb     ! for 2 year before (_b) and before year.
-   REAL(wp) ::   fwfold    ! fwfold to be suppressed
+   REAL(wp) ::   rn_fwb0   ! initial freshwater adjustment flux [kg/m2/s] (nn_fwb = 2 only)
+   REAL(wp) ::   a_fwb     ! annual domain averaged freshwater budget from the
+                           ! previous year
    REAL(wp) ::   area      ! global mean ocean surface (interior domain)
 
@@ -65,5 +66,5 @@
       INTEGER, INTENT( in ) ::   Kmm      ! ocean time level index
       !
-      INTEGER  ::   inum, ikty, iyear     ! local integers
+      INTEGER  ::   ios, inum, ikty       ! local integers
       REAL(wp) ::   z_fwf, z_fwf_nsrf, zsum_fwf, zsum_erp                ! local scalars
       REAL(wp) ::   zsurf_neg, zsurf_pos, zsurf_tospread, zcoef          !   -      -
@@ -72,7 +73,14 @@
       REAL(wp)   ,DIMENSION(1) ::   z_fwfprv  
       COMPLEX(dp),DIMENSION(1) ::   y_fwfnow  
+      !
+      NAMELIST/namsbc_fwb/rn_fwb0
       !!----------------------------------------------------------------------
       !
       IF( kt == nit000 ) THEN
+         READ( numnam_ref, namsbc_fwb, IOSTAT = ios, ERR = 901 )
+901      IF( ios /= 0 ) CALL ctl_nam( ios, 'namsbc_fwb in reference namelist'     )
+         READ( numnam_cfg, namsbc_fwb, IOSTAT = ios, ERR = 902 )
+902      IF( ios >  0 ) CALL ctl_nam( ios, 'namsbc_fwb in configuration namelist' )
+         IF(lwm) WRITE( numond, namsbc_fwb )
          IF(lwp) THEN
             WRITE(numout,*)
@@ -80,6 +88,11 @@
             WRITE(numout,*) '~~~~~~~'
             IF( kn_fwb == 1 )   WRITE(numout,*) '          instantaneously set to zero'
-            IF( kn_fwb == 2 )   WRITE(numout,*) '          adjusted from previous year budget'
             IF( kn_fwb == 3 )   WRITE(numout,*) '          fwf set to zero and spread out over erp area'
+            IF( kn_fwb == 2 ) THEN
+               WRITE(numout,*) '          adjusted from previous year budget'
+               WRITE(numout,*)
+               WRITE(numout,*) '   Namelist namsbc_fwb'
+               WRITE(numout,*) '      Initial freshwater adjustment flux [kg/m2/s] = ', rn_fwb0
+            END IF
          ENDIF
          !
@@ -112,41 +125,48 @@
          ENDIF
          !
-      CASE ( 2 )                             !==  fwf budget adjusted from the previous year  ==!
-         !
-         IF( kt == nit000 ) THEN                      ! initialisation
-            !                                         ! Read the corrective factor on precipitations (fwfold)
-            CALL ctl_opn( inum, 'EMPave_old.dat', 'OLD', 'FORMATTED', 'SEQUENTIAL', -1, numout, .FALSE. )
-            READ ( inum, "(24X,I8,2ES24.16)" ) iyear, a_fwb_b, a_fwb
-            CLOSE( inum )
-            fwfold = a_fwb                            ! current year freshwater budget correction
-            !                                         ! estimate from the previous year budget
+      CASE ( 2 )                             !==  fw adjustment based on fw budget at the end of the previous year  ==!
+         !
+         IF( kt == nit000 ) THEN                                                                    ! initialisation
+            !                                                                                       ! set the fw adjustment (a_fwb)
+            IF ( ln_rstart .AND. iom_varid( numror, 'a_fwb',   ldstop = .FALSE. ) > 0 ) THEN        !    as read from restart file
+               IF(lwp) WRITE(numout,*) 'sbc_fwb : reading FW-budget adjustment from restart file'
+               CALL iom_get( numror, 'a_fwb',   a_fwb,   ldxios = lrxios )
+            ELSE                                                                                    !    as specified in namelist
+               a_fwb = rn_fwb0
+            END IF
+            !
             IF(lwp)WRITE(numout,*)
-            IF(lwp)WRITE(numout,*)'sbc_fwb : year = ',iyear  , ' freshwater budget correction = ', fwfold
-            IF(lwp)WRITE(numout,*)'          year = ',iyear-1, ' freshwater budget read       = ', a_fwb
-            IF(lwp)WRITE(numout,*)'          year = ',iyear-2, ' freshwater budget read       = ', a_fwb_b
+            IF(lwp)WRITE(numout,*)'sbc_fwb : initial freshwater-budget adjustment = ', a_fwb, 'kg/m2/s'
+            !
+            IF( lwxios ) THEN                         ! Activate output of restart variables
+               CALL iom_set_rstw_var_active( 'a_fwb'   )
+            END IF
          ENDIF   
-         !                                         ! Update fwfold if new year start
+         !                                         ! Update a_fwb if new year start
          ikty = 365 * 86400 / rn_Dt                  !!bug  use of 365 days leap year or 360d year !!!!!!!
          IF( MOD( kt, ikty ) == 0 ) THEN
-            a_fwb_b = a_fwb                           ! mean sea level taking into account the ice+snow
+                                                      ! mean sea level taking into account the ice+snow
                                                       ! sum over the global domain
             a_fwb   = glob_sum( 'sbcfwb', e1e2t(:,:) * ( ssh(:,:,Kmm) + snwice_mass(:,:) * r1_rho0 ) )
             a_fwb   = a_fwb * 1.e+3 / ( area * rday * 365. )     ! convert in Kg/m3/s = mm/s
 !!gm        !                                                      !!bug 365d year 
-            fwfold =  a_fwb                           ! current year freshwater budget correction
-            !                                         ! estimate from the previous year budget
          ENDIF
          ! 
          IF( MOD( kt-1, kn_fsbc ) == 0 ) THEN         ! correct the freshwater fluxes
-            zcoef = fwfold * rcp
-            emp(:,:) = emp(:,:) + fwfold             * tmask(:,:,1)
+            zcoef = a_fwb * rcp
+            emp(:,:) = emp(:,:) + a_fwb              * tmask(:,:,1)
             qns(:,:) = qns(:,:) - zcoef * sst_m(:,:) * tmask(:,:,1) ! account for change to the heat budget due to fw correction
          ENDIF
-         !
-         IF( kt == nitend .AND. lwm ) THEN            ! save fwfold value in a file (only one required)
-            CALL ctl_opn( inum, 'EMPave.dat', 'REPLACE', 'FORMATTED', 'SEQUENTIAL', -1, numout, .FALSE., narea )
-            WRITE( inum, "(24X,I8,2ES24.16)" ) nyear, a_fwb_b, a_fwb
-            CLOSE( inum )
-         ENDIF
+         ! Output restart information
+         IF( lrst_oce ) THEN
+            IF(lwp) WRITE(numout,*)
+            IF(lwp) WRITE(numout,*) 'sbc_fwb : writing FW-budget adjustment to ocean restart file at it = ', kt
+            IF(lwp) WRITE(numout,*) '~~~~'
+            IF( lwxios ) CALL iom_swap( cwxios_context )
+            CALL iom_rstput( kt, nitrst, numrow, 'a_fwb',   a_fwb,   ldxios = lwxios )
+            IF( lwxios ) CALL iom_swap( cxios_context  )
+         END IF
+         !
+         IF( kt == nitend .AND. lwp ) WRITE(numout,*) 'sbc_fwb : final freshwater-budget adjustment = ', a_fwb, 'kg/m2/s'
          !
       CASE ( 3 )                             !==  global fwf set to zero and spread out over erp area  ==!
