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