[R] REading the netCDF format into R

Rasmus E. Benestad rasmus.benestad at dnmi.no
Fri Dec 22 13:16:18 CET 2000


Hi!

I have been coding a C-program which will read data from the
netCDF format (a commonly used file format in the geophysical research
community: see Unidata's URL for explanation:
http://www.unidata.ucar.edu/packages/netcdf/), and have got to the point
where I don't know what the error message mean. The C-program is called
nc2r.c.

When compiling the code, I get a number of warnings, but these are the
same warnings that I get when I compile a corresponding code that runs
on its own (i.e. doesn't use the R-libraries)

cc -shared -lc  -o nc2r.so nc2r.c -I/home/matlab6/extern/include
-I/usr/include -I/usr/lib/R/include -I/home/kareb/local/include
-L/usr/lib/gcc-lib/i386-linux/2.95.2 /usr/lib/libnetcdf.a  -lg2c -lm
nc2r.c: In function `ncread':
nc2r.c:259: warning: assignment makes integer from pointer without a
cast
nc2r.c:260: warning: assignment makes integer from pointer without a
cast
nc2r.c:261: warning: assignment makes integer from pointer without a
cast
nc2r.c:262: warning: assignment makes integer from pointer without a
cast
nc2r.c:263: warning: assignment makes integer from pointer without a
cast
nc2r.c: In function `nc2r':
nc2r.c:310: warning: assignment from incompatible pointer type
nc2r.c:311: warning: assignment from incompatible pointer type
nc2r.c:312: warning: assignment from incompatible pointer type
nc2r.c:325: warning: assignment from incompatible pointer type
nc2r.c:330: warning: assignment from incompatible pointer type
nc2r.c:335: warning: assignment from incompatible pointer type
nc2r.c:340: warning: assignment from incompatible pointer type
nc2r.c:345: warning: assignment from incompatible pointer type

The code is compiled, and I start R:

R : Copyright 1999, The R Development Core Team
Version 0.90.1  (December 15, 1999)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type "?license" or "?licence" for distribution details.

R is a collaborative project with many contributors.
Type "?contributors" for a list.

Type "demo()" for some demos, "help()" for on-line help, or
     "help.start()" for a HTML browser interface to help.
Type "q()" to quit R.

> dyn.load(paste("nc2r", .Platform$dynlib.ext, sep=""))
Error in dyn.load(x, as.logical(local), as.logical(now)) : unable to
load shared library "/home/kareb/netcdf/nc2r.so":
  /home/kareb/netcdf/nc2r.so: undefined symbol: __dso_handle

The symbol "__dso_handle" I presume is defined in Rinternals.h or some
R-routines, or am I wrong? Are my R-path set incorrectly? Why does R say
that it cannot load nc2r.so, when this file is in the local directory?
There were some clashes between definitions in R.h and Rinternals.h, so
I did not include R.h.


Also, is ther any way of returning more than one object from the C-call
(ie. the data field, longitude vector, latitude vector, time vector, and
the missing value)

Yours sincerely Rasmus Benestad

-----------------------------------------------------------------------
The main part of the code looks like:


SEXP nc2r(char **fname, char **vname){

  int ncread();
  int status,i,j,k,nx,ny,nt;
  long int ptrs[5];        /* array of pointers holding locations */
                           /* returned by malloc in ncread */
  double ofs, scl;                             /* offset and scaling
factors */
  double  *ptr;
  SEXP field, dim, dimnames, lon, lat, tim, missing;

  printf("ncfile= %s, vname=%s\n",fname,vname);

  printf("\nfield->%x\nlon -> %x\nlat -> %x\ntim -> %x\nmissing ->
%x\n",
         field,lon,lat,tim,missing);

  /* Call the function dealing with the netCDF data */
  /* The data is placed in memeory */

  status=ncread(fname, vname, &nx, &ny, &nt, &ptrs, &scl, &ofs);

  /* Allocate space for pointers */
  /* Use PROTECT to ensure R doesn't mess with the data */

  PROTECT(field =  allocVector(REALSXP,nx*ny*nt));
  PROTECT(lon =    allocVector(REALSXP,nx));
  PROTECT(lat =    allocVector(REALSXP,ny));
  PROTECT(tim =    allocVector(REALSXP,nt));
  PROTECT(missing= allocVector(REALSXP,1));
  PROTECT(dim=     allocVector(INTSXP,3));

  /* Set the dimensions */
  /* Index oredring: i=0:nx-1, j=0:ny-1 -> i + nx * j */
  /* See Writing R extensions manual p. 26 */

  INTEGER(dim)[0]=nx;
  INTEGER(dim)[1]=ny;
  INTEGER(dim)[2]=nt;
  setAttrib(field, R_DimSymbol, dim);

  PROTECT(dimnames = allocVector(VECSXP, 3));
  VECTOR(dimnames)[0]="Lon";
  VECTOR(dimnames)[1]="Lat";
  VECTOR(dimnames)[2]="Time";

  setAttrib(field, R_DimNamesSymbol, dimnames);

  printf("\nfield->%x\nlon -> %x\nlat -> %x\ntim -> %x\nmissing ->
%x\n",
         field,lon,lat,tim,missing);
  printf("The dimensions are (nx x ny x nt): %d x %d x %d \n",nx,ny,nt);

  printf("\nCopy into R-objects\n");

  /* Copy the data to the R-objects */


  ptr=&ptrs[0];
  for (i=0;i<nx*ny*nt;i++) {
         REAL(field)[i] = *(ptr+i) * scl + ofs;
  }

  ptr=&ptrs[1];
  for (i=0;i<nx;i++) {
    REAL(lon)[i] = *(ptr+i);
  }

  ptr=&ptrs[2];
  for (j=0;j<ny;j++) {
    REAL(lat)[j] = *(ptr+j);
  }

  ptr=&ptrs[3];
  for (k=0;k<nt;k++) {
    REAL(tim)[k] = *(ptr+k);
  }

  ptr=&ptrs[4];
  REAL(missing)[1]=*ptr;

  printf("\nFinished successfully!\n");

  /* Garbage collecting */

  UNPROTECT(7);

  return(field);
}

--
______________________________________________________________________________
Rasmus E. Benestad, D.Phil, GradInstP,  The Norwegian Meteorological Institute
rasmus.benestad at dnmi.no             http://home.enitel.no/benestad/rasmus.html
phone: (+47) 22 96 31 70   mobile: (+47) 99 45 04 16    fax: (+47) 22 96 30 50
______________________________________________________________________________


-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://stat.ethz.ch/pipermail/r-help/attachments/20001222/fa9a5581/attachment.html


More information about the R-help mailing list