source: trunk/LMDZ.MARS/libf/phymars/writediagsoil.F90 @ 1047

Last change on this file since 1047 was 1047, checked in by emillour, 11 years ago

Mars GCM:

  • IMPORTANT CHANGE: Removed all reference/use of ngridmx (dimphys.h) in routines (necessary prerequisite to using parallel dynamics); in most cases this just means adding 'ngrid' as routine argument, and making local saved variables allocatable (and allocated at first call). In the process, had to convert many *.h files to equivalent modules: yomaer.h => yomaer_h.F90 , surfdat.h => surfdat_h.F90 , comsaison.h => comsaison_h.F90 , yomlw.h => yomlw_h.F90 , comdiurn.h => comdiurn_h.F90 , dimradmars.h => dimradmars_mod.F90 , comgeomfi.h => comgeomfi_h.F90, comsoil.h => comsoil_h.F90 , slope.h => slope_mod.F90
  • Also updated EOF routines, everything is now in eofdump_mod.F90
  • Removed unused routine lectfux.F (in dyn3d)

EM

File size: 8.6 KB
Line 
1subroutine writediagsoil(ngrid,name,title,units,dimpx,px)
2
3! Write variable 'name' to NetCDF file 'diagsoil.nc'.
4! The variable may be 3D (lon,lat,depth) subterranean field,
5! a 2D (lon,lat) surface field, or a simple scalar (0D variable).
6!
7! Calls to 'writediagsoil' can originate from anywhere in the program;
8! An initialisation of variable 'name' is done if it is the first time
9! that this routine is called with given 'name'; otherwise data is appended
10! (yielding the sought time series of the variable)
11
12! Modifs: Aug.2010 Ehouarn: enforce outputs to be real*4
13
14use comsoil_h, only: nsoilmx
15
16implicit none
17
18#include"dimensions.h"
19!#include"dimphys.h"
20#include"paramet.h"
21#include"control.h"
22!#include"comsoil.h"
23#include"netcdf.inc"
24
25! Arguments:
26integer,intent(in) :: ngrid ! number of (horizontal) points of physics grid
27! i.e. ngrid = 2+(jjm-1)*iim - 1/jjm
28character(len=*),intent(in) :: name ! 'name' of the variable
29character(len=*),intent(in) :: title ! 'long_name' attribute of the variable
30character(len=*),intent(in) :: units ! 'units' attribute of the variable
31integer,intent(in) :: dimpx ! dimension of the variable (3,2 or 0)
32real,dimension(ngrid,nsoilmx),intent(in) :: px ! variable
33! Note: nsoilmx is a parameter set in 'comsoil_h'
34
35! Local variables:
36real*4,dimension(iip1,jjp1,nsoilmx) :: data3 ! to store 3D data
37! Note iip1,jjp1 known from paramet.h; nsoilmx known from comsoil_h
38real*4,dimension(iip1,jjp1) :: data2 ! to store 2D data
39real*4 :: data0 ! to store 0D data
40integer :: i,j,l ! for loops
41integer :: ig0
42
43real*4,save :: date ! time counter (in elapsed days)
44integer,save :: isample ! sample rate at which data is to be written to output
45integer,save :: ntime=0 ! counter to internally store time steps
46character(len=20),save :: firstname="1234567890"
47integer,save :: zitau=0
48
49character(len=30) :: filename="diagsoil.nc"
50
51! NetCDF stuff:
52integer :: nid ! NetCDF output file ID
53integer :: varid ! NetCDF ID of a variable
54integer :: ierr ! NetCDF routines return code
55integer,dimension(4) :: id ! NetCDF IDs of the dimensions of the variable
56integer,dimension(4) :: edges,corners
57
58! 1. Initialization step
59if (firstname.eq."1234567890") then
60  ! Store 'name' as 'firstname'
61  firstname=name
62  ! From now on, if 'name'.eq.'firstname', then it is a new time cycle
63
64  ! just to be sure, check that firstnom is large enough to hold nom
65  if (len_trim(firstname).lt.len_trim(name)) then
66    write(*,*) "writediagsoil: Error !!!"
67    write(*,*) "   firstname string not long enough!!"
68    write(*,*) "   increase its size to at least ",len_trim(name)
69    stop
70  endif
71 
72  ! Set output sample rate
73  isample=int(ecritphy) ! same as for diagfi outputs
74  ! Note ecritphy is known from control.h
75 
76  ! Create output NetCDF file
77  ierr=NF_CREATE(filename,IOR(NF_CLOBBER,NF_64BIT_OFFSET),nid)
78  if (ierr.ne.NF_NOERR) then
79    write(*,*)'writediagsoil: Error, failed creating file '//trim(filename)
80    stop
81  endif
82 
83  ! Define dimensions and axis attributes
84  call iniwritesoil(nid,ngrid)
85 
86  ! set zitau to -1 to be compatible with zitau incrementation step below
87  zitau=-1
88 
89else
90  ! If not an initialization call, simply open the NetCDF file
91  ierr=NF_OPEN(filename,NF_WRITE,nid)
92endif ! of if (firstname.eq."1234567890")
93
94! 2. Increment local time counter, if necessary
95if (name.eq.firstname) then
96  ! if we run across 'firstname', then it is a new time step
97  zitau=zitau+iphysiq
98  ! Note iphysiq is known from control.h
99endif
100
101! 3. Write data, if the time index matches the sample rate
102if (mod(zitau+1,isample).eq.0) then
103
104! 3.1 If first call at this date, update 'time' variable
105  if (name.eq.firstname) then
106    ntime=ntime+1
107    date=real(zitau+1)/real(day_step)
108    ! Note: day_step is known from control.h
109   
110    ! Get NetCDF ID for "time"
111    ierr=NF_INQ_VARID(nid,"time",varid)
112    ! Add the current value of date to the "time" array
113!#ifdef NC_DOUBLE
114!    ierr=NF_PUT_VARA_DOUBLE(nid,varid,ntime,1,date)
115!#else
116    ierr=NF_PUT_VARA_REAL(nid,varid,ntime,1,date)
117!#endif
118    if (ierr.ne.NF_NOERR) then
119      write(*,*)"writediagsoil: Failed writing date to time variable"
120      stop
121    endif
122  endif ! of if (name.eq.firstname)
123
124! 3.2 Write the variable to the NetCDF file
125if (dimpx.eq.3) then ! Case of a 3D variable
126  ! A. Recast data along 'dynamics' grid
127  do l=1,nsoilmx
128    ! handle the poles
129    do i=1,iip1
130      data3(i,1,l)=px(1,l)
131      data3(i,jjp1,l)=px(ngrid,l)
132    enddo
133    ! rest of the grid
134    do j=2,jjm
135      ig0=1+(j-2)*iim
136      do i=1,iim
137        data3(i,j,l)=px(ig0+i,l)
138      enddo
139      data3(iip1,j,l)=data3(1,j,l) ! extra (modulo) longitude
140    enddo
141  enddo
142 
143  ! B. Write (append) the variable to the NetCDF file
144  ! B.1. Get the ID of the variable
145  ierr=NF_INQ_VARID(nid,name,varid)
146  if (ierr.ne.NF_NOERR) then
147    ! If we failed geting the variable's ID, we assume it is because
148    ! the variable doesn't exist yet and must be created.
149    ! Start by obtaining corresponding dimensions IDs
150    ierr=NF_INQ_DIMID(nid,"longitude",id(1))
151    ierr=NF_INQ_DIMID(nid,"latitude",id(2))
152    ierr=NF_INQ_DIMID(nid,"depth",id(3))
153    ierr=NF_INQ_DIMID(nid,"time",id(4))
154    ! Tell the world about it
155    write(*,*) "====================="
156    write(*,*) "writediagsoil: creating variable "//trim(name)
157    call def_var(nid,name,title,units,4,id,varid,ierr)
158  endif ! of if (ierr.ne.NF_NOERR)
159 
160  ! B.2. Prepare things to be able to write/append the variable
161  corners(1)=1
162  corners(2)=1
163  corners(3)=1
164  corners(4)=ntime
165 
166  edges(1)=iip1
167  edges(2)=jjp1
168  edges(3)=nsoilmx
169  edges(4)=1
170 
171  ! B.3. Write the slab of data
172!#ifdef NC_DOUBLE
173!  ierr=NF_PUT_VARA_DOUBLE(nid,varid,corners,edges,data3)
174!#else
175  ierr=NF_PUT_VARA_REAL(nid,varid,corners,edges,data3)
176!#endif
177  if (ierr.ne.NF_NOERR) then
178    write(*,*) "writediagsoil: Error: Failed writing "//trim(name)//&
179               " to file "//trim(filename)//" at time",date
180  endif
181
182elseif (dimpx.eq.2) then ! Case of a 2D variable
183  ! A. Recast data along 'dynamics' grid
184  ! handle the poles
185  do i=1,iip1
186    data2(i,1)=px(1,1)
187    data2(i,jjp1)=px(ngrid,1)
188  enddo
189  ! rest of the grid
190  do j=2,jjm
191    ig0=1+(j-2)*iim
192    do i=1,iim
193      data2(i,j)=px(ig0+i,1)
194    enddo
195    data2(iip1,j)=data2(1,j) ! extra (modulo) longitude
196  enddo
197
198  ! B. Write (append) the variable to the NetCDF file
199  ! B.1. Get the ID of the variable
200  ierr=NF_INQ_VARID(nid,name,varid)
201  if (ierr.ne.NF_NOERR) then
202    ! If we failed geting the variable's ID, we assume it is because
203    ! the variable doesn't exist yet and must be created.
204    ! Start by obtaining corresponding dimensions IDs
205    ierr=NF_INQ_DIMID(nid,"longitude",id(1))
206    ierr=NF_INQ_DIMID(nid,"latitude",id(2))
207    ierr=NF_INQ_DIMID(nid,"time",id(3))
208    ! Tell the world about it
209    write(*,*) "====================="
210    write(*,*) "writediagsoil: creating variable "//trim(name)
211    call def_var(nid,name,title,units,3,id,varid,ierr)
212  endif ! of if (ierr.ne.NF_NOERR)
213
214  ! B.2. Prepare things to be able to write/append the variable
215  corners(1)=1
216  corners(2)=1
217  corners(3)=ntime
218 
219  edges(1)=iip1
220  edges(2)=jjp1
221  edges(3)=1
222 
223  ! B.3. Write the slab of data
224!#ifdef NC_DOUBLE
225!  ierr=NF_PUT_VARA_DOUBLE(nid,varid,corners,edges,data2)
226!#else
227  ierr=NF_PUT_VARA_REAL(nid,varid,corners,edges,data2)
228!#endif
229  if (ierr.ne.NF_NOERR) then
230    write(*,*) "writediagsoil: Error: Failed writing "//trim(name)//&
231               " to file "//trim(filename)//" at time",date
232  endif
233
234elseif (dimpx.eq.0) then ! Case of a 0D variable
235  ! A. Copy data value
236  data0=px(1,1)
237
238  ! B. Write (append) the variable to the NetCDF file
239  ! B.1. Get the ID of the variable
240  ierr=NF_INQ_VARID(nid,name,varid)
241  if (ierr.ne.NF_NOERR) then
242    ! If we failed geting the variable's ID, we assume it is because
243    ! the variable doesn't exist yet and must be created.
244    ! Start by obtaining corresponding dimensions IDs
245    ierr=NF_INQ_DIMID(nid,"time",id(1))
246    ! Tell the world about it
247    write(*,*) "====================="
248    write(*,*) "writediagsoil: creating variable "//trim(name)
249    call def_var(nid,name,title,units,1,id,varid,ierr)
250  endif ! of if (ierr.ne.NF_NOERR)
251
252  ! B.2. Prepare things to be able to write/append the variable
253  corners(1)=ntime
254 
255  edges(1)=1
256
257  ! B.3. Write the data
258!#ifdef NC_DOUBLE
259!  ierr=NF_PUT_VARA_DOUBLE(nid,varid,corners,edges,data0)
260!#else
261  ierr=NF_PUT_VARA_REAL(nid,varid,corners,edges,data0)
262!#endif
263  if (ierr.ne.NF_NOERR) then
264    write(*,*) "writediagsoil: Error: Failed writing "//trim(name)//&
265               " to file "//trim(filename)//" at time",date
266  endif
267
268endif ! of if (dimpx.eq.3) elseif (dimpx.eq.2) ...
269endif ! of if (mod(zitau+1,isample).eq.0)
270
271! 4. Close the NetCDF file
272ierr=NF_CLOSE(nid)
273
274end subroutine writediagsoil
Note: See TracBrowser for help on using the repository browser.