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 #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 #include "molfile_plugin.h"
00037 #include "endianswap.h"
00038
00039 typedef struct {
00040 FILE *fd;
00041 int nsets;
00042 int swap;
00043 molfile_volumetric_t *vol;
00044 } plt_t;
00045
00046
00047 static void *open_plt_read(const char *filepath, const char *filetype,
00048 int *natoms) {
00049 FILE *fd;
00050 plt_t *plt;
00051 int swap=0;
00052
00053 int intHeader[5];
00054 float floatHeader[6];
00055
00056 fd = fopen(filepath, "rb");
00057 if (!fd) {
00058 fprintf(stderr, "pltplugin) Error opening file.\n");
00059 return NULL;
00060 }
00061
00062
00063
00064 fread(intHeader, sizeof(int), 5, fd);
00065 if (intHeader[0] != 3) {
00066
00067 swap4_aligned(intHeader, 5);
00068 if (intHeader[0] == 3)
00069 swap = 1;
00070 else {
00071 fprintf(stderr, "pltplugin) Incorrect header.\n");
00072 return NULL;
00073 }
00074 }
00075
00076
00077 fread(floatHeader, sizeof(float), 6, fd);
00078 if (swap)
00079 swap4_aligned(floatHeader, 6);
00080
00081
00082 plt = new plt_t;
00083 plt->fd = fd;
00084 plt->vol = NULL;
00085 *natoms = MOLFILE_NUMATOMS_NONE;
00086 plt->nsets = 1;
00087 plt->swap = swap;
00088
00089 plt->vol = new molfile_volumetric_t[1];
00090 strcpy(plt->vol[0].dataname, "PLT Electron Density Map");
00091
00092
00093
00094 plt->vol[0].origin[0] = floatHeader[4];
00095 plt->vol[0].origin[1] = floatHeader[2];
00096 plt->vol[0].origin[2] = floatHeader[0];
00097
00098 plt->vol[0].xaxis[0] = floatHeader[5] - floatHeader[4];
00099 plt->vol[0].xaxis[1] = 0;
00100 plt->vol[0].xaxis[2] = 0;
00101
00102 plt->vol[0].yaxis[0] = 0;
00103 plt->vol[0].yaxis[1] = floatHeader[3] - floatHeader[2];
00104 plt->vol[0].yaxis[2] = 0;
00105
00106 plt->vol[0].zaxis[0] = 0;
00107 plt->vol[0].zaxis[1] = 0;
00108 plt->vol[0].zaxis[2] = floatHeader[1] - floatHeader[0];
00109
00110 plt->vol[0].xsize = intHeader[4];
00111 plt->vol[0].ysize = intHeader[3];
00112 plt->vol[0].zsize = intHeader[2];
00113
00114 plt->vol[0].has_color = 0;
00115
00116 return plt;
00117 }
00118
00119 static int read_plt_metadata(void *v, int *nsets,
00120 molfile_volumetric_t **metadata) {
00121 plt_t *plt = (plt_t *)v;
00122 *nsets = plt->nsets;
00123 *metadata = plt->vol;
00124
00125 return MOLFILE_SUCCESS;
00126 }
00127
00128 static int read_plt_data(void *v, int set, float *datablock,
00129 float *colorblock) {
00130 plt_t *plt = (plt_t *)v;
00131 int swap, ndata;
00132 FILE *fd = plt->fd;
00133
00134 swap = plt->swap;
00135 ndata = plt->vol[0].xsize * plt->vol[0].ysize * plt->vol[0].zsize;
00136
00137
00138 if ( fread(datablock, sizeof(float), ndata, fd) != ndata ) {
00139 fprintf(stderr, "pltplugin) Error reading data, not enough values read.\n");
00140 return MOLFILE_ERROR;
00141 }
00142
00143 if (swap)
00144 swap4_aligned(datablock, ndata);
00145
00146 return MOLFILE_SUCCESS;
00147 }
00148
00149 static void close_plt_read(void *v) {
00150 plt_t *plt = (plt_t *)v;
00151
00152 fclose(plt->fd);
00153 if (plt->vol != NULL)
00154 delete [] plt->vol;
00155 delete plt;
00156 }
00157
00158
00159
00160
00161 static molfile_plugin_t plugin;
00162
00163 VMDPLUGIN_API int VMDPLUGIN_init(void) {
00164 memset(&plugin, 0, sizeof(molfile_plugin_t));
00165 plugin.abiversion = vmdplugin_ABIVERSION;
00166 plugin.type = MOLFILE_PLUGIN_TYPE;
00167 plugin.name = "plt";
00168 plugin.prettyname = "gOpenmol plt";
00169 plugin.author = "Eamon Caddigan";
00170 plugin.majorv = 0;
00171 plugin.minorv = 4;
00172 plugin.is_reentrant = VMDPLUGIN_THREADSAFE;
00173 plugin.filename_extension = "plt";
00174 plugin.open_file_read = open_plt_read;
00175 plugin.read_volumetric_metadata = read_plt_metadata;
00176 plugin.read_volumetric_data = read_plt_data;
00177 plugin.close_file_read = close_plt_read;
00178 return VMDPLUGIN_SUCCESS;
00179 }
00180
00181 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00182 (*cb)(v, (vmdplugin_t *)&plugin);
00183 return VMDPLUGIN_SUCCESS;
00184 }
00185
00186 VMDPLUGIN_API int VMDPLUGIN_fini(void) { return VMDPLUGIN_SUCCESS; }
00187