source: trunk/exports/G2export_shelx.py @ 1115

Last change on this file since 1115 was 1115, checked in by toby, 9 years ago

rework exports for new types

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 4.8 KB
RevLine 
[1102]1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3########### SVN repository information ###################
4# $Date: 2013-10-18 21:00:32 +0000 (Fri, 18 Oct 2013) $
5# $Author: toby $
6# $Revision: 1115 $
7# $URL: trunk/exports/G2export_shelx.py $
8# $Id: G2export_shelx.py 1115 2013-10-18 21:00:32Z toby $
9########### SVN repository information ###################
10'''Code to export coordinates in SHELX format, as best as I can makes sense of it
11'''
12import os.path
13import GSASIIpath
14GSASIIpath.SetVersionNumber("$Revision: 1115 $")
15import GSASIIIO as G2IO
16import GSASIIstrIO as G2stIO
17import GSASIIlattice as G2lat
18import GSASIIspc as G2spc
19
20class ExportPhaseShelx(G2IO.ExportBaseclass):
21    '''Used to create a SHELX .ins file for a phase
22
23    :param wx.Frame G2frame: reference to main GSAS-II frame
24    '''
25    def __init__(self,G2frame):
26        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
27            G2frame=G2frame,
[1115]28            formatName = 'SHELX .ins',
[1102]29            extension='.ins',
30            longFormatName = 'Export phase as SHELX .ins file'
31            )
32        self.exporttype = ['phase']
33        self.multiple = True
34
35    def Exporter(self,event=None):
36        '''Export as a SHELX .ins file
37        '''
38        import re
39        # the export process starts here
[1115]40        self.InitExport(event)
[1102]41        # load all of the tree into a set of dicts
42        self.loadTree()
43        # create a dict with refined values and their uncertainties
44        self.loadParmDict()
[1115]45        if self.ExportSelect(  # set export parameters
46            AskFile=True): return 
[1102]47        for phasenam in self.phasenam:
48            phasedict = self.Phases[phasenam] # pointer to current phase info           
49            i = self.Phases[phasenam]['pId']
50            if len(self.phasenam) > 1: # if more than one filename is included, add a phase #
51                nam,ext = os.path.splitext(self.filename)
52                fil = nam+"_"+str(i)+ext
53            else:
54                fil = self.filename
55            fp = self.OpenFile(fil)
56            # title line
57            self.Write("TITL from "+str(self.G2frame.GSASprojectfile)+", phase "+str(phasenam))
[1115]58            # get & write cell parameters
59            cell,sig = self.GetCell(phasenam)
60            self.Write("CELL 0.5 {:.5f} {:.5f} {:.5f} {:.3f} {:.3f} {:.3f}".format(*cell[:6]))
[1102]61            # Shelx lattice number
62            lattnum = {'P':1,'I':2,'R':2,'F':3,'A':4,'B':5,'C':6}.get(phasedict['General']['SGData']['SGLatt'],0)
63            if not phasedict['General']['SGData']['SGInv']: lattnum *= -1
64            self.Write("LATT "+str(lattnum))
65            # generate symmetry operations not including centering and center of symmetry
66            for M,T in phasedict['General']['SGData']['SGOps']:
67                sym = G2spc.MT2text(M,T).lower().replace(" ,",", ")
68                self.Write('SYMM '+G2IO.trim(sym))
69            # scan through atom types, count the number of times that each element occurs
70            AtomsList = self.GetAtoms(phasenam)
71            maxmult = 0
72            elemtypes = {}
73            for lbl,typ,mult,xyz,td in AtomsList:
74                maxmult = max(maxmult,mult)
75                typ = re.search('[A-Za-z]+',typ).group(0)
76                typ = typ[0:2]
77                typ = typ[0].upper()+typ[1:].lower()
78                if elemtypes.get(typ) is None:
79                    elemtypes[typ] = 1
80                else:
81                    elemtypes[typ] += 1
82            # create scattering factor record
83            s = "SFAC"
84            elemlist = sorted(elemtypes)
85            for elem in elemlist:
86                s += " " + elem
87            self.Write(s)
88            # handle atom records
89            count = {}
90            for lbl,typ,mult,xyz,td in AtomsList:
91                typ = re.search('[A-Za-z]+',typ).group(0)
92                typ = typ[0:2]
93                typ = typ[0].upper()+typ[1:].lower()
94                if count.get(typ) is None:
95                    count[typ] = 0
96                else:
97                    count[typ] += 1
98                # make a unique <=4 character label, if possible
99                if elemtypes[typ] <= 99:
100                    lbl = "{:s}{:d}".format(typ,count[typ])
101                else: # more than 99 atoms, use hexadecimal notation
102                    lbl = typ + "{:X}".format(count[typ])[-2:]
103                sfnum = elemlist.index(typ)+1 # element number in scattering factor list
104                l = lbl+" "+str(sfnum)
105                l += " {:.5f} {:.5f} {:.5f}".format(*[x[0] for x in xyz[:3]])
106                if mult == maxmult:
107                    m = 1
108                else:
109                    m = 1.*mult/maxmult
110                if xyz[3][0] == 1: # frac
111                    occ = 10 + m
112                else:
113                    occ = m * xyz[3][0]
114                l += " {:.3f}".format(occ)
115                for val,sig in td:
116                    l += " {:.3f}".format(val)                   
117                self.Write(l)
118            self.CloseFile()
119            print('Phase '+str(phasenam)+' written to file '+str(fil))
Note: See TracBrowser for help on using the repository browser.