FMS  2025.03
Flexible Modeling System
fms_string_utils.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 !> @brief Converts a 1D array of real numbers to a string
21 !> @return The 1D array as a string
22 function stringify_1d_(arr, fmt)
23  real(STRING_UTILS_KIND_), dimension(:), intent(in) :: arr !< Real array to be converted to a string
24  character(*), intent(in), optional :: fmt !< Optional format string for the real array entries
25  character(:), allocatable :: STRINGIFY_1D_
26  integer :: i, n
27 
28  n = size(arr)
29 
30  if (n .gt. 0) then
31  stringify_1d_ = "[" // string(arr(1), fmt)
32  else
33  stringify_1d_ = "["
34  endif
35 
36  do i = 2,n
37  stringify_1d_ = stringify_1d_ // ", " // string(arr(i), fmt)
38  enddo
39 
40  stringify_1d_ = stringify_1d_ // "]"
41 end function
42 
43 !> @brief Converts a 2D array of real numbers to a string
44 !> @return The 2D array as a string
45 function stringify_2d_(arr, fmt)
46  real(STRING_UTILS_KIND_), dimension(:,:), intent(in) :: arr !< Real array to be converted to a string
47  character(*), intent(in), optional :: fmt !< Optional format string for the real array entries
48  character(:), allocatable :: STRINGIFY_2D_
49  integer :: i, n
50 
51  n = size(arr, 2)
52 
53  if (n .gt. 0) then
54  stringify_2d_ = "[" // stringify_1d_(arr(:,1), fmt)
55  else
56  stringify_2d_ = "["
57  endif
58 
59  do i = 2,n
60  stringify_2d_ = stringify_2d_ // ", " // stringify_1d_(arr(:,i), fmt)
61  enddo
62 
63  stringify_2d_ = stringify_2d_ // "]"
64 end function
65 
66 !> @brief Converts a 3D array of real numbers to a string
67 !> @return The 3D array as a string
68 function stringify_3d_(arr, fmt)
69  real(STRING_UTILS_KIND_), dimension(:,:,:), intent(in) :: arr !< Real array to be converted to a string
70  character(*), intent(in), optional :: fmt !< Optional format string for the real array entries
71  character(:), allocatable :: STRINGIFY_3D_
72  integer :: i, n
73 
74  n = size(arr, 3)
75 
76  if (n .gt. 0) then
77  stringify_3d_ = "[" // stringify_2d_(arr(:,:,1), fmt)
78  else
79  stringify_3d_ = "["
80  endif
81 
82  do i = 2,n
83  stringify_3d_ = stringify_3d_ // ", " // stringify_2d_(arr(:,:,i), fmt)
84  enddo
85 
86  stringify_3d_ = stringify_3d_ // "]"
87 end function