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

Last change on this file since 367 was 367, checked in by bellier, 14 years ago

Adapting some tests to be conform with new versions of NETCDF

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