source: trunk/exports/G2export_shelx.py @ 3011

Last change on this file since 3011 was 2819, checked in by vondreele, 5 years ago

fix printing of nonascii characters (e.g. "umlaut-o") in file names, etc. in exporters

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