00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
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
00078
00079 fread(&dataBegin, sizeof(int), 1, fd);
00080 if ( (dataBegin > 255) || (dataBegin < 0) ) {
00081
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
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
00104 nnx = ((int *)header)[25];
00105 nny = ((int *)header)[26];
00106 nnz = ((int *)header)[27];
00107
00108
00109 ra = header[28];
00110
00111
00112 rx = header[29];
00113 ry = header[30];
00114 rz = header[31];
00115
00116
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
00127
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;
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;
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
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
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
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
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