[netcdfgroup] reading null string attribute


Hi,

I have a number of files with a global file text attribute
which is sometimes a "null" (zero length) string. An ncdump
shows the global attribute as:

 :initial_file = "" ;

I am having trouble reading this with the f77 interface,
or at least I cannot determine if it is a null value or not.
I write a short serial fortran test code which declares
local character(len=1024) :: initial_file, and initializes
it to blanks (I confirm that len_trim(initial_file)=0,
and it prints all blanks when printed to stdout).

Then a call to nf_get_att_text returns without error. When
I print it to stdout, it does print a null string (although
in my original large MPI code it prints 1024 garbage chars),
but now len_trim(initial_file) is 1024, not zero. I try a
conditional comparing it to "", but it returns false. So I
cannot tell if I have read a null string or not.

A call to nf_inq_attlen reports the length to be 1024, which
was the declared character length of the variable that wrote
the attribute to the file in the first place, not the actual
length of the attribute on the current file, which is zero.

Its not convenient to change the attribute on all these files,
since they go back at least a few years. BTW, I am running this
on yellowstone. I have attached the small test program, which
has the full path to the current file.
Thanks for any advice,

--Ben
program read_initial_file
  implicit none
#include <netcdf.inc>
  integer :: ncid,istat,len
  integer,parameter :: mxlen=1024
  character(len=mxlen) :: file,initial_file

! file = 'TGCM.timegcm-1.5.beta01.pclim_smin_equil.nc'
  file = 
'/glade/scratch/foster/timegcm-1.5.beta01/timegcm-ys/TGCM.timegcm-1.5.beta01.pclim_smin_equil.nc'

  istat = nf_open(file,NF_NOWRITE,ncid)
  if (istat /= NF_NOERR) write(6,"('>>> Error opening file ',a)") file

  istat = nf_inq_attlen(ncid,NF_GLOBAL,"initial_file",len)
  write(6,"('nf_inq_attlen len=',i4)") len
  initial_file = ""
  write(6,"('len_trim=',i4,' initial_file=$',a,'$')") 
len_trim(initial_file),initial_file
  istat = nf_get_att_text(ncid,NF_GLOBAL,"initial_file",initial_file)
  write(6,"('len_trim=',i4,' istat=',i4,' initial_file=$',a,'$')") &
    len_trim(initial_file),istat,initial_file 

  if (initial_file=="".or.initial_file==" ") then
    write(6,"('Read null string')")
  else
    write(6,"('NOT null string')")
  endif
  

end program read_initial_file
  • 2014 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: