Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

uhbdplugin.C

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2005 The Board of Trustees of the
00004  *cr                        University of Illinois
00005  *cr                         All Rights Reserved
00006  *cr
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *      $RCSfile: uhbdplugin.C,v $
00013  *      $Author: johns $       $Locker:  $             $State: Exp $
00014  *      $Revision: 1.8 $       $Date: 2006/03/14 08:16:28 $
00015  *
00016  ***************************************************************************/
00017 
00018 /*
00019  * Plugin by Alexander Spaar for reading UHBD grid files
00020  * UHBD related docs:
00021  *   http://adrik.bchs.uh.edu/uhbd.html
00022  *   http://adrik.bchs.uh.edu/uhbd/
00023  *   http://mccammon.ucsd.edu/uhbd.html
00024  */
00025 
00026 #include <stdlib.h>
00027 #include <stdio.h>
00028 #include <ctype.h>
00029 #include <math.h>
00030 #include <string.h>
00031 
00032 #if defined(_AIX)
00033 #include <strings.h>
00034 #endif
00035 
00036 #if defined(WIN32) || defined(WIN64)
00037 #define strcasecmp  stricmp
00038 #define strncasecmp strnicmp
00039 #endif
00040 
00041 #include "molfile_plugin.h"
00042 
00043 #define LINESIZE 85
00044 
00045 typedef struct {
00046   FILE *fd;
00047   int nsets;
00048   molfile_volumetric_t *vol;
00049 } uhbd_t;
00050 
00051 
00052 // Get a string from a stream, printing any errors that occur
00053 static char *uhbdgets(char *s, int n, FILE *stream, const char *msg) {
00054   char *returnVal;
00055 
00056   if (feof(stream)) {
00057     printf(msg);
00058     printf("uhbdplugin) Unexpected end-of-file.\n");
00059     return NULL;
00060   } else if (ferror(stream)) {
00061     printf(msg);
00062     printf("uhbdplugin) Error reading file.\n");
00063     return NULL;
00064   } else {
00065     returnVal = fgets(s, n, stream);
00066     if (returnVal == NULL) {
00067       printf(msg);
00068       printf("uhbdplugin) Encountered EOF or error reading line.\n");
00069     }
00070   }
00071 
00072   return returnVal;
00073 }
00074 
00075 
00076 static void *open_uhbd_read(const char *filepath, const char *filetype,
00077     int *natoms) {
00078   FILE *fd;
00079   uhbd_t *uhbd;
00080   char inbuf[LINESIZE];
00081   int xsize, ysize, zsize;
00082   float orig[3], ra, o[3], s[3]; //xdelta[3], ydelta[3], zdelta[3];
00083   
00084   if ((fd = fopen(filepath, "rb")) == NULL) {
00085     printf("uhbdplugin) Error opening file.\n");
00086     return NULL;
00087   }
00088 
00089   // Read the header
00090   if (uhbdgets(inbuf, LINESIZE, fd, 
00091       "uhbdplugin) error while skipping header lines\n") == NULL) 
00092     return NULL;
00093   if (uhbdgets(inbuf, LINESIZE, fd,
00094       "uhbdplugin) error while skipping header lines\n") == NULL) 
00095     return NULL;
00096 
00097   /* get grid dimensions, spacing and origin */
00098   if (uhbdgets(inbuf, LINESIZE, fd,
00099       "uhbdplugin) error while getting grid dimensions\n") == NULL) {
00100     return NULL;
00101   }
00102   if (sscanf(inbuf, "%d %d %d %e %e %e %e", &xsize, &ysize, &zsize, &ra, orig, orig+1, orig+2)  != 7) {
00103     printf("uhbdplugin) Error reading grid dimensions, spacing and origin.\n");
00104     return NULL;
00105   }
00106   if (uhbdgets(inbuf, LINESIZE, fd,
00107       "uhbdplugin) error while skipping header lines\n") == NULL) 
00108     return NULL;
00109   if (uhbdgets(inbuf, LINESIZE, fd,
00110       "uhbdplugin) error while skipping header lines\n") == NULL) 
00111     return NULL;
00112 
00113   /* allocate and initialize the uhbd structure */
00114   uhbd = new uhbd_t;
00115   uhbd->fd = fd;
00116   uhbd->vol = NULL;
00117   *natoms = MOLFILE_NUMATOMS_NONE;
00118   uhbd->nsets = 1; /* this file contains only one data set */
00119 
00120   uhbd->vol = new molfile_volumetric_t[1];
00121   strcpy(uhbd->vol[0].dataname, "UHBD ascii Electron Density Map");
00122 
00123   /* Set the unit cell origin and basis vectors */
00124   for (int i=0; i<3; i++) {
00125     uhbd->vol[0].origin[i] = orig[i] + ra;
00126     o[i] = uhbd->vol[0].origin[i];
00127   }
00128 
00129   uhbd->vol[0].xaxis[0] = ra * (xsize-1);
00130   uhbd->vol[0].xaxis[1] = 0;
00131   uhbd->vol[0].xaxis[2] = 0;
00132 
00133   uhbd->vol[0].yaxis[0] = 0;
00134   uhbd->vol[0].yaxis[1] = ra * (ysize-1);
00135   uhbd->vol[0].yaxis[2] = 0;
00136 
00137   uhbd->vol[0].zaxis[0] = 0;
00138   uhbd->vol[0].zaxis[1] = 0;
00139   uhbd->vol[0].zaxis[2] = ra * (zsize-1);
00140 
00141   s[0] = uhbd->vol[0].xaxis[0];
00142   s[1] = uhbd->vol[0].yaxis[1];
00143   s[2] = uhbd->vol[0].zaxis[2];
00144 
00145   uhbd->vol[0].xsize = xsize;
00146   uhbd->vol[0].ysize = ysize;
00147   uhbd->vol[0].zsize = zsize;
00148 
00149   /* UHBD files contain no color information. */
00150   uhbd->vol[0].has_color = 0;
00151 
00152   return uhbd;
00153 }
00154 
00155 static int read_uhbd_metadata(void *v, int *nsets, 
00156   molfile_volumetric_t **metadata) {
00157   uhbd_t *uhbd = (uhbd_t *)v;
00158   *nsets = uhbd->nsets; 
00159   *metadata = uhbd->vol;  
00160 
00161   return MOLFILE_SUCCESS;
00162 }
00163 
00164 static int read_uhbd_data(void *v, int set, float *datablock,
00165                          float *colorblock) {
00166   uhbd_t *uhbd = (uhbd_t *)v;
00167   FILE *fd = uhbd->fd;
00168   char inbuf[LINESIZE];
00169   float grid[6];
00170   int z, xsize, ysize, zsize, xysize, count, count2, total, i;
00171 
00172   xsize = uhbd->vol[0].xsize;
00173   ysize = uhbd->vol[0].ysize;
00174   zsize = uhbd->vol[0].zsize;
00175   xysize = xsize * ysize;
00176   total = xysize * zsize;
00177 
00178   /* Read the values from the file */
00179   for (z = 0; z < zsize; z++) {
00180     // read header
00181     if (uhbdgets(inbuf, LINESIZE, fd, 
00182         "uhbdplugin) error while getting density plane indices\n") == NULL)
00183       return MOLFILE_ERROR;
00184 
00185     // read data
00186     for (count = 0; count < xysize/6; count++) {
00187       if (uhbdgets(inbuf, LINESIZE, fd,
00188           "uhbdplugin) error while getting density values\n") == NULL)
00189         return MOLFILE_ERROR;
00190 
00191       if (sscanf(inbuf, "%e %e %e %e %e %e", &grid[0], &grid[1], &grid[2], &grid[3], &grid[4], &grid[5]) != 6) {
00192         printf("uhbdplugin) Error reading grid data.\n");
00193         return MOLFILE_ERROR;
00194       }
00195     
00196       for (i = 0; i < 6; i++) { 
00197         datablock[i + count*6 + z*xysize] = grid[i];
00198       }
00199     }
00200 
00201     if ((xysize%6) != 0) {
00202       if (uhbdgets(inbuf, LINESIZE, fd, 
00203           "uhbdplugin) error reading data elements modulo 6\n") == NULL)
00204         return MOLFILE_ERROR;
00205 
00206       count2 = sscanf(inbuf, "%e %e %e %e %e %e", &grid[0], &grid[1], &grid[2], &grid[3], &grid[4], &grid[5]);
00207       if (count2 != (xysize%6)) {
00208         printf("uhbdplugin) Error: incorrect number of data points.\n");
00209         return MOLFILE_ERROR;
00210       }
00211 
00212       for (i = 0; i < count2; i++) {
00213         datablock[i + (count+1)*6 + z*xysize] = grid[i];
00214       }
00215     }
00216   }
00217 
00218   return MOLFILE_SUCCESS;
00219 }
00220 
00221 
00222 static void close_uhbd_read(void *v) {
00223   uhbd_t *uhbd = (uhbd_t *)v;
00224   
00225   fclose(uhbd->fd);
00226   if (uhbd->vol != NULL)
00227     delete [] uhbd->vol; 
00228   delete uhbd;
00229 }
00230 
00231 
00232 /*
00233  * Initialization stuff here
00234  */
00235 static molfile_plugin_t plugin = {
00236   vmdplugin_ABIVERSION,   /* ABI version */
00237   MOLFILE_PLUGIN_TYPE,    /* plugin type */
00238   "uhbd",                 /* short file format description */
00239   "UHBD Grid",            /* pretty file format description */
00240   "Alexander Spaar",      /* author(s) */
00241   0,                      /* major version */
00242   2,                      /* minor version */
00243   VMDPLUGIN_THREADSAFE,   /* is reentrant */
00244   "uhbdgrd,grd"           /* filename extension */
00245 };
00246 
00247 
00248 VMDPLUGIN_API int VMDPLUGIN_init(void) { return VMDPLUGIN_SUCCESS; }
00249 VMDPLUGIN_API int VMDPLUGIN_fini(void) { return VMDPLUGIN_SUCCESS; }
00250 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00251   plugin.open_file_read = open_uhbd_read;
00252   plugin.read_volumetric_metadata = read_uhbd_metadata;
00253   plugin.read_volumetric_data = read_uhbd_data;
00254   plugin.close_file_read = close_uhbd_read;
00255   (*cb)(v, (vmdplugin_t *)&plugin);
00256   return VMDPLUGIN_SUCCESS;
00257 }
00258 

Generated on Wed Mar 22 13:15:31 2006 for VMD Plugins (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002