ensure_netcdf_att_exists_text Subroutine

private subroutine ensure_netcdf_att_exists_text(file_id, var_id, att_name, att_text)

Add an attribute to an existing netCDF variable. Aborts if any errors are detected.

Arguments

Type IntentOptional Attributes Name
integer(kind=kind_nf), intent(in) :: file_id

ID of the file or group to look for or create the variable under

integer(kind=kind_nf), intent(in) :: var_id

The netCDF ID of the variable under file_id

character(len=*), intent(in) :: att_name

Name of the attribute

character(len=*), intent(in) :: att_text

Attribute text to store


Contents


Source Code

  subroutine ensure_netcdf_att_exists_text(file_id, var_id, att_name, att_text)
    !> ID of the file or group to look for or create the variable under
    integer(kind_nf), intent(in) :: file_id
    !> The netCDF ID of the variable under `file_id`
    integer(kind_nf), intent(in) :: var_id
    !> Name of the attribute
    character(len=*), intent(in) :: att_name
    !> Attribute text to store
    character(len=*), intent(in) :: att_text

    ! Error code of the netCDF calls
    integer(kind_nf) :: status

    status = nf90_inquire_attribute(file_id, var_id, att_name)
    ! Attribute doesn't exist, so let's create it
    if (status == NF90_ENOTATT) then
      status = nf90_put_att(file_id, var_id, att_name, att_text)
    end if
    ! Something went wrong with one of the previous two calls
    if (status /= NF90_NOERR) then
      call netcdf_error(status, file_id, var_id, att=att_name, &
                        message="(ensure_netcdf_att_exists)", abort=.true.)
    end if
  end subroutine ensure_netcdf_att_exists_text