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.
icerst.F90 in NEMO/branches/2019/dev_r11756_SI3restart_XIOS/src/ICE – NEMO

source: NEMO/branches/2019/dev_r11756_SI3restart_XIOS/src/ICE/icerst.F90 @ 11870

Last change on this file since 11870 was 11870, checked in by andmirek, 4 years ago

Ticket #2323: open single restart write context for all restart files

  • Property svn:keywords set to Id
File size: 14.9 KB
Line 
1MODULE icerst
2   !!======================================================================
3   !!                     ***  MODULE  icerst  ***
4   !!   sea-ice :  write/read the ice restart file
5   !!======================================================================
6   !! History:   4.0  !  2018     (many people)      SI3 [aka Sea Ice cube]
7   !!----------------------------------------------------------------------
8#if defined key_si3
9   !!----------------------------------------------------------------------
10   !!   'key_si3'                                       SI3 sea-ice model
11   !!----------------------------------------------------------------------
12   !!   ice_rst_opn   : open  restart file
13   !!   ice_rst_write : write restart file
14   !!   ice_rst_read  : read  restart file
15   !!----------------------------------------------------------------------
16   USE ice            ! sea-ice: variables
17   USE dom_oce        ! ocean domain
18   USE phycst  , ONLY : rt0
19   USE sbc_oce , ONLY : nn_fsbc, ln_cpl
20   USE iceistate      ! sea-ice: initial state
21   USE icectl         ! sea-ice: control
22   !
23   USE in_out_manager ! I/O manager
24   USE iom            ! I/O manager library
25   USE lib_mpp        ! MPP library
26   USE lib_fortran    ! fortran utilities (glob_sum + no signed zero)
27
28   IMPLICIT NONE
29   PRIVATE
30
31   PUBLIC   ice_rst_opn     ! called by icestp
32   PUBLIC   ice_rst_write   ! called by icestp
33   PUBLIC   ice_rst_read    ! called by ice_init
34
35   !!----------------------------------------------------------------------
36   !! NEMO/ICE 4.0 , NEMO Consortium (2018)
37   !! $Id$
38   !! Software governed by the CeCILL license (see ./LICENSE)
39   !!----------------------------------------------------------------------
40CONTAINS
41
42   SUBROUTINE ice_rst_opn( kt )
43      !!----------------------------------------------------------------------
44      !!                    ***  ice_rst_opn  ***
45      !!
46      !! ** purpose  :   open restart file
47      !!----------------------------------------------------------------------
48      INTEGER, INTENT(in) ::   kt       ! number of iteration
49      !
50      CHARACTER(len=20)   ::   clkt     ! ocean time-step define as a character
51      CHARACTER(len=50)   ::   clname   ! ice output restart file name
52      CHARACTER(len=256)  ::   clpath   ! full path to ice output restart file
53      CHARACTER(LEN=52)   ::   clpname   ! ocean output restart file name including prefix for AGRIF
54      !!----------------------------------------------------------------------
55      !
56      IF( kt == nit000 )   lrst_ice = .FALSE.   ! default definition
57
58      IF( ln_rst_list .OR. nn_stock /= -1 ) THEN
59      ! in order to get better performances with NetCDF format, we open and define the ice restart file
60      ! one ice time step before writing the data (-> at nitrst - 2*nn_fsbc + 1), except if we write ice
61      ! restart files every ice time step or if an ice restart file was writen at nitend - 2*nn_fsbc + 1
62      IF( kt == nitrst - 2*nn_fsbc + 1 .OR. nn_stock == nn_fsbc    &
63         &                             .OR. ( kt == nitend - nn_fsbc + 1 .AND. .NOT. lrst_ice ) ) THEN
64         IF( nitrst <= nitend .AND. nitrst > 0 ) THEN
65            ! beware of the format used to write kt (default is i8.8, that should be large enough...)
66            IF( nitrst > 99999999 ) THEN   ;   WRITE(clkt, *       ) nitrst
67            ELSE                           ;   WRITE(clkt, '(i8.8)') nitrst
68            ENDIF
69            ! create the file
70            clname = TRIM(cexper)//"_"//TRIM(ADJUSTL(clkt))//"_"//TRIM(cn_icerst_out)
71            clpath = TRIM(cn_icerst_outdir) 
72            IF( clpath(LEN_TRIM(clpath):) /= '/' ) clpath = TRIM(clpath)//'/'
73            IF(lwp) THEN
74               WRITE(numout,*)
75               WRITE(numout,*) '             open ice restart NetCDF file: ',TRIM(clpath)//clname
76               IF( kt == nitrst - 2*nn_fsbc + 1 ) THEN
77                  WRITE(numout,*) '             kt = nitrst - 2*nn_fsbc + 1 = ', kt,' date= ', ndastp
78               ELSE
79                  WRITE(numout,*) '             kt = '                         , kt,' date= ', ndastp
80               ENDIF
81            ENDIF
82            !
83            IF(lwxios) THEN
84               CALL iom_setkt( nitrst, cwxios_context )
85            ELSE
86               CALL iom_open( TRIM(clpath)//TRIM(clname), numriw, ldwrt = .TRUE., kdlev = jpl )
87            ENDIF
88            lrst_ice = .TRUE.
89         ENDIF
90      ENDIF
91      ENDIF
92      !
93      IF( ln_icectl )   CALL ice_prt( kt, iiceprt, jiceprt, 1, ' - Beginning the time step - ' )   ! control print
94      !
95   END SUBROUTINE ice_rst_opn
96
97
98   SUBROUTINE ice_rst_write( kt )
99      !!----------------------------------------------------------------------
100      !!                    ***  ice_rst_write  ***
101      !!
102      !! ** purpose  :   write restart file
103      !!----------------------------------------------------------------------
104      INTEGER, INTENT(in) ::   kt     ! number of iteration
105      !!
106      INTEGER ::   jk    ! dummy loop indices
107      INTEGER ::   iter
108      CHARACTER(len=25) ::   znam
109      CHARACTER(len=2)  ::   zchar, zchar1
110      REAL(wp), DIMENSION(jpi,jpj,jpl) ::   z3d   ! 3D workspace
111      !!----------------------------------------------------------------------
112
113      iter = kt + nn_fsbc - 1   ! ice restarts are written at kt == nitrst - nn_fsbc + 1
114
115      IF( iter == nitrst ) THEN
116         IF(lwp) WRITE(numout,*)
117         IF(lwp) WRITE(numout,*) 'ice_rst_write : write ice restart file  kt =', kt
118         IF(lwp) WRITE(numout,*) '~~~~~~~~~~~~~~'         
119      ENDIF
120
121      ! Write in numriw (if iter == nitrst)
122      ! ------------------
123      !                                                                        ! calendar control
124      IF(lwxios) CALL iom_swap(      cwxios_context          )
125      CALL iom_rstput( iter, nitrst, numriw, 'nn_fsbc_ice', REAL( nn_fsbc, wp ), ldxios = lwxios )      ! time-step
126      CALL iom_rstput( iter, nitrst, numriw, 'kt_ice' , REAL( iter   , wp ), ldxios = lwxios )      ! date
127      CALL iom_delay_rst( 'WRITE', 'ICE', numriw )   ! save only ice delayed global communication variables
128
129      ! Prognostic variables
130      CALL iom_rstput( iter, nitrst, numriw, 'v_i'  , v_i, ldxios = lwxios   )
131      CALL iom_rstput( iter, nitrst, numriw, 'v_s'  , v_s, ldxios = lwxios   )
132      CALL iom_rstput( iter, nitrst, numriw, 'sv_i' , sv_i, ldxios = lwxios  )
133      CALL iom_rstput( iter, nitrst, numriw, 'a_i'  , a_i, ldxios = lwxios   )
134      CALL iom_rstput( iter, nitrst, numriw, 't_su' , t_su, ldxios = lwxios  )
135      CALL iom_rstput( iter, nitrst, numriw, 'u_ice', u_ice, ldxios = lwxios )
136      CALL iom_rstput( iter, nitrst, numriw, 'v_ice', v_ice, ldxios = lwxios )
137      CALL iom_rstput( iter, nitrst, numriw, 'oa_i' , oa_i, ldxios = lwxios  )
138      CALL iom_rstput( iter, nitrst, numriw, 'a_ip' , a_ip, ldxios = lwxios  )
139      CALL iom_rstput( iter, nitrst, numriw, 'v_ip' , v_ip, ldxios = lwxios  )
140      ! Snow enthalpy
141      DO jk = 1, nlay_s 
142         WRITE(zchar1,'(I2.2)') jk
143         znam = 'e_s'//'_l'//zchar1
144         z3d(:,:,:) = e_s(:,:,jk,:)
145         CALL iom_rstput( iter, nitrst, numriw, znam , z3d, ldxios = lwxios )
146      END DO
147      ! Ice enthalpy
148      DO jk = 1, nlay_i 
149         WRITE(zchar1,'(I2.2)') jk
150         znam = 'e_i'//'_l'//zchar1
151         z3d(:,:,:) = e_i(:,:,jk,:)
152         CALL iom_rstput( iter, nitrst, numriw, znam , z3d, ldxios = lwxios )
153      END DO
154      ! fields needed for Met Office (Jules) coupling
155      IF( ln_cpl ) THEN
156         CALL iom_rstput( iter, nitrst, numriw, 'cnd_ice', cnd_ice, ldxios = lwxios )
157         CALL iom_rstput( iter, nitrst, numriw, 't1_ice' , t1_ice, ldxios = lwxios  )
158      ENDIF
159      IF(lwxios) CALL iom_swap(      cxios_context          )
160      !
161
162      ! close restart file
163      ! ------------------
164      IF( iter == nitrst ) THEN
165         IF(.NOT. lwxios) THEN
166            CALL iom_close( numriw )
167         ENDIF
168         lrst_ice = .FALSE.
169      ENDIF
170      !
171   END SUBROUTINE ice_rst_write
172
173
174   SUBROUTINE ice_rst_read
175      !!----------------------------------------------------------------------
176      !!                    ***  ice_rst_read  ***
177      !!
178      !! ** purpose  :   read restart file
179      !!----------------------------------------------------------------------
180      INTEGER           ::   jk
181      LOGICAL           ::   llok
182      INTEGER           ::   id0, id1, id2, id3, id4   ! local integer
183      CHARACTER(len=25) ::   znam
184      CHARACTER(len=2)  ::   zchar, zchar1
185      REAL(wp)          ::   zfice, ziter
186      REAL(wp), DIMENSION(jpi,jpj,jpl) ::   z3d   ! 3D workspace
187      !!----------------------------------------------------------------------
188
189      IF(lwp) THEN
190         WRITE(numout,*)
191         WRITE(numout,*) 'ice_rst_read: read ice NetCDF restart file'
192         WRITE(numout,*) '~~~~~~~~~~~~'
193      ENDIF
194
195      lxios_sini = .FALSE.
196      CALL iom_open ( TRIM(cn_icerst_indir)//'/'//cn_icerst_in, numrir, kdlev = jpl )
197
198      ! test if v_i exists
199      id0 = iom_varid( numrir, 'v_i' , ldstop = .FALSE. )
200
201      !                    ! ------------------------------ !
202      IF( id0 > 0 ) THEN   ! == case of a normal restart == !
203         !                 ! ------------------------------ !
204         IF(.NOT.lxios_set) THEN
205            lrxios = lrxios.AND.lxios_sini
206         ELSE
207            IF(.NOT.lxios_sini) THEN
208               WRITE(ctmp1,*) 'E R R O R : XIOS can not be used to read SI3 restart from multiple files'
209               CALL ctl_stop( 'ice_rst_read:', ctmp1 )
210            ENDIF
211         ENDIF
212
213         lrsi3_nemo = .TRUE.                       ! SI3 restart written by NEMO
214
215         IF(lrxios) THEN
216           crixios_context = 'si3_rst'
217           CALL iom_init( crixios_context, TRIM(cn_icerst_indir)//'/'//cn_icerst_in, ld_tmppatch = .false. )
218           lxios_set = .TRUE.
219         ENDIF
220
221         ! Time info
222         CALL iom_get( numrir, 'nn_fsbc_ice', zfice, ldxios = lrxios )
223         CALL iom_get( numrir, 'kt_ice' , ziter, ldxios = lrxios )   
224         IF(lwp) WRITE(numout,*) '   read ice restart file at time step    : ', ziter
225         IF(lwp) WRITE(numout,*) '   in any case we force it to nit000 - 1 : ', nit000 - 1
226
227         ! Control of date
228         IF( ( nit000 - NINT(ziter) ) /= 1 .AND. ABS( nrstdt ) == 1 )   &
229            &     CALL ctl_stop( 'ice_rst_read ===>>>> : problem with nit000 in ice restart',  &
230            &                   '   verify the file or rerun with the value 0 for the',        &
231            &                   '   control of time parameter  nrstdt' )
232         IF( NINT(zfice) /= nn_fsbc          .AND. ABS( nrstdt ) == 1 )   &
233                 CALL ctl_stop( 'ice_rst_read ===>>>> : problem with nn_fsbc in ice restart',  &
234            &                   '   verify the file or rerun with the value 0 for the',         &
235            &                   '   control of time parameter  nrstdt' )
236
237         ! --- mandatory fields --- !
238         CALL iom_get( numrir, jpdom_autoglo, 'v_i'  , v_i, ldxios = lrxios   )
239         CALL iom_get( numrir, jpdom_autoglo, 'v_s'  , v_s, ldxios = lrxios   )
240         CALL iom_get( numrir, jpdom_autoglo, 'sv_i' , sv_i, ldxios = lrxios  )
241         CALL iom_get( numrir, jpdom_autoglo, 'a_i'  , a_i, ldxios = lrxios   )
242         CALL iom_get( numrir, jpdom_autoglo, 't_su' , t_su, ldxios = lrxios  )
243         CALL iom_get( numrir, jpdom_autoglo, 'u_ice', u_ice, ldxios = lrxios )
244         CALL iom_get( numrir, jpdom_autoglo, 'v_ice', v_ice, ldxios = lrxios )
245         ! Snow enthalpy
246         IF(lrsi3_nemo) THEN
247!NEMO read inerface
248            DO jk = 1, nlay_s
249               WRITE(zchar1,'(I2.2)') jk
250               znam = 'e_s'//'_l'//zchar1
251               CALL iom_get( numrir, jpdom_autoglo, znam , z3d, ldxios = lrxios  )
252               e_s(:,:,jk,:) = z3d(:,:,:)
253            END DO
254            ! Ice enthalpy
255            DO jk = 1, nlay_i
256               WRITE(zchar1,'(I2.2)') jk
257               znam = 'e_i'//'_l'//zchar1
258               CALL iom_get( numrir, jpdom_autoglo, znam , z3d, ldxios = lrxios  )
259               e_i(:,:,jk,:) = z3d(:,:,:)
260            END DO
261         ELSE
262!XIOS read interface
263            CALL iom_get( numrir, jpdom_autoglo, "e_s" , e_s, ldxios = lrxios  )
264            CALL iom_get( numrir, jpdom_autoglo, "e_i" , e_i, ldxios = lrxios  ) 
265         ENDIF
266         ! -- optional fields -- !
267         ! ice age
268         id1 = iom_varid( numrir, 'oa_i' , ldstop = .FALSE. )
269         IF( id1 > 0 ) THEN                       ! fields exist
270            CALL iom_get( numrir, jpdom_autoglo, 'oa_i', oa_i, ldxios = lrxios )
271         ELSE                                     ! start from rest
272            IF(lwp) WRITE(numout,*) '   ==>>   previous run without ice age output then set it to zero'
273            oa_i(:,:,:) = 0._wp
274         ENDIF
275         ! melt ponds
276         id2 = iom_varid( numrir, 'a_ip' , ldstop = .FALSE. )
277         IF( id2 > 0 ) THEN                       ! fields exist
278            CALL iom_get( numrir, jpdom_autoglo, 'a_ip' , a_ip, ldxios = lrxios )
279            CALL iom_get( numrir, jpdom_autoglo, 'v_ip' , v_ip, ldxios = lrxios )
280         ELSE                                     ! start from rest
281            IF(lwp) WRITE(numout,*) '   ==>>   previous run without melt ponds output then set it to zero'
282            a_ip(:,:,:) = 0._wp
283            v_ip(:,:,:) = 0._wp
284         ENDIF
285         ! fields needed for Met Office (Jules) coupling
286         IF( ln_cpl ) THEN
287            id3 = iom_varid( numrir, 'cnd_ice' , ldstop = .FALSE. )
288            id4 = iom_varid( numrir, 't1_ice'  , ldstop = .FALSE. )
289            IF( id3 > 0 .AND. id4 > 0 ) THEN         ! fields exist
290               CALL iom_get( numrir, jpdom_autoglo, 'cnd_ice', cnd_ice, ldxios = lrxios )
291               CALL iom_get( numrir, jpdom_autoglo, 't1_ice' , t1_ice, ldxios = lrxios  )
292            ELSE                                     ! start from rest
293               IF(lwp) WRITE(numout,*) '   ==>>   previous run without conductivity output then set it to zero'
294               cnd_ice(:,:,:) = 0._wp
295               t1_ice (:,:,:) = rt0
296            ENDIF
297         ENDIF
298         IF(lrxios) CALL iom_swap( TRIM(cxios_context) )
299         CALL iom_delay_rst( 'READ', 'ICE', numrir )   ! read only ice delayed global communication variables
300
301         !                 ! ---------------------------------- !
302      ELSE                 ! == case of a simplified restart == !
303         !                 ! ---------------------------------- !
304         CALL ctl_warn('ice_rst_read: you are using a simplified ice restart')
305         !
306         CALL ice_istate_init
307         CALL ice_istate( nit000 )
308         !
309         IF( .NOT.ln_iceini .OR. .NOT.ln_iceini_file ) &
310            &   CALL ctl_stop('STOP', 'ice_rst_read: you need ln_ice_ini=T and ln_iceini_file=T')
311         !
312      ENDIF
313
314   END SUBROUTINE ice_rst_read
315
316#else
317   !!----------------------------------------------------------------------
318   !!   Default option :       Empty module           NO SI3 sea-ice model
319   !!----------------------------------------------------------------------
320#endif
321
322   !!======================================================================
323END MODULE icerst
Note: See TracBrowser for help on using the repository browser.