source: trunk/LMDZ.TITAN/libf/phytitan/iniwritesoil.F90

Last change on this file was 1531, checked in by mturbet, 9 years ago

Generic GCM:

  • Fix buggy ouputs in 1D introduced by previous code modifications.

EM+MT

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