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

pltplugin.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: pltplugin.C,v $
00013  *      $Author: johns $       $Locker:  $             $State: Exp $
00014  *      $Revision: 1.14 $       $Date: 2016/11/28 05:01:54 $
00015  *
00016  ***************************************************************************/
00017 
00018 /* 
00019  * plt format electron density maps from gOpenMol.
00020  *
00021  * More info for format can be found at 
00022  * <http://www.csc.fi/gopenmol/developers/plt_format.phtml>
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   // File header data:
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   // Integer header info: rank (always 3), surface type, z length, y length, 
00063   // x length.
00064   fread(intHeader, sizeof(int), 5, fd);
00065   if (intHeader[0] != 3) {
00066     // check if the bytes need to be swapped
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   // Float header info: z min, z max, y min, y max, xmin, x max.
00077   fread(floatHeader, sizeof(float), 6, fd);
00078   if (swap)
00079     swap4_aligned(floatHeader, 6);
00080 
00081   // Allocate and initialize the plt structure
00082   plt = new plt_t;
00083   plt->fd = fd;
00084   plt->vol = NULL;
00085   *natoms = MOLFILE_NUMATOMS_NONE;
00086   plt->nsets = 1; // this file contains only one data set
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   // Best guesses for unit cell information, as none is included in the plt
00093   // file format.
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   // Read the densities. Order for file is x fast, y medium, z slow
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  * Initialization stuff here
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 

Generated on Tue Jan 21 02:56:20 2020 for VMD Plugins (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002