[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[netCDF #FQF-733069]: what is wrong with this small program?



Angela,

Have you checked the error returns from your other statements, to make
sure there are no errors before your call to nc_put_vara?

I compiled and ran a program copied from yours with the modifications
needed, and it worked fine, producing a file that had the intended
data in it, according to the ncdump utility.

I've attached the source of the program I compiled and ran, in fqf.c.  
Could you try it and see if you still get an error?

When I run it, it creates the file "fqf.nc", and ncdump shows its 
contents as:

$ ncdump fqf.nc
netcdf fqf {
dimensions:
        x = 2 ;
        y = 3 ;

group: group1 {
  variables:
        int data(x, y) ;
  data:

   data =
  1, 2, 3,
  _, _, _ ;
  } // group group1
}


--Russ

> Thanks for the quick response but I tried both 'data' and '&data[0][0]'
> and it still does not work.
> 
> I still get -101 returned from nc_put_vara.
> 
> Thanks,
> Angela Sigmund
> 
> 
> 
> Unidata netCDF Support wrote:
> > 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
> >
> >
> 
> --
> 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
#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;
size_t start[2]={0,0};
size_t count[2]={1,3};

ret = nc_create("fqf.nc", NC_NETCDF4, &ncid);
if(ret != NC_NOERR) {printf("netCDF error: %s\n", nc_strerror(ret)); return 1;}
ret=nc_def_grp(ncid, "group1",&gid);
if(ret != NC_NOERR) {printf("netCDF error: %s\n", nc_strerror(ret)); return 1;}
ret=nc_def_dim(ncid,"x",2,&dimids[0]);
if(ret != NC_NOERR) {printf("netCDF error: %s\n", nc_strerror(ret)); return 1;}
ret=nc_def_dim(ncid,"y",3,&dimids[1]);
if(ret != NC_NOERR) {printf("netCDF error: %s\n", nc_strerror(ret)); return 1;}
ret=nc_def_var(gid,"data",NC_INT,2,dimids,&varid);
if(ret != NC_NOERR) {printf("netCDF error: %s\n", nc_strerror(ret)); return 1;}
ret = nc_enddef(ncid);
if(ret != NC_NOERR) {printf("netCDF error: %s\n", nc_strerror(ret)); return 1;}

ret = nc_put_vara(gid,varid, start, count, data);
if(ret != NC_NOERR) {printf("netCDF error: %s\n", nc_strerror(ret)); return 1;}

ret= nc_close(ncid);
if(ret != NC_NOERR) {printf("netCDF error: %s\n", nc_strerror(ret)); return 1;}

}