[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[netCDF #FQF-733069]: what is wrong with this small program?
- Subject: [netCDF #FQF-733069]: what is wrong with this small program?
- Date: Wed, 26 May 2010 09:13:33 -0600
Hi Angela,
> Is something wrong with my start/count? I get -101 returned from
> nc_put_vara.
Nothing is wrong with your start/count, but address for the start of the
data array that you provided is wrong. Instead of
(const void *)&data[0]
which would be OK for a one-dimensional data array, use
&data[0][0]
because you have declared data to be a 2-dimensional array. It also works if
you just use
data
as the argument, because in C for a 2D array, that's the same as &data[0][0].
Your statement
> ret = nc_put_vara(gid,varid,(const size_t *)start,(const size_t
> *)count,(const void *)&data[0]);
could be simplified to
ret = nc_put_vara(gid,varid, start, count, data);
if you declared start and count to be of type size_t instead of int:
size_t start[2]={0,0};
size_t count[2]={1,3};
--Russ
> Thanks,
> Angela Sigmund
>
> *******************************************************
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> #include <netcdf.h>
>
> int main(void){
>
> int ret,ncid,gid;
> int dimids[2];
> int data[2][3]={1,2,3,4,5,6};
> int varid;
> int start[2]={0,0};
> int count[2]={1,3};
>
> ret = nc_create("/home/asigmund/data/test.nc", NC_NETCDF4, &ncid);
> ret=nc_def_grp(ncid, "group1",&gid);
> ret=nc_def_dim(ncid,"x",2,&dimids[0]);
> ret=nc_def_dim(ncid,"y",3,&dimids[1]);
> ret=nc_def_var(gid,"data",NC_INT,2,dimids,&varid);
> ret = nc_enddef(ncid);
>
> ret = nc_put_vara(gid,varid,(const size_t *)start,(const size_t
> *)count,(const void *)&data[0]);
> if (ret!=0) printf("%d\n",ret);
>
> ret= nc_close(ncid);
>
> }
>
>
>
> --
> This message has been scanned for viruses and
> dangerous content by MailScanner, and is
> believed to be clean.
>
>
Russ Rew UCAR Unidata Program
address@hidden http://www.unidata.ucar.edu
Ticket Details
===================
Ticket ID: FQF-733069
Department: Support netCDF
Priority: Normal
Status: Closed