NOTICE: This version of the NSF Unidata web site (archive.unidata.ucar.edu) is no longer being updated.
Current content can be found at unidata.ucar.edu.

[netcdfgroup] problem reading array of strings in F90/C

  • To: netcdfgroup@xxxxxxxxxxxxxxxx
  • Subject: [netcdfgroup] problem reading array of strings in F90/C
  • From: M P <mzp3769@xxxxxxxxx>
  • Date: Thu, 20 Oct 2022 11:16:03 -0600
  • Authentication-results: vscanx3.ucar.edu; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=Ed5V36le
  • Authentication-results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Ed5V36le; spf=pass (google.com: domain of mzp3769@xxxxxxxxx designates 209.85.220.41 as permitted sender) smtp.mailfrom=mzp3769@xxxxxxxxx; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com
Hello,
>From this group's archive and searches I figured that reading those with
f90 is probably
not possible. I wrote a simple code in C but that also is not successful.
Can you help with solving this problem?
Thanks,
Mark

netcdf file (datetime elements always 20 chars)
....

        int nlocs(nlocs) ;
                nlocs:suggested_chunk_dim = 429LL ;
....
group: MetaData {
  variables:
        string datetime(nlocs) ;
                string datetime:_FillValue = "" ;
                string datetime:units = "" ;


ncdump -v  datetime file.nc
.......
  datetime = "2021-09-30T23:02:25Z", "2021-09-30T23:07:25Z",
      "2021-09-30T23:09:23Z", "2021-09-30T23:17:25Z",
"2021-09-30T23:19:22Z",
.......

C code: (nlocs, length=20 are set to correct values)
...
  int status;
..
  size_t start[] = {0,0};
  size_t count[] = {nlocs,length+1};

  char datetime[nlocs][length+1];
  char *datetime_p[nlocs];

...
  for (i = 0; i < nlocs; i++)
    datetime_p[i] = datetime[i];
...
  if ((status = nc_get_vara_string(grpid, varid, start, count,
                                   datetime_p)))
    ERR(status);
/* Error: NetCDF: Start+count exceeds dimension bound */

  if ((status = nc_get_vara(grpid, varid, start, count,
                                   datetime_p)))
    ERR(status);
/* Error: NetCDF: Start+count exceeds dimension bound* /

  if ((status = nc_get_var(grpid, varid, datetime_p)))
    ERR(status);
/* values in array are garbage */

  if ((status = nc_get_var(grpid, varid, &datetime[0][0])))
    ERR(status);
/* values in array are garbage */
........

f90 code:
.......
INTEGER, PARAMETER :: date_string_length=20
CHARACTER(len=date_string_length), ALLOCATABLE :: aertimestr(:)
.......

CALL check_nc(nf90_inq_grp_ncid(ncid,"MetaData",grpid))
CALL check_nc(nf90_inq_varid(grpid,"datetime",varid))
CALL check_nc(nf90_get_var(grpid,varid,aertimestr))

.......
Error:
NetCDF: Attempt to convert between text & numbers