source: trunk/LMDZ.MARS/libf/phymars/iniwritesoil.F90 @ 3362

Last change on this file since 3362 was 3344, checked in by jbclement, 6 months ago

Mars PCM:
Modification of "iniwritesoil.F90": 'nsoilmx' and 'mlayer' are now arguments + making it as a module.
JBC

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