Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

gridplugin.C

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2016 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: gridplugin.C,v $
00013  *      $Author: johns $       $Locker:  $             $State: Exp $
00014  *      $Revision: 1.15 $       $Date: 2016/11/28 05:01:54 $
00015  *
00016  ***************************************************************************/
00017 
00018 /* 
00019  * Binary potential map format used by Molecular Discovery GRID, 
00020  * UHBD, and other packages.
00021  *
00022  * Files begin with a 160-byte formatted fortran header.
00023  * For each plane in the grid, there is a 12-byte formatted fortran record
00024  * giving the grid coordinates, followed by a nx*ny*4 byte formatted fortran
00025  * record with the grid data.
00026  *
00027  * The header has the following format (addresses in bytes):
00028  * header[0..71]:     char * 72, grid title
00029  * header[72..79]:    float * 2, grid clearance and cutoff energy (unused by
00030  *                    VMD)
00031  * header[80..99]:    unknown
00032  * header[100..111]:  int * 3, number of planes in each direction
00033  * header[112..127]:  float * 4, grid spacing in angstroms, followed by grid
00034  *                    origin
00035  * header[128..151]:  float * 6, VDW radius, neff, alph, q, emin, rmin
00036  *                    (unused by VMD)
00037  * header[152..159]:  int * 2, jd, ja (unused by VMD)
00038  *
00039  * XXX - Not sure if slicing order is the same in every file. Also, other
00040  * values from plane metadata seem to have no use -- I'm probably missing
00041  * something. 
00042  *
00043  */
00044 
00045 #include <stdlib.h>
00046 #include <stdio.h>
00047 #include <string.h>
00048 
00049 #if defined(_AIX)
00050 #include <strings.h>
00051 #endif
00052 
00053 #include "molfile_plugin.h"
00054 #include "endianswap.h"
00055 #include "fortread.h"
00056 
00057 typedef struct {
00058   FILE *fd;
00059   int swap;
00060   molfile_volumetric_t *vol;
00061 } grid_t;
00062 
00063 
00064 static void *open_grid_read(const char *filepath, const char *filetype,
00065     int *natoms) {
00066   FILE *fd;
00067   grid_t *grid;
00068   float header[64], ra, rx, ry, rz;
00069   int dataBegin, swap, blocksize, nnx, nny, nnz;
00070   
00071   fd = fopen(filepath, "rb");
00072   if (!fd) {
00073     fprintf(stderr, "gridplugin) Error opening file.\n");
00074     return NULL;
00075   }
00076 
00077   // Use the first four-byte integer in the file to determine the file's
00078   // byte-order
00079   fread(&dataBegin, sizeof(int), 1, fd);
00080   if ( (dataBegin > 255) || (dataBegin < 0) ) {
00081     // check if the bytes need to be swapped
00082     swap4_aligned(&dataBegin, 1);
00083     if (dataBegin <= 255) {
00084       swap = 1;
00085     } else {
00086       fprintf(stderr, "gridplugin) Cannot read file: header block is too large.\n");
00087       return NULL;
00088     }
00089   }
00090   else {
00091     swap = 0;
00092   }
00093 
00094   // Read the header
00095   rewind(fd);
00096   blocksize = fortread_4(header, 64, swap, fd);
00097 
00098   if (blocksize != 40) {
00099     fprintf(stderr, "gridplugin) Incorrect header size.\n");
00100     return NULL;
00101   }
00102 
00103   // number of planes in each dimension
00104   nnx = ((int *)header)[25];
00105   nny = ((int *)header)[26];
00106   nnz = ((int *)header)[27];
00107 
00108   // grid spacing in angstroms
00109   ra = header[28];
00110 
00111   // reference point for grid position
00112   rx = header[29];
00113   ry = header[30];
00114   rz = header[31];
00115 
00116   // Allocate and initialize the structure 
00117   grid = new grid_t;
00118   grid->fd = fd;
00119   grid->vol = NULL;
00120   *natoms = MOLFILE_NUMATOMS_NONE;
00121   grid->swap = swap;
00122 
00123   grid->vol = new molfile_volumetric_t[1];
00124   strcpy(grid->vol[0].dataname, "GRID Electron Density Map");
00125 
00126   // XXX - origin seems to be shifted by one grid point in each direction
00127   // from the reference point. 
00128   grid->vol[0].origin[0] = rx + ra;
00129   grid->vol[0].origin[1] = ry + ra;
00130   grid->vol[0].origin[2] = rz + ra;
00131 
00132   grid->vol[0].xaxis[0] = nnx * ra;
00133   grid->vol[0].xaxis[1] = 0;
00134   grid->vol[0].xaxis[2] = 0;
00135 
00136   grid->vol[0].yaxis[0] = 0;
00137   grid->vol[0].yaxis[1] = nny * ra;
00138   grid->vol[0].yaxis[2] = 0;
00139 
00140   grid->vol[0].zaxis[0] = 0;
00141   grid->vol[0].zaxis[1] = 0;
00142   grid->vol[0].zaxis[2] = nnz * ra;
00143 
00144   grid->vol[0].xsize = nnx;
00145   grid->vol[0].ysize = nny;
00146   grid->vol[0].zsize = nnz;
00147 
00148   grid->vol[0].has_color = 0;   // This file has no color
00149 
00150   return grid;
00151 }
00152 
00153 static int read_grid_metadata(void *v, int *nsets, 
00154   molfile_volumetric_t **metadata) {
00155   grid_t *grid = (grid_t *)v;
00156   *nsets = 1;                   // This file contains only one data set.
00157   *metadata = grid->vol;  
00158 
00159   return MOLFILE_SUCCESS;
00160 }
00161 
00162 static int read_grid_data(void *v, int set, float *datablock,
00163                          float *colorblock) {
00164   grid_t *grid = (grid_t *)v;
00165   int planeHeader[3], planeSize, i, z;
00166   float *planeData;
00167 
00168   planeSize = grid->vol[0].xsize * grid->vol[0].ysize;
00169 
00170   planeData = new float[planeSize];
00171 
00172   for (i = 0; i < grid->vol[0].zsize; i++) {
00173     // read the plane metadata
00174     if (fortread_4(planeHeader, 3, grid->swap, grid->fd) != 3) {
00175       fprintf(stderr, "gridplugin) Error reading plane metadata.\n");
00176       delete [] planeData;
00177       return MOLFILE_ERROR;
00178     }
00179     z = planeHeader[0] - 1;
00180 
00181     // read the plane data
00182     if (fortread_4(planeData, planeSize, grid->swap, grid->fd) != planeSize) {
00183       fprintf(stderr, "gridplugin) Error reading plane data.\n");
00184       delete [] planeData;
00185       return MOLFILE_ERROR;
00186     }
00187 
00188     // copy the plane data to the datablock
00189     memcpy(datablock + z*planeSize, planeData, planeSize * sizeof(float));
00190   }
00191 
00192   delete [] planeData;
00193   return MOLFILE_SUCCESS;
00194 }
00195 
00196 static void close_grid_read(void *v) {
00197   grid_t *grid = (grid_t *)v;
00198 
00199   fclose(grid->fd);
00200   if (grid->vol != NULL)
00201     delete [] grid->vol; 
00202   delete grid;
00203 }
00204 
00205 /*
00206  * Initialization stuff here
00207  */
00208 static molfile_plugin_t plugin;
00209 
00210 VMDPLUGIN_API int VMDPLUGIN_init(void) { 
00211   memset(&plugin, 0, sizeof(molfile_plugin_t));
00212   plugin.abiversion = vmdplugin_ABIVERSION;
00213   plugin.type = MOLFILE_PLUGIN_TYPE;
00214   plugin.name = "grid";
00215   plugin.prettyname = "GRID,UHBD Binary Potential Map";
00216   plugin.author = "Eamon Caddigan";
00217   plugin.majorv = 0;
00218   plugin.minorv = 3;
00219   plugin.is_reentrant = VMDPLUGIN_THREADSAFE;
00220   plugin.filename_extension = "grid";
00221   plugin.open_file_read = open_grid_read;
00222   plugin.read_volumetric_metadata = read_grid_metadata;
00223   plugin.read_volumetric_data = read_grid_data;
00224   plugin.close_file_read = close_grid_read;
00225   return VMDPLUGIN_SUCCESS;
00226 }
00227 
00228 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00229   (*cb)(v, (vmdplugin_t *)&plugin);
00230   return VMDPLUGIN_SUCCESS;
00231 }
00232 
00233 VMDPLUGIN_API int VMDPLUGIN_fini(void) { return VMDPLUGIN_SUCCESS; }
00234 

Generated on Wed Nov 11 03:06:25 2020 for VMD Plugins (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002