1 | subroutine iniwritesoil(nid,ngrid) |
---|
2 | |
---|
3 | use comsoil_h, only : inertiedat, mlayer |
---|
4 | #ifdef CPP_PARA |
---|
5 | use mod_phys_lmdz_para, only : is_mpi_root, is_master, gather |
---|
6 | use 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 | |
---|
13 | implicit 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: |
---|
23 | integer,intent(in) :: ngrid |
---|
24 | integer,intent(in) :: nid ! NetCDF output file ID |
---|
25 | |
---|
26 | ! Local variables: |
---|
27 | |
---|
28 | ! NetCDF stuff: |
---|
29 | integer :: ierr ! NetCDF routines return code |
---|
30 | integer :: idim_rlatu ! ID of the 'latitude' dimension |
---|
31 | integer :: idim_rlonv ! ID of the 'longitude' dimension |
---|
32 | integer :: idim_depth ! ID of the 'depth' dimension |
---|
33 | integer :: idim_time ! ID of the 'time' dimension |
---|
34 | integer :: varid ! to store NetCDF ID of a variable |
---|
35 | integer,dimension(3) :: dimids ! to store IDs of dimensions of a variable |
---|
36 | character(len=60) :: text ! to store some text |
---|
37 | real,dimension(iip1,jjp1,nsoilmx) :: data3 ! to store 3D data |
---|
38 | integer :: i,j,l,ig0 |
---|
39 | |
---|
40 | #ifdef CPP_PARA |
---|
41 | ! Added to work in parallel mode |
---|
42 | real dx3_glop(klon_glo,nsoilmx) |
---|
43 | real dx3_glo(iim,jjp1,nsoilmx) ! to store a global 3D data set |
---|
44 | #else |
---|
45 | logical,parameter :: is_master=.true. |
---|
46 | logical,parameter :: is_mpi_root=.true. |
---|
47 | #endif |
---|
48 | |
---|
49 | ! 1. Define the dimensions |
---|
50 | if (is_master) then |
---|
51 | ! Switch to NetCDF define mode |
---|
52 | ierr=NF_REDEF(nid) |
---|
53 | |
---|
54 | ! Define the dimensions |
---|
55 | ierr=NF_DEF_DIM(nid,"longitude",iip1,idim_rlonv) |
---|
56 | ! iip1 known from paramet.h |
---|
57 | if (ierr.ne.NF_NOERR) then |
---|
58 | write(*,*)"iniwritesoil: Error, could not define longitude dimension" |
---|
59 | endif |
---|
60 | ierr=NF_DEF_DIM(nid,"latitude",jjp1,idim_rlatu) |
---|
61 | ! jjp1 known from paramet.h |
---|
62 | if (ierr.ne.NF_NOERR) then |
---|
63 | write(*,*)"iniwritesoil: Error, could not define latitude dimension" |
---|
64 | endif |
---|
65 | ierr=NF_DEF_DIM(nid,"depth",nsoilmx,idim_depth) |
---|
66 | ! nsoilmx known from dimphys.h |
---|
67 | if (ierr.ne.NF_NOERR) then |
---|
68 | write(*,*)"iniwritesoil: Error, could not define depth dimension" |
---|
69 | endif |
---|
70 | ierr=NF_DEF_DIM(nid,"time",NF_UNLIMITED,idim_time) |
---|
71 | if (ierr.ne.NF_NOERR) then |
---|
72 | write(*,*)"iniwritesoil: Error, could not define time dimension" |
---|
73 | endif |
---|
74 | |
---|
75 | ! Switch out of NetCDF define mode |
---|
76 | ierr=NF_ENDDEF(nid) |
---|
77 | |
---|
78 | ! 2. Define (as variables) and write dimensions, as well as their attributes |
---|
79 | ! 2.1. Longitude |
---|
80 | ierr=NF_REDEF(nid) ! switch to NetCDF define mode |
---|
81 | |
---|
82 | ! Define the variable |
---|
83 | #ifdef NC_DOUBLE |
---|
84 | ierr=NF_DEF_VAR(nid,"longitude",NF_DOUBLE,1,idim_rlonv,varid) |
---|
85 | #else |
---|
86 | ierr=NF_DEF_VAR(nid,"longitude",NF_FLOAT,1,idim_rlonv,varid) |
---|
87 | #endif |
---|
88 | if (ierr.ne.NF_NOERR) then |
---|
89 | write(*,*)"iniwritesoil: Error, could not define longitude variable" |
---|
90 | endif |
---|
91 | |
---|
92 | ! Longitude attributes |
---|
93 | text="East longitude" |
---|
94 | ierr=NF_PUT_ATT_TEXT(nid,varid,"long_name",len_trim(text),text) |
---|
95 | text="degrees_east" |
---|
96 | ierr=NF_PUT_ATT_TEXT(nid,varid,"units",len_trim(text),text) |
---|
97 | |
---|
98 | ! Write longitude to file |
---|
99 | ierr=NF_ENDDEF(nid) ! switch out of NetCDF define mode |
---|
100 | ! Write |
---|
101 | #ifdef NC_DOUBLE |
---|
102 | ierr=NF_PUT_VAR_DOUBLE(nid,varid,rlonv*(180./pi)) |
---|
103 | #else |
---|
104 | ierr=NF_PUT_VAR_REAL(nid,varid,rlonv*(180./pi)) |
---|
105 | #endif |
---|
106 | ! Note: rlonv is known from comgeom.h and pi from comcstfi.h |
---|
107 | if (ierr.ne.NF_NOERR) then |
---|
108 | write(*,*)"iniwritesoil: Error, could not write longitude variable" |
---|
109 | endif |
---|
110 | |
---|
111 | ! 2.2. Latitude |
---|
112 | ierr=NF_REDEF(nid) ! switch to NetCDF define mode |
---|
113 | |
---|
114 | ! Define the variable |
---|
115 | #ifdef NC_DOUBLE |
---|
116 | ierr=NF_DEF_VAR(nid,"latitude",NF_DOUBLE,1,idim_rlatu,varid) |
---|
117 | #else |
---|
118 | ierr=NF_DEF_VAR(nid,"latitude",NF_FLOAT,1,idim_rlatu,varid) |
---|
119 | #endif |
---|
120 | if (ierr.ne.NF_NOERR) then |
---|
121 | write(*,*)"iniwritesoil: Error, could not define latitude variable" |
---|
122 | endif |
---|
123 | |
---|
124 | ! Latitude attributes |
---|
125 | text="North latitude" |
---|
126 | ierr=NF_PUT_ATT_TEXT(nid,varid,"long_name",len_trim(text),text) |
---|
127 | text="degrees_north" |
---|
128 | ierr=NF_PUT_ATT_TEXT(nid,varid,"units",len_trim(text),text) |
---|
129 | |
---|
130 | ! Write latitude to file |
---|
131 | ierr=NF_ENDDEF(nid) ! switch out of NetCDF define mode |
---|
132 | ! Write |
---|
133 | #ifdef NC_DOUBLE |
---|
134 | ierr=NF_PUT_VAR_DOUBLE(nid,varid,rlatu*(180./pi)) |
---|
135 | #else |
---|
136 | ierr=NF_PUT_VAR_REAL(nid,varid,rlatu*(180./pi)) |
---|
137 | #endif |
---|
138 | ! Note: rlatu is known from comgeom.h and pi from comcstfi.h |
---|
139 | if (ierr.ne.NF_NOERR) then |
---|
140 | write(*,*)"iniwritesoil: Error, could not write longitude variable" |
---|
141 | endif |
---|
142 | |
---|
143 | ! 2.3. Depth |
---|
144 | ierr=NF_REDEF(nid) ! switch to NetCDF define mode |
---|
145 | |
---|
146 | ! Define the variable |
---|
147 | #ifdef NC_DOUBLE |
---|
148 | ierr=NF_DEF_VAR(nid,"depth",NF_DOUBLE,1,idim_depth,varid) |
---|
149 | #else |
---|
150 | ierr=NF_DEF_VAR(nid,"depth",NF_FLOAT,1,idim_depth,varid) |
---|
151 | #endif |
---|
152 | if (ierr.ne.NF_NOERR) then |
---|
153 | write(*,*)"iniwritesoil: Error, could not define depth variable" |
---|
154 | endif |
---|
155 | |
---|
156 | ! Depth attributes |
---|
157 | text="Soil mid-layer depth" |
---|
158 | ierr=NF_PUT_ATT_TEXT(nid,varid,"long_name",len_trim(text),text) |
---|
159 | text="m" |
---|
160 | ierr=NF_PUT_ATT_TEXT(nid,varid,"units",len_trim(text),text) |
---|
161 | text="down" |
---|
162 | ierr=NF_PUT_ATT_TEXT(nid,varid,"positive",len_trim(text),text) |
---|
163 | |
---|
164 | ! Write depth to file |
---|
165 | ierr=NF_ENDDEF(nid) ! switch out of NetCDF define mode |
---|
166 | ! Write |
---|
167 | #ifdef NC_DOUBLE |
---|
168 | ierr=NF_PUT_VAR_DOUBLE(nid,varid,mlayer) |
---|
169 | #else |
---|
170 | ierr=NF_PUT_VAR_REAL(nid,varid,mlayer) |
---|
171 | #endif |
---|
172 | ! Note mlayer(0:nsoilmx-1) known from comsoil.h |
---|
173 | if (ierr.ne.NF_NOERR) then |
---|
174 | write(*,*)"iniwritesoil: Error, could not write depth variable" |
---|
175 | endif |
---|
176 | |
---|
177 | ! 2.4. Time |
---|
178 | ierr=NF_REDEF(nid) ! switch to NetCDF define mode |
---|
179 | |
---|
180 | ! Define the variable |
---|
181 | #ifdef NC_DOUBLE |
---|
182 | ierr=NF_DEF_VAR(nid,"time",NF_DOUBLE,1,idim_time,varid) |
---|
183 | #else |
---|
184 | ierr=NF_DEF_VAR(nid,"time",NF_FLOAT,1,idim_time,varid) |
---|
185 | #endif |
---|
186 | if (ierr.ne.NF_NOERR) then |
---|
187 | write(*,*)"iniwritesoil: Error, could not define depth variable" |
---|
188 | endif |
---|
189 | |
---|
190 | ! time attributes |
---|
191 | text="Time" |
---|
192 | ierr=NF_PUT_ATT_TEXT(nid,varid,"long_name",len_trim(text),text) |
---|
193 | text="days since 0000-01-01 00:00:00" |
---|
194 | ierr=NF_PUT_ATT_TEXT(nid,varid,"units",len_trim(text),text) |
---|
195 | |
---|
196 | ierr=NF_ENDDEF(nid) ! switch out of NetCDF define mode |
---|
197 | ! Note no need to write time variable here; it is done in writediagsoil. |
---|
198 | |
---|
199 | endif ! of if (is_master) |
---|
200 | |
---|
201 | ! 3. Other variables to be included |
---|
202 | |
---|
203 | ! 3.1 mesh area surrounding each horizontal point |
---|
204 | if (is_master) then |
---|
205 | ierr=NF_REDEF(nid) ! switch to NetCDF define mode |
---|
206 | |
---|
207 | ! Define the variable |
---|
208 | dimids(1)=idim_rlonv ! ID of the 'longitude' dimension |
---|
209 | dimids(2)=idim_rlatu ! ID of the 'latitude' dimension |
---|
210 | #ifdef NC_DOUBLE |
---|
211 | ierr=NF_DEF_VAR(nid,"area",NF_DOUBLE,2,dimids,varid) |
---|
212 | #else |
---|
213 | ierr=NF_DEF_VAR(nid,"area",NF_FLOAT,2,dimids,varid) |
---|
214 | #endif |
---|
215 | if (ierr.ne.NF_NOERR) then |
---|
216 | write(*,*)"iniwritesoil: Error, could not define area variable" |
---|
217 | endif |
---|
218 | |
---|
219 | ! Area attributes |
---|
220 | text="Mesh area" |
---|
221 | ierr=NF_PUT_ATT_TEXT(nid,varid,"long_name",len_trim(text),text) |
---|
222 | text="m2" |
---|
223 | ierr=NF_PUT_ATT_TEXT(nid,varid,"units",len_trim(text),text) |
---|
224 | |
---|
225 | ! Write area to file |
---|
226 | ierr=NF_ENDDEF(nid) ! switch out of NetCDF define mode |
---|
227 | ! Write |
---|
228 | #ifdef NC_DOUBLE |
---|
229 | ierr=NF_PUT_VAR_DOUBLE(nid,varid,aire) |
---|
230 | #else |
---|
231 | ierr=NF_PUT_VAR_REAL(nid,varid,aire) |
---|
232 | #endif |
---|
233 | ! Note: aire is known from comgeom.h |
---|
234 | if (ierr.ne.NF_NOERR) then |
---|
235 | write(*,*)"iniwritesoil: Error, could not write area variable" |
---|
236 | endif |
---|
237 | |
---|
238 | endif ! of if (is_master) |
---|
239 | |
---|
240 | |
---|
241 | ! 3.2 Thermal inertia |
---|
242 | if (is_master) then |
---|
243 | ierr=NF_REDEF(nid) ! switch to NetCDF define mode |
---|
244 | |
---|
245 | ! Define the variable |
---|
246 | dimids(1)=idim_rlonv ! ID of the 'longitude' dimension |
---|
247 | dimids(2)=idim_rlatu ! ID of the 'latitude' dimension |
---|
248 | dimids(3)=idim_depth ! ID of the 'depth' dimension |
---|
249 | #ifdef NC_DOUBLE |
---|
250 | ierr=NF_DEF_VAR(nid,"th_inertia",NF_DOUBLE,3,dimids,varid) |
---|
251 | #else |
---|
252 | ierr=NF_DEF_VAR(nid,"th_inertia",NF_FLOAT,3,dimids,varid) |
---|
253 | #endif |
---|
254 | if (ierr.ne.NF_NOERR) then |
---|
255 | write(*,*)"iniwritesoil: Error, could not define th_inertia variable" |
---|
256 | endif |
---|
257 | |
---|
258 | ! Attributes |
---|
259 | text="Thermal inertia" |
---|
260 | ierr=NF_PUT_ATT_TEXT(nid,varid,"long_name",len_trim(text),text) |
---|
261 | text="J.s-1/2.m-2.K-1" |
---|
262 | ierr=NF_PUT_ATT_TEXT(nid,varid,"units",len_trim(text),text) |
---|
263 | |
---|
264 | endif !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 |
---|
281 | do 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 |
---|
295 | enddo ! of do l=1,nsoilmx |
---|
296 | #endif |
---|
297 | |
---|
298 | ! Write data3 to file |
---|
299 | if (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 |
---|
310 | endif ! of if (is_master) |
---|
311 | |
---|
312 | end subroutine iniwritesoil |
---|