source: IOIPSL/trunk/src/restcom.f90 @ 428

Last change on this file since 428 was 428, checked in by bellier, 13 years ago

some bugfixes

  • Property svn:keywords set to Id
File size: 77.6 KB
Line 
1MODULE restcom
2!-
3!$Id$
4!-
5! This software is governed by the CeCILL license
6! See IOIPSL/IOIPSL_License_CeCILL.txt
7!-
8USE netcdf
9!-
10USE errioipsl, ONLY : ipslerr,ipsldbg
11USE stringop
12USE calendar
13USE mathelp
14USE fliocom,   ONLY : flio_dom_file,flio_dom_att
15!-
16IMPLICIT NONE
17!-
18PRIVATE
19!-
20PUBLIC :: &
21 &  restini, restget, restput, restclo, &
22 &  ioconf_setatt, ioget_vname, ioconf_expval, &
23 &  ioget_expval, ioget_vdim
24!-
25INTERFACE restput
26  MODULE PROCEDURE &
27 &  restput_r3d, restput_r2d, restput_r1d, &
28 &  restput_opp_r2d, restput_opp_r1d
29END INTERFACE
30!-
31INTERFACE restget
32  MODULE PROCEDURE &
33 &  restget_r3d,restget_r2d,restget_r1d, &
34 &  restget_opp_r2d,restget_opp_r1d
35END INTERFACE
36!-
37! We do not use allocatable arrays because these sizes are safe
38! and we do not know from start how many variables will be in
39! the out file.
40!-
41  INTEGER,PARAMETER :: &
42 &  max_var=500, max_file=50, max_dim=NF90_MAX_VAR_DIMS
43!-
44  CHARACTER(LEN=9),SAVE :: calend_str='unknown'
45!-
46! The IDs of the netCDF files are going in pairs.
47! The input one (netcdf_id(?,1)) and the output one (netcdf_id(?,2))
48!-
49  INTEGER,SAVE :: nb_fi = 0
50  INTEGER,DIMENSION(max_file,2),SAVE :: netcdf_id = -1
51!-
52! Description of the content of the 'in' files and the 'out' files.
53!   Number of variables   : nbvar_*
54!   Number of dimensions  : nbdim_*
55!   ID of the time axis   : tdimid_*
56!-
57  INTEGER,SAVE :: nbvar_in(max_file), nbvar_out(max_file)
58  INTEGER,SAVE :: tdimid_in(max_file), tdimid_out(max_file)
59!-
60! Variables for one or the other file
61!-
62! Number of dimensions in the input file               : nbdim_in
63! Number of variables read so far from the input file  : nbvar_read
64! Type of variable read from the input file            : vartyp_in
65!   (Could be used later to test if we have a restart file)
66!-
67  INTEGER,SAVE :: nbdim_in(max_file), nbvar_read(max_file)
68  INTEGER,SAVE :: vartyp_in(max_file, max_var)
69!-
70! Time step and time origine in the input file.
71!-
72  REAL,DIMENSION(max_file),SAVE :: deltat,timeorig
73!-
74! Description of the axes in the output file
75!-
76!   tstp_out  : Index on the tie axis currently beeing written
77!   itau_out  : Time step which is written on this index of the file
78!-
79  INTEGER,DIMENSION(max_file),SAVE :: tstp_out,itau_out
80!-
81! Description of the axes in the output file
82!-
83! For the ?ax_infs variable the following order is used :
84!   ?ax_infs (if,in,1) = size of axis
85!   ?ax_infs (if,in,2) = id of dimension
86! Number of x,y and z axes in the output file :
87!   ?ax_nb(if)
88!-
89  INTEGER,DIMENSION(max_file,max_dim,2),SAVE :: &
90 &  xax_infs,yax_infs,zax_infs
91  INTEGER,DIMENSION(max_file),SAVE :: &
92 &  xax_nb=0,yax_nb=0,zax_nb=0
93!-
94! Description of the time axes in the input and output files
95!-
96!   ID of the variable which contains the itaus :
97!     tind_varid_*
98!   ID of the variables which contains the seconds since date :
99!     tax_varid_*
100!   Size of the time axis in the input file :
101!     tax_size_in
102!-
103  INTEGER,SAVE :: tind_varid_in(max_file),  tax_varid_in(max_file), &
104 &                tind_varid_out(max_file), tax_varid_out(max_file)
105  INTEGER,SAVE :: tax_size_in(max_file)=1
106!-
107! The two time axes we have in the input file :
108!   t_index   : dates in itaus
109!               (thus the variable has a tstep_sec attribute)
110!   t_julian  : Julian days of the time axis
111!-
112  INTEGER,SAVE,ALLOCATABLE :: t_index(:,:)
113  REAL,SAVE,ALLOCATABLE :: t_julian(:,:)
114!-
115! Here we save a number of informations on the variables
116! in the files we are handling
117!-
118! Name of variables :                                    varname_*
119! ID of the variables :                                  varid_*
120! Number of dimensions of the variable :                 varnbdim_*
121! Dimensions which are used for the variable :           vardims_*
122! Number of attributes for a variables :                 varatt_*
123! A flag which markes the variables we have worked on :  touched_*
124!-
125  CHARACTER(LEN=20),DIMENSION(max_file,max_var),SAVE :: &
126 &  varname_in,varname_out
127  INTEGER,DIMENSION(max_file,max_var),SAVE :: &
128 &  varid_in,varid_out,varnbdim_in,varatt_in
129  INTEGER,DIMENSION(max_file,max_var,max_dim),SAVE :: &
130 &  vardims_in
131  LOGICAL,DIMENSION(max_file,max_var),SAVE :: &
132 &  touched_in,touched_out
133!-
134  CHARACTER(LEN=120),SAVE :: indchfun= 'scatter, fill, gather, coll'
135  REAL,PARAMETER :: missing_val=1.e20
136!                or HUGE(1.0) (maximum real number)
137!-
138! The default value we will use for variables
139! which are not present in the restart file
140!-
141  REAL,SAVE :: val_exp =  999999.
142  LOGICAL,SAVE :: lock_valexp = .FALSE.
143!-
144! Temporary variables in which we store the attributed which are going
145! to be given to a new variable which is going to be defined.
146!-
147  CHARACTER(LEN=80),SAVE :: rest_units='XXXXX',rest_lname='XXXXX'
148!-
149! For allocations
150!-
151  REAL,ALLOCATABLE,DIMENSION(:),SAVE :: buff_tmp1,buff_tmp2
152!-
153!===
154CONTAINS
155!===
156!-
157SUBROUTINE restini &
158 & (fnamein,iim,jjm,lon,lat,llm,lev, &
159 &  fnameout,itau,date0,dt,fid,owrite_time_in,domain_id)
160!---------------------------------------------------------------------
161!- This subroutine sets up all the restart process.
162!- It will call the subroutine which opens the input
163!- and output files.
164!- The time step (itau), date of origine (date0) and time step are
165!- READ from the input file.
166!- A file ID, which is common to the input and output file is returned
167!-
168!- If fnamein = fnameout then the same file is used for the reading
169!- the restart conditions and writing the new restart.
170!-
171!- A special mode can be switched in with filename='NONE'.
172!- This means that no restart file is present.
173!- Usefull for creating the first restart file
174!- or to get elements in a file without creating an output file.
175!-
176!- A mode needs to be written in which itau, date0 and dt
177!- are given to the restart process and thus
178!- written into the output restart file.
179!-
180!- INPUT
181!-
182!- fnamein  : name of the file for the restart
183!- iim      : Dimension in x
184!- jjm      : Dimension in y
185!- lon      : Longitude in the x,y domain
186!- lat      : Latitude in the x,y domain
187!- llm      : Dimension in the vertical
188!- lev      : Positions of the levels
189!- fnameout :
190!-
191!- OUTPUT
192!-
193!- itau     : Time step of the restart file and at which the model
194!-            should restart
195!- date0    : Time at which itau = 0
196!- dt       : time step in seconds between two succesiv itaus
197!- fid      : File identification of the restart file
198!-
199!- Optional INPUT arguments
200!-
201!- owrite_time_in : logical  argument which allows to
202!-                  overwrite the time in the restart file
203!- domain_id      : Domain identifier
204!---------------------------------------------------------------------
205  IMPLICIT NONE
206!-
207  CHARACTER(LEN=*),INTENT(IN) :: fnamein,fnameout
208  INTEGER :: iim,jjm,llm,fid,itau
209  REAL :: lon(iim,jjm),lat(iim,jjm),lev(llm)
210  REAL :: date0,dt
211  LOGICAL,OPTIONAL :: owrite_time_in
212  INTEGER,INTENT(IN),OPTIONAL :: domain_id
213!-
214  INTEGER :: ncfid
215  REAL :: dt_tmp,date0_tmp
216  LOGICAL :: l_fi,l_fo,l_rw
217  LOGICAL :: overwrite_time
218  CHARACTER(LEN=120) :: fname
219  LOGICAL :: l_dbg
220!---------------------------------------------------------------------
221  CALL ipsldbg (old_status=l_dbg)
222!-
223! 0.0 Prepare the configuration before opening any files
224!-
225  IF (.NOT.PRESENT(owrite_time_in)) THEN
226    overwrite_time = .FALSE.
227  ELSE
228    overwrite_time = owrite_time_in
229  ENDIF
230!-
231  IF (l_dbg) THEN
232    WRITE(*,*) 'restini 0.0 : ',TRIM(fnamein),' , ',TRIM(fnameout)
233  ENDIF
234!-
235  nb_fi = nb_fi+1
236!-
237  IF (nb_fi > max_file) THEN
238    CALL ipslerr (3,'restini',&
239 &   'Too many restart files are used. The problem can be',&
240 &   'solved by increasing max_file in restcom.f90 ',&
241 &   'and recompiling ioipsl.')
242  ENDIF
243!-
244! 0.1 Define the open flags
245!-
246  l_fi = (TRIM(fnamein)  /= 'NONE')
247  l_fo = (TRIM(fnameout) /= 'NONE')
248  IF ((.NOT.l_fi).AND.(.NOT.l_fo)) THEN
249    CALL ipslerr (3,'restini',&
250 &   'Input and output file names are both to NONE.',&
251 &   'It is probably an error.','Verify your logic.')
252  ENDIF
253  l_rw = l_fi.AND.l_fo.AND.(TRIM(fnamein) == TRIM(fnameout))
254!-
255  IF (l_dbg) THEN
256    WRITE(*,*) 'restini 0.1 l_fi, l_fo, l_rw : ',l_fi,l_fo,l_rw
257  ENDIF
258!-
259! 1.0 Open the input file.
260!-
261  IF (l_fi) THEN
262!---
263    IF (l_dbg) WRITE(*,*) 'restini 1.0 : Open input file'
264!-- Add DOMAIN number and ".nc" suffix in file names if needed
265    fname = fnamein
266    CALL flio_dom_file (fname,domain_id)
267!-- Open the file
268    CALL restopenin (nb_fi,fname,l_rw,iim,jjm,lon,lat,llm,lev,ncfid)
269    netcdf_id(nb_fi,1) = ncfid
270!---
271!-- 1.3 Extract the time information
272!---
273    IF (overwrite_time) THEN
274       date0_tmp = date0
275    ENDIF
276    CALL restsett (dt_tmp,date0_tmp,itau,overwrite_time)
277    IF (.NOT.overwrite_time) THEN
278      dt = dt_tmp
279      date0 = date0_tmp
280    ENDIF
281!---
282  ELSE
283!---
284!-- 2.0 The case of a missing restart file is dealt with
285!---
286    IF (l_dbg) WRITE(*,*) 'restini 2.0'
287!---
288    IF (     (ALL(MINLOC(lon(:iim,:jjm)) == MAXLOC(lon(:iim,:jjm)))) &
289        .AND.(iim > 1) ) THEN
290      CALL ipslerr (3,'restini',&
291        & 'For creating a restart file the longitudes of the',&
292        & 'grid need to be provided to restini. This ',&
293        & 'information is needed for the restart files')
294    ENDIF
295    IF (     (ALL(MINLOC(lat(:iim,:jjm)) == MAXLOC(lat(:iim,:jjm)))) &
296        .AND.(jjm > 1) ) THEN
297      CALL ipslerr (3,'restini',&
298        & 'For creating a restart file the latitudes of the',&
299        & 'grid need to be provided to restini. This ',&
300        & 'information is needed for the restart files')
301    ENDIF
302    IF (     (ALL(MINLOC(lev(:llm)) == MAXLOC(lev(:llm)))) &
303        .AND.(llm > 1) ) THEN
304      CALL ipslerr (3,'restini',&
305        & 'For creating a restart file the levels of the',&
306        & 'grid need to be provided to restini. This',&
307        & 'information is needed for the restart files')
308    ENDIF
309!---
310!-- 2.2 Allocate the time axes and write the inputed variables
311!---
312    tax_size_in(nb_fi) = 1
313    CALL rest_atim (l_dbg,'restini')
314    t_index(nb_fi,1) = itau
315    t_julian(nb_fi,1) = date0
316  ENDIF
317!-
318  IF (l_fo.AND.(.NOT.l_rw)) THEN
319!-- Add DOMAIN number and ".nc" suffix in file names if needed
320    fname = fnameout
321    CALL flio_dom_file (fname,domain_id)
322!-- Open the file
323    CALL restopenout &
324      (nb_fi,fname,iim,jjm,lon,lat,llm,lev,dt,date0,ncfid,domain_id)
325    netcdf_id(nb_fi,2) = ncfid
326  ELSE IF (l_fi.AND.l_fo) THEN
327    netcdf_id(nb_fi,2) = netcdf_id(nb_fi,1)
328    varname_out(nb_fi,:) = varname_in(nb_fi,:)
329    nbvar_out(nb_fi) = nbvar_in(nb_fi)
330    tind_varid_out(nb_fi) = tind_varid_in(nb_fi)
331    tax_varid_out(nb_fi) = tax_varid_in(nb_fi)
332    varid_out(nb_fi,:) = varid_in(nb_fi,:)
333    touched_out(nb_fi,:) = .TRUE.
334  ENDIF
335!-
336! 2.3 Set the calendar for the run.
337!     This should not produce any error message if
338!     This does not mean any change in calendar
339!     (to be modified in ioconf_calendar)
340!-
341  IF (l_dbg) THEN
342    WRITE(*,*) 'restini 2.3 : Configure calendar if needed : ', &
343                calend_str
344  ENDIF
345!-
346  IF (INDEX(calend_str,'unknown') < 1) THEN
347    CALL ioconf_calendar (calend_str)
348    IF (l_dbg) THEN
349      WRITE(*,*) 'restini 2.3b : new calendar : ',calend_str
350    ENDIF
351  ENDIF
352!-
353! Save some data in the module
354!-
355  deltat(nb_fi) = dt
356!-
357! Prepare the variables which will be returned
358!-
359  fid = nb_fi
360  IF (l_dbg) THEN
361    WRITE(*,*) 'SIZE of t_index :',SIZE(t_index), &
362               SIZE(t_index,dim=1),SIZE(t_index,dim=2)
363    WRITE(*,*) 't_index = ',t_index(fid,:)
364  ENDIF
365  itau = t_index(fid,1)
366!-
367  IF (l_dbg) WRITE(*,*) 'restini END'
368!---------------------
369END SUBROUTINE restini
370!===
371SUBROUTINE restopenin &
372  (fid,fname,l_rw,iim,jjm,lon,lat,llm,lev,ncfid)
373!---------------------------------------------------------------------
374!- Opens the restart file and checks that it belongsd to the model.
375!- This means that the coordinates of the model are compared to the
376!- ones in the file.
377!-
378!- The number and name of variable in the file are exctracted. Also
379!- the time details.
380!---------------------------------------------------------------------
381  IMPLICIT NONE
382!-
383  INTEGER,INTENT(IN) :: fid,iim,jjm,llm
384  CHARACTER(LEN=*),INTENT(IN) :: fname
385  REAL :: lon(iim,jjm),lat(iim,jjm),lev(llm)
386  LOGICAL,INTENT(IN) :: l_rw
387  INTEGER,INTENT(OUT) :: ncfid
388!-
389  INTEGER,DIMENSION(max_dim) :: var_dims,dimlen
390  INTEGER :: nb_dim,nb_var,id_unl,id,iv
391  INTEGER :: iread,jread,lread,iret
392  INTEGER :: lon_vid,lat_vid
393  REAL :: lon_read(iim,jjm),lat_read(iim,jjm)
394  REAL :: lev_read(llm)
395  REAL :: mdlon,mdlat
396  CHARACTER(LEN=80) :: units
397  CHARACTER(LEN=NF90_max_name),DIMENSION(max_dim) :: dimname
398  LOGICAL :: l_dbg
399!---------------------------------------------------------------------
400  CALL ipsldbg (old_status=l_dbg)
401!-
402! If we reuse the same file for input and output
403! then we open it in write mode
404!-
405  IF (l_rw) THEN; id = NF90_WRITE; ELSE; id = NF90_NOWRITE; ENDIF
406  iret = NF90_OPEN(fname,id,ncfid)
407  IF (iret /= NF90_NOERR) THEN
408    CALL ipslerr (3,'restopenin','Could not open file :',fname,' ')
409  ENDIF
410!-
411  IF (l_dbg) WRITE (*,*) "restopenin 0.0 ",TRIM(fname)
412  iret = NF90_INQUIRE(ncfid,nDimensions=nb_dim, &
413 &         nVariables=nb_var,unlimitedDimId=id_unl)
414  tdimid_in(fid) = id_unl
415!-
416  IF (nb_dim > max_dim) THEN
417    CALL ipslerr (3,'restopenin',&
418      & 'More dimensions present in file that can be store',&
419      & 'Please increase max_dim in the global variables ',&
420      & 'in restcom.F90')
421  ENDIF
422  IF (nb_var > max_var) THEN
423    CALL ipslerr (3,'restopenin',&
424      & 'More variables present in file that can be store',&
425      & 'Please increase max_var in the global variables ',&
426      & 'in restcom.F90')
427  ENDIF
428!-
429  nbvar_in(fid) = nb_var
430  nbdim_in(fid) = nb_dim
431  iread = -1; jread = -1; lread = -1;
432  DO id=1,nb_dim
433    iret = NF90_INQUIRE_DIMENSION(ncfid,id, &
434 &           len=dimlen(id),name=dimname(id))
435    IF (l_dbg) THEN
436      WRITE (*,*) "restopenin 0.0 dimname",id,TRIM(dimname(id))
437    ENDIF
438    IF      (TRIM(dimname(id)) == 'x') THEN
439      iread = dimlen(id)
440      IF (l_dbg) WRITE (*,*) "iread",iread
441    ELSE IF (TRIM(dimname(id)) == 'y') THEN
442      jread = dimlen(id)
443      IF (l_dbg) WRITE (*,*) "jread",jread
444    ELSE IF (TRIM(dimname(id)) == 'z') THEN
445      lread = dimlen(id)
446      IF (l_dbg) WRITE (*,*) "lread",lread
447    ENDIF
448  ENDDO
449!-
450  IF (id_unl > 0) THEN
451!---
452!-- 0.1 If we are going to add values to this file
453!--     we need to know where it ends
454!--     We also need to have all the dimensions in the file
455!---
456    IF (l_rw) THEN
457      tstp_out(fid) = dimlen(id_unl)
458      itau_out(fid) = -1
459      tdimid_out(fid) =  tdimid_in(fid)
460      IF (l_dbg) THEN
461        WRITE (*,*) &
462 &       "restopenin 0.0 unlimited axis dimname", &
463 &       dimname(id_unl),tstp_out(fid)
464      ENDIF
465!-----
466      xax_nb(fid) = 0
467      yax_nb(fid) = 0
468      zax_nb(fid) = 0
469!-----
470      DO id=1,nb_dim
471        IF      (dimname(id)(1:1) == 'x') THEN
472          xax_nb(fid) = xax_nb(fid)+1
473          xax_infs(fid,xax_nb(fid),1) = dimlen(id)
474          xax_infs(fid,xax_nb(fid),2) = id
475        ELSE IF (dimname(id)(1:1) == 'y') THEN
476          yax_nb(fid) = yax_nb(fid)+1
477          yax_infs(fid,yax_nb(fid),1) = dimlen(id)
478          yax_infs(fid,yax_nb(fid),2) = id
479        ELSE IF (dimname(id)(1:1) == 'z') THEN
480          zax_nb(fid) = zax_nb(fid)+1
481          zax_infs(fid,zax_nb(fid),1) = dimlen(id)
482          zax_infs(fid,zax_nb(fid),2) = id
483        ENDIF
484      ENDDO
485    ENDIF
486  ELSE
487!---
488!-- Still need to find a method for dealing with this
489!---
490!  CALL ipslerr (3,'restopenin',&
491!    & ' We do not deal yet with files without time axis.',' ',' ')
492  ENDIF
493!-
494! 1.0 First let us check that we have the righ restart file
495!-
496  IF ((iread /= iim).OR.(jread /= jjm).OR.(lread /= llm)) THEN
497    CALL ipslerr (3,'restopenin',&
498 &    'The grid of the restart file does not correspond',&
499 &    'to that of the model',' ')
500  ENDIF
501!-
502! 2.0 Get the list of variables
503!-
504  IF (l_dbg) WRITE(*,*) 'restopenin 1.2'
505!-
506  lat_vid = -1
507  lon_vid = -1
508  tind_varid_in(fid) = -1
509  tax_varid_in(fid) = -1
510!-
511  DO iv=1,nb_var
512!---
513    varid_in(fid,iv) = iv
514    var_dims(:) = 0
515    iret = NF90_INQUIRE_VARIABLE(ncfid,iv, &
516 &           name=varname_in(fid,iv),xtype=vartyp_in(fid,iv), &
517 &           ndims=varnbdim_in(fid,iv),dimids=var_dims, &
518 &           nAtts=varatt_in(fid,iv))
519!---
520    DO id=1,varnbdim_in(fid,iv)
521      iret = NF90_INQUIRE_DIMENSION &
522 &             (ncfid,var_dims(id),len=vardims_in(fid,iv,id))
523    ENDDO
524!---
525!-- 2.1 Read the units of the variable
526!---
527    units=''
528    iret = NF90_GET_ATT(ncfid,iv,'units',units)
529    CALL strlowercase (units)
530    CALL cmpblank (units)
531!---
532!-- 2.2 Catch the time variables
533!---
534    IF (varnbdim_in(fid,iv) == 1) THEN
535      IF (     (INDEX(units,'timesteps since') > 0) &
536          .AND.(tind_varid_in(fid) < 0) ) THEN
537        tind_varid_in(fid) = iv
538        tax_size_in(fid) = vardims_in(fid,iv,1)
539      ENDIF
540      IF (     (INDEX(units,'seconds since') > 0) &
541          .AND.(tax_varid_in(fid) < 0) ) THEN
542        tax_varid_in(fid) = iv
543        tax_size_in(fid) = vardims_in(fid,iv,1)
544      ENDIF
545    ENDIF
546!---
547!-- 2.3 Catch longitude and latitude variables
548!---
549    IF      (INDEX(units,'degrees_nort') > 0) THEN
550      lat_vid = iv
551    ELSE IF (INDEX(units,'degrees_east') > 0) THEN
552      lon_vid = iv
553    ENDIF
554!---
555  ENDDO
556!-
557! 2.4 None of the variables was yet read
558!-
559  nbvar_read(fid) = 0
560  touched_in(fid,:) = .FALSE.
561!-
562! 3.0 Reading the coordinates from the input restart file
563!-
564  lon_read = missing_val
565  lat_read = missing_val
566!-
567  IF (lon_vid < 0 .OR. lat_vid < 0) THEN
568    CALL ipslerr (3,'restopenin',&
569      & ' No variables containing longitude or latitude were ',&
570      & ' found in the restart file.',' ')
571  ELSE
572    iret = NF90_GET_VAR(ncfid,lon_vid,lon_read)
573    iret = NF90_GET_VAR(ncfid,lat_vid,lat_read)
574!---
575    IF (  (ABS( MAXVAL(lon(:,:)) &
576 &             -MINVAL(lon(:,:))) < EPSILON(MAXVAL(lon(:,:)))) &
577 &   .AND.(ABS( MAXVAL(lat(:,:)) &
578 &             -MINVAL(lat(:,:))) < EPSILON(MAXVAL(lat(:,:)))) ) THEN
579!-----
580!---- 3.1 No longitude nor latitude are provided thus
581!---- they are taken from the restart file
582!-----
583      lon(:,:) = lon_read(:,:)
584      lat(:,:) = lat_read(:,:)
585    ELSE
586!-----
587!---- 3.2 We check that the longitudes and latitudes
588!----     in the file and the model are the same
589!-----
590      mdlon = MAXVAL(ABS(lon_read-lon))
591      mdlat = MAXVAL(ABS(lat_read-lat))
592!-----
593!---- We can not test against epsilon here as the longitude
594!---- can be stored at another precision in the netCDF file.
595!---- The test here does not need to be very precise.
596!-----
597      IF (mdlon > 1.e-4 .OR. mdlat > 1.e-4) THEN
598        CALL ipslerr (3,'restopenin',&
599          & ' The longitude or latitude found in the restart ',&
600          & ' file are not the same as the ones used in the model.',&
601          & ' ')
602      ENDIF
603    ENDIF
604  ENDIF
605!------------------------
606END SUBROUTINE restopenin
607!===
608SUBROUTINE restsett (timestep,date0,itau,owrite_time_in)
609!---------------------------------------------------------------------
610!- Here we get all the time information from the file.
611!-
612!- The time information can come in three forms :
613!- -global attributes which give the time origine and the
614!-  time step is taken from the input to restinit
615!- -A physical time exists and thus the julian date from the
616!-  input is used for positioning using the itau as input
617!- -A time-step axis exists and itau is positioned on it.
618!-
619!- What takes precedence : the model
620!-
621!- itau     : Time step of the model
622!-
623!- Optional INPUT arguments
624!-
625!- owrite_time_in : logical  argument which allows to
626!-                  overwrite the time in the restart file
627!---------------------------------------------------------------------
628  IMPLICIT NONE
629!-
630  REAL :: date0,timestep
631  INTEGER :: itau
632  LOGICAL,OPTIONAL :: owrite_time_in
633!-
634  INTEGER :: ncfid,iret,it,iax,iv
635  CHARACTER(LEN=80) :: itau_orig,tax_orig,calendar
636  CHARACTER(LEN=9) :: tmp_cal
637  INTEGER :: year0,month0,day0,hours0,minutes0,seci
638  REAL :: sec0,one_day,one_year,date0_ju,ttmp
639  CHARACTER :: strc
640  LOGICAL :: ow_time
641  LOGICAL :: l_dbg
642!---------------------------------------------------------------------
643  CALL ipsldbg (old_status=l_dbg)
644!-
645  IF (PRESENT(owrite_time_in)) THEN
646    ow_time = owrite_time_in
647  ELSE
648    ow_time = .FALSE.
649  ENDIF
650!-
651  ncfid = netcdf_id(nb_fi,1)
652!-
653! Allocate the space we need for the time axes
654!-
655  CALL rest_atim (l_dbg,'restsett')
656!-
657! Get the calendar if possible. Else it will be gregorian.
658!-
659  IF (tax_size_in(nb_fi) > 0) THEN
660    iret = NF90_GET_ATT(ncfid,tax_varid_in(nb_fi),'calendar',calendar)
661    IF (iret == NF90_NOERR) THEN
662      CALL ioconf_calendar (calendar)
663      IF (l_dbg) THEN
664        WRITE(*,*) 'restsett : calendar of the restart ',calendar
665      ENDIF
666    ENDIF
667  ENDIF
668  CALL ioget_calendar (one_year,one_day)
669  IF (l_dbg) THEN
670    WRITE(*,*) 'one_year,one_day = ',one_year,one_day
671  ENDIF
672!-
673  itau_orig = 'XXXXX'
674  tax_orig  = 'XXXXX'
675!-
676! Get the time steps of the time axis if available on the restart file
677!-
678  IF (tind_varid_in(nb_fi) > 0) THEN
679    IF (ow_time) THEN
680      t_index(nb_fi,:) = itau
681      IF (l_dbg) THEN
682        WRITE(*,*) "nb_fi,t_index",nb_fi,t_index(nb_fi,:)
683      ENDIF
684      CALL ju2ymds (date0,year0,month0,day0,sec0)
685      hours0 = NINT(sec0/3600)
686      sec0 = sec0 - 3600 * hours0
687      minutes0 = NINT(sec0 / 60)
688      sec0 = sec0 - 60 * minutes0
689      seci = NINT(sec0)
690      strc=':'
691      IF (l_dbg) THEN
692        WRITE(*,*) date0
693        WRITE (UNIT=itau_orig,FMT='(I4.4,5(A,I2.2))') &
694 &       year0,'-',month0,'-',day0,' ',hours0,':',minutes0,':',seci
695        WRITE(*,*) "itau_orig : ",itau_orig
696      ENDIF
697    ELSE
698      iret = NF90_GET_VAR(ncfid,tind_varid_in(nb_fi),t_index(nb_fi,:))
699      IF (l_dbg) THEN
700        WRITE(*,*) "restsett, time axis : ",t_index(nb_fi,:)
701      ENDIF
702      iret = NF90_GET_ATT(ncfid,tind_varid_in(nb_fi),'units',itau_orig)
703      itau_orig = &
704 &      itau_orig(INDEX(itau_orig,'since')+6:LEN_TRIM(itau_orig))
705      iret = &
706 &      NF90_GET_ATT(ncfid,tind_varid_in(nb_fi),'tstep_sec',timestep)
707!-----
708!---- This time origin will dominate as it is linked to the time steps.
709!-----
710      READ (UNIT=itau_orig,FMT='(I4.4,5(A,I2.2))') &
711 &      year0,strc,month0,strc,day0,strc, &
712 &      hours0,strc,minutes0,strc,seci
713      sec0 = REAL(seci)
714      sec0 = hours0*3600.+minutes0*60.+sec0
715      CALL ymds2ju (year0,month0,day0,sec0,date0)
716    ENDIF
717  ENDIF
718!-
719! If a julian day time axis is available then we get it
720!-
721  IF (tax_varid_in(nb_fi) > 0) THEN
722    iret = NF90_GET_VAR(ncfid,tax_varid_in(nb_fi),t_julian(nb_fi,:))
723    iret = NF90_GET_ATT(ncfid,tax_varid_in(nb_fi),'units',tax_orig)
724    tax_orig = tax_orig(INDEX(tax_orig,'since')+6:LEN_TRIM(tax_orig))
725    iret = NF90_GET_ATT(ncfid,tax_varid_in(nb_fi),'calendar',tmp_cal)
726    IF (l_dbg) THEN
727      WRITE(*,*) 'restsett : tmp_calendar of the restart ',tmp_cal
728    ENDIF
729!---
730    CALL strlowercase (tmp_cal)
731    IF (INDEX(calend_str,tmp_cal) < 1) THEN
732      IF (INDEX(calend_str,'unknown') > 0) THEN
733        calend_str = tmp_cal
734      ELSE
735        CALL ipslerr (3,'restsett', &
736 &       ' In the restart files two different calendars were found.', &
737 &       ' Please check the files you have used',' ')
738      ENDIF
739    ENDIF
740!---
741!-- We need to transform that into julian days
742!-- to get ride of the intial date.
743!---
744    IF (l_dbg) WRITE(*,*) 'tax_orig : ',TRIM(tax_orig)
745    READ (UNIT=tax_orig,FMT='(I4.4,5(a,I2.2))') &
746      year0,strc,month0,strc,day0,strc, &
747      hours0,strc,minutes0,strc,seci
748    sec0 = REAL(seci)
749    sec0 = hours0*3600.+minutes0*60.+sec0
750    CALL ymds2ju (year0,month0,day0,sec0,date0_ju)
751    t_julian(nb_fi,:) = t_julian(nb_fi,:)/one_day+date0_ju
752  ENDIF
753!-
754  IF (     (INDEX(itau_orig,'XXXXX') > 0) &
755      .AND.(INDEX(tax_orig,'XXXXX')  < 1) ) THEN
756!!- Compute the t_itau from the date read and the timestep in the input
757  ENDIF
758!-
759  IF (     (INDEX(tax_orig,'XXXXX')  > 0) &
760      .AND.(INDEX(itau_orig,'XXXXX') < 1) ) THEN
761    DO it=1,tax_size_in(nb_fi)
762      t_julian(nb_fi,it) = itau2date(t_index(nb_fi,it),date0,timestep)
763    ENDDO
764  ENDIF
765!-
766! If neither the indices or time is present then get global attributes
767! This is for compatibility reasons and should not be used.
768!-
769  IF ((tax_varid_in(nb_fi) < 0).AND.(tind_varid_in(nb_fi) < 0)) THEN
770    iax = -1
771    DO iv=1,nbvar_in(nb_fi)
772      IF (    (INDEX(varname_in(nb_fi,iv),'tsteps') > 0) &
773 &        .OR.(INDEX(varname_in(nb_fi,iv),'time_steps') > 0)) THEN
774        iax = iv
775      ENDIF
776    ENDDO
777!---
778    IF (iax < 0) THEN
779      CALL ipslerr (3,'restsett',&
780        & 'No time axis was found in the restart file. Please check',&
781        & 'that it corresponds to the convention used in restsett',&
782        & ' ')
783    ELSE
784      iret = NF90_GET_VAR(ncfid,tind_varid_in(nb_fi),t_index(nb_fi,:))
785      iret = NF90_GET_ATT(ncfid,NF90_GLOBAL,'delta_tstep_sec',timestep)
786      iret = NF90_GET_ATT(ncfid,NF90_GLOBAL,'year0',ttmp)
787      year0 = NINT(ttmp)
788      iret = NF90_GET_ATT(ncfid,NF90_GLOBAL,'month0',ttmp)
789      month0 = NINT(ttmp)
790      iret = NF90_GET_ATT(ncfid,NF90_GLOBAL,'day0',ttmp)
791      day0 = NINT(ttmp)
792      iret = NF90_GET_ATT(ncfid,NF90_GLOBAL,'sec0',sec0)
793!---
794      CALL ymds2ju (year0,month0,day0,sec0,date0)
795      t_julian(nb_fi,1) = itau2date(t_index(nb_fi,1),date0,timestep)
796    ENDIF
797  ENDIF
798!----------------------
799END SUBROUTINE restsett
800!===
801SUBROUTINE restopenout &
802  (fid,fname,iim,jjm, &
803   lon,lat,llm,lev,timestep,date,ncfid,domain_id)
804!---------------------------------------------------------------------
805!- Opens the restart file for output.
806!- The longitude and time variables are written.
807!---------------------------------------------------------------------
808  IMPLICIT NONE
809!-
810  INTEGER,INTENT(IN) :: fid,iim,jjm,llm
811  CHARACTER(LEN=*) :: fname
812  REAL :: date,timestep
813  REAL :: lon(iim,jjm),lat(iim,jjm),lev(llm)
814  INTEGER,INTENT(OUT) :: ncfid
815  INTEGER,INTENT(IN),OPTIONAL :: domain_id
816!-
817  INTEGER :: iret
818  CHARACTER(LEN=70) :: str_t
819  INTEGER :: x_id,y_id,z_id,itauid
820  INTEGER :: nlonid,nlatid,nlevid,timeid
821  INTEGER :: year,month,day,hours,minutes
822  REAL :: sec
823  CHARACTER(LEN=3),DIMENSION(12) :: &
824    cal = (/'JAN','FEB','MAR','APR','MAY','JUN', &
825            'JUL','AUG','SEP','OCT','NOV','DEC'/)
826  CHARACTER(LEN=30) :: timenow
827  LOGICAL :: l_dbg
828!---------------------------------------------------------------------
829  CALL ipsldbg (old_status=l_dbg)
830!-
831  IF (l_dbg) WRITE(*,*) "restopenout 0.0 ",TRIM(fname)
832!-
833!  If we use the same file for input and output
834!- we will not even call restopenout
835!-
836  iret = NF90_CREATE(fname,NF90_NOCLOBBER,ncfid)
837  IF (iret == -35) THEN
838    CALL ipslerr (3,'restopenout',&
839      & ' The restart file aready exists on the disc. IOIPSL ',&
840      & ' will not overwrite it. You should remove the old one or ',&
841      & ' generate the new one with another name')
842  ENDIF
843!-
844  iret = NF90_DEF_DIM(ncfid,'x',iim,x_id)
845  xax_nb(fid) = xax_nb(fid)+1
846  xax_infs(fid,xax_nb(fid),1) = iim
847  xax_infs(fid,xax_nb(fid),2) = x_id
848!-
849  iret = NF90_DEF_DIM(ncfid,'y',jjm,y_id)
850  yax_nb(fid)  = yax_nb(fid)+1
851  yax_infs(fid,yax_nb(fid),1) = jjm
852  yax_infs(fid,yax_nb(fid),2) = y_id
853!-
854  iret = NF90_DEF_DIM(ncfid,'z',llm,z_id)
855  zax_nb(fid) = zax_nb(fid)+1
856  zax_infs(fid,zax_nb(fid),1) = llm
857  zax_infs(fid,zax_nb(fid),2) = z_id
858!-
859  iret = NF90_DEF_DIM(ncfid,'time',NF90_UNLIMITED,tdimid_out(fid))
860!-
861! 1.0 Longitude
862!-
863  IF (l_dbg) WRITE(*,*) "restopenout 1.0"
864!-
865  iret = NF90_DEF_VAR(ncfid,"nav_lon",NF90_FLOAT,(/x_id,y_id/),nlonid)
866  iret = NF90_PUT_ATT(ncfid,nlonid,'units',"degrees_east")
867  iret = NF90_PUT_ATT(ncfid,nlonid,'valid_min',REAL(-180.,KIND=4))
868  iret = NF90_PUT_ATT(ncfid,nlonid,'valid_max',REAL( 180.,KIND=4))
869  iret = NF90_PUT_ATT(ncfid,nlonid,'long_name',"Longitude")
870!-
871! 2.0 Latitude
872!-
873  IF (l_dbg) WRITE(*,*) "restopenout 2.0"
874!-
875  iret = NF90_DEF_VAR(ncfid,"nav_lat",NF90_FLOAT,(/x_id,y_id/),nlatid)
876  iret = NF90_PUT_ATT(ncfid,nlatid,'units',"degrees_north")
877  iret = NF90_PUT_ATT(ncfid,nlatid,'valid_min',REAL(-90.,KIND=4))
878  iret = NF90_PUT_ATT(ncfid,nlatid,'valid_max',REAL( 90.,KIND=4))
879  iret = NF90_PUT_ATT(ncfid,nlatid,'long_name',"Latitude")
880!-
881! 3.0 Levels
882!-
883  IF (l_dbg) WRITE(*,*) "restopenout 3.0"
884!-
885  iret = NF90_DEF_VAR(ncfid,"nav_lev",NF90_FLOAT,z_id,nlevid)
886  iret = NF90_PUT_ATT(ncfid,nlevid,'units',"model_levels")
887  iret = NF90_PUT_ATT(ncfid,nlevid,'valid_min', &
888 &                     REAL(MINVAL(lev),KIND=4))
889  iret = NF90_PUT_ATT(ncfid,nlevid,'valid_max', &
890 &                     REAL(MAXVAL(lev),KIND=4))
891  iret = NF90_PUT_ATT(ncfid,nlevid,'long_name',"Model levels")
892!-
893! 4.0 Time axis, this is the seconds since axis
894!-
895  IF (l_dbg) WRITE(*,*) "restopenout 4.0"
896!-
897  iret = NF90_DEF_VAR(ncfid,"time",NF90_FLOAT, &
898                       tdimid_out(fid),timeid)
899  tax_varid_out(fid) = timeid
900!-
901  timeorig(fid) = date
902  CALL ju2ymds (date,year,month,day,sec)
903  hours   = INT(sec/(60.*60.))
904  minutes = INT((sec-hours*60.*60.)/60.)
905  sec     = sec-(hours*60.*60.+minutes*60.)
906  WRITE (UNIT=str_t, &
907   FMT='("seconds since ",I4.4,2("-",I2.2)," ",I2.2,2(":",I2.2))') &
908 &  year,month,day,hours,minutes,INT(sec)
909  iret = NF90_PUT_ATT(ncfid,timeid,'units',TRIM(str_t))
910!-
911  CALL ioget_calendar (str_t)
912  iret = NF90_PUT_ATT(ncfid,timeid,'calendar',TRIM(str_t))
913  iret = NF90_PUT_ATT(ncfid,timeid,'title','Time')
914  iret = NF90_PUT_ATT(ncfid,timeid,'long_name','Time axis')
915!-
916  WRITE(UNIT=str_t, &
917   FMT='(" ",I4.4,"-",A3,"-",I2.2," ",I2.2,2(":",I2.2))') &
918 &  year,cal(month),day,hours,minutes,INT(sec)
919  iret = NF90_PUT_ATT(ncfid,timeid,'time_origin',TRIM(str_t))
920!-
921! 5.0 Time axis, this is the time steps since axis
922!-
923  IF (l_dbg) WRITE(*,*) "restopenout 5.0"
924!-
925  iret = NF90_DEF_VAR(ncfid,"time_steps",NF90_INT, &
926 &                    tdimid_out(fid),itauid)
927  tind_varid_out(fid) = itauid
928!-
929  CALL ju2ymds (date,year,month,day,sec)
930!-
931  hours   = INT(sec/(60.*60.))
932  minutes = INT((sec-hours*60.*60.)/60.)
933  sec     = sec-(hours*60.*60.+minutes*60.)
934!-
935  WRITE (UNIT=str_t, &
936   FMT='("timesteps since ",I4.4,2("-",I2.2)," ",I2.2,2(":",I2.2))') &
937 &  year,month,day,hours,minutes,INT(sec)
938!-
939  iret = NF90_PUT_ATT(ncfid,itauid,'units',TRIM(str_t))
940  iret = NF90_PUT_ATT(ncfid,itauid,'title','Time steps')
941  iret = NF90_PUT_ATT(ncfid,itauid,'tstep_sec',REAL(timestep,KIND=4))
942  iret = NF90_PUT_ATT(ncfid,itauid,'long_name','Time step axis')
943!-
944  WRITE(UNIT=str_t, &
945   FMT='(" ",I4.4,"-",A3,"-",I2.2," ",I2.2,2(":",I2.2))') &
946 &  year,cal(month),day,hours,minutes,INT(sec)
947  iret = NF90_PUT_ATT(ncfid,itauid,'time_origin',TRIM(str_t))
948!-
949!  5.2 Write global attributes
950!-
951  iret = NF90_PUT_ATT(ncfid,NF90_GLOBAL,'Conventions',"CF-1.1")
952  iret = NF90_PUT_ATT(ncfid,NF90_GLOBAL,'file_name',TRIM(fname))
953!!  TO BE DONE LATER
954!!   iret = NF90_PUT_ATT(ncfid,NF90_GLOBAL, &
955!!                       'production',TRIM(model_name))
956!!   lock_modname = .TRUE.
957  CALL ioget_timestamp (timenow)
958  iret = NF90_PUT_ATT(ncfid,NF90_GLOBAL,'TimeStamp',TRIM(timenow))
959!-
960! Add DOMAIN attributes if needed
961!-
962  CALL flio_dom_att (ncfid,domain_id)
963!-
964! 6.0 The coordinates are written to the file
965!-
966  iret = NF90_ENDDEF(ncfid)
967!-
968  iret = NF90_PUT_VAR(ncfid,nlonid,lon)
969  iret = NF90_PUT_VAR(ncfid,nlatid,lat)
970  iret = NF90_PUT_VAR(ncfid,nlevid,lev)
971!-
972! 7.0 Set a few variables related to the out file
973!-
974  nbvar_out(fid) = 0
975  itau_out(fid) = -1
976  tstp_out(fid) = 0
977  touched_out(fid,:) = .FALSE.
978!-
979! 7.1 The file is put back in define mode.
980!     This will last until itau_out >= 0
981!-
982  iret = NF90_REDEF(ncfid)
983!-
984  IF (l_dbg) WRITE(*,*) "restopenout END"
985!-------------------------
986END SUBROUTINE restopenout
987!===
988SUBROUTINE restget_opp_r1d &
989 & (fid,vname_q,iim,jjm,llm,itau,def_beha, &
990 &  var,MY_OPERATOR,nbindex,ijndex)
991!---------------------------------------------------------------------
992!- This subroutine serves as an interface to restget_real
993!-
994!- Should work as restput_opp_r1d but the other way around !
995!---------------------------------------------------------------------
996  IMPLICIT NONE
997!-
998  INTEGER :: fid
999  CHARACTER(LEN=*) :: vname_q
1000  INTEGER :: iim,jjm,llm,itau
1001  LOGICAL def_beha
1002  REAL :: var(:)
1003  CHARACTER(LEN=*) :: MY_OPERATOR
1004  INTEGER :: nbindex,ijndex(nbindex)
1005!-
1006  INTEGER :: req_sz,siz1
1007  REAL :: scal
1008  CHARACTER(LEN=7) :: topp
1009  LOGICAL :: l_dbg
1010!---------------------------------------------------------------------
1011  CALL ipsldbg (old_status=l_dbg)
1012!-
1013! 0.0 What size should be the data in the file
1014!-
1015  req_sz = 1
1016  IF (nbindex == iim .AND. jjm <= 1 .AND. llm <= 1) THEN
1017    IF (xax_infs(fid,1,1) > 0) req_sz = req_sz*xax_infs(fid,1,1)
1018    IF (yax_infs(fid,1,1) > 0) req_sz = req_sz*yax_infs(fid,1,1)
1019    IF (zax_infs(fid,1,1) > 0) req_sz = req_sz*zax_infs(fid,1,1)
1020  ELSE
1021    CALL ipslerr (3,'resget_opp_r1d', &
1022      'Unable to performe an operation on this variable as it has',&
1023      'a second and third dimension',vname_q)
1024  ENDIF
1025!-
1026! 1.0 Allocate the temporary buffer we need
1027!     to put the variable in right dimension
1028!-
1029  siz1 = SIZE(var)
1030  CALL rest_alloc (1,siz1,l_dbg,'restget_opp_r1d')
1031  CALL rest_alloc (2,req_sz,l_dbg,'restget_opp_r1d')
1032!-
1033! 2.0 Here we get the variable from the restart file
1034!-
1035  CALL restget_real &
1036    (fid,vname_q,xax_infs(fid,1,1),yax_infs(fid,1,1), &
1037     zax_infs(fid,1,1),itau,def_beha,buff_tmp2)
1038!-
1039! 4.0 Transfer the buffer obtained from the restart file
1040!     into the variable the model expects
1041!-
1042  topp = MY_OPERATOR(1:MIN(LEN_TRIM(MY_OPERATOR),7))
1043!-
1044  IF (INDEX(indchfun,topp(:LEN_TRIM(topp))) > 0) THEN
1045    scal = missing_val
1046    CALL mathop (topp,req_sz,buff_tmp2,missing_val, &
1047 &               nbindex,ijndex,scal,siz1,buff_tmp1)
1048    var(:) = buff_tmp1(1:siz1)
1049  ELSE
1050    CALL ipslerr (3,'resget_opp_r1d', &
1051      'The operation you wish to do on the variable for the ',&
1052      'restart file is not allowed.',topp)
1053  ENDIF
1054!-----------------------------
1055END SUBROUTINE restget_opp_r1d
1056!===
1057SUBROUTINE restget_opp_r2d &
1058 & (fid,vname_q,iim,jjm,llm,itau,def_beha, &
1059 &  var,MY_OPERATOR,nbindex,ijndex)
1060!---------------------------------------------------------------------
1061!- This subroutine serves as an interface to restget_real
1062!-
1063!- Should work as restput_opp_r2d but the other way around !
1064!---------------------------------------------------------------------
1065  IMPLICIT NONE
1066!-
1067  INTEGER :: fid
1068  CHARACTER(LEN=*) :: vname_q
1069  INTEGER :: iim,jjm,llm,itau
1070  LOGICAL def_beha
1071  REAL :: var(:,:)
1072  CHARACTER(LEN=*) :: MY_OPERATOR
1073  INTEGER :: nbindex,ijndex(nbindex)
1074!-
1075  INTEGER :: jj,req_sz,ist,var_sz,siz1
1076  REAL :: scal
1077  CHARACTER(LEN=7) :: topp
1078  LOGICAL :: l_dbg
1079!---------------------------------------------------------------------
1080  CALL ipsldbg (old_status=l_dbg)
1081!-
1082! 0.0 What size should be the data in the file
1083!-
1084  req_sz = 1
1085  IF (nbindex == iim  .AND. llm <= 1) THEN
1086    IF (xax_infs(fid,1,1) > 0) req_sz = req_sz*xax_infs(fid,1,1)
1087    IF (yax_infs(fid,1,1) > 0) req_sz = req_sz*yax_infs(fid,1,1)
1088  ELSE
1089    CALL ipslerr (3,'resget_opp_r2d', &
1090      'Unable to performe an operation on this variable as it has', &
1091      'a second and third dimension',vname_q)
1092  ENDIF
1093!-
1094  IF (jjm < 1) THEN
1095    CALL ipslerr (3,'resget_opp_r2d', &
1096      'Please specify a second dimension which is the', &
1097      'layer on which the operations are performed',vname_q)
1098  ENDIF
1099!-
1100! 1.0 Allocate the temporary buffer we need
1101!     to put the variable in right dimension
1102!-
1103  siz1 = SIZE(var,1)
1104  CALL rest_alloc (1,siz1,l_dbg,'restget_opp_r2d')
1105  CALL rest_alloc (2,req_sz*jjm,l_dbg,'restget_opp_r2d')
1106!-
1107! 2.0 Here we get the full variable from the restart file
1108!-
1109  CALL restget_real &
1110 & (fid,vname_q,xax_infs(fid,1,1),yax_infs(fid,1,1), &
1111 &  jjm,itau,def_beha,buff_tmp2)
1112!-
1113! 4.0 Transfer the buffer obtained from the restart file
1114!     into the variable the model expects
1115!-
1116  topp = MY_OPERATOR(1:MIN(LEN_TRIM(MY_OPERATOR),7))
1117!-
1118  IF (INDEX(indchfun,topp(:LEN_TRIM(topp))) > 0) THEN
1119    scal = missing_val
1120    var_sz = siz1
1121    DO jj = 1,jjm
1122      ist = (jj-1)*req_sz+1
1123      CALL mathop (topp,req_sz,buff_tmp2(ist:ist+req_sz-1), &
1124 &      missing_val,nbindex,ijndex,scal,var_sz,buff_tmp1)
1125      var(:,jj) = buff_tmp1(1:siz1)
1126    ENDDO
1127  ELSE
1128    CALL ipslerr (3,'resget_opp_r2d', &
1129      'The operation you wish to do on the variable for the ',&
1130      'restart file is not allowed.',topp)
1131  ENDIF
1132!-----------------------------
1133END SUBROUTINE restget_opp_r2d
1134!===
1135SUBROUTINE restget_r1d &
1136 & (fid,vname_q,iim,jjm,llm,itau,def_beha,var)
1137!---------------------------------------------------------------------
1138!- This subroutine serves as an interface to restget_real
1139!---------------------------------------------------------------------
1140  IMPLICIT NONE
1141!-
1142  INTEGER :: fid
1143  CHARACTER(LEN=*) :: vname_q
1144  INTEGER :: iim,jjm,llm,itau
1145  LOGICAL :: def_beha
1146  REAL :: var(:)
1147!-
1148  INTEGER :: ji,jl,req_sz,var_sz,siz1
1149  CHARACTER(LEN=70) :: str,str2
1150  LOGICAL :: l_dbg
1151!---------------------------------------------------------------------
1152  CALL ipsldbg (old_status=l_dbg)
1153!-
1154! 1.0 Allocate the temporary buffer we need
1155!     to put the variable in right dimension
1156!-
1157  siz1 = SIZE(var)
1158  var_sz = siz1
1159  CALL rest_alloc (1,var_sz,l_dbg,'restget_r1d')
1160!-
1161! 2.0 Here we could check if the sizes specified agree
1162!     with the size of the variable provided
1163!-
1164  req_sz = 1
1165  IF (iim > 0) req_sz = req_sz*iim
1166  IF (jjm > 0) req_sz = req_sz*jjm
1167  IF (llm > 0) req_sz = req_sz*llm
1168  IF (req_sz > var_sz) THEN
1169    WRITE(str, &
1170 &    '("Size of variable requested from file should be ",I6)') req_sz
1171    WRITE(str2, &
1172 &    '("but the provided variable can only hold ",I6)') var_sz
1173    CALL ipslerr (3,'restget_r1d',str,str2,' ')
1174  ENDIF
1175  IF (req_sz < var_sz) THEN
1176    WRITE(str, &
1177 &    '("the size of variable requested from file is ",I6)') req_sz
1178    WRITE(str2, &
1179 &    '("but the provided variable can hold ",I6)') var_sz
1180    CALL ipslerr (2,'restget_r1d', &
1181      'There could be a problem here :',str,str2)
1182  ENDIF
1183!-
1184  CALL restget_real &
1185 & (fid,vname_q,iim,jjm,llm,itau,def_beha,buff_tmp1)
1186!-
1187! 4.0 Transfer the buffer obtained from the restart file
1188!     into the variable the model expects
1189!-
1190  jl=0
1191  DO ji=1,siz1
1192    jl=jl+1
1193    var(ji) = buff_tmp1(jl)
1194  ENDDO
1195!-------------------------
1196END SUBROUTINE restget_r1d
1197!===
1198SUBROUTINE restget_r2d &
1199 & (fid,vname_q,iim,jjm,llm,itau,def_beha,var)
1200!---------------------------------------------------------------------
1201!- This subroutine serves as an interface to restget_real
1202!---------------------------------------------------------------------
1203  IMPLICIT NONE
1204!-
1205  INTEGER :: fid
1206  CHARACTER(LEN=*) :: vname_q
1207  INTEGER :: iim,jjm,llm,itau
1208  LOGICAL :: def_beha
1209  REAL :: var(:,:)
1210!-
1211  INTEGER :: ji,jj,jl,req_sz,var_sz,siz1,siz2
1212  CHARACTER(LEN=70) :: str,str2
1213  LOGICAL :: l_dbg
1214!---------------------------------------------------------------------
1215  CALL ipsldbg (old_status=l_dbg)
1216!-
1217! 1.0 Allocate the temporary buffer we need
1218!     to put the variable in right dimension
1219!-
1220  siz1 = SIZE(var,1)
1221  siz2 = SIZE(var,2)
1222  var_sz = siz1*siz2
1223  CALL rest_alloc (1,var_sz,l_dbg,'restget_r2d')
1224!-
1225! 2.0 Here we check if the sizes specified agree
1226!     with the size of the variable provided
1227!-
1228  req_sz = 1
1229  IF (iim > 0) req_sz = req_sz*iim
1230  IF (jjm > 0) req_sz = req_sz*jjm
1231  IF (llm > 0) req_sz = req_sz*llm
1232  IF (req_sz > var_sz) THEN
1233    WRITE(str, &
1234 &    '("Size of variable ",A, &
1235 &      //" requested from file should be ",I6)') TRIM(vname_q),req_sz
1236    WRITE(str2, &
1237 &    '("but the provided variable can only hold ",I6)') var_sz
1238    CALL ipslerr (3,'restget_r2d',str,str2,' ')
1239  ENDIF
1240  IF (req_sz < var_sz) THEN
1241    WRITE(str, &
1242 &    '("Size of variable ",A, &
1243 &      //" requested from file is ",I6)') TRIM(vname_q),req_sz
1244    WRITE(str2,'("but the provided variable can hold ",I6)') var_sz
1245    CALL ipslerr (2,'restget_r2d', &
1246      'There could be a problem here :',str,str2)
1247  ENDIF
1248!-
1249  CALL restget_real &
1250 & (fid,vname_q,iim,jjm,llm,itau,def_beha,buff_tmp1)
1251!-
1252! 4.0 Transfer the buffer obtained from the restart file
1253!     into the variable the model expects
1254!-
1255  jl=0
1256  DO jj=1,siz2
1257    DO ji=1,siz1
1258      jl=jl+1
1259      var(ji,jj) = buff_tmp1(jl)
1260    ENDDO
1261  ENDDO
1262!-------------------------
1263END SUBROUTINE restget_r2d
1264!===
1265SUBROUTINE restget_r3d &
1266  (fid,vname_q,iim,jjm,llm,itau,def_beha,var)
1267!---------------------------------------------------------------------
1268!- This subroutine serves as an interface to restget_real
1269!---------------------------------------------------------------------
1270  IMPLICIT NONE
1271!-
1272  INTEGER :: fid
1273  CHARACTER(LEN=*) :: vname_q
1274  INTEGER :: iim,jjm,llm,itau
1275  LOGICAL def_beha
1276  REAL :: var(:,:,:)
1277!-
1278  INTEGER :: ji,jj,jk,jl,req_sz,var_sz,siz1,siz2,siz3
1279  CHARACTER(LEN=70) :: str,str2
1280  LOGICAL :: l_dbg
1281!---------------------------------------------------------------------
1282  CALL ipsldbg (old_status=l_dbg)
1283!-
1284! 1.0 Allocate the temporary buffer we need
1285!     to put the variable in right dimension
1286!-
1287  siz1 = SIZE(var,1)
1288  siz2 = SIZE(var,2)
1289  siz3 = SIZE(var,3)
1290  var_sz = siz1*siz2*siz3
1291  CALL rest_alloc (1,var_sz,l_dbg,'restget_r3d')
1292!-
1293! 2.0 Here we check if the sizes specified agree
1294!     with the size of the variable provided
1295!-
1296  req_sz = 1
1297  IF (iim > 0) req_sz = req_sz*iim
1298  IF (jjm > 0) req_sz = req_sz*jjm
1299  IF (llm > 0) req_sz = req_sz*llm
1300  IF (req_sz > var_sz) THEN
1301    WRITE(str, &
1302 &    '("Size of variable ",A, &
1303 &      //" requested from file should be ",I6)') TRIM(vname_q),req_sz
1304    WRITE(str2, &
1305 &    '("but the provided variable can only hold ",I6)') var_sz
1306    CALL ipslerr (3,'restget_r3d',str,str2,' ')
1307  ENDIF
1308  IF (req_sz < var_sz) THEN
1309    WRITE(str, &
1310 &    '("Size of variable ",A, &
1311 &      //" requested from file is ",I6)') TRIM(vname_q),req_sz
1312    WRITE(str2,'("but the provided variable can hold ",I6)') var_sz
1313    CALL ipslerr (2,'restget_r3d', &
1314      'There could be a problem here :',str,str2)
1315  ENDIF
1316!-
1317  CALL restget_real &
1318    (fid,vname_q,iim,jjm,llm,itau,def_beha,buff_tmp1)
1319!-
1320! 4.0 Transfer the buffer obtained from the restart file
1321!     into the variable the model expects
1322!-
1323  jl=0
1324  DO jk=1,siz3
1325    DO jj=1,siz2
1326      DO ji=1,siz1
1327        jl=jl+1
1328        var(ji,jj,jk) = buff_tmp1(jl)
1329      ENDDO
1330    ENDDO
1331  ENDDO
1332!-------------------------
1333END SUBROUTINE restget_r3d
1334!===
1335SUBROUTINE restget_real &
1336  (fid,vname_q,iim,jjm,llm,itau,def_beha,var)
1337!---------------------------------------------------------------------
1338!- This subroutine is for getting a variable from the restart file.
1339!- A number of verifications will be made :
1340!- - Is this the first time we read this variable ?
1341!- - Are the dimensions correct ?
1342!- - Is the correct time step present in the file
1343!- - is a default behaviour possible. If not the model is stoped.
1344!- Default procedure is to write the content of val_exp on all values.
1345!-
1346!- INPUT
1347!-
1348!- fid            : Identification of the file
1349!- vname_q        : Name of the variable to be read
1350!- iim, jjm ,llm  : Dimensions of the variable that should be read
1351!- itau           : Time step at whcih we are when we want
1352!-                  to read the variable
1353!- def_beha       : If the model can restart without this variable
1354!-                  then some strange value is given.
1355!-
1356!- OUTPUT
1357!-
1358!- var            : Variable in which the data is put
1359!---------------------------------------------------------------------
1360  IMPLICIT NONE
1361!-
1362  INTEGER :: fid
1363  CHARACTER(LEN=*) :: vname_q
1364  INTEGER :: iim,jjm,llm,itau
1365  LOGICAL :: def_beha
1366  REAL :: var(:)
1367!-
1368  INTEGER :: vid,vnb,ncfid,iret,index,it,ndim,ia
1369  CHARACTER(LEN=70) str,str2
1370  CHARACTER(LEN=80) attname
1371  INTEGER,DIMENSION(4) :: corner,edge
1372!---------------------------------------------------------------------
1373  ncfid = netcdf_id(fid,1)
1374!-
1375  CALL find_str (varname_in(fid,1:nbvar_in(fid)),vname_q,vnb)
1376!-
1377! 1.0 If the variable is not present then ERROR or filled up
1378!     by default values if allowed
1379!-
1380  IF (vnb < 0) THEN
1381    IF (def_beha) THEN
1382!-----
1383      lock_valexp = .TRUE.
1384      var(:) = val_exp
1385!----
1386      str = 'Variable '//TRIM(vname_q) &
1387          //' is not present in the restart file'
1388      CALL ipslerr (1,'restget', &
1389 &      str,'but default values are used to fill in',' ')
1390!----
1391      IF (nbvar_in(fid) >= max_var) THEN
1392        CALL ipslerr (3,'restget', &
1393         'Too many variables for the restcom module', &
1394         'Please increase the value of max_var',' ')
1395      ENDIF
1396      nbvar_in(fid) = nbvar_in(fid)+1
1397      vnb = nbvar_in(fid)
1398      varname_in(fid,vnb) = vname_q
1399      touched_in(fid,vnb) = .TRUE.
1400!-----
1401      CALL restdefv (fid,vname_q,iim,jjm,llm,.TRUE.)
1402!-----
1403    ELSE
1404      str = 'Variable '//TRIM(vname_q) &
1405          //' is not present in the restart file'
1406      CALL ipslerr (3,'restget', &
1407 &      str,'but it is need to restart the model',' ')
1408    ENDIF
1409!---
1410  ELSE
1411!---
1412!--  2.0 Check if the variable has not yet been read
1413!--      and that the time is OK
1414!---
1415    vid = varid_in(fid,vnb)
1416!---
1417    nbvar_read(fid) = nbvar_read(fid)+1
1418!---
1419    IF (touched_in(fid,vnb)) THEN
1420      str = 'Variable '//TRIM(vname_q) &
1421          //' has already been read from file'
1422      CALL ipslerr (3,'restget',str,' ',' ')
1423    ENDIF
1424!---
1425!-- 3.0 get the time step of the restart file
1426!--     and check if it is correct
1427!---
1428    index = -1
1429    DO it=1,tax_size_in(fid)
1430      IF (t_index(fid,it) == itau)  index = it
1431    ENDDO
1432    IF (index < 0) THEN
1433      str = 'The time step requested for variable '//TRIM(vname_q)
1434      CALL ipslerr (3,'restget', &
1435 &      str,'is not available in the current file',' ')
1436    ENDIF
1437!---
1438!-- 4.0 Read the data. Note that the variables in the restart files
1439!--     have no time axis is and thus we write -1
1440!---
1441    str='Incorrect dimension for '//TRIM(vname_q)
1442    ndim = 0
1443    IF (iim > 0) THEN
1444      ndim = ndim+1
1445      IF (vardims_in(fid,vnb,ndim) == iim) THEN
1446        corner(ndim) = 1
1447        edge(ndim) = iim
1448      ELSE
1449        WRITE (str2,'("Incompatibility for iim : ",I6,I6)') &
1450             iim,vardims_in(fid,vnb,ndim)
1451        CALL ipslerr (3,'restget',str,str2,' ')
1452      ENDIF
1453    ENDIF
1454!---
1455    IF (jjm > 0) THEN
1456      ndim = ndim+1
1457      IF (vardims_in(fid,vnb,ndim) == jjm) THEN
1458        corner(ndim) = 1
1459        edge(ndim) = jjm
1460      ELSE
1461        WRITE (str2,'("Incompatibility for jjm : ",I6,I6)') &
1462             jjm,vardims_in(fid,vnb,ndim)
1463        CALL ipslerr (3,'restget',str,str2,' ')
1464      ENDIF
1465    ENDIF
1466!---
1467    IF (llm > 0) THEN
1468      ndim = ndim+1
1469      IF (vardims_in(fid,vnb,ndim) == llm) THEN
1470        corner(ndim) = 1
1471        edge(ndim) = llm
1472      ELSE
1473        WRITE (str2,'("Incompatibility for llm : ",I6,I6)') &
1474             llm,vardims_in(fid,vnb,ndim)
1475        CALL ipslerr (3,'restget',str,str2,' ')
1476      ENDIF
1477    ENDIF
1478!---
1479!-- Time
1480!---
1481    ndim = ndim+1
1482    corner(ndim) = index
1483!!????? edge(ndim) = index
1484    edge(ndim) = 1
1485!---
1486    iret = NF90_GET_VAR(ncfid,vid,var, &
1487 &                      start=corner(1:ndim),count=edge(1:ndim))
1488!---
1489!-- 5.0 The variable we have just read is created
1490!--      in the next restart file
1491!---
1492    IF (     (netcdf_id(fid,1) /= netcdf_id(fid,2))  &
1493 &      .AND.(netcdf_id(fid,2) > 0) ) THEN
1494!-----
1495      CALL restdefv (fid,vname_q,iim,jjm,llm,.FALSE.)
1496!-----
1497      DO ia = 1,varatt_in(fid,vnb)
1498        iret = NF90_INQ_ATTNAME(ncfid,vid,ia,attname)
1499        iret = NF90_COPY_ATT(ncfid,vid,attname, &
1500 &               netcdf_id(fid,2),varid_out(fid,nbvar_out(fid)))
1501      ENDDO
1502!-----
1503      IF (itau_out(fid) >= 0) THEN
1504        iret = NF90_ENDDEF(netcdf_id(fid,2))
1505      ENDIF
1506    ENDIF
1507!---
1508  ENDIF
1509!--------------------------
1510END SUBROUTINE restget_real
1511!===
1512SUBROUTINE restput_opp_r1d &
1513 & (fid,vname_q,iim,jjm,llm,itau,var,MY_OPERATOR,nbindex,ijndex)
1514!---------------------------------------------------------------------
1515!- This subroutine is the interface to restput_real which allows
1516!- to re-index data onto the original grid of the restart file.
1517!- The logic we use is still fuzzy in my mind but that is probably
1518!- only because I have not yet though through everything.
1519!-
1520!- In the case iim = nbindex it means that the user attempts
1521!- to project a vector back onto the original 2D or 3D field.
1522!- This requires that jjm and llm be equal to 1 or 0,
1523!- else I would not know what it means.
1524!---------------------------------------------------------------------
1525  IMPLICIT NONE
1526!-
1527  INTEGER :: fid
1528  CHARACTER(LEN=*) :: vname_q
1529  INTEGER :: iim,jjm,llm,itau
1530  REAL :: var(:)
1531  CHARACTER(LEN=*) :: MY_OPERATOR
1532  INTEGER :: nbindex,ijndex(nbindex)
1533!-
1534  INTEGER :: req_sz,siz1
1535  REAL :: scal
1536  CHARACTER(LEN=7) :: topp
1537  LOGICAL :: l_dbg
1538!---------------------------------------------------------------------
1539  CALL ipsldbg (old_status=l_dbg)
1540!-
1541! 0.0 What size should be the data in the file
1542!-
1543  req_sz = 1
1544  IF ( nbindex == iim .AND. jjm <= 1 .AND. llm <= 1) THEN
1545    IF (xax_infs(fid,1,1) > 0) req_sz = req_sz*xax_infs(fid,1,1)
1546    IF (yax_infs(fid,1,1) > 0) req_sz = req_sz*yax_infs(fid,1,1)
1547    IF (zax_infs(fid,1,1) > 0) req_sz = req_sz*zax_infs(fid,1,1)
1548  ELSE
1549    CALL ipslerr (3,'restput_opp_r1d', &
1550      'Unable to performe an operation on this variable as it has', &
1551      'a second and third dimension',vname_q)
1552  ENDIF
1553!-
1554! 1.0 Allocate the temporary buffer we need
1555!     to put the variable in right dimension
1556!-
1557  siz1 = SIZE(var)
1558  CALL rest_alloc (1,siz1,l_dbg,'restput_opp_r1d')
1559  CALL rest_alloc (2,req_sz,l_dbg,'restput_opp_r1d')
1560!-
1561! 2.0 We do the operation needed.
1562!     It can only be a re-indexing operation.
1563!     You would not want to change the values in a restart file or ?
1564!-
1565  topp = MY_OPERATOR(1:MIN(LEN_TRIM(MY_OPERATOR),7))
1566!-
1567  IF (INDEX(indchfun,topp(:LEN_TRIM(topp))) > 0) THEN
1568    scal = missing_val
1569    buff_tmp1(1:siz1) = var(:)
1570    CALL mathop &
1571 &    (topp,siz1,buff_tmp1,missing_val,nbindex,ijndex, &
1572 &     scal,req_sz,buff_tmp2)
1573  ELSE
1574    CALL ipslerr (3,'restput_opp_r1d', &
1575 &    'The operation you wish to do on the variable for the ', &
1576 &    'restart file is not allowed.',topp)
1577  ENDIF
1578!-
1579  CALL restput_real &
1580 & (fid,vname_q,xax_infs(fid,1,1),yax_infs(fid,1,1), &
1581 &  zax_infs(fid,1,1),itau,buff_tmp2)
1582!-----------------------------
1583END SUBROUTINE restput_opp_r1d
1584!===
1585SUBROUTINE restput_opp_r2d &
1586 & (fid,vname_q,iim,jjm,llm,itau,var,MY_OPERATOR,nbindex,ijndex)
1587!---------------------------------------------------------------------
1588!- This subroutine is the interface to restput_real which allows
1589!- to re-index data onto the original grid of the restart file.
1590!- The logic we use is still fuzzy in my mind but that is probably
1591!- only because I have not yet though through everything.
1592!-
1593!- In the case iim = nbindex it means that the user attempts
1594!- to project the first dimension of the matrix back onto a 3D field
1595!- where jjm will be the third dimension.
1596!- Here we do not allow for 4D data, thus we will take the first
1597!- two dimensions in the file and require that llm = 1.
1598!- These are pretty heavy constraints but I do not know how
1599!- to make it more general. I need to think about it some more.
1600!---------------------------------------------------------------------
1601  IMPLICIT NONE
1602!-
1603  INTEGER :: fid
1604  CHARACTER(LEN=*) :: vname_q
1605  INTEGER :: iim,jjm,llm,itau
1606  REAL :: var(:,:)
1607  CHARACTER(LEN=*) :: MY_OPERATOR
1608  INTEGER :: nbindex,ijndex(nbindex)
1609!-
1610  INTEGER :: jj,req_sz,ist,siz1
1611  REAL :: scal
1612  CHARACTER(LEN=7) :: topp
1613  LOGICAL :: l_dbg
1614!---------------------------------------------------------------------
1615  CALL ipsldbg (old_status=l_dbg)
1616!-
1617! 0.0 What size should be the data in the file
1618!-
1619  req_sz = 1
1620  IF ( nbindex == iim .AND. llm <= 1) THEN
1621    IF (xax_infs(fid,1,1) > 0) req_sz = req_sz*xax_infs(fid,1,1)
1622    IF (yax_infs(fid,1,1) > 0) req_sz = req_sz*yax_infs(fid,1,1)
1623  ELSE
1624    CALL ipslerr (3,'restput_opp_r2d', &
1625      'Unable to performe an operation on this variable as it has', &
1626      'a second and third dimension',vname_q)
1627  ENDIF
1628!-
1629  IF (jjm < 1) THEN
1630    CALL ipslerr (3,'restput_opp_r2d', &
1631      'Please specify a second dimension which is the', &
1632      'layer on which the operations are performed',vname_q)
1633  ENDIF
1634!-
1635! 1.0 Allocate the temporary buffer we need
1636!     to put the variable in right dimension
1637!-
1638  siz1 = SIZE(var,1)
1639  CALL rest_alloc (1,siz1,l_dbg,'restput_opp_r2d')
1640  CALL rest_alloc (2,req_sz*jjm,l_dbg,'restput_opp_r2d')
1641!-
1642! 2.0 We do the operation needed.
1643!     It can only be a re-indexing operation.
1644!     You would not want to change the values in a restart file or ?
1645!-
1646  topp = MY_OPERATOR(1:MIN(LEN_TRIM(MY_OPERATOR),7))
1647!-
1648  IF (INDEX(indchfun,topp(:LEN_TRIM(topp))) > 0) THEN
1649    scal = missing_val
1650    DO jj = 1,jjm
1651      buff_tmp1(1:siz1) = var(:,jj)
1652      ist = (jj-1)*req_sz+1
1653      CALL mathop &
1654 &      (topp,siz1,buff_tmp1,missing_val,nbindex,ijndex, &
1655 &       scal,req_sz,buff_tmp2(ist:ist+req_sz-1))
1656    ENDDO
1657  ELSE
1658    CALL ipslerr (3,'restput_opp_r2d', &
1659 &    'The operation you wish to do on the variable for the ', &
1660 &    'restart file is not allowed.',topp)
1661  ENDIF
1662!-
1663  CALL restput_real &
1664 & (fid,vname_q,xax_infs(fid,1,1),yax_infs(fid,1,1), &
1665 &  jjm,itau,buff_tmp2)
1666!-----------------------------
1667END SUBROUTINE restput_opp_r2d
1668!===
1669SUBROUTINE restput_r1d (fid,vname_q,iim,jjm,llm,itau,var)
1670!---------------------------------------------------------------------
1671!- This subroutine serves as an interface to restput_real
1672!---------------------------------------------------------------------
1673  IMPLICIT NONE
1674!-
1675  INTEGER :: fid
1676  CHARACTER(LEN=*) :: vname_q
1677  INTEGER :: iim,jjm,llm,itau
1678  REAL :: var(:)
1679!-
1680  INTEGER :: ji,jl,req_sz,var_sz,siz1
1681  CHARACTER(LEN=70) :: str,str2
1682  LOGICAL :: l_dbg
1683!---------------------------------------------------------------------
1684  CALL ipsldbg (old_status=l_dbg)
1685!-
1686! 1.0 Allocate the temporary buffer we need
1687!     to put the variable in right dimension
1688!-
1689  siz1 = SIZE(var)
1690  var_sz = siz1
1691  CALL rest_alloc (1,var_sz,l_dbg,'restput_r1d')
1692!-
1693! 2.0 Here we could check if the sizes specified agree
1694!     with the size of the variable provided
1695!-
1696  req_sz = 1
1697  IF (iim > 0) req_sz = req_sz*iim
1698  IF (jjm > 0) req_sz = req_sz*jjm
1699  IF (llm > 0) req_sz = req_sz*llm
1700  IF (req_sz > var_sz) THEN
1701    WRITE(str, &
1702 &    '("Size of variable put to the file should be ",I6)') req_sz
1703    WRITE(str2, &
1704 &    '("but the provided variable is of size ",I6)') var_sz
1705    CALL ipslerr (3,'restput_r1d',str,str2,' ')
1706  ENDIF
1707  IF (req_sz < var_sz) THEN
1708    WRITE(str,'("the size of variable put to the file is ",I6)') req_sz
1709    WRITE(str2,'("but the provided variable is larger ",I6)') var_sz
1710    CALL ipslerr (2,'restput_r1d', &
1711      'There could be a problem here :',str,str2)
1712  ENDIF
1713!-
1714! 4.0 Transfer the buffer obtained from the restart file
1715!     into the variable the model expects
1716!-
1717  jl=0
1718  DO ji=1,siz1
1719    jl=jl+1
1720    buff_tmp1(jl) = var(ji)
1721  ENDDO
1722!-
1723  CALL restput_real (fid,vname_q,iim,jjm,llm,itau,buff_tmp1)
1724!-------------------------
1725END SUBROUTINE restput_r1d
1726!===
1727SUBROUTINE restput_r2d (fid,vname_q,iim,jjm,llm,itau,var)
1728!---------------------------------------------------------------------
1729!- This subroutine serves as an interface to restput_real
1730!---------------------------------------------------------------------
1731  IMPLICIT NONE
1732!-
1733  INTEGER :: fid
1734  CHARACTER(LEN=*) :: vname_q
1735  INTEGER :: iim,jjm,llm,itau
1736  REAL :: var(:,:)
1737!-
1738  INTEGER :: ji,jj,jl,req_sz,var_sz,siz1,siz2
1739  CHARACTER(LEN=70) :: str,str2
1740  LOGICAL :: l_dbg
1741!---------------------------------------------------------------------
1742  CALL ipsldbg (old_status=l_dbg)
1743!-
1744! 1.0 Allocate the temporary buffer we need
1745!     to put the variable in right dimension
1746!-
1747  siz1 = SIZE(var,1)
1748  siz2 = SIZE(var,2)
1749  var_sz = siz1*siz2
1750  CALL rest_alloc (1,var_sz,l_dbg,'restput_r2d')
1751!-
1752! 2.0 Here we could check if the sizes specified agree
1753!     with the size of the variable provided
1754!-
1755  req_sz = 1
1756  IF (iim > 0) req_sz = req_sz*iim
1757  IF (jjm > 0) req_sz = req_sz*jjm
1758  IF (llm > 0) req_sz = req_sz*llm
1759  IF (req_sz > var_sz) THEN
1760    WRITE(str, &
1761&         '("Size of variable put to the file should be ",I6)') req_sz
1762    WRITE(str2,'("but the provided variable is of size  ",I6)') var_sz
1763    CALL ipslerr (3,'restput_r2d',str,str2,' ')
1764  ENDIF
1765  IF (req_sz < var_sz) THEN
1766    WRITE(str,'("the size of variable put to the file is ",I6)') req_sz
1767    WRITE(str2,'("but the provided variable is larger ",I6)')  var_sz
1768    CALL ipslerr (2,'restput_r2d', &
1769      'There could be a problem here :',str,str2)
1770  ENDIF
1771!-
1772! 4.0 Transfer the buffer obtained from the restart file
1773!     into the variable the model expects
1774!-
1775  jl=0
1776  DO jj=1,siz2
1777    DO ji=1,siz1
1778      jl=jl+1
1779      buff_tmp1(jl) = var(ji,jj)
1780    ENDDO
1781  ENDDO
1782!-
1783  CALL restput_real(fid,vname_q,iim,jjm,llm,itau,buff_tmp1)
1784!-------------------------
1785END SUBROUTINE restput_r2d
1786!===
1787SUBROUTINE restput_r3d (fid,vname_q,iim,jjm,llm,itau,var)
1788!---------------------------------------------------------------------
1789!- This subroutine serves as an interface to restput_real
1790!---------------------------------------------------------------------
1791  IMPLICIT NONE
1792!-
1793  INTEGER :: fid
1794  CHARACTER(LEN=*) :: vname_q
1795  INTEGER :: iim,jjm,llm,itau
1796  REAL :: var(:,:,:)
1797!-
1798  INTEGER :: ji,jj,jk,jl,req_sz,var_sz,siz1,siz2,siz3
1799  CHARACTER(LEN=70) :: str,str2
1800  LOGICAL :: l_dbg
1801!---------------------------------------------------------------------
1802  CALL ipsldbg (old_status=l_dbg)
1803!-
1804! 1.0 Allocate the temporary buffer we need
1805!     to put the variable in right dimension
1806!-
1807  siz1 = SIZE(var,1)
1808  siz2 = SIZE(var,2)
1809  siz3 = SIZE(var,3)
1810  var_sz = siz1*siz2*siz3
1811  CALL rest_alloc (1,var_sz,l_dbg,'restput_r3d')
1812!-
1813! 2.0 Here we could check if the sizes specified agree
1814!     with the size of the variable provided
1815!-
1816  req_sz = 1
1817  IF (iim > 0) req_sz = req_sz*iim
1818  IF (jjm > 0) req_sz = req_sz*jjm
1819  IF (llm > 0) req_sz = req_sz*llm
1820  IF (req_sz > var_sz) THEN
1821    WRITE(str, &
1822 &    '("Size of variable put to the file should be ",I6)') req_sz
1823    WRITE(str2, &
1824 &    '("but the provided variable is of size  ",I6)')  var_sz
1825    CALL ipslerr (3,'restput_r3d',str,str2,' ')
1826  ENDIF
1827  IF (req_sz < var_sz) THEN
1828    WRITE(str,'("the size of variable put to the file is ",I6)') req_sz
1829    WRITE(str2,'("but the provided variable is larger ",I6)')  var_sz
1830    CALL ipslerr (2,'restput_r3d', &
1831      'There could be a problem here :',str,str2)
1832  ENDIF
1833!-
1834! 4.0 Transfer the buffer obtained from the restart file
1835!     into the variable the model expects
1836!-
1837  jl=0
1838  DO jk=1,siz3
1839    DO jj=1,siz2
1840      DO ji=1,siz1
1841        jl=jl+1
1842        buff_tmp1(jl) = var(ji,jj,jk)
1843      ENDDO
1844    ENDDO
1845  ENDDO
1846!-
1847  CALL restput_real(fid,vname_q,iim,jjm,llm,itau,buff_tmp1)
1848!-------------------------
1849END SUBROUTINE restput_r3d
1850!===
1851SUBROUTINE restput_real (fid,vname_q,iim,jjm,llm,itau,var)
1852!---------------------------------------------------------------------
1853!- This subroutine will put a variable into the restart file.
1854!- But it will do a lot of other things if needed :
1855!- - Open a file if non is opened for this time-step
1856!-   and all variables were written.
1857!- - Add an axis if needed
1858!- - verify that the variable has the right time step for this file
1859!- - If it is time for a new file then it is opened
1860!-   and the old one closed
1861!- This requires that variables read from the last restart file were all
1862!- written
1863!-
1864!- INPUT
1865!-
1866!- fid         : Id of the file in which we will write the variable
1867!- vname_q     : Name of the variable to be written
1868!- iim,jjm,llm : Size in 3D of the variable
1869!- itau        : Time step at which the variable is written
1870!- var         : Variable
1871!-
1872!- OUTPUT
1873!-
1874!- NONE
1875!---------------------------------------------------------------------
1876  IMPLICIT NONE
1877!-
1878  CHARACTER(LEN=*) :: vname_q
1879  INTEGER :: fid,iim,jjm,llm,itau
1880  REAL :: var(:)
1881!-
1882  INTEGER :: iret,vid,ncid,iv,vnb
1883  INTEGER :: ierr
1884  REAL :: secsince,one_day,one_year
1885  INTEGER :: ndims
1886  INTEGER,DIMENSION(4) :: corner,edge
1887  LOGICAL :: l_dbg
1888!---------------------------------------------------------------------
1889  CALL ipsldbg (old_status=l_dbg)
1890!-
1891! 0.0 Get some variables
1892!-
1893  ncid = netcdf_id(fid,2)
1894  IF (netcdf_id(fid,2) < 0) THEN
1895    CALL ipslerr (3,'restput', &
1896 &    'The output restart file is undefined.',' ',' ')
1897  ENDIF
1898  CALL ioget_calendar (one_year,one_day)
1899!-
1900! 1.0 Check if the variable is already present
1901!-
1902  IF (l_dbg) WRITE(*,*) 'RESTPUT 1.0 : ',TRIM(vname_q)
1903!-
1904  CALL find_str (varname_out(fid,1:nbvar_out(fid)),vname_q,vnb)
1905!-
1906  IF (l_dbg) THEN
1907    WRITE(*,*) 'RESTPUT 1.1 : ',varname_out(fid,1:nbvar_out(fid)),vnb
1908  ENDIF
1909!-
1910! 2.0 If variable is not present then declare it
1911!     and add extra dimensions if needed.
1912!-
1913  IF (vnb <= 0) THEN
1914    CALL restdefv (fid,vname_q,iim,jjm,llm,.TRUE.)
1915    vnb = nbvar_out(fid)
1916  ENDIF
1917  vid = varid_out(fid,vnb)
1918!-
1919  IF (l_dbg) WRITE(*,*) 'RESTPUT 2.0 : ',vnb,vid
1920!-
1921! 2.1 Is this file already in write mode ?
1922!     If itau_out is still negative then we have
1923!     never written to it and we need to go into write mode.
1924!-
1925  IF (itau_out(fid) < 0) THEN
1926    iret = NF90_ENDDEF(ncid)
1927  ENDIF
1928!-
1929! 3.0 Is this itau already on the axis ?
1930!     If not then check that all variables of previous time is OK.
1931!-
1932  IF (l_dbg) WRITE(*,*) 'RESTPUT 3.0 : ',itau,itau_out(fid)
1933!-
1934  IF (itau /= itau_out(fid)) THEN
1935!---
1936!-- If it is the first time step written on the restart
1937!-- then we only check the number
1938!-- Else we see if every variable was written
1939!---
1940    IF (tstp_out(fid) == 0) THEN
1941      IF (nbvar_out(fid) < nbvar_read(fid)) THEN
1942        WRITE(*,*) "ERROR :",tstp_out(fid), &
1943                   nbvar_out(fid),nbvar_read(fid)
1944        CALL ipslerr (1,'restput', &
1945 &        'There are fewer variables read from the output file', &
1946 &        'than written onto the input file.', &
1947 &        'We trust you know what you are doing')
1948      ENDIF
1949    ELSE
1950      ierr = 0
1951      DO iv=1,nbvar_out(fid)
1952        IF (.NOT.touched_out(fid,iv)) ierr = ierr+1
1953      ENDDO
1954      IF (ierr > 0) THEN
1955        WRITE(*,*) "ERROR :",nbvar_out(fid)
1956        CALL ipslerr (1,'restput', &
1957 &        'There are fewer variables in the output file for this', &
1958 &        'time step than for the previous one',' ')
1959      ELSE
1960        touched_out(fid,:) = .FALSE.
1961      ENDIF
1962    ENDIF
1963!---
1964    secsince = itau*deltat(fid)
1965    corner(1) =  tstp_out(fid)+1
1966    edge(1) = 1
1967!---
1968!-- 3.1 Here we add the values to the time axes
1969!---
1970    IF (l_dbg) THEN
1971      WRITE(*,*) 'RESTPUT 3.1 : ',itau,secsince,corner(1),edge(1)
1972    ENDIF
1973!---
1974    iret = NF90_PUT_VAR(ncid,tind_varid_out(fid),itau, &
1975 &                      start=corner(1:1))
1976    iret = NF90_PUT_VAR(ncid,tax_varid_out(fid),secsince, &
1977 &                      start=corner(1:1))
1978!---
1979    tstp_out(fid) = tstp_out(fid)+1
1980    itau_out(fid) = itau
1981  ENDIF
1982!-
1983! 4.0 Variable and time step should be present
1984!     now so we can dump variable
1985!-
1986  ndims = 0
1987  IF (iim > 0) THEN
1988    ndims = ndims+1
1989    corner(ndims) = 1
1990    edge(ndims) = iim
1991  ENDIF
1992  IF (jjm > 0) THEN
1993    ndims = ndims+1
1994    corner(ndims) = 1
1995    edge(ndims) = jjm
1996  ENDIF
1997  IF (llm > 0) THEN
1998    ndims = ndims+1
1999    corner(ndims) = 1
2000    edge(ndims) = llm
2001  ENDIF
2002  ndims = ndims+1
2003  corner(ndims) = tstp_out(fid)
2004  edge(ndims) = 1
2005!-
2006  iret = NF90_PUT_VAR(ncid,vid,var, &
2007 &                    start=corner(1:ndims),count=edge(1:ndims))
2008!-
2009  IF (iret /= NF90_NOERR) THEN
2010    CALL ipslerr (2,'restput_real',NF90_STRERROR(iret), &
2011 &    'Bug in restput.',&
2012 &    'Please, verify compatibility between get and put commands.')
2013  ENDIF
2014!-
2015!  5.0 Note that the variables was treated
2016!-
2017  touched_out(fid,vnb) = .TRUE.
2018!---------------------------
2019END  SUBROUTINE restput_real
2020!===
2021SUBROUTINE restdefv (fid,varname,iim,jjm,llm,write_att)
2022!---------------------------------------------------------------------
2023! This subroutine adds a variable to the output file.
2024! The attributes are either taken from.
2025!---------------------------------------------------------------------
2026  IMPLICIT NONE
2027!-
2028  INTEGER ::fid
2029  CHARACTER(LEN=*) :: varname
2030  INTEGER :: iim,jjm,llm
2031  LOGICAL :: write_att
2032!-
2033  INTEGER :: dims(4),ic,xloc,ndim,ncfid
2034  INTEGER :: iret,ax_id
2035  CHARACTER(LEN=3) :: str
2036  LOGICAL :: l_dbg
2037!---------------------------------------------------------------------
2038  CALL ipsldbg (old_status=l_dbg)
2039!-
2040  ncfid = netcdf_id(fid,2)
2041  IF (nbvar_out(fid) >= max_var) THEN
2042    CALL ipslerr (3,'restdefv', &
2043      'Too many variables for the restcom module', &
2044      'Please increase the value of max_var',' ')
2045  ENDIF
2046  nbvar_out(fid) = nbvar_out(fid)+1
2047  varname_out(fid,nbvar_out(fid)) = varname
2048!-
2049! 0.0 Put the file in define mode if needed
2050!-
2051  IF (itau_out(fid) >= 0) THEN
2052    iret = NF90_REDEF(ncfid)
2053  ENDIF
2054!-
2055! 1.0 Do we have all dimensions and can we go ahead
2056!-
2057  IF (l_dbg) THEN
2058    WRITE(*,*) 'restdefv 1.0 :',TRIM(varname),nbvar_out(fid)
2059  ENDIF
2060!-
2061  ndim = 0
2062!-
2063! 1.1 Work on x
2064!-
2065  IF (iim > 0) THEN
2066    ndim = ndim+1
2067    xloc = 0
2068    DO ic=1,xax_nb(fid)
2069      IF (xax_infs(fid,ic,1) == iim) xloc = ic
2070    ENDDO
2071!---
2072    IF (xloc > 0) THEN
2073      dims(ndim) = xax_infs(fid,xloc,2)
2074    ELSE
2075      str='x_'//CHAR(96+xax_nb(fid))
2076      iret = NF90_DEF_DIM(ncfid,str,iim,ax_id)
2077      xax_nb(fid) = xax_nb(fid)+1
2078      xax_infs(fid,xax_nb(fid),1) = iim
2079      xax_infs(fid,xax_nb(fid),2) = ax_id
2080      dims(ndim) = ax_id
2081    ENDIF
2082  ENDIF
2083!-
2084! 1.2 Work on y
2085!-
2086  IF (jjm > 0) THEN
2087    ndim = ndim+1
2088    xloc = 0
2089    DO ic=1,yax_nb(fid)
2090      IF (yax_infs(fid,ic,1) == jjm) xloc = ic
2091    ENDDO
2092!---
2093    IF (xloc > 0) THEN
2094      dims(ndim) = yax_infs(fid,xloc,2)
2095    ELSE
2096      str='y_'//CHAR(96+yax_nb(fid))
2097      iret = NF90_DEF_DIM(ncfid,str,jjm,ax_id)
2098      yax_nb(fid) = yax_nb(fid)+1
2099      yax_infs(fid,yax_nb(fid),1) = jjm
2100      yax_infs(fid,yax_nb(fid),2) = ax_id
2101      dims(ndim) = ax_id
2102    ENDIF
2103  ENDIF
2104!-
2105! 1.3 Work on z
2106!-
2107  IF (llm > 0) THEN
2108    ndim = ndim+1
2109    xloc = 0
2110    DO ic=1,zax_nb(fid)
2111      IF (zax_infs(fid,ic,1) == llm) xloc = ic
2112    ENDDO
2113!---
2114    IF (xloc > 0) THEN
2115      dims(ndim) = zax_infs(fid,xloc,2)
2116    ELSE
2117      str='z_'//CHAR(96+zax_nb(fid))
2118      iret = NF90_DEF_DIM(ncfid,str,llm,ax_id)
2119      zax_nb(fid) = zax_nb(fid)+1
2120      zax_infs(fid,zax_nb(fid),1) = llm
2121      zax_infs(fid,zax_nb(fid),2) = ax_id
2122      dims(ndim) = ax_id
2123    ENDIF
2124  ENDIF
2125!-
2126! 1.4  Time needs to be added
2127!-
2128  ndim = ndim+1
2129  dims(ndim) = tdimid_out(fid)
2130!-
2131! 2.0  Declare the variable
2132!-
2133  IF (l_dbg) THEN
2134    WRITE(*,*) 'restdefv 2.0 :',ndim,' :: ',dims(1:ndim),tdimid_out(fid)
2135  ENDIF
2136!-
2137  iret = NF90_DEF_VAR(ncfid,varname,NF90_DOUBLE,dims(1:ndim), &
2138 &                    varid_out(fid,nbvar_out(fid)))
2139  IF (iret /= NF90_NOERR) THEN
2140    CALL ipslerr (3,'restdefv', &
2141      'Could not define new variable in file', &
2142      NF90_STRERROR(iret),varname)
2143  ENDIF
2144!-
2145! 3.0 Add the attributes if requested
2146!-
2147  IF (write_att) THEN
2148    IF (rest_units /= 'XXXXX') THEN
2149      iret =  NF90_PUT_ATT(ncfid,varid_out(fid,nbvar_out(fid)), &
2150 &                         'units',TRIM(rest_units))
2151      rest_units = 'XXXXX'
2152    ENDIF
2153!---
2154    IF (rest_lname /= 'XXXXX') THEN
2155      iret =  NF90_PUT_ATT(ncfid,varid_out(fid,nbvar_out(fid)), &
2156 &                         'long_name',TRIM(rest_lname))
2157      rest_lname = 'XXXXX'
2158    ENDIF
2159!---
2160    iret = NF90_PUT_ATT(ncfid,varid_out(fid,nbvar_out(fid)), &
2161 &                      'missing_value',REAL(missing_val,KIND=4))
2162!---
2163    IF (itau_out(fid) >= 0) THEN
2164      iret = NF90_ENDDEF(ncfid)
2165    ENDIF
2166  ENDIF
2167!-
2168  IF (l_dbg) THEN
2169    WRITE(*,*) &
2170 &    'restdefv 3.0 : LIST OF VARS ',varname_out(fid,1:nbvar_out(fid))
2171  ENDIF
2172!----------------------
2173END SUBROUTINE restdefv
2174!===
2175SUBROUTINE rest_atim (l_msg,c_p)
2176!---------------------------------------------------------------------
2177! Called by "c_p", [re]allocate the time axes
2178!---------------------------------------------------------------------
2179  IMPLICIT NONE
2180!-
2181  LOGICAL,INTENT(IN) :: l_msg
2182  CHARACTER(LEN=*),INTENT(IN) :: c_p
2183!-
2184  INTEGER :: i_err,tszij
2185  INTEGER,ALLOCATABLE :: tmp_index(:,:)
2186  REAL,ALLOCATABLE :: tmp_julian(:,:)
2187!---------------------------------------------------------------------
2188!-
2189!  Allocate the space we need for the time axes
2190!-
2191  IF (.NOT.ALLOCATED(t_index) .AND. .NOT.ALLOCATED(t_julian)) THEN
2192    IF (l_msg) THEN
2193      WRITE(*,*) TRIM(c_p)//' : Allocate times axes at :', &
2194 &               max_file,tax_size_in(nb_fi)
2195    ENDIF
2196!---
2197    ALLOCATE(t_index(max_file,tax_size_in(nb_fi)),STAT=i_err)
2198    IF (i_err/=0) THEN
2199      WRITE(*,*) "ERROR IN ALLOCATION of t_index : ",i_err
2200      CALL ipslerr (3,TRIM(c_p), &
2201 &      'Problem in allocation of t_index','', &
2202 &      '(you must increase memory)')
2203    ENDIF
2204    t_index (:,:) = 0
2205!---
2206    ALLOCATE(t_julian(max_file,tax_size_in(nb_fi)),STAT=i_err)
2207    IF (i_err/=0) THEN
2208      WRITE(*,*) "ERROR IN ALLOCATION of t_julian : ",i_err
2209      CALL ipslerr (3,TRIM(c_p), &
2210 &      'Problem in allocation of max_file,tax_size_in','', &
2211 &      '(you must increase memory)')
2212    ENDIF
2213    t_julian (:,:) = 0.0
2214  ELSE IF (    (SIZE(t_index,DIM=2)  < tax_size_in(nb_fi)) &
2215 &         .OR.(SIZE(t_julian,DIM=2) < tax_size_in(nb_fi)) ) THEN
2216    IF (l_msg) THEN
2217      WRITE(*,*) TRIM(c_p)//' : Reallocate times axes at :', &
2218 &               max_file,tax_size_in(nb_fi)
2219    ENDIF
2220!---
2221    ALLOCATE (tmp_index(max_file,tax_size_in(nb_fi)),STAT=i_err)
2222    IF (i_err/=0) THEN
2223      WRITE(*,*) "ERROR IN ALLOCATION of tmp_index : ",i_err
2224      CALL ipslerr (3,TRIM(c_p), &
2225 &      'Problem in allocation of tmp_index','', &
2226 &      '(you must increase memory)')
2227    ENDIF
2228    tszij = SIZE(t_index,DIM=2)
2229    tmp_index(:,1:tszij) = t_index(:,1:tszij)
2230    DEALLOCATE(t_index)
2231    ALLOCATE (t_index(max_file,tax_size_in(nb_fi)),STAT=i_err)
2232    IF (i_err/=0) THEN
2233      WRITE(*,*) "ERROR IN ALLOCATION of t_index : ",i_err
2234      CALL ipslerr (3,TRIM(c_p), &
2235 &     'Problem in reallocation of t_index','', &
2236 &     '(you must increase memory)')
2237    ENDIF
2238    t_index(:,1:tszij) = tmp_index(:,1:tszij)
2239!---
2240    ALLOCATE (tmp_julian(max_file,tax_size_in(nb_fi)),STAT=i_err)
2241    IF (i_err/=0) THEN
2242      WRITE(*,*) "ERROR IN ALLOCATION of tmp_julian : ",i_err
2243      CALL ipslerr (3,TRIM(c_p), &
2244 &     'Problem in allocation of tmp_julian','', &
2245 &     '(you must increase memory)')
2246    ENDIF
2247    tszij = SIZE(t_julian,DIM=2)
2248    tmp_julian(:,1:tszij) = t_julian(:,1:tszij)
2249    DEALLOCATE(t_julian)
2250    ALLOCATE (t_julian(max_file,tax_size_in(nb_fi)),STAT=i_err)
2251    IF (i_err/=0) THEN
2252      WRITE(*,*) "ERROR IN ALLOCATION of t_julian : ",i_err
2253      CALL ipslerr (3,TRIM(c_p), &
2254 &      'Problem in reallocation of t_julian','', &
2255 &      '(you must increase memory)')
2256    ENDIF
2257    t_julian(:,1:tszij) = tmp_julian(:,1:tszij)
2258  ENDIF
2259!-----------------------
2260END SUBROUTINE rest_atim
2261!===
2262SUBROUTINE rest_alloc (i_buff,i_qsz,l_msg,c_p)
2263!---------------------------------------------------------------------
2264! Called by "c_p", allocate a temporary buffer
2265! (buff_tmp[1/2] depending on "i_buff" value) to the size "i_qsz".
2266!---------------------------------------------------------------------
2267  IMPLICIT NONE
2268!-
2269  INTEGER,INTENT(IN) :: i_buff,i_qsz
2270  LOGICAL,INTENT(IN) :: l_msg
2271  CHARACTER(LEN=*),INTENT(IN) :: c_p
2272!-
2273  INTEGER :: i_bsz,i_err
2274  LOGICAL :: l_alloc1,l_alloc2
2275  CHARACTER(LEN=9) :: cbn
2276  CHARACTER(LEN=5) :: c_err
2277!---------------------------------------------------------------------
2278  IF      (i_buff == 1) THEN
2279    IF (ALLOCATED(buff_tmp1)) THEN
2280      i_bsz = SIZE(buff_tmp1)
2281    ELSE
2282      i_bsz = 0
2283    ENDIF
2284    l_alloc1 =    (.NOT.ALLOCATED(buff_tmp1)) &
2285 &            .OR.((ALLOCATED(buff_tmp1)).AND.(i_qsz > i_bsz))
2286    l_alloc2 = .FALSE.
2287    cbn = 'buff_tmp1'
2288  ELSE IF (i_buff == 2) THEN
2289    IF (ALLOCATED(buff_tmp2)) THEN
2290      i_bsz = SIZE(buff_tmp2)
2291    ELSE
2292      i_bsz = 0
2293    ENDIF
2294    l_alloc1 = .FALSE.
2295    l_alloc2 =    (.NOT.ALLOCATED(buff_tmp2)) &
2296 &            .OR.((ALLOCATED(buff_tmp2)).AND.(i_qsz > i_bsz))
2297    cbn = 'buff_tmp2'
2298  ELSE
2299    CALL ipslerr (3,'rest_alloc', &
2300 &    'Called by '//TRIM(c_p),'with a wrong value of i_buff','')
2301  ENDIF
2302!-
2303!-
2304  IF (l_alloc1.OR.l_alloc2) THEN
2305    IF (l_msg) THEN
2306      IF (    (l_alloc1.AND.ALLOCATED(buff_tmp1)) &
2307 &        .OR.(l_alloc2.AND.ALLOCATED(buff_tmp2)) ) THEN
2308        WRITE(*,*) TRIM(c_p)//" : re_allocate "//TRIM(cbn)//"=",i_qsz
2309      ELSE
2310        WRITE(*,*) TRIM(c_p)//" : allocate "//TRIM(cbn)//"=",i_qsz
2311      ENDIF
2312    ENDIF
2313    IF (l_alloc1) THEN
2314      IF (ALLOCATED(buff_tmp1)) THEN
2315        DEALLOCATE(buff_tmp1)
2316      ENDIF
2317      ALLOCATE (buff_tmp1(i_qsz),STAT=i_err)
2318    ELSE
2319      IF (ALLOCATED(buff_tmp2)) THEN
2320        DEALLOCATE(buff_tmp2)
2321      ENDIF
2322      ALLOCATE (buff_tmp2(i_qsz),STAT=i_err)
2323    ENDIF
2324    IF (i_err /= 0) THEN
2325      WRITE (UNIT=c_err,FMT='(I5)') i_err
2326      CALL ipslerr (3,TRIM(c_p), &
2327 &      'Problem in allocation of',TRIM(cbn), &
2328 &      'Error : '//TRIM(c_err)//' (you must increase memory)')
2329    ENDIF
2330  ENDIF
2331!------------------------
2332END SUBROUTINE rest_alloc
2333!===
2334SUBROUTINE ioconf_setatt (attname,value)
2335!---------------------------------------------------------------------
2336  IMPLICIT NONE
2337!-
2338  CHARACTER(LEN=*) :: attname,value
2339!-
2340  CHARACTER(LEN=LEN_TRIM(attname)) :: tmp_str
2341!---------------------------------------------------------------------
2342  tmp_str = attname
2343  CALL strlowercase (tmp_str)
2344!-
2345  SELECT CASE(tmp_str)
2346    CASE('units')
2347      rest_units = value
2348    CASE('long_name')
2349      rest_lname = value
2350    CASE DEFAULT
2351      CALL ipslerr (2,'ioconf_restatt', &
2352        'The attribute name provided is unknown',attname,' ')
2353  END SELECT
2354!---------------------------
2355END SUBROUTINE ioconf_setatt
2356!===
2357SUBROUTINE ioget_vdim (fid,vname_q,varnbdim_max,varnbdim,vardims)
2358!---------------------------------------------------------------------
2359!- This routine allows the user to get the dimensions
2360!- of a field in the restart file.
2361!- This is the file which is read.
2362!---------------------------------------------------------------------
2363  IMPLICIT NONE
2364!-
2365  INTEGER,INTENT(IN) :: fid
2366  CHARACTER(LEN=*) :: vname_q
2367  INTEGER,INTENT(IN) :: varnbdim_max
2368  INTEGER,INTENT(OUT) ::  varnbdim
2369  INTEGER,DIMENSION(varnbdim_max),INTENT(OUT) :: vardims
2370!-
2371  INTEGER :: vnb
2372!---------------------------------------------------------------------
2373! Find the index of the variable
2374  CALL find_str (varname_in(fid,1:nbvar_in(fid)),vname_q,vnb)
2375!-
2376  IF (vnb > 0) THEN
2377    varnbdim = varnbdim_in(fid,vnb)
2378    IF (varnbdim_max < varnbdim) THEN
2379      CALL ipslerr (3,'ioget_vdim', &
2380        'The provided array for the variable dimensions is too small', &
2381        '','')
2382    ELSE
2383      vardims(1:varnbdim) = vardims_in(fid,vnb,1:varnbdim)
2384    ENDIF
2385  ELSE
2386    varnbdim = 0
2387    CALL ipslerr (2,'ioget_vdim', &
2388      'Variable '//TRIM(vname_q)//' not found','','')
2389  ENDIF
2390!------------------------
2391END SUBROUTINE ioget_vdim
2392!===
2393SUBROUTINE ioget_vname (fid,nbvar,varnames)
2394!---------------------------------------------------------------------
2395!- This routine allows the user to extract the list
2396!- of variables in an opened restart file.
2397!- This is the file which is read
2398!---------------------------------------------------------------------
2399  IMPLICIT NONE
2400!-
2401  INTEGER,INTENT(IN) :: fid
2402  INTEGER,INTENT(OUT) ::  nbvar
2403  CHARACTER(LEN=*),INTENT(OUT) :: varnames(:)
2404!---------------------------------------------------------------------
2405  nbvar = nbvar_in(fid)
2406!-
2407  IF (SIZE(varnames) < nbvar) THEN
2408    CALL ipslerr (3,'ioget_vname', &
2409      'The provided array for the variable names is too small','','')
2410  ELSE
2411    varnames(1:nbvar) = varname_in(fid,1:nbvar)
2412  ENDIF
2413!-------------------------
2414END SUBROUTINE ioget_vname
2415!===
2416SUBROUTINE ioconf_expval (new_exp_val)
2417!---------------------------------------------------------------------
2418!- The default value written into the variables which are not
2419!- in the restart file can only be changed once.
2420!- This avoids further complications.
2421!---------------------------------------------------------------------
2422  IMPLICIT NONE
2423!-
2424  REAL :: new_exp_val
2425!---------------------------------------------------------------------
2426  IF (.NOT.lock_valexp) THEN
2427    lock_valexp = .TRUE.
2428    val_exp = new_exp_val
2429  ELSE
2430    CALL ipslerr (2,'ioconf_expval', &
2431     'The default value for variable' &
2432   //'not available in the restart file ', &
2433     'has already been locked and can not be changed at this point', &
2434     ' ')
2435  ENDIF
2436!---------------------------
2437END SUBROUTINE ioconf_expval
2438!===
2439SUBROUTINE ioget_expval (get_exp_val)
2440!---------------------------------------------------------------------
2441!- Once the user has extracted the default value,
2442!- we lock it so that it can not be changed anymore.
2443!---------------------------------------------------------------------
2444  IMPLICIT NONE
2445!-
2446  REAL :: get_exp_val
2447!---------------------------------------------------------------------
2448  get_exp_val = val_exp
2449  lock_valexp = .TRUE.
2450!--------------------------
2451END SUBROUTINE ioget_expval
2452!===
2453SUBROUTINE restclo (fid)
2454!---------------------------------------------------------------------
2455!- This subroutine closes one or any opened restart file.
2456!-
2457!- INPUT
2458!-
2459!- fid    : File ID in the restcom system (not the netCDF ID)(optional)
2460!-
2461!- OUTPUT
2462!-
2463!- NONE
2464!---------------------------------------------------------------------
2465  IMPLICIT NONE
2466!-
2467  INTEGER,INTENT(in),OPTIONAL :: fid
2468!-
2469  INTEGER :: iret,ifnc
2470  CHARACTER(LEN=6) :: n_e
2471  CHARACTER(LEN=3) :: n_f
2472  LOGICAL :: l_dbg
2473!---------------------------------------------------------------------
2474  CALL ipsldbg (old_status=l_dbg)
2475!-
2476  IF (PRESENT(fid)) THEN
2477!---
2478    IF (l_dbg) THEN
2479      WRITE(*,*) &
2480        'restclo : Closing specified restart file number :', &
2481        fid,netcdf_id(fid,1:2)
2482    ENDIF
2483!---
2484    IF (netcdf_id(fid,1) > 0) THEN
2485      iret = NF90_CLOSE(netcdf_id(fid,1))
2486      IF (iret /= NF90_NOERR) THEN
2487        WRITE (n_e,'(I6)') iret
2488        WRITE (n_f,'(I3)') netcdf_id(fid,1)
2489        CALL ipslerr (2,'restclo', &
2490          "Error "//n_e//" in closing file : "//n_f,'',' ')
2491      ENDIF
2492      IF (netcdf_id(fid,1) == netcdf_id(fid,2)) THEN
2493        netcdf_id(fid,2) = -1
2494      ENDIF
2495      netcdf_id(fid,1) = -1
2496    ENDIF
2497!---
2498    IF (netcdf_id(fid,2) > 0)  THEN
2499      iret = NF90_CLOSE(netcdf_id(fid,2))
2500      IF (iret /= NF90_NOERR) THEN
2501        WRITE (n_e,'(I6)') iret
2502        WRITE (n_f,'(I3)') netcdf_id(fid,2)
2503        CALL ipslerr (2,'restclo', &
2504          "Error "//n_e//" in closing file : "//n_f,'',' ')
2505      ENDIF
2506      netcdf_id(fid,2) = -1
2507    ENDIF
2508!---
2509  ELSE
2510!---
2511    IF (l_dbg) WRITE(*,*) 'restclo : Closing all files'
2512!---
2513    DO ifnc=1,nb_fi
2514      IF (netcdf_id(ifnc,1) > 0) THEN
2515        iret = NF90_CLOSE(netcdf_id(ifnc,1))
2516        IF (iret /= NF90_NOERR) THEN
2517          WRITE (n_e,'(I6)') iret
2518          WRITE (n_f,'(I3)') netcdf_id(ifnc,1)
2519          CALL ipslerr (2,'restclo', &
2520            "Error "//n_e//" in closing file : "//n_f,'',' ')
2521        ENDIF
2522        IF (netcdf_id(ifnc,1) == netcdf_id(ifnc,2)) THEN
2523          netcdf_id(ifnc,2) = -1
2524        ENDIF
2525        netcdf_id(ifnc,1) = -1
2526      ENDIF
2527!-----
2528      IF (netcdf_id(ifnc,2) > 0) THEN
2529        iret = NF90_CLOSE(netcdf_id(ifnc,2))
2530        IF (iret /= NF90_NOERR) THEN
2531          WRITE (n_e,'(I6)') iret
2532          WRITE (n_f,'(I3)') netcdf_id(ifnc,2)
2533          CALL ipslerr (2,'restclo', &
2534            "Error "//n_e//" in closing file : "//n_f,'',' ')
2535        END IF
2536        netcdf_id(ifnc,2) = -1
2537      ENDIF
2538    ENDDO
2539  ENDIF
2540!---------------------
2541END SUBROUTINE restclo
2542!===
2543!-----------------
2544END MODULE restcom
Note: See TracBrowser for help on using the repository browser.