ROMS
Loading...
Searching...
No Matches
close_io.F
Go to the documentation of this file.
1#include "cppdefs.h"
3!
4!git $Id$
5!================================================== Hernan G. Arango ===
6! Copyright (c) 2002-2025 The ROMS Group !
7! Licensed under a MIT/X style license !
8! See License_ROMS.md !
9!=======================================================================
10! !
11! This module closes input and output files using either the standard !
12! NetCDF library or the Parallel-IO (PIO) library. !
13! !
14! During initialization, the input input files need to be in closed !
15! state to facilitate multi-file processing. This is important in !
16! iterative algorithms that run the model kernels repetitevely. !
17! !
18!=======================================================================
19!
20 USE mod_param
21 USE mod_parallel
22 USE mod_iounits
23 USE mod_ncparam
24 USE mod_netcdf
25#if defined PIO_LIB && defined DISTRIBUTE
27#endif
28 USE mod_scalars
29!
30 USE dateclock_mod, ONLY : get_date
31 USE strings_mod, ONLY : founderror
32!
33 implicit none
34!
35 PUBLIC :: close_file
36 PUBLIC :: close_inp
37 PUBLIC :: close_out
38!
39 CONTAINS
40!
41!***********************************************************************
42 SUBROUTINE close_file (ng, model, S, ncname, Lupdate)
43!***********************************************************************
44!
45! Imported variable declarations.
46!
47 integer, intent(in) :: ng, model
48!
49 TYPE(t_io), intent(inout) :: s
50!
51 logical, intent(in), optional :: lupdate
52!
53 character (len=*), intent(in), optional :: ncname
54!
55! Local variable declarations.
56!
57 integer :: closedstate = -1
58!
59 character (len=*), parameter :: myfile = &
60 & __FILE__//", close_file_nf90"
61!
62!-----------------------------------------------------------------------
63! Close specified NetCDF file.
64!-----------------------------------------------------------------------
65!
66 SELECT CASE (s%IOtype)
67 CASE (io_nf90)
68 IF (s%ncid.ne.closedstate) THEN
69 CALL netcdf_close (ng, model, s%ncid, &
70 & trim(ncname), lupdate)
71 s%ncid=closedstate
72 END IF
73
74#if defined PIO_LIB && defined DISTRIBUTE
75 CASE (io_pio)
76 IF (ASSOCIATED(s%pioFile%iosystem)) THEN
77 IF (s%pioFile%fh.ne.closedstate) THEN
78 CALL pio_netcdf_close (ng, model, s%pioFile, &
79 & trim(ncname), lupdate)
80 s%pioFile%fh=closedstate
81 END IF
82 END IF
83#endif
84 END SELECT
85 IF (founderror(exit_flag, noerror, __line__, myfile)) RETURN
86!
87 RETURN
88 END SUBROUTINE close_file
89!
90!***********************************************************************
91 SUBROUTINE close_inp (ng, model)
92!***********************************************************************
93!
94! Imported variable declarations.
95!
96 integer, intent(in) :: ng, model
97!
98! Local variable declarations.
99!
100 integer :: fcount, i
101!
102 character (len=*), parameter :: myfile = &
103 & __FILE__//", close_inp"
104!
105 sourcefile=myfile
106!
107!-----------------------------------------------------------------------
108! If multi-file input fields, close several input files.
109!-----------------------------------------------------------------------
110!
111! Skip if configuration error.
112!
113 IF ((exit_flag.eq.5).or.(exit_flag.eq.6)) RETURN
114
115#ifdef FRC_FILE
116!
117! If appropriate, close input forcing files and set several parameter
118! to closed state.
119!
120 DO i=1,nffiles(ng)
121 IF ((frc(i,ng)%Nfiles.gt.0).and.(frc(i,ng)%ncid.ne.-1)) THEN
122 fcount=frc(i,ng)%Fcount
123 CALL close_file (ng, model, frc(i,ng), &
124 & frc(i,ng)%files(fcount), .false.)
125 IF (founderror(exit_flag, noerror, __line__, myfile)) RETURN
126 frcids=-1
127 frcncid=-1
128 fcount=1
129 frc(i,ng)%Fcount=fcount
130 frc(i,ng)%name=trim(frc(i,ng)%files(fcount))
131 END IF
132 END DO
133#endif
134!
135! If appropriate, close boundary files.
136!
137 IF (obcdata(ng)) THEN
138 DO i=1,nbcfiles(ng)
139 IF ((bry(i,ng)%Nfiles.gt.0).and.(bry(i,ng)%ncid.ne.-1)) THEN
140 fcount=bry(i,ng)%Fcount
141 CALL close_file (ng, model, bry(i,ng), &
142 & bry(i,ng)%files(fcount), .false.)
143 IF (founderror(exit_flag, noerror, __line__, myfile)) RETURN
144 bryids=-1
145 bryncid=-1
146 fcount=1
147 bry(i,ng)%Fcount=fcount
148 bry(i,ng)%name=trim(bry(i,ng)%files(fcount))
149 END IF
150 END DO
151 END IF
152!
153! If appropriate, close climatology files.
154!
155 IF (clm_file(ng)) THEN
156 DO i=1,nclmfiles(ng)
157 IF ((clm(i,ng)%Nfiles.gt.0).and.(clm(i,ng)%ncid.ne.-1)) THEN
158 fcount=clm(i,ng)%Fcount
159 CALL close_file (ng, model, clm(i,ng), &
160 & clm(i,ng)%files(fcount), .false.)
161 IF (founderror(exit_flag, noerror, __line__, myfile)) RETURN
162 clmids=-1
163 clmncid=-1
164 fcount=1
165 clm(i,ng)%Fcount=fcount
166 clm(i,ng)%name=trim(clm(i,ng)%files(fcount))
167 END IF
168 END DO
169 END IF
170!
171 RETURN
172 END SUBROUTINE close_inp
173!
174 SUBROUTINE close_out
175!
176!=======================================================================
177! !
178! This subroutine flushes and closes all output files. !
179! !
180!=======================================================================
181!
182! Local variable declarations.
183!
184 logical :: first, lupdate
185!
186 integer :: fcount, myerror, i, ivalue, ng
187!
188 character (len=256) :: ana_string
189
190 character (len=*), parameter :: myfile = &
191 & __FILE__//", close_out"
192!
193 sourcefile=myfile
194!
195!-----------------------------------------------------------------------
196! Close output NetCDF files. Set file indices to closed state.
197!-----------------------------------------------------------------------
198!
199! Skip if configuration error.
200!
201 IF ((exit_flag.eq.5).or.(exit_flag.eq.6)) RETURN
202!
203! If appropriate, set switch for updating biology header file global
204! attribute in output NetCDF files.
205!
206#ifdef BIOLOGY
207 lupdate=.true.
208#else
209 lupdate=.false.
210#endif
211!
212! Close output NetCDF files.
213!
214 DO ng=1,ngrids
215 CALL close_file (ng, inlm, rst(ng), rst(ng)%name, lupdate)
216#if defined FOUR_DVAR || defined ENKF_RESTART || defined VERIFICATION
217 CALL close_file (ng, inlm, dai(ng), dai(ng)%name, lupdate)
218 CALL close_file (ng, inlm, dav(ng), dav(ng)%name, lupdate)
219#endif
220#if defined FORWARD_READ || defined FORWARD_WRITE
221 IF (fwd(ng)%IOtype.eq.io_nf90) THEN
222 IF ((fwd(ng)%ncid.ne.-1).and. &
223 & (fwd(ng)%ncid.eq.his(ng)%ncid)) THEN
224 fwd(ng)%ncid=-1
225 END IF
226# if defined PIO_LIB && defined DISTRIBUTE
227 ELSE IF (fwd(ng)%IOtype.eq.io_pio) THEN
228 IF ((fwd(ng)%pioFile%fh.ne.-1).and. &
229 & (fwd(ng)%pioFile%fh.eq.his(ng)%pioFile%fh)) THEN
230 fwd(ng)%pioFile%fh=-1
231 END IF
232# endif
233 END IF
234 CALL close_file (ng, inlm, fwd(ng), fwd(ng)%name, lupdate)
235#endif
236 CALL close_file (ng, inlm, his(ng), his(ng)%name, lupdate)
237 CALL close_file (ng, inlm, qck(ng), qck(ng)%name, lupdate)
238#ifdef SP4DVAR
239 CALL close_file (ng, itlm, spt(ng), spt(ng)%name, lupdate)
240 CALL close_file (ng, itlm, sct(ng), sct(ng)%name, lupdate)
241 CALL close_file (ng, iadm, spa(ng), spa(ng)%name, lupdate)
242#endif
243#ifdef ADJOINT
244 CALL close_file (ng, iadm, adm(ng), adm(ng)%name, lupdate)
245#endif
246#ifdef TANGENT
247 CALL close_file (ng, itlm, itl(ng), itl(ng)%name, lupdate)
248 CALL close_file (ng, itlm, tlm(ng), tlm(ng)%name, lupdate)
249#endif
250#if defined TL_IOMS && defined FOUR_DVAR
251 CALL close_file (ng, irpm, irp(ng), irp(ng)%name, lupdate)
252#endif
253#ifdef WEAK_CONSTRAINT
254 CALL close_file (ng, itlm, tlf(ng), tlf(ng)%name, lupdate)
255#endif
256#ifdef FOUR_DVAR
257 CALL close_file (ng, iadm, hss(ng), hss(ng)%name, lupdate)
258 CALL close_file (ng, iadm, lcz(ng), lcz(ng)%name, lupdate)
259#endif
260#if defined AVERAGES || \
261 (defined ad_averages && defined adjoint) || \
262 (defined rp_averages && defined tl_ioms) || \
263 (defined tl_averages && defined tangent)
264 CALL close_file (ng, inlm, avg(ng), avg(ng)%name, lupdate)
265#endif
266#if defined AVERAGES && defined AVERAGES_DETIDE && \
267 (defined ssh_tides || defined uv_tides)
268 CALL close_file (ng, inlm, har(ng), har(ng)%name, lupdate)
269#endif
270#ifdef DIAGNOSTICS
271 CALL close_file (ng, inlm, dia(ng), dia(ng)%name, lupdate)
272#endif
273#ifdef FLOATS
274 CALL close_file (ng, inlm, flt(ng), flt(ng)%name, lupdate)
275#endif
276#ifdef STATIONS
277 CALL close_file (ng, inlm, sta(ng), sta(ng)%name, lupdate)
278#endif
279#if defined WEAK_CONSTRAINT && \
280 (defined posterior_error_f || defined posterior_error_i)
281 CALL close_file (ng, itlm, err(ng), err(ng)%name, lupdate)
282#endif
283!
284! Report number of time records written.
285!
286 IF (master) THEN
287 WRITE (stdout,10) ng
288
289 IF (associated(his(ng)%Nrec)) THEN
290 IF (any(his(ng)%Nrec.gt.0)) THEN
291 WRITE (stdout,20) 'HISTORY', sum(his(ng)%Nrec)
292 END IF
293 END IF
294
295 IF (associated(rst(ng)%Nrec)) THEN
296 fcount=rst(ng)%load
297 IF (rst(ng)%Nrec(fcount).gt.0) THEN
298 IF (lcyclerst(ng)) THEN
299 IF (rst(ng)%Nrec(fcount).gt.1) THEN
300 rst(ng)%Nrec(fcount)=2
301 ELSE
302 rst(ng)%Nrec(fcount)=1
303 END IF
304 END IF
305 WRITE (stdout,20) 'RESTART', rst(ng)%Nrec(fcount)
306 END IF
307 END IF
308
309#if defined FOUR_DVAR || defined ENKF_RESTART
310 IF (associated(dai(ng)%Nrec)) THEN
311 IF (any(dai(ng)%Nrec.gt.0)) THEN
312 WRITE (stdout,20) 'DA IC ', sum(dai(ng)%Nrec)
313 END IF
314 END IF
315#endif
316
317#ifdef ADJOINT
318 IF (associated(adm(ng)%Nrec)) THEN
319 IF (any(adm(ng)%Nrec.gt.0)) THEN
320 WRITE (stdout,20) 'ADJOINT', sum(adm(ng)%Nrec)
321 END IF
322 END IF
323#endif
324
325#ifdef TANGENT
326 IF (associated(tlm(ng)%Nrec)) THEN
327 IF (any(tlm(ng)%Nrec.gt.0)) THEN
328 WRITE (stdout,20) 'TANGENT', sum(tlm(ng)%Nrec)
329 END IF
330 END IF
331#endif
332
333#if defined AVERAGES || \
334 (defined ad_averages && defined adjoint) || \
335 (defined rp_averages && defined tl_ioms) || \
336 (defined tl_averages && defined tangent)
337 IF (associated(avg(ng)%Nrec)) THEN
338 IF (any(avg(ng)%Nrec.gt.0)) THEN
339 WRITE (stdout,20) 'AVERAGE', sum(avg(ng)%Nrec)
340 END IF
341 END IF
342#endif
343
344#ifdef STATIONS
345 IF (associated(sta(ng)%Nrec)) THEN
346 IF (any(sta(ng)%Nrec.gt.0)) THEN
347 WRITE (stdout,20) 'STATION', sum(sta(ng)%Nrec)
348 END IF
349 END IF
350#endif
351
352#if defined WEAK_CONSTRAINT && \
353 (defined posterior_error_f || defined posterior_error_i)
354 IF (associated(err(ng)%Nrec)) THEN
355 IF (any(err(ng)%Nrec.gt.0)) THEN
356 WRITE (stdout,20) 'ERROR ', sum(err(ng)%Nrec)
357 END IF
358 END IF
359#endif
360 END IF
361 END DO
362!
363!-----------------------------------------------------------------------
364! Report analytical header files used.
365!-----------------------------------------------------------------------
366!
367 IF (master) THEN
368 first=.true.
369 DO i=1,39
370 ana_string=trim(ananame(i))
371 ivalue=ichar(ana_string(1:1))
372 IF (ivalue.ge.47) THEN ! decimal value for characters
373 IF (exit_flag.ne.5) THEN
374 IF (first) THEN
375 first=.false.
376 WRITE (stdout,30) ' Analytical header files used:'
377 END IF
378 WRITE (stdout,'(5x,a)') trim(adjustl(ananame(i)))
379 END IF
380 END IF
381 END DO
382 END IF
383
384#ifdef BIOLOGY
385!
386!-----------------------------------------------------------------------
387! Report biology model header files used.
388!-----------------------------------------------------------------------
389!
390 IF (master) THEN
391 first=.true.
392 DO i=1,4
393 ana_string=trim(bioname(i))
394 ivalue=ichar(ana_string(1:1))
395 IF (ivalue.ge.47) THEN ! decimal value for characters
396 IF (exit_flag.ne.5) THEN
397 IF (first) THEN
398 first=.false.
399 WRITE (stdout,30) ' Biology model header files used:'
400 END IF
401 WRITE (stdout,'(5x,a)') trim(adjustl(bioname(i)))
402 END IF
403 END IF
404 END DO
405 END IF
406#endif
407!
408!-----------------------------------------------------------------------
409! If applicable, report internal exit errors.
410!-----------------------------------------------------------------------
411!
412 IF (master.and. &
413 & (founderror(exit_flag, noerror, __line__, myfile))) THEN
414 WRITE (stdout,40) rerror(exit_flag), exit_flag
415 END IF
416 IF (blowup.ne.0) THEN
417 IF (master) WRITE (stdout,50) trim(blowup_string)
418 ELSE IF (exit_flag.eq.noerror) THEN
419 CALL get_date (date_str)
420 IF (master) WRITE (stdout,60) trim(date_str)
421 ELSE IF (exit_flag.eq.2) THEN
422 IF (master) WRITE (stdout,70) nf90_strerror(ioerror)
423 ELSE IF (exit_flag.eq.3) THEN
424 IF (master) WRITE (stdout,80) nf90_strerror(ioerror)
425 ELSE IF (exit_flag.eq.4) THEN
426 IF (master) WRITE (stdout,90)
427 ELSE IF (exit_flag.eq.5) THEN
428 IF (master) WRITE (stdout,100)
429 ELSE IF (exit_flag.eq.6) THEN
430 IF (master) WRITE (stdout,110)
431 ELSE IF (exit_flag.eq.7) THEN
432 IF (master) WRITE (stdout,120)
433 ELSE IF (exit_flag.eq.8) THEN
434 IF (master) WRITE (stdout,130)
435 END IF
436
437#ifdef ROMS_STDOUT
438!
439!-----------------------------------------------------------------------
440! Close ROMS standard outpu file.
441!-----------------------------------------------------------------------
442!
443 FLUSH (stdout)
444 CLOSE (stdout)
445#endif
446!
447 10 FORMAT (/,' ROMS - Output NetCDF summary for Grid ', &
448 & i2.2,':')
449 20 FORMAT (13x,'number of time records written in ', &
450 & a,' file = ',i0)
451 30 FORMAT (/,a,/)
452 40 FORMAT (/,a,i0,/)
453 50 FORMAT (/,' MAIN: Abnormal termination: BLOWUP.',/, &
454 & ' REASON: ',a)
455 60 FORMAT (/,' ROMS: DONE... ',a)
456 70 FORMAT (/,' ERROR: Abnormal termination: NetCDF INPUT.',/, &
457 & ' REASON: ',a)
458 80 FORMAT (/,' ERROR: Abnormal termination: NetCDF OUTPUT.',/, &
459 & ' REASON: ',a)
460 90 FORMAT (/,' ERROR: I/O related problem.')
461100 FORMAT (/,' ERROR: Illegal model configuration.')
462110 FORMAT (/,' ERROR: Illegal domain partition.')
463120 FORMAT (/,' ERROR: Illegal input parameter.')
464130 FORMAT (/,' ERROR: Fatal algorithm result.')
465!
466 RETURN
467 END SUBROUTINE close_out
468
469 END MODULE close_io_mod
subroutine, public close_out
Definition close_io.F:175
subroutine, public close_file(ng, model, s, ncname, lupdate)
Definition close_io.F:43
subroutine, public close_inp(ng, model)
Definition close_io.F:92
subroutine, public get_date(date_str)
Definition dateclock.F:856
type(t_io), dimension(:), allocatable lcz
type(t_io), dimension(:), allocatable spa
character(len=50), dimension(9) rerror
integer ioerror
type(t_io), dimension(:), allocatable his
type(t_io), dimension(:), allocatable adm
type(t_io), dimension(:), allocatable spt
type(t_io), dimension(:), allocatable irp
integer, dimension(:,:), allocatable clmids
type(t_io), dimension(:), allocatable flt
type(t_io), dimension(:), allocatable sct
type(t_io), dimension(:), allocatable tlf
type(t_io), dimension(:), allocatable hss
integer, dimension(:), allocatable nclmfiles
type(t_io), dimension(:), allocatable har
type(t_io), dimension(:), allocatable tlm
type(t_io), dimension(:), allocatable itl
type(t_io), dimension(:,:), allocatable frc
type(t_io), dimension(:), allocatable dai
integer, dimension(:,:), allocatable bryids
type(t_io), dimension(:), allocatable dav
type(t_io), dimension(:), allocatable qck
type(t_io), dimension(:,:), allocatable bry
type(t_io), dimension(:), allocatable fwd
type(t_io), dimension(:), allocatable rst
integer, dimension(:), allocatable nbcfiles
type(t_io), dimension(:,:), allocatable clm
integer, dimension(:,:), allocatable frcids
type(t_io), dimension(:), allocatable avg
type(t_io), dimension(:), allocatable sta
type(t_io), dimension(:), allocatable err
integer stdout
character(len=256) sourcefile
type(t_io), dimension(:), allocatable dia
integer, dimension(:), allocatable nffiles
integer, parameter io_nf90
Definition mod_ncparam.F:95
integer, dimension(:,:), allocatable clmncid
integer, parameter io_pio
Definition mod_ncparam.F:96
character(len=44) date_str
integer, dimension(:,:), allocatable frcncid
integer, dimension(:,:), allocatable bryncid
character(len=256), dimension(39) ananame
subroutine, public netcdf_close(ng, model, ncid, ncname, lupdate)
logical master
integer, parameter inlm
Definition mod_param.F:662
integer, parameter irpm
Definition mod_param.F:664
integer, parameter iadm
Definition mod_param.F:665
integer ngrids
Definition mod_param.F:113
integer, parameter itlm
Definition mod_param.F:663
subroutine, public pio_netcdf_close(ng, model, piofile, ncname, lupdate)
logical, dimension(:), allocatable clm_file
integer blowup
logical, dimension(:), allocatable obcdata
integer exit_flag
character(len=80) blowup_string
integer noerror
logical, dimension(:), allocatable lcyclerst
logical function, public founderror(flag, noerr, line, routine)
Definition strings.F:52