27 attribute_value, str_len)
29 class(fmsnetcdffile_t),
intent(in) :: fileobj
30 character(len=*),
intent(in) :: variable_name
31 character(len=*),
intent(in) :: attribute_name
32 class(*),
intent(in) :: attribute_value
33 integer,
intent(in),
optional :: str_len
39 integer(kind=i4_kind),
dimension(2) :: i32_range
40 integer(kind=i8_kind),
dimension(2) :: i64_range
41 real(kind=r4_kind),
dimension(2) :: r32_range
42 real(kind=r8_kind),
dimension(2) :: r64_range
43 character(len=200) :: append_error_msg
45 append_error_msg =
"register_variable_attribute_0d: file:"//trim(fileobj%path)//
"- variable:"//&
46 &trim(variable_name)//
" attribute: "//trim(attribute_name)
48 if (fileobj%is_root)
then
49 call set_netcdf_mode(fileobj%ncid, define_mode)
50 varid = get_variable_id(fileobj%ncid, trim(variable_name), msg=append_error_msg)
51 select type(attribute_value)
52 type is (
character(len=*))
53 if (.not.
present(str_len))
call error(
"Need to include str length:"//trim(append_error_msg))
54 err = nf90_put_att(fileobj%ncid, varid, trim(attribute_name), &
55 trim(attribute_value(1:str_len)))
56 type is (
integer(kind=i4_kind))
57 err = nf90_put_att(fileobj%ncid, varid, trim(attribute_name), &
59 type is (
integer(kind=i8_kind))
60 if ( .not. fileobj%is_netcdf4)
call error(trim(fileobj%path)//&
61 &
": 64 bit integers are only supported with 'netcdf4' file format"// &
62 &
". Set netcdf_default_format='netcdf4' in the fms2_io namelist OR "//&
63 &
"add nc_format='netcdf4' to your open_file call")
64 err = nf90_put_att(fileobj%ncid, varid, trim(attribute_name), &
66 type is (real(kind=r4_kind))
67 err = nf90_put_att(fileobj%ncid, varid, trim(attribute_name), &
69 type is (real(kind=r8_kind))
70 err = nf90_put_att(fileobj%ncid, varid, trim(attribute_name), &
73 call error(
"Unsupported attribute type:"//trim(append_error_msg))
75 call check_netcdf_code(err, append_error_msg)
79 axtype = get_attribute_type(fileobj%ncid, varid, attribute_name, append_error_msg)
80 xtype = get_variable_type(fileobj%ncid, varid, append_error_msg)
81 if (string_compare(attribute_name,
"_FillValue") .or. &
82 string_compare(attribute_name,
"valid_min") .or. &
83 string_compare(attribute_name,
"valid_max") .or. &
84 string_compare(attribute_name,
"scale_factor") .or. &
85 string_compare(attribute_name,
"add_offset") .or. &
86 string_compare(attribute_name,
"missing_value"))
then
87 if (axtype .ne. xtype)
then
88 call error(
"The variable type does not match the attribute type: "//trim(append_error_msg))
92 if (string_compare(attribute_name,
"_FillValue"))
then
93 if (attribute_exists(fileobj%ncid, varid,
"valid_range", msg=append_error_msg))
then
94 select type(attribute_value)
95 type is (
integer(kind=i4_kind))
96 call get_variable_attribute(fileobj, variable_name,
"valid_range", &
98 if (attribute_value .lt. i32_range(1) .or. &
99 attribute_value .gt. i32_range(2))
then
100 call error(
"_FillValue inside valid_range: "//trim(append_error_msg))
102 type is (
integer(kind=i8_kind))
103 call get_variable_attribute(fileobj, variable_name,
"valid_range", &
105 if (attribute_value .lt. i64_range(1) .or. &
106 attribute_value .gt. i64_range(2))
then
107 call error(
"_FillValue inside valid_range: "//trim(append_error_msg))
109 type is (real(kind=r4_kind))
110 call get_variable_attribute(fileobj, variable_name,
"valid_range", &
112 if (attribute_value .lt. r32_range(1) .or. &
113 attribute_value .gt. r32_range(2))
then
114 call error(
"_FillValue inside valid_range: "//trim(append_error_msg))
116 type is (real(kind=r8_kind))
117 call get_variable_attribute(fileobj, variable_name,
"valid_range", &
119 if (attribute_value .lt. r64_range(1) .or. &
120 attribute_value .gt. r64_range(2))
then
121 call error(
"_FillValue inside valid_range: "//trim(append_error_msg))
124 call error(
"unsupported attribute type: "//trim(append_error_msg))
127 elseif (string_compare(attribute_name,
"valid_min") .or. &
128 string_compare(attribute_name,
"valid_max"))
then
129 if (attribute_exists(fileobj%ncid, varid,
"valid_range", msg=append_error_msg))
then
130 call error(
"cannot have attributes valid_range and valid_min/max: "//trim(append_error_msg))
139 attribute_value, str_len)
141 class(fmsnetcdffile_t),
intent(in) :: fileobj
142 character(len=*),
intent(in) :: variable_name
143 character(len=*),
intent(in) :: attribute_name
144 class(*),
dimension(:),
intent(in) :: attribute_value
145 integer,
intent(in),
optional :: str_len
151 character(len=200) :: append_error_msg
153 append_error_msg =
"register_variable_attribute_0d: file:"//trim(fileobj%path)//
"- variable:"//&
154 &trim(variable_name)//
" attribute: "//trim(attribute_name)
156 if (fileobj%is_root)
then
157 call set_netcdf_mode(fileobj%ncid, define_mode)
158 varid = get_variable_id(fileobj%ncid, trim(variable_name), msg=append_error_msg)
159 select type(attribute_value)
160 type is (
integer(kind=i4_kind))
161 err = nf90_put_att(fileobj%ncid, varid, trim(attribute_name), &
163 type is (
integer(kind=i8_kind))
164 if ( .not. fileobj%is_netcdf4)
call error(trim(fileobj%path)// &
165 &
": 64 bit integers are only supported with 'netcdf4' file format"// &
166 &
". Set netcdf_default_format='netcdf4' in the fms2_io namelist OR "//&
167 &
"add nc_format='netcdf4' to your open_file call")
168 err = nf90_put_att(fileobj%ncid, varid, trim(attribute_name), &
170 type is (real(kind=r4_kind))
171 err = nf90_put_att(fileobj%ncid, varid, trim(attribute_name), &
173 type is (real(kind=r8_kind))
174 err = nf90_put_att(fileobj%ncid, varid, trim(attribute_name), &
177 call error(
"unsupported attribute type: "//trim(append_error_msg))
179 call check_netcdf_code(err, append_error_msg)
181 if (string_compare(attribute_name,
"_FillValue") .or. &
182 string_compare(attribute_name,
"valid_min") .or. &
183 string_compare(attribute_name,
"valid_max") .or. &
184 string_compare(attribute_name,
"scale_factor") .or. &
185 string_compare(attribute_name,
"add_offset") .or. &
186 string_compare(attribute_name,
"missing_value"))
then
187 call error(trim(attribute_name)//
" must be a scalar.")
188 elseif (string_compare(attribute_name,
"valid_range"))
then
189 if (attribute_exists(fileobj%ncid, varid,
"valid_min", msg=append_error_msg) .or. &
190 attribute_exists(fileobj%ncid, varid,
"valid_max", msg=append_error_msg))
then
191 call error(
"cannot have valid_range and valid_min/max: "//trim(append_error_msg))
193 axtype = get_attribute_type(fileobj%ncid, varid, attribute_name, append_error_msg)
194 xtype = get_variable_type(fileobj%ncid, varid, append_error_msg)
195 if (axtype .ne. xtype)
then
196 call error(
"The type of the variable is not the same as the type of the variable: "//trim(append_error_msg))
198 if (
size(attribute_value) .ne. 2)
then
199 call error(
"valid_range must be a vector with two values: "//trim(append_error_msg))
subroutine register_variable_attribute_1d(fileobj, variable_name, attribute_name, attribute_value, str_len)
Add an attribute to a variable.
subroutine register_variable_attribute_0d(fileobj, variable_name, attribute_name, attribute_value, str_len)
Add an attribute to a variable.