source: trunk/exports/G2export_map.py @ 2567

Last change on this file since 2567 was 1261, checked in by toby, 8 years ago

reorg exports to implement directory selection

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 5.7 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3########### SVN repository information ###################
4# $Date: 2014-03-24 22:22:41 +0000 (Mon, 24 Mar 2014) $
5# $Author: vondreele $
6# $Revision: 1261 $
7# $URL: trunk/exports/G2export_map.py $
8# $Id: G2export_map.py 1261 2014-03-24 22:22:41Z vondreele $
9########### SVN repository information ###################
10'''
11*Module G2export_map: Map export*
12-------------------------------------------
13
14Code to write Fourier/Charge-Flip atomic density maps out in formats that
15can be read by external programs. At present a GSAS format
16that is supported by FOX and DrawXTL
17(:class:`ExportMapASCII`) and the CCP4 format that
18is used by COOT (:class:`ExportMapCCP4`) are implemented.
19'''
20import os
21import GSASIIpath
22import numpy as np
23GSASIIpath.SetVersionNumber("$Revision: 1261 $")
24import GSASIIIO as G2IO
25#import GSASIIgrid as G2gd
26#import GSASIIstrIO as G2stIO
27#import GSASIImath as G2mth
28#import GSASIIlattice as G2lat
29#import GSASIIspc as G2spc
30#import GSASIIphsGUI as G2pg
31#import GSASIIstrMain as G2stMn
32
33class ExportMapASCII(G2IO.ExportBaseclass):
34    '''Used to create a text file for a phase
35
36    :param wx.Frame G2frame: reference to main GSAS-II frame
37    '''
38    def __init__(self,G2frame):
39        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
40            G2frame=G2frame,
41            formatName = 'FOX/DrawXTL file',
42            extension='.grd',
43            longFormatName = 'Export map as text (.grd) file'
44            )
45        self.exporttype = ['map']
46        self.multiple = False 
47
48    def Exporter(self,event=None):
49        '''Export a map as a text file
50        '''
51        # the export process starts here
52        self.InitExport(event)
53        # load all of the tree into a set of dicts
54        self.loadTree()
55        if self.ExportSelect(): return  # set export parameters, get file name
56        filename = self.filename
57        for phasenam in self.phasenam:
58            phasedict = self.Phases[phasenam] # pointer to current phase info           
59            rho = phasedict['General']['Map'].get('rho',[])
60            if not len(rho):
61                print "There is no map for phase "+str(phasenam)
62                continue
63            if len(self.phasenam) > 1: # if more than one filename is written, add a phase # -- not in use yet
64                i = self.Phases[phasenam]['pId']
65                self.filename = os.path.splitext(filename)[1] + "_" + mapData['MapType'] + str(i) + self.extension
66            self.OpenFile()
67            self.Write("Map of Phase "+str(phasenam)+" from "+str(self.G2frame.GSASprojectfile))
68            # get cell parameters & print them
69            cellList,cellSig = self.GetCell(phasenam)
70            fmt = 3*" {:9.5f}" + 3*" {:9.3f}"
71            self.Write(fmt.format(*cellList[:6]))
72            nx,ny,nz = rho.shape
73            self.Write(" {:3d} {:3d} {:3d}".format(nx,ny,nz))
74            for i in range(nx):
75                for j in range(ny):
76                    for k in range(nz):
77                        self.Write(str(rho[i,j,k]))
78            self.CloseFile()
79            print('map from Phase '+str(phasenam)+' written to file '+str(self.fullpath))
80
81class ExportMapCCP4(G2IO.ExportBaseclass):
82    '''Used to create a text file for a phase
83
84    :param wx.Frame G2frame: reference to main GSAS-II frame
85    '''
86    def __init__(self,G2frame):
87        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
88            G2frame=G2frame,
89            formatName = 'CCP4 map file',
90            extension='.map',
91            longFormatName = 'Export CCP4 .map file'
92            )
93        self.exporttype = ['map']
94        self.multiple = False 
95
96    # Tools for file writing.
97    def Write(self,data,dtype):
98        import struct
99        '''write a block of output
100
101        :param data: the data to be written.
102        '''
103        self.fp.write(struct.pack(dtype,data))
104       
105    def Exporter(self,event=None):
106        '''Export a map as a text file
107        '''
108        # the export process starts here
109        self.InitExport(event)
110        # load all of the tree into a set of dicts
111        self.loadTree()
112        if self.ExportSelect(): return  # set export parameters, get file name
113        filename = self.filename
114        for phasenam in self.phasenam:
115            phasedict = self.Phases[phasenam] # pointer to current phase info
116            mapData = phasedict['General']['Map']
117            rho = mapData.get('rho',[])
118           
119            if not len(rho):
120                print "There is no map for phase "+str(phasenam)
121                continue
122            if len(self.phasenam) > 1: # if more than one filename is written, add a phase # -- not in use yet
123                i = self.Phases[phasenam]['pId']
124                self.filename = os.path.splitext(filename)[1] + "_" + mapData['MapType'] + str(i) + self.extension
125            cell = phasedict['General']['Cell'][1:7]
126            nx,ny,nz = rho.shape
127            self.OpenFile(mode='wb')
128            for n in rho.shape: self.Write(n,'i')  #nX,nY,nZ
129            self.Write(2,'i')           #mode=2 float map
130            for i in [0,0,0]: self.Write(i,'i')    #1st position on x,y,z
131            for n in rho.shape: self.Write(n,'i')  #nX,nY,nZ
132            for c in cell: self.Write(c,'f')
133            for i in [1,2,3]: self.Write(i,'i')    #axes order = x,y,z
134            self.Write(np.min(rho),'f')
135            self.Write(np.max(rho),'f')
136            self.Write(np.mean(rho),'f')
137            self.Write(0,'i')
138            for i in range(24,53):
139                self.Write(0,'i')
140            for s in ['M','A','P',' ']: self.Write(s,'s')
141            self.Write(0x44410000,'i')
142            self.Write(np.std(rho),'f')
143            for i in range(56,257):
144                self.Write(0,'i')
145            for x in rho.flatten('F'):
146                self.Write(x,'f')
147            self.CloseFile()
148            print('map from Phase '+str(phasenam)+' written to file '+str(self.fullpath))
Note: See TracBrowser for help on using the repository browser.