Skip to content

Unable to read string attributes larger than some length #475

@AgilentGCMS

Description

@AgilentGCMS

I have two netcdf files, oco2_l4_regions_patra.nc and vpp_mask_ecoregions.nc. They both have a global attribute REGION_NAMES, which is a string. Only difference is that that string is significantly longer in the second file than in the first. Say I use the following program get_long_attribute.F90 to read that attribute into a fortran variable.

program main
  use netcdf
  implicit none

  integer :: nc_id, nc_stat, total_length
  character(len=:), allocatable :: all_region_names
  !character(len=*), parameter :: input_filename = 'oco2_l4_regions_patra.nc'
  character(len=*), parameter :: input_filename = 'vpp_mask_ecoregions.nc'

  nc_stat = nf90_open(input_filename, NF90_NOWRITE, nc_id)
  if (nc_stat /= NF90_NOERR) then
     write(*,'("Opening file failed")')
     stop
  end if

  nc_stat = nf90_inquire_attribute(nc_id, NF90_GLOBAL, 'REGION_NAMES', len=total_length)
  if (nc_stat /= NF90_NOERR) then
     write(*,'("Getting attribute length failed")')
     stop
  end if
  write(*,'("length of attribute = ", i0)') total_length

  allocate(character(total_length) :: all_region_names)
  nc_stat = nf90_get_att(nc_id, NF90_GLOBAL, 'REGION_NAMES', all_region_names)
  if (nc_stat /= NF90_NOERR) then
     write(*,'("Reading attribute failed")')
     stop
  end if
  write(*,'("all_region_names = ", a)') trim(all_region_names)

end program main

For the file oco2_l4_regions_patra.nc, this correctly reads the 2093 character string. However, for the file vpp_mask_ecoregions.nc, it incorrectly reads the length as 1, and the call to nf90_get_att fails. Is there a limit on the attribute length or size that the fortran API can read? The files were written with the C API, so I assume this is not a fundamental limit of the netcdf format.

  • netcdf C version -- 4.9.3
  • netcdf Fortran version -- 4.6.2
  • compiler version -- Intel oneapi 2024.2.1
  • compile line -- ifx $(nf-config --fflags) get_long_attribute.F90 $(nf-config --flibs)
  • OS -- Red Hat Enterprise Linux 8.10

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions