New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
asminc.F90 in branches/2013/dev_r3853_CNRS9_ConfSetting/NEMOGCM/NEMO/OPA_SRC/ASM – NEMO

source: branches/2013/dev_r3853_CNRS9_ConfSetting/NEMOGCM/NEMO/OPA_SRC/ASM/asminc.F90 @ 3875

Last change on this file since 3875 was 3875, checked in by clevy, 11 years ago

Configuration Setting/Step? 1, see ticket:#1074

  • Property svn:keywords set to Id
File size: 49.4 KB
Line 
1MODULE asminc
2   !!======================================================================
3   !!                       ***  MODULE asminc  ***
4   !! Assimilation increment : Apply an increment generated by data
5   !!                          assimilation
6   !!======================================================================
7   !! History :       ! 2007-03  (M. Martin)  Met Office version
8   !!                 ! 2007-04  (A. Weaver)  calc_date original code
9   !!                 ! 2007-04  (A. Weaver)  Merge with OPAVAR/NEMOVAR
10   !!   NEMO     3.3  ! 2010-05  (D. Lea)  Update to work with NEMO v3.2
11   !!             -   ! 2010-05  (D. Lea)  add calc_month_len routine based on day_init
12   !!            3.4  ! 2012-10  (A. Weaver and K. Mogensen) Fix for direct initialization
13   !!----------------------------------------------------------------------
14
15   !!----------------------------------------------------------------------
16   !!   'key_asminc'   : Switch on the assimilation increment interface
17   !!----------------------------------------------------------------------
18   !!   asm_inc_init   : Initialize the increment arrays and IAU weights
19   !!   calc_date      : Compute the calendar date YYYYMMDD on a given step
20   !!   tra_asm_inc    : Apply the tracer (T and S) increments
21   !!   dyn_asm_inc    : Apply the dynamic (u and v) increments
22   !!   ssh_asm_inc    : Apply the SSH increment
23   !!   seaice_asm_inc : Apply the seaice increment
24   !!----------------------------------------------------------------------
25   USE wrk_nemo         ! Memory Allocation
26   USE par_oce          ! Ocean space and time domain variables
27   USE dom_oce          ! Ocean space and time domain
28   USE domvvl           ! domain: variable volume level
29   USE oce              ! Dynamics and active tracers defined in memory
30   USE ldfdyn_oce       ! ocean dynamics: lateral physics
31   USE eosbn2           ! Equation of state - in situ and potential density
32   USE zpshde           ! Partial step : Horizontal Derivative
33   USE iom              ! Library to read input files
34   USE asmpar           ! Parameters for the assmilation interface
35   USE c1d              ! 1D initialization
36   USE in_out_manager   ! I/O manager
37   USE lib_mpp          ! MPP library
38#if defined key_lim2
39   USE ice_2            ! LIM2
40#endif
41   USE sbc_oce          ! Surface boundary condition variables.
42
43   IMPLICIT NONE
44   PRIVATE
45   
46   PUBLIC   asm_inc_init   !: Initialize the increment arrays and IAU weights
47   PUBLIC   calc_date      !: Compute the calendar date YYYYMMDD on a given step
48   PUBLIC   tra_asm_inc    !: Apply the tracer (T and S) increments
49   PUBLIC   dyn_asm_inc    !: Apply the dynamic (u and v) increments
50   PUBLIC   ssh_asm_inc    !: Apply the SSH increment
51   PUBLIC   seaice_asm_inc !: Apply the seaice increment
52
53#if defined key_asminc
54    LOGICAL, PUBLIC, PARAMETER :: lk_asminc = .TRUE.   !: Logical switch for assimilation increment interface
55#else
56    LOGICAL, PUBLIC, PARAMETER :: lk_asminc = .FALSE.  !: No assimilation increments
57#endif
58   LOGICAL, PUBLIC :: ln_bkgwri = .FALSE. !: No output of the background state fields
59   LOGICAL, PUBLIC :: ln_asmiau = .FALSE. !: No applying forcing with an assimilation increment
60   LOGICAL, PUBLIC :: ln_asmdin = .FALSE. !: No direct initialization
61   LOGICAL, PUBLIC :: ln_trainc = .FALSE. !: No tracer (T and S) assimilation increments
62   LOGICAL, PUBLIC :: ln_dyninc = .FALSE. !: No dynamics (u and v) assimilation increments
63   LOGICAL, PUBLIC :: ln_sshinc = .FALSE. !: No sea surface height assimilation increment
64   LOGICAL, PUBLIC :: ln_seaiceinc = .FALSE. !: No sea ice concentration increment
65   LOGICAL, PUBLIC :: ln_salfix = .FALSE. !: Apply minimum salinity check
66   LOGICAL, PUBLIC :: ln_temnofreeze = .FALSE. !: Don't allow the temperature to drop below freezing
67   INTEGER, PUBLIC :: nn_divdmp = 0       !: Apply divergence damping filter nn_divdmp times
68
69   REAL(wp), PUBLIC, DIMENSION(:,:,:), ALLOCATABLE ::   t_bkg   , s_bkg      !: Background temperature and salinity
70   REAL(wp), PUBLIC, DIMENSION(:,:,:), ALLOCATABLE ::   u_bkg   , v_bkg      !: Background u- & v- velocity components
71   REAL(wp), PUBLIC, DIMENSION(:,:,:), ALLOCATABLE ::   t_bkginc, s_bkginc   !: Increment to the background T & S
72   REAL(wp), PUBLIC, DIMENSION(:,:,:), ALLOCATABLE ::   u_bkginc, v_bkginc   !: Increment to the u- & v-components
73   REAL(wp), PUBLIC, DIMENSION(:)    , ALLOCATABLE ::   wgtiau               !: IAU weights for each time step
74#if defined key_asminc
75   REAL(wp), PUBLIC, DIMENSION(:,:), ALLOCATABLE ::   ssh_iau           !: IAU-weighted sea surface height increment
76#endif
77   !                                !!! time steps relative to the cycle interval [0,nitend-nit000-1]
78   INTEGER , PUBLIC ::   nitbkg      !: Time step of the background state used in the Jb term
79   INTEGER , PUBLIC ::   nitdin      !: Time step of the background state for direct initialization
80   INTEGER , PUBLIC ::   nitiaustr   !: Time step of the start of the IAU interval
81   INTEGER , PUBLIC ::   nitiaufin   !: Time step of the end of the IAU interval
82   !
83   INTEGER , PUBLIC ::   niaufn      !: Type of IAU weighing function: = 0   Constant weighting
84   !                                 !: = 1   Linear hat-like, centred in middle of IAU interval
85   REAL(wp), PUBLIC ::   salfixmin   !: Ensure that the salinity is larger than this  value if (ln_salfix)
86
87   REAL(wp), DIMENSION(:,:), ALLOCATABLE ::   ssh_bkg, ssh_bkginc   ! Background sea surface height and its increment
88   REAL(wp), DIMENSION(:,:), ALLOCATABLE ::   seaice_bkginc         ! Increment to the background sea ice conc
89
90   !! * Substitutions
91#  include "domzgr_substitute.h90"
92#  include "ldfdyn_substitute.h90"
93#  include "vectopt_loop_substitute.h90"
94   !!----------------------------------------------------------------------
95   !! NEMO/OPA 3.3 , NEMO Consortium (2010)
96   !! $Id$
97   !! Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
98   !!----------------------------------------------------------------------
99CONTAINS
100
101   SUBROUTINE asm_inc_init
102      !!----------------------------------------------------------------------
103      !!                    ***  ROUTINE asm_inc_init  ***
104      !!         
105      !! ** Purpose : Initialize the assimilation increment and IAU weights.
106      !!
107      !! ** Method  : Initialize the assimilation increment and IAU weights.
108      !!
109      !! ** Action  :
110      !!----------------------------------------------------------------------
111      INTEGER :: ji, jj, jk
112      INTEGER :: jt
113      INTEGER :: imid
114      INTEGER :: inum
115      INTEGER :: iiauper         ! Number of time steps in the IAU period
116      INTEGER :: icycper         ! Number of time steps in the cycle
117      INTEGER :: iitend_date     ! Date YYYYMMDD of final time step
118      INTEGER :: iitbkg_date     ! Date YYYYMMDD of background time step for Jb term
119      INTEGER :: iitdin_date     ! Date YYYYMMDD of background time step for DI
120      INTEGER :: iitiaustr_date  ! Date YYYYMMDD of IAU interval start time step
121      INTEGER :: iitiaufin_date  ! Date YYYYMMDD of IAU interval final time step
122      INTEGER :: ios             ! Local integer output status for namelist read
123
124      REAL(wp) :: znorm        ! Normalization factor for IAU weights
125      REAL(wp) :: ztotwgt      ! Value of time-integrated IAU weights
126                               ! (should be equal to one)
127      REAL(wp) :: z_inc_dateb  ! Start date of interval on which increment is valid
128      REAL(wp) :: z_inc_datef  ! End date of interval on which increment is valid
129      REAL(wp) :: zdate_bkg    ! Date in background state file for DI
130      REAL(wp) :: zdate_inc    ! Time axis in increments file
131
132      REAL(wp), POINTER, DIMENSION(:,:) :: hdiv
133      !!
134      NAMELIST/nam_asminc/ ln_bkgwri,                                      &
135         &                 ln_trainc, ln_dyninc, ln_sshinc,                &
136         &                 ln_asmdin, ln_asmiau,                           &
137         &                 nitbkg, nitdin, nitiaustr, nitiaufin, niaufn,   &
138         &                 ln_salfix, salfixmin,                &
139         &                 nn_divdmp
140      !!----------------------------------------------------------------------
141
142      !-----------------------------------------------------------------------
143      ! Read Namelist nam_asminc : assimilation increment interface
144      !-----------------------------------------------------------------------
145
146      ! Set default values
147      ln_bkgwri = .FALSE.
148      ln_trainc = .FALSE.
149      ln_dyninc = .FALSE.
150      ln_sshinc = .FALSE.
151      ln_seaiceinc = .FALSE.
152      ln_asmdin = .FALSE.
153      ln_asmiau = .TRUE.
154      ln_salfix = .FALSE.
155      ln_temnofreeze = .FALSE.
156      salfixmin = -9999
157      nitbkg    = 0
158      nitdin    = 0     
159      nitiaustr = 1
160      nitiaufin = 150      ! = 10 days with ORCA2
161      niaufn    = 0
162
163      REWIND( numnam_ref )              ! Namelist nam_asminc in reference namelist : Assimilation increment
164      READ  ( numnam_ref, nam_asminc, IOSTAT = ios, ERR = 901)
165901   IF( ios /= 0 ) CALL ctl_nam ( ios , 'nam_asminc in reference namelist', lwp )
166
167      REWIND( numnam_cfg )              ! Namelist nam_asminc in configuration namelist : Assimilation increment
168      READ  ( numnam_cfg, nam_asminc, IOSTAT = ios, ERR = 902 )
169902   IF( ios /= 0 ) CALL ctl_nam ( ios , 'nam_asminc in configuration namelist', lwp )
170      WRITE ( numond, nam_asminc )
171
172      ! Control print
173      IF(lwp) THEN
174         WRITE(numout,*)
175         WRITE(numout,*) 'asm_inc_init : Assimilation increment initialization :'
176         WRITE(numout,*) '~~~~~~~~~~~~'
177         WRITE(numout,*) '   Namelist namasm : set assimilation increment parameters'
178         WRITE(numout,*) '      Logical switch for writing out background state          ln_bkgwri = ', ln_bkgwri
179         WRITE(numout,*) '      Logical switch for applying tracer increments            ln_trainc = ', ln_trainc
180         WRITE(numout,*) '      Logical switch for applying velocity increments          ln_dyninc = ', ln_dyninc
181         WRITE(numout,*) '      Logical switch for applying SSH increments               ln_sshinc = ', ln_sshinc
182         WRITE(numout,*) '      Logical switch for Direct Initialization (DI)            ln_asmdin = ', ln_asmdin
183         WRITE(numout,*) '      Logical switch for applying sea ice increments        ln_seaiceinc = ', ln_seaiceinc
184         WRITE(numout,*) '      Logical switch for Incremental Analysis Updating (IAU)   ln_asmiau = ', ln_asmiau
185         WRITE(numout,*) '      Timestep of background in [0,nitend-nit000-1]            nitbkg    = ', nitbkg
186         WRITE(numout,*) '      Timestep of background for DI in [0,nitend-nit000-1]     nitdin    = ', nitdin
187         WRITE(numout,*) '      Timestep of start of IAU interval in [0,nitend-nit000-1] nitiaustr = ', nitiaustr
188         WRITE(numout,*) '      Timestep of end of IAU interval in [0,nitend-nit000-1]   nitiaufin = ', nitiaufin
189         WRITE(numout,*) '      Type of IAU weighting function                           niaufn    = ', niaufn
190         WRITE(numout,*) '      Logical switch for ensuring that the sa > salfixmin      ln_salfix = ', ln_salfix
191         WRITE(numout,*) '      Minimum salinity after applying the increments           salfixmin = ', salfixmin
192      ENDIF
193
194      nitbkg_r    = nitbkg    + nit000 - 1  ! Background time referenced to nit000
195      nitdin_r    = nitdin    + nit000 - 1  ! Background time for DI referenced to nit000
196      nitiaustr_r = nitiaustr + nit000 - 1  ! Start of IAU interval referenced to nit000
197      nitiaufin_r = nitiaufin + nit000 - 1  ! End of IAU interval referenced to nit000
198
199      iiauper = nitiaufin_r - nitiaustr_r + 1  ! IAU interval length
200      icycper = nitend      - nit000      + 1  ! Cycle interval length
201
202      ! Date of final time step
203      CALL calc_date( nit000, nitend, ndate0, iitend_date )
204
205      ! Background time for Jb referenced to ndate0
206      CALL calc_date( nit000, nitbkg_r, ndate0, iitbkg_date )
207
208      ! Background time for DI referenced to ndate0
209      CALL calc_date( nit000, nitdin_r, ndate0, iitdin_date )
210
211      ! IAU start time referenced to ndate0
212      CALL calc_date( nit000, nitiaustr_r, ndate0, iitiaustr_date )
213
214      ! IAU end time referenced to ndate0
215      CALL calc_date( nit000, nitiaufin_r, ndate0, iitiaufin_date )
216
217      IF(lwp) THEN
218         WRITE(numout,*)
219         WRITE(numout,*) '   Time steps referenced to current cycle:'
220         WRITE(numout,*) '       iitrst      = ', nit000 - 1
221         WRITE(numout,*) '       nit000      = ', nit000
222         WRITE(numout,*) '       nitend      = ', nitend
223         WRITE(numout,*) '       nitbkg_r    = ', nitbkg_r
224         WRITE(numout,*) '       nitdin_r    = ', nitdin_r
225         WRITE(numout,*) '       nitiaustr_r = ', nitiaustr_r
226         WRITE(numout,*) '       nitiaufin_r = ', nitiaufin_r
227         WRITE(numout,*)
228         WRITE(numout,*) '   Dates referenced to current cycle:'
229         WRITE(numout,*) '       ndastp         = ', ndastp
230         WRITE(numout,*) '       ndate0         = ', ndate0
231         WRITE(numout,*) '       iitend_date    = ', iitend_date
232         WRITE(numout,*) '       iitbkg_date    = ', iitbkg_date
233         WRITE(numout,*) '       iitdin_date    = ', iitdin_date
234         WRITE(numout,*) '       iitiaustr_date = ', iitiaustr_date
235         WRITE(numout,*) '       iitiaufin_date = ', iitiaufin_date
236      ENDIF
237
238      IF ( nacc /= 0 ) &
239         & CALL ctl_stop( ' nacc /= 0 and key_asminc :',  &
240         &                ' Assimilation increments have only been implemented', &
241         &                ' for synchronous time stepping' )
242
243      IF ( ( ln_asmdin ).AND.( ln_asmiau ) )   &
244         & CALL ctl_stop( ' ln_asmdin and ln_asmiau :', &
245         &                ' Choose Direct Initialization OR Incremental Analysis Updating')
246
247      IF (      ( ( .NOT. ln_asmdin ).AND.( .NOT. ln_asmiau ) ) &
248           .AND.( ( ln_trainc ).OR.( ln_dyninc ).OR.( ln_sshinc ) .OR. ( ln_seaiceinc) )) &
249         & CALL ctl_stop( ' One or more of ln_trainc, ln_dyninc, ln_sshinc and ln_seaiceinc is set to .true.', &
250         &                ' but ln_asmdin and ln_asmiau are both set to .false. :', &
251         &                ' Inconsistent options')
252
253      IF ( ( ln_bkgwri ).AND.( ( ln_asmdin ).OR.( ln_asmiau ) ) )  &
254         & CALL ctl_stop( ' ln_bkgwri and either ln_asmdin or ln_asmiau are set to .true.:', &
255         &                ' The background state must be written before applying the increments')
256
257      IF ( ( niaufn /= 0 ).AND.( niaufn /= 1 ) ) &
258         & CALL ctl_stop( ' niaufn /= 0 or niaufn /=1 :',  &
259         &                ' Type IAU weighting function is invalid')
260
261      IF ( ( .NOT. ln_trainc ).AND.( .NOT. ln_dyninc ).AND.( .NOT. ln_sshinc ).AND.( .NOT. ln_seaiceinc ) &
262         &                     )  &
263         & CALL ctl_warn( ' ln_trainc, ln_dyninc, ln_sshinc and ln_seaiceinc are set to .false. :', &
264         &                ' The assimilation increments are not applied')
265
266      IF ( ( ln_asmiau ).AND.( nitiaustr == nitiaufin ) ) &
267         & CALL ctl_stop( ' nitiaustr = nitiaufin :',  &
268         &                ' IAU interval is of zero length')
269
270      IF ( ( ln_asmiau ).AND.( ( nitiaustr_r < nit000 ).OR.( nitiaufin_r > nitend ) ) ) &
271         & CALL ctl_stop( ' nitiaustr or nitiaufin :',  &
272         &                ' IAU starting or final time step is outside the cycle interval', &
273         &                 ' Valid range nit000 to nitend')
274
275      IF ( ( nitbkg_r < nit000 - 1 ).OR.( nitbkg_r > nitend ) ) &
276         & CALL ctl_stop( ' nitbkg :',  &
277         &                ' Background time step is outside the cycle interval')
278
279      IF ( ( nitdin_r < nit000 - 1 ).OR.( nitdin_r > nitend ) ) &
280         & CALL ctl_stop( ' nitdin :',  &
281         &                ' Background time step for Direct Initialization is outside', &
282         &                ' the cycle interval')
283
284      IF ( nstop > 0 ) RETURN       ! if there are any errors then go no further
285
286      !--------------------------------------------------------------------
287      ! Initialize the Incremental Analysis Updating weighting function
288      !--------------------------------------------------------------------
289
290      IF ( ln_asmiau ) THEN
291
292         ALLOCATE( wgtiau( icycper ) )
293
294         wgtiau(:) = 0.0
295
296         IF ( niaufn == 0 ) THEN
297
298            !---------------------------------------------------------
299            ! Constant IAU forcing
300            !---------------------------------------------------------
301
302            DO jt = 1, iiauper
303               wgtiau(jt+nitiaustr-1) = 1.0 / REAL( iiauper )
304            END DO
305
306         ELSEIF ( niaufn == 1 ) THEN
307
308            !---------------------------------------------------------
309            ! Linear hat-like, centred in middle of IAU interval
310            !---------------------------------------------------------
311
312            ! Compute the normalization factor
313            znorm = 0.0
314            IF ( MOD( iiauper, 2 ) == 0 ) THEN  ! Even number of time steps in IAU interval
315               imid = iiauper / 2 
316               DO jt = 1, imid
317                  znorm = znorm + REAL( jt )
318               END DO
319               znorm = 2.0 * znorm
320            ELSE                               ! Odd number of time steps in IAU interval
321               imid = ( iiauper + 1 ) / 2       
322               DO jt = 1, imid - 1
323                  znorm = znorm + REAL( jt )
324               END DO
325               znorm = 2.0 * znorm + REAL( imid )
326            ENDIF
327            znorm = 1.0 / znorm
328
329            DO jt = 1, imid - 1
330               wgtiau(jt+nitiaustr-1) = REAL( jt ) * znorm
331            END DO
332            DO jt = imid, iiauper
333               wgtiau(jt+nitiaustr-1) = REAL( iiauper - jt + 1 ) * znorm
334            END DO
335
336         ENDIF
337
338         ! Test that the integral of the weights over the weighting interval equals 1
339          IF(lwp) THEN
340             WRITE(numout,*)
341             WRITE(numout,*) 'asm_inc_init : IAU weights'
342             WRITE(numout,*) '~~~~~~~~~~~~'
343             WRITE(numout,*) '             time step         IAU  weight'
344             WRITE(numout,*) '             =========     ====================='
345             ztotwgt = 0.0
346             DO jt = 1, icycper
347                ztotwgt = ztotwgt + wgtiau(jt)
348                WRITE(numout,*) '         ', jt, '       ', wgtiau(jt) 
349             END DO   
350             WRITE(numout,*) '         ==================================='
351             WRITE(numout,*) '         Time-integrated weight = ', ztotwgt
352             WRITE(numout,*) '         ==================================='
353          ENDIF
354         
355      ENDIF
356
357      !--------------------------------------------------------------------
358      ! Allocate and initialize the increment arrays
359      !--------------------------------------------------------------------
360
361      ALLOCATE( t_bkginc(jpi,jpj,jpk) )
362      ALLOCATE( s_bkginc(jpi,jpj,jpk) )
363      ALLOCATE( u_bkginc(jpi,jpj,jpk) )
364      ALLOCATE( v_bkginc(jpi,jpj,jpk) )
365      ALLOCATE( ssh_bkginc(jpi,jpj)   )
366      ALLOCATE( seaice_bkginc(jpi,jpj))
367#if defined key_asminc
368      ALLOCATE( ssh_iau(jpi,jpj)      )
369#endif
370      t_bkginc(:,:,:) = 0.0
371      s_bkginc(:,:,:) = 0.0
372      u_bkginc(:,:,:) = 0.0
373      v_bkginc(:,:,:) = 0.0
374      ssh_bkginc(:,:) = 0.0
375      seaice_bkginc(:,:) = 0.0
376#if defined key_asminc
377      ssh_iau(:,:)    = 0.0
378#endif
379      IF ( ( ln_trainc ).OR.( ln_dyninc ).OR.( ln_sshinc ).OR.( ln_seaiceinc ) ) THEN
380
381         !--------------------------------------------------------------------
382         ! Read the increments from file
383         !--------------------------------------------------------------------
384
385         CALL iom_open( c_asminc, inum )
386
387         CALL iom_get( inum, 'time', zdate_inc ) 
388
389         CALL iom_get( inum, 'z_inc_dateb', z_inc_dateb )
390         CALL iom_get( inum, 'z_inc_datef', z_inc_datef )
391         z_inc_dateb = zdate_inc
392         z_inc_datef = zdate_inc
393
394         IF(lwp) THEN
395            WRITE(numout,*) 
396            WRITE(numout,*) 'asm_inc_init : Assimilation increments valid ', &
397               &            ' between dates ', NINT( z_inc_dateb ),' and ',  &
398               &            NINT( z_inc_datef )
399            WRITE(numout,*) '~~~~~~~~~~~~'
400         ENDIF
401
402         IF (     ( NINT( z_inc_dateb ) < ndastp      ) &
403            & .OR.( NINT( z_inc_datef ) > iitend_date ) ) &
404            & CALL ctl_warn( ' Validity time of assimilation increments is ', &
405            &                ' outside the assimilation interval' )
406
407         IF ( ( ln_asmdin ).AND.( NINT( zdate_inc ) /= iitdin_date ) ) &
408            & CALL ctl_warn( ' Validity time of assimilation increments does ', &
409            &                ' not agree with Direct Initialization time' )
410
411         IF ( ln_trainc ) THEN   
412            CALL iom_get( inum, jpdom_autoglo, 'bckint', t_bkginc, 1 )
413            CALL iom_get( inum, jpdom_autoglo, 'bckins', s_bkginc, 1 )
414            ! Apply the masks
415            t_bkginc(:,:,:) = t_bkginc(:,:,:) * tmask(:,:,:)
416            s_bkginc(:,:,:) = s_bkginc(:,:,:) * tmask(:,:,:)
417            ! Set missing increments to 0.0 rather than 1e+20
418            ! to allow for differences in masks
419            WHERE( ABS( t_bkginc(:,:,:) ) > 1.0e+10 ) t_bkginc(:,:,:) = 0.0
420            WHERE( ABS( s_bkginc(:,:,:) ) > 1.0e+10 ) s_bkginc(:,:,:) = 0.0
421         ENDIF
422
423         IF ( ln_dyninc ) THEN   
424            CALL iom_get( inum, jpdom_autoglo, 'bckinu', u_bkginc, 1 )             
425            CALL iom_get( inum, jpdom_autoglo, 'bckinv', v_bkginc, 1 )             
426            ! Apply the masks
427            u_bkginc(:,:,:) = u_bkginc(:,:,:) * umask(:,:,:)
428            v_bkginc(:,:,:) = v_bkginc(:,:,:) * vmask(:,:,:)
429            ! Set missing increments to 0.0 rather than 1e+20
430            ! to allow for differences in masks
431            WHERE( ABS( u_bkginc(:,:,:) ) > 1.0e+10 ) u_bkginc(:,:,:) = 0.0
432            WHERE( ABS( v_bkginc(:,:,:) ) > 1.0e+10 ) v_bkginc(:,:,:) = 0.0
433         ENDIF
434       
435         IF ( ln_sshinc ) THEN
436            CALL iom_get( inum, jpdom_autoglo, 'bckineta', ssh_bkginc, 1 )
437            ! Apply the masks
438            ssh_bkginc(:,:) = ssh_bkginc(:,:) * tmask(:,:,1)
439            ! Set missing increments to 0.0 rather than 1e+20
440            ! to allow for differences in masks
441            WHERE( ABS( ssh_bkginc(:,:) ) > 1.0e+10 ) ssh_bkginc(:,:) = 0.0
442         ENDIF
443
444         IF ( ln_seaiceinc ) THEN
445            CALL iom_get( inum, jpdom_autoglo, 'bckinseaice', seaice_bkginc, 1 )
446            ! Apply the masks
447            seaice_bkginc(:,:) = seaice_bkginc(:,:) * tmask(:,:,1)
448            ! Set missing increments to 0.0 rather than 1e+20
449            ! to allow for differences in masks
450            WHERE( ABS( seaice_bkginc(:,:) ) > 1.0e+10 ) seaice_bkginc(:,:) = 0.0
451         ENDIF
452
453         CALL iom_close( inum )
454 
455      ENDIF
456
457      !-----------------------------------------------------------------------
458      ! Apply divergence damping filter
459      !-----------------------------------------------------------------------
460
461      IF ( ln_dyninc .AND. nn_divdmp > 0 ) THEN
462
463         CALL wrk_alloc(jpi,jpj,hdiv) 
464
465         DO  jt = 1, nn_divdmp
466
467            DO jk = 1, jpkm1
468
469               hdiv(:,:) = 0._wp
470
471               DO jj = 2, jpjm1
472                  DO ji = fs_2, fs_jpim1   ! vector opt.
473                     hdiv(ji,jj) =   &
474                        (  e2u(ji  ,jj  ) * fse3u(ji  ,jj  ,jk) * u_bkginc(ji  ,jj  ,jk)     &
475                         - e2u(ji-1,jj  ) * fse3u(ji-1,jj  ,jk) * u_bkginc(ji-1,jj  ,jk)     &
476                         + e1v(ji  ,jj  ) * fse3v(ji  ,jj  ,jk) * v_bkginc(ji  ,jj  ,jk)     &
477                         - e1v(ji  ,jj-1) * fse3v(ji  ,jj-1,jk) * v_bkginc(ji  ,jj-1,jk)  )  &
478                         / ( e1t(ji,jj) * e2t(ji,jj) * fse3t(ji,jj,jk) )
479                  END DO
480               END DO
481
482               CALL lbc_lnk( hdiv, 'T', 1. )   ! lateral boundary cond. (no sign change)
483
484               DO jj = 2, jpjm1
485                  DO ji = fs_2, fs_jpim1   ! vector opt.
486                     u_bkginc(ji,jj,jk) = u_bkginc(ji,jj,jk) + 0.2_wp * ( e1t(ji+1,jj)*e2t(ji+1,jj) * hdiv(ji+1,jj)   &
487                                                                        - e1t(ji  ,jj)*e2t(ji  ,jj) * hdiv(ji  ,jj) ) &
488                                                                      / e1u(ji,jj) * umask(ji,jj,jk) 
489                     v_bkginc(ji,jj,jk) = v_bkginc(ji,jj,jk) + 0.2_wp * ( e1t(ji,jj+1)*e2t(ji,jj+1) * hdiv(ji,jj+1)   &
490                                                                        - e1t(ji,jj  )*e2t(ji,jj  ) * hdiv(ji,jj  ) ) &
491                                                                      / e2v(ji,jj) * vmask(ji,jj,jk) 
492                  END DO
493               END DO
494
495            END DO
496
497         END DO
498
499         CALL wrk_dealloc(jpi,jpj,hdiv) 
500
501      ENDIF
502
503
504
505      !-----------------------------------------------------------------------
506      ! Allocate and initialize the background state arrays
507      !-----------------------------------------------------------------------
508
509      IF ( ln_asmdin ) THEN
510
511         ALLOCATE( t_bkg(jpi,jpj,jpk) )
512         ALLOCATE( s_bkg(jpi,jpj,jpk) )
513         ALLOCATE( u_bkg(jpi,jpj,jpk) )
514         ALLOCATE( v_bkg(jpi,jpj,jpk) )
515         ALLOCATE( ssh_bkg(jpi,jpj)   )
516
517         t_bkg(:,:,:) = 0.0
518         s_bkg(:,:,:) = 0.0
519         u_bkg(:,:,:) = 0.0
520         v_bkg(:,:,:) = 0.0
521         ssh_bkg(:,:) = 0.0
522
523         !--------------------------------------------------------------------
524         ! Read from file the background state at analysis time
525         !--------------------------------------------------------------------
526
527         CALL iom_open( c_asmdin, inum )
528
529         CALL iom_get( inum, 'rdastp', zdate_bkg ) 
530       
531         IF(lwp) THEN
532            WRITE(numout,*) 
533            WRITE(numout,*) 'asm_inc_init : Assimilation background state valid at : ', &
534               &  NINT( zdate_bkg )
535            WRITE(numout,*) '~~~~~~~~~~~~'
536         ENDIF
537
538         IF ( NINT( zdate_bkg ) /= iitdin_date ) &
539            & CALL ctl_warn( ' Validity time of assimilation background state does', &
540            &                ' not agree with Direct Initialization time' )
541
542         IF ( ln_trainc ) THEN   
543            CALL iom_get( inum, jpdom_autoglo, 'tn', t_bkg )
544            CALL iom_get( inum, jpdom_autoglo, 'sn', s_bkg )
545            t_bkg(:,:,:) = t_bkg(:,:,:) * tmask(:,:,:)
546            s_bkg(:,:,:) = s_bkg(:,:,:) * tmask(:,:,:)
547         ENDIF
548
549         IF ( ln_dyninc ) THEN   
550            CALL iom_get( inum, jpdom_autoglo, 'un', u_bkg )
551            CALL iom_get( inum, jpdom_autoglo, 'vn', v_bkg )
552            u_bkg(:,:,:) = u_bkg(:,:,:) * umask(:,:,:)
553            v_bkg(:,:,:) = v_bkg(:,:,:) * vmask(:,:,:)
554         ENDIF
555       
556         IF ( ln_sshinc ) THEN
557            CALL iom_get( inum, jpdom_autoglo, 'sshn', ssh_bkg )
558            ssh_bkg(:,:) = ssh_bkg(:,:) * tmask(:,:,1)
559         ENDIF
560
561         CALL iom_close( inum )
562
563      ENDIF
564      !
565   END SUBROUTINE asm_inc_init
566
567
568   SUBROUTINE calc_date( kit000, kt, kdate0, kdate )
569      !!----------------------------------------------------------------------
570      !!                    ***  ROUTINE calc_date  ***
571      !!         
572      !! ** Purpose : Compute the calendar date YYYYMMDD at a given time step.
573      !!
574      !! ** Method  : Compute the calendar date YYYYMMDD at a given time step.
575      !!
576      !! ** Action  :
577      !!----------------------------------------------------------------------
578      INTEGER, INTENT(IN) :: kit000  ! Initial time step
579      INTEGER, INTENT(IN) :: kt      ! Current time step referenced to kit000
580      INTEGER, INTENT(IN) :: kdate0  ! Initial date
581      INTEGER, INTENT(OUT) :: kdate  ! Current date reference to kdate0
582      !
583      INTEGER :: iyea0    ! Initial year
584      INTEGER :: imon0    ! Initial month
585      INTEGER :: iday0    ! Initial day
586      INTEGER :: iyea     ! Current year
587      INTEGER :: imon     ! Current month
588      INTEGER :: iday     ! Current day
589      INTEGER :: idaystp  ! Number of days between initial and current date
590      INTEGER :: idaycnt  ! Day counter
591
592      INTEGER, DIMENSION(12) ::   imonth_len    !: length in days of the months of the current year
593
594      !-----------------------------------------------------------------------
595      ! Compute the calendar date YYYYMMDD
596      !-----------------------------------------------------------------------
597
598      ! Initial date
599      iyea0 =   kdate0 / 10000
600      imon0 = ( kdate0 - ( iyea0 * 10000 ) ) / 100
601      iday0 =   kdate0 - ( iyea0 * 10000 ) - ( imon0 * 100 ) 
602
603      ! Check that kt >= kit000 - 1
604      IF ( kt < kit000 - 1 ) CALL ctl_stop( ' kt must be >= kit000 - 1')
605
606      ! If kt = kit000 - 1 then set the date to the restart date
607      IF ( kt == kit000 - 1 ) THEN
608
609         kdate = ndastp
610         RETURN
611
612      ENDIF
613
614      ! Compute the number of days from the initial date
615      idaystp = INT( REAL( kt - kit000 ) * rdt / 86400. )
616   
617      iday    = iday0
618      imon    = imon0
619      iyea    = iyea0
620      idaycnt = 0
621
622      CALL calc_month_len( iyea, imonth_len )
623
624      DO WHILE ( idaycnt < idaystp )
625         iday = iday + 1
626         IF ( iday > imonth_len(imon) )  THEN
627            iday = 1
628            imon = imon + 1
629         ENDIF
630         IF ( imon > 12 ) THEN
631            imon = 1
632            iyea = iyea + 1
633            CALL calc_month_len( iyea, imonth_len )  ! update month lengths
634         ENDIF                 
635         idaycnt = idaycnt + 1
636      END DO
637      !
638      kdate = iyea * 10000 + imon * 100 + iday
639      !
640   END SUBROUTINE
641
642
643   SUBROUTINE calc_month_len( iyear, imonth_len )
644      !!----------------------------------------------------------------------
645      !!                    ***  ROUTINE calc_month_len  ***
646      !!         
647      !! ** Purpose : Compute the number of days in a months given a year.
648      !!
649      !! ** Method  :
650      !!----------------------------------------------------------------------
651      INTEGER, DIMENSION(12) ::   imonth_len    !: length in days of the months of the current year
652      INTEGER :: iyear         !: year
653      !!----------------------------------------------------------------------
654      !
655      ! length of the month of the current year (from nleapy, read in namelist)
656      IF ( nleapy < 2 ) THEN
657         imonth_len(:) = (/ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 /)
658         IF ( nleapy == 1 ) THEN   ! we are using calendar with leap years
659            IF ( MOD(iyear, 4) == 0 .AND. ( MOD(iyear, 400) == 0 .OR. MOD(iyear, 100) /= 0 ) ) THEN
660               imonth_len(2) = 29
661            ENDIF
662         ENDIF
663      ELSE
664         imonth_len(:) = nleapy   ! all months with nleapy days per year
665      ENDIF
666      !
667   END SUBROUTINE
668
669
670   SUBROUTINE tra_asm_inc( kt )
671      !!----------------------------------------------------------------------
672      !!                    ***  ROUTINE tra_asm_inc  ***
673      !!         
674      !! ** Purpose : Apply the tracer (T and S) assimilation increments
675      !!
676      !! ** Method  : Direct initialization or Incremental Analysis Updating
677      !!
678      !! ** Action  :
679      !!----------------------------------------------------------------------
680      INTEGER, INTENT(IN) :: kt               ! Current time step
681      !
682      INTEGER :: ji,jj,jk
683      INTEGER :: it
684      REAL(wp) :: zincwgt  ! IAU weight for current time step
685      REAL (wp), DIMENSION(jpi,jpj,jpk) :: fzptnz ! 3d freezing point values
686      !!----------------------------------------------------------------------
687
688      ! freezing point calculation taken from oc_fz_pt (but calculated for all depths)
689      ! used to prevent the applied increments taking the temperature below the local freezing point
690
691#if defined key_cice 
692        fzptnz(:,:,:) = -1.8_wp
693#else
694        DO jk = 1, jpk
695           DO jj = 1, jpj
696              DO ji = 1, jpk
697                 fzptnz (ji,jj,jk) = ( -0.0575_wp + 1.710523e-3_wp * SQRT( tsn(ji,jj,jk,jp_sal) )                   & 
698                                                  - 2.154996e-4_wp *       tsn(ji,jj,jk,jp_sal)   ) * tsn(ji,jj,jk,jp_sal)  & 
699                                                  - 7.53e-4_wp * fsdepw(ji,jj,jk)       ! (pressure in dbar)
700              END DO
701           END DO
702        END DO
703#endif
704
705      IF ( ln_asmiau ) THEN
706
707         !--------------------------------------------------------------------
708         ! Incremental Analysis Updating
709         !--------------------------------------------------------------------
710
711         IF ( ( kt >= nitiaustr_r ).AND.( kt <= nitiaufin_r ) ) THEN
712
713            it = kt - nit000 + 1
714            zincwgt = wgtiau(it) / rdt   ! IAU weight for the current time step
715
716            IF(lwp) THEN
717               WRITE(numout,*) 
718               WRITE(numout,*) 'tra_asm_inc : Tracer IAU at time step = ', &
719                  &  kt,' with IAU weight = ', wgtiau(it)
720               WRITE(numout,*) '~~~~~~~~~~~~'
721            ENDIF
722
723            ! Update the tracer tendencies
724            DO jk = 1, jpkm1
725               IF (ln_temnofreeze) THEN
726                  ! Do not apply negative increments if the temperature will fall below freezing
727                  WHERE(t_bkginc(:,:,jk) > 0.0_wp .OR. &
728                     &   tsn(:,:,jk,jp_tem) + tsa(:,:,jk,jp_tem) + t_bkginc(:,:,jk) * wgtiau(it) > fzptnz(:,:,jk) ) 
729                     tsa(:,:,jk,jp_tem) = tsa(:,:,jk,jp_tem) + t_bkginc(:,:,jk) * zincwgt 
730                  END WHERE
731               ELSE
732                  tsa(:,:,jk,jp_tem) = tsa(:,:,jk,jp_tem) + t_bkginc(:,:,jk) * zincwgt 
733               ENDIF
734               IF (ln_salfix) THEN
735                  ! Do not apply negative increments if the salinity will fall below a specified
736                  ! minimum value salfixmin
737                  WHERE(s_bkginc(:,:,jk) > 0.0_wp .OR. &
738                     &   tsn(:,:,jk,jp_sal) + tsa(:,:,jk,jp_sal) + s_bkginc(:,:,jk) * wgtiau(it) > salfixmin ) 
739                     tsa(:,:,jk,jp_sal) = tsa(:,:,jk,jp_sal) + s_bkginc(:,:,jk) * zincwgt
740                  END WHERE
741               ELSE
742                  tsa(:,:,jk,jp_sal) = tsa(:,:,jk,jp_sal) + s_bkginc(:,:,jk) * zincwgt
743               ENDIF
744            END DO
745
746         ENDIF
747
748         IF ( kt == nitiaufin_r + 1  ) THEN   ! For bias crcn to work
749            DEALLOCATE( t_bkginc )
750            DEALLOCATE( s_bkginc )
751         ENDIF
752
753
754      ELSEIF ( ln_asmdin ) THEN
755
756         !--------------------------------------------------------------------
757         ! Direct Initialization
758         !--------------------------------------------------------------------
759           
760         IF ( kt == nitdin_r ) THEN
761
762            neuler = 0  ! Force Euler forward step
763
764            ! Initialize the now fields with the background + increment
765            IF (ln_temnofreeze) THEN
766               ! Do not apply negative increments if the temperature will fall below freezing
767               WHERE(t_bkginc(:,:,:) > 0.0_wp .OR. &
768                  &   tsn(:,:,:,jp_tem) + t_bkginc(:,:,:) > fzptnz(:,:,:) ) 
769                  tsn(:,:,:,jp_tem) = t_bkg(:,:,:) + t_bkginc(:,:,:)   
770               END WHERE
771            ELSE
772               tsn(:,:,:,jp_tem) = t_bkg(:,:,:) + t_bkginc(:,:,:)   
773            ENDIF
774            IF (ln_salfix) THEN
775               ! Do not apply negative increments if the salinity will fall below a specified
776               ! minimum value salfixmin
777               WHERE(s_bkginc(:,:,:) > 0.0_wp .OR. &
778                  &   tsn(:,:,:,jp_sal) + s_bkginc(:,:,:) > salfixmin ) 
779                  tsn(:,:,:,jp_sal) = s_bkg(:,:,:) + s_bkginc(:,:,:)   
780               END WHERE
781            ELSE
782               tsn(:,:,:,jp_sal) = s_bkg(:,:,:) + s_bkginc(:,:,:)   
783            ENDIF
784
785            tsb(:,:,:,:) = tsn(:,:,:,:)               ! Update before fields
786
787            CALL eos( tsb, rhd, rhop )                ! Before potential and in situ densities
788         
789            IF( ln_zps .AND. .NOT. lk_c1d ) &
790               &  CALL zps_hde( nit000, jpts, tsb, &  ! Partial steps: before horizontal derivative
791               &                gtsu, gtsv, rhd,   &  ! of T, S, rd at the bottom ocean level
792               &                gru , grv )
793
794#if defined key_zdfkpp
795            CALL eos( tsn, rhd )                      ! Compute rhd
796#endif
797
798            DEALLOCATE( t_bkginc )
799            DEALLOCATE( s_bkginc )
800            DEALLOCATE( t_bkg    )
801            DEALLOCATE( s_bkg    )
802         ENDIF
803         
804      ENDIF
805      ! Perhaps the following call should be in step
806      IF   ( ln_seaiceinc  )   CALL seaice_asm_inc ( kt )   ! apply sea ice concentration increment
807      !
808   END SUBROUTINE tra_asm_inc
809
810
811   SUBROUTINE dyn_asm_inc( kt )
812      !!----------------------------------------------------------------------
813      !!                    ***  ROUTINE dyn_asm_inc  ***
814      !!         
815      !! ** Purpose : Apply the dynamics (u and v) assimilation increments.
816      !!
817      !! ** Method  : Direct initialization or Incremental Analysis Updating.
818      !!
819      !! ** Action  :
820      !!----------------------------------------------------------------------
821      INTEGER, INTENT(IN) :: kt   ! Current time step
822      !
823      INTEGER :: jk
824      INTEGER :: it
825      REAL(wp) :: zincwgt  ! IAU weight for current time step
826      !!----------------------------------------------------------------------
827
828      IF ( ln_asmiau ) THEN
829
830         !--------------------------------------------------------------------
831         ! Incremental Analysis Updating
832         !--------------------------------------------------------------------
833
834         IF ( ( kt >= nitiaustr_r ).AND.( kt <= nitiaufin_r ) ) THEN
835
836            it = kt - nit000 + 1
837            zincwgt = wgtiau(it) / rdt   ! IAU weight for the current time step
838
839            IF(lwp) THEN
840               WRITE(numout,*) 
841               WRITE(numout,*) 'dyn_asm_inc : Dynamics IAU at time step = ', &
842                  &  kt,' with IAU weight = ', wgtiau(it)
843               WRITE(numout,*) '~~~~~~~~~~~~'
844            ENDIF
845
846            ! Update the dynamic tendencies
847            DO jk = 1, jpkm1
848               ua(:,:,jk) = ua(:,:,jk) + u_bkginc(:,:,jk) * zincwgt
849               va(:,:,jk) = va(:,:,jk) + v_bkginc(:,:,jk) * zincwgt
850            END DO
851           
852            IF ( kt == nitiaufin_r ) THEN
853               DEALLOCATE( u_bkginc )
854               DEALLOCATE( v_bkginc )
855            ENDIF
856
857         ENDIF
858
859      ELSEIF ( ln_asmdin ) THEN 
860
861         !--------------------------------------------------------------------
862         ! Direct Initialization
863         !--------------------------------------------------------------------
864         
865         IF ( kt == nitdin_r ) THEN
866
867            neuler = 0                    ! Force Euler forward step
868
869            ! Initialize the now fields with the background + increment
870            un(:,:,:) = u_bkg(:,:,:) + u_bkginc(:,:,:)
871            vn(:,:,:) = v_bkg(:,:,:) + v_bkginc(:,:,:) 
872
873            ub(:,:,:) = un(:,:,:)         ! Update before fields
874            vb(:,:,:) = vn(:,:,:)
875 
876            DEALLOCATE( u_bkg    )
877            DEALLOCATE( v_bkg    )
878            DEALLOCATE( u_bkginc )
879            DEALLOCATE( v_bkginc )
880         ENDIF
881         !
882      ENDIF
883      !
884   END SUBROUTINE dyn_asm_inc
885
886
887   SUBROUTINE ssh_asm_inc( kt )
888      !!----------------------------------------------------------------------
889      !!                    ***  ROUTINE ssh_asm_inc  ***
890      !!         
891      !! ** Purpose : Apply the sea surface height assimilation increment.
892      !!
893      !! ** Method  : Direct initialization or Incremental Analysis Updating.
894      !!
895      !! ** Action  :
896      !!----------------------------------------------------------------------
897      INTEGER, INTENT(IN) :: kt   ! Current time step
898      !
899      INTEGER :: it
900      INTEGER :: jk
901      REAL(wp) :: zincwgt  ! IAU weight for current time step
902      !!----------------------------------------------------------------------
903
904      IF ( ln_asmiau ) THEN
905
906         !--------------------------------------------------------------------
907         ! Incremental Analysis Updating
908         !--------------------------------------------------------------------
909
910         IF ( ( kt >= nitiaustr_r ).AND.( kt <= nitiaufin_r ) ) THEN
911
912            it = kt - nit000 + 1
913            zincwgt = wgtiau(it) / rdt   ! IAU weight for the current time step
914
915            IF(lwp) THEN
916               WRITE(numout,*) 
917               WRITE(numout,*) 'ssh_asm_inc : SSH IAU at time step = ', &
918                  &  kt,' with IAU weight = ', wgtiau(it)
919               WRITE(numout,*) '~~~~~~~~~~~~'
920            ENDIF
921
922            ! Save the tendency associated with the IAU weighted SSH increment
923            ! (applied in dynspg.*)
924#if defined key_asminc
925            ssh_iau(:,:) = ssh_bkginc(:,:) * zincwgt
926#endif
927            IF ( kt == nitiaufin_r ) THEN
928               DEALLOCATE( ssh_bkginc )
929            ENDIF
930
931         ENDIF
932
933      ELSEIF ( ln_asmdin ) THEN
934
935         !--------------------------------------------------------------------
936         ! Direct Initialization
937         !--------------------------------------------------------------------
938
939         IF ( kt == nitdin_r ) THEN
940
941            neuler = 0                    ! Force Euler forward step
942
943            ! Initialize the now fields the background + increment
944            sshn(:,:) = ssh_bkg(:,:) + ssh_bkginc(:,:) 
945
946            ! Update before fields
947            sshb(:,:) = sshn(:,:)         
948
949            IF( lk_vvl ) THEN
950               DO jk = 1, jpk
951                  fse3t_b(:,:,jk) = fse3t_n(:,:,jk)
952               END DO
953            ENDIF
954
955            DEALLOCATE( ssh_bkg    )
956            DEALLOCATE( ssh_bkginc )
957
958         ENDIF
959         !
960      ENDIF
961      !
962   END SUBROUTINE ssh_asm_inc
963
964
965   SUBROUTINE seaice_asm_inc( kt, kindic )
966      !!----------------------------------------------------------------------
967      !!                    ***  ROUTINE seaice_asm_inc  ***
968      !!         
969      !! ** Purpose : Apply the sea ice assimilation increment.
970      !!
971      !! ** Method  : Direct initialization or Incremental Analysis Updating.
972      !!
973      !! ** Action  :
974      !!
975      !!----------------------------------------------------------------------
976      IMPLICIT NONE
977      !
978      INTEGER, INTENT(in)           ::   kt   ! Current time step
979      INTEGER, INTENT(in), OPTIONAL ::   kindic   ! flag for disabling the deallocation
980      !
981      INTEGER  ::   it
982      REAL(wp) ::   zincwgt   ! IAU weight for current time step
983#if defined key_lim2
984      REAL(wp), DIMENSION(jpi,jpj) ::   zofrld, zohicif, zseaicendg, zhicifinc  ! LIM
985      REAL(wp) ::   zhicifmin = 0.5_wp      ! ice minimum depth in metres
986#endif
987      !!----------------------------------------------------------------------
988
989      IF ( ln_asmiau ) THEN
990
991         !--------------------------------------------------------------------
992         ! Incremental Analysis Updating
993         !--------------------------------------------------------------------
994
995         IF ( ( kt >= nitiaustr_r ).AND.( kt <= nitiaufin_r ) ) THEN
996
997            it = kt - nit000 + 1
998            zincwgt = wgtiau(it)      ! IAU weight for the current time step
999            ! note this is not a tendency so should not be divided by rdt (as with the tracer and other increments)
1000
1001            IF(lwp) THEN
1002               WRITE(numout,*) 
1003               WRITE(numout,*) 'seaice_asm_inc : sea ice conc IAU at time step = ', &
1004                  &  kt,' with IAU weight = ', wgtiau(it)
1005               WRITE(numout,*) '~~~~~~~~~~~~'
1006            ENDIF
1007
1008            ! Sea-ice : LIM-3 case (to add)
1009
1010#if defined key_lim2
1011            ! Sea-ice : LIM-2 case
1012            zofrld (:,:) = frld(:,:)
1013            zohicif(:,:) = hicif(:,:)
1014            !
1015            frld  = MIN( MAX( frld (:,:) - seaice_bkginc(:,:) * zincwgt, 0.0_wp), 1.0_wp)
1016            pfrld = MIN( MAX( pfrld(:,:) - seaice_bkginc(:,:) * zincwgt, 0.0_wp), 1.0_wp)
1017            fr_i(:,:) = 1.0_wp - frld(:,:)        ! adjust ice fraction
1018            !
1019            zseaicendg(:,:) = zofrld(:,:) - frld(:,:)   ! find out actual sea ice nudge applied
1020            !
1021            ! Nudge sea ice depth to bring it up to a required minimum depth
1022            WHERE( zseaicendg(:,:) > 0.0_wp .AND. hicif(:,:) < zhicifmin ) 
1023               zhicifinc(:,:) = (zhicifmin - hicif(:,:)) * zincwgt   
1024            ELSEWHERE
1025               zhicifinc(:,:) = 0.0_wp
1026            END WHERE
1027            !
1028            ! nudge ice depth
1029            hicif (:,:) = hicif (:,:) + zhicifinc(:,:)
1030            phicif(:,:) = phicif(:,:) + zhicifinc(:,:)       
1031            !
1032            ! seaice salinity balancing (to add)
1033#endif
1034
1035#if defined key_cice
1036            ! Sea-ice : CICE case. Pass ice increment tendency into CICE
1037            ndaice_da(:,:) = seaice_bkginc(:,:) * zincwgt / rdt
1038#endif
1039
1040            IF ( kt == nitiaufin_r ) THEN
1041               DEALLOCATE( seaice_bkginc )
1042            ENDIF
1043
1044         ELSE
1045
1046#if defined key_cice
1047            ! Sea-ice : CICE case. Zero ice increment tendency into CICE
1048            ndaice_da(:,:) = 0.0_wp
1049#endif
1050
1051         ENDIF
1052
1053      ELSEIF ( ln_asmdin ) THEN
1054
1055         !--------------------------------------------------------------------
1056         ! Direct Initialization
1057         !--------------------------------------------------------------------
1058
1059         IF ( kt == nitdin_r ) THEN
1060
1061            neuler = 0                    ! Force Euler forward step
1062
1063            ! Sea-ice : LIM-3 case (to add)
1064
1065#if defined key_lim2
1066            ! Sea-ice : LIM-2 case.
1067            zofrld(:,:)=frld(:,:)
1068            zohicif(:,:)=hicif(:,:)
1069            !
1070            ! Initialize the now fields the background + increment
1071            frld (:,:) = MIN( MAX( frld(:,:) - seaice_bkginc(:,:), 0.0_wp), 1.0_wp)
1072            pfrld(:,:) = frld(:,:) 
1073            fr_i (:,:) = 1.0_wp - frld(:,:)                ! adjust ice fraction
1074            zseaicendg(:,:) = zofrld(:,:) - frld(:,:)      ! find out actual sea ice nudge applied
1075            !
1076            ! Nudge sea ice depth to bring it up to a required minimum depth
1077            WHERE( zseaicendg(:,:) > 0.0_wp .AND. hicif(:,:) < zhicifmin ) 
1078               zhicifinc(:,:) = (zhicifmin - hicif(:,:)) * zincwgt   
1079            ELSEWHERE
1080               zhicifinc(:,:) = 0.0_wp
1081            END WHERE
1082            !
1083            ! nudge ice depth
1084            hicif (:,:) = hicif (:,:) + zhicifinc(:,:)
1085            phicif(:,:) = phicif(:,:)       
1086            !
1087            ! seaice salinity balancing (to add)
1088#endif
1089 
1090#if defined key_cice
1091            ! Sea-ice : CICE case. Pass ice increment tendency into CICE - is this correct?
1092           ndaice_da(:,:) = seaice_bkginc(:,:) / rdt
1093#endif
1094           IF ( .NOT. PRESENT(kindic) ) THEN
1095              DEALLOCATE( seaice_bkginc )
1096           END IF
1097
1098         ELSE
1099
1100#if defined key_cice
1101            ! Sea-ice : CICE case. Zero ice increment tendency into CICE
1102            ndaice_da(:,:) = 0.0_wp
1103#endif
1104         
1105         ENDIF
1106
1107!#if defined defined key_lim2 || defined key_cice
1108!
1109!            IF (ln_seaicebal ) THEN       
1110!             !! balancing salinity increments
1111!             !! simple case from limflx.F90 (doesn't include a mass flux)
1112!             !! assumption is that as ice concentration is reduced or increased
1113!             !! the snow and ice depths remain constant
1114!             !! note that snow is being created where ice concentration is being increased
1115!             !! - could be more sophisticated and
1116!             !! not do this (but would need to alter h_snow)
1117!
1118!             usave(:,:,:)=sb(:,:,:)   ! use array as a temporary store
1119!
1120!             DO jj = 1, jpj
1121!               DO ji = 1, jpi
1122!           ! calculate change in ice and snow mass per unit area
1123!           ! positive values imply adding salt to the ocean (results from ice formation)
1124!           ! fwf : ice formation and melting
1125!
1126!                 zfons = ( -nfresh_da(ji,jj)*soce + nfsalt_da(ji,jj) )*rdt
1127!
1128!           ! change salinity down to mixed layer depth
1129!                 mld=hmld_kara(ji,jj)
1130!
1131!           ! prevent small mld
1132!           ! less than 10m can cause salinity instability
1133!                 IF (mld < 10) mld=10
1134!
1135!           ! set to bottom of a level
1136!                 DO jk = jpk-1, 2, -1
1137!                   IF ((mld > gdepw(ji,jj,jk)) .and. (mld < gdepw(ji,jj,jk+1))) THEN
1138!                     mld=gdepw(ji,jj,jk+1)
1139!                     jkmax=jk
1140!                   ENDIF
1141!                 ENDDO
1142!
1143!            ! avoid applying salinity balancing in shallow water or on land
1144!            !
1145!
1146!            ! dsal_ocn (psu kg m^-2) / (kg m^-3 * m)
1147!
1148!                 dsal_ocn=0.0_wp
1149!                 sal_thresh=5.0_wp        ! minimum salinity threshold for salinity balancing
1150!
1151!                 if (tmask(ji,jj,1) > 0 .AND. tmask(ji,jj,jkmax) > 0 ) &
1152!                              dsal_ocn = zfons / (rhop(ji,jj,1) * mld)
1153!
1154!           ! put increments in for levels in the mixed layer
1155!           ! but prevent salinity below a threshold value
1156!
1157!                   DO jk = 1, jkmax             
1158!
1159!                     IF (dsal_ocn > 0.0_wp .or. sb(ji,jj,jk)+dsal_ocn > sal_thresh) THEN
1160!                           sb(ji,jj,jk) = sb(ji,jj,jk) + dsal_ocn
1161!                           sn(ji,jj,jk) = sn(ji,jj,jk) + dsal_ocn
1162!                     ENDIF
1163!
1164!                   ENDDO
1165!
1166!      !            !  salt exchanges at the ice/ocean interface
1167!      !            zpmess         = zfons / rdt_ice    ! rdt_ice is ice timestep
1168!      !
1169!      !! Adjust fsalt. A +ve fsalt means adding salt to ocean
1170!      !!           fsalt(ji,jj) =  fsalt(ji,jj) + zpmess     ! adjust fsalt 
1171!      !!               
1172!      !!           emps(ji,jj) = emps(ji,jj) + zpmess        ! or adjust emps (see icestp1d)
1173!      !!                                                     ! E-P (kg m-2 s-2)
1174!      !            emp(ji,jj) = emp(ji,jj) + zpmess          ! E-P (kg m-2 s-2)
1175!               ENDDO !ji
1176!             ENDDO !jj!
1177!
1178!            ENDIF !ln_seaicebal
1179!
1180!#endif
1181
1182      ENDIF
1183
1184   END SUBROUTINE seaice_asm_inc
1185   
1186   !!======================================================================
1187END MODULE asminc
Note: See TracBrowser for help on using the repository browser.