On Fri, 2003-08-08 at 19:07, Akua A Nti wrote:
> Yes, I would be interested in seeing the code for your module.  How does
> one go about writing modules, anyway?  What is it about Python that makes
> you think that I could use it do what I'm trying to do here?  Thanks.
please find attached a number of files (Makefile for compilation, C
source code and the module description file). it should be fairly
self-explanetory what everything does. you can find instructions on
building your own modules in the documentation:
http://opendx.npaci.edu/docs/html/proguide.htm
there are python bindings for some of the opendx stuff and it would be a
lot easier to do this kind of stuff in python. however, i haven't
managed to look into it yet.
hope this helps
cheers
magi
-- 
Magnus Hagdorn
Department of Geology and Geophysics
The University of Edinburgh
Grant Institute
West Mains Road
Edinburgh EH9 3JW
Scotland
PHONE: (+44) 131 650 5917
FAX:   (+44) 131 668 3184
email: [EMAIL PROTECTED]
include ../make.config

FILES_Query_netCDF = userQuery_netCDF.$(OBJEXT) Query_netCDF.$(OBJEXT)

BIN = $(BASE)/bin

CFLAGS = -I./ -I$(BASE)/include -I$(NETCDF)/include $(DX_CFLAGS)
LDFLAGS = -L$(BASE)/lib_$(DXARCH) 
LIBS = -lDX $(DX_GL_LINK_LIBS) $(DXEXECLINKLIBS)
OLIBS = -lDXlite -lm

Query_netCDF:	$(FILES_Query_netCDF)
		$(SHARED_LINK) $(DXABI) $(LDFLAGS) -o Query_netCDF userQuery_netCDF.$(OBJEXT) Query_netCDF.$(OBJEXT) $(DX_RTL_LDFLAGS) $(SYSLIBS)  

.c.o: ; $(CC) -c $(DXABI) $(DX_RTL_CFLAGS) $(CFLAGS) $*.c 

.C.o: ; $(CC) -c $(DXABI) $(DX_RTL_CFLAGS) $(CFLAGS) $*.C 

userQuery_netCDF.c:	Query_netCDF.mdf
		$(BIN)/mdf2c -m Query_netCDF.mdf > userQuery_netCDF.c

clean: 
		rm -f userQuery_netCDF.c Query_netCDF userQuery_netCDF.o Query_netCDF.o 
/* Query_netCDF.c
   Magnus Hagdorn, September 2001

   OpenDX module used to query contents of a netCDF file

   module inputs : filename(string): name of netCDF file
   module outputs: variables(array of strings): variables in netCDF file
                   unlimited(integer): size of unlimited dimension
*/

#include <dx/dx.h>
#include <netcdf.h>
#include <string.h>

#define EXTRA_VARS 8

Error m_QuerynetCDF(Object *in, Object *out)
{
  char *filename;           /* filename */
  
  Array variables=NULL;     /* OpenDX array for variable names */
  Array unlimited_size=NULL;
  size_t unlimited=0;       /* variable containing size of unlimited dimension */

  int status;               /* netCDF status */
  int nc_id;                /* ID of netCDF file */
  int udim_id;
  int i,j,n;

  int vect_only = 0;

  int velos=0;
  int avg_velos = 0;
  int b_velos = 0;
  int s_velos = 0;
  int b_shear = 0;
  int nvars;                 /* number of variables in netCDF file */
  char **varnames;      /* pointer to strings containing netCDF variable names */
  
  /* extract filename from in[0] and check it's a string */
  if (!in[0])
    {
      DXSetError(ERROR_BAD_PARAMETER,"missing filename");
      return ERROR;
    }
  else if (!DXExtractString(in[0], &filename))
    {
      DXSetError(ERROR_BAD_PARAMETER,"filename must be string");
      return ERROR;
    }

  /* check if we should only look for vectors */
  if (!in[1])
    {
      DXSetError(ERROR_BAD_PARAMETER,"missing vect_only");
      return ERROR;
    }
  else if (!DXExtractInteger(in[1], &vect_only))
    {
      DXSetError(ERROR_BAD_PARAMETER,"error getting vect_only");
      return ERROR;
    }

  /* open netCDF file */
  if ((status = nc_open(filename, 0, &nc_id)) != NC_NOERR)
    {
      DXSetError(ERROR_BAD_PARAMETER,"Error opening file \"%s\": \"%s\"",filename,nc_strerror(status));
      return ERROR;
    }

  /* getting number of variables */
  if ((status = nc_inq_nvars(nc_id, &nvars)) != NC_NOERR)
    {
      DXSetError(ERROR_BAD_PARAMETER,"Error reading file \"%s\": \"%s\"",filename,nc_strerror(status));
      return ERROR;
    }

  /* allocating memory for variable names */
  varnames = (char **) malloc((nvars+EXTRA_VARS)*sizeof(char *));
  for (i=0;i<nvars+EXTRA_VARS;i++)
    *(varnames+i) = NULL;

  /* reading variable names */
  j=0;
  for (i=0;i<nvars;i++)
    {
      nc_inq_varndims(nc_id,i,&n);
      if (n>2)
	{
	  *(varnames+j) = (char *) malloc(NC_MAX_NAME*sizeof(char));
	  if ((status = nc_inq_varname(nc_id, i, *(varnames+j))) != NC_NOERR)
	    {
	      DXSetError(ERROR_BAD_PARAMETER,"Error reading file \"%s\": \"%s\"",filename,nc_strerror(status));
	      return ERROR;
	    }
	  if (strcmp(*(varnames+j),"ih")==0)
	    {
	      if (vect_only == 0)
		{
		  j++;
		  *(varnames+j) = (char *) malloc(NC_MAX_NAME*sizeof(char));
		  strcpy(*(varnames+j),"is");
		}
	    }
	  if (strcmp(*(varnames+j),"vx")==0)
	    velos++;
	  if (strcmp(*(varnames+j),"vy")==0)
	    velos+=2;
	  if (strcmp(*(varnames+j),"vz")==0)
	    velos+4;
	  if (strcmp(*(varnames+j),"svx")==0)
	    s_velos++;
	  if (strcmp(*(varnames+j),"svy")==0)
	    s_velos+=2;
	  if (strcmp(*(varnames+j),"svz")==0)
	    s_velos+4;
	  if (strcmp(*(varnames+j),"bvx")==0)
	    b_velos++;
	  if (strcmp(*(varnames+j),"bvy")==0)
	    b_velos+=2;
	  if (strcmp(*(varnames+j),"bvz")==0)
	    b_velos+4;
	  if (strcmp(*(varnames+j),"hvx")==0)
	    avg_velos++;
	  if (strcmp(*(varnames+j),"hvy")==0)
	    avg_velos+=2;
	  if (strcmp(*(varnames+j),"shearx")==0)
	    b_shear++;
	  if (strcmp(*(varnames+j),"sheary")==0)
	    b_shear+=2;

	  if (vect_only == 0)
	    j++;
	}
    }
  if (velos==3)
    {
      *(varnames+j) = (char *) malloc(8*sizeof(char));
      strcpy(*(varnames+j),"2D velos");
      j++;
    }
  /*if (velos==7)
    {
      *(varnames+j) = (char *) malloc(8*sizeof(char));
      strcpy(*(varnames+j),"3D velos");
      j++;
      }*/
  if (s_velos==3)
    {
      *(varnames+j) = (char *) malloc(16*sizeof(char));
      strcpy(*(varnames+j),"2D surface velos");
      j++;
    }
  /*if (s_velos==7)
    {
      *(varnames+j) = (char *) malloc(16*sizeof(char));
      strcpy(*(varnames+j),"3D surface velos");
      j++;
      }*/
  if (b_velos==3)
    {
      *(varnames+j) = (char *) malloc(14*sizeof(char));
      strcpy(*(varnames+j),"2D basal velos");
      j++;
    }
  /*if (b_velos==7)
    {
      *(varnames+j) = (char *) malloc(14*sizeof(char));
      strcpy(*(varnames+j),"3D basal velos");
      j++;
      }*/
  if (avg_velos==3)
    {
      *(varnames+j) = (char *) malloc(17*sizeof(char));
      strcpy(*(varnames+j),"2D averaged velos");
      j++;
    }
  if (b_shear==3)
    {
      *(varnames+j) = (char *) malloc(22*sizeof(char));
      strcpy(*(varnames+j),"horizontal basal shear");
      j++;
    }
  
  

  /* creating OpenDX stringlist */
  if (!(variables = DXMakeStringListV(j,varnames)))
    {
      DXDelete((Object) variables);
      return ERROR;
    }
  for (i=0;i<nvars+EXTRA_VARS;i++)
    free(*(varnames+i));
  free(varnames);

  /* getting size of unlimited dimension */
  if ((status = nc_inq_unlimdim(nc_id, &udim_id)) != NC_NOERR)
    {
      DXSetError(ERROR_BAD_PARAMETER,"Error reading file \"%s\": \"%s\"",filename,nc_strerror(status));
      DXDelete((Object) variables);
      return ERROR;
    }
  if (udim_id!=-1)
    {
      if ((status = nc_inq_dimlen(nc_id, udim_id, &unlimited)) != NC_NOERR)
	{
	  DXSetError(ERROR_BAD_PARAMETER,"Error reading file \"%s\": \"%s\"",filename,nc_strerror(status));
	  DXDelete((Object) variables);
	  return ERROR;
	}
      if (!(unlimited_size = DXMakeInteger(unlimited)))
	{
	  DXDelete((Object) variables);
	  DXDelete((Object) unlimited_size);
	}
    }
  
  out[0] = (Object) variables;
  out[1] = (Object) unlimited_size;
  
  return OK;
}
 
MODULE QuerynetCDF
CATEGORY Import and Export
DESCRIPTION module used to determine variable names and size of unlimited 
dimension of a netCDF file
LOADABLE /home/magi/ism/tools/OpenDX/modules/Query_netCDF/Query_netCDF
INPUT filename; string; (none); name of netCDF file
INPUT vect_only[visible:0]; integer; 0; find only vector variables
OPTIONS 0; 1
OUTPUT variables; string list; variable names
OUTPUT size; integer; size of unlimited dimension

Reply via email to