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/2020/dev_12905_xios_restart/src/ICE – NEMO

source: NEMO/branches/2020/dev_12905_xios_restart/src/ICE/icerst.F90 @ 12957

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

Ticket #2462: write/read SI3 restart with XIOS (has debug print statements)

  • Property svn:keywords set to Id
File size: 15.4 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(.NOT.lwxios) THEN
84               CALL iom_open( TRIM(clpath)//TRIM(clname), numriw, ldwrt = .TRUE., kdlev = jpl, cdcomp = 'ICE' )
85            ELSE
86#if defined key_iomput
87               cwixios_context = "rstwi_"//TRIM(ADJUSTL(clkt))
88               IF( TRIM(Agrif_CFixed()) == '0' ) THEN
89                  clpname = clname
90               ELSE
91                  clpname = TRIM(Agrif_CFixed())//"_"//clname
92               ENDIF
93               CALL iom_init( cwixios_context, TRIM(clpath)//TRIM(clpname), .false., ld_closedef = .FALSE. )
94               CALL iom_swap(      cxios_context          )
95#else
96               clinfo = 'Can not use XIOS in rst_opn'
97               CALL ctl_stop(TRIM(clinfo))
98#endif
99            ENDIF
100            lrst_ice = .TRUE.
101         ENDIF
102      ENDIF
103      ENDIF
104      !
105      IF( ln_icectl )   CALL ice_prt( kt, iiceprt, jiceprt, 1, ' - Beginning the time step - ' )   ! control print
106      !
107   END SUBROUTINE ice_rst_opn
108
109
110   SUBROUTINE ice_rst_write( kt )
111      !!----------------------------------------------------------------------
112      !!                    ***  ice_rst_write  ***
113      !!
114      !! ** purpose  :   write restart file
115      !!----------------------------------------------------------------------
116      INTEGER, INTENT(in) ::   kt     ! number of iteration
117      !!
118      INTEGER ::   jk    ! dummy loop indices
119      INTEGER ::   iter
120      CHARACTER(len=25) ::   znam
121      CHARACTER(len=2)  ::   zchar, zchar1
122      REAL(wp), DIMENSION(jpi,jpj,jpl) ::   z3d   ! 3D workspace
123      !!----------------------------------------------------------------------
124
125      iter = kt + nn_fsbc - 1   ! ice restarts are written at kt == nitrst - nn_fsbc + 1
126
127      IF( iter == nitrst ) THEN
128         IF(lwp) WRITE(numout,*)
129         IF(lwp) WRITE(numout,*) 'ice_rst_write : write ice restart file  kt =', kt
130         IF(lwp) WRITE(numout,*) '~~~~~~~~~~~~~~'         
131      ENDIF
132
133      ! Write in numriw (if iter == nitrst)
134      ! ------------------
135      !                                                                        ! calendar control
136      IF( lwxios ) CALL iom_swap(      cwixios_context         )
137      CALL iom_rstput( iter, nitrst, numriw, 'nn_fsbc', REAL( nn_fsbc, wp ), ldxios = lwxios )      ! time-step
138      CALL iom_rstput( iter, nitrst, numriw, 'kt_ice' , REAL( iter   , wp ), ldxios = lwxios )      ! date
139      CALL iom_delay_rst( 'WRITE', 'ICE', numriw )   ! save only ice delayed global communication variables
140
141      ! Prognostic variables
142      CALL iom_rstput( iter, nitrst, numriw, 'v_i'  , v_i,   ldxios = lwxios )
143      CALL iom_rstput( iter, nitrst, numriw, 'v_s'  , v_s,   ldxios = lwxios )
144      CALL iom_rstput( iter, nitrst, numriw, 'sv_i' , sv_i,  ldxios = lwxios )
145      CALL iom_rstput( iter, nitrst, numriw, 'a_i'  , a_i,   ldxios = lwxios )
146      CALL iom_rstput( iter, nitrst, numriw, 't_su' , t_su,  ldxios = lwxios )
147      CALL iom_rstput( iter, nitrst, numriw, 'u_ice', u_ice, ldxios = lwxios )
148      CALL iom_rstput( iter, nitrst, numriw, 'v_ice', v_ice, ldxios = lwxios )
149      CALL iom_rstput( iter, nitrst, numriw, 'oa_i' , oa_i,  ldxios = lwxios )
150      CALL iom_rstput( iter, nitrst, numriw, 'a_ip' , a_ip,  ldxios = lwxios )
151      CALL iom_rstput( iter, nitrst, numriw, 'v_ip' , v_ip,  ldxios = lwxios )
152      ! Snow enthalpy
153      DO jk = 1, nlay_s 
154         WRITE(zchar1,'(I2.2)') jk
155         znam = 'e_s'//'_l'//zchar1
156         z3d(:,:,:) = e_s(:,:,jk,:)
157         CALL iom_rstput( iter, nitrst, numriw, znam , z3d, ldxios = lwxios )
158      END DO
159      ! Ice enthalpy
160      DO jk = 1, nlay_i 
161         WRITE(zchar1,'(I2.2)') jk
162         znam = 'e_i'//'_l'//zchar1
163         z3d(:,:,:) = e_i(:,:,jk,:)
164         CALL iom_rstput( iter, nitrst, numriw, znam , z3d, ldxios = lwxios )
165      END DO
166      ! fields needed for Met Office (Jules) coupling
167      IF( ln_cpl ) THEN
168         CALL iom_rstput( iter, nitrst, numriw, 'cnd_ice', cnd_ice, ldxios = lwxios )
169         CALL iom_rstput( iter, nitrst, numriw, 't1_ice' , t1_ice,  ldxios = lwxios )
170      ENDIF
171      IF( lwxios ) CALL iom_swap(      cxios_context         )
172      !
173
174      ! close restart file
175      ! ------------------
176      IF( iter == nitrst ) THEN
177         IF(.NOT.lwxios) THEN
178            CALL iom_close( numriw )
179         ELSE
180            CALL iom_context_finalize(      cwixios_context          )
181         ENDIF
182         lrst_ice = .FALSE.
183      ENDIF
184      !
185   END SUBROUTINE ice_rst_write
186
187
188   SUBROUTINE ice_rst_read( Kbb, Kmm, Kaa )
189      !!----------------------------------------------------------------------
190      !!                    ***  ice_rst_read  ***
191      !!
192      !! ** purpose  :   read restart file
193      !!----------------------------------------------------------------------
194      INTEGER, INTENT(in) :: Kbb, Kmm, Kaa ! ocean time level indices
195      INTEGER           ::   jk
196      LOGICAL           ::   llok
197      INTEGER           ::   id0, id1, id2, id3, id4   ! local integer
198      CHARACTER(len=25) ::   znam
199      CHARACTER(len=2)  ::   zchar, zchar1
200      REAL(wp)          ::   zfice, ziter
201      CHARACTER(lc)     ::   clpname
202      REAL(wp), DIMENSION(jpi,jpj,jpl) ::   z3d   ! 3D workspace
203      !!----------------------------------------------------------------------
204
205      IF(lwp) THEN
206         WRITE(numout,*)
207         WRITE(numout,*) 'ice_rst_read: read ice NetCDF restart file'
208         WRITE(numout,*) '~~~~~~~~~~~~'
209      ENDIF
210
211      lxios_sini = .FALSE.
212      CALL iom_open ( TRIM(cn_icerst_indir)//'/'//cn_icerst_in, numrir )
213
214      IF( lrxios .AND. .NOT. lxios_sini) STOP 'OCE and ICE restart must be in a single file'
215      IF(lwp) write(numout, *) 'USE XIOS to read restart: ', lrxios
216      IF(lwp) CALL FLUSH(numout)
217      IF( lrxios) THEN
218          crixios_context = 'si3_rst'
219          IF(lwp) WRITE(numout,*) 'Enable restart reading by XIOS for SI3'
220          IF( TRIM(Agrif_CFixed()) == '0' ) THEN
221             clpname = cn_icerst_in
222          ELSE
223             clpname = TRIM(Agrif_CFixed())//"_"//cn_icerst_in   
224          ENDIF
225          CALL iom_init( crixios_context, fname = TRIM(cn_icerst_indir)//'/'//TRIM(clpname), ld_tmppatch = .TRUE. )
226      ENDIF
227
228      ! test if v_i exists
229      id0 = iom_varid( numrir, 'v_i' , ldstop = .FALSE. )
230
231      !                    ! ------------------------------ !
232      IF( id0 > 0 ) THEN   ! == case of a normal restart == !
233         !                 ! ------------------------------ !
234         IF(lrxios) CALL iom_swap(crixios_context) 
235         ! Time info
236         CALL iom_get( numrir, 'nn_fsbc', zfice, ldxios = lrxios )
237         CALL iom_get( numrir, 'kt_ice' , ziter, ldxios = lrxios )   
238         IF(lwp) WRITE(numout,*) '   read ice restart file at time step    : ', ziter
239         IF(lwp) WRITE(numout,*) '   in any case we force it to nit000 - 1 : ', nit000 - 1
240
241         ! Control of date
242         IF( ( nit000 - NINT(ziter) ) /= 1 .AND. ABS( nrstdt ) == 1 )   &
243            &     CALL ctl_stop( 'ice_rst_read ===>>>> : problem with nit000 in ice restart',  &
244            &                   '   verify the file or rerun with the value 0 for the',        &
245            &                   '   control of time parameter  nrstdt' )
246         IF( NINT(zfice) /= nn_fsbc          .AND. ABS( nrstdt ) == 1 )   &
247            &     CALL ctl_stop( 'ice_rst_read ===>>>> : problem with nn_fsbc in ice restart',  &
248            &                   '   verify the file or rerun with the value 0 for the',         &
249            &                   '   control of time parameter  nrstdt' )
250
251         ! --- mandatory fields --- !
252         CALL iom_get( numrir, jpdom_autoglo, 'v_i'  , v_i,   ldxios = lrxios )
253         CALL iom_get( numrir, jpdom_autoglo, 'v_s'  , v_s,   ldxios = lrxios )
254         CALL iom_get( numrir, jpdom_autoglo, 'sv_i' , sv_i,  ldxios = lrxios )
255         CALL iom_get( numrir, jpdom_autoglo, 'a_i'  , a_i,   ldxios = lrxios )
256         CALL iom_get( numrir, jpdom_autoglo, 't_su' , t_su,  ldxios = lrxios )
257         CALL iom_get( numrir, jpdom_autoglo, 'u_ice', u_ice, ldxios = lrxios )
258         CALL iom_get( numrir, jpdom_autoglo, 'v_ice', v_ice, ldxios = lrxios )
259         ! Snow enthalpy
260         DO jk = 1, nlay_s
261            WRITE(zchar1,'(I2.2)') jk
262            znam = 'e_s'//'_l'//zchar1
263            CALL iom_get( numrir, jpdom_autoglo, znam , z3d, ldxios = lrxios )
264            e_s(:,:,jk,:) = z3d(:,:,:)
265         END DO
266         ! Ice enthalpy
267         DO jk = 1, nlay_i
268            WRITE(zchar1,'(I2.2)') jk
269            znam = 'e_i'//'_l'//zchar1
270            CALL iom_get( numrir, jpdom_autoglo, znam , z3d, ldxios = lrxios )
271            e_i(:,:,jk,:) = z3d(:,:,:)
272         END DO
273         ! -- optional fields -- !
274         ! ice age
275         id1 = iom_varid( numrir, 'oa_i' , ldstop = .FALSE. )
276         IF( id1 > 0 ) THEN                       ! fields exist
277            CALL iom_get( numrir, jpdom_autoglo, 'oa_i', oa_i, ldxios = lrxios )
278         ELSE                                     ! start from rest
279            IF(lwp) WRITE(numout,*) '   ==>>   previous run without ice age output then set it to zero'
280            oa_i(:,:,:) = 0._wp
281         ENDIF
282         ! melt ponds
283         id2 = iom_varid( numrir, 'a_ip' , ldstop = .FALSE. )
284         IF( id2 > 0 ) THEN                       ! fields exist
285            CALL iom_get( numrir, jpdom_autoglo, 'a_ip' , a_ip, ldxios = lrxios )
286            CALL iom_get( numrir, jpdom_autoglo, 'v_ip' , v_ip, ldxios = lrxios )
287         ELSE                                     ! start from rest
288            IF(lwp) WRITE(numout,*) '   ==>>   previous run without melt ponds output then set it to zero'
289            a_ip(:,:,:) = 0._wp
290            v_ip(:,:,:) = 0._wp
291         ENDIF
292         ! fields needed for Met Office (Jules) coupling
293         IF( ln_cpl ) THEN
294            id3 = iom_varid( numrir, 'cnd_ice' , ldstop = .FALSE. )
295            id4 = iom_varid( numrir, 't1_ice'  , ldstop = .FALSE. )
296            IF( id3 > 0 .AND. id4 > 0 ) THEN         ! fields exist
297               CALL iom_get( numrir, jpdom_autoglo, 'cnd_ice', cnd_ice, ldxios = lrxios )
298               CALL iom_get( numrir, jpdom_autoglo, 't1_ice' , t1_ice,  ldxios = lrxios )
299            ELSE                                     ! start from rest
300               IF(lwp) WRITE(numout,*) '   ==>>   previous run without conductivity output then set it to zero'
301               cnd_ice(:,:,:) = 0._wp
302               t1_ice (:,:,:) = rt0
303            ENDIF
304         ENDIF
305
306         CALL iom_delay_rst( 'READ', 'ICE', numrir )   ! read only ice delayed global communication variables
307         IF(lrxios) CALL iom_swap(cxios_context)
308         !                 ! ---------------------------------- !
309      ELSE                 ! == case of a simplified restart == !
310         !                 ! ---------------------------------- !
311         CALL ctl_warn('ice_rst_read: you are using a simplified ice restart')
312         !
313         CALL ice_istate_init
314         CALL ice_istate( nit000, Kbb, Kmm, Kaa )
315         !
316         IF( .NOT.ln_iceini .OR. .NOT.ln_iceini_file ) &
317            &   CALL ctl_stop('STOP', 'ice_rst_read: you need ln_ice_ini=T and ln_iceini_file=T')
318         !
319      ENDIF
320
321   END SUBROUTINE ice_rst_read
322
323#else
324   !!----------------------------------------------------------------------
325   !!   Default option :       Empty module           NO SI3 sea-ice model
326   !!----------------------------------------------------------------------
327#endif
328
329   !!======================================================================
330END MODULE icerst
Note: See TracBrowser for help on using the repository browser.