source: trunk/imports/G2sfact.py @ 1359

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

start allowing reading of multiple HKLF files.
put an IndexAllIds? into UpdateConstraints?
edf image fixes
more places in Pawley d-min
format of penalty fxn chi2
allow one Bank selection set to be used for multiple files
new reader for TOF HKLF2 data - nonworking just now

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 7.3 KB
Line 
1# -*- coding: utf-8 -*-
2########### SVN repository information ###################
3# $Date: 2014-05-21 17:59:59 +0000 (Wed, 21 May 2014) $
4# $Author: vondreele $
5# $Revision: 1359 $
6# $URL: trunk/imports/G2sfact.py $
7# $Id: G2sfact.py 1359 2014-05-21 17:59:59Z vondreele $
8########### SVN repository information ###################
9'''
10*Module G2sfact: simple HKL import*
11-----------------------------------
12Read structure factors from a simple hkl file. Two routines are
13provided to read from files containing F or F\ :sup:`2` values.
14
15'''
16import sys
17import numpy as np
18import GSASIIIO as G2IO
19import GSASIIpath
20GSASIIpath.SetVersionNumber("$Revision: 1359 $")
21
22def ColumnValidator(parent, filepointer):
23    'Validate a file to check that it contains columns of numbers'
24    l = S = filepointer.readline()
25    line = 1
26    while '#' in S[0]:        #get past comments, if any
27        l = filepointer.readline()       
28        line += 1
29    for i in range(10): # scan a few lines
30        S = l.split()
31        if len(S) != 5:
32            parent.errors = 'line '+str(line)+': invalid input\n'+l
33            return False
34        for v in S:
35            try:
36                float(v)
37            except ValueError:
38                parent.errors = 'line '+str(line)+': string found where a number is expected\n'+l
39                return False           
40        l = filepointer.readline()
41        line += 1
42    return True
43
44
45class HKLF_ReaderClass(G2IO.ImportStructFactor):
46    'Routines to import F, sig(F) reflections from a HKLF file'
47    def __init__(self):
48        super(self.__class__,self).__init__( # fancy way to self-reference
49            extensionlist=('.hkl','.HKL'),
50            strictExtension=False,
51            formatName = 'HKL containing F',
52            longFormatName = 'Simple [hkl, Fo, sig(Fo)] Structure factor text file'
53            )
54
55    def ContentsValidator(self, filepointer):
56        'Make sure file contains the expected columns on numbers'
57        return ColumnValidator(self, filepointer)
58
59    def Reader(self,filename,filepointer, ParentFrame=None, **unused):
60        'Read the file'
61        try:
62            for line,S in enumerate(filepointer):
63                self.errors = '  Error reading line '+str(line+1)
64                if S[0] == '#': continue       #ignore comments, if any
65                h,k,l,Fo,sigFo = S.split()
66                h,k,l = [int(h),int(k),int(l)]
67                if not any([h,k,l]):
68                    break
69                Fo = float(Fo)
70                sigFo = float(sigFo)
71                # h,k,l,m,dsp,Fo2,sig,Fc2,Fot2,Fct2,phase,...
72                self.RefDict['RefList'].append([h,k,l,0,0,Fo**2,2.*Fo*sigFo,0,Fo**2,0,0,0])
73                #self.RefDict['FF'].append({}) # now done in OnImportSfact
74            self.errors = 'Error after reading reflections (unexpected!)'
75            self.RefDict['RefList'] = np.array(self.RefDict['RefList'])
76            self.UpdateControls(Type='Fosq',FcalcPresent=False) # set Fobs type & if Fcalc values are loaded
77            self.UpdateParameters(Type='SXC',Wave=None) # histogram type
78            return True
79        except Exception as detail:
80            self.errors += '\n  '+str(detail)
81            print '\n\n'+self.formatName+' read error: '+str(detail) # for testing
82            import traceback
83            traceback.print_exc(file=sys.stdout)
84            return False
85
86class HKLF2_ReaderClass(G2IO.ImportStructFactor):
87    'Routines to import F**2, sig(F**2) reflections from a HKLF file'
88    def __init__(self):
89        super(self.__class__,self).__init__( # fancy way to self-reference
90            extensionlist=('.hkl','.HKL'),
91            strictExtension=False,
92            formatName = u'HKL containing F\u00b2',
93            longFormatName = u'Simple [hkl, Fo\u00b2, sig(Fo\u00b2)] Structure factor text file'
94            )
95
96    def ContentsValidator(self, filepointer):
97        'Make sure file contains the expected columns on numbers'
98        return ColumnValidator(self, filepointer)
99
100    def Reader(self,filename,filepointer, ParentFrame=None, **unused):
101        'Read the file'
102        try:
103            for line,S in enumerate(filepointer):
104                self.errors = '  Error reading line '+str(line+1)
105                if S[0] == '#': continue       #ignore comments, if any
106                h,k,l,Fo,sigFo = S.split()
107                h,k,l = [int(h),int(k),int(l)]
108                if not any([h,k,l]):
109                    break
110                Fo = float(Fo)
111                sigFo = float(sigFo)
112                # h,k,l,m,dsp,Fo2,sig,Fc2,Fot2,Fct2,phase,...
113                self.RefDict['RefList'].append([h,k,l,0,0,Fo,sigFo,0,Fo,0,0,0])
114                #self.RefDict['FF'].append({}) # now done in OnImportSfact
115            self.errors = 'Error after reading reflections (unexpected!)'
116            self.RefDict['RefList'] = np.array(self.RefDict['RefList'])
117            self.UpdateControls(Type='Fosq',FcalcPresent=False) # set Fobs type & if Fcalc values are loaded
118            self.UpdateParameters(Type='SXC',Wave=None) # histogram type
119            return True
120        except Exception as detail:
121            self.errors += '\n  '+str(detail)
122            print '\n\n'+self.formatName+' read error: '+str(detail) # for testing
123            import traceback
124            traceback.print_exc(file=sys.stdout)
125            return False
126
127class NT_HKLF2_ReaderClass(G2IO.ImportStructFactor):
128    'Routines to import neutron TOF F**2, sig(F**2) reflections from a HKLF file'
129    def __init__(self):
130        super(self.__class__,self).__init__( # fancy way to self-reference
131            extensionlist=('.hkl','.HKL'),
132            strictExtension=False,
133            formatName = u'Neutron TOF HKL containing F\u00b2',
134            longFormatName = u'Neutron TOF [hkl, Fo\u00b2, sig(Fo\u00b2),...] Structure factor text file'
135            )
136
137    def ContentsValidator(self, filepointer):
138        'Make sure file contains the expected columns on numbers'
139        return ColumnValidator(self, filepointer)
140
141    def Reader(self,filename,filepointer, ParentFrame=None, **unused):
142        'Read the file'
143        try:
144            oldNo = -1
145            Banks = []
146            for line,S in enumerate(filepointer):
147                if S[0] == '#': continue       #ignore comments, if any
148                bankNo = S.split()[5]
149                if bankNo != oldNo:
150                    Banks.append([])
151                   
152            for line,S in enumerate(filepointer):
153                self.errors = '  Error reading line '+str(line+1)
154                if S[0] == '#': continue       #ignore comments, if any
155                h,k,l,Fo,sigFo = S.split()
156                h,k,l = [int(h),int(k),int(l)]
157                if not any([h,k,l]):
158                    break
159                Fo = float(Fo)
160                sigFo = float(sigFo)
161                # h,k,l,m,dsp,Fo2,sig,Fc2,Fot2,Fct2,phase,...
162                self.RefDict['RefList'].append([h,k,l,0,0,Fo,sigFo,0,Fo,0,0,0])
163            self.errors = 'Error after reading reflections (unexpected!)'
164            self.RefDict['RefList'] = np.array(self.RefDict['RefList'])
165            self.UpdateControls(Type='Fosq',FcalcPresent=False) # set Fobs type & if Fcalc values are loaded
166            self.UpdateParameters(Type='SNT',Wave=None) # histogram type
167            return True
168        except Exception as detail:
169            self.errors += '\n  '+str(detail)
170            print '\n\n'+self.formatName+' read error: '+str(detail) # for testing
171            import traceback
172            traceback.print_exc(file=sys.stdout)
173            return False
174
Note: See TracBrowser for help on using the repository browser.