source: trunk/LMDZ.COMMON/libf/evolution/iostart_PEM.F90 @ 3321

Last change on this file since 3321 was 3297, checked in by jbclement, 8 months ago

PEM:
Integration of the module "layering_mod.F90" with the rest of the PEM:

  • The linked list data structure representative of layered deposits is converted into an array which can be outputed in the "restratpem.nc" files. This array has dimensions (ngrid,nslope,nb_str_max,6) where 'nb_str_max' is the maximum number of 'stratum' through the layerings and '6' is the number of properties of 'stratum';
  • this structure can also be read from "startpem.nc" files to initialize PEM runs;
  • The layering algorithm is now used in the main PEM loop to make the layerings evolve.

JBC

File size: 37.7 KB
Line 
1MODULE iostart_PEM
2!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3!!!
4!!! Purpose: Read and write specific netcdf for the PEM
5!!!
6!!!
7!!! Author: LL, inspired by iostart from the PCM
8!!!
9!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10
11    IMPLICIT NONE
12PRIVATE
13    INTEGER,SAVE :: nid_start ! NetCDF file identifier for startfi.nc file
14    INTEGER,SAVE :: nid_restart ! NetCDF file identifier for restartfi.nc file
15
16    ! restartfi.nc file dimension identifiers: (see open_restartphy())
17    INTEGER,SAVE :: idim1  ! "index" dimension
18    INTEGER,SAVE :: idim2  ! "physical_points" dimension
19    INTEGER,SAVE :: idim3  ! "subsurface_layers" dimension
20    INTEGER,SAVE :: idim4  ! "nlayer_plus_1" dimension
21    INTEGER,SAVE :: idim5  ! "number_of_advected_fields" dimension
22    INTEGER,SAVE :: idim6  ! "nlayer" dimension
23    INTEGER,SAVE :: idim7  ! "Time" dimension
24    INTEGER,SAVE :: idim8  ! "nslope" dimension
25    INTEGER,SAVE :: idim9  ! "inter slope" dimension
26    INTEGER,SAVE :: idim10 ! "nb_str_max" dimension
27    INTEGER,SAVE :: timeindex ! current time index (for time-dependent fields)
28    INTEGER,PARAMETER :: length=100 ! size of tab_cntrl array
29
30    INTERFACE get_field
31      MODULE PROCEDURE Get_field_r1,Get_field_r2,Get_field_r3
32    END INTERFACE get_field
33
34    INTERFACE get_var
35      MODULE PROCEDURE get_var_r0,Get_var_r1,Get_var_r2,Get_var_r3
36    END INTERFACE get_var
37
38    INTERFACE put_field
39      MODULE PROCEDURE put_field_r1,put_field_r2,put_field_r3
40    END INTERFACE put_field
41
42    INTERFACE put_var
43      MODULE PROCEDURE put_var_r0,put_var_r1,put_var_r2,put_var_r3
44    END INTERFACE put_var
45
46    PUBLIC nid_start, length
47    PUBLIC get_field,get_var,put_field,put_var
48    PUBLIC inquire_dimension, inquire_dimension_length
49    PUBLIC inquire_field, inquire_field_ndims
50    PUBLIC open_startphy,close_startphy,open_restartphy,close_restartphy
51
52CONTAINS
53
54  SUBROUTINE open_startphy(filename)
55  USE netcdf, only: NF90_OPEN, NF90_NOERR, NF90_NOWRITE, nf90_strerror
56  USE mod_phys_lmdz_para, only: is_master, bcast
57  IMPLICIT NONE
58    CHARACTER(LEN=*) :: filename
59    INTEGER          :: ierr
60
61    IF (is_master) THEN
62      ierr = NF90_OPEN (filename, NF90_NOWRITE, nid_start)
63      IF (ierr.NE.NF90_NOERR) THEN
64        write(*,*)'open_startphy: problem opening file '//trim(filename)
65        write(*,*)trim(nf90_strerror(ierr))
66        CALL abort_physic("open_startphy","Cannot open file",1)
67      ENDIF
68    ENDIF
69
70    CALL bcast(nid_start) ! tell all procs about nid_start
71
72  END SUBROUTINE open_startphy
73
74  SUBROUTINE close_startphy
75  USE netcdf, only: NF90_CLOSE
76  USE mod_phys_lmdz_para, only: is_master
77  IMPLICIT NONE
78    INTEGER          :: ierr
79
80    IF (is_master) THEN
81        ierr = NF90_CLOSE (nid_start)
82    ENDIF
83
84  END SUBROUTINE close_startphy
85
86
87  FUNCTION inquire_field(Field_name)
88  ! check if a given field is present in the input file
89  USE netcdf, only: NF90_INQ_VARID, NF90_NOERR
90  USE mod_phys_lmdz_para, only: is_master, bcast
91  IMPLICIT NONE
92    CHARACTER(LEN=*),INTENT(IN) :: Field_name
93    LOGICAL :: inquire_field
94    INTEGER :: varid
95    INTEGER :: ierr
96
97    IF (is_master) THEN
98      ierr=NF90_INQ_VARID(nid_start,Field_name,varid)
99      IF (ierr==NF90_NOERR) THEN
100        Inquire_field=.TRUE.
101      ELSE
102        Inquire_field=.FALSE.
103      ENDIF
104    ENDIF
105
106    CALL bcast(inquire_field)
107
108  END FUNCTION inquire_field
109
110
111  FUNCTION inquire_field_ndims(Field_name)
112  ! give the number of dimensions of "Field_name" stored in the input file
113  USE netcdf, only: nf90_inq_varid, nf90_inquire_variable, &
114                    NF90_NOERR, nf90_strerror
115  USE mod_phys_lmdz_para, only: is_master, bcast
116  IMPLICIT NONE
117    CHARACTER(LEN=*),INTENT(IN) :: Field_name
118    INTEGER :: inquire_field_ndims
119    INTEGER :: varid
120    INTEGER :: ierr
121
122    IF (is_master) THEN
123      ierr=nf90_inq_varid(nid_start,Field_name,varid)
124      ierr=nf90_inquire_variable(nid_start,varid,&
125                                  ndims=inquire_field_ndims)
126      IF (ierr.NE.NF90_NOERR) THEN
127        write(*,*)'inquire_field_ndims: problem obtaining ndims of '&
128                  //trim(field_name)
129        write(*,*)trim(nf90_strerror(ierr))
130        CALL abort_physic("inquire_field_ndims","Failed to get ndims",1)
131      ENDIF
132    ENDIF
133
134    CALL bcast(inquire_field_ndims)
135
136  END FUNCTION inquire_field_ndims
137
138
139  FUNCTION inquire_dimension(Field_name)
140  ! check if a given dimension is present in the input file
141  USE netcdf, only: nf90_inq_dimid, NF90_NOERR
142  USE mod_phys_lmdz_para, only: is_master, bcast
143  IMPLICIT NONE
144    CHARACTER(LEN=*),INTENT(IN) :: Field_name
145    LOGICAL :: inquire_dimension
146    INTEGER :: varid
147    INTEGER :: ierr
148
149    IF (is_master) THEN
150      ierr=NF90_INQ_DIMID(nid_start,Field_name,varid)
151      IF (ierr==NF90_NOERR) THEN
152        Inquire_dimension=.TRUE.
153      ELSE
154        Inquire_dimension=.FALSE.
155      ENDIF
156    ENDIF
157
158    CALL bcast(inquire_dimension)
159
160  END FUNCTION inquire_dimension
161
162  FUNCTION inquire_dimension_length(Field_name)
163  ! give the length of the "Field_name" dimension stored in the input file
164  USE netcdf, only: nf90_inquire_dimension, nf90_inq_dimid, &
165                    NF90_NOERR, nf90_strerror
166  USE mod_phys_lmdz_para, only: is_master, bcast
167  IMPLICIT NONE
168    CHARACTER(LEN=*),INTENT(IN) :: Field_name
169    INTEGER :: inquire_dimension_length
170    INTEGER :: varid
171    INTEGER :: ierr
172
173    IF (is_master) THEN
174      ierr=nf90_inq_dimid(nid_start,Field_name,varid)
175      ierr=nf90_inquire_dimension(nid_start,varid,&
176                                  len=inquire_dimension_length)
177      IF (ierr.NE.NF90_NOERR) THEN
178        write(*,*)'inquire_field_length: problem obtaining length of '&
179                  //trim(field_name)
180        write(*,*)trim(nf90_strerror(ierr))
181        CALL abort_physic("inquire_field_ndims","Failed to get length",1)
182      ENDIF
183    ENDIF
184
185    CALL bcast(inquire_dimension_length)
186
187  END FUNCTION inquire_dimension_length
188
189
190
191  SUBROUTINE Get_Field_r1(field_name,field,found,timeindex)
192  ! For a surface field
193  use mod_grid_phy_lmdz, only: klon_glo ! number of atmospheric columns (full grid)
194  IMPLICIT NONE
195    CHARACTER(LEN=*),INTENT(IN)    :: Field_name
196    REAL,INTENT(INOUT)               :: Field(:)
197    LOGICAL,INTENT(OUT),OPTIONAL   :: found
198    INTEGER,INTENT(IN),OPTIONAL    :: timeindex ! time index of sought data
199
200    integer :: edges(4), corners(4)
201
202    edges(1)=klon_glo
203    edges(2:4)=1
204    corners(1:4)=1
205    if (PRESENT(timeindex)) then
206      corners(2)=timeindex
207    endif
208
209    IF (PRESENT(found)) THEN
210      CALL Get_field_rgen(field_name,field,1,corners,edges,found)
211    ELSE
212      CALL Get_field_rgen(field_name,field,1,corners,edges)
213    ENDIF
214
215  END SUBROUTINE Get_Field_r1
216
217  SUBROUTINE Get_Field_r2(field_name,field,found,timeindex)
218  ! For a "3D" horizontal-vertical field
219  use mod_grid_phy_lmdz, only: klon_glo ! number of atmospheric columns (full grid)
220  IMPLICIT NONE
221    CHARACTER(LEN=*),INTENT(IN)    :: Field_name
222    REAL,INTENT(INOUT)               :: Field(:,:)
223    LOGICAL,INTENT(OUT),OPTIONAL   :: found
224    INTEGER,INTENT(IN),OPTIONAL    :: timeindex ! time index of sought data
225
226    integer :: edges(4), corners(4)
227
228    edges(1)=klon_glo
229    edges(2)=size(field,2)
230    edges(3:4)=1
231    corners(1:4)=1
232    if (PRESENT(timeindex)) then
233      corners(3)=timeindex
234    endif
235
236    IF (PRESENT(found)) THEN
237      CALL Get_field_rgen(field_name,field,size(field,2),&
238                          corners,edges,found)
239    ELSE
240      CALL Get_field_rgen(field_name,field,size(field,2),&
241                          corners,edges)
242    ENDIF
243
244
245  END SUBROUTINE Get_Field_r2
246
247  SUBROUTINE Get_Field_r3(field_name,field,found,timeindex)
248  ! for a "4D" field surf/alt/??
249  use mod_grid_phy_lmdz, only: klon_glo ! number of atmospheric columns (full grid)
250  IMPLICIT NONE
251    CHARACTER(LEN=*),INTENT(IN)    :: Field_name
252    REAL,INTENT(INOUT)               :: Field(:,:,:)
253    LOGICAL,INTENT(OUT),OPTIONAL   :: found
254    INTEGER,INTENT(IN),OPTIONAL    :: timeindex ! time index of sought data
255
256    integer :: edges(4), corners(4)
257
258    edges(1)=klon_glo
259    edges(2)=size(field,2)
260    edges(3)=size(field,3)
261    edges(4)=1
262    corners(1:4)=1
263    if (PRESENT(timeindex)) then
264      corners(4)=timeindex
265    endif
266
267    IF (PRESENT(found)) THEN
268      CALL Get_field_rgen(field_name,field,size(field,2)*size(field,3),&
269                          corners,edges,found)
270    ELSE
271      CALL Get_field_rgen(field_name,field,size(field,2)*size(field,3),&
272                          corners,edges)
273    ENDIF
274
275  END SUBROUTINE Get_Field_r3
276
277  SUBROUTINE Get_field_rgen(field_name,field,field_size, &
278                            corners,edges,found)
279  USE netcdf
280  USE dimphy
281  USE mod_grid_phy_lmdz
282  USE mod_phys_lmdz_para
283  IMPLICIT NONE
284    CHARACTER(LEN=*) :: Field_name
285    INTEGER          :: field_size
286    REAL             :: field(klon,field_size)
287    INTEGER,INTENT(IN) :: corners(4)
288    INTEGER,INTENT(IN) :: edges(4)
289    LOGICAL,OPTIONAL :: found
290
291    REAL    :: field_glo(klon_glo,field_size)
292    LOGICAL :: tmp_found
293    INTEGER :: varid
294    INTEGER :: ierr
295
296    IF (is_master) THEN
297
298      ierr=NF90_INQ_VARID(nid_start,Field_name,varid)
299
300      IF (ierr==NF90_NOERR) THEN
301        CALL body(field_glo)
302        tmp_found=.TRUE.
303      ELSE
304        tmp_found=.FALSE.
305      ENDIF
306
307    ENDIF
308
309    CALL bcast(tmp_found)
310
311    IF (tmp_found) THEN
312      CALL scatter(field_glo,field)
313    ENDIF
314
315    IF (PRESENT(found)) THEN
316      found=tmp_found
317    ELSE
318      IF (.NOT. tmp_found) THEN
319        write(*,*) 'get_field_rgen: Field <'//field_name//'> not found'
320        CALL abort_physic("get_field_rgen","Failed to get field",1)
321      ENDIF
322    ENDIF
323
324
325    CONTAINS
326
327     SUBROUTINE body(field_glo)
328       REAL :: field_glo(klon_glo*field_size)
329         ierr=NF90_GET_VAR(nid_start,varid,field_glo,corners,edges)
330         IF (ierr/=NF90_NOERR) THEN
331           ! La variable exist dans le fichier mais la lecture a echouee.
332           write(*,*) 'get_field_rgen: Failed reading <'//field_name//'>'
333
334!           IF (field_name=='CLWCON' .OR. field_name=='RNEBCON' .OR. field_name=='RATQS') THEN
335!              ! Essaye de lire le variable sur surface uniqument, comme fait avant
336!              field_glo(:)=0.
337!              ierr=NF90_GET_VAR(nid_start,varid,field_glo(1:klon_glo))
338!              IF (ierr/=NF90_NOERR) THEN
339!                 write(*,*) 'phyetat0: Lecture echouee aussi en 2D pour <'//field_name//'>'
340!                 CALL abort
341!              ELSE
342!                 write(*,*) 'phyetat0: La variable <'//field_name//'> lu sur surface seulement'!, selon ancien format, le reste mis a zero'
343!              END IF
344!           ELSE
345              CALL abort_physic("get_field_rgen","Failed to read field",1)
346!           ENDIF
347         ENDIF
348
349     END SUBROUTINE body
350
351  END SUBROUTINE Get_field_rgen
352
353
354  SUBROUTINE get_var_r0(var_name,var,found)
355  ! Get a scalar from input file
356  IMPLICIT NONE
357    CHARACTER(LEN=*),INTENT(IN)  :: var_name
358    REAL,INTENT(INOUT)             :: var
359    LOGICAL,OPTIONAL,INTENT(OUT) :: found
360
361    REAL                         :: varout(1)
362
363    IF (PRESENT(found)) THEN
364      CALL Get_var_rgen(var_name,varout,size(varout),found)
365    ELSE
366      CALL Get_var_rgen(var_name,varout,size(varout))
367    ENDIF
368    var=varout(1)
369
370  END SUBROUTINE get_var_r0
371
372  SUBROUTINE get_var_r1(var_name,var,found)
373  ! Get a vector from input file
374  IMPLICIT NONE
375    CHARACTER(LEN=*),INTENT(IN)  :: var_name
376    REAL,INTENT(INOUT)             :: var(:)
377    LOGICAL,OPTIONAL,INTENT(OUT) :: found
378
379    IF (PRESENT(found)) THEN
380      CALL Get_var_rgen(var_name,var,size(var),found)
381    ELSE
382      CALL Get_var_rgen(var_name,var,size(var))
383    ENDIF
384
385  END SUBROUTINE get_var_r1
386
387  SUBROUTINE get_var_r2(var_name,var,found)
388  ! Get a 2D field from input file
389  IMPLICIT NONE
390    CHARACTER(LEN=*),INTENT(IN)  :: var_name
391    REAL,INTENT(OUT)             :: var(:,:)
392    LOGICAL,OPTIONAL,INTENT(OUT) :: found
393
394    IF (PRESENT(found)) THEN
395      CALL Get_var_rgen(var_name,var,size(var),found)
396    ELSE
397      CALL Get_var_rgen(var_name,var,size(var))
398    ENDIF
399
400  END SUBROUTINE get_var_r2
401
402  SUBROUTINE get_var_r3(var_name,var,found)
403  ! Get a 3D field frominput file
404  IMPLICIT NONE
405    CHARACTER(LEN=*),INTENT(IN)  :: var_name
406    REAL,INTENT(INOUT)             :: var(:,:,:)
407    LOGICAL,OPTIONAL,INTENT(OUT) :: found
408
409    IF (PRESENT(found)) THEN
410      CALL Get_var_rgen(var_name,var,size(var),found)
411    ELSE
412      CALL Get_var_rgen(var_name,var,size(var))
413    ENDIF
414
415  END SUBROUTINE get_var_r3
416
417  SUBROUTINE Get_var_rgen(var_name,var,var_size,found)
418  USE netcdf
419  USE dimphy
420  USE mod_grid_phy_lmdz
421  USE mod_phys_lmdz_para
422  IMPLICIT NONE
423    CHARACTER(LEN=*) :: var_name
424    INTEGER          :: var_size
425    REAL             :: var(var_size)
426    LOGICAL,OPTIONAL :: found
427
428    LOGICAL :: tmp_found
429    INTEGER :: varid
430    INTEGER :: ierr
431
432    IF (is_mpi_root .AND. is_omp_root) THEN
433
434      ierr=NF90_INQ_VARID(nid_start,var_name,varid)
435
436      IF (ierr==NF90_NOERR) THEN
437        ierr=NF90_GET_VAR(nid_start,varid,var)
438        IF (ierr/=NF90_NOERR) THEN
439          write(*,*) 'phyetat0: Failed loading <'//trim(var_name)//'>'
440          CALL abort_physic("get_var_rgen","Failed to read variable",1)
441        ENDIF
442        tmp_found=.TRUE.
443      ELSE
444        tmp_found=.FALSE.
445      ENDIF
446
447    ENDIF
448
449    CALL bcast(tmp_found)
450
451    IF (tmp_found) THEN
452      CALL bcast(var)
453    ENDIF
454
455    IF (PRESENT(found)) THEN
456      found=tmp_found
457    ELSE
458      IF (.NOT. tmp_found) THEN
459        write(*,*) 'phyetat0: Variable <'//trim(var_name)//'> not found'
460        CALL abort_physic("get_var_rgen","Failed to read variable",1)
461      ENDIF
462    ENDIF
463
464  END SUBROUTINE Get_var_rgen
465
466!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
467
468  SUBROUTINE open_restartphy(filename)
469  USE netcdf, only: NF90_CREATE, NF90_CLOBBER, NF90_64BIT_OFFSET, &
470                    NF90_NOERR, nf90_strerror, &
471                    NF90_PUT_ATT, NF90_GLOBAL, NF90_DEF_DIM, &
472                    NF90_UNLIMITED, NF90_ENDDEF, &
473                    NF90_WRITE, NF90_OPEN
474  USE mod_phys_lmdz_para, only: is_master
475  USE mod_grid_phy_lmdz, only: klon_glo
476  USE dimphy, only: klev, klevp1
477#ifndef CPP_STD
478  USE tracer_mod, only: nqmx
479#else
480  use tracer_h, only:   nqtot
481#endif
482  USE comsoil_h_PEM, only:  nsoilmx_PEM
483  USE comslope_mod, only: nslope
484  use layering_mod, only: nb_str_max
485  IMPLICIT NONE
486    CHARACTER(LEN=*),INTENT(IN) :: filename
487    INTEGER                     :: ierr
488    LOGICAL,SAVE :: already_created=.false.
489
490#ifdef CPP_STD
491    INTEGER :: nqmx
492    nqmx=nqtot
493#endif
494
495    IF (is_master) THEN
496      if (.not.already_created) then
497        ! At the very first call, create the file
498        ierr=NF90_CREATE(filename,IOR(NF90_CLOBBER,NF90_64BIT_OFFSET), &
499                          nid_restart)
500        IF (ierr/=NF90_NOERR) THEN
501          write(*,*)'open_restartphy: problem creating file '//trim(filename)
502          write(*,*)trim(nf90_strerror(ierr))
503          CALL abort_physic("open_restartphy","Failed creating file",1)
504        ENDIF
505        already_created=.true.
506      else
507        ! Just open the file
508        ierr=NF90_OPEN(filename,NF90_WRITE,nid_restart)
509        IF (ierr/=NF90_NOERR) THEN
510          write(*,*)'open_restartphy: problem opening file '//trim(filename)
511          write(*,*)trim(nf90_strerror(ierr))
512          CALL abort_physic("open_restartphy","Failed opening file",1)
513        ENDIF
514        return
515      endif ! of if (.not.already_created)
516
517      ierr=NF90_PUT_ATT(nid_restart,NF90_GLOBAL,"title",&
518                        "Physics start file")
519      IF (ierr/=NF90_NOERR) THEN
520        write(*,*)'open_restartphy: problem writing title '
521        write(*,*)trim(nf90_strerror(ierr))
522      ENDIF
523
524      ierr=NF90_DEF_DIM(nid_restart,"index",length,idim1)
525      IF (ierr/=NF90_NOERR) THEN
526        write(*,*)'open_restartphy: problem defining index dimension '
527        write(*,*)trim(nf90_strerror(ierr))
528        CALL abort_physic("open_restartphy","Failed defining index",1)
529      ENDIF
530
531      ierr=NF90_DEF_DIM(nid_restart,"physical_points",klon_glo,idim2)
532      IF (ierr/=NF90_NOERR) THEN
533        write(*,*)'open_restartphy: problem defining physical_points dimension '
534        write(*,*)trim(nf90_strerror(ierr))
535        CALL abort_physic("open_restartphy","Failed defining physical_points",1)
536      ENDIF
537
538      ierr=NF90_DEF_DIM(nid_restart,"subsurface_layers",nsoilmx_PEM,idim3)
539      IF (ierr/=NF90_NOERR) THEN
540        write(*,*)'open_restartphy: problem defining subsurface_layers dimension '
541        write(*,*)trim(nf90_strerror(ierr))
542        CALL abort_physic("open_restartphy","Failed defining subsurface_layers",1)
543      ENDIF
544
545      ierr=NF90_DEF_DIM(nid_restart,"nlayer_plus_1",klevp1,idim4)
546      IF (ierr/=NF90_NOERR) THEN
547        write(*,*)'open_restartphy: problem defining nlayer_plus_1 dimension '
548        write(*,*)trim(nf90_strerror(ierr))
549        CALL abort_physic("open_restartphy","Failed defining nlayer_plus_1",1)
550      ENDIF
551
552      if (nqmx.ne.0) then
553        ierr=NF90_DEF_DIM(nid_restart,"number_of_advected_fields",nqmx,idim5)
554      else
555        ! pretend nqmx=1 because 0 size implies unlimited dimension for NetCDF
556        ierr=NF90_DEF_DIM(nid_restart,"number_of_advected_fields",1,idim5)
557      endif
558      IF (ierr/=NF90_NOERR) THEN
559        write(*,*)'open_restartphy: problem defining number_of_advected_fields dimension '
560        write(*,*)trim(nf90_strerror(ierr))
561        CALL abort_physic("open_restartphy","Failed defining number_of_advected_fields",1)
562      ENDIF
563
564      ierr=NF90_DEF_DIM(nid_restart,"nlayer",klev,idim6)
565      IF (ierr/=NF90_NOERR) THEN
566        write(*,*)'open_restartphy: problem defining nlayer dimension '
567        write(*,*)trim(nf90_strerror(ierr))
568        CALL abort_physic("open_restartphy","Failed defining nlayer",1)
569      ENDIF
570
571      ierr=NF90_DEF_DIM(nid_restart,"Time",NF90_UNLIMITED,idim7)
572      IF (ierr/=NF90_NOERR) THEN
573        write(*,*)'open_restartphy: problem defining Time dimension '
574        write(*,*)trim(nf90_strerror(ierr))
575        CALL abort_physic("open_restartphy","Failed defining Time",1)
576      ENDIF
577
578      ierr=NF90_DEF_DIM(nid_restart,"nslope",nslope,idim8)
579      IF (ierr/=NF90_NOERR) THEN
580        write(*,*)'phyredem: problem defining nslope dimension'
581        write(*,*)trim(nf90_strerror(ierr))
582        CALL ABORT
583      ENDIF
584
585      ierr=NF90_DEF_DIM(nid_restart,"inter slope",nslope+1,idim9)
586      IF (ierr/=NF90_NOERR) THEN
587        write(*,*)'phyredem: problem defining inter slope dimension'
588        write(*,*)trim(nf90_strerror(ierr))
589        CALL ABORT
590      ENDIF
591
592      ierr=NF90_DEF_DIM(nid_restart,"nb_str_max",nb_str_max,idim10)
593      IF (ierr/=NF90_NOERR) THEN
594        write(*,*)'phyredem: problem defining nb_str_max dimension'
595        write(*,*)trim(nf90_strerror(ierr))
596        CALL ABORT
597      ENDIF
598
599      ierr=NF90_ENDDEF(nid_restart)
600      IF (ierr/=NF90_NOERR) THEN
601        write(*,*)'open_restartphy: problem ending definition mode '
602        write(*,*)trim(nf90_strerror(ierr))
603        CALL abort_physic("open_restartphy","Failed ending definition mode",1)
604      ENDIF
605    ENDIF
606
607  END SUBROUTINE open_restartphy
608
609  SUBROUTINE close_restartphy
610  USE netcdf, only: NF90_CLOSE
611  USE mod_phys_lmdz_para, only: is_master
612  IMPLICIT NONE
613    INTEGER          :: ierr
614
615    IF (is_master) THEN
616      ierr = NF90_CLOSE (nid_restart)
617    ENDIF
618
619  END SUBROUTINE close_restartphy
620
621  SUBROUTINE put_field_r1(field_name,title,field,time)
622  ! For a surface field
623  IMPLICIT NONE
624  CHARACTER(LEN=*),INTENT(IN)    :: field_name
625  CHARACTER(LEN=*),INTENT(IN)    :: title
626  REAL,INTENT(IN)                :: field(:)
627  REAL,OPTIONAL,INTENT(IN)       :: time
628
629  IF (present(time)) THEN
630    ! if timeindex is present, it is a time-dependent variable
631    CALL put_field_rgen(field_name,title,field,1,time)
632  ELSE
633    CALL put_field_rgen(field_name,title,field,1)
634  ENDIF
635
636  END SUBROUTINE put_field_r1
637
638  SUBROUTINE put_field_r2(field_name,title,field,time)
639  ! For a "3D" horizontal-vertical field
640  IMPLICIT NONE
641  CHARACTER(LEN=*),INTENT(IN)    :: field_name
642  CHARACTER(LEN=*),INTENT(IN)    :: title
643  REAL,INTENT(IN)                :: field(:,:)
644  REAL,OPTIONAL,INTENT(IN)       :: time
645
646  IF (present(time)) THEN
647    ! if timeindex is present, it is a time-dependent variable
648    CALL put_field_rgen(field_name,title,field,size(field,2),time)
649  ELSE
650    CALL put_field_rgen(field_name,title,field,size(field,2))
651  ENDIF
652
653  END SUBROUTINE put_field_r2
654
655  SUBROUTINE put_field_r3(field_name,title,field,time)
656  ! For a "4D" field surf/alt/??
657  IMPLICIT NONE
658  CHARACTER(LEN=*),INTENT(IN)    :: field_name
659  CHARACTER(LEN=*),INTENT(IN)    :: title
660  REAL,INTENT(IN)                :: field(:,:,:)
661  REAL,OPTIONAL,INTENT(IN)       :: time
662
663  IF (present(time)) THEN
664    ! if timeindex is present, it is a time-dependent variable
665    CALL put_field_rgen(field_name,title,field,size(field,2)*size(field,3),&
666                        time)
667  ELSE
668    CALL put_field_rgen(field_name,title,field,size(field,2)*size(field,3))
669  ENDIF
670
671  END SUBROUTINE put_field_r3
672
673  SUBROUTINE put_field_rgen(field_name,title,field,field_size,time)
674  USE netcdf
675  USE dimphy
676  USE comsoil_h_PEM, only:  nsoilmx_PEM
677  USE comslope_mod, ONLY: nslope
678  use layering_mod, only: nb_str_max
679  USE mod_grid_phy_lmdz
680  USE mod_phys_lmdz_para
681  IMPLICIT NONE
682  CHARACTER(LEN=*),INTENT(IN)    :: field_name
683  CHARACTER(LEN=*),INTENT(IN)    :: title
684  INTEGER,INTENT(IN)             :: field_size
685  REAL,INTENT(IN)                :: field(klon,field_size)
686  REAL,OPTIONAL,INTENT(IN)       :: time
687
688  REAL                           :: field_glo(klon_glo,field_size)
689  INTEGER                        :: ierr
690  INTEGER                        :: nvarid
691
692    CALL gather(field,field_glo)
693
694    IF (is_master) THEN
695
696      IF (field_size==1) THEN
697        ! input is a 1D "surface field" array
698        if (.not.present(time)) then ! for a time-independent field
699          ierr=NF90_REDEF(nid_restart)
700#ifdef NC_DOUBLE
701          ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_DOUBLE,&
702                            (/idim2/),nvarid)
703#else
704          ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_FLOAT,&
705                            (/idim2/),nvarid)
706#endif
707          if (ierr.ne.NF90_NOERR) then
708            write(*,*)"put_field_rgen error: failed to define "//trim(field_name)
709            write(*,*)trim(nf90_strerror(ierr))
710          endif
711          IF (LEN_TRIM(title) > 0) ierr=NF90_PUT_ATT(nid_restart,nvarid,"title",title)
712          ierr=NF90_ENDDEF(nid_restart)
713          ierr=NF90_PUT_VAR(nid_restart,nvarid,field_glo)
714        else
715          ! check if the variable has already been defined:
716          ierr=NF90_INQ_VARID(nid_restart,field_name,nvarid)
717          if (ierr/=NF90_NOERR) then ! variable not found, define it
718            ierr=NF90_REDEF(nid_restart)
719#ifdef NC_DOUBLE
720            ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_DOUBLE,&
721                              (/idim2,idim7/),nvarid)
722#else
723            ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_FLOAT,&
724                              (/idim2,idim7/),nvarid)
725#endif
726            if (ierr.ne.NF90_NOERR) then
727              write(*,*)"put_field_rgen error: failed to define "//trim(field_name)
728              write(*,*)trim(nf90_strerror(ierr))
729            endif
730            IF (LEN_TRIM(title) > 0) ierr=NF90_PUT_ATT(nid_restart,nvarid,"title",title)
731            ierr=NF90_ENDDEF(nid_restart)
732          endif
733          ! Write the variable
734          ierr=NF90_PUT_VAR(nid_restart,nvarid,field_glo,&
735                            start=(/1,timeindex/))
736        endif ! of if (.not.present(timeindex))
737
738      ELSE IF (field_size==klev) THEN
739        ! input is a 2D "atmospheric field" array
740        if (.not.present(time)) then ! for a time-independent field
741          ierr=NF90_REDEF(nid_restart)
742#ifdef NC_DOUBLE
743          ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_DOUBLE,&
744                            (/idim2,idim6/),nvarid)
745#else
746          ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_FLOAT,&
747                            (/idim2,idim6/),nvarid)
748#endif
749          if (ierr.ne.NF90_NOERR) then
750            write(*,*)"put_field_rgen error: failed to define "//trim(field_name)
751            write(*,*)trim(nf90_strerror(ierr))
752          endif
753          IF (LEN_TRIM(title) > 0) ierr=NF90_PUT_ATT(nid_restart,nvarid,"title",title)
754          ierr=NF90_ENDDEF(nid_restart)
755          ierr=NF90_PUT_VAR(nid_restart,nvarid,field_glo)
756        else
757          ! check if the variable has already been defined:
758          ierr=NF90_INQ_VARID(nid_restart,field_name,nvarid)
759          if (ierr/=NF90_NOERR) then ! variable not found, define it
760            ierr=NF90_REDEF(nid_restart)
761#ifdef NC_DOUBLE
762            ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_DOUBLE,&
763                              (/idim2,idim6,idim7/),nvarid)
764#else
765            ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_FLOAT,&
766                              (/idim2,idim6,idim7/),nvarid)
767#endif
768            if (ierr.ne.NF90_NOERR) then
769              write(*,*)"put_field_rgen error: failed to define "//trim(field_name)
770              write(*,*)trim(nf90_strerror(ierr))
771            endif
772            IF (LEN_TRIM(title) > 0) ierr=NF90_PUT_ATT(nid_restart,nvarid,"title",title)
773            ierr=NF90_ENDDEF(nid_restart)
774          endif
775          ! Write the variable
776          ierr=NF90_PUT_VAR(nid_restart,nvarid,field_glo,&
777                            start=(/1,1,timeindex/))
778        endif ! of if (.not.present(time))
779
780      ELSE IF (field_size==klevp1) THEN
781        ! input is a 2D "interlayer atmospheric field" array
782        if (.not.present(time)) then ! for a time-independent field
783          ierr=NF90_REDEF(nid_restart)
784#ifdef NC_DOUBLE
785          ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_DOUBLE,&
786                            (/idim2,idim4/),nvarid)
787#else
788          ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_FLOAT,&
789                            (/idim2,idim4/),nvarid)
790#endif
791          if (ierr.ne.NF90_NOERR) then
792            write(*,*)"put_field_rgen error: failed to define "//trim(field_name)
793            write(*,*)trim(nf90_strerror(ierr))
794          endif
795          IF (LEN_TRIM(title) > 0) ierr=NF90_PUT_ATT(nid_restart,nvarid,"title",title)
796          ierr = NF90_ENDDEF(nid_restart)
797          ierr = NF90_PUT_VAR(nid_restart,nvarid,field_glo)
798        else
799          ! check if the variable has already been defined:
800          ierr=NF90_INQ_VARID(nid_restart,field_name,nvarid)
801          if (ierr/=NF90_NOERR) then ! variable not found, define it
802            ierr=NF90_REDEF(nid_restart)
803#ifdef NC_DOUBLE
804            ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_DOUBLE,&
805                              (/idim2,idim4,idim7/),nvarid)
806#else
807            ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_FLOAT,&
808                              (/idim2,idim4,idim7/),nvarid)
809#endif
810            if (ierr.ne.NF90_NOERR) then
811              write(*,*)"put_field_rgen error: failed to define "//trim(field_name)
812              write(*,*)trim(nf90_strerror(ierr))
813            endif
814            IF (LEN_TRIM(title) > 0) ierr=NF90_PUT_ATT(nid_restart,nvarid,"title",title)
815            ierr=NF90_ENDDEF(nid_restart)
816          endif
817          ! Write the variable
818          ierr=NF90_PUT_VAR(nid_restart,nvarid,field_glo,&
819                            start=(/1,1,timeindex/))
820        endif ! of if (.not.present(timeindex))
821
822      ELSE IF (field_size==nsoilmx_PEM) THEN
823        ! input is a 2D "subsurface field" array
824        if (.not.present(time)) then ! for a time-independent field
825          ierr = NF90_REDEF(nid_restart)
826#ifdef NC_DOUBLE
827          ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_DOUBLE,&
828                            (/idim2,idim3/),nvarid)
829#else
830          ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_FLOAT,&
831                            (/idim2,idim3/),nvarid)
832#endif
833          if (ierr.ne.NF90_NOERR) then
834            write(*,*)"put_field_rgen error: failed to define "//trim(field_name)
835            write(*,*)trim(nf90_strerror(ierr))
836          endif
837          IF (LEN_TRIM(title) > 0) ierr=NF90_PUT_ATT(nid_restart,nvarid,"title",title)
838          ierr = NF90_ENDDEF(nid_restart)
839          ierr = NF90_PUT_VAR(nid_restart,nvarid,field_glo)
840        else
841          ! check if the variable has already been defined:
842          ierr=NF90_INQ_VARID(nid_restart,field_name,nvarid)
843          if (ierr/=NF90_NOERR) then ! variable not found, define it
844            ierr=NF90_REDEF(nid_restart)
845#ifdef NC_DOUBLE
846            ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_DOUBLE,&
847                              (/idim2,idim3,idim7/),nvarid)
848#else
849            ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_FLOAT,&
850                              (/idim2,idim3,idim7/),nvarid)
851#endif
852           if (ierr.ne.NF90_NOERR) then
853              write(*,*)"put_field_rgen error: failed to define "//trim(field_name)
854              write(*,*)trim(nf90_strerror(ierr))
855            endif
856            IF (LEN_TRIM(title) > 0) ierr=NF90_PUT_ATT(nid_restart,nvarid,"title",title)
857            ierr=NF90_ENDDEF(nid_restart)
858          endif
859          ! Write the variable
860          ierr=NF90_PUT_VAR(nid_restart,nvarid,field_glo,&
861                            start=(/1,1,timeindex/))
862
863        endif ! of if (.not.present(time))
864
865
866      ELSE IF (field_size==nslope) THEN
867        ! input is a 2D "subsurface field" array
868        if (.not.present(time)) then ! for a time-independent field
869          ierr = NF90_REDEF(nid_restart)
870#ifdef NC_DOUBLE
871          ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_DOUBLE,&
872                            (/idim2,idim8/),nvarid)
873#else
874          ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_FLOAT,&
875                            (/idim2,idim8/),nvarid)
876#endif
877          if (ierr.ne.NF90_NOERR) then
878            write(*,*)"put_field_rgen error: failed to define"//trim(field_name)
879            write(*,*)trim(nf90_strerror(ierr))
880          endif
881          IF (LEN_TRIM(title) > 0) ierr=NF90_PUT_ATT(nid_restart,nvarid,"title",title)
882          ierr = NF90_ENDDEF(nid_restart)
883          ierr = NF90_PUT_VAR(nid_restart,nvarid,field_glo)
884        else
885          ! check if the variable has already been defined:
886          ierr=NF90_INQ_VARID(nid_restart,field_name,nvarid)
887          if (ierr/=NF90_NOERR) then ! variable not found, define it
888            ierr=NF90_REDEF(nid_restart)
889#ifdef NC_DOUBLE
890            ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_DOUBLE,&
891                              (/idim2,idim8,idim7/),nvarid)
892#else
893            ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_FLOAT,&
894                              (/idim2,idim8,idim7/),nvarid)
895#endif
896           if (ierr.ne.NF90_NOERR) then
897              write(*,*)"put_field_rgen error: failed to define"//trim(field_name)
898              write(*,*)trim(nf90_strerror(ierr))
899            endif
900            IF (LEN_TRIM(title) > 0) ierr=NF90_PUT_ATT(nid_restart,nvarid,"title",title)
901            ierr=NF90_ENDDEF(nid_restart)
902          endif
903          ! Write the variable
904          ierr=NF90_PUT_VAR(nid_restart,nvarid,field_glo,&
905                            start=(/1,1,timeindex/))
906
907        endif ! of if (.not.present(time))
908
909      ELSE IF (field_size == nb_str_max) THEN
910        ! input is a 2D "stratification" array
911        if (.not.present(time)) then ! for a time-independent field
912          ierr = NF90_REDEF(nid_restart)
913#ifdef NC_DOUBLE
914          ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_DOUBLE,&
915                            (/idim2,idim8/),nvarid)
916#else
917          ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_FLOAT,&
918                            (/idim2,idim8/),nvarid)
919#endif
920          if (ierr.ne.NF90_NOERR) then
921            write(*,*)"put_field_rgen error: failed to define"//trim(field_name)
922            write(*,*)trim(nf90_strerror(ierr))
923          endif
924          IF (LEN_TRIM(title) > 0) ierr=NF90_PUT_ATT(nid_restart,nvarid,"title",title)
925          ierr = NF90_ENDDEF(nid_restart)
926          ierr = NF90_PUT_VAR(nid_restart,nvarid,field_glo)
927        else
928          ! check if the variable has already been defined:
929          ierr=NF90_INQ_VARID(nid_restart,field_name,nvarid)
930          if (ierr/=NF90_NOERR) then ! variable not found, define it
931            ierr=NF90_REDEF(nid_restart)
932#ifdef NC_DOUBLE
933            ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_DOUBLE,&
934                              (/idim2,idim10,idim7/),nvarid)
935#else
936            ierr=NF90_DEF_VAR(nid_restart,field_name,NF90_FLOAT,&
937                              (/idim2,idim10,idim7/),nvarid)
938#endif
939           if (ierr.ne.NF90_NOERR) then
940              write(*,*)"put_field_rgen error: failed to define"//trim(field_name)
941              write(*,*)trim(nf90_strerror(ierr))
942            endif
943            IF (LEN_TRIM(title) > 0) ierr=NF90_PUT_ATT(nid_restart,nvarid,"title",title)
944            ierr=NF90_ENDDEF(nid_restart)
945          endif
946          ! Write the variable
947          ierr=NF90_PUT_VAR(nid_restart,nvarid,field_glo,&
948                            start=(/1,1,timeindex/))
949
950        endif ! of if (.not.present(time))
951
952      ELSE
953        write(*,*) "Error phyredem(put_field_rgen) : wrong dimension for ",trim(field_name)
954        write(*,*) "  field_size =",field_size
955        CALL abort_physic("put_field_rgen","wrong field dimension",1)
956      ENDIF
957
958      ! Check the writting of field to file went OK
959      if (ierr.ne.NF90_NOERR) then
960        write(*,*) " Error phyredem(put_field_rgen) : failed writing ",trim(field_name)
961        write(*,*)trim(nf90_strerror(ierr))
962        CALL abort_physic("put_field_rgen","Failed writing field",1)
963      endif
964
965    ENDIF ! of IF (is_master)
966
967  END SUBROUTINE put_field_rgen
968
969  SUBROUTINE put_var_r0(var_name,title,var)
970  ! Put a scalar in file
971   IMPLICIT NONE
972     CHARACTER(LEN=*),INTENT(IN) :: var_name
973     CHARACTER(LEN=*),INTENT(IN) :: title
974     REAL,INTENT(IN)             :: var
975     REAL                        :: varin(1)
976
977     varin(1)=var
978
979     CALL put_var_rgen(var_name,title,varin,size(varin))
980
981  END SUBROUTINE put_var_r0
982
983
984  SUBROUTINE put_var_r1(var_name,title,var)
985  ! Put a vector in file
986   IMPLICIT NONE
987     CHARACTER(LEN=*),INTENT(IN) :: var_name
988     CHARACTER(LEN=*),INTENT(IN) :: title
989     REAL,INTENT(IN)             :: var(:)
990
991     CALL put_var_rgen(var_name,title,var,size(var))
992
993  END SUBROUTINE put_var_r1
994
995  SUBROUTINE put_var_r2(var_name,title,var)
996  ! Put a 2D field in file
997   IMPLICIT NONE
998     CHARACTER(LEN=*),INTENT(IN) :: var_name
999     CHARACTER(LEN=*),INTENT(IN) :: title
1000     REAL,INTENT(IN)             :: var(:,:)
1001
1002     CALL put_var_rgen(var_name,title,var,size(var))
1003
1004  END SUBROUTINE put_var_r2
1005
1006  SUBROUTINE put_var_r3(var_name,title,var)
1007  ! Put a 3D field in file
1008   IMPLICIT NONE
1009     CHARACTER(LEN=*),INTENT(IN) :: var_name
1010     CHARACTER(LEN=*),INTENT(IN) :: title
1011     REAL,INTENT(IN)             :: var(:,:,:)
1012
1013     CALL put_var_rgen(var_name,title,var,size(var))
1014
1015  END SUBROUTINE put_var_r3
1016
1017  SUBROUTINE put_var_rgen(var_name,title,var,var_size)
1018  USE netcdf, only: NF90_REDEF, NF90_DEF_VAR, NF90_ENDDEF, NF90_PUT_VAR, &
1019                    NF90_FLOAT, NF90_DOUBLE, &
1020                    NF90_PUT_ATT, NF90_NOERR, nf90_strerror, &
1021                    nf90_inq_dimid, nf90_inquire_dimension, NF90_INQ_VARID
1022  USE comsoil_h_PEM, only:  nsoilmx_PEM
1023  USE comslope_mod, only: nslope
1024  USE mod_phys_lmdz_para, only: is_master
1025  IMPLICIT NONE
1026     CHARACTER(LEN=*),INTENT(IN) :: var_name
1027     CHARACTER(LEN=*),INTENT(IN) :: title
1028     INTEGER,INTENT(IN)          :: var_size
1029     REAL,INTENT(IN)             :: var(var_size)
1030
1031     INTEGER :: ierr
1032     INTEGER :: nvarid
1033     INTEGER :: idim1d
1034     logical,save :: firsttime=.true.
1035
1036    IF (is_master) THEN
1037
1038      IF (var_name=="Time") THEN
1039        ! Very specific case of "Time" variable
1040        if (firsttime) then
1041          ! Create the "Time variable"
1042          ierr=NF90_REDEF(nid_restart)
1043#ifdef NC_DOUBLE
1044          ierr=NF90_DEF_VAR(nid_restart,var_name,NF90_DOUBLE,&
1045                            (/idim7/),nvarid)
1046#else
1047          ierr=NF90_DEF_VAR(nid_restart,var_name,NF90_FLOAT,&
1048                            (/idim7/),nvarid)
1049#endif
1050          IF (LEN_TRIM(title) > 0) ierr=NF90_PUT_ATT(nid_restart,nvarid,"title",title)
1051          ierr=NF90_ENDDEF(nid_restart)
1052
1053          firsttime=.false.
1054        endif
1055        ! Append "time" value
1056        ! get current length of "Time" dimension
1057        ierr=nf90_inq_dimid(nid_restart,var_name,idim1d)
1058        ierr=nf90_inquire_dimension(nid_restart,idim1d,len=timeindex)
1059        timeindex=timeindex+1
1060        ierr=NF90_INQ_VARID(nid_restart,var_name,nvarid)
1061        ierr=NF90_PUT_VAR(nid_restart,nvarid,var,&
1062                           start=(/timeindex/))
1063        IF (ierr/=NF90_NOERR) THEN
1064          write(*,*)'put_var_rgen: problem writing Time'
1065          write(*,*)trim(nf90_strerror(ierr))
1066          CALL abort_physic("get_var_rgen","Failed to write Time",1)
1067        ENDIF
1068        return ! nothing left to do
1069      ELSEIF (var_size==length) THEN
1070        ! We know it is a "controle" kind of 1D array
1071        idim1d=idim1
1072      ELSEIF (var_size==nsoilmx_PEM) THEN
1073        ! We know it is an "mlayer" kind of 1D array
1074        idim1d=idim3
1075      ELSEIF (var_size==nslope+1) THEN
1076        ! We know it is an "inter slope" kind of 1D array
1077        idim1d=idim9
1078      ELSEIF (var_name == "nb_str_max") THEN
1079        ! We know it is a kind of stratification array
1080        idim1d = idim10
1081      ELSE
1082        write(*,*) "put_var_rgen error : wrong dimension"
1083        write(*,*) "  var_size =",var_size
1084        CALL abort_physic("get_var_rgen","Wrong variable dimension",1)
1085
1086      ENDIF ! of IF (var_size==length) THEN
1087
1088      ! Swich to NetCDF define mode
1089      ierr=NF90_REDEF (nid_restart)
1090      ! Define the variable
1091#ifdef NC_DOUBLE
1092      ierr=NF90_DEF_VAR(nid_restart,var_name,NF90_DOUBLE,(/idim1d/),nvarid)
1093#else
1094      ierr=NF90_DEF_VAR(nid_restart,var_name,NF90_FLOAT,(/idim1d/),nvarid)
1095#endif
1096      ! Add a "title" attribute
1097      IF (LEN_TRIM(title)>0) ierr=NF90_PUT_ATT(nid_restart,nvarid,"title",title)
1098      ! Swich out of define mode
1099      ierr=NF90_ENDDEF(nid_restart)
1100      ! Write variable to file
1101      ierr=NF90_PUT_VAR(nid_restart,nvarid,var)
1102      IF (ierr/=NF90_NOERR) THEN
1103        write(*,*)'put_var_rgen: problem writing '//trim(var_name)
1104        write(*,*)trim(nf90_strerror(ierr))
1105        CALL abort_physic("get_var_rgen","Failed writing variable",1)
1106      ENDIF
1107    ENDIF ! of IF (is_master)
1108
1109  END SUBROUTINE put_var_rgen
1110
1111END MODULE iostart_PEM
Note: See TracBrowser for help on using the repository browser.