source: trunk/LMDZ.GENERIC/libf/phystd/iniwritesoil.F90 @ 1395

Last change on this file since 1395 was 1384, checked in by emillour, 10 years ago

Generic GCM:

  • Some code cleanup: turning comcstfi.h into module comcstfi_mod.F90

EM

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