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

  • To: M P <mzp3769@xxxxxxxxx>
  • Subject: Re: [netcdfgroup] problem reading array of strings in F90/C
  • From: Dave Allured - NOAA Affiliate <dave.allured@xxxxxxxx>
  • Date: Fri, 21 Oct 2022 09:20:03 -0600
MP, download a copy of the netcdf-c source code.  In the test directories,
I think there are valid working examples of reading and writing string
arrays with C.  In particular, look for files tst_string*.c.  This should
get you started.

If desired, you can then write a fortran-callable C routine that reads
string arrays, using a memory structure of your own choice.


On Thu, Oct 20, 2022 at 12:34 PM M P <mzp3769@xxxxxxxxx> wrote:

> 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
>
  • 2022 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: