source: trunk/LMDZ.PLUTO/libf/phypluto/iostart.F90

Last change on this file was 3184, checked in by afalco, 10 months ago

Pluto PCM:
Added LMDZ.PLUTO, a copy of the generic model,
cleaned from some unnecessary modules (water, ...)
AF

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