FMS  2025.04
Flexible Modeling System
register_global_attribute.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 !> @file
19 !> @brief Routines for different dimensions to add global attributes in netcdf files for the
20 !! @ref register_global_attribute interface
21 
22 !> @addtogroup netcdf_io_mod
23 !> @{
24 
25 !> @brief Add a global attribute.
26 subroutine register_global_attribute_0d(fileobj, &
27  attribute_name, &
28  attribute_value, str_len)
29  class(fmsnetcdffile_t),intent(in) :: fileobj !< File object.
30  character(len=*),intent(in) :: attribute_name !< Attribute name.
31  class(*), intent(in) :: attribute_value !< Attribute value
32  integer, intent(in), optional :: str_len !< Length of the string
33  integer :: err
34  if (fileobj%is_root) then
35  call set_netcdf_mode(fileobj%ncid, &
36  define_mode)
37  select type(attribute_value)
38  type is (character(len=*))
39  if (.not. present(str_len)) call error("register_global_attribute_0d: Need to include str length")
40  err = nf90_put_att(fileobj%ncid, &
41  nf90_global, &
42  trim(attribute_name), &
43  trim(attribute_value(1:str_len)))
44  type is (integer(kind=i4_kind))
45  err = nf90_put_att(fileobj%ncid, &
46  nf90_global, &
47  trim(attribute_name), &
48  attribute_value)
49  type is (integer(kind=i8_kind))
50  if ( .not. fileobj%is_netcdf4) call error(trim(fileobj%path)// &
51  & ": 64 bit integers are only supported with 'netcdf4' file format"// &
52  & ". Set netcdf_default_format='netcdf4' in the fms2_io namelist OR "//&
53  & "add nc_format='netcdf4' to your open_file call")
54  err = nf90_put_att(fileobj%ncid, &
55  nf90_global, &
56  trim(attribute_name), &
57  attribute_value)
58  type is (real(kind=r4_kind))
59  err = nf90_put_att(fileobj%ncid, &
60  nf90_global, &
61  trim(attribute_name), &
62  attribute_value)
63  type is (real(kind=r8_kind))
64  err = nf90_put_att(fileobj%ncid, &
65  nf90_global, &
66  trim(attribute_name), &
67  attribute_value)
68  class default
69  call error("register_global_attribute_0d: unsupported type for "//&
70  trim(attribute_name)//" for file: "//trim(fileobj%path)//"")
71  end select
72  call check_netcdf_code(err, "register_global_attribute_0d: file:"//trim(fileobj%path)//"- attribute:"// &
73  & trim(attribute_name))
74  endif
75 end subroutine register_global_attribute_0d
76 !> @brief Add a global attribute.
77 subroutine register_global_attribute_1d(fileobj, &
78  attribute_name, &
79  attribute_value, str_len)
80  class(fmsnetcdffile_t),intent(in) :: fileobj !< File object.
81  character(len=*),intent(in) :: attribute_name !< Attribute name.
82  class(*),dimension(:), intent(in) :: attribute_value !< Attribute value
83  integer, intent(in), optional :: str_len !< Length of the string
84  integer :: err
85  if (fileobj%is_root) then
86  call set_netcdf_mode(fileobj%ncid, &
87  define_mode)
88  select type(attribute_value)
89 
90  type is (integer(kind=i4_kind))
91  err = nf90_put_att(fileobj%ncid, &
92  nf90_global, &
93  trim(attribute_name), &
94  attribute_value)
95  type is (integer(kind=i8_kind))
96  if ( .not. fileobj%is_netcdf4) call error(trim(fileobj%path)// &
97  &": 64 bit integers are only supported with 'netcdf4' file format"//&
98  &". Set netcdf_default_format='netcdf4' in the fms2_io namelist OR "//&
99  &"add nc_format='netcdf4' to your open_file call")
100  err = nf90_put_att(fileobj%ncid, &
101  nf90_global, &
102  trim(attribute_name), &
103  attribute_value)
104  type is (real(kind=r4_kind))
105  err = nf90_put_att(fileobj%ncid, &
106  nf90_global, &
107  trim(attribute_name), &
108  attribute_value)
109  type is (real(kind=r8_kind))
110  err = nf90_put_att(fileobj%ncid, &
111  nf90_global, &
112  trim(attribute_name), &
113  attribute_value)
114  class default
115  call error("register_global_attribute_1d: unsupported type for "//&
116  trim(attribute_name)//" for file: "//trim(fileobj%path)//"")
117  end select
118  call check_netcdf_code(err, "register_global_attribute_1d: file:"//trim(fileobj%path)//"- attribute:"// &
119  & trim(attribute_name))
120  endif
121 end subroutine register_global_attribute_1d
122 !> @}
subroutine register_global_attribute_0d(fileobj, attribute_name, attribute_value, str_len)
Add a global attribute.
subroutine register_global_attribute_1d(fileobj, attribute_name, attribute_value, str_len)
Add a global attribute.