source: trunk/LMDZ.GENERIC/libf/phystd/iniwritesoil.F90 @ 965

Last change on this file since 965 was 965, checked in by emillour, 12 years ago

Common dynamics and generic/universal GCM:

  • LMDZ.COMMON: minor bug fix on the computation of physics mesh area in gcm.F
  • LMDZ.UNIVERSAL: missing clean initialization of tab_cntrl(:) array in phyredem.F90
  • LMDZ.GENERIC: minor bug fix in hydrol.F90, only output runoff if it is used. Update output routines so that all outputs files (stats, diagfi.nc, diagsoil.nc, diagspecIR.nc and diagspecVI.nc) can be generated when running LMDZ.UNIVERSAL in MPI mode.

EM

File size: 8.6 KB
Line 
1subroutine iniwritesoil(nid,ngrid)
2
3use comsoil_h, only : inertiedat, mlayer
4#ifdef CPP_PARA
5use mod_phys_lmdz_para, only : is_mpi_root, is_master, gather
6use mod_grid_phy_lmdz, only : klon_glo, Grid1Dto2D_glo
7#endif
8
9! initialization routine for 'writediagoil'. Here we create/define
10! dimensions (longitude, latitude, depth and time) and other fixed
11! (time-independent) parameters.
12
13implicit none
14
15#include"dimensions.h"
16#include"dimphys.h"
17#include"paramet.h"
18#include"comcstfi.h"
19#include"comgeom.h"
20#include"netcdf.inc"
21
22! Arguments:
23integer,intent(in) :: ngrid
24integer,intent(in) :: nid ! NetCDF output file ID
25
26! Local variables:
27
28! NetCDF stuff:
29integer :: ierr ! NetCDF routines return code
30integer :: idim_rlatu ! ID of the 'latitude' dimension
31integer :: idim_rlonv ! ID of the 'longitude' dimension
32integer :: idim_depth ! ID of the 'depth' dimension
33integer :: idim_time  ! ID of the 'time' dimension
34integer :: varid ! to store NetCDF ID of a variable
35integer,dimension(3) :: dimids ! to store IDs of dimensions of a variable
36character(len=60) :: text ! to store some text
37real,dimension(iip1,jjp1,nsoilmx) :: data3 ! to store 3D data
38integer :: i,j,l,ig0
39
40#ifdef CPP_PARA
41! Added to work in parallel mode
42real dx3_glop(klon_glo,nsoilmx)
43real dx3_glo(iim,jjp1,nsoilmx) ! to store a global 3D data set
44#else
45logical,parameter :: is_master=.true.
46logical,parameter :: is_mpi_root=.true.
47#endif
48
49! 1. Define the dimensions
50if (is_master) then
51! Switch to NetCDF define mode
52ierr=NF_REDEF(nid)
53
54! Define the dimensions
55ierr=NF_DEF_DIM(nid,"longitude",iip1,idim_rlonv)
56! iip1 known from paramet.h
57if (ierr.ne.NF_NOERR) then
58  write(*,*)"iniwritesoil: Error, could not define longitude dimension"
59endif
60ierr=NF_DEF_DIM(nid,"latitude",jjp1,idim_rlatu)
61! jjp1 known from paramet.h
62if (ierr.ne.NF_NOERR) then
63  write(*,*)"iniwritesoil: Error, could not define latitude dimension"
64endif
65ierr=NF_DEF_DIM(nid,"depth",nsoilmx,idim_depth)
66! nsoilmx known from dimphys.h
67if (ierr.ne.NF_NOERR) then
68  write(*,*)"iniwritesoil: Error, could not define depth dimension"
69endif
70ierr=NF_DEF_DIM(nid,"time",NF_UNLIMITED,idim_time)
71if (ierr.ne.NF_NOERR) then
72  write(*,*)"iniwritesoil: Error, could not define time dimension"
73endif
74
75! Switch out of NetCDF define mode
76ierr=NF_ENDDEF(nid)
77
78! 2. Define (as variables) and write dimensions, as well as their attributes
79! 2.1. Longitude
80ierr=NF_REDEF(nid) ! switch to NetCDF define mode
81
82! Define the variable
83#ifdef NC_DOUBLE
84ierr=NF_DEF_VAR(nid,"longitude",NF_DOUBLE,1,idim_rlonv,varid)
85#else
86ierr=NF_DEF_VAR(nid,"longitude",NF_FLOAT,1,idim_rlonv,varid)
87#endif
88if (ierr.ne.NF_NOERR) then
89  write(*,*)"iniwritesoil: Error, could not define longitude variable"
90endif
91
92! Longitude attributes
93text="East longitude"
94ierr=NF_PUT_ATT_TEXT(nid,varid,"long_name",len_trim(text),text)
95text="degrees_east"
96ierr=NF_PUT_ATT_TEXT(nid,varid,"units",len_trim(text),text)
97
98! Write longitude to file
99ierr=NF_ENDDEF(nid) ! switch out of NetCDF define mode
100! Write
101#ifdef NC_DOUBLE
102ierr=NF_PUT_VAR_DOUBLE(nid,varid,rlonv*(180./pi))
103#else
104ierr=NF_PUT_VAR_REAL(nid,varid,rlonv*(180./pi))
105#endif
106! Note: rlonv is known from comgeom.h and pi from comcstfi.h
107if (ierr.ne.NF_NOERR) then
108  write(*,*)"iniwritesoil: Error, could not write longitude variable"
109endif
110
111! 2.2. Latitude
112ierr=NF_REDEF(nid) ! switch to NetCDF define mode
113
114! Define the variable
115#ifdef NC_DOUBLE
116ierr=NF_DEF_VAR(nid,"latitude",NF_DOUBLE,1,idim_rlatu,varid)
117#else
118ierr=NF_DEF_VAR(nid,"latitude",NF_FLOAT,1,idim_rlatu,varid)
119#endif
120if (ierr.ne.NF_NOERR) then
121  write(*,*)"iniwritesoil: Error, could not define latitude variable"
122endif
123
124! Latitude attributes
125text="North latitude"
126ierr=NF_PUT_ATT_TEXT(nid,varid,"long_name",len_trim(text),text)
127text="degrees_north"
128ierr=NF_PUT_ATT_TEXT(nid,varid,"units",len_trim(text),text)
129
130! Write latitude to file
131ierr=NF_ENDDEF(nid) ! switch out of NetCDF define mode
132! Write
133#ifdef NC_DOUBLE
134ierr=NF_PUT_VAR_DOUBLE(nid,varid,rlatu*(180./pi))
135#else
136ierr=NF_PUT_VAR_REAL(nid,varid,rlatu*(180./pi))
137#endif
138! Note: rlatu is known from comgeom.h and pi from comcstfi.h
139if (ierr.ne.NF_NOERR) then
140  write(*,*)"iniwritesoil: Error, could not write longitude variable"
141endif
142
143! 2.3. Depth
144ierr=NF_REDEF(nid) ! switch to NetCDF define mode
145
146! Define the variable
147#ifdef NC_DOUBLE
148ierr=NF_DEF_VAR(nid,"depth",NF_DOUBLE,1,idim_depth,varid)
149#else
150ierr=NF_DEF_VAR(nid,"depth",NF_FLOAT,1,idim_depth,varid)
151#endif
152if (ierr.ne.NF_NOERR) then
153  write(*,*)"iniwritesoil: Error, could not define depth variable"
154endif
155
156! Depth attributes
157text="Soil mid-layer depth"
158ierr=NF_PUT_ATT_TEXT(nid,varid,"long_name",len_trim(text),text)
159text="m"
160ierr=NF_PUT_ATT_TEXT(nid,varid,"units",len_trim(text),text)
161text="down"
162ierr=NF_PUT_ATT_TEXT(nid,varid,"positive",len_trim(text),text)
163
164! Write depth to file
165ierr=NF_ENDDEF(nid) ! switch out of NetCDF define mode
166! Write
167#ifdef NC_DOUBLE
168ierr=NF_PUT_VAR_DOUBLE(nid,varid,mlayer)
169#else
170ierr=NF_PUT_VAR_REAL(nid,varid,mlayer)
171#endif
172! Note mlayer(0:nsoilmx-1) known from comsoil.h
173if (ierr.ne.NF_NOERR) then
174  write(*,*)"iniwritesoil: Error, could not write depth variable"
175endif
176
177! 2.4. Time
178ierr=NF_REDEF(nid) ! switch to NetCDF define mode
179
180! Define the variable
181#ifdef NC_DOUBLE
182ierr=NF_DEF_VAR(nid,"time",NF_DOUBLE,1,idim_time,varid)
183#else
184ierr=NF_DEF_VAR(nid,"time",NF_FLOAT,1,idim_time,varid)
185#endif
186if (ierr.ne.NF_NOERR) then
187  write(*,*)"iniwritesoil: Error, could not define depth variable"
188endif
189
190! time attributes
191text="Time"
192ierr=NF_PUT_ATT_TEXT(nid,varid,"long_name",len_trim(text),text)
193text="days since 0000-01-01 00:00:00"
194ierr=NF_PUT_ATT_TEXT(nid,varid,"units",len_trim(text),text)
195
196ierr=NF_ENDDEF(nid) ! switch out of NetCDF define mode
197! Note no need to write time variable here; it is done in writediagsoil.
198
199endif ! of if (is_master)
200
201! 3. Other variables to be included
202
203! 3.1 mesh area surrounding each horizontal point
204if (is_master) then
205ierr=NF_REDEF(nid) ! switch to NetCDF define mode
206
207! Define the variable
208dimids(1)=idim_rlonv ! ID of the 'longitude' dimension
209dimids(2)=idim_rlatu ! ID of the 'latitude' dimension
210#ifdef NC_DOUBLE
211ierr=NF_DEF_VAR(nid,"area",NF_DOUBLE,2,dimids,varid)
212#else
213ierr=NF_DEF_VAR(nid,"area",NF_FLOAT,2,dimids,varid)
214#endif
215if (ierr.ne.NF_NOERR) then
216  write(*,*)"iniwritesoil: Error, could not define area variable"
217endif
218
219! Area attributes
220text="Mesh area"
221ierr=NF_PUT_ATT_TEXT(nid,varid,"long_name",len_trim(text),text)
222text="m2"
223ierr=NF_PUT_ATT_TEXT(nid,varid,"units",len_trim(text),text)
224
225! Write area to file
226ierr=NF_ENDDEF(nid) ! switch out of NetCDF define mode
227! Write
228#ifdef NC_DOUBLE
229ierr=NF_PUT_VAR_DOUBLE(nid,varid,aire)
230#else
231ierr=NF_PUT_VAR_REAL(nid,varid,aire)
232#endif
233! Note: aire is known from comgeom.h
234if (ierr.ne.NF_NOERR) then
235  write(*,*)"iniwritesoil: Error, could not write area variable"
236endif
237
238endif ! of if (is_master)
239
240
241! 3.2 Thermal inertia
242if (is_master) then
243ierr=NF_REDEF(nid) ! switch to NetCDF define mode
244
245! Define the variable
246dimids(1)=idim_rlonv ! ID of the 'longitude' dimension
247dimids(2)=idim_rlatu ! ID of the 'latitude' dimension
248dimids(3)=idim_depth ! ID of the 'depth' dimension
249#ifdef NC_DOUBLE
250ierr=NF_DEF_VAR(nid,"th_inertia",NF_DOUBLE,3,dimids,varid)
251#else
252ierr=NF_DEF_VAR(nid,"th_inertia",NF_FLOAT,3,dimids,varid)
253#endif
254if (ierr.ne.NF_NOERR) then
255  write(*,*)"iniwritesoil: Error, could not define th_inertia variable"
256endif
257
258! Attributes
259text="Thermal inertia"
260ierr=NF_PUT_ATT_TEXT(nid,varid,"long_name",len_trim(text),text)
261text="J.s-1/2.m-2.K-1"
262ierr=NF_PUT_ATT_TEXT(nid,varid,"units",len_trim(text),text)
263
264endif !of if (is_master)
265
266! Recast data along 'dynamics' grid
267#ifdef CPP_PARA
268  ! gather field on a "global" (without redundant longitude) array
269  call Gather(inertiedat,dx3_glop)
270!$OMP MASTER
271  if (is_mpi_root) then
272    call Grid1Dto2D_glo(dx3_glop,dx3_glo)
273    ! copy dx3_glo() to dx3(:) and add redundant longitude
274    data3(1:iim,:,:)=dx3_glo(1:iim,:,:)
275    data3(iip1,:,:)=data3(1,:,:)
276  endif
277!$OMP END MASTER
278!$OMP BARRIER
279#else
280! Note: inertiedat is known from comsoil.h
281do l=1,nsoilmx
282  ! handle the poles
283  do i=1,iip1
284    data3(i,1,l)=inertiedat(1,l)
285    data3(i,jjp1,l)=inertiedat(ngrid,l)
286  enddo
287  ! rest of the grid
288  do j=2,jjm
289    ig0=1+(j-2)*iim
290    do i=1,iim
291      data3(i,j,l)=inertiedat(ig0+i,l)
292    enddo
293    data3(iip1,j,l)=data3(1,j,l) ! extra (modulo) longitude
294  enddo
295enddo ! of do l=1,nsoilmx
296#endif
297
298! Write data3 to file
299if (is_master) then
300 ierr=NF_ENDDEF(nid) ! switch out of NetCDF define mode
301 ! Write
302#ifdef NC_DOUBLE
303 ierr=NF_PUT_VAR_DOUBLE(nid,varid,data3)
304#else
305 ierr=NF_PUT_VAR_REAL(nid,varid,data3)
306#endif
307 if (ierr.ne.NF_NOERR) then
308  write(*,*)"iniwritesoil: Error, could not write th_inertia variable"
309 endif
310endif ! of if (is_master)
311
312end subroutine iniwritesoil
Note: See TracBrowser for help on using the repository browser.