FMS  2024.03
Flexible Modeling System
fms_io_unstructured_get_file_unit.inc
1 !***********************************************************************
2 !* GNU Lesser General Public License
3 !*
4 !* This file is part of the GFDL Flexible Modeling System (FMS).
5 !*
6 !* FMS is free software: you can redistribute it and/or modify it under
7 !* the terms of the GNU Lesser General Public License as published by
8 !* the Free Software Foundation, either version 3 of the License, or (at
9 !* your option) any later version.
10 !*
11 !* FMS is distributed in the hope that it will be useful, but WITHOUT
12 !* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 !* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 !* for more details.
15 !*
16 !* You should have received a copy of the GNU Lesser General Public
17 !* License along with FMS. If not, see <http://www.gnu.org/licenses/>.
18 !***********************************************************************
19 !----------
20 !ug support
21 !> @addtogroup fms_io_mod
22 !> @{
23 
24 !>Return the file unit and index in the "files_read" module array for the
25 !!inputted file. If the file does not currently exist in the "files_read"
26 !!array (i.e., it is not currenly open), then open it.
27 subroutine fms_io_unstructured_get_file_unit(filename, &
28  funit, &
29  index_file, &
30  read_dist, &
31  domain)
32 
33  !Inputs/Outputs
34  character(len=*),intent(in) :: filename !<Name of the file to be read from.
35  integer(INT_KIND),intent(out) :: funit !<File unit for the inputted file.
36  integer(INT_KIND),intent(out) :: index_file !<Index of the inputted file in the "files_read" module array.
37  logical(INT_KIND),intent(in) :: read_dist !<Flag telling if the IO domain tile id string
38  !! exists at the end of the inputted file name.
39  type(domainug),intent(in) :: domain !<An unstructured mpp domain.
40 
41  !Local variables
42  integer(INT_KIND) :: i !<Loop variable.
43 
44  !Check if the file exists in the "files_read" module array. If the file
45  !is found in the array, this implies that the file was opened at some
46  !point. If the file is still open, then return. If not, then throw a
47  !fatal error.
48  !num_files_r is a module variable.
49  !files_read is a module variable.
50  do i = 1,num_files_r
51  if (trim(files_read(i)%name) .eq. trim(filename)) then
52  index_file = i
53  funit = files_read(index_file)%unit
54  if (.not. mpp_file_is_opened(funit)) then
55  call mpp_error(fatal, &
56  "fms_io_unstructured_get_file_unit:" &
57  //" the file "//trim(filename) &
58  //" was previously opened, but is not" &
59  //" currently open.")
60  endif
61  return
62  endif
63  enddo
64 
65  !Make sure that the number of files open for reading will not exceed the
66  !maximum number allowed.
67  !max_files_r is a module variable.
68  if (num_files_r .eq. max_files_r) then
69  call mpp_error(fatal, &
70  "fms_io_unstructured_get_file_unit: the number of" &
71  //" files currently open for reading exceeds" &
72  //" max_files_r. Pleaes increase this value via" &
73  //" the fms_io_nml namelist.")
74  endif
75 
76  !Store the filename and initialize an array for the file fields.
77  num_files_r = num_files_r + 1
78  files_read(num_files_r)%name = trim(filename)
79  allocate(files_read(num_files_r)%var(max_fields))
80  files_read(num_files_r)%nvar = 0
81  index_file = num_files_r
82 
83  !Open the file.
84  if (read_dist) then
85  call mpp_open(funit, &
86  trim(filename), &
87  form=form, &
88  action=mpp_rdonly, &
89  threading=mpp_multi, &
90  fileset=mpp_multi, &
91  domain_ug=domain)
92  else
93  call mpp_open(funit, &
94  trim(filename), &
95  form=form, &
96  action=mpp_rdonly, &
97  threading=mpp_multi, &
98  fileset=mpp_single)
99  endif
100 
101  !Store the file unit returned by mpp_open.
102  files_read(index_file)%unit = funit
103 
104  return
106 !> @}
subroutine fms_io_unstructured_get_file_unit(filename, funit, index_file, read_dist, domain)
Return the file unit and index in the "files_read" module array for the inputted file....
logical function mpp_file_is_opened(unit)
return if certain file with unit is opened or not