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
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 #include <stdlib.h>
00063 #include <stdio.h>
00064 #include <ctype.h>
00065 #include <math.h>
00066 #include <string.h>
00067
00068 #if defined(_AIX)
00069 #include <strings.h>
00070 #endif
00071
00072 #if defined(WIN32) || defined(WIN64)
00073 #define strcasecmp stricmp
00074 #define strncasecmp strnicmp
00075 #endif
00076
00077 #include "molfile_plugin.h"
00078 #include "largefiles.h"
00079
00080 #define THISPLUGIN plugin
00081 #include "vmdconio.h"
00082
00083 #define LINESIZE 2040
00084
00085 typedef struct {
00086 FILE *fd;
00087 int nsets;
00088 molfile_volumetric_t *vol;
00089 int isBinary;
00090 } dx_t;
00091
00092
00093
00094 static char *dxgets(char *s, int n, FILE *stream) {
00095 char *returnVal;
00096
00097 if (feof(stream)) {
00098 vmdcon_printf(VMDCON_ERROR, "dxplugin) Unexpected end-of-file.\n");
00099 return NULL;
00100 } else if (ferror(stream)) {
00101 vmdcon_printf(VMDCON_ERROR, "dxplugin) Error reading file.\n");
00102 return NULL;
00103 } else {
00104 returnVal = fgets(s, n, stream);
00105 if (returnVal == NULL) {
00106 vmdcon_printf(VMDCON_ERROR, "dxplugin) Error reading line.\n");
00107 }
00108 }
00109
00110 return returnVal;
00111 }
00112
00113
00114 static void *open_dx_read(const char *filepath, const char *filetype,
00115 int *natoms) {
00116 FILE *fd;
00117 dx_t *dx;
00118 char inbuf[LINESIZE];
00119 int xsize, ysize, zsize;
00120 float orig[3], xdelta[3], ydelta[3], zdelta[3];
00121 int isBinary = 0;
00122
00123 fd = fopen(filepath, "rb");
00124 if (!fd) {
00125 vmdcon_printf(VMDCON_ERROR, "dxplugin) Error opening file.\n");
00126 return NULL;
00127 }
00128
00129
00130 do {
00131 if (dxgets(inbuf, LINESIZE, fd) == NULL)
00132 return NULL;
00133 } while (inbuf[0] == '#');
00134
00135
00136 if (sscanf(inbuf, "object 1 class gridpositions counts %d %d %d", &xsize, &ysize, &zsize) != 3) {
00137 vmdcon_printf(VMDCON_ERROR, "dxplugin) Error reading grid dimensions.\n");
00138 return NULL;
00139 }
00140
00141
00142 if (dxgets(inbuf, LINESIZE, fd) == NULL) {
00143 return NULL;
00144 }
00145 if (sscanf(inbuf, "origin %e %e %e", orig, orig+1, orig+2) != 3) {
00146 vmdcon_printf(VMDCON_ERROR, "dxplugin) Error reading grid origin.\n");
00147 return NULL;
00148 }
00149
00150
00151 if (dxgets(inbuf, LINESIZE, fd) == NULL) {
00152 return NULL;
00153 }
00154 if (sscanf(inbuf, "delta %e %e %e", xdelta, xdelta+1, xdelta+2) != 3) {
00155 vmdcon_printf(VMDCON_ERROR, "dxplugin) Error reading cell x-dimension.\n");
00156 return NULL;
00157 }
00158
00159 if (dxgets(inbuf, LINESIZE, fd) == NULL) {
00160 return NULL;
00161 }
00162 if (sscanf(inbuf, "delta %e %e %e", ydelta, ydelta+1, ydelta+2) != 3) {
00163 vmdcon_printf(VMDCON_ERROR, "dxplugin) Error reading cell y-dimension.\n");
00164 return NULL;
00165 }
00166
00167 if (dxgets(inbuf, LINESIZE, fd) == NULL) {
00168 return NULL;
00169 }
00170 if (sscanf(inbuf, "delta %e %e %e", zdelta, zdelta+1, zdelta+2) != 3) {
00171 vmdcon_printf(VMDCON_ERROR, "dxplugin) Error reading cell z-dimension.\n");
00172 return NULL;
00173 }
00174
00175
00176
00177 if (dxgets(inbuf, LINESIZE, fd) == NULL)
00178 return NULL;
00179
00180
00181
00182
00183 if (dxgets(inbuf, LINESIZE, fd) == NULL)
00184 return NULL;
00185 if (strstr(inbuf, "binary")) {
00186 isBinary = 1;
00187 }
00188
00189
00190 dx = new dx_t;
00191 dx->fd = fd;
00192 dx->vol = NULL;
00193 dx->isBinary = isBinary;
00194 *natoms = MOLFILE_NUMATOMS_NONE;
00195 dx->nsets = 1;
00196
00197 dx->vol = new molfile_volumetric_t[1];
00198 strcpy(dx->vol[0].dataname, "DX map");
00199
00200
00201 for (int i=0; i<3; i++) {
00202 dx->vol[0].origin[i] = orig[i];
00203 dx->vol[0].xaxis[i] = xdelta[i] * ((xsize-1 > 0) ? (xsize-1) : 1);
00204 dx->vol[0].yaxis[i] = ydelta[i] * ((ysize-1 > 0) ? (ysize-1) : 1);
00205 dx->vol[0].zaxis[i] = zdelta[i] * ((zsize-1 > 0) ? (zsize-1) : 1);
00206 }
00207
00208 dx->vol[0].xsize = xsize;
00209 dx->vol[0].ysize = ysize;
00210 dx->vol[0].zsize = zsize;
00211
00212
00213 dx->vol[0].has_color = 0;
00214
00215 return dx;
00216 }
00217
00218 static int read_dx_metadata(void *v, int *nsets,
00219 molfile_volumetric_t **metadata) {
00220 dx_t *dx = (dx_t *)v;
00221 *nsets = dx->nsets;
00222 *metadata = dx->vol;
00223
00224 return MOLFILE_SUCCESS;
00225 }
00226
00227 static int read_binary_dx_data(dx_t *dx, int set, float *datablock) {
00228
00229 int i, j, k;
00230 int xsize = dx->vol[0].xsize;
00231 int ysize = dx->vol[0].ysize;
00232 int zsize = dx->vol[0].zsize;
00233 int xysize = xsize * ysize;
00234 size_t total = xysize * zsize;
00235 float *tmp = (float *)malloc(total*sizeof(float));
00236 if (fread(tmp, sizeof(float), total, dx->fd) != total) {
00237 vmdcon_printf(VMDCON_ERROR, "dxplugin) Failed to read %d binary floats\n", total);
00238 free(tmp);
00239 return MOLFILE_ERROR;
00240 }
00241
00242 int ind = 0;
00243 for (i=0; i<xsize; i++) {
00244 for (j=0; j<ysize; j++) {
00245 for (k=0; k<zsize; k++) {
00246 datablock[k * xysize + j*xsize + i] = tmp[ind++];
00247 }
00248 }
00249 }
00250 free( tmp );
00251 return MOLFILE_SUCCESS;
00252 }
00253
00254 static int read_dx_data(void *v, int set, float *datablock,
00255 float *colorblock) {
00256 dx_t *dx = (dx_t *)v;
00257 FILE *fd = dx->fd;
00258 char inbuf[LINESIZE];
00259 char *p;
00260 float grid;
00261 int x, y, z, xsize, ysize, zsize, xysize, count, total, i, line;
00262
00263 if (dx->isBinary)
00264 return read_binary_dx_data(dx, set, datablock);
00265
00266 xsize = dx->vol[0].xsize;
00267 ysize = dx->vol[0].ysize;
00268 zsize = dx->vol[0].zsize;
00269 xysize = xsize * ysize;
00270 total = xysize * zsize;
00271
00272
00273 x = y = z = line = 0;
00274 for (count = 0; count < total;) {
00275 ++line;
00276 p=dxgets(inbuf, LINESIZE, fd);
00277 if (p == NULL) {
00278 vmdcon_printf(VMDCON_ERROR, "dxplugin) Error reading grid data.\n");
00279 vmdcon_printf(VMDCON_ERROR, "dxplugin) line: %d. item: %d/%d. last data: %s\n",
00280 line, count, total, inbuf);
00281 return MOLFILE_ERROR;
00282 }
00283
00284
00285 while (*p != '\n' && *p != '\0') {
00286
00287
00288 while (*p != '\0' && (*p == ' ' || *p == '\t' || *p == '\n')) ++p;
00289 i = sscanf(p, "%e", &grid);
00290 if (i < 0) break;
00291
00292
00293 if (i == 0) {
00294 vmdcon_printf(VMDCON_ERROR, "dxplugin) Error parsing grid data.\n");
00295 vmdcon_printf(VMDCON_ERROR, "dxplugin) line: %d. item: %d/%d. data %s\n",
00296 line, count, total, p);
00297 return MOLFILE_ERROR;
00298 }
00299
00300
00301 if (i == 1) {
00302 ++count;
00303 datablock[x + y*xsize + z*xysize] = grid;
00304 z++;
00305 if (z >= zsize) {
00306 z = 0; y++;
00307 if (y >= ysize) {
00308 y = 0; x++;
00309 }
00310 }
00311 }
00312
00313
00314 while (*p != '\0' && *p != ' ' && *p != '\t' && *p != '\n') ++p;
00315 }
00316 }
00317
00318 char dxname[256];
00319 while (dxgets(inbuf, LINESIZE, dx->fd)) {
00320 if (sscanf(inbuf, "object \"%[^\"]\" class field", dxname) == 1) {
00321
00322 strcpy(dx->vol[0].dataname, dxname);
00323 break;
00324 }
00325 }
00326
00327 return MOLFILE_SUCCESS;
00328 }
00329
00330 static void close_dx_read(void *v) {
00331 dx_t *dx = (dx_t *)v;
00332
00333 fclose(dx->fd);
00334 if (dx->vol != NULL)
00335 delete [] dx->vol;
00336 delete dx;
00337 }
00338
00339 static void *open_dx_write(const char *filepath, const char *filetype,
00340 int natoms) {
00341
00342 FILE *fd = fopen(filepath, "wb");
00343 if (!fd) {
00344 vmdcon_printf(VMDCON_ERROR,
00345 "dxplugin) Could not open path '%s' for writing.\n",
00346 filepath);
00347 }
00348 return fd;
00349 }
00350
00351 static void close_dx_write(void *v) {
00352 if (v) {
00353 fclose((FILE *)v);
00354 }
00355 }
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372 static int write_dx_data(void *v, molfile_volumetric_t *metadata,
00373 float *datablock, float *colorblock) {
00374
00375 int i, j, k, count;
00376 FILE *fd = (FILE *)v;
00377 const int xsize = metadata->xsize;
00378 const int ysize = metadata->ysize;
00379 const int zsize = metadata->zsize;
00380 const int xysize = xsize * ysize;
00381 const int total = xysize * zsize;
00382
00383 double xdelta[3], ydelta[3], zdelta[3];
00384 for (i=0; i<3; i++) {
00385 xdelta[i] = metadata->xaxis[i]/(xsize - 1);
00386 ydelta[i] = metadata->yaxis[i]/(ysize - 1);
00387 zdelta[i] = metadata->zaxis[i]/(zsize - 1);
00388 }
00389
00390 fprintf(fd, "# Data from VMD\n");
00391 fprintf(fd, "# %s\n", metadata->dataname);
00392 fprintf(fd, "object 1 class gridpositions counts %d %d %d\n",
00393 xsize, ysize, zsize);
00394 fprintf(fd, "origin %g %g %g\n",
00395 metadata->origin[0], metadata->origin[1], metadata->origin[2]);
00396 fprintf(fd, "delta %g %g %g\n",
00397 xdelta[0], xdelta[1], xdelta[2]);
00398 fprintf(fd, "delta %g %g %g\n",
00399 ydelta[0], ydelta[1], ydelta[2]);
00400 fprintf(fd, "delta %g %g %g\n",
00401 zdelta[0], zdelta[1], zdelta[2]);
00402 fprintf(fd, "object 2 class gridconnections counts %d %d %d\n",
00403 xsize, ysize, zsize);
00404
00405 int useBinary = (getenv("VMDBINARYDX") != NULL);
00406 fprintf(fd, "object 3 class array type double rank 0 items %d %sdata follows\n", total, useBinary ? "binary " : "");
00407 count = 0;
00408 for (i=0; i<xsize; i++) {
00409 for (j=0; j<ysize; j++) {
00410 for (k=0; k<zsize; k++) {
00411 if (useBinary) {
00412 fwrite(datablock + k*xysize + j*xsize + i, sizeof(float),
00413 1, fd);
00414 } else {
00415 fprintf(fd, "%g ", datablock[k*xysize + j*xsize + i]);
00416 if (++count == 3) {
00417 fprintf(fd, "\n");
00418 count = 0;
00419 }
00420 }
00421 }
00422 }
00423 }
00424 if (!useBinary && count)
00425 fprintf(fd, "\n");
00426
00427
00428
00429
00430 char *squotes = new char[strlen(metadata->dataname)+1];
00431 strcpy(squotes, metadata->dataname);
00432 char *s = squotes;
00433
00434 while(1) {
00435 s=strchr(s, '"');
00436 if (s) {
00437 *s = '\'';
00438 } else {
00439 break;
00440 }
00441 }
00442
00443
00444 fprintf(fd, "object \"%s\" class field\n", squotes);
00445 delete [] squotes;
00446
00447 fflush(fd);
00448 return MOLFILE_SUCCESS;
00449 }
00450
00451
00452
00453
00454
00455 VMDPLUGIN_API int VMDPLUGIN_init(void) {
00456 memset(&plugin, 0, sizeof(molfile_plugin_t));
00457 plugin.abiversion = vmdplugin_ABIVERSION;
00458 plugin.type = MOLFILE_PLUGIN_TYPE;
00459 plugin.name = "dx";
00460 plugin.prettyname = "DX";
00461 plugin.author = "Eamon Caddigan, Justin Gullingsrud, John Stone, Leonardo Trabuco";
00462 plugin.majorv = 1;
00463 plugin.minorv = 9;
00464 plugin.is_reentrant = VMDPLUGIN_THREADUNSAFE;
00465 plugin.filename_extension = "dx";
00466 plugin.open_file_read = open_dx_read;
00467 plugin.read_volumetric_metadata = read_dx_metadata;
00468 plugin.read_volumetric_data = read_dx_data;
00469 plugin.close_file_read = close_dx_read;
00470 #if vmdplugin_ABIVERSION > 9
00471 plugin.open_file_write = open_dx_write;
00472 plugin.write_volumetric_data = write_dx_data;
00473 plugin.close_file_write = close_dx_write;
00474 #endif
00475 return VMDPLUGIN_SUCCESS;
00476 }
00477
00478 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00479 (*cb)(v, (vmdplugin_t *)&plugin);
00480 return VMDPLUGIN_SUCCESS;
00481 }
00482
00483 VMDPLUGIN_API int VMDPLUGIN_fini(void) { return VMDPLUGIN_SUCCESS; }
00484