ROMS
Loading...
Searching...
No Matches
edit_multifile.F
Go to the documentation of this file.
1#include "cppdefs.h"
2 SUBROUTINE edit_multifile (task)
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! Edits and updates derived type structure TYPE(T_IO) for the I/O !
12! manipulation in some algorithms. !
13! !
14! For example, the forward trajectory files can be split into several !
15! multifiles to avoid creating large files in 4D-Var. !
16! !
17! Notice the base filename is not modified to preserve the root value !
18! specified by the user. !
19! !
20!=======================================================================
21!
22 USE mod_param
23 USE mod_iounits
24 USE mod_ncparam
25 USE mod_scalars
26!
27 USE close_io_mod, ONLY : close_file
28!
29! Imported variable declarations.
30!
31 character (len=*) :: task
32!
33! Local variable declarations.
34!
35 integer :: Iunder, ifile, lstr, ng
36 integer :: Nfiles
37!
38 character (len=*), parameter :: MyFile = &
39 & __FILE__
40!
41 sourcefile=myfile
42!
43!-----------------------------------------------------------------------
44! Edit and update multi-file structure acconding to requested task.
45!-----------------------------------------------------------------------
46!
47! If multiple history files, close the last one created by the
48! nonlinear model to avoid exceeding the number of allowed opened
49! files in UNIX. Recall that the 4D-Var algorithms will open and
50! close the forward multi-file many times in the TLM and ADM in
51! the inner loops.
52!
53 DO ng=1,ngrids
54!
55 SELECT CASE (trim(adjustl(task)))
56!
57! Load HIS information into the FWD structure so it can be used to
58! process the NLM background trajectory by the ADM and TLM kernels.
59!
60 CASE ('HIS2FWD')
61 fwd(ng)%IOtype=his(ng)%IOtype
62 IF (ndefhis(ng).gt.0) THEN
63 CALL close_file (ng, inlm, his(ng), his(ng)%name)
64 nfiles=ntimes(ng)/ndefhis(ng)
65 IF (nhis(ng).eq.ndefhis(ng)) nfiles=nfiles+1
66 CALL edit_file_struct (ng, nfiles, fwd)
67 DO ifile=1,nfiles
68 fwd(ng)%files(ifile)=trim(his(ng)%files(ifile))
69 END DO
70 fwd(ng)%name=trim(fwd(ng)%files(1))
71 ELSE
72 IF (fwd(ng)%IOtype.eq.io_nf90) THEN
73 fwd(ng)%ncid=his(ng)%ncid
74#if defined PIO_LIB && defined DISTRIBUTE
75 ELSE IF (fwd(ng)%IOtype.eq.io_pio) THEN
76 fwd(ng)%pioFile=his(ng)%pioFile
77#endif
78 END IF
79 fwd(ng)%name=trim(his(ng)%name)
80 fwd(ng)%files(1)=trim(his(ng)%name)
81 END IF
82!
83! Load HIS information into the BLK structure so it can be used to
84! process the NLM background surface forcing to be read and processd
85! by the ADM and TLM kernels.
86!
87 CASE ('HIS2BLK')
88 blk(ng)%IOtype=his(ng)%IOtype
89 IF (ndefhis(ng).gt.0) THEN
90 CALL close_file (ng, inlm, his(ng), his(ng)%name)
91 nfiles=ntimes(ng)/ndefhis(ng)
92 IF (nhis(ng).eq.ndefhis(ng)) nfiles=nfiles+1
93 CALL edit_file_struct (ng, nfiles, blk)
94 DO ifile=1,nfiles
95 blk(ng)%files(ifile)=trim(his(ng)%files(ifile))
96 END DO
97 blk(ng)%name=trim(blk(ng)%files(1))
98 ELSE
99 IF (blk(ng)%IOtype.eq.io_nf90) THEN
100 blk(ng)%ncid=-1
101#if defined PIO_LIB && defined DISTRIBUTE
102 ELSE IF (blk(ng)%IOtype.eq.io_pio) THEN
103 blk(ng)%pioFile%fh=-1
104#endif
105 END IF
106 blk(ng)%name=trim(his(ng)%name)
107 blk(ng)%files(1)=trim(his(ng)%name)
108 END IF
109!
110! Load QCK information into the BLK structure so it can be used to
111! process the NLM background surface forcing to be read and processed
112! by the TLM, RPM, and ADM kernels.
113!
114 CASE ('QCK2BLK')
115 blk(ng)%IOtype=qck(ng)%IOtype
116 IF (ndefqck(ng).gt.0) THEN
117 CALL close_file (ng, inlm, qck(ng), qck(ng)%name)
118 nfiles=ntimes(ng)/ndefqck(ng)
119 IF (nqck(ng).eq.ndefqck(ng)) nfiles=nfiles+1
120 CALL edit_file_struct (ng, nfiles, blk)
121 DO ifile=1,nfiles
122 blk(ng)%files(ifile)=trim(qck(ng)%files(ifile))
123 END DO
124 blk(ng)%name=trim(blk(ng)%files(1))
125 ELSE
126 IF (blk(ng)%IOtype.eq.io_nf90) THEN
127 blk(ng)%ncid=-1
128#if defined PIO_LIB && defined DISTRIBUTE
129 ELSE IF (blk(ng)%IOtype.eq.io_pio) THEN
130 blk(ng)%pioFile%fh=-1
131#endif
132 END IF
133 blk(ng)%name=trim(qck(ng)%name)
134 blk(ng)%files(1)=trim(qck(ng)%name)
135 END IF
136!
137! Load FWD information into the BLK structure so it can be used to
138! process the NLM background surface forcing to be read and processd
139! by the ADM and TLM kernels.
140!
141 CASE ('FWD2BLK')
142 blk(ng)%IOtype=fwd(ng)%IOtype
143 nfiles=fwd(ng)%Nfiles
144 IF (nfiles.gt.1) THEN
145 CALL close_file (ng, inlm, blk(ng), blk(ng)%name)
146 CALL edit_file_struct (ng, nfiles, blk)
147 DO ifile=1,nfiles
148 blk(ng)%files(ifile)=trim(fwd(ng)%files(ifile))
149 END DO
150 blk(ng)%name=trim(blk(ng)%files(1))
151 ELSE
152 IF (blk(ng)%IOtype.eq.io_nf90) THEN
153 blk(ng)%ncid=-1
154#if defined PIO_LIB && defined DISTRIBUTE
155 ELSE IF (blk(ng)%IOtype.eq.io_pio) THEN
156 blk(ng)%pioFile%fh=-1
157#endif
158 END IF
159 blk(ng)%name=trim(fwd(ng)%name)
160 blk(ng)%files(1)=trim(fwd(ng)%name)
161 END IF
162
163#ifdef RBL4DVAR_FCT_SENSITIVITY
164!
165! Save FWD information into the HIS structure so it can be used to
166! process the NLM background trajectory by the ADM and TLM kernels.
167! If multi-file, FWD(ng)%head and FWD(ng)%base is overwritten to
168! default values. The initialized values in "load_s1d" are incorrect
169! because of specified input filenames are already split.
170!
171 CASE ('FWD2HIS')
172 his(ng)%IOtype=fwd(ng)%IOtype
173 nfiles=fwd(ng)%Nfiles
174 IF (nfiles.gt.1) THEN
175 CALL close_file (ng, inlm, his(ng), his(ng)%name)
176 CALL edit_file_struct (ng, nfiles, his)
177 DO ifile=1,nfiles
178 his(ng)%files(ifile)=trim(fwd(ng)%files(ifile))
179 END DO
180 his(ng)%name=trim(his(ng)%files(1))
181 istring=index(his(ng)%name,'_outer',back=.false.)
182 IF (istring.gt.0) THEN ! outer loop prefix
183 lstr=istring-1
184 his(ng)%head=trim(adjustl(his(ng)%name(1:lstr)))
185 fwd(ng)%head=trim(adjustl(his(ng)%name(1:lstr)))
186 END IF
187 istring=index(his(ng)%name,char(95),back=.true.)
188 IF (istring.gt.0) THEN ! first underscode backwards
189 lstr=istring-1
190 his(ng)%base=trim(adjustl(his(ng)%name(1:lstr)))
191 fwd(ng)%base=trim(adjustl(his(ng)%name(1:lstr)))
192 END IF
193 ELSE
194 IF (his(ng)%IOtype.eq.io_nf90) THEN
195 his(ng)%ncid=fwd(ng)%ncid
196#if defined PIO_LIB && defined DISTRIBUTE
197 ELSE IF (his(ng)%IOtype.eq.io_pio) THEN
198 his(ng)%pioFile=fwd(ng)%pioFile
199#endif
200 END IF
201 his(ng)%files(1)=trim(fwd(ng)%files(1))
202 his(ng)%name=trim(his(ng)%files(1))
203 istring=index(his(ng)%name,'_outer',back=.false.)
204 IF (istring.gt.0) THEN ! outer loop prefix
205 lstr=istring-1
206 his(ng)%head=trim(adjustl(his(ng)%name(1:lstr)))
207 his(ng)%base=trim(adjustl(his(ng)%name(1:lstr)))
208 END IF
209 END IF
210!
211! Load FCTA information into the FWD structure so it can be used to
212! process the NLM background trajectory by the ADM and TLM kernels.
213!
214 CASE ('FCTA2FWD')
215 fwd(ng)%IOtype=fcta(ng)%IOtype
216 nfiles=fcta(ng)%Nfiles
217 IF (nfiles.gt.1) THEN
218 CALL close_file (ng, inlm, fcta(ng), fcta(ng)%name)
219 CALL edit_file_struct (ng, nfiles, fwd)
220 DO ifile=1,nfiles
221 fwd(ng)%files(ifile)=trim(fcta(ng)%files(ifile))
222 END DO
223 fwd(ng)%name=trim(fwd(ng)%files(1))
224 ELSE
225 IF (fwd(ng)%IOtype.eq.io_nf90) THEN
226 fwd(ng)%ncid=fcta(ng)%ncid
227#if defined PIO_LIB && defined DISTRIBUTE
228 ELSE IF (fwd(ng)%IOtype.eq.io_pio) THEN
229 fwd(ng)%pioFile=fcta(ng)%pioFile
230#endif
231 END IF
232 fwd(ng)%name=trim(fcta(ng)%name)
233 fwd(ng)%files(1)=trim(fcta(ng)%name)
234 END IF
235!
236! Load FCTA information into the BLK structure so it can be used to
237! process the NLM background surface forcing to be read and processd
238! by the ADM and TLM kernels.
239!
240 CASE ('FCTA2BLK')
241 blk(ng)%IOtype=fcta(ng)%IOtype
242 nfiles=fcta(ng)%Nfiles
243 IF (nfiles.gt.1) THEN
244 CALL close_file (ng, inlm, fcta(ng), fcta(ng)%name)
245 CALL edit_file_struct (ng, nfiles, blk)
246 DO ifile=1,nfiles
247 blk(ng)%files(ifile)=trim(fcta(ng)%files(ifile))
248 END DO
249 blk(ng)%name=trim(blk(ng)%files(1))
250 ELSE
251 IF (blk(ng)%IOtype.eq.io_nf90) THEN
252 blk(ng)%ncid=-1
253#if defined PIO_LIB && defined DISTRIBUTE
254 ELSE IF (blk(ng)%IOtype.eq.io_pio) THEN
255 blk(ng)%pioFile%fh=-1
256#endif
257 END IF
258 blk(ng)%name=trim(fcta(ng)%name)
259 blk(ng)%files(1)=trim(fcta(ng)%name)
260 END IF
261!
262! Load FCTB information into the FWD structure so it can be used to
263! process the NLM background trajectory by the ADM and TLM kernels.
264!
265 CASE ('FCTB2FWD')
266 fwd(ng)%IOtype=fctb(ng)%IOtype
267 nfiles=fctb(ng)%Nfiles
268 IF (nfiles.gt.1) THEN
269 CALL close_file (ng, inlm, fctb(ng), fctb(ng)%name)
270 CALL edit_file_struct (ng, nfiles, fwd)
271 DO ifile=1,nfiles
272 fwd(ng)%files(ifile)=trim(fctb(ng)%files(ifile))
273 END DO
274 fwd(ng)%name=trim(fwd(ng)%files(1))
275 ELSE
276 IF (fwd(ng)%IOtype.eq.io_nf90) THEN
277 fwd(ng)%ncid=fctb(ng)%ncid
278#if defined PIO_LIB && defined DISTRIBUTE
279 ELSE IF (fwd(ng)%IOtype.eq.io_pio) THEN
280 fwd(ng)%pioFile=fctb(ng)%pioFile
281#endif
282 END IF
283 fwd(ng)%name=trim(fctb(ng)%name)
284 fwd(ng)%files(1)=trim(fctb(ng)%name)
285 END IF
286!
287! Load FCTB information into the BLK structure so it can be used to
288! process the NLM background surface forcing to be read and processd
289! by the ADM and TLM kernels.
290!
291 CASE ('FCTB2BLK')
292 blk(ng)%IOtype=fctb(ng)%IOtype
293 nfiles=fctb(ng)%Nfiles
294 IF (nfiles.gt.1) THEN
295 CALL close_file (ng, inlm, fctb(ng), fctb(ng)%name)
296 CALL edit_file_struct (ng, nfiles, blk)
297 DO ifile=1,nfiles
298 blk(ng)%files(ifile)=trim(fctb(ng)%files(ifile))
299 END DO
300 blk(ng)%name=trim(blk(ng)%files(1))
301 ELSE
302 IF (blk(ng)%IOtype.eq.io_nf90) THEN
303 blk(ng)%ncid=-1
304#if defined PIO_LIB && defined DISTRIBUTE
305 ELSE IF (blk(ng)%IOtype.eq.io_pio) THEN
306 blk(ng)%pioFile%fh=-1
307#endif
308 END IF
309 blk(ng)%name=trim(fctb(ng)%name)
310 blk(ng)%files(1)=trim(fctb(ng)%name)
311 END IF
312#endif
313!
314! Load TLM information into the FWD structure so it can be used to
315! process the RPM background trajectory by the RPM, ADM and TLM
316! kernels. Used in R4D-Var.
317!
318 CASE ('TLM2FWD')
319 fwd(ng)%IOtype=tlm(ng)%IOtype
320 IF (ndeftlm(ng).gt.0) THEN
321 CALL close_file (ng, inlm, tlm(ng), tlm(ng)%name)
322 nfiles=ntimes(ng)/ndeftlm(ng)
323 IF (ntlm(ng).eq.ndeftlm(ng)) nfiles=nfiles+1
324 CALL edit_file_struct (ng, nfiles, fwd)
325 DO ifile=1,nfiles
326 fwd(ng)%files(ifile)=trim(tlm(ng)%files(ifile))
327 END DO
328 fwd(ng)%name=trim(fwd(ng)%files(1))
329 ELSE
330 IF (fwd(ng)%IOtype.eq.io_nf90) THEN
331 fwd(ng)%ncid=tlm(ng)%ncid
332#if defined PIO_LIB && defined DISTRIBUTE
333 ELSE IF (fwd(ng)%IOtype.eq.io_pio) THEN
334 fwd(ng)%pioFile=tlm(ng)%pioFile
335#endif
336 END IF
337 fwd(ng)%name=trim(tlm(ng)%name)
338 fwd(ng)%files(1)=trim(tlm(ng)%name)
339 END IF
340!
341 END SELECT
342 END DO
343
344 RETURN
345 END SUBROUTINE edit_multifile
346!
347 SUBROUTINE edit_file_struct (ng, Nfiles, S)
348!
349!***********************************************************************
350! !
351! This function loads input values into requested 1D structure !
352! containing information about I/O files. !
353! !
354! On Input: !
355! !
356! ng Nested grid number (integer) !
357! Nfiles Number of desired files (integer) !
358! S Derived type 1D structure, TYPE(T_IO) !
359! !
360! On Output: !
361! !
362! S Updated erived type 1D structure, TYPE(T_IO) !
363! !
364!***********************************************************************
365!
366 USE mod_param
367 USE mod_iounits
368 USE mod_ncparam, ONLY : nv
369!
370! Imported variable declarations.
371!
372 integer, intent(in) :: ng, Nfiles
373
374 TYPE(t_io), intent(inout) :: S(Ngrids)
375!
376! Local variable declarations.
377!
378 integer :: i, j, lstr
379
380 character (len= 1), parameter :: blank = ' '
381!
382!-----------------------------------------------------------------------
383! If the number of multifiles in the structure is different, allocate
384! to the desired number of files.
385!-----------------------------------------------------------------------
386!
387! If the number of muti-files in structure is different than requested
388! values, deallocate and reallocate to the desired number of files.
389!
390 IF (s(ng)%Nfiles.ne.nfiles) THEN
391 IF (associated(s(ng)%Nrec)) deallocate (s(ng)%Nrec)
392 IF (associated(s(ng)%time_min)) deallocate (s(ng)%time_min)
393 IF (associated(s(ng)%time_max)) deallocate (s(ng)%time_max)
394 IF (associated(s(ng)%files)) deallocate (s(ng)%files)
395!
396 allocate ( s(ng)%Nrec(nfiles) )
397 allocate ( s(ng)%time_min(nfiles) )
398 allocate ( s(ng)%time_max(nfiles) )
399 allocate ( s(ng)%files(nfiles) )
400 END IF
401!
402! Intialize strings to blank to facilitate processing.
403!
404 lstr=len(s(ng)%name)
405 DO i=1,lstr
406 s(ng)%name(i:i)=blank
407 END DO
408 DO j=1,nfiles
409 DO i=1,lstr
410 s(ng)%files(j)(i:i)=blank
411 END DO
412 END DO
413!
414! Initialize and load fields into structure. The base filename value
415! was already updated somewhere else.
416!
417 s(ng)%Nfiles=nfiles ! number of multi-files
418 s(ng)%Fcount=1 ! multi-file counter
419 s(ng)%Rindex=0 ! time index
420 s(ng)%ncid=-1 ! closed NetCDF state
421 s(ng)%Vid=-1 ! NetCDF variables IDs
422 s(ng)%Tid=-1 ! NetCDF tracers IDs
423#if defined PIO_LIB && defined DISTRIBUTE
424 s(ng)%pioFile%fh=-1 ! closed file handler
425 DO i=1,nv
426 s(ng)%pioVar(i)%vd%varID=-1 ! variables IDs
427 s(ng)%pioVar(i)%dkind=-1 ! variables data kind
428 s(ng)%pioVar(i)%gtype=0 ! variables C-grid type
429 END DO
430 DO i=1,mt
431 s(ng)%pioTrc(i)%vd%varID=-1 ! tracers IDs
432 s(ng)%pioTrc(i)%dkind=-1 ! tracers data kind
433 s(ng)%pioTrc(i)%gtype=0 ! tracers C-grid type
434 END DO
435#endif
436 s(ng)%Nrec=0 ! record counter
437 s(ng)%time_min=0.0_dp ! starting time
438 s(ng)%time_max=0.0_dp ! ending time
439!
440 10 FORMAT (a,'_',i4.4,'.nc')
441!
442 RETURN
443 END SUBROUTINE edit_file_struct
subroutine edit_file_struct(ng, nfiles, s)
subroutine edit_multifile(task)
subroutine, public close_file(ng, model, s, ncname, lupdate)
Definition close_io.F:43
type(t_io), dimension(:), allocatable his
type(t_io), dimension(:), allocatable fcta
type(t_io), dimension(:), allocatable fctb
type(t_io), dimension(:), allocatable tlm
type(t_io), dimension(:), allocatable blk
type(t_io), dimension(:), allocatable qck
type(t_io), dimension(:), allocatable fwd
character(len=256) sourcefile
integer, parameter io_nf90
Definition mod_ncparam.F:95
integer, parameter nv
integer, parameter io_pio
Definition mod_ncparam.F:96
integer, parameter inlm
Definition mod_param.F:662
integer ngrids
Definition mod_param.F:113
integer mt
Definition mod_param.F:490
integer, dimension(:), allocatable ntimes
integer, dimension(:), allocatable ndefhis
integer, dimension(:), allocatable ntlm
integer, dimension(:), allocatable nqck
integer, dimension(:), allocatable ndeftlm
integer, dimension(:), allocatable ndefqck
integer, dimension(:), allocatable nhis