<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<pre>Hi!</pre>
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: <A HREF="http://www.unidata.ucar.edu/packages/netcdf/">http://www.unidata.ucar.edu/packages/netcdf/</A>),
and have got to the point where I don't know what the error message mean.
The C-program is called nc2r.c.
<p>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)
<p>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
<br>nc2r.c: In function `ncread':
<br>nc2r.c:259: warning: assignment makes integer from pointer without
a cast
<br>nc2r.c:260: warning: assignment makes integer from pointer without
a cast
<br>nc2r.c:261: warning: assignment makes integer from pointer without
a cast
<br>nc2r.c:262: warning: assignment makes integer from pointer without
a cast
<br>nc2r.c:263: warning: assignment makes integer from pointer without
a cast
<br>nc2r.c: In function `nc2r':
<br>nc2r.c:310: warning: assignment from incompatible pointer type
<br>nc2r.c:311: warning: assignment from incompatible pointer type
<br>nc2r.c:312: warning: assignment from incompatible pointer type
<br>nc2r.c:325: warning: assignment from incompatible pointer type
<br>nc2r.c:330: warning: assignment from incompatible pointer type
<br>nc2r.c:335: warning: assignment from incompatible pointer type
<br>nc2r.c:340: warning: assignment from incompatible pointer type
<br>nc2r.c:345: warning: assignment from incompatible pointer type
<p>The code is compiled, and I start R:
<p>R : Copyright 1999, The R Development Core Team
<br>Version 0.90.1 (December 15, 1999)
<p>R is free software and comes with ABSOLUTELY NO WARRANTY.
<br>You are welcome to redistribute it under certain conditions.
<br>Type "?license" or "?licence" for distribution details.
<p>R is a collaborative project with many contributors.
<br>Type "?contributors" for a list.
<p>Type "demo()" for some demos, "help()" for on-line help, or
<br> "help.start()" for a HTML browser interface
to help.
<br>Type "q()" to quit R.
<p>> dyn.load(paste("nc2r", .Platform$dynlib.ext, sep=""))
<br>Error in dyn.load(x, as.logical(local), as.logical(now)) : unable to
load shared library "/home/kareb/netcdf/nc2r.so":
<br> /home/kareb/netcdf/nc2r.so: undefined symbol: __dso_handle
<p>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.
<br>
<p>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)
<p>Yours sincerely Rasmus Benestad
<p>-----------------------------------------------------------------------
<br>The main part of the code looks like:
<br>
<p>SEXP nc2r(char **fname, char **vname){
<p> int ncread();
<br> int status,i,j,k,nx,ny,nt;
<br> long int ptrs[5];
/* array of pointers holding locations */
<br>
/* returned by malloc in ncread */
<br> double ofs, scl;
/* offset and scaling factors */
<br> double *ptr;
<br> SEXP field, dim, dimnames, lon, lat, tim, missing;
<p> printf("ncfile= %s, vname=%s\n",fname,vname);
<br>
<br> printf("\nfield->%x\nlon -> %x\nlat -> %x\ntim -> %x\nmissing
-> %x\n",
<br> field,lon,lat,tim,missing);
<p> /* Call the function dealing with the netCDF data */
<br> /* The data is placed in memeory */
<p> status=ncread(fname, vname, &nx, &ny, &nt, &ptrs,
&scl, &ofs);
<p> /* Allocate space for pointers */
<br> /* Use PROTECT to ensure R doesn't mess with the data */
<p> PROTECT(field = allocVector(REALSXP,nx*ny*nt));
<br> PROTECT(lon = allocVector(REALSXP,nx));
<br> PROTECT(lat = allocVector(REALSXP,ny));
<br> PROTECT(tim = allocVector(REALSXP,nt));
<br> PROTECT(missing= allocVector(REALSXP,1));
<br> PROTECT(dim= allocVector(INTSXP,3));
<p> /* Set the dimensions */
<br> /* Index oredring: i=0:nx-1, j=0:ny-1 -> i + nx * j */
<br> /* See Writing R extensions manual p. 26 */
<p> INTEGER(dim)[0]=nx;
<br> INTEGER(dim)[1]=ny;
<br> INTEGER(dim)[2]=nt;
<br> setAttrib(field, R_DimSymbol, dim);
<p> PROTECT(dimnames = allocVector(VECSXP, 3));
<br> VECTOR(dimnames)[0]="Lon";
<br> VECTOR(dimnames)[1]="Lat";
<br> VECTOR(dimnames)[2]="Time";
<p> setAttrib(field, R_DimNamesSymbol, dimnames);
<br>
<br> printf("\nfield->%x\nlon -> %x\nlat -> %x\ntim -> %x\nmissing
-> %x\n",
<br> field,lon,lat,tim,missing);
<br> printf("The dimensions are (nx x ny x nt): %d x %d x %d \n",nx,ny,nt);
<p> printf("\nCopy into R-objects\n");
<p> /* Copy the data to the R-objects */
<br>
<p> ptr=&ptrs[0];
<br> for (i=0;i<nx*ny*nt;i++) {
<br> REAL(field)[i] = *(ptr+i)
* scl + ofs;
<br> }
<p> ptr=&ptrs[1];
<br> for (i=0;i<nx;i++) {
<br> REAL(lon)[i] = *(ptr+i);
<br> }
<p> ptr=&ptrs[2];
<br> for (j=0;j<ny;j++) {
<br> REAL(lat)[j] = *(ptr+j);
<br> }
<p> ptr=&ptrs[3];
<br> for (k=0;k<nt;k++) {
<br> REAL(tim)[k] = *(ptr+k);
<br> }
<p> ptr=&ptrs[4];
<br> REAL(missing)[1]=*ptr;
<p> printf("\nFinished successfully!\n");
<p> /* Garbage collecting */
<p> UNPROTECT(7);
<p> return(field);
<br>}
<pre>--
______________________________________________________________________________
Rasmus E. Benestad, D.Phil, GradInstP, The Norwegian Meteorological Institute
rasmus.benestad@dnmi.no <A HREF="http://home.enitel.no/benestad/rasmus.html">http://home.enitel.no/benestad/rasmus.html</A>
phone: (+47) 22 96 31 70 mobile: (+47) 99 45 04 16 fax: (+47) 22 96 30 50
______________________________________________________________________________</pre>
</html>