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