source: trunk/LMDZ.COMMON/libf/dyn3d_common/dynredem.F90 @ 2236

Last change on this file since 2236 was 1850, checked in by jvatant, 7 years ago

Follow-up of r1849 : understood the black magic !
--JVO

File size: 14.5 KB
Line 
1!
2! $Id: dynredem.F 1635 2012-07-12 11:37:16Z lguez $
3!
4!
5SUBROUTINE dynredem0(fichnom,iday_end,phis)
6#ifdef CPP_IOIPSL
7  USE IOIPSL
8#endif
9  USE infotrac, ONLY: nqtot, tname, ttext
10  USE netcdf, ONLY: NF90_CREATE, NF90_DEF_DIM, NF90_INQ_VARID, NF90_GLOBAL,    &
11                    NF90_CLOSE,  NF90_PUT_ATT, NF90_UNLIMITED, NF90_CLOBBER
12  USE dynredem_mod, ONLY: cre_var, put_var1, put_var2, err, modname, fil
13  use netcdf95, only: NF95_PUT_VAR
14  use control_mod, only : planet_type
15  USE comvert_mod, ONLY: ap,bp,aps,bps,presnivs,pseudoalt,pa,preff, &
16                        nivsig,nivsigs
17  USE comconst_mod, ONLY: daysec,dtvr,rad,omeg,g,cpp,kappa,pi
18  USE logic_mod, ONLY: fxyhypb,ysinus
19  USE serre_mod, ONLY: clon,clat,grossismx,grossismy,dzoomx,dzoomy, &
20                        taux,tauy
21  USE temps_mod, ONLY: annee_ref,day_ref,itau_dyn,itaufin, &
22                        start_time,hour_ini
23  USE ener_mod, ONLY: etot0,ptot0,ztot0,stot0,ang0
24
25  IMPLICIT NONE
26!=======================================================================
27! Writting the NetCDF restart file (initialisation)
28!=======================================================================
29!   Declarations:
30!   -------------
31  include "dimensions.h"
32  include "paramet.h"
33  include "comgeom2.h"
34  include "netcdf.inc"
35  include "iniprint.h"
36
37!===============================================================================
38! Arguments:
39  CHARACTER(LEN=*), INTENT(IN) :: fichnom          !--- FILE NAME
40  INTEGER,          INTENT(IN) :: iday_end         !---
41  REAL,             INTENT(IN) :: phis(iip1, jjp1) !--- GROUND GEOPOTENTIAL
42!===============================================================================
43! Local variables:
44  INTEGER :: iq,l
45  INTEGER, PARAMETER :: length=100
46  REAL :: tab_cntrl(length) ! run parameters
47  INTEGER :: ierr
48  CHARACTER(LEN=80) :: abort_message
49
50!   For NetCDF:
51  CHARACTER(LEN=30) :: unites
52  INTEGER :: indexID
53  INTEGER :: rlonuID, rlonvID, rlatuID, rlatvID
54  INTEGER :: sID, sigID, nID, vID, timID
55  INTEGER :: yyears0, jjour0, mmois0
56  REAL    :: zan0, zjulian, hours
57
58  CHARACTER(len=12) :: start_file_type="earth" ! default start file type
59  INTEGER :: idecal
60
61!===============================================================================
62  ! fill dynredem_mod module variables
63  modname='dynredem0'; fil=fichnom
64
65#ifdef CPP_IOIPSL
66  call ymds2ju(annee_ref, 1, iday_end, 0.0, zjulian)
67  call ju2ymds(zjulian, yyears0, mmois0, jjour0, hours)
68#else
69! set yyears0, mmois0, jjour0 to 0,1,1 (hours is not used)
70  yyears0=0
71  mmois0=1
72  jjour0=1
73#endif       
74
75  !!! AS: idecal is a hack to be able to read planeto starts...
76  !!!     .... while keeping everything OK for LMDZ EARTH
77  if ((planet_type.eq."generic").or.(planet_type.eq."mars")) then
78    write(lunout,*) trim(modname),' : Planeto-like start file'
79    start_file_type="planeto"
80    idecal = 4
81  else
82    if ( planet_type.eq."titan" ) then
83      ! Titan inherited Earth-like start files with idecal=5
84      write(lunout,*) trim(modname),' : Titan start file'
85    else
86      write(lunout,*) trim(modname),' : Earth-like start file'
87    endif
88    idecal = 5
89  endif
90
91  tab_cntrl(:)  = 0.
92  tab_cntrl(1)  = REAL(iim)
93  tab_cntrl(2)  = REAL(jjm)
94  tab_cntrl(3)  = REAL(llm)
95  if (start_file_type.eq."earth") then
96    tab_cntrl(4)=REAL(day_ref)
97  else
98    !tab_cntrl(4)=REAL(day_end)
99    tab_cntrl(4)=REAL(iday_end)
100  endif
101  tab_cntrl(5)  = REAL(annee_ref)
102  tab_cntrl(idecal+1)  = rad
103  tab_cntrl(idecal+2)  = omeg
104  tab_cntrl(idecal+3)  = g
105  tab_cntrl(idecal+4)  = cpp
106  tab_cntrl(idecal+5) = kappa
107  tab_cntrl(idecal+6) = daysec
108  tab_cntrl(idecal+7) = dtvr
109  tab_cntrl(idecal+8) = etot0
110  tab_cntrl(idecal+9) = ptot0
111  tab_cntrl(idecal+10) = ztot0
112  tab_cntrl(idecal+11) = stot0
113  tab_cntrl(idecal+12) = ang0
114  tab_cntrl(idecal+13) = pa
115  tab_cntrl(idecal+14) = preff
116
117!    .....    parameters for the zoom      ......   
118  tab_cntrl(idecal+15)  = clon
119  tab_cntrl(idecal+16)  = clat
120  tab_cntrl(idecal+17)  = grossismx
121  tab_cntrl(idecal+18)  = grossismy
122!
123  IF ( fxyhypb )   THEN
124    tab_cntrl(idecal+19) = 1.
125    tab_cntrl(idecal+20) = dzoomx
126    tab_cntrl(idecal+21) = dzoomy
127    tab_cntrl(idecal+22) = 0.
128    tab_cntrl(idecal+23) = taux
129    tab_cntrl(idecal+24) = tauy
130  ELSE
131    tab_cntrl(idecal+19) = 0.
132    tab_cntrl(idecal+20) = dzoomx
133    tab_cntrl(idecal+21) = dzoomy
134    tab_cntrl(idecal+22) = 0.
135    tab_cntrl(idecal+23) = 0.
136    tab_cntrl(idecal+24) = 0.
137    IF( ysinus )  tab_cntrl(idecal+22) = 1.
138  ENDIF
139
140  if (start_file_type.eq."earth") then
141    tab_cntrl(idecal+25) = REAL(iday_end)
142    tab_cntrl(idecal+26) = REAL(itau_dyn + itaufin)
143! start_time: start_time of simulation (not necessarily 0.)
144    tab_cntrl(idecal+27) = start_time
145  endif
146
147  if (planet_type=="mars") then ! For Mars only
148    tab_cntrl(29)=hour_ini
149  endif
150
151!--- File creation
152  CALL err(NF90_CREATE(fichnom,NF90_CLOBBER,nid))
153
154!--- Some global attributes
155  CALL err(NF90_PUT_ATT(nid,NF90_GLOBAL,"title","Fichier demarrage dynamique"))
156
157!--- Dimensions
158  if (start_file_type.eq."earth") then
159    CALL err(NF90_DEF_DIM(nid,"index", length, indexID))
160    CALL err(NF90_DEF_DIM(nid,"rlonu", iip1,   rlonuID))
161    CALL err(NF90_DEF_DIM(nid,"rlatu", jjp1,   rlatuID))
162    CALL err(NF90_DEF_DIM(nid,"rlonv", iip1,   rlonvID))
163    CALL err(NF90_DEF_DIM(nid,"rlatv", jjm,    rlatvID))
164    CALL err(NF90_DEF_DIM(nid,"sigs",  llm,        sID))
165    CALL err(NF90_DEF_DIM(nid,"sig",   llmp1,    sigID))
166    CALL err(NF90_DEF_DIM(nid,"temps", NF90_UNLIMITED, timID))
167  else
168    CALL err(NF90_DEF_DIM(nid,"index", length, indexID))
169    CALL err(NF90_DEF_DIM(nid,"rlonu", iip1,   rlonuID))
170    CALL err(NF90_DEF_DIM(nid,"latitude", jjp1,   rlatuID))
171    CALL err(NF90_DEF_DIM(nid,"longitude", iip1,   rlonvID))
172    CALL err(NF90_DEF_DIM(nid,"rlatv", jjm,    rlatvID))
173    CALL err(NF90_DEF_DIM(nid,"altitude",  llm,        sID))
174    CALL err(NF90_DEF_DIM(nid,"interlayer",   llmp1,    sigID))
175    CALL err(NF90_DEF_DIM(nid,"Time", NF90_UNLIMITED, timID))
176  endif
177
178!--- Define and save invariant fields
179  CALL put_var1(nid,"controle","Parametres de controle" ,[indexID],tab_cntrl)
180  CALL put_var1(nid,"rlonu"   ,"Longitudes des points U",[rlonuID],rlonu)
181  CALL put_var1(nid,"rlatu"   ,"Latitudes des points U" ,[rlatuID],rlatu)
182  CALL put_var1(nid,"rlonv"   ,"Longitudes des points V",[rlonvID],rlonv)
183  CALL put_var1(nid,"rlatv"   ,"Latitudes des points V" ,[rlatvID],rlatv)
184  if (start_file_type.eq."earth") then
185    CALL put_var1(nid,"nivsigs" ,"Numero naturel des couches s"    ,[sID]  ,nivsigs)
186    CALL put_var1(nid,"nivsig"  ,"Numero naturel des couches sigma",[sigID],nivsig)
187  endif ! of if (start_file_type.eq."earth")
188  CALL put_var1(nid,"ap"      ,"Coefficient A pour hybride"      ,[sigID],ap)
189  CALL put_var1(nid,"bp"      ,"Coefficient B pour hybride"      ,[sigID],bp)
190  if (start_file_type.ne."earth") then
191    CALL put_var1(nid,"aps","Coef AS: hybrid pressure at midlayers",[sID],aps)
192    CALL put_var1(nid,"bps","Coef BS: hybrid sigma at midlayers",[sID],bps)
193  endif ! of if (start_file_type.eq."earth")
194  CALL put_var1(nid,"presnivs",""                                ,[sID]  ,presnivs)
195  if (start_file_type.ne."earth") then
196        ierr = NF_REDEF (nid)
197#ifdef NC_DOUBLE
198        ierr = NF_DEF_VAR(nid,"latitude",NF_DOUBLE,1,rlatuID,vID)
199#else
200        ierr = NF_DEF_VAR(nid,"latitude",NF_FLOAT,1,rlatuID,vID)
201#endif
202        ierr =NF_PUT_ATT_TEXT(nid,vID,'units',13,"degrees_north")
203        ierr = NF_PUT_ATT_TEXT (nid,vID,"long_name", 14, &
204              "North latitude")
205        ierr = NF_ENDDEF(nid)
206        call NF95_PUT_VAR(nid,vID,rlatu*180/pi)
207!
208        ierr = NF_REDEF (nid)
209#ifdef NC_DOUBLE
210        ierr=NF_DEF_VAR(nid,"longitude",NF_DOUBLE,1,rlonvID,vID)
211#else
212        ierr=NF_DEF_VAR(nid,"longitude",NF_FLOAT,1,rlonvID,vID)
213#endif
214        ierr = NF_PUT_ATT_TEXT (nid,vID,"long_name", 14, &
215              "East longitude")
216        ierr = NF_PUT_ATT_TEXT(nid,vID,'units',12,"degrees_east")
217        ierr = NF_ENDDEF(nid)
218        call NF95_PUT_VAR(nid,vID,rlonv*180/pi)
219!
220        ierr = NF_REDEF (nid)
221#ifdef NC_DOUBLE
222        ierr = NF_DEF_VAR (nid, "altitude", NF_DOUBLE, 1, &
223             sID,vID)
224#else
225        ierr = NF_DEF_VAR (nid, "altitude", NF_FLOAT, 1, &
226             sID,vID)
227#endif
228        ierr = NF_PUT_ATT_TEXT(nid,vID,"long_name",10,"pseudo-alt")
229        ierr = NF_PUT_ATT_TEXT (nid,vID,'units',2,"km")
230        ierr = NF_PUT_ATT_TEXT (nid,vID,'positive',2,"up")
231        ierr = NF_ENDDEF(nid)
232        call NF95_PUT_VAR(nid,vID,pseudoalt)
233        CALL err(NF_REDEF(nid))
234  endif ! of if (start_file_type.ne."earth")
235
236! covariant <-> contravariant <-> natural conversion coefficients
237  CALL put_var2(nid,"cu","Coefficient de passage pour U",[rlonuID,rlatuID],cu)
238  CALL put_var2(nid,"cv","Coefficient de passage pour V",[rlonvID,rlatvID],cv)
239  CALL put_var2(nid,"aire","Aires de chaque maille"     ,[rlonvID,rlatuID],aire)
240  CALL put_var2(nid,"phisinit","Geopotentiel au sol"    ,[rlonvID,rlatuID],phis)
241
242
243! Define variables that will be stored later:
244  WRITE(unites,"('days since ',i4,'-',i2.2,'-',i2.2,' 00:00:00')"),&
245               yyears0,mmois0,jjour0
246  IF (start_file_type.eq."earth") THEN
247    CALL cre_var(nid,"temps","Temps de simulation",[timID],unites)
248  ELSE
249    CALL cre_var(nid,"Time","Temps de simulation",[timID],unites)
250  ENDIF
251
252  CALL cre_var(nid,"ucov" ,"Vitesse U"  ,[rlonuID,rlatuID,sID,timID])
253  CALL cre_var(nid,"vcov" ,"Vitesse V"  ,[rlonvID,rlatvID,sID,timID])
254  CALL cre_var(nid,"teta" ,"Temperature",[rlonvID,rlatuID,sID,timID])
255
256  IF(nqtot.GE.1) THEN
257    DO iq=1,nqtot
258      CALL cre_var(nid,tname(iq),ttext(iq),[rlonvID,rlatuID,sID,timID])
259    END DO
260  ENDIF
261
262  CALL cre_var(nid,"masse","Masse d air"    ,[rlonvID,rlatuID,sID,timID])
263  CALL cre_var(nid,"ps"   ,"Pression au sol",[rlonvID,rlatuID    ,timID])
264
265  CALL err(NF90_CLOSE (nid)) ! close file
266
267  WRITE(lunout,*)TRIM(modname)//': iim,jjm,llm,iday_end',iim,jjm,llm,iday_end
268  WRITE(lunout,*)TRIM(modname)//': rad,omeg,g,cpp,kappa',rad,omeg,g,cpp,kappa
269
270END SUBROUTINE dynredem0
271
272!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
273
274SUBROUTINE dynredem1(fichnom,time,vcov,ucov,teta,q,masse,ps)
275!
276!-------------------------------------------------------------------------------
277! Purpose: Write the NetCDF restart file (append).
278!-------------------------------------------------------------------------------
279  USE infotrac, ONLY: nqtot, tname, type_trac
280  USE control_mod, only : planet_type
281  USE netcdf,   ONLY: NF90_OPEN,  NF90_NOWRITE, NF90_GET_VAR, NF90_INQ_VARID,  &
282                      NF90_CLOSE, NF90_WRITE,   NF90_PUT_VAR, NF90_NoErr
283  use netcdf95, only: NF95_PUT_VAR
284  USE temps_mod, ONLY: itaufin,itau_dyn
285  USE dynredem_mod, ONLY: dynredem_write_u, dynredem_write_v, dynredem_read_u, &
286                          err, modname, fil, msg
287 
288  IMPLICIT NONE
289  include "dimensions.h"
290  include "paramet.h"
291  include "netcdf.inc"
292  include "comgeom.h"
293  include "iniprint.h"
294!===============================================================================
295! Arguments:
296  CHARACTER(LEN=*), INTENT(IN) :: fichnom              !-- FILE NAME
297  REAL, INTENT(IN)    ::  time                         !-- TIME
298  REAL, INTENT(IN)    ::  vcov(iip1,jjm, llm)          !-- V COVARIANT WIND
299  REAL, INTENT(IN)    ::  ucov(iip1,jjp1,llm)          !-- U COVARIANT WIND
300  REAL, INTENT(IN)    ::  teta(iip1,jjp1,llm)          !-- POTENTIAL TEMPERATURE
301  REAL, INTENT(INOUT) ::     q(iip1,jjp1,llm,nqtot)    !-- TRACERS
302  REAL, INTENT(IN)    :: masse(iip1,jjp1,llm)          !-- MASS PER CELL
303  REAL, INTENT(IN)    ::    ps(iip1,jjp1)              !-- GROUND PRESSURE
304!===============================================================================
305! Local variables:
306  INTEGER :: l, iq, nid, vID, ierr, nid_trac, vID_trac
307  INTEGER,SAVE :: nb=0
308  INTEGER, PARAMETER :: length=100
309  REAL               :: tab_cntrl(length) ! tableau des parametres du run
310  CHARACTER(LEN=256) :: var, dum
311  LOGICAL            :: lread_inca
312  CHARACTER(LEN=80) :: abort_message
313  CHARACTER(len=12) :: start_file_type="earth" ! default start file type
314
315  ! fill dynredem_mod module variables
316  modname='dynredem1'; fil=fichnom
317
318  if ((planet_type.eq."generic").or.(planet_type.eq."mars")) then
319      write(lunout,*) trim(modname),' : Planeto-like start file'
320      start_file_type="planeto"
321  else
322      write(lunout,*) trim(modname),' : Earth-like start file'
323  endif
324
325  CALL err(NF90_OPEN(fil,NF90_WRITE,nid),"open",fil)
326
327!--- Write/extend time coordinate
328  nb = nb + 1
329  if (start_file_type.eq."earth") then
330        ierr = NF_INQ_VARID(nid, "temps", vID)
331        IF (ierr .NE. NF_NOERR) THEN
332          write(lunout,*) NF_STRERROR(ierr)
333          abort_message='Variable temps n est pas definie'
334          CALL abort_gcm(modname,abort_message,ierr)
335        ENDIF
336 else
337        ierr = NF_INQ_VARID(nid,"Time", vID)
338        IF (ierr .NE. NF_NOERR) THEN
339          write(lunout,*) NF_STRERROR(ierr)
340          abort_message='Variable Time not found'
341          CALL abort_gcm(modname,abort_message,ierr)
342        ENDIF
343  endif ! of if (start_file_type.eq."earth")
344  call NF95_PUT_VAR(nid,vID,time,start=(/nb/))
345  WRITE(lunout,*)TRIM(modname)//": Saving for ", nb, time
346
347!--- Rewrite control table (itaufin undefined in dynredem0)
348  var="controle"
349  CALL err(NF90_INQ_VARID(nid,var,vID),"inq",var)
350  CALL err(NF90_GET_VAR(nid,vID,tab_cntrl),"get",var)
351  if (start_file_type=="earth") then
352    tab_cntrl(31) = REAL(itau_dyn + itaufin)
353  else
354    tab_cntrl(31) = 0
355  endif
356  CALL err(NF90_INQ_VARID(nid,var,vID),"inq",var)
357  CALL err(NF90_PUT_VAR(nid,vID,tab_cntrl),"put",var)
358
359!--- Save fields
360  CALL dynredem_write_u(nid,"ucov" ,ucov ,llm, nb)
361  CALL dynredem_write_v(nid,"vcov" ,vcov ,llm, nb)
362  CALL dynredem_write_u(nid,"teta" ,teta ,llm, nb)
363  CALL dynredem_write_u(nid,"masse",masse,llm, nb)
364  CALL dynredem_write_u(nid,"ps"   ,ps   ,1, nb)
365
366!--- Tracers in file "start_trac.nc" (added by Anne)
367  lread_inca=.FALSE.; fil="start_trac.nc"
368  IF(type_trac=='inca') INQUIRE(FILE=fil,EXIST=lread_inca)
369  IF(lread_inca) CALL err(NF90_OPEN(fil,NF90_NOWRITE,nid_trac),"open")
370
371!--- Save tracers
372  IF(nqtot.GE.1) THEN
373    DO iq=1,nqtot
374      var=tname(iq); ierr=-1
375      IF(lread_inca) THEN                  !--- Possibly read from "start_trac.nc"
376        fil="start_trac.nc"
377        ierr=NF90_INQ_VARID(nid_trac,var,vID_trac)
378        dum='inq'; IF(ierr==NF90_NoErr) dum='fnd'
379        WRITE(lunout,*)msg(dum,var)
380
381
382        IF(ierr==NF90_NoErr) CALL dynredem_read_u(nid_trac,var,q(:,:,:,iq),llm)
383      END IF ! of IF(lread_inca)
384      fil=fichnom
385      CALL dynredem_write_u(nid,var,q(:,:,:,iq),llm,nb)
386    END DO ! of DO iq=1,nqtot
387  ENDIF ! of IF(nqtot.GE.1)
388
389  CALL err(NF90_CLOSE(nid),"close")
390  fil="start_trac.nc"
391  IF(lread_inca) CALL err(NF90_CLOSE(nid_trac),"close")
392
393END SUBROUTINE dynredem1
394
Note: See TracBrowser for help on using the repository browser.