Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for single string attributes in read #453

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions fortran/netcdf_attributes.F90
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,53 @@ function nf90_put_att_text(ncid, varid, name, values)
end function nf90_put_att_text
! -------
function nf90_get_att_text(ncid, varid, name, values)
use, intrinsic :: iso_c_binding, only: c_ptr, c_size_t, c_f_pointer, c_int
implicit none
integer, intent( in) :: ncid, varid
character(len = *), intent( in) :: name
character(len = *), intent(out) :: values
integer :: nf90_get_att_text

values = ' ' !! make sure result will be blank padded
nf90_get_att_text = nf_get_att_text(ncid, varid, name, values)
interface
integer(c_int) function nc_get_att_string(ncid, varid, name, pp) bind(c)
use iso_c_binding , only : c_int , c_char , c_ptr
integer(c_int) , value :: ncid , varid
character(kind=c_char) , intent(in) :: name
type(c_ptr), intent(out) :: pp
end function nc_get_att_string
end interface
interface
integer(c_size_t) function strlen(cs) bind(c, name='strlen')
use, intrinsic :: iso_c_binding , only : c_size_t , c_ptr
implicit none
type(c_ptr), intent(in), value :: cs
end function strlen
end interface
integer :: xtype , nlen , attid , i
integer(c_int) :: c_ncid , c_varid , c_status , c_nlen
type(c_ptr) :: c_str
character(len_trim(name)+1) :: c_aname
character , pointer :: f_str(:)
nf90_get_att_text = nf90_inquire_attribute(ncid, varid, name, &
xtype, nlen, attid)
if ( nf90_get_att_text == nf90_noerr ) then
if ( xtype == nf90_string .and. nlen == 1 ) then
c_ncid = ncid
c_varid = varid - 1
c_aname = name//char(0)
c_status = nc_get_att_string(c_ncid, c_varid, c_aname, c_str)
nf90_get_att_text = c_status
if ( nf90_get_att_text == nf90_noerr ) then
call c_f_pointer(c_str,f_str,[strlen(c_str)])
values = adjustl("")
do i = 1, size(f_str)
values(i:i) = f_str(i)
end do
end if
else
values = ' ' !! make sure result will be blank padded
nf90_get_att_text = nf_get_att_text(ncid,varid,name,values)
end if
end if
end function nf90_get_att_text
! -------
! Integer attributes
Expand Down Expand Up @@ -183,7 +223,7 @@ function nf90_get_att_FourByteInt(ncid, varid, name, values)
character(len = *), intent( in) :: name
integer (kind = FourByteInt), dimension(:), intent(out) :: values
integer :: nf90_get_att_FourByteInt

integer, dimension(size(values)) :: defaultInteger

nf90_get_att_FourByteInt = nf_get_att_int(ncid, varid, name, defaultInteger)
Expand Down