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