<!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&nbsp;format
(a commonly used file format in the geophysical research community: see
Unidata's URL&nbsp;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&nbsp; -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&nbsp; -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&nbsp; (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>&nbsp;&nbsp;&nbsp;&nbsp; "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>&nbsp; /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>&nbsp;
<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>&nbsp;
<p>SEXP nc2r(char **fname, char **vname){
<p>&nbsp; int ncread();
<br>&nbsp; int status,i,j,k,nx,ny,nt;
<br>&nbsp; long int ptrs[5];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/* array of pointers holding locations */
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/* returned by malloc in ncread */
<br>&nbsp; double ofs, scl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/* offset and scaling factors */
<br>&nbsp; double&nbsp; *ptr;
<br>&nbsp; SEXP field, dim, dimnames, lon, lat, tim, missing;
<p>&nbsp; printf("ncfile= %s, vname=%s\n",fname,vname);
<br>&nbsp;
<br>&nbsp; printf("\nfield->%x\nlon -> %x\nlat -> %x\ntim -> %x\nmissing
-> %x\n",
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; field,lon,lat,tim,missing);
<p>&nbsp; /* Call the function dealing with the netCDF data */
<br>&nbsp; /* The data is placed in memeory */
<p>&nbsp; status=ncread(fname, vname, &amp;nx, &amp;ny, &amp;nt, &amp;ptrs,
&amp;scl, &amp;ofs);
<p>&nbsp; /* Allocate space for pointers */
<br>&nbsp; /* Use PROTECT to ensure R doesn't mess with the data */
<p>&nbsp; PROTECT(field =&nbsp; allocVector(REALSXP,nx*ny*nt));
<br>&nbsp; PROTECT(lon =&nbsp;&nbsp;&nbsp; allocVector(REALSXP,nx));
<br>&nbsp; PROTECT(lat =&nbsp;&nbsp;&nbsp; allocVector(REALSXP,ny));
<br>&nbsp; PROTECT(tim =&nbsp;&nbsp;&nbsp; allocVector(REALSXP,nt));
<br>&nbsp; PROTECT(missing= allocVector(REALSXP,1));
<br>&nbsp; PROTECT(dim=&nbsp;&nbsp;&nbsp;&nbsp; allocVector(INTSXP,3));
<p>&nbsp; /* Set the dimensions */
<br>&nbsp; /* Index oredring: i=0:nx-1, j=0:ny-1 -> i + nx * j */
<br>&nbsp; /* See Writing R extensions manual p. 26 */
<p>&nbsp; INTEGER(dim)[0]=nx;
<br>&nbsp; INTEGER(dim)[1]=ny;
<br>&nbsp; INTEGER(dim)[2]=nt;
<br>&nbsp; setAttrib(field, R_DimSymbol, dim);
<p>&nbsp; PROTECT(dimnames = allocVector(VECSXP, 3));
<br>&nbsp; VECTOR(dimnames)[0]="Lon";
<br>&nbsp; VECTOR(dimnames)[1]="Lat";
<br>&nbsp; VECTOR(dimnames)[2]="Time";
<p>&nbsp; setAttrib(field, R_DimNamesSymbol, dimnames);
<br>&nbsp;
<br>&nbsp; printf("\nfield->%x\nlon -> %x\nlat -> %x\ntim -> %x\nmissing
-> %x\n",
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; field,lon,lat,tim,missing);
<br>&nbsp; printf("The dimensions are (nx x ny x nt): %d x %d x %d \n",nx,ny,nt);
<p>&nbsp; printf("\nCopy into R-objects\n");
<p>&nbsp; /* Copy the data to the R-objects */
<br>&nbsp;
<p>&nbsp; ptr=&amp;ptrs[0];
<br>&nbsp; for (i=0;i&lt;nx*ny*nt;i++) {
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; REAL(field)[i] = *(ptr+i)
* scl + ofs;
<br>&nbsp; }
<p>&nbsp; ptr=&amp;ptrs[1];
<br>&nbsp; for (i=0;i&lt;nx;i++) {
<br>&nbsp;&nbsp;&nbsp; REAL(lon)[i] = *(ptr+i);
<br>&nbsp; }
<p>&nbsp; ptr=&amp;ptrs[2];
<br>&nbsp; for (j=0;j&lt;ny;j++) {
<br>&nbsp;&nbsp;&nbsp; REAL(lat)[j] = *(ptr+j);
<br>&nbsp; }
<p>&nbsp; ptr=&amp;ptrs[3];
<br>&nbsp; for (k=0;k&lt;nt;k++) {
<br>&nbsp;&nbsp;&nbsp; REAL(tim)[k] = *(ptr+k);
<br>&nbsp; }
<p>&nbsp; ptr=&amp;ptrs[4];
<br>&nbsp; REAL(missing)[1]=*ptr;
<p>&nbsp; printf("\nFinished successfully!\n");
<p>&nbsp; /* Garbage collecting */
<p>&nbsp; UNPROTECT(7);
<p>&nbsp; return(field);
<br>}
<pre>--&nbsp;
______________________________________________________________________________
Rasmus E. Benestad, D.Phil, GradInstP,&nbsp; The Norwegian Meteorological Institute
rasmus.benestad@dnmi.no&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <A HREF="http://home.enitel.no/benestad/rasmus.html">http://home.enitel.no/benestad/rasmus.html</A>&nbsp;
phone: (+47) 22 96 31 70&nbsp;&nbsp; mobile: (+47) 99 45 04 16&nbsp;&nbsp;&nbsp; fax: (+47) 22 96 30 50
______________________________________________________________________________</pre>
&nbsp;</html>