1 | MODULE iom_nf90 |
---|
2 | !!===================================================================== |
---|
3 | !! *** MODULE iom_nf90 *** |
---|
4 | !! Input/Output manager : Library to read input files with NF90 (only fliocom module) |
---|
5 | !!==================================================================== |
---|
6 | !! History : 9.0 ! 05 12 (J. Belier) Original code |
---|
7 | !! 9.0 ! 06 02 (S. Masson) Adaptation to NEMO |
---|
8 | !! " ! 07 07 (D. Storkey) Changes to iom_nf90_gettime |
---|
9 | !!-------------------------------------------------------------------- |
---|
10 | !!gm caution add !DIR nec: improved performance to be checked as well as no result changes |
---|
11 | |
---|
12 | !!-------------------------------------------------------------------- |
---|
13 | !! iom_open : open a file read only |
---|
14 | !! iom_close : close a file or all files opened by iom |
---|
15 | !! iom_get : read a field (interfaced to several routines) |
---|
16 | !! iom_gettime : read the time axis kvid in the file |
---|
17 | !! iom_varid : get the id of a variable in a file |
---|
18 | !! iom_rstput : write a field in a restart file (interfaced to several routines) |
---|
19 | !!-------------------------------------------------------------------- |
---|
20 | USE dom_oce ! ocean space and time domain |
---|
21 | USE lbclnk ! lateal boundary condition / mpp exchanges |
---|
22 | USE iom_def ! iom variables definitions |
---|
23 | USE netcdf ! NetCDF library |
---|
24 | USE in_out_manager ! I/O manager |
---|
25 | USE lib_mpp ! MPP library |
---|
26 | |
---|
27 | IMPLICIT NONE |
---|
28 | PRIVATE |
---|
29 | |
---|
30 | PUBLIC iom_nf90_open, iom_nf90_close, iom_nf90_varid, iom_nf90_get, iom_nf90_gettime, iom_nf90_rstput |
---|
31 | PUBLIC iom_nf90_getatt |
---|
32 | |
---|
33 | INTERFACE iom_nf90_get |
---|
34 | MODULE PROCEDURE iom_nf90_g0d, iom_nf90_g123d |
---|
35 | END INTERFACE |
---|
36 | INTERFACE iom_nf90_getatt |
---|
37 | MODULE PROCEDURE iom_nf90_intatt |
---|
38 | END INTERFACE |
---|
39 | INTERFACE iom_nf90_rstput |
---|
40 | MODULE PROCEDURE iom_nf90_rp0123d |
---|
41 | END INTERFACE |
---|
42 | |
---|
43 | !!---------------------------------------------------------------------- |
---|
44 | !! NEMO/OPA 3.3 , NEMO Consortium (2010) |
---|
45 | !! $Id$ |
---|
46 | !! Software governed by the CeCILL licence (NEMOGCM/NEMO_CeCILL.txt) |
---|
47 | !!---------------------------------------------------------------------- |
---|
48 | |
---|
49 | CONTAINS |
---|
50 | |
---|
51 | SUBROUTINE iom_nf90_open( cdname, kiomid, ldwrt, ldok, kdompar ) |
---|
52 | !!--------------------------------------------------------------------- |
---|
53 | !! *** SUBROUTINE iom_open *** |
---|
54 | !! |
---|
55 | !! ** Purpose : open an input file with NF90 |
---|
56 | !!--------------------------------------------------------------------- |
---|
57 | CHARACTER(len=*) , INTENT(inout) :: cdname ! File name |
---|
58 | INTEGER , INTENT( out) :: kiomid ! nf90 identifier of the opened file |
---|
59 | LOGICAL , INTENT(in ) :: ldwrt ! read or write the file? |
---|
60 | LOGICAL , INTENT(in ) :: ldok ! check the existence |
---|
61 | INTEGER, DIMENSION(2,5), INTENT(in ), OPTIONAL :: kdompar ! domain parameters: |
---|
62 | |
---|
63 | CHARACTER(LEN=256) :: clinfo ! info character |
---|
64 | CHARACTER(LEN=256) :: cltmp ! temporary character |
---|
65 | INTEGER :: iln ! lengths of character |
---|
66 | INTEGER :: istop ! temporary storage of nstop |
---|
67 | INTEGER :: if90id ! nf90 identifier of the opened file |
---|
68 | INTEGER :: idmy ! dummy variable |
---|
69 | INTEGER :: jl ! loop variable |
---|
70 | INTEGER :: ichunk ! temporary storage of nn_chunksz |
---|
71 | INTEGER :: imode ! creation mode flag: NF90_CLOBBER or NF90_NOCLOBBER or NF90_HDF5 |
---|
72 | INTEGER :: ihdf5 ! local variable for retrieval of value for NF90_HDF5 |
---|
73 | LOGICAL :: llclobber ! local definition of ln_clobber |
---|
74 | !--------------------------------------------------------------------- |
---|
75 | |
---|
76 | clinfo = ' iom_nf90_open ~~~ ' |
---|
77 | istop = nstop ! store the actual value of nstop |
---|
78 | IF( nn_chunksz > 0 ) THEN ; ichunk = nn_chunksz |
---|
79 | ELSE ; ichunk = NF90_SIZEHINT_DEFAULT |
---|
80 | ENDIF |
---|
81 | ! |
---|
82 | llclobber = ldwrt .AND. ln_clobber |
---|
83 | IF( ldok .AND. .NOT. llclobber ) THEN ! Open existing file... |
---|
84 | ! ! ============= |
---|
85 | IF( ldwrt ) THEN ! ... in write mode |
---|
86 | IF(lwp) WRITE(numout,*) TRIM(clinfo)//' open existing file: '//TRIM(cdname)//' in WRITE mode' |
---|
87 | #if defined key_netcdf4 |
---|
88 | CALL iom_nf90_check(NF90_OPEN( TRIM(cdname), NF90_WRITE , if90id ), clinfo) |
---|
89 | #else |
---|
90 | CALL iom_nf90_check(NF90_OPEN( TRIM(cdname), NF90_WRITE , if90id, chunksize = ichunk ), clinfo) |
---|
91 | #endif |
---|
92 | CALL iom_nf90_check(NF90_SET_FILL( if90id, NF90_NOFILL, idmy ), clinfo) |
---|
93 | ELSE ! ... in read mode |
---|
94 | IF(lwp) WRITE(numout,*) TRIM(clinfo)//' open existing file: '//TRIM(cdname)//' in READ mode' |
---|
95 | CALL iom_nf90_check(NF90_OPEN( TRIM(cdname), NF90_NOWRITE, if90id, chunksize = ichunk ), clinfo) |
---|
96 | ENDIF |
---|
97 | ELSE ! the file does not exist (or we overwrite it) |
---|
98 | ! ! ============= |
---|
99 | iln = INDEX( cdname, '.nc' ) |
---|
100 | IF( ldwrt ) THEN ! the file should be open in write mode so we create it... |
---|
101 | IF( jpnij > 1 ) THEN |
---|
102 | WRITE(cltmp,'(a,a,i4.4,a)') cdname(1:iln-1), '_', narea-1, '.nc' |
---|
103 | cdname = TRIM(cltmp) |
---|
104 | ENDIF |
---|
105 | IF(lwp) WRITE(numout,*) TRIM(clinfo)//' create new file: '//TRIM(cdname)//' in WRITE mode' |
---|
106 | |
---|
107 | IF( llclobber ) THEN ; imode = IOR( NF90_64BIT_OFFSET, NF90_CLOBBER ) |
---|
108 | ELSE ; imode = IOR( NF90_64BIT_OFFSET, NF90_NOCLOBBER ) |
---|
109 | ENDIF |
---|
110 | #if defined key_netcdf4 |
---|
111 | IF(lwp) WRITE(numout,*) TRIM(clinfo)//' creating file: '//TRIM(cdname)//' in hdf5 (netcdf4) mode' |
---|
112 | CALL GET_NF90_SYMBOL("NF90_HDF5", ihdf5) |
---|
113 | IF( llclobber ) THEN ; imode = IOR(ihdf5, NF90_CLOBBER) |
---|
114 | ELSE ; imode = IOR(ihdf5, NF90_NOCLOBBER) |
---|
115 | ENDIF |
---|
116 | CALL iom_nf90_check(NF90_CREATE( TRIM(cdname), imode, if90id ), clinfo) |
---|
117 | #else |
---|
118 | CALL iom_nf90_check(NF90_CREATE( TRIM(cdname), imode, if90id, chunksize = ichunk ), clinfo) |
---|
119 | #endif |
---|
120 | CALL iom_nf90_check(NF90_SET_FILL( if90id, NF90_NOFILL, idmy ), clinfo) |
---|
121 | ! define dimensions |
---|
122 | CALL iom_nf90_check(NF90_DEF_DIM( if90id, 'x', kdompar(1,1) , idmy ), clinfo) |
---|
123 | CALL iom_nf90_check(NF90_DEF_DIM( if90id, 'y', kdompar(2,1) , idmy ), clinfo) |
---|
124 | CALL iom_nf90_check(NF90_DEF_DIM( if90id, 'z', jpk , idmy ), clinfo) |
---|
125 | CALL iom_nf90_check(NF90_DEF_DIM( if90id, 't', NF90_UNLIMITED, idmy ), clinfo) |
---|
126 | ! global attributes |
---|
127 | CALL iom_nf90_check(NF90_PUT_ATT( if90id, NF90_GLOBAL, 'DOMAIN_number_total' , jpnij ), clinfo) |
---|
128 | CALL iom_nf90_check(NF90_PUT_ATT( if90id, NF90_GLOBAL, 'DOMAIN_number' , narea-1 ), clinfo) |
---|
129 | CALL iom_nf90_check(NF90_PUT_ATT( if90id, NF90_GLOBAL, 'DOMAIN_dimensions_ids' , (/1 , 2 /) ), clinfo) |
---|
130 | CALL iom_nf90_check(NF90_PUT_ATT( if90id, NF90_GLOBAL, 'DOMAIN_size_global' , (/jpiglo, jpjglo/) ), clinfo) |
---|
131 | CALL iom_nf90_check(NF90_PUT_ATT( if90id, NF90_GLOBAL, 'DOMAIN_size_local' , kdompar(:,1) ), clinfo) |
---|
132 | CALL iom_nf90_check(NF90_PUT_ATT( if90id, NF90_GLOBAL, 'DOMAIN_position_first' , kdompar(:,2) ), clinfo) |
---|
133 | CALL iom_nf90_check(NF90_PUT_ATT( if90id, NF90_GLOBAL, 'DOMAIN_position_last' , kdompar(:,3) ), clinfo) |
---|
134 | CALL iom_nf90_check(NF90_PUT_ATT( if90id, NF90_GLOBAL, 'DOMAIN_halo_size_start', kdompar(:,4) ), clinfo) |
---|
135 | CALL iom_nf90_check(NF90_PUT_ATT( if90id, NF90_GLOBAL, 'DOMAIN_halo_size_end' , kdompar(:,5) ), clinfo) |
---|
136 | CALL iom_nf90_check(NF90_PUT_ATT( if90id, NF90_GLOBAL, 'DOMAIN_type' , 'BOX' ), clinfo) |
---|
137 | ELSE ! the file should be open for read mode so it must exist... |
---|
138 | CALL ctl_stop( TRIM(clinfo), ' should be impossible case...' ) |
---|
139 | ENDIF |
---|
140 | ENDIF |
---|
141 | ! start to fill file informations |
---|
142 | ! ============= |
---|
143 | IF( istop == nstop ) THEN ! no error within this routine |
---|
144 | !does not work with some compilers kiomid = MINLOC(iom_file(:)%nfid, dim = 1) |
---|
145 | kiomid = 0 |
---|
146 | DO jl = jpmax_files, 1, -1 |
---|
147 | IF( iom_file(jl)%nfid == 0 ) kiomid = jl |
---|
148 | ENDDO |
---|
149 | iom_file(kiomid)%name = TRIM(cdname) |
---|
150 | iom_file(kiomid)%nfid = if90id |
---|
151 | iom_file(kiomid)%iolib = jpnf90 |
---|
152 | iom_file(kiomid)%nvars = 0 |
---|
153 | iom_file(kiomid)%irec = -1 ! useless for NetCDF files, used to know if the file is in define mode |
---|
154 | CALL iom_nf90_check(NF90_Inquire(if90id, unlimitedDimId = iom_file(kiomid)%iduld), clinfo) |
---|
155 | IF ( iom_file(kiomid)%iduld .GE. 0 ) THEN |
---|
156 | CALL iom_nf90_check(NF90_Inquire_Dimension(if90id, iom_file(kiomid)%iduld, & |
---|
157 | & name = iom_file(kiomid)%uldname, & |
---|
158 | & len = iom_file(kiomid)%lenuld ), clinfo ) |
---|
159 | ENDIF |
---|
160 | IF(lwp) WRITE(numout,*) ' ---> '//TRIM(cdname)//' OK' |
---|
161 | ELSE |
---|
162 | kiomid = 0 ! return error flag |
---|
163 | ENDIF |
---|
164 | ! |
---|
165 | END SUBROUTINE iom_nf90_open |
---|
166 | |
---|
167 | |
---|
168 | SUBROUTINE iom_nf90_close( kiomid ) |
---|
169 | !!-------------------------------------------------------------------- |
---|
170 | !! *** SUBROUTINE iom_nf90_close *** |
---|
171 | !! |
---|
172 | !! ** Purpose : close an input file with NF90 |
---|
173 | !!-------------------------------------------------------------------- |
---|
174 | INTEGER, INTENT(in) :: kiomid ! iom identifier of the file to be closed |
---|
175 | CHARACTER(LEN=100) :: clinfo ! info character |
---|
176 | !--------------------------------------------------------------------- |
---|
177 | ! |
---|
178 | clinfo = ' iom_nf90_close , file: '//TRIM(iom_file(kiomid)%name) |
---|
179 | CALL iom_nf90_check(NF90_CLOSE(iom_file(kiomid)%nfid), clinfo) |
---|
180 | ! |
---|
181 | END SUBROUTINE iom_nf90_close |
---|
182 | |
---|
183 | |
---|
184 | FUNCTION iom_nf90_varid ( kiomid, cdvar, kiv, kdimsz, kndims ) |
---|
185 | !!----------------------------------------------------------------------- |
---|
186 | !! *** FUNCTION iom_varid *** |
---|
187 | !! |
---|
188 | !! ** Purpose : get the id of a variable in a file with NF90 |
---|
189 | !!----------------------------------------------------------------------- |
---|
190 | INTEGER , INTENT(in ) :: kiomid ! file Identifier |
---|
191 | CHARACTER(len=*) , INTENT(in ) :: cdvar ! name of the variable |
---|
192 | INTEGER , INTENT(in ) :: kiv ! |
---|
193 | INTEGER, DIMENSION(:), INTENT( out), OPTIONAL :: kdimsz ! size of the dimensions |
---|
194 | INTEGER, INTENT( out), OPTIONAL :: kndims ! size of the dimensions |
---|
195 | ! |
---|
196 | INTEGER :: iom_nf90_varid ! iom variable Id |
---|
197 | INTEGER :: if90id ! nf90 file identifier |
---|
198 | INTEGER :: ji ! dummy loop index |
---|
199 | INTEGER :: ivarid ! NetCDF variable Id |
---|
200 | INTEGER :: i_nvd ! number of dimension of the variable |
---|
201 | INTEGER, DIMENSION(jpmax_dims) :: idimid ! dimension ids of the variable |
---|
202 | LOGICAL :: llok ! ok test |
---|
203 | CHARACTER(LEN=100) :: clinfo ! info character |
---|
204 | !!----------------------------------------------------------------------- |
---|
205 | clinfo = ' iom_nf90_varid, file: '//TRIM(iom_file(kiomid)%name)//', var: '//TRIM(cdvar) |
---|
206 | iom_nf90_varid = 0 ! default definition |
---|
207 | IF( PRESENT(kdimsz) ) kdimsz(:) = 0 ! default definition |
---|
208 | if90id = iom_file(kiomid)%nfid ! get back NetCDF file id |
---|
209 | ! |
---|
210 | llok = NF90_INQ_VARID( if90id, TRIM(cdvar), ivarid ) == nf90_noerr ! does the variable exist in the file |
---|
211 | IF( llok ) THEN |
---|
212 | iom_nf90_varid = kiv |
---|
213 | iom_file(kiomid)%nvars = kiv |
---|
214 | iom_file(kiomid)%nvid(kiv) = ivarid |
---|
215 | iom_file(kiomid)%cn_var(kiv) = TRIM(cdvar) |
---|
216 | CALL iom_nf90_check(NF90_Inquire_Variable(if90id, ivarid, ndims = i_nvd), clinfo) ! number of dimensions |
---|
217 | iom_file(kiomid)%ndims(kiv) = i_nvd |
---|
218 | CALL iom_nf90_check(NF90_Inquire_Variable(if90id, ivarid, dimids = idimid(1:i_nvd)), clinfo) ! dimensions ids |
---|
219 | iom_file(kiomid)%luld(kiv) = .FALSE. ! default value |
---|
220 | iom_file(kiomid)%dimsz(:,kiv) = 0 ! reset dimsz in case previously used |
---|
221 | DO ji = 1, i_nvd ! dimensions size |
---|
222 | CALL iom_nf90_check(NF90_Inquire_Dimension(if90id, idimid(ji), len = iom_file(kiomid)%dimsz(ji,kiv)), clinfo) |
---|
223 | IF( idimid(ji) == iom_file(kiomid)%iduld ) iom_file(kiomid)%luld(kiv) = .TRUE. ! unlimited dimension? |
---|
224 | END DO |
---|
225 | !---------- Deal with scale_factor and add_offset |
---|
226 | llok = NF90_Inquire_attribute(if90id, ivarid, 'scale_factor') == nf90_noerr |
---|
227 | IF( llok) THEN |
---|
228 | CALL iom_nf90_check(NF90_GET_ATT(if90id, ivarid, 'scale_factor', iom_file(kiomid)%scf(kiv)), clinfo) |
---|
229 | ELSE |
---|
230 | iom_file(kiomid)%scf(kiv) = 1. |
---|
231 | END IF |
---|
232 | llok = NF90_Inquire_attribute(if90id, ivarid, 'add_offset') == nf90_noerr |
---|
233 | IF( llok ) THEN |
---|
234 | CALL iom_nf90_check(NF90_GET_ATT(if90id, ivarid, 'add_offset', iom_file(kiomid)%ofs(kiv)), clinfo) |
---|
235 | ELSE |
---|
236 | iom_file(kiomid)%ofs(kiv) = 0. |
---|
237 | END IF |
---|
238 | ! return the simension size |
---|
239 | IF( PRESENT(kdimsz) ) THEN |
---|
240 | IF( i_nvd == SIZE(kdimsz) ) THEN |
---|
241 | kdimsz(:) = iom_file(kiomid)%dimsz(1:i_nvd,kiv) |
---|
242 | ELSE |
---|
243 | WRITE(ctmp1,*) i_nvd, SIZE(kdimsz) |
---|
244 | CALL ctl_stop( TRIM(clinfo), 'error in kdimsz size'//TRIM(ctmp1) ) |
---|
245 | ENDIF |
---|
246 | ENDIF |
---|
247 | IF( PRESENT(kndims) ) kndims = iom_file(kiomid)%ndims(kiv) |
---|
248 | ELSE |
---|
249 | iom_nf90_varid = -1 ! variable not found, return error code: -1 |
---|
250 | ENDIF |
---|
251 | ! |
---|
252 | END FUNCTION iom_nf90_varid |
---|
253 | |
---|
254 | |
---|
255 | SUBROUTINE iom_nf90_g0d( kiomid, kvid, pvar, kstart ) |
---|
256 | !!----------------------------------------------------------------------- |
---|
257 | !! *** ROUTINE iom_nf90_g0d *** |
---|
258 | !! |
---|
259 | !! ** Purpose : read a scalar with NF90 |
---|
260 | !!----------------------------------------------------------------------- |
---|
261 | INTEGER , INTENT(in ) :: kiomid ! Identifier of the file |
---|
262 | INTEGER , INTENT(in ) :: kvid ! variable id |
---|
263 | REAL(wp), INTENT( out) :: pvar ! read field |
---|
264 | INTEGER , DIMENSION(1), INTENT(in ), OPTIONAL :: kstart ! start position of the reading in each axis |
---|
265 | ! |
---|
266 | CHARACTER(LEN=100) :: clinfo ! info character |
---|
267 | !--------------------------------------------------------------------- |
---|
268 | clinfo = 'iom_nf90_g0d , file: '//TRIM(iom_file(kiomid)%name)//', var: '//TRIM(iom_file(kiomid)%cn_var(kvid)) |
---|
269 | CALL iom_nf90_check(NF90_GET_VAR(iom_file(kiomid)%nfid, iom_file(kiomid)%nvid(kvid), pvar, start = kstart), clinfo ) |
---|
270 | ! |
---|
271 | END SUBROUTINE iom_nf90_g0d |
---|
272 | |
---|
273 | |
---|
274 | SUBROUTINE iom_nf90_g123d( kiomid, kvid, knbdim, kstart, kcount, kx1, kx2, ky1, ky2, & |
---|
275 | & pv_r1d, pv_r2d, pv_r3d ) |
---|
276 | !!----------------------------------------------------------------------- |
---|
277 | !! *** ROUTINE iom_nf90_g123d *** |
---|
278 | !! |
---|
279 | !! ** Purpose : read a 1D/2D/3D variable with NF90 |
---|
280 | !! |
---|
281 | !! ** Method : read ONE record at each CALL |
---|
282 | !!----------------------------------------------------------------------- |
---|
283 | INTEGER , INTENT(in ) :: kiomid ! iom identifier of the file |
---|
284 | INTEGER , INTENT(in ) :: kvid ! Name of the variable |
---|
285 | INTEGER , INTENT(in ) :: knbdim ! number of dimensions of the variable |
---|
286 | INTEGER , DIMENSION(:) , INTENT(in ) :: kstart ! start position of the reading in each axis |
---|
287 | INTEGER , DIMENSION(:) , INTENT(in ) :: kcount ! number of points to be read in each axis |
---|
288 | INTEGER , INTENT(in ) :: kx1, kx2, ky1, ky2 ! subdomain indexes |
---|
289 | REAL(wp), DIMENSION(:) , INTENT( out), OPTIONAL :: pv_r1d ! read field (1D case) |
---|
290 | REAL(wp), DIMENSION(:,:) , INTENT( out), OPTIONAL :: pv_r2d ! read field (2D case) |
---|
291 | REAL(wp), DIMENSION(:,:,:) , INTENT( out), OPTIONAL :: pv_r3d ! read field (3D case) |
---|
292 | ! |
---|
293 | CHARACTER(LEN=100) :: clinfo ! info character |
---|
294 | INTEGER :: if90id ! nf90 identifier of the opened file |
---|
295 | INTEGER :: ivid ! nf90 variable id |
---|
296 | !--------------------------------------------------------------------- |
---|
297 | clinfo = 'iom_nf90_g123d , file: '//TRIM(iom_file(kiomid)%name)//', var: '//TRIM(iom_file(kiomid)%cn_var(kvid)) |
---|
298 | if90id = iom_file(kiomid)%nfid ! get back NetCDF file id |
---|
299 | ivid = iom_file(kiomid)%nvid(kvid) ! get back NetCDF var id |
---|
300 | ! |
---|
301 | IF( PRESENT(pv_r1d) ) THEN |
---|
302 | CALL iom_nf90_check( NF90_GET_VAR(if90id, ivid, pv_r1d(: ), start = kstart(1:knbdim), & |
---|
303 | & count = kcount(1:knbdim)), clinfo ) |
---|
304 | ELSEIF( PRESENT(pv_r2d) ) THEN |
---|
305 | CALL iom_nf90_check( NF90_GET_VAR(if90id, ivid, pv_r2d(kx1:kx2,ky1:ky2 ), start = kstart(1:knbdim), & |
---|
306 | & count = kcount(1:knbdim)), clinfo ) |
---|
307 | ELSEIF( PRESENT(pv_r3d) ) THEN |
---|
308 | CALL iom_nf90_check( NF90_GET_VAR(if90id, ivid, pv_r3d(kx1:kx2,ky1:ky2,:), start = kstart(1:knbdim), & |
---|
309 | & count = kcount(1:knbdim)), clinfo ) |
---|
310 | ENDIF |
---|
311 | ! |
---|
312 | END SUBROUTINE iom_nf90_g123d |
---|
313 | |
---|
314 | |
---|
315 | SUBROUTINE iom_nf90_intatt( kiomid, cdatt, pvar ) |
---|
316 | !!----------------------------------------------------------------------- |
---|
317 | !! *** ROUTINE iom_nf90_intatt *** |
---|
318 | !! |
---|
319 | !! ** Purpose : read an integer attribute with NF90 |
---|
320 | !!----------------------------------------------------------------------- |
---|
321 | INTEGER , INTENT(in ) :: kiomid ! Identifier of the file |
---|
322 | CHARACTER(len=*), INTENT(in ) :: cdatt ! attribute name |
---|
323 | INTEGER , INTENT( out) :: pvar ! read field |
---|
324 | ! |
---|
325 | INTEGER :: if90id ! temporary integer |
---|
326 | LOGICAL :: llok ! temporary logical |
---|
327 | CHARACTER(LEN=100) :: clinfo ! info character |
---|
328 | !--------------------------------------------------------------------- |
---|
329 | ! |
---|
330 | if90id = iom_file(kiomid)%nfid |
---|
331 | llok = NF90_Inquire_attribute(if90id, NF90_GLOBAL, cdatt) == nf90_noerr |
---|
332 | IF( llok) THEN |
---|
333 | clinfo = 'iom_nf90_getatt, file: '//TRIM(iom_file(kiomid)%name)//', att: '//TRIM(cdatt) |
---|
334 | CALL iom_nf90_check(NF90_GET_ATT(if90id, NF90_GLOBAL, cdatt, values=pvar), clinfo) |
---|
335 | ELSE |
---|
336 | CALL ctl_warn('iom_nf90_getatt: no attribute '//cdatt//' found') |
---|
337 | pvar = -999 |
---|
338 | ENDIF |
---|
339 | ! |
---|
340 | END SUBROUTINE iom_nf90_intatt |
---|
341 | |
---|
342 | |
---|
343 | SUBROUTINE iom_nf90_gettime( kiomid, kvid, ptime, cdunits, cdcalendar ) |
---|
344 | !!-------------------------------------------------------------------- |
---|
345 | !! *** SUBROUTINE iom_gettime *** |
---|
346 | !! |
---|
347 | !! ** Purpose : read the time axis kvid in the file with NF90 |
---|
348 | !!-------------------------------------------------------------------- |
---|
349 | INTEGER , INTENT(in ) :: kiomid ! file Identifier |
---|
350 | INTEGER , INTENT(in ) :: kvid ! variable id |
---|
351 | REAL(wp), DIMENSION(:) , INTENT( out) :: ptime ! the time axis |
---|
352 | CHARACTER(len=*), OPTIONAL, INTENT( out) :: cdunits ! units attribute |
---|
353 | CHARACTER(len=*), OPTIONAL, INTENT( out) :: cdcalendar ! calendar attribute |
---|
354 | ! |
---|
355 | CHARACTER(LEN=100) :: clinfo ! info character |
---|
356 | !--------------------------------------------------------------------- |
---|
357 | clinfo = 'iom_nf90_gettime, file: '//TRIM(iom_file(kiomid)%name)//', var: '//TRIM(iom_file(kiomid)%cn_var(kvid)) |
---|
358 | CALL iom_nf90_check(NF90_GET_VAR(iom_file(kiomid)%nfid, iom_file(kiomid)%nvid(kvid), ptime(:), & |
---|
359 | & start=(/ 1 /), count=(/ iom_file(kiomid)%dimsz(1, kvid) /)), clinfo) |
---|
360 | IF ( PRESENT(cdunits) ) THEN |
---|
361 | CALL iom_nf90_check(NF90_GET_ATT(iom_file(kiomid)%nfid, iom_file(kiomid)%nvid(kvid), "units", & |
---|
362 | & values=cdunits), clinfo) |
---|
363 | ENDIF |
---|
364 | IF ( PRESENT(cdcalendar) ) THEN |
---|
365 | CALL iom_nf90_check(NF90_GET_ATT(iom_file(kiomid)%nfid, iom_file(kiomid)%nvid(kvid), "calendar", & |
---|
366 | & values=cdcalendar), clinfo) |
---|
367 | ENDIF |
---|
368 | ! |
---|
369 | END SUBROUTINE iom_nf90_gettime |
---|
370 | |
---|
371 | |
---|
372 | SUBROUTINE iom_nf90_rp0123d( kt, kwrite, kiomid, cdvar , kvid , ktype, & |
---|
373 | & pv_r0d, pv_r1d, pv_r2d, pv_r3d ) |
---|
374 | !!-------------------------------------------------------------------- |
---|
375 | !! *** SUBROUTINE iom_nf90_rstput *** |
---|
376 | !! |
---|
377 | !! ** Purpose : read the time axis cdvar in the file |
---|
378 | !!-------------------------------------------------------------------- |
---|
379 | INTEGER , INTENT(in) :: kt ! ocean time-step |
---|
380 | INTEGER , INTENT(in) :: kwrite ! writing time-step |
---|
381 | INTEGER , INTENT(in) :: kiomid ! Identifier of the file |
---|
382 | CHARACTER(len=*) , INTENT(in) :: cdvar ! variable name |
---|
383 | INTEGER , INTENT(in) :: kvid ! variable id |
---|
384 | INTEGER , INTENT(in), OPTIONAL :: ktype ! variable type (default R8) |
---|
385 | REAL(wp) , INTENT(in), OPTIONAL :: pv_r0d ! written Od field |
---|
386 | REAL(wp), DIMENSION( :), INTENT(in), OPTIONAL :: pv_r1d ! written 1d field |
---|
387 | REAL(wp), DIMENSION(:, : ), INTENT(in), OPTIONAL :: pv_r2d ! written 2d field |
---|
388 | REAL(wp), DIMENSION(:, :, :), INTENT(in), OPTIONAL :: pv_r3d ! written 3d field |
---|
389 | ! |
---|
390 | INTEGER :: idims ! number of dimension |
---|
391 | INTEGER :: idvar ! variable id |
---|
392 | INTEGER :: jd ! dimension loop counter |
---|
393 | INTEGER :: ix1, ix2, iy1, iy2 ! subdomain indexes |
---|
394 | INTEGER, DIMENSION(4) :: idimsz ! dimensions size |
---|
395 | INTEGER, DIMENSION(4) :: idimid ! dimensions id |
---|
396 | CHARACTER(LEN=256) :: clinfo ! info character |
---|
397 | CHARACTER(LEN= 12), DIMENSION(4) :: cltmp ! temporary character |
---|
398 | INTEGER :: if90id ! nf90 file identifier |
---|
399 | INTEGER :: idmy ! dummy variable |
---|
400 | INTEGER :: itype ! variable type |
---|
401 | INTEGER, DIMENSION(4) :: ichunksz ! NetCDF4 chunk sizes. Will be computed using |
---|
402 | ! nn_nchunks_[i,j,k,t] namelist parameters |
---|
403 | INTEGER :: ichunkalg, ishuffle,& |
---|
404 | ideflate, ideflate_level |
---|
405 | ! NetCDF4 internally fixed parameters |
---|
406 | LOGICAL :: lchunk ! logical switch to activate chunking and compression |
---|
407 | ! when appropriate (currently chunking is applied to 4d fields only) |
---|
408 | !--------------------------------------------------------------------- |
---|
409 | ! |
---|
410 | clinfo = ' iom_nf90_rp0123d, file: '//TRIM(iom_file(kiomid)%name)//', var: '//TRIM(cdvar) |
---|
411 | if90id = iom_file(kiomid)%nfid |
---|
412 | ! |
---|
413 | ! define dimension variables if it is not already done |
---|
414 | ! ========================== |
---|
415 | IF( iom_file(kiomid)%nvars == 0 ) THEN |
---|
416 | ! are we in define mode? |
---|
417 | IF( iom_file(kiomid)%irec /= -1 ) THEN ! trick: irec used to know if the file is in define mode or not |
---|
418 | CALL iom_nf90_check(NF90_REDEF( if90id ), clinfo) ; iom_file(kiomid)%irec = -1 |
---|
419 | ENDIF |
---|
420 | ! define the dimension variables if it is not already done |
---|
421 | cltmp = (/ 'nav_lon ', 'nav_lat ', 'nav_lev ', 'time_counter' /) |
---|
422 | CALL iom_nf90_check(NF90_DEF_VAR( if90id, TRIM(cltmp(1)), NF90_FLOAT , (/ 1, 2 /), iom_file(kiomid)%nvid(1) ), clinfo) |
---|
423 | CALL iom_nf90_check(NF90_DEF_VAR( if90id, TRIM(cltmp(2)), NF90_FLOAT , (/ 1, 2 /), iom_file(kiomid)%nvid(2) ), clinfo) |
---|
424 | CALL iom_nf90_check(NF90_DEF_VAR( if90id, TRIM(cltmp(3)), NF90_FLOAT , (/ 3 /), iom_file(kiomid)%nvid(3) ), clinfo) |
---|
425 | CALL iom_nf90_check(NF90_DEF_VAR( if90id, TRIM(cltmp(4)), NF90_DOUBLE, (/ 4 /), iom_file(kiomid)%nvid(4) ), clinfo) |
---|
426 | ! update informations structure related the dimension variable we just added... |
---|
427 | iom_file(kiomid)%nvars = 4 |
---|
428 | iom_file(kiomid)%luld(1:4) = (/ .FALSE., .FALSE., .FALSE., .TRUE. /) |
---|
429 | iom_file(kiomid)%cn_var(1:4) = cltmp |
---|
430 | iom_file(kiomid)%ndims(1:4) = (/ 2, 2, 1, 1 /) |
---|
431 | ! trick: defined to 0 to say that dimension variables are defined but not yet written |
---|
432 | iom_file(kiomid)%dimsz(1, 1) = 0 |
---|
433 | IF(lwp) WRITE(numout,*) TRIM(clinfo)//' define dimension variables done' |
---|
434 | ENDIF |
---|
435 | ! define the data if it is not already done |
---|
436 | ! =============== |
---|
437 | IF( kvid <= 0 ) THEN |
---|
438 | ! |
---|
439 | ! NetCDF4 chunking and compression fixed settings |
---|
440 | ichunkalg = 0 |
---|
441 | ishuffle = 1 |
---|
442 | ideflate = 1 |
---|
443 | ideflate_level = 1 |
---|
444 | ! |
---|
445 | idvar = iom_file(kiomid)%nvars + 1 |
---|
446 | ! are we in define mode? |
---|
447 | IF( iom_file(kiomid)%irec /= -1 ) THEN ! trick: irec used to know if the file is in define mode or not |
---|
448 | CALL iom_nf90_check(NF90_REDEF( if90id ), clinfo) ; iom_file(kiomid)%irec = -1 |
---|
449 | ENDIF |
---|
450 | ! variable definition |
---|
451 | IF( PRESENT(pv_r0d) ) THEN ; idims = 0 |
---|
452 | ELSEIF( PRESENT(pv_r1d) ) THEN ; idims = 2 ; idimid(1:idims) = (/ 3,4/) |
---|
453 | ELSEIF( PRESENT(pv_r2d) ) THEN ; idims = 3 ; idimid(1:idims) = (/1,2 ,4/) |
---|
454 | ELSEIF( PRESENT(pv_r3d) ) THEN ; idims = 4 ; idimid(1:idims) = (/1,2,3,4/) |
---|
455 | ENDIF |
---|
456 | IF( PRESENT(ktype) ) THEN ! variable external type |
---|
457 | SELECT CASE (ktype) |
---|
458 | CASE (jp_r8) ; itype = NF90_DOUBLE |
---|
459 | CASE (jp_r4) ; itype = NF90_FLOAT |
---|
460 | CASE (jp_i4) ; itype = NF90_INT |
---|
461 | CASE (jp_i2) ; itype = NF90_SHORT |
---|
462 | CASE (jp_i1) ; itype = NF90_BYTE |
---|
463 | CASE DEFAULT ; CALL ctl_stop( TRIM(clinfo)//' unknown variable type' ) |
---|
464 | END SELECT |
---|
465 | ELSE |
---|
466 | itype = NF90_DOUBLE |
---|
467 | ENDIF |
---|
468 | IF( PRESENT(pv_r0d) ) THEN |
---|
469 | CALL iom_nf90_check(NF90_DEF_VAR( if90id, TRIM(cdvar), itype, & |
---|
470 | & iom_file(kiomid)%nvid(idvar) ), clinfo) |
---|
471 | ELSE |
---|
472 | CALL iom_nf90_check(NF90_DEF_VAR( if90id, TRIM(cdvar), itype, idimid(1:idims), & |
---|
473 | & iom_file(kiomid)%nvid(idvar) ), clinfo) |
---|
474 | ENDIF |
---|
475 | lchunk = .false. |
---|
476 | IF( snc4set%luse .AND. idims.eq.4 ) lchunk = .true. |
---|
477 | ! update informations structure related the new variable we want to add... |
---|
478 | iom_file(kiomid)%nvars = idvar |
---|
479 | iom_file(kiomid)%cn_var(idvar) = TRIM(cdvar) |
---|
480 | iom_file(kiomid)%scf(idvar) = 1. |
---|
481 | iom_file(kiomid)%ofs(idvar) = 0. |
---|
482 | iom_file(kiomid)%ndims(idvar) = idims |
---|
483 | IF( .NOT. PRESENT(pv_r0d) ) THEN ; iom_file(kiomid)%luld(idvar) = .TRUE. |
---|
484 | ELSE ; iom_file(kiomid)%luld(idvar) = .FALSE. |
---|
485 | ENDIF |
---|
486 | DO jd = 1, idims |
---|
487 | CALL iom_nf90_check(NF90_INQUIRE_DIMENSION( if90id, idimid(jd), len = iom_file(kiomid)%dimsz(jd,idvar) ), clinfo) |
---|
488 | IF ( lchunk ) ichunksz(jd) = iom_file(kiomid)%dimsz(jd,idvar) |
---|
489 | END DO |
---|
490 | IF ( lchunk ) THEN |
---|
491 | ! Calculate chunk sizes by partitioning each dimension as requested in namnc4 namelist |
---|
492 | ! Disallow very small chunk sizes and prevent chunk sizes larger than each individual dimension |
---|
493 | ichunksz(1) = MIN( ichunksz(1),MAX( (ichunksz(1)-1)/snc4set%ni + 1 ,16 ) ) ! Suggested default nc4set%ni=4 |
---|
494 | ichunksz(2) = MIN( ichunksz(2),MAX( (ichunksz(2)-1)/snc4set%nj + 1 ,16 ) ) ! Suggested default nc4set%nj=2 |
---|
495 | ichunksz(3) = MIN( ichunksz(3),MAX( (ichunksz(3)-1)/snc4set%nk + 1 , 1 ) ) ! Suggested default nc4set%nk=6 |
---|
496 | ichunksz(4) = 1 ! Do not allow chunks to span the |
---|
497 | ! unlimited dimension |
---|
498 | CALL iom_nf90_check(SET_NF90_DEF_VAR_CHUNKING(if90id, idvar, ichunkalg, ichunksz), clinfo) |
---|
499 | CALL iom_nf90_check(SET_NF90_DEF_VAR_DEFLATE(if90id, idvar, ishuffle, ideflate, ideflate_level), clinfo) |
---|
500 | IF(lwp) WRITE(numout,*) TRIM(clinfo)//' chunked ok. Chunks sizes: ', ichunksz |
---|
501 | ENDIF |
---|
502 | IF(lwp) WRITE(numout,*) TRIM(clinfo)//' defined ok' |
---|
503 | ELSE |
---|
504 | idvar = kvid |
---|
505 | ENDIF |
---|
506 | |
---|
507 | ! time step kwrite : write the variable |
---|
508 | IF( kt == kwrite ) THEN |
---|
509 | ! are we in write mode? |
---|
510 | IF( iom_file(kiomid)%irec == -1 ) THEN ! trick: irec used to know if the file is in define mode or not |
---|
511 | CALL iom_nf90_check(NF90_ENDDEF( if90id ), clinfo) ; iom_file(kiomid)%irec = 0 |
---|
512 | ENDIF |
---|
513 | ! on what kind of domain must the data be written? |
---|
514 | IF( PRESENT(pv_r2d) .OR. PRESENT(pv_r3d) ) THEN |
---|
515 | idimsz(1:2) = iom_file(kiomid)%dimsz(1:2,idvar) |
---|
516 | IF( idimsz(1) == (nlei - nldi + 1) .AND. idimsz(2) == (nlej - nldj + 1) ) THEN |
---|
517 | ix1 = nldi ; ix2 = nlei ; iy1 = nldj ; iy2 = nlej |
---|
518 | ELSEIF( idimsz(1) == nlci .AND. idimsz(2) == nlcj ) THEN |
---|
519 | ix1 = 1 ; ix2 = nlci ; iy1 = 1 ; iy2 = nlcj |
---|
520 | ELSEIF( idimsz(1) == jpi .AND. idimsz(2) == jpj ) THEN |
---|
521 | ix1 = 1 ; ix2 = jpi ; iy1 = 1 ; iy2 = jpj |
---|
522 | ELSE |
---|
523 | CALL ctl_stop( 'iom_nf90_rp0123d: should have been an impossible case...' ) |
---|
524 | ENDIF |
---|
525 | |
---|
526 | ! write dimension variables if it is not already done |
---|
527 | ! ============= |
---|
528 | ! trick: is defined to 0 => dimension variable are defined but not yet written |
---|
529 | IF( iom_file(kiomid)%dimsz(1, 1) == 0 ) THEN |
---|
530 | CALL iom_nf90_check(NF90_INQ_VARID( if90id, 'nav_lon' , idmy ), clinfo) |
---|
531 | CALL iom_nf90_check(NF90_PUT_VAR( if90id, idmy, glamt(ix1:ix2, iy1:iy2) ), clinfo) |
---|
532 | CALL iom_nf90_check(NF90_INQ_VARID( if90id, 'nav_lat' , idmy ), clinfo) |
---|
533 | CALL iom_nf90_check(NF90_PUT_VAR( if90id, idmy, gphit(ix1:ix2, iy1:iy2) ), clinfo) |
---|
534 | CALL iom_nf90_check(NF90_INQ_VARID( if90id, 'nav_lev' , idmy ), clinfo) |
---|
535 | CALL iom_nf90_check(NF90_PUT_VAR( if90id, idmy, gdept_1d ), clinfo) |
---|
536 | ! +++ WRONG VALUE: to be improved but not really useful... |
---|
537 | CALL iom_nf90_check(NF90_INQ_VARID( if90id, 'time_counter', idmy ), clinfo) |
---|
538 | CALL iom_nf90_check(NF90_PUT_VAR( if90id, idmy, kt ), clinfo) |
---|
539 | ! update the values of the variables dimensions size |
---|
540 | CALL iom_nf90_check(NF90_INQUIRE_DIMENSION( if90id, 1, len = iom_file(kiomid)%dimsz(1,1) ), clinfo) |
---|
541 | CALL iom_nf90_check(NF90_INQUIRE_DIMENSION( if90id, 2, len = iom_file(kiomid)%dimsz(2,1) ), clinfo) |
---|
542 | iom_file(kiomid)%dimsz(1:2, 2) = iom_file(kiomid)%dimsz(1:2, 1) |
---|
543 | CALL iom_nf90_check(NF90_INQUIRE_DIMENSION( if90id, 3, len = iom_file(kiomid)%dimsz(1,3) ), clinfo) |
---|
544 | iom_file(kiomid)%dimsz(1 , 4) = 1 ! unlimited dimension |
---|
545 | IF(lwp) WRITE(numout,*) TRIM(clinfo)//' write dimension variables done' |
---|
546 | ENDIF |
---|
547 | ENDIF |
---|
548 | |
---|
549 | ! write the data |
---|
550 | ! ============= |
---|
551 | IF( PRESENT(pv_r0d) ) THEN |
---|
552 | CALL iom_nf90_check(NF90_PUT_VAR( if90id, idvar, pv_r0d ), clinfo) |
---|
553 | ELSEIF( PRESENT(pv_r1d) ) THEN |
---|
554 | CALL iom_nf90_check(NF90_PUT_VAR( if90id, idvar, pv_r1d( :) ), clinfo) |
---|
555 | ELSEIF( PRESENT(pv_r2d) ) THEN |
---|
556 | CALL iom_nf90_check(NF90_PUT_VAR( if90id, idvar, pv_r2d(ix1:ix2, iy1:iy2 ) ), clinfo) |
---|
557 | ELSEIF( PRESENT(pv_r3d) ) THEN |
---|
558 | CALL iom_nf90_check(NF90_PUT_VAR( if90id, idvar, pv_r3d(ix1:ix2, iy1:iy2, :) ), clinfo) |
---|
559 | ENDIF |
---|
560 | ! add 1 to the size of the temporal dimension (not really useful...) |
---|
561 | IF( iom_file(kiomid)%luld(idvar) ) iom_file(kiomid)%dimsz(iom_file(kiomid)%ndims(idvar), idvar) & |
---|
562 | & = iom_file(kiomid)%dimsz(iom_file(kiomid)%ndims(idvar), idvar) + 1 |
---|
563 | IF(lwp) WRITE(numout,*) TRIM(clinfo)//' written ok' |
---|
564 | ENDIF |
---|
565 | ! |
---|
566 | END SUBROUTINE iom_nf90_rp0123d |
---|
567 | |
---|
568 | |
---|
569 | SUBROUTINE iom_nf90_check( kstatus, cdinfo ) |
---|
570 | !!-------------------------------------------------------------------- |
---|
571 | !! *** SUBROUTINE iom_nf90_check *** |
---|
572 | !! |
---|
573 | !! ** Purpose : check nf90 errors |
---|
574 | !!-------------------------------------------------------------------- |
---|
575 | INTEGER, INTENT(in) :: kstatus |
---|
576 | CHARACTER(LEN=*), INTENT(in) :: cdinfo |
---|
577 | !--------------------------------------------------------------------- |
---|
578 | IF(kstatus /= nf90_noerr) CALL ctl_stop( 'iom_nf90_check : '//TRIM(nf90_strerror(kstatus)), TRIM(cdinfo) ) |
---|
579 | END SUBROUTINE iom_nf90_check |
---|
580 | |
---|
581 | !!====================================================================== |
---|
582 | END MODULE iom_nf90 |
---|