source: trunk/exports/G2export_map.py @ 1122

Last change on this file since 1122 was 1122, checked in by vondreele, 8 years ago

rename ASCIImap to just map
add export of ccp4 maps

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