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