FMS  2025.04
Flexible Modeling System
fms_string_utils.inc
1 !***********************************************************************
2 !* Apache License 2.0
3 !*
4 !* This file is part of the GFDL Flexible Modeling System (FMS).
5 !*
6 !* Licensed under the Apache License, Version 2.0 (the "License");
7 !* you may not use this file except in compliance with the License.
8 !* You may obtain a copy of the License at
9 !*
10 !* http://www.apache.org/licenses/LICENSE-2.0
11 !*
12 !* FMS is distributed in the hope that it will be useful, but WITHOUT
13 !* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied;
14 !* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15 !* PARTICULAR PURPOSE. See the License for the specific language
16 !* governing permissions and limitations under the License.
17 !***********************************************************************
18 
19 !> @brief Converts a 1D array of real numbers to a string
20 !> @return The 1D array as a string
21 function stringify_1d_(arr, fmt)
22  real(STRING_UTILS_KIND_), dimension(:), intent(in) :: arr !< Real array to be converted to a string
23  character(*), intent(in), optional :: fmt !< Optional format string for the real array entries
24  character(:), allocatable :: STRINGIFY_1D_
25  integer :: i, n
26 
27  n = size(arr)
28 
29  if (n .gt. 0) then
30  stringify_1d_ = "[" // string(arr(1), fmt)
31  else
32  stringify_1d_ = "["
33  endif
34 
35  do i = 2,n
36  stringify_1d_ = stringify_1d_ // ", " // string(arr(i), fmt)
37  enddo
38 
39  stringify_1d_ = stringify_1d_ // "]"
40 end function
41 
42 !> @brief Converts a 2D array of real numbers to a string
43 !> @return The 2D array as a string
44 function stringify_2d_(arr, fmt)
45  real(STRING_UTILS_KIND_), dimension(:,:), intent(in) :: arr !< Real array to be converted to a string
46  character(*), intent(in), optional :: fmt !< Optional format string for the real array entries
47  character(:), allocatable :: STRINGIFY_2D_
48  integer :: i, n
49 
50  n = size(arr, 2)
51 
52  if (n .gt. 0) then
53  stringify_2d_ = "[" // stringify_1d_(arr(:,1), fmt)
54  else
55  stringify_2d_ = "["
56  endif
57 
58  do i = 2,n
59  stringify_2d_ = stringify_2d_ // ", " // stringify_1d_(arr(:,i), fmt)
60  enddo
61 
62  stringify_2d_ = stringify_2d_ // "]"
63 end function
64 
65 !> @brief Converts a 3D array of real numbers to a string
66 !> @return The 3D array as a string
67 function stringify_3d_(arr, fmt)
68  real(STRING_UTILS_KIND_), dimension(:,:,:), intent(in) :: arr !< Real array to be converted to a string
69  character(*), intent(in), optional :: fmt !< Optional format string for the real array entries
70  character(:), allocatable :: STRINGIFY_3D_
71  integer :: i, n
72 
73  n = size(arr, 3)
74 
75  if (n .gt. 0) then
76  stringify_3d_ = "[" // stringify_2d_(arr(:,:,1), fmt)
77  else
78  stringify_3d_ = "["
79  endif
80 
81  do i = 2,n
82  stringify_3d_ = stringify_3d_ // ", " // stringify_2d_(arr(:,:,i), fmt)
83  enddo
84 
85  stringify_3d_ = stringify_3d_ // "]"
86 end function