source: trunk/imports/G2phase_INS.py @ 2285

Last change on this file since 2285 was 2285, checked in by vondreele, 7 years ago

add importer for Shelx .ins files
also one for old GSAS Pnn powder histogram files.

File size: 5.1 KB
Line 
1# -*- coding: utf-8 -*-
2########### SVN repository information ###################
3# $Date: 2015-04-27 13:22:06 -0500 (Mon, 27 Apr 2015) $
4# $Author: vondreele $
5# $Revision: 1812 $
6# $URL: https://subversion.xray.aps.anl.gov/pyGSAS/trunk/imports/G2phase_GPX.py $
7# $Id: G2phase_GPX.py 1812 2015-04-27 18:22:06Z vondreele $
8########### SVN repository information ###################
9'''
10*Module G2phase_INS: Import phase from SHELX INS file*
11--------------------------------------------------------
12
13Copies a phase from SHELX ins file into the
14current project.
15
16'''
17import sys
18import traceback
19import math
20import numpy as np
21import random as ran
22import GSASIIIO as G2IO
23import GSASIIspc as G2spc
24import GSASIIlattice as G2lat
25import GSASIIpath
26GSASIIpath.SetVersionNumber("$Revision: 1812 $")
27
28class PhaseReaderClass(G2IO.ImportPhase):
29    'Opens a .INS file and pulls out a selected phase'
30    def __init__(self):
31        super(self.__class__,self).__init__( # fancy way to say ImportPhase.__init__
32            extensionlist=('.ins','.INS'),
33            strictExtension=True,
34            formatName = 'SHELX ins',
35            longFormatName = 'SHELX input (*.ins) file import'
36            )
37       
38    def ContentsValidator(self, filepointer):
39        "Test if the ins file has a CELL record"
40        for i,l in enumerate(filepointer):
41            if l.startswith('CELL'):
42                break
43        else:
44            self.errors = 'no CELL record found'
45            self.errors = 'This is not a valid .ins file.'
46            return False
47        return True
48
49    def Reader(self,filename,filepointer, ParentFrame=None, **unused):
50        'Read a ins file using :meth:`ReadINSPhase`'
51        try:
52            self.Phase = self.ReadINSPhase(filename, ParentFrame)
53            return True
54        except Exception as detail:
55            self.errors += '\n  '+str(detail)
56            print 'INS read error:',detail # for testing
57            traceback.print_exc(file=sys.stdout)
58            return False
59
60    def ReadINSPhase(self,filename,parent=None):
61        '''Read a phase from a INS file.
62        '''
63        EightPiSq = 8.*math.pi**2
64        self.errors = 'Error opening file'
65        file = open(filename, 'Ur')
66        Phase = {}
67        Title = ''
68        Compnd = ''
69        Atoms = []
70        A = np.zeros(shape=(3,3))
71        S = file.readline()
72        line = 1
73        SGData = None
74        cell = None
75        numbs = [str(i) for i in range(10)]
76        while S:
77            if '!' in S:
78                S = S.split('!')[0]
79            self.errors = 'Error reading at line '+str(line)
80            Atom = []
81            Aindx = [ch in numbs for ch in S[:4]]   #False for all letters
82            if 'TITL' in S[:4]:
83                Title = S[4:72].strip()
84            elif 'CELL' in S[:4]:
85                abc = S[12:40].split()
86                angles = S[40:64].split()
87                cell=[float(abc[0]),float(abc[1]),float(abc[2]),
88                    float(angles[0]),float(angles[1]),float(angles[2])]
89                Volume = G2lat.calc_V(G2lat.cell2A(cell))
90                AA,AB = G2lat.cell2AB(cell)
91                SpGrp = 'P 1'
92                SGData = G2IO.SGData # P 1
93                self.warnings += '\nThe space group is not given in an ins file and has been set to "P 1".'
94                self.warnings += "Change this in phase's General tab."
95            elif S[:4] in 'SFAC':
96                aTypes = S[4:].split()
97            elif S[0] == 'Q':
98                pass
99            elif np.any(Aindx):   #this will find an atom record!
100                iNum = Aindx.index(True)
101                Atype = S[:iNum]
102                Aname = S[:4]
103                x,y,z = S[9:45].split()
104                XYZ = np.array([float(x),float(y),float(z)])
105                XYZ = np.where(np.abs(XYZ)<0.00001,0,XYZ)
106                SytSym,Mult = G2spc.SytSym(XYZ,SGData)
107                if '=' not in S:
108                    IA = 'I'
109                    Uiso = float(S[57:68])
110                    if Uiso < 0.:
111                        Uiso = 0.025
112                    Uij = [0. for i in range(6)]
113                else:
114                    IA = 'A'
115                    Uiso = 0.
116                    Ustr = S[57:78].split()
117                    S = file.readline()
118                    if '!' in S:
119                        S = S.split('!')[0]
120                    line += 1
121                    Ustr += S[6:51].split()
122                    Uij = [float(Ustr[i]) for i in range(6)]
123                Atom = [Aname,Atype,'',XYZ[0],XYZ[1],XYZ[2],1.0,SytSym,Mult,IA,Uiso]
124                Atom += Uij
125                Atom.append(ran.randint(0,sys.maxint))
126                Atoms.append(Atom)
127            S = file.readline()
128            line += 1
129        file.close()
130        self.errors = 'Error after read complete'
131        Phase = G2IO.SetNewPhase(Name='ShelX phase',SGData=SGData,cell=cell+[Volume,])
132        Phase['General']['Type'] = 'nuclear'
133        Phase['General']['AtomPtrs'] = [3,1,7,9]
134        Phase['Atoms'] = Atoms
135        return Phase
Note: See TracBrowser for help on using the repository browser.