source: trunk/exports/G2export_map.py @ 3011

Last change on this file since 3011 was 3000, checked in by toby, 4 years ago

make the two frame version the trunk as we hit version 3000

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