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.
restart.F90 in branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM – NEMO

source: branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/restart.F90 @ 8079

Last change on this file since 8079 was 8079, checked in by andmirek, 7 years ago

#1882 a first working version with XIOS writing restart file. Works with MO suite u-am389

File size: 18.3 KB
Line 
1MODULE restart
2   !!======================================================================
3   !!                     ***  MODULE  restart  ***
4   !! Ocean restart :  write the ocean restart file
5   !!======================================================================
6   !! History :  OPA  !  1999-11  (M. Imbard)  Original code
7   !!   NEMO     1.0  !  2002-08  (G. Madec)  F90: Free form
8   !!            2.0  !  2006-07  (S. Masson)  use IOM for restart
9   !!            3.3  !  2010-04  (M. Leclair, G. Madec)  modified LF-RA
10   !!            - -  !  2010-10  (C. Ethe, G. Madec) TRC-TRA merge (T-S in 4D)
11   !!----------------------------------------------------------------------
12
13   !!----------------------------------------------------------------------
14   !!   rst_opn    : open the ocean restart file
15   !!   rst_write  : write the ocean restart file
16   !!   rst_read   : read the ocean restart file
17   !!----------------------------------------------------------------------
18   USE oce             ! ocean dynamics and tracers
19   USE dom_oce         ! ocean space and time domain
20   USE phycst          ! physical constants
21   USE in_out_manager  ! I/O manager
22   USE iom             ! I/O module
23   USE ioipsl, ONLY : ju2ymds    ! for calendar
24   USE eosbn2          ! equation of state            (eos bn2 routine)
25   USE trdmxl_oce      ! ocean active mixed layer tracers trends variables
26   USE divcur          ! hor. divergence and curl      (div & cur routines)
27   USE sbc_oce         ! for icesheet freshwater input variables
28   USE iom_def, ONLY : lwxios
29
30   IMPLICIT NONE
31   PRIVATE
32
33   PUBLIC   rst_opn         ! routine called by step module
34   PUBLIC   rst_write       ! routine called by step module
35   PUBLIC   rst_read        ! routine called by istate module
36   PUBLIC   rst_read_open   ! routine called in rst_read and (possibly) in dom_vvl_init
37
38   !! * Substitutions
39#  include "domzgr_substitute.h90"
40#  include "vectopt_loop_substitute.h90"
41   !!----------------------------------------------------------------------
42   !! NEMO/OPA 3.3 , NEMO Consortium (2010)
43   !! $Id$
44   !! Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
45   !!----------------------------------------------------------------------
46CONTAINS
47
48   SUBROUTINE rst_opn( kt )
49      !!---------------------------------------------------------------------
50      !!                   ***  ROUTINE rst_opn  ***
51      !!                     
52      !! ** Purpose : + initialization (should be read in the namelist) of nitrst
53      !!              + open the restart when we are one time step before nitrst
54      !!                   - restart header is defined when kt = nitrst-1
55      !!                   - restart data  are written when kt = nitrst
56      !!              + define lrst_oce to .TRUE. when we need to define or write the restart
57      !!----------------------------------------------------------------------
58      INTEGER, INTENT(in) ::   kt     ! ocean time-step
59      INTEGER             ::   iyear, imonth, iday
60      REAL (wp)           ::   zsec
61      REAL (wp)           ::   zfjulday
62      !!
63      CHARACTER(LEN=20)   ::   clkt     ! ocean time-step deine as a character
64      CHARACTER(LEN=50)   ::   clname   ! ocean output restart file name
65      CHARACTER(LEN=150)  ::   clpath   ! full path to ocean output restart file
66      !!----------------------------------------------------------------------
67      !
68
69      IF( kt == nit000 ) THEN   ! default definitions
70         lrst_oce = .FALSE.   
71         IF( ln_rst_list ) THEN
72            nrst_lst = 1
73            nitrst = nstocklist( nrst_lst )
74         ELSE
75            nitrst = nitend
76         ENDIF
77      ENDIF
78
79      ! frequency-based restart dumping (nn_stock)
80      IF( .NOT. ln_rst_list .AND. MOD( kt - 1, nstock ) == 0 ) THEN   
81         ! we use kt - 1 and not kt - nit000 to keep the same periodicity from the beginning of the experiment
82         nitrst = kt + nstock - 1                  ! define the next value of nitrst for restart writing
83         IF( nitrst > nitend )   nitrst = nitend   ! make sure we write a restart at the end of the run
84      ENDIF
85      ! to get better performances with NetCDF format:
86      ! we open and define the ocean restart file one time step before writing the data (-> at nitrst - 1)
87      ! except if we write ocean restart files every time step or if an ocean restart file was writen at nitend - 1
88      ! NO, because the data is written 2 times on nitrst - 1 and nitrst
89!     IF( kt == nitrst - 1 .OR. nstock == 1 .OR. ( kt == nitend .AND. .NOT. lrst_oce ) ) THEN
90      IF( kt == nitrst .OR. nstock == 1 ) THEN
91         IF( nitrst <= nitend .AND. nitrst > 0 ) THEN
92            IF ( ln_rstdate ) THEN
93               zfjulday = fjulday + rdttra(1) / rday
94               IF( ABS(zfjulday - REAL(NINT(zfjulday),wp)) < 0.1 / rday )   zfjulday = REAL(NINT(zfjulday),wp)   ! avoid truncation error
95               CALL ju2ymds( zfjulday, iyear, imonth, iday, zsec )           
96               WRITE(clkt, '(i4.4,2i2.2)') iyear, imonth, iday
97            ELSE
98               ! beware of the format used to write kt (default is i8.8, that should be large enough...)
99               IF( nitrst > 999999999 ) THEN   ;   WRITE(clkt, *       ) nitrst
100               ELSE                            ;   WRITE(clkt, '(i8.8)') nitrst
101               ENDIF
102            ENDIF
103            ! create the file
104            clname = TRIM(cexper)//"_"//TRIM(ADJUSTL(clkt))//"_"//TRIM(cn_ocerst_out)
105            clpath = TRIM(cn_ocerst_outdir)
106            IF( clpath(LEN_TRIM(clpath):) /= '/' ) clpath = TRIM(clpath) // '/'
107            IF(lwp) THEN
108               WRITE(numout,*)
109               IF(lwxios) THEN
110                  WRITE(numout,*)                                                     &
111                      '        XIOS open ocean restart NetCDF file: ',TRIM(clpath)//TRIM(clname)
112               ELSE
113                  SELECT CASE ( jprstlib )
114                  CASE ( jprstdimg )   ;   WRITE(numout,*)                            &
115                      '             open ocean restart binary file: ',TRIM(clpath)//TRIM(clname)
116                  CASE DEFAULT         ;   WRITE(numout,*)                            &
117                      '             open ocean restart NetCDF file: ',TRIM(clpath)//TRIM(clname)
118                  END SELECT
119                  IF ( snc4set%luse )      WRITE(numout,*) '             opened for NetCDF4 chunking and compression'
120                  IF( kt == nitrst - 1 ) THEN   ;   WRITE(numout,*) '             kt = nitrst - 1 = ', kt
121                  ELSE                          ;   WRITE(numout,*) '             kt = '             , kt
122                  ENDIF
123               ENDIF
124            ENDIF
125            !
126            IF(.NOT.lwxios) THEN
127               CALL iom_open( TRIM(clpath)//TRIM(clname), numrow, ldwrt = .TRUE., kiolib = jprstlib )
128            ELSE
129               CALL iom_init( wxios_context, TRIM(clpath)//TRIM(clname) )
130               CALL xios_update_calendar(nitrst)
131               CALL iom_swap(      cxios_context          )
132            ENDIF
133!HAVE TO set lrst_oce to FALSE after XIOS restart is written !!!!!!
134!           THIS CALL SHOULDN't be here we open restart file on iter-1, move it
135!           to SUBROUTINE day to set the flag on the step when we save data
136            lrst_oce = .TRUE.
137         ENDIF
138      ENDIF
139
140      !
141   END SUBROUTINE rst_opn
142
143
144   SUBROUTINE rst_write( kt )
145      !!---------------------------------------------------------------------
146      !!                   ***  ROUTINE rstwrite  ***
147      !!                     
148      !! ** Purpose :   Write restart fields in the format corresponding to jprstlib
149      !!
150      !! ** Method  :   Write in numrow when kt == nitrst in NetCDF
151      !!              file, save fields which are necessary for restart
152      !!----------------------------------------------------------------------
153      INTEGER, INTENT(in) ::   kt   ! ocean time-step
154      !!----------------------------------------------------------------------
155                     IF(lwxios) CALL iom_swap(      wxios_context          )
156                     CALL iom_rstput( kt, nitrst, numrow, 'rdt'    , rdt       , lxios = lwxios)   ! dynamics time step
157                     CALL iom_rstput( kt, nitrst, numrow, 'rdttra1', rdttra(1) , lxios = lwxios)   ! surface tracer time step
158
159                     CALL iom_rstput( kt, nitrst, numrow, 'ub'     , ub        , lxios = lwxios)     ! before fields
160                     CALL iom_rstput( kt, nitrst, numrow, 'vb'     , vb        , lxios = lwxios)
161                     CALL iom_rstput( kt, nitrst, numrow, 'tb'     , tsb(:,:,:,jp_tem) , lxios = lwxios)
162                     CALL iom_rstput( kt, nitrst, numrow, 'sb'     , tsb(:,:,:,jp_sal) , lxios = lwxios)
163                     CALL iom_rstput( kt, nitrst, numrow, 'rotb'   , rotb      , lxios = lwxios)
164                     CALL iom_rstput( kt, nitrst, numrow, 'hdivb'  , hdivb     , lxios = lwxios)
165                     CALL iom_rstput( kt, nitrst, numrow, 'sshb'   , sshb      , lxios = lwxios)
166                     !
167                     CALL iom_rstput( kt, nitrst, numrow, 'un'     , un        , lxios = lwxios)     ! now fields
168                     CALL iom_rstput( kt, nitrst, numrow, 'vn'     , vn        , lxios = lwxios)
169                     CALL iom_rstput( kt, nitrst, numrow, 'tn'     , tsn(:,:,:,jp_tem) , lxios = lwxios)
170                     CALL iom_rstput( kt, nitrst, numrow, 'sn'     , tsn(:,:,:,jp_sal) , lxios = lwxios)
171                     CALL iom_rstput( kt, nitrst, numrow, 'rotn'   , rotn      , lxios = lwxios)
172                     CALL iom_rstput( kt, nitrst, numrow, 'hdivn'  , hdivn     , lxios = lwxios)
173                     CALL iom_rstput( kt, nitrst, numrow, 'sshn'   , sshn      , lxios = lwxios)
174                     CALL iom_rstput( kt, nitrst, numrow, 'rhop'   , rhop      , lxios = lwxios)
175#if defined key_zdfkpp
176                     CALL iom_rstput( kt, nitrst, numrow, 'rhd'    , rhd       , lxios = lwxios)
177#endif
178                     IF( lk_oasis) THEN
179                     ! ln_coupled_iceshelf_fluxes uninitialised unless lk_oasis=true
180                     IF( ln_coupled_iceshelf_fluxes ) THEN
181                        CALL iom_rstput( kt, nitrst, numrow, 'greenland_icesheet_mass', greenland_icesheet_mass ,&
182      &                                   lxios = lwxios)
183                        CALL iom_rstput( kt, nitrst, numrow, 'greenland_icesheet_timelapsed', greenland_icesheet_timelapsed ,&
184      &                                   lxios = lwxios)
185                        CALL iom_rstput( kt, nitrst, numrow, 'greenland_icesheet_mass_roc', greenland_icesheet_mass_rate_of_change ,&
186      &                                   lxios = lwxios)
187                        CALL iom_rstput( kt, nitrst, numrow, 'antarctica_icesheet_mass', antarctica_icesheet_mass , &
188      &                                  lxios = lwxios)
189                        CALL iom_rstput( kt, nitrst, numrow, 'antarctica_icesheet_timelapsed', antarctica_icesheet_timelapsed ,&
190      &                                   lxios = lwxios)
191                        CALL iom_rstput( kt, nitrst, numrow, 'antarctica_icesheet_mass_roc', &
192      &                                  antarctica_icesheet_mass_rate_of_change , lxios = lwxios)
193                     ENDIF
194                     ENDIF
195
196      IF( kt == nitrst ) THEN
197         IF(.NOT.lwxios) THEN
198            CALL iom_close( numrow )     ! close the restart file (only at last time step)
199         ELSE
200            CALL iom_context_finalize(      wxios_context          )
201            CALL iom_swap(      cxios_context          )
202         ENDIF
203!!gm         IF( .NOT. lk_trdmld )   lrst_oce = .FALSE.
204!!gm  not sure what to do here   ===>>>  ask to Sebastian
205         lrst_oce = .FALSE.
206            IF( ln_rst_list ) THEN
207               nrst_lst = MIN(nrst_lst + 1, SIZE(nstocklist,1))
208               nitrst = nstocklist( nrst_lst )
209            ENDIF
210      ENDIF
211      !
212   END SUBROUTINE rst_write
213
214
215   SUBROUTINE rst_read_open
216      !!----------------------------------------------------------------------
217      !!                   ***  ROUTINE rst_read_open  ***
218      !!
219      !! ** Purpose :   Open read files for restart (format fixed by jprstlib )
220      !!
221      !! ** Method  :   Use a non-zero, positive value of numror to assess whether or not
222      !!                the file has already been opened
223      !!----------------------------------------------------------------------
224      INTEGER        ::   jlibalt = jprstlib
225      LOGICAL        ::   llok
226      CHARACTER(lc)  ::   clpath   ! full path to ocean output restart file
227      !!----------------------------------------------------------------------
228      !
229      IF( numror <= 0 ) THEN
230         IF(lwp) THEN                                             ! Contol prints
231            WRITE(numout,*)
232            SELECT CASE ( jprstlib )
233            CASE ( jpnf90    )   ;   WRITE(numout,*) 'rst_read : read oce NetCDF restart file'
234            CASE ( jprstdimg )   ;   WRITE(numout,*) 'rst_read : read oce binary restart file'
235            END SELECT
236            IF ( snc4set%luse )      WRITE(numout,*) 'rst_read : configured with NetCDF4 support'
237            WRITE(numout,*) '~~~~~~~~'
238         ENDIF
239
240         clpath = TRIM(cn_ocerst_indir)
241         IF( clpath(LEN_TRIM(clpath):) /= '/' ) clpath = TRIM(clpath) // '/'
242         IF ( jprstlib == jprstdimg ) THEN
243           ! eventually read netcdf file (monobloc)  for restarting on different number of processors
244           ! if {cn_ocerst_in}.nc exists, then set jlibalt to jpnf90
245           INQUIRE( FILE = TRIM(cn_ocerst_indir)//'/'//TRIM(cn_ocerst_in)//'.nc', EXIST = llok )
246           IF ( llok ) THEN ; jlibalt = jpnf90  ; ELSE ; jlibalt = jprstlib ; ENDIF
247         ENDIF
248         CALL iom_open( TRIM(clpath)//cn_ocerst_in, numror, kiolib = jlibalt )
249      ENDIF
250   END SUBROUTINE rst_read_open
251
252   SUBROUTINE rst_read
253      !!----------------------------------------------------------------------
254      !!                   ***  ROUTINE rst_read  ***
255      !!
256      !! ** Purpose :   Read files for restart (format fixed by jprstlib )
257      !!
258      !! ** Method  :   Read in restart.nc file fields which are necessary for restart
259      !!----------------------------------------------------------------------
260      REAL(wp) ::   zrdt, zrdttra1
261      INTEGER  ::   jk
262      LOGICAL  ::   llok
263      !!----------------------------------------------------------------------
264
265      CALL rst_read_open           ! open restart for reading (if not already opened)
266
267      ! Check dynamics and tracer time-step consistency and force Euler restart if changed
268      IF( iom_varid( numror, 'rdt', ldstop = .FALSE. ) > 0 )   THEN
269         CALL iom_get( numror, 'rdt', zrdt )
270         IF( zrdt /= rdt )   neuler = 0
271      ENDIF
272      IF( iom_varid( numror, 'rdttra1', ldstop = .FALSE. ) > 0 )   THEN
273         CALL iom_get( numror, 'rdttra1', zrdttra1 )
274         IF( zrdttra1 /= rdttra(1) )   neuler = 0
275      ENDIF
276      !
277      IF( iom_varid( numror, 'ub', ldstop = .FALSE. ) > 0 ) THEN
278         CALL iom_get( numror, jpdom_autoglo, 'ub'     , ub      )   ! before fields
279         CALL iom_get( numror, jpdom_autoglo, 'vb'     , vb      )
280         CALL iom_get( numror, jpdom_autoglo, 'tb'     , tsb(:,:,:,jp_tem) )
281         CALL iom_get( numror, jpdom_autoglo, 'sb'     , tsb(:,:,:,jp_sal) )
282         CALL iom_get( numror, jpdom_autoglo, 'rotb'   , rotb    )
283         CALL iom_get( numror, jpdom_autoglo, 'hdivb'  , hdivb   )
284         CALL iom_get( numror, jpdom_autoglo, 'sshb'   , sshb    )
285      ELSE
286         neuler = 0
287      ENDIF
288      !
289      CALL iom_get( numror, jpdom_autoglo, 'un'     , un      )   ! now    fields
290      CALL iom_get( numror, jpdom_autoglo, 'vn'     , vn      )
291      CALL iom_get( numror, jpdom_autoglo, 'tn'     , tsn(:,:,:,jp_tem) )
292      CALL iom_get( numror, jpdom_autoglo, 'sn'     , tsn(:,:,:,jp_sal) )
293      CALL iom_get( numror, jpdom_autoglo, 'sshn'   , sshn    )
294      IF( iom_varid( numror, 'rotn', ldstop = .FALSE. ) > 0 ) THEN
295         CALL iom_get( numror, jpdom_autoglo, 'rotn'   , rotn    )
296         CALL iom_get( numror, jpdom_autoglo, 'hdivn'  , hdivn   )
297      ELSE
298         CALL div_cur( 0 )                              ! Horizontal divergence & Relative vorticity
299      ENDIF
300      IF( iom_varid( numror, 'rhop', ldstop = .FALSE. ) > 0 ) THEN
301         CALL iom_get( numror, jpdom_autoglo, 'rhop'   , rhop    )   ! now    potential density
302      ELSE
303         CALL eos    ( tsn, rhd, rhop, fsdept_n(:,:,:) )   
304      ENDIF
305#if defined key_zdfkpp
306      IF( iom_varid( numror, 'rhd', ldstop = .FALSE. ) > 0 ) THEN
307         CALL iom_get( numror, jpdom_autoglo, 'rhd'    , rhd     )   ! now    in situ density anomaly
308      ELSE
309         CALL eos( tsn, rhd, fsdept_n(:,:,:) )   ! compute rhd
310      ENDIF
311#endif
312      !
313      IF( iom_varid( numror, 'greenland_icesheet_mass', ldstop = .FALSE. ) > 0 )   THEN
314         CALL iom_get( numror, 'greenland_icesheet_mass', greenland_icesheet_mass )
315         CALL iom_get( numror, 'greenland_icesheet_timelapsed', greenland_icesheet_timelapsed )
316         CALL iom_get( numror, 'greenland_icesheet_mass_roc', greenland_icesheet_mass_rate_of_change )
317      ELSE
318         greenland_icesheet_mass = 0.0 
319         greenland_icesheet_mass_rate_of_change = 0.0 
320         greenland_icesheet_timelapsed = 0.0
321      ENDIF
322      IF( iom_varid( numror, 'antarctica_icesheet_mass', ldstop = .FALSE. ) > 0 )   THEN
323         CALL iom_get( numror, 'antarctica_icesheet_mass', antarctica_icesheet_mass )
324         CALL iom_get( numror, 'antarctica_icesheet_timelapsed', antarctica_icesheet_timelapsed )
325         CALL iom_get( numror, 'antarctica_icesheet_mass_roc', antarctica_icesheet_mass_rate_of_change )
326      ELSE
327         antarctica_icesheet_mass = 0.0 
328         antarctica_icesheet_mass_rate_of_change = 0.0 
329         antarctica_icesheet_timelapsed = 0.0
330      ENDIF
331
332      IF( neuler == 0 ) THEN                                  ! Euler restart (neuler=0)
333         tsb  (:,:,:,:) = tsn  (:,:,:,:)                             ! all before fields set to now values
334         ub   (:,:,:)   = un   (:,:,:)
335         vb   (:,:,:)   = vn   (:,:,:)
336         rotb (:,:,:)   = rotn (:,:,:)
337         hdivb(:,:,:)   = hdivn(:,:,:)
338         sshb (:,:)     = sshn (:,:)
339
340         IF( lk_vvl ) THEN
341            DO jk = 1, jpk
342               fse3t_b(:,:,jk) = fse3t_n(:,:,jk)
343            END DO
344         ENDIF
345
346      ENDIF
347      !
348   END SUBROUTINE rst_read
349   !!=====================================================================
350END MODULE restart
Note: See TracBrowser for help on using the repository browser.