source: trunk/GSASIIstrIO.py @ 2562

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

more work on CellEsds?

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 145.4 KB
Line 
1# -*- coding: utf-8 -*-
2########### SVN repository information ###################
3# $Date: 2016-12-06 18:52:55 +0000 (Tue, 06 Dec 2016) $
4# $Author: vondreele $
5# $Revision: 2562 $
6# $URL: trunk/GSASIIstrIO.py $
7# $Id: GSASIIstrIO.py 2562 2016-12-06 18:52:55Z vondreele $
8########### SVN repository information ###################
9'''
10*GSASIIstrIO: structure I/O routines*
11-------------------------------------
12
13'''
14import os
15import os.path as ospath
16import time
17import math
18import copy
19import cPickle
20import numpy as np
21import numpy.ma as ma
22import GSASIIpath
23GSASIIpath.SetVersionNumber("$Revision: 2562 $")
24import GSASIIElem as G2el
25import GSASIIlattice as G2lat
26import GSASIIspc as G2spc
27import GSASIIobj as G2obj
28import GSASIImapvars as G2mv
29import GSASIImath as G2mth
30
31sind = lambda x: np.sin(x*np.pi/180.)
32cosd = lambda x: np.cos(x*np.pi/180.)
33tand = lambda x: np.tan(x*np.pi/180.)
34asind = lambda x: 180.*np.arcsin(x)/np.pi
35acosd = lambda x: 180.*np.arccos(x)/np.pi
36atan2d = lambda y,x: 180.*np.arctan2(y,x)/np.pi
37   
38ateln2 = 8.0*math.log(2.0)
39
40def GetControls(GPXfile):
41    ''' Returns dictionary of control items found in GSASII gpx file
42
43    :param str GPXfile: full .gpx file name
44    :return: dictionary of control items
45    '''
46    Controls = copy.copy(G2obj.DefaultControls)
47    fl = open(GPXfile,'rb')
48    while True:
49        try:
50            data = cPickle.load(fl)
51        except EOFError:
52            break
53        datum = data[0]
54        if datum[0] == 'Controls':
55            Controls.update(datum[1])
56    fl.close()
57    return Controls
58   
59def GetConstraints(GPXfile):
60    '''Read the constraints from the GPX file and interpret them
61
62    called in :func:`ReadCheckConstraints`, :func:`GSASIIstrMain.Refine`
63    and :func:`GSASIIstrMain.SeqRefine`.
64    '''
65    fl = open(GPXfile,'rb')
66    while True:
67        try:
68            data = cPickle.load(fl)
69        except EOFError:
70            break
71        datum = data[0]
72        if datum[0] == 'Constraints':
73            constList = []
74            for item in datum[1]:
75                if item.startswith('_'): continue
76                constList += datum[1][item]
77            fl.close()
78            constDict,fixedList,ignored = ProcessConstraints(constList)
79            if ignored:
80                print ignored,'Constraints were rejected. Was a constrained phase, histogram or atom deleted?'
81            return constDict,fixedList
82    fl.close()
83    raise Exception,"No constraints in GPX file"
84   
85def ProcessConstraints(constList):
86    """Interpret the constraints in the constList input into a dictionary, etc.
87    All :class:`GSASIIobj.G2VarObj` objects are mapped to the appropriate
88    phase/hist/atoms based on the object internals (random Ids). If this can't be
89    done (if a phase has been deleted, etc.), the variable is ignored.
90    If the constraint cannot be used due to too many dropped variables,
91    it is counted as ignored.
92   
93    :param list constList: a list of lists where each item in the outer list
94      specifies a constraint of some form, as described in the :mod:`GSASIIobj`
95      :ref:`Constraint definition <Constraint_definitions_table>`.
96
97    :returns:  a tuple of (constDict,fixedList,ignored) where:
98     
99      * constDict (list of dicts) contains the constraint relationships
100      * fixedList (list) contains the fixed values for each type
101        of constraint.
102      * ignored (int) counts the number of invalid constraint items
103        (should always be zero!)
104    """
105    constDict = []
106    fixedList = []
107    ignored = 0
108    for item in constList:
109        if item[-1] == 'h':
110            # process a hold
111            fixedList.append('0')
112            var = str(item[0][1])
113            if '?' not in var:
114                constDict.append({var:0.0})
115            else:
116                ignored += 1
117        elif item[-1] == 'f':
118            # process a new variable
119            fixedList.append(None)
120            D = {}
121            varyFlag = item[-2]
122            varname = item[-3]
123            for term in item[:-3]:
124                var = str(term[1])
125                if '?' not in var:
126                    D[var] = term[0]
127            if len(D) > 1:
128                # add extra dict terms for input variable name and vary flag
129                if varname is not None:                   
130                    if varname.startswith('::'):
131                        varname = varname[2:].replace(':',';')
132                    else:
133                        varname = varname.replace(':',';')
134                    D['_name'] = '::' + varname
135                D['_vary'] = varyFlag == True # force to bool
136                constDict.append(D)
137            else:
138                ignored += 1
139            #constFlag[-1] = ['Vary']
140        elif item[-1] == 'c': 
141            # process a contraint relationship
142            D = {}
143            for term in item[:-3]:
144                var = str(term[1])
145                if '?' not in var:
146                    D[var] = term[0]
147            if len(D) >= 1:
148                fixedList.append(str(item[-3]))
149                constDict.append(D)
150            else:
151                ignored += 1
152        elif item[-1] == 'e':
153            # process an equivalence
154            firstmult = None
155            eqlist = []
156            for term in item[:-3]:
157                if term[0] == 0: term[0] = 1.0
158                var = str(term[1])
159                if '?' in var: continue
160                if firstmult is None:
161                    firstmult = term[0]
162                    firstvar = var
163                else:
164                    eqlist.append([var,firstmult/term[0]])
165            if len(eqlist) > 0:
166                G2mv.StoreEquivalence(firstvar,eqlist)
167            else:
168                ignored += 1
169        else:
170            ignored += 1
171    return constDict,fixedList,ignored
172
173def ReadCheckConstraints(GPXfile):
174    '''Load constraints and related info and return any error or warning messages'''
175    # init constraints
176    G2mv.InitVars()   
177    # get variables
178    Histograms,Phases = GetUsedHistogramsAndPhases(GPXfile)
179    if not Phases:
180        return 'Error: No phases or no histograms in phases!',''
181    if not Histograms:
182        return 'Error: no diffraction data',''
183    constrDict,fixedList = GetConstraints(GPXfile) # load user constraints before internally generated ones
184    rigidbodyDict = GetRigidBodies(GPXfile)
185    rbIds = rigidbodyDict.get('RBIds',{'Vector':[],'Residue':[]})
186    rbVary,rbDict = GetRigidBodyModels(rigidbodyDict,Print=False)
187    Natoms,atomIndx,phaseVary,phaseDict,pawleyLookup,FFtables,BLtables,MFtables,maxSSwave = \
188        GetPhaseData(Phases,RestraintDict=None,rbIds=rbIds,Print=False) # generates atom symmetry constraints
189    hapVary,hapDict,controlDict = GetHistogramPhaseData(Phases,Histograms,Print=False)
190    histVary,histDict,controlDict = GetHistogramData(Histograms,Print=False)
191    varyList = rbVary+phaseVary+hapVary+histVary
192    errmsg, warnmsg = G2mv.CheckConstraints(varyList,constrDict,fixedList)
193    if errmsg:
194        # print some diagnostic info on the constraints
195        print('Error in constraints:\n'+errmsg+
196              '\nRefinement not possible due to conflict in constraints, see below:\n')
197        print G2mv.VarRemapShow(varyList,True)
198    return errmsg, warnmsg
199   
200def makeTwinFrConstr(Phases,Histograms,hapVary):
201    TwConstr = []
202    TwFixed = []
203    for Phase in Phases:
204        pId = Phases[Phase]['pId']
205        for Histogram in Phases[Phase]['Histograms']:
206            try:
207                hId = Histograms[Histogram]['hId']
208                phfx = '%d:%d:'%(pId,hId)
209                if phfx+'TwinFr:0' in hapVary:
210                    TwFixed.append('1.0')     #constraint value
211                    nTwin = len(Phases[Phase]['Histograms'][Histogram]['Twins'])
212                    TwConstr.append({phfx+'TwinFr:'+str(i):'1.0' for i in range(nTwin)})
213            except KeyError:    #unused histograms?
214                pass
215    return TwConstr,TwFixed   
216   
217def GetRestraints(GPXfile):
218    '''Read the restraints from the GPX file.
219    Throws an exception if not found in the .GPX file
220    '''
221    fl = open(GPXfile,'rb')
222    while True:
223        try:
224            data = cPickle.load(fl)
225        except EOFError:
226            break
227        datum = data[0]
228        if datum[0] == 'Restraints':
229            restraintDict = datum[1]
230    fl.close()
231    return restraintDict
232   
233def GetRigidBodies(GPXfile):
234    '''Read the rigid body models from the GPX file
235    '''
236    fl = open(GPXfile,'rb')
237    while True:
238        try:
239            data = cPickle.load(fl)
240        except EOFError:
241            break
242        datum = data[0]
243        if datum[0] == 'Rigid bodies':
244            rigidbodyDict = datum[1]
245    fl.close()
246    return rigidbodyDict
247       
248def GetFprime(controlDict,Histograms):
249    'Needs a doc string'
250    FFtables = controlDict['FFtables']
251    if not FFtables:
252        return
253    histoList = Histograms.keys()
254    histoList.sort()
255    for histogram in histoList:
256        if histogram[:4] in ['PWDR','HKLF']:
257            Histogram = Histograms[histogram]
258            hId = Histogram['hId']
259            hfx = ':%d:'%(hId)
260            if 'X' in controlDict[hfx+'histType']:
261                keV = controlDict[hfx+'keV']
262                for El in FFtables:
263                    Orbs = G2el.GetXsectionCoeff(El.split('+')[0].split('-')[0])
264                    FP,FPP,Mu = G2el.FPcalc(Orbs, keV)
265                    FFtables[El][hfx+'FP'] = FP
266                    FFtables[El][hfx+'FPP'] = FPP
267                   
268def PrintFprime(FFtables,pfx,pFile):
269    print >>pFile,'\n Resonant form factors:'
270    Elstr = ' Element:'
271    FPstr = " f'     :"
272    FPPstr = ' f"     :'
273    for El in FFtables:
274        Elstr += ' %8s'%(El)
275        FPstr += ' %8.3f'%(FFtables[El][pfx+'FP'])
276        FPPstr += ' %8.3f'%(FFtables[El][pfx+'FPP'])
277    print >>pFile,Elstr
278    print >>pFile,FPstr
279    print >>pFile,FPPstr
280           
281def GetPhaseNames(GPXfile):
282    ''' Returns a list of phase names found under 'Phases' in GSASII gpx file
283
284    :param str GPXfile: full .gpx file name
285    :return: list of phase names
286    '''
287    fl = open(GPXfile,'rb')
288    PhaseNames = []
289    while True:
290        try:
291            data = cPickle.load(fl)
292        except EOFError:
293            break
294        datum = data[0]
295        if 'Phases' == datum[0]:
296            for datus in data[1:]:
297                PhaseNames.append(datus[0])
298    fl.close()
299    return PhaseNames
300
301def GetAllPhaseData(GPXfile,PhaseName):
302    ''' Returns the entire dictionary for PhaseName from GSASII gpx file
303
304    :param str GPXfile: full .gpx file name
305    :param str PhaseName: phase name
306    :return: phase dictionary
307    '''       
308    fl = open(GPXfile,'rb')
309    while True:
310        try:
311            data = cPickle.load(fl)
312        except EOFError:
313            break
314        datum = data[0]
315        if 'Phases' == datum[0]:
316            for datus in data[1:]:
317                if datus[0] == PhaseName:
318                    break
319    fl.close()
320    return datus[1]
321   
322def GetHistograms(GPXfile,hNames):
323    """ Returns a dictionary of histograms found in GSASII gpx file
324
325    :param str GPXfile: full .gpx file name
326    :param str hNames: list of histogram names
327    :return: dictionary of histograms (types = PWDR & HKLF)
328
329    """
330    fl = open(GPXfile,'rb')
331    Histograms = {}
332    while True:
333        try:
334            data = cPickle.load(fl)
335        except EOFError:
336            break
337        datum = data[0]
338        hist = datum[0]
339        if hist in hNames:
340            if 'PWDR' in hist[:4]:
341                PWDRdata = {}
342                PWDRdata.update(datum[1][0])        #weight factor
343                PWDRdata['Data'] = ma.array(ma.getdata(datum[1][1]))          #masked powder data arrays/clear previous masks
344                PWDRdata[data[2][0]] = data[2][1]       #Limits & excluded regions (if any)
345                PWDRdata[data[3][0]] = data[3][1]       #Background
346                PWDRdata[data[4][0]] = data[4][1]       #Instrument parameters
347                PWDRdata[data[5][0]] = data[5][1]       #Sample parameters
348                try:
349                    PWDRdata[data[9][0]] = data[9][1]       #Reflection lists might be missing
350                except IndexError:
351                    PWDRdata['Reflection Lists'] = {}
352                PWDRdata['Residuals'] = {}
353   
354                Histograms[hist] = PWDRdata
355            elif 'HKLF' in hist[:4]:
356                HKLFdata = {}
357                HKLFdata.update(datum[1][0])        #weight factor
358#patch
359                if 'list' in str(type(datum[1][1])):
360                #if isinstance(datum[1][1],list):
361                    RefData = {'RefList':[],'FF':{}}
362                    for ref in datum[1][1]:
363                        RefData['RefList'].append(ref[:11]+[ref[13],])
364                    RefData['RefList'] = np.array(RefData['RefList'])
365                    datum[1][1] = RefData
366#end patch
367                datum[1][1]['FF'] = {}
368                HKLFdata['Data'] = datum[1][1]
369                HKLFdata[data[1][0]] = data[1][1]       #Instrument parameters
370                HKLFdata['Reflection Lists'] = None
371                HKLFdata['Residuals'] = {}
372                Histograms[hist] = HKLFdata           
373    fl.close()
374    return Histograms
375   
376def GetHistogramNames(GPXfile,hType):
377    """ Returns a list of histogram names found in GSASII gpx file
378
379    :param str GPXfile: full .gpx file name
380    :param str hType: list of histogram types
381    :return: list of histogram names (types = PWDR & HKLF)
382
383    """
384    fl = open(GPXfile,'rb')
385    HistogramNames = []
386    while True:
387        try:
388            data = cPickle.load(fl)
389        except EOFError:
390            break
391        datum = data[0]
392        if datum[0][:4] in hType:
393            HistogramNames.append(datum[0])
394    fl.close()
395    return HistogramNames
396   
397def GetUsedHistogramsAndPhases(GPXfile):
398    ''' Returns all histograms that are found in any phase
399    and any phase that uses a histogram. This also
400    assigns numbers to used phases and histograms by the
401    order they appear in the file.
402
403    :param str GPXfile: full .gpx file name
404    :returns: (Histograms,Phases)
405
406     * Histograms = dictionary of histograms as {name:data,...}
407     * Phases = dictionary of phases that use histograms
408
409    '''
410    phaseNames = GetPhaseNames(GPXfile)
411    histoList = GetHistogramNames(GPXfile,['PWDR','HKLF'])
412    allHistograms = GetHistograms(GPXfile,histoList)
413    phaseData = {}
414    for name in phaseNames: 
415        phaseData[name] =  GetAllPhaseData(GPXfile,name)
416    Histograms = {}
417    Phases = {}
418    for phase in phaseData:
419        Phase = phaseData[phase]
420        if Phase['General']['Type'] == 'faulted': continue      #don't use faulted phases!
421        if Phase['Histograms']:
422            if phase not in Phases:
423                pId = phaseNames.index(phase)
424                Phase['pId'] = pId
425                Phases[phase] = Phase
426            for hist in Phase['Histograms']:
427                if 'Use' not in Phase['Histograms'][hist]:      #patch
428                    Phase['Histograms'][hist]['Use'] = True         
429                if hist not in Histograms and Phase['Histograms'][hist]['Use']:
430                    try:
431                        Histograms[hist] = allHistograms[hist]
432                        hId = histoList.index(hist)
433                        Histograms[hist]['hId'] = hId
434                    except KeyError: # would happen if a referenced histogram were
435                        # renamed or deleted
436                        print('For phase "'+str(phase)+
437                              '" unresolved reference to histogram "'+str(hist)+'"')
438    G2obj.IndexAllIds(Histograms=Histograms,Phases=Phases)
439    return Histograms,Phases
440   
441def getBackupName(GPXfile,makeBack):
442    '''
443    Get the name for the backup .gpx file name
444   
445    :param str GPXfile: full .gpx file name
446    :param bool makeBack: if True the name of a new file is returned, if
447      False the name of the last file that exists is returned
448    :returns: the name of a backup file
449   
450    '''
451    GPXpath,GPXname = ospath.split(GPXfile)
452    if GPXpath == '': GPXpath = '.'
453    Name = ospath.splitext(GPXname)[0]
454    files = os.listdir(GPXpath)
455    last = 0
456    for name in files:
457        name = name.split('.')
458        if len(name) == 3 and name[0] == Name and 'bak' in name[1]:
459            if makeBack:
460                last = max(last,int(name[1].strip('bak'))+1)
461            else:
462                last = max(last,int(name[1].strip('bak')))
463    GPXback = ospath.join(GPXpath,ospath.splitext(GPXname)[0]+'.bak'+str(last)+'.gpx')
464    return GPXback   
465       
466def GPXBackup(GPXfile,makeBack=True):
467    '''
468    makes a backup of the current .gpx file (?)
469   
470    :param str GPXfile: full .gpx file name
471    :param bool makeBack: if True (default), the backup is written to
472      a new file; if False, the last backup is overwritten
473    :returns: the name of the backup file that was written
474    '''
475    import distutils.file_util as dfu
476    GPXback = getBackupName(GPXfile,makeBack)
477    tries = 0
478    while True:
479        try:
480            dfu.copy_file(GPXfile,GPXback)
481            break
482        except:
483            tries += 1
484            if tries > 10:
485                return GPXfile  #failed!
486            time.sleep(1)           #just wait a second!         
487    return GPXback
488
489def SetUsedHistogramsAndPhases(GPXfile,Histograms,Phases,RigidBodies,CovData,makeBack=True):
490    ''' Updates gpxfile from all histograms that are found in any phase
491    and any phase that used a histogram. Also updates rigid body definitions.
492
493
494    :param str GPXfile: full .gpx file name
495    :param dict Histograms: dictionary of histograms as {name:data,...}
496    :param dict Phases: dictionary of phases that use histograms
497    :param dict RigidBodies: dictionary of rigid bodies
498    :param dict CovData: dictionary of refined variables, varyList, & covariance matrix
499    :param bool makeBack: True if new backup of .gpx file is to be made; else use the last one made
500
501    '''
502                       
503    import distutils.file_util as dfu
504    GPXback = GPXBackup(GPXfile,makeBack)
505    print 'Read from file:',GPXback
506    print 'Save to file  :',GPXfile
507    infile = open(GPXback,'rb')
508    outfile = open(GPXfile,'wb')
509    while True:
510        try:
511            data = cPickle.load(infile)
512        except EOFError:
513            break
514        datum = data[0]
515#        print 'read: ',datum[0]
516        if datum[0] == 'Phases':
517            for iphase in range(len(data)):
518                if data[iphase][0] in Phases:
519                    phaseName = data[iphase][0]
520                    data[iphase][1].update(Phases[phaseName])
521        elif datum[0] == 'Covariance':
522            data[0][1] = CovData
523        elif datum[0] == 'Rigid bodies':
524            data[0][1] = RigidBodies
525        try:
526            histogram = Histograms[datum[0]]
527#            print 'found ',datum[0]
528            data[0][1][1] = histogram['Data']
529            data[0][1][0].update(histogram['Residuals'])
530            for datus in data[1:]:
531#                print '    read: ',datus[0]
532                if datus[0] in ['Background','Instrument Parameters','Sample Parameters','Reflection Lists']:
533                    datus[1] = histogram[datus[0]]
534        except KeyError:
535            pass
536        try:                       
537            cPickle.dump(data,outfile,1)
538        except AttributeError:
539            print 'ERROR - bad data in least squares result'
540            infile.close()
541            outfile.close()
542            dfu.copy_file(GPXback,GPXfile)
543            print 'GPX file save failed - old version retained'
544            return
545           
546    print 'GPX file save successful'
547   
548def SetSeqResult(GPXfile,Histograms,SeqResult):
549    '''
550    Needs doc string
551   
552    :param str GPXfile: full .gpx file name
553    '''
554    GPXback = GPXBackup(GPXfile)
555    print 'Read from file:',GPXback
556    print 'Save to file  :',GPXfile
557    infile = open(GPXback,'rb')
558    outfile = open(GPXfile,'wb')
559    while True:
560        try:
561            data = cPickle.load(infile)
562        except EOFError:
563            break
564        datum = data[0]
565        if datum[0] == 'Sequential results':
566            data[0][1] = SeqResult
567        # reset the Copy Next flag, since it should not be needed twice in a row
568        if datum[0] == 'Controls':
569            data[0][1]['Copy2Next'] = False
570        try:
571            histogram = Histograms[datum[0]]
572            data[0][1][1] = list(histogram['Data'])
573            for datus in data[1:]:
574                if datus[0] in ['Background','Instrument Parameters','Sample Parameters','Reflection Lists']:
575                    datus[1] = histogram[datus[0]]
576        except KeyError:
577            pass
578                               
579        cPickle.dump(data,outfile,1)
580    infile.close()
581    outfile.close()
582    print 'GPX file save successful'
583                       
584def ShowBanner(pFile=None):
585    'Print authorship, copyright and citation notice'
586    print >>pFile,80*'*'
587    print >>pFile,'   General Structure Analysis System-II Crystal Structure Refinement'
588    print >>pFile,'              by Robert B. Von Dreele & Brian H. Toby'
589    print >>pFile,'                Argonne National Laboratory(C), 2010'
590    print >>pFile,' This product includes software developed by the UChicago Argonne, LLC,' 
591    print >>pFile,'            as Operator of Argonne National Laboratory.'
592    print >>pFile,'                          Please cite:'
593    print >>pFile,'   B.H. Toby & R.B. Von Dreele, J. Appl. Cryst. 46, 544-549 (2013)'
594
595    print >>pFile,80*'*','\n'
596
597def ShowControls(Controls,pFile=None,SeqRef=False):
598    'Print controls information'
599    print >>pFile,' Least squares controls:'
600    print >>pFile,' Refinement type: ',Controls['deriv type']
601    if 'Hessian' in Controls['deriv type']:
602        print >>pFile,' Maximum number of cycles:',Controls['max cyc']
603    else:
604        print >>pFile,' Minimum delta-M/M for convergence: ','%.2g'%(Controls['min dM/M'])
605    print >>pFile,' Regularize hydrogens (if any):',Controls.get('HatomFix',False)
606    print >>pFile,' Initial shift factor: ','%.3f'%(Controls['shift factor'])
607    if SeqRef:
608        print >>pFile,' Sequential refinement controls:'
609        print >>pFile,' Copy of histogram results to next: ',Controls['Copy2Next']
610        print >>pFile,' Process histograms in reverse order: ',Controls['Reverse Seq']
611   
612def GetPawleyConstr(SGLaue,PawleyRef,im,pawleyVary):
613    'needs a doc string'
614#    if SGLaue in ['-1','2/m','mmm']:
615#        return                      #no Pawley symmetry required constraints
616    eqvDict = {}
617    for i,varyI in enumerate(pawleyVary):
618        eqvDict[varyI] = []
619        refI = int(varyI.split(':')[-1])
620        ih,ik,il = PawleyRef[refI][:3]
621        dspI = PawleyRef[refI][4+im]
622        for varyJ in pawleyVary[i+1:]:
623            refJ = int(varyJ.split(':')[-1])
624            jh,jk,jl = PawleyRef[refJ][:3]
625            dspJ = PawleyRef[refJ][4+im]
626            if SGLaue in ['4/m','4/mmm']:
627                isum = ih**2+ik**2
628                jsum = jh**2+jk**2
629                if abs(il) == abs(jl) and isum == jsum:
630                    eqvDict[varyI].append(varyJ) 
631            elif SGLaue in ['3R','3mR']:
632                isum = ih**2+ik**2+il**2
633                jsum = jh**2+jk**2+jl**2
634                isum2 = ih*ik+ih*il+ik*il
635                jsum2 = jh*jk+jh*jl+jk*jl
636                if isum == jsum and isum2 == jsum2:
637                    eqvDict[varyI].append(varyJ) 
638            elif SGLaue in ['3','3m1','31m','6/m','6/mmm']:
639                isum = ih**2+ik**2+ih*ik
640                jsum = jh**2+jk**2+jh*jk
641                if abs(il) == abs(jl) and isum == jsum:
642                    eqvDict[varyI].append(varyJ) 
643            elif SGLaue in ['m3','m3m']:
644                isum = ih**2+ik**2+il**2
645                jsum = jh**2+jk**2+jl**2
646                if isum == jsum:
647                    eqvDict[varyI].append(varyJ)
648            elif abs(dspI-dspJ)/dspI < 1.e-4:
649                eqvDict[varyI].append(varyJ) 
650    for item in pawleyVary:
651        if eqvDict[item]:
652            for item2 in pawleyVary:
653                if item2 in eqvDict[item]:
654                    eqvDict[item2] = []
655            G2mv.StoreEquivalence(item,eqvDict[item])
656                   
657def cellVary(pfx,SGData): 
658    'needs a doc string'
659    if SGData['SGLaue'] in ['-1',]:
660        return [pfx+'A0',pfx+'A1',pfx+'A2',pfx+'A3',pfx+'A4',pfx+'A5']
661    elif SGData['SGLaue'] in ['2/m',]:
662        if SGData['SGUniq'] == 'a':
663            return [pfx+'A0',pfx+'A1',pfx+'A2',pfx+'A5']
664        elif SGData['SGUniq'] == 'b':
665            return [pfx+'A0',pfx+'A1',pfx+'A2',pfx+'A4']
666        else:
667            return [pfx+'A0',pfx+'A1',pfx+'A2',pfx+'A3']
668    elif SGData['SGLaue'] in ['mmm',]:
669        return [pfx+'A0',pfx+'A1',pfx+'A2']
670    elif SGData['SGLaue'] in ['4/m','4/mmm']:
671        G2mv.StoreEquivalence(pfx+'A0',(pfx+'A1',))
672        return [pfx+'A0',pfx+'A1',pfx+'A2']
673    elif SGData['SGLaue'] in ['6/m','6/mmm','3m1', '31m', '3']:
674        G2mv.StoreEquivalence(pfx+'A0',(pfx+'A1',pfx+'A3',))
675        return [pfx+'A0',pfx+'A1',pfx+'A2',pfx+'A3']
676    elif SGData['SGLaue'] in ['3R', '3mR']:
677        G2mv.StoreEquivalence(pfx+'A0',(pfx+'A1',pfx+'A2',))
678        G2mv.StoreEquivalence(pfx+'A3',(pfx+'A4',pfx+'A5',))
679        return [pfx+'A0',pfx+'A1',pfx+'A2',pfx+'A3',pfx+'A4',pfx+'A5']                       
680    elif SGData['SGLaue'] in ['m3m','m3']:
681        G2mv.StoreEquivalence(pfx+'A0',(pfx+'A1',pfx+'A2',))
682        return [pfx+'A0',pfx+'A1',pfx+'A2']
683       
684def modVary(pfx,SSGData):
685    vary = []
686    for i,item in enumerate(SSGData['modSymb']):
687        if item in ['a','b','g']:
688            vary.append(pfx+'mV%d'%(i))
689    return vary
690       
691################################################################################
692##### Rigid Body Models and not General.get('doPawley')
693################################################################################
694       
695def GetRigidBodyModels(rigidbodyDict,Print=True,pFile=None):
696    'needs a doc string'
697   
698    def PrintResRBModel(RBModel):
699        atNames = RBModel['atNames']
700        rbRef = RBModel['rbRef']
701        rbSeq = RBModel['rbSeq']
702        print >>pFile,'Residue RB name: ',RBModel['RBname'],' no.atoms: ',len(RBModel['rbTypes']), \
703            'No. times used: ',RBModel['useCount']
704        print >>pFile,'    At name       x          y          z'
705        for name,xyz in zip(atNames,RBModel['rbXYZ']):
706            print >>pFile,%8s %10.4f %10.4f %10.4f'%(name,xyz[0],xyz[1],xyz[2])
707        print >>pFile,'Orientation defined by:',atNames[rbRef[0]],' -> ',atNames[rbRef[1]], \
708            ' & ',atNames[rbRef[0]],' -> ',atNames[rbRef[2]]
709        if rbSeq:
710            for i,rbseq in enumerate(rbSeq):
711                print >>pFile,'Torsion sequence ',i,' Bond: '+atNames[rbseq[0]],' - ', \
712                    atNames[rbseq[1]],' riding: ',[atNames[i] for i in rbseq[3]]
713       
714    def PrintVecRBModel(RBModel):
715        rbRef = RBModel['rbRef']
716        atTypes = RBModel['rbTypes']
717        print >>pFile,'Vector RB name: ',RBModel['RBname'],' no.atoms: ',len(RBModel['rbTypes']), \
718            'No. times used: ',RBModel['useCount']
719        for i in range(len(RBModel['VectMag'])):
720            print >>pFile,'Vector no.: ',i,' Magnitude: ', \
721                '%8.4f'%(RBModel['VectMag'][i]),' Refine? ',RBModel['VectRef'][i]
722            print >>pFile,'  No. Type     vx         vy         vz'
723            for j,[name,xyz] in enumerate(zip(atTypes,RBModel['rbVect'][i])):
724                print >>pFile,%d   %2s %10.4f %10.4f %10.4f'%(j,name,xyz[0],xyz[1],xyz[2])
725        print >>pFile,'  No. Type      x          y          z'
726        for i,[name,xyz] in enumerate(zip(atTypes,RBModel['rbXYZ'])):
727            print >>pFile,%d   %2s %10.4f %10.4f %10.4f'%(i,name,xyz[0],xyz[1],xyz[2])
728        print >>pFile,'Orientation defined by: atom ',rbRef[0],' -> atom ',rbRef[1], \
729            ' & atom ',rbRef[0],' -> atom ',rbRef[2]
730    rbVary = []
731    rbDict = {}
732    rbIds = rigidbodyDict.get('RBIds',{'Vector':[],'Residue':[]})
733    if len(rbIds['Vector']):
734        for irb,item in enumerate(rbIds['Vector']):
735            if rigidbodyDict['Vector'][item]['useCount']:
736                RBmags = rigidbodyDict['Vector'][item]['VectMag']
737                RBrefs = rigidbodyDict['Vector'][item]['VectRef']
738                for i,[mag,ref] in enumerate(zip(RBmags,RBrefs)):
739                    pid = '::RBV;'+str(i)+':'+str(irb)
740                    rbDict[pid] = mag
741                    if ref:
742                        rbVary.append(pid)
743                if Print:
744                    print >>pFile,'\nVector rigid body model:'
745                    PrintVecRBModel(rigidbodyDict['Vector'][item])
746    if len(rbIds['Residue']):
747        for item in rbIds['Residue']:
748            if rigidbodyDict['Residue'][item]['useCount']:
749                if Print:
750                    print >>pFile,'\nResidue rigid body model:'
751                    PrintResRBModel(rigidbodyDict['Residue'][item])
752    return rbVary,rbDict
753   
754def SetRigidBodyModels(parmDict,sigDict,rigidbodyDict,pFile=None):
755    'needs a doc string'
756   
757    def PrintRBVectandSig(VectRB,VectSig):
758        print >>pFile,'\n Rigid body vector magnitudes for '+VectRB['RBname']+':'
759        namstr = '  names :'
760        valstr = '  values:'
761        sigstr = '  esds  :'
762        for i,[val,sig] in enumerate(zip(VectRB['VectMag'],VectSig)):
763            namstr += '%12s'%('Vect '+str(i))
764            valstr += '%12.4f'%(val)
765            if sig:
766                sigstr += '%12.4f'%(sig)
767            else:
768                sigstr += 12*' '
769        print >>pFile,namstr
770        print >>pFile,valstr
771        print >>pFile,sigstr       
772       
773    RBIds = rigidbodyDict.get('RBIds',{'Vector':[],'Residue':[]})  #these are lists of rbIds
774    if not RBIds['Vector']:
775        return
776    for irb,item in enumerate(RBIds['Vector']):
777        if rigidbodyDict['Vector'][item]['useCount']:
778            VectSig = []
779            RBmags = rigidbodyDict['Vector'][item]['VectMag']
780            for i,mag in enumerate(RBmags):
781                name = '::RBV;'+str(i)+':'+str(irb)
782                if name in sigDict:
783                    VectSig.append(sigDict[name])
784            PrintRBVectandSig(rigidbodyDict['Vector'][item],VectSig)   
785       
786################################################################################
787##### Phase data
788################################################################################       
789                   
790def GetPhaseData(PhaseData,RestraintDict={},rbIds={},Print=True,pFile=None,seqRef=False):
791    'needs a doc string'
792           
793    def PrintFFtable(FFtable):
794        print >>pFile,'\n X-ray scattering factors:'
795        print >>pFile,'   Symbol     fa                                      fb                                      fc'
796        print >>pFile,99*'-'
797        for Ename in FFtable:
798            ffdata = FFtable[Ename]
799            fa = ffdata['fa']
800            fb = ffdata['fb']
801            print >>pFile,' %8s %9.5f %9.5f %9.5f %9.5f %9.5f %9.5f %9.5f %9.5f %9.5f' %  \
802                (Ename.ljust(8),fa[0],fa[1],fa[2],fa[3],fb[0],fb[1],fb[2],fb[3],ffdata['fc'])
803               
804    def PrintMFtable(MFtable):
805        print >>pFile,'\n <j0> Magnetic scattering factors:'
806        print >>pFile,'   Symbol     mfa                                    mfb                                     mfc'
807        print >>pFile,99*'-'
808        for Ename in MFtable:
809            mfdata = MFtable[Ename]
810            fa = mfdata['mfa']
811            fb = mfdata['mfb']
812            print >>pFile,' %8s %9.5f %9.5f %9.5f %9.5f %9.5f %9.5f %9.5f %9.5f %9.5f' %  \
813                (Ename.ljust(8),fa[0],fa[1],fa[2],fa[3],fb[0],fb[1],fb[2],fb[3],mfdata['mfc'])
814        print >>pFile,'\n <j2> Magnetic scattering factors:'
815        print >>pFile,'   Symbol     nfa                                    nfb                                     nfc'
816        print >>pFile,99*'-'
817        for Ename in MFtable:
818            mfdata = MFtable[Ename]
819            fa = mfdata['nfa']
820            fb = mfdata['nfb']
821            print >>pFile,' %8s %9.5f %9.5f %9.5f %9.5f %9.5f %9.5f %9.5f %9.5f %9.5f' %  \
822                (Ename.ljust(8),fa[0],fa[1],fa[2],fa[3],fb[0],fb[1],fb[2],fb[3],mfdata['nfc'])
823               
824    def PrintBLtable(BLtable):
825        print >>pFile,'\n Neutron scattering factors:'
826        print >>pFile,'   Symbol   isotope       mass       b       resonant terms'
827        print >>pFile,99*'-'
828        for Ename in BLtable:
829            bldata = BLtable[Ename]
830            isotope = bldata[0]
831            mass = bldata[1]['Mass']
832            if 'SL' in bldata[1]:
833                blen = bldata[1]['SL'][0]
834                bres = []
835            else:
836                blen = 0
837                bres = bldata[1]['BW-LS']
838            line = ' %8s%11s %10.3f %8.3f'%(Ename.ljust(8),isotope.center(11),mass,blen)
839            for item in bres:
840                line += '%10.5g'%(item)
841            print >>pFile,line
842           
843    def PrintRBObjects(resRBData,vecRBData):
844       
845        def PrintRBThermals():
846            tlstr = ['11','22','33','12','13','23']
847            sstr = ['12','13','21','23','31','32','AA','BB']
848            TLS = RB['ThermalMotion'][1]
849            TLSvar = RB['ThermalMotion'][2]
850            if 'T' in RB['ThermalMotion'][0]:
851                print >>pFile,'TLS data'
852                text = ''
853                for i in range(6):
854                    text += 'T'+tlstr[i]+' %8.4f %s '%(TLS[i],str(TLSvar[i])[0])
855                print >>pFile,text
856                if 'L' in RB['ThermalMotion'][0]: 
857                    text = ''
858                    for i in range(6,12):
859                        text += 'L'+tlstr[i-6]+' %8.2f %s '%(TLS[i],str(TLSvar[i])[0])
860                    print >>pFile,text
861                if 'S' in RB['ThermalMotion'][0]:
862                    text = ''
863                    for i in range(12,20):
864                        text += 'S'+sstr[i-12]+' %8.3f %s '%(TLS[i],str(TLSvar[i])[0])
865                    print >>pFile,text
866            if 'U' in RB['ThermalMotion'][0]:
867                print >>pFile,'Uiso data'
868                text = 'Uiso'+' %10.3f %s'%(TLS[0],str(TLSvar[0])[0])           
869           
870        if len(resRBData):
871            for RB in resRBData:
872                Oxyz = RB['Orig'][0]
873                Qrijk = RB['Orient'][0]
874                Angle = 2.0*acosd(Qrijk[0])
875                print >>pFile,'\nRBObject ',RB['RBname'],' at ',      \
876                    '%10.4f %10.4f %10.4f'%(Oxyz[0],Oxyz[1],Oxyz[2]),' Refine?',RB['Orig'][1]
877                print >>pFile,'Orientation angle,vector:',      \
878                    '%10.3f %10.4f %10.4f %10.4f'%(Angle,Qrijk[1],Qrijk[2],Qrijk[3]),' Refine? ',RB['Orient'][1]
879                Torsions = RB['Torsions']
880                if len(Torsions):
881                    text = 'Torsions: '
882                    for torsion in Torsions:
883                        text += '%10.4f Refine? %s'%(torsion[0],torsion[1])
884                    print >>pFile,text
885                PrintRBThermals()
886        if len(vecRBData):
887            for RB in vecRBData:
888                Oxyz = RB['Orig'][0]
889                Qrijk = RB['Orient'][0]
890                Angle = 2.0*acosd(Qrijk[0])
891                print >>pFile,'\nRBObject ',RB['RBname'],' at ',      \
892                    '%10.4f %10.4f %10.4f'%(Oxyz[0],Oxyz[1],Oxyz[2]),' Refine?',RB['Orig'][1]           
893                print >>pFile,'Orientation angle,vector:',      \
894                    '%10.3f %10.4f %10.4f %10.4f'%(Angle,Qrijk[1],Qrijk[2],Qrijk[3]),' Refine? ',RB['Orient'][1]
895                PrintRBThermals()
896               
897    def PrintAtoms(General,Atoms):
898        cx,ct,cs,cia = General['AtomPtrs']
899        print >>pFile,'\n Atoms:'
900        line = '   name    type  refine?   x         y         z    '+ \
901            '  frac site sym  mult I/A   Uiso     U11     U22     U33     U12     U13     U23'
902        if General['Type'] == 'macromolecular':
903            line = ' res no residue chain'+line
904        print >>pFile,line
905        if General['Type'] in ['nuclear','magnetic','faulted',]:
906            print >>pFile,135*'-'
907            for i,at in enumerate(Atoms):
908                line = '%7s'%(at[ct-1])+'%7s'%(at[ct])+'%7s'%(at[ct+1])+'%10.5f'%(at[cx])+'%10.5f'%(at[cx+1])+ \
909                    '%10.5f'%(at[cx+2])+'%8.3f'%(at[cx+3])+'%7s'%(at[cs])+'%5d'%(at[cs+1])+'%5s'%(at[cia])
910                if at[cia] == 'I':
911                    line += '%8.5f'%(at[cia+1])+48*' '
912                else:
913                    line += 8*' '
914                    for j in range(6):
915                        line += '%8.5f'%(at[cia+2+j])
916                print >>pFile,line
917        elif General['Type'] == 'macromolecular':
918            print >>pFile,135*'-'           
919            for i,at in enumerate(Atoms):
920                line = '%7s'%(at[0])+'%7s'%(at[1])+'%7s'%(at[2])+'%7s'%(at[ct-1])+'%7s'%(at[ct])+'%7s'%(at[ct+1])+'%10.5f'%(at[cx])+'%10.5f'%(at[cx+1])+ \
921                    '%10.5f'%(at[cx+2])+'%8.3f'%(at[cx+3])+'%7s'%(at[cs])+'%5d'%(at[cs+1])+'%5s'%(at[cia])
922                if at[cia] == 'I':
923                    line += '%8.4f'%(at[cia+1])+48*' '
924                else:
925                    line += 8*' '
926                    for j in range(6):
927                        line += '%8.4f'%(at[cia+2+j])
928                print >>pFile,line
929               
930    def PrintMoments(General,Atoms):
931        cx,ct,cs,cia = General['AtomPtrs']
932        cmx = cx+4
933        AtInfo = dict(zip(General['AtomTypes'],General['Lande g']))
934        print >>pFile,'\n Magnetic moments:'
935        line = '   name    type  refine?  Mx        My        Mz    '
936        print >>pFile,line
937        print >>pFile,135*'-'
938        for i,at in enumerate(Atoms):
939            if AtInfo[at[ct]]:
940                line = '%7s'%(at[ct-1])+'%7s'%(at[ct])+'%7s'%(at[ct+1])+'%10.5f'%(at[cmx])+'%10.5f'%(at[cmx+1])+ \
941                    '%10.5f'%(at[cmx+2])
942                print >>pFile,line
943       
944               
945    def PrintWaves(General,Atoms):
946        cx,ct,cs,cia = General['AtomPtrs']
947        print >>pFile,'\n Modulation waves'
948        names = {'Sfrac':['Fsin','Fcos','Fzero','Fwid'],'Spos':['Xsin','Ysin','Zsin','Xcos','Ycos','Zcos','Tmin','Tmax','Xmax','Ymax','Zmax'],
949            'Sadp':['U11sin','U22sin','U33sin','U12sin','U13sin','U23sin','U11cos','U22cos',
950            'U33cos','U12cos','U13cos','U23cos'],'Smag':['MXsin','MYsin','MZsin','MXcos','MYcos','MZcos']}
951        print >>pFile,135*'-'
952        for i,at in enumerate(Atoms):
953            AtomSS = at[-1]['SS1']
954            for Stype in ['Sfrac','Spos','Sadp','Smag']:
955                Waves = AtomSS[Stype]
956                if len(Waves):
957                    print >>pFile,' atom: %s, site sym: %s, type: %s wave type: %s:'    \
958                        %(at[ct-1],at[cs],Stype,AtomSS['waveType'])
959                for iw,wave in enumerate(Waves):                   
960                    line = ''
961                    if AtomSS['waveType'] in ['Block','ZigZag'] and Stype == 'Spos' and not iw:
962                        for item in names[Stype][6:]:
963                            line += '%8s '%(item)                       
964                    else:
965                        if Stype == 'Spos':
966                            for item in names[Stype][:6]:
967                                line += '%8s '%(item)
968                        else:
969                            for item in names[Stype]:
970                                line += '%8s '%(item)
971                    print >>pFile,line
972                    line = ''
973                    for item in wave[0]:
974                        line += '%8.4f '%(item)
975                    line += ' Refine? '+str(wave[1])
976                    print >>pFile,line
977       
978    def PrintTexture(textureData):
979        topstr = '\n Spherical harmonics texture: Order:' + \
980            str(textureData['Order'])
981        if textureData['Order']:
982            print >>pFile,topstr+' Refine? '+str(textureData['SH Coeff'][0])
983        else:
984            print >>pFile,topstr
985            return
986        names = ['omega','chi','phi']
987        line = '\n'
988        for name in names:
989            line += ' SH '+name+':'+'%12.4f'%(textureData['Sample '+name][1])+' Refine? '+str(textureData['Sample '+name][0])
990        print >>pFile,line
991        print >>pFile,'\n Texture coefficients:'
992        SHcoeff = textureData['SH Coeff'][1]
993        SHkeys = SHcoeff.keys()
994        nCoeff = len(SHcoeff)
995        nBlock = nCoeff/10+1
996        iBeg = 0
997        iFin = min(iBeg+10,nCoeff)
998        for block in range(nBlock):
999            ptlbls = ' names :'
1000            ptstr =  ' values:'
1001            for item in SHkeys[iBeg:iFin]:
1002                ptlbls += '%12s'%(item)
1003                ptstr += '%12.4f'%(SHcoeff[item]) 
1004            print >>pFile,ptlbls
1005            print >>pFile,ptstr
1006            iBeg += 10
1007            iFin = min(iBeg+10,nCoeff)
1008       
1009    def MakeRBParms(rbKey,phaseVary,phaseDict):
1010        rbid = str(rbids.index(RB['RBId']))
1011        pfxRB = pfx+'RB'+rbKey+'P'
1012        pstr = ['x','y','z']
1013        ostr = ['a','i','j','k']
1014        for i in range(3):
1015            name = pfxRB+pstr[i]+':'+str(iRB)+':'+rbid
1016            phaseDict[name] = RB['Orig'][0][i]
1017            if RB['Orig'][1]:
1018                phaseVary += [name,]
1019        pfxRB = pfx+'RB'+rbKey+'O'
1020        for i in range(4):
1021            name = pfxRB+ostr[i]+':'+str(iRB)+':'+rbid
1022            phaseDict[name] = RB['Orient'][0][i]
1023            if RB['Orient'][1] == 'AV' and i:
1024                phaseVary += [name,]
1025            elif RB['Orient'][1] == 'A' and not i:
1026                phaseVary += [name,]
1027           
1028    def MakeRBThermals(rbKey,phaseVary,phaseDict):
1029        rbid = str(rbids.index(RB['RBId']))
1030        tlstr = ['11','22','33','12','13','23']
1031        sstr = ['12','13','21','23','31','32','AA','BB']
1032        if 'T' in RB['ThermalMotion'][0]:
1033            pfxRB = pfx+'RB'+rbKey+'T'
1034            for i in range(6):
1035                name = pfxRB+tlstr[i]+':'+str(iRB)+':'+rbid
1036                phaseDict[name] = RB['ThermalMotion'][1][i]
1037                if RB['ThermalMotion'][2][i]:
1038                    phaseVary += [name,]
1039        if 'L' in RB['ThermalMotion'][0]:
1040            pfxRB = pfx+'RB'+rbKey+'L'
1041            for i in range(6):
1042                name = pfxRB+tlstr[i]+':'+str(iRB)+':'+rbid
1043                phaseDict[name] = RB['ThermalMotion'][1][i+6]
1044                if RB['ThermalMotion'][2][i+6]:
1045                    phaseVary += [name,]
1046        if 'S' in RB['ThermalMotion'][0]:
1047            pfxRB = pfx+'RB'+rbKey+'S'
1048            for i in range(8):
1049                name = pfxRB+sstr[i]+':'+str(iRB)+':'+rbid
1050                phaseDict[name] = RB['ThermalMotion'][1][i+12]
1051                if RB['ThermalMotion'][2][i+12]:
1052                    phaseVary += [name,]
1053        if 'U' in RB['ThermalMotion'][0]:
1054            name = pfx+'RB'+rbKey+'U:'+str(iRB)+':'+rbid
1055            phaseDict[name] = RB['ThermalMotion'][1][0]
1056            if RB['ThermalMotion'][2][0]:
1057                phaseVary += [name,]
1058               
1059    def MakeRBTorsions(rbKey,phaseVary,phaseDict):
1060        rbid = str(rbids.index(RB['RBId']))
1061        pfxRB = pfx+'RB'+rbKey+'Tr;'
1062        for i,torsion in enumerate(RB['Torsions']):
1063            name = pfxRB+str(i)+':'+str(iRB)+':'+rbid
1064            phaseDict[name] = torsion[0]
1065            if torsion[1]:
1066                phaseVary += [name,]
1067                   
1068    if Print:
1069        print  >>pFile,'\n Phases:'
1070    phaseVary = []
1071    phaseDict = {}
1072    pawleyLookup = {}
1073    FFtables = {}                   #scattering factors - xrays
1074    MFtables = {}                   #Mag. form factors
1075    BLtables = {}                   # neutrons
1076    Natoms = {}
1077    maxSSwave = {}
1078    shModels = ['cylindrical','none','shear - 2/m','rolling - mmm']
1079    SamSym = dict(zip(shModels,['0','-1','2/m','mmm']))
1080    atomIndx = {}
1081    for name in PhaseData:
1082        General = PhaseData[name]['General']
1083        pId = PhaseData[name]['pId']
1084        pfx = str(pId)+'::'
1085        FFtable = G2el.GetFFtable(General['AtomTypes'])
1086        BLtable = G2el.GetBLtable(General)
1087        FFtables.update(FFtable)
1088        BLtables.update(BLtable)
1089        phaseDict[pfx+'isMag'] = False
1090        SGData = General['SGData']
1091        SGtext,SGtable = G2spc.SGPrint(SGData)
1092        if General['Type'] == 'magnetic':
1093            MFtable = G2el.GetMFtable(General['AtomTypes'],General['Lande g'])
1094            MFtables.update(MFtable)
1095            phaseDict[pfx+'isMag'] = True
1096            SpnFlp = SGData['SpnFlp']
1097        Atoms = PhaseData[name]['Atoms']
1098        if Atoms and not General.get('doPawley'):
1099            cx,ct,cs,cia = General['AtomPtrs']
1100            AtLookup = G2mth.FillAtomLookUp(Atoms,cia+8)
1101        PawleyRef = PhaseData[name].get('Pawley ref',[])
1102        cell = General['Cell']
1103        A = G2lat.cell2A(cell[1:7])
1104        phaseDict.update({pfx+'A0':A[0],pfx+'A1':A[1],pfx+'A2':A[2],
1105            pfx+'A3':A[3],pfx+'A4':A[4],pfx+'A5':A[5],pfx+'Vol':G2lat.calc_V(A)})
1106        if cell[0]:
1107            phaseVary += cellVary(pfx,SGData)       #also fills in symmetry required constraints
1108        SSGtext = []    #no superstructure
1109        im = 0
1110        if General.get('Modulated',False):
1111            im = 1      #refl offset
1112            Vec,vRef,maxH = General['SuperVec']
1113            phaseDict.update({pfx+'mV0':Vec[0],pfx+'mV1':Vec[1],pfx+'mV2':Vec[2]})
1114            SSGData = General['SSGData']
1115            SSGtext,SSGtable = G2spc.SSGPrint(SGData,SSGData)
1116            if vRef:
1117                phaseVary += modVary(pfx,SSGData)       
1118        resRBData = PhaseData[name]['RBModels'].get('Residue',[])
1119        if resRBData:
1120            rbids = rbIds['Residue']    #NB: used in the MakeRB routines
1121            for iRB,RB in enumerate(resRBData):
1122                MakeRBParms('R',phaseVary,phaseDict)
1123                MakeRBThermals('R',phaseVary,phaseDict)
1124                MakeRBTorsions('R',phaseVary,phaseDict)
1125       
1126        vecRBData = PhaseData[name]['RBModels'].get('Vector',[])
1127        if vecRBData:
1128            rbids = rbIds['Vector']    #NB: used in the MakeRB routines
1129            for iRB,RB in enumerate(vecRBData):
1130                MakeRBParms('V',phaseVary,phaseDict)
1131                MakeRBThermals('V',phaseVary,phaseDict)
1132                   
1133        Natoms[pfx] = 0
1134        maxSSwave[pfx] = {'Sfrac':0,'Spos':0,'Sadp':0,'Smag':0}
1135        if Atoms and not General.get('doPawley'):
1136            cx,ct,cs,cia = General['AtomPtrs']
1137            Natoms[pfx] = len(Atoms)
1138            for i,at in enumerate(Atoms):
1139                atomIndx[at[cia+8]] = [pfx,i]      #lookup table for restraints
1140                phaseDict.update({pfx+'Atype:'+str(i):at[ct],pfx+'Afrac:'+str(i):at[cx+3],pfx+'Amul:'+str(i):at[cs+1],
1141                    pfx+'Ax:'+str(i):at[cx],pfx+'Ay:'+str(i):at[cx+1],pfx+'Az:'+str(i):at[cx+2],
1142                    pfx+'dAx:'+str(i):0.,pfx+'dAy:'+str(i):0.,pfx+'dAz:'+str(i):0.,         #refined shifts for x,y,z
1143                    pfx+'AI/A:'+str(i):at[cia],})
1144                if at[cia] == 'I':
1145                    phaseDict[pfx+'AUiso:'+str(i)] = at[cia+1]
1146                else:
1147                    phaseDict.update({pfx+'AU11:'+str(i):at[cia+2],pfx+'AU22:'+str(i):at[cia+3],pfx+'AU33:'+str(i):at[cia+4],
1148                        pfx+'AU12:'+str(i):at[cia+5],pfx+'AU13:'+str(i):at[cia+6],pfx+'AU23:'+str(i):at[cia+7]})
1149                if General['Type'] == 'magnetic':
1150                    phaseDict.update({pfx+'AMx:'+str(i):at[cx+4],pfx+'AMy:'+str(i):at[cx+5],pfx+'AMz:'+str(i):at[cx+6]})
1151                if 'F' in at[ct+1]:
1152                    phaseVary.append(pfx+'Afrac:'+str(i))
1153                if 'X' in at[ct+1]:
1154                    try:    #patch for sytsym name changes
1155                        xId,xCoef = G2spc.GetCSxinel(at[cs])
1156                    except KeyError:
1157                        Sytsym = G2spc.SytSym(at[cx:cx+3],SGData)[0]
1158                        at[cs] = Sytsym
1159                        xId,xCoef = G2spc.GetCSxinel(at[cs])
1160                    xId,xCoef = G2spc.GetCSxinel(at[cs])
1161                    names = [pfx+'dAx:'+str(i),pfx+'dAy:'+str(i),pfx+'dAz:'+str(i)]
1162                    equivs = [[],[],[]]
1163                    for j in range(3):
1164                        if xId[j] > 0:                               
1165                            phaseVary.append(names[j])
1166                            equivs[xId[j]-1].append([names[j],xCoef[j]])
1167                    for equiv in equivs:
1168                        if len(equiv) > 1:
1169                            name = equiv[0][0]
1170                            coef = equiv[0][1]
1171                            for eqv in equiv[1:]:
1172                                eqv[1] /= coef
1173                                G2mv.StoreEquivalence(name,(eqv,))
1174                if 'U' in at[ct+1]:
1175                    if at[cia] == 'I':
1176                        phaseVary.append(pfx+'AUiso:'+str(i))
1177                    else:
1178                        try:    #patch for sytsym name changes
1179                            uId,uCoef = G2spc.GetCSuinel(at[cs])[:2]
1180                        except KeyError:
1181                            Sytsym = G2spc.SytSym(at[cx:cx+3],SGData)[0]
1182                            at[cs] = Sytsym
1183                            uId,uCoef = G2spc.GetCSuinel(at[cs])[:2]
1184                        uId,uCoef = G2spc.GetCSuinel(at[cs])[:2]
1185                        names = [pfx+'AU11:'+str(i),pfx+'AU22:'+str(i),pfx+'AU33:'+str(i),
1186                            pfx+'AU12:'+str(i),pfx+'AU13:'+str(i),pfx+'AU23:'+str(i)]
1187                        equivs = [[],[],[],[],[],[]]
1188                        for j in range(6):
1189                            if uId[j] > 0:                               
1190                                phaseVary.append(names[j])
1191                                equivs[uId[j]-1].append([names[j],uCoef[j]])
1192                        for equiv in equivs:
1193                            if len(equiv) > 1:
1194                                name = equiv[0][0]
1195                                coef = equiv[0][1]
1196                                for eqv in equiv[1:]:
1197                                    eqv[1] /= coef
1198                                G2mv.StoreEquivalence(name,equiv[1:])
1199                if 'M' in at[ct+1]:
1200                    SytSym,Mul,Nop,dupDir = G2spc.SytSym(at[cx:cx+3],SGData)
1201                    mId,mCoef = G2spc.GetCSpqinel(SytSym,SpnFlp,dupDir)
1202                    names = [pfx+'AMx:'+str(i),pfx+'AMy:'+str(i),pfx+'AMz:'+str(i)]
1203                    equivs = [[],[],[]]
1204                    for j in range(3):
1205                        if mId[j] > 0:
1206                            phaseVary.append(names[j])
1207                            equivs[mId[j]-1].append([names[j],mCoef[j]])
1208                    for equiv in equivs:
1209                        if len(equiv) > 1:
1210                            name = equiv[0][0]
1211                            coef = equiv[0][1]
1212                            for eqv in equiv[1:]:
1213                                eqv[1] /= coef
1214                                G2mv.StoreEquivalence(name,(eqv,))
1215                if General.get('Modulated',False):
1216                    AtomSS = at[-1]['SS1']
1217                    waveType = AtomSS['waveType']
1218                    phaseDict[pfx+'waveType:'+str(i)] = waveType
1219                    for Stype in ['Sfrac','Spos','Sadp','Smag']:
1220                        Waves = AtomSS[Stype]
1221                        nx = 0
1222                        for iw,wave in enumerate(Waves):
1223                            if not iw:
1224                                if waveType in ['ZigZag','Block']:
1225                                    nx = 1
1226                                CSI = G2spc.GetSSfxuinel(waveType,1,at[cx:cx+3],SGData,SSGData)
1227                            else:
1228                                CSI = G2spc.GetSSfxuinel('Fourier',iw+1-nx,at[cx:cx+3],SGData,SSGData)
1229                            uId,uCoef = CSI[Stype]
1230                            stiw = str(i)+':'+str(iw)
1231                            if Stype == 'Spos':
1232                                if waveType in ['ZigZag','Block',] and not iw:
1233                                    names = [pfx+'Tmin:'+stiw,pfx+'Tmax:'+stiw,pfx+'Xmax:'+stiw,pfx+'Ymax:'+stiw,pfx+'Zmax:'+stiw]
1234                                    equivs = [[],[], [],[],[]]
1235                                else:
1236                                    names = [pfx+'Xsin:'+stiw,pfx+'Ysin:'+stiw,pfx+'Zsin:'+stiw,
1237                                        pfx+'Xcos:'+stiw,pfx+'Ycos:'+stiw,pfx+'Zcos:'+stiw]
1238                                    equivs = [[],[],[], [],[],[]]
1239                            elif Stype == 'Sadp':
1240                                names = [pfx+'U11sin:'+stiw,pfx+'U22sin:'+stiw,pfx+'U33sin:'+stiw,
1241                                    pfx+'U12sin:'+stiw,pfx+'U13sin:'+stiw,pfx+'U23sin:'+stiw,
1242                                    pfx+'U11cos:'+stiw,pfx+'U22cos:'+stiw,pfx+'U33cos:'+stiw,
1243                                    pfx+'U12cos:'+stiw,pfx+'U13cos:'+stiw,pfx+'U23cos:'+stiw]
1244                                equivs = [[],[],[],[],[],[], [],[],[],[],[],[]]
1245                            elif Stype == 'Sfrac':
1246                                equivs = [[],[]]
1247                                if 'Crenel' in waveType and not iw:
1248                                    names = [pfx+'Fzero:'+stiw,pfx+'Fwid:'+stiw]
1249                                else:
1250                                    names = [pfx+'Fsin:'+stiw,pfx+'Fcos:'+stiw]
1251                            elif Stype == 'Smag':
1252                                equivs = [[],[],[], [],[],[]]
1253                                names = [pfx+'MXsin:'+stiw,pfx+'MYsin:'+stiw,pfx+'MZsin:'+stiw,
1254                                    pfx+'MXcos:'+stiw,pfx+'MYcos:'+stiw,pfx+'MZcos:'+stiw]
1255                            phaseDict.update(dict(zip(names,wave[0])))
1256                            if wave[1]: #what do we do here for multiple terms in modulation constraints?
1257                                for j in range(len(equivs)):
1258                                    if uId[j][0] > 0:                               
1259                                        phaseVary.append(names[j])
1260                                        equivs[uId[j][0]-1].append([names[j],uCoef[j][0]])
1261                                for equiv in equivs:
1262                                    if len(equiv) > 1:
1263                                        name = equiv[0][0]
1264                                        coef = equiv[0][1]
1265                                        for eqv in equiv[1:]:
1266                                            eqv[1] /= coef
1267                                        G2mv.StoreEquivalence(name,equiv[1:])
1268                            maxSSwave[pfx][Stype] = max(maxSSwave[pfx][Stype],iw+1)
1269            textureData = General['SH Texture']
1270            if textureData['Order'] and not seqRef:
1271                phaseDict[pfx+'SHorder'] = textureData['Order']
1272                phaseDict[pfx+'SHmodel'] = SamSym[textureData['Model']]
1273                for item in ['omega','chi','phi']:
1274                    phaseDict[pfx+'SH '+item] = textureData['Sample '+item][1]
1275                    if textureData['Sample '+item][0]:
1276                        phaseVary.append(pfx+'SH '+item)
1277                for item in textureData['SH Coeff'][1]:
1278                    phaseDict[pfx+item] = textureData['SH Coeff'][1][item]
1279                    if textureData['SH Coeff'][0]:
1280                        phaseVary.append(pfx+item)
1281               
1282            if Print:
1283                print >>pFile,'\n Phase name: ',General['Name']
1284                print >>pFile,135*'='
1285                PrintFFtable(FFtable)
1286                PrintBLtable(BLtable)
1287                if General['Type'] == 'magnetic':
1288                    PrintMFtable(MFtable)
1289                print >>pFile,''
1290                #how do we print magnetic symmetry table? TBD
1291                if len(SSGtext):    #if superstructure
1292                    for line in SSGtext: print >>pFile,line
1293                    if len(SSGtable):
1294                        for item in SSGtable:
1295                            line = ' %s '%(item)
1296                            print >>pFile,line   
1297                    else:
1298                        print >>pFile,' ( 1)    %s'%(SSGtable[0])
1299                else:
1300                    for line in SGtext: print >>pFile,line
1301                    if len(SGtable):
1302                        for item in SGtable:
1303                            line = ' %s '%(item)
1304                            print >>pFile,line   
1305                    else:
1306                        print >>pFile,' ( 1)    %s'%(SGtable[0])
1307                PrintRBObjects(resRBData,vecRBData)
1308                PrintAtoms(General,Atoms)
1309                if General['Type'] == 'magnetic':
1310                    PrintMoments(General,Atoms)
1311                if General.get('Modulated',False):
1312                    PrintWaves(General,Atoms)
1313                print >>pFile,'\n Unit cell: a = %.5f'%(cell[1]),' b = %.5f'%(cell[2]),' c = %.5f'%(cell[3]), \
1314                    ' alpha = %.3f'%(cell[4]),' beta = %.3f'%(cell[5]),' gamma = %.3f'%(cell[6]), \
1315                    ' volume = %.3f'%(cell[7]),' Refine?',cell[0]
1316                if len(SSGtext):    #if superstructure
1317                    print >>pFile,'\n Modulation vector: mV0 = %.4f'%(Vec[0]),' mV1 = %.4f'%(Vec[1]),   \
1318                        ' mV2 = %.4f'%(Vec[2]),' max mod. index = %d'%(maxH),' Refine?',vRef
1319                if not seqRef:
1320                    PrintTexture(textureData)
1321                if name in RestraintDict:
1322                    PrintRestraints(cell[1:7],SGData,General['AtomPtrs'],Atoms,AtLookup,
1323                        textureData,RestraintDict[name],pFile)
1324                   
1325        elif PawleyRef:
1326            if Print:
1327                print >>pFile,'\n Phase name: ',General['Name']
1328                print >>pFile,135*'='
1329                print >>pFile,''
1330                if len(SSGtext):    #if superstructure
1331                    for line in SSGtext: print >>pFile,line
1332                    if len(SSGtable):
1333                        for item in SSGtable:
1334                            line = ' %s '%(item)
1335                            print >>pFile,line   
1336                    else:
1337                        print >>pFile,' ( 1)    %s'%(SSGtable[0])
1338                else:
1339                    for line in SGtext: print >>pFile,line
1340                    if len(SGtable):
1341                        for item in SGtable:
1342                            line = ' %s '%(item)
1343                            print >>pFile,line   
1344                    else:
1345                        print >>pFile,' ( 1)    %s'%(SGtable[0])
1346                print >>pFile,'\n Unit cell: a = %.5f'%(cell[1]),' b = %.5f'%(cell[2]),' c = %.5f'%(cell[3]), \
1347                    ' alpha = %.3f'%(cell[4]),' beta = %.3f'%(cell[5]),' gamma = %.3f'%(cell[6]), \
1348                    ' volume = %.3f'%(cell[7]),' Refine?',cell[0]
1349                if len(SSGtext):    #if superstructure
1350                    print >>pFile,'\n Modulation vector: mV0 = %.4f'%(Vec[0]),' mV1 = %.4f'%(Vec[1]),   \
1351                        ' mV2 = %.4f'%(Vec[2]),' max mod. index = %d'%(maxH),' Refine?',vRef
1352            pawleyVary = []
1353            for i,refl in enumerate(PawleyRef):
1354                phaseDict[pfx+'PWLref:'+str(i)] = refl[6+im]
1355                if im:
1356                    pawleyLookup[pfx+'%d,%d,%d,%d'%(refl[0],refl[1],refl[2],refl[3])] = i
1357                else:
1358                    pawleyLookup[pfx+'%d,%d,%d'%(refl[0],refl[1],refl[2])] = i
1359                if refl[5+im]:
1360                    pawleyVary.append(pfx+'PWLref:'+str(i))
1361            GetPawleyConstr(SGData['SGLaue'],PawleyRef,im,pawleyVary)      #does G2mv.StoreEquivalence
1362            phaseVary += pawleyVary
1363               
1364    return Natoms,atomIndx,phaseVary,phaseDict,pawleyLookup,FFtables,BLtables,MFtables,maxSSwave
1365   
1366def cellFill(pfx,SGData,parmDict,sigDict): 
1367    '''Returns the filled-out reciprocal cell (A) terms and their uncertainties
1368    from the parameter and sig dictionaries.
1369
1370    :param str pfx: parameter prefix ("n::", where n is a phase number)
1371    :param dict SGdata: a symmetry object
1372    :param dict parmDict: a dictionary of parameters
1373    :param dict sigDict:  a dictionary of uncertainties on parameters
1374
1375    :returns: A,sigA where each is a list of six terms with the A terms
1376    '''
1377    if SGData['SGLaue'] in ['-1',]:
1378        A = [parmDict[pfx+'A0'],parmDict[pfx+'A1'],parmDict[pfx+'A2'],
1379            parmDict[pfx+'A3'],parmDict[pfx+'A4'],parmDict[pfx+'A5']]
1380    elif SGData['SGLaue'] in ['2/m',]:
1381        if SGData['SGUniq'] == 'a':
1382            A = [parmDict[pfx+'A0'],parmDict[pfx+'A1'],parmDict[pfx+'A2'],
1383                0,0,parmDict[pfx+'A5']]
1384        elif SGData['SGUniq'] == 'b':
1385            A = [parmDict[pfx+'A0'],parmDict[pfx+'A1'],parmDict[pfx+'A2'],
1386                0,parmDict[pfx+'A4'],0]
1387        else:
1388            A = [parmDict[pfx+'A0'],parmDict[pfx+'A1'],parmDict[pfx+'A2'],
1389                parmDict[pfx+'A3'],0,0]
1390    elif SGData['SGLaue'] in ['mmm',]:
1391        A = [parmDict[pfx+'A0'],parmDict[pfx+'A1'],parmDict[pfx+'A2'],0,0,0]
1392    elif SGData['SGLaue'] in ['4/m','4/mmm']:
1393        A = [parmDict[pfx+'A0'],parmDict[pfx+'A0'],parmDict[pfx+'A2'],0,0,0]
1394    elif SGData['SGLaue'] in ['6/m','6/mmm','3m1', '31m', '3']:
1395        A = [parmDict[pfx+'A0'],parmDict[pfx+'A0'],parmDict[pfx+'A2'],
1396            parmDict[pfx+'A0'],0,0]
1397    elif SGData['SGLaue'] in ['3R', '3mR']:
1398        A = [parmDict[pfx+'A0'],parmDict[pfx+'A0'],parmDict[pfx+'A0'],
1399            parmDict[pfx+'A3'],parmDict[pfx+'A3'],parmDict[pfx+'A3']]
1400    elif SGData['SGLaue'] in ['m3m','m3']:
1401        A = [parmDict[pfx+'A0'],parmDict[pfx+'A0'],parmDict[pfx+'A0'],0,0,0]
1402
1403    try:
1404        if SGData['SGLaue'] in ['-1',]:
1405            sigA = [sigDict[pfx+'A0'],sigDict[pfx+'A1'],sigDict[pfx+'A2'],
1406                sigDict[pfx+'A3'],sigDict[pfx+'A4'],sigDict[pfx+'A5']]
1407        elif SGData['SGLaue'] in ['2/m',]:
1408            if SGData['SGUniq'] == 'a':
1409                sigA = [sigDict[pfx+'A0'],sigDict[pfx+'A1'],sigDict[pfx+'A2'],
1410                    0,0,sigDict[pfx+'A5']]
1411            elif SGData['SGUniq'] == 'b':
1412                sigA = [sigDict[pfx+'A0'],sigDict[pfx+'A1'],sigDict[pfx+'A2'],
1413                    0,sigDict[pfx+'A4'],0]
1414            else:
1415                sigA = [sigDict[pfx+'A0'],sigDict[pfx+'A1'],sigDict[pfx+'A2'],
1416                    sigDict[pfx+'A3'],0,0]
1417        elif SGData['SGLaue'] in ['mmm',]:
1418            sigA = [sigDict[pfx+'A0'],sigDict[pfx+'A1'],sigDict[pfx+'A2'],0,0,0]
1419        elif SGData['SGLaue'] in ['4/m','4/mmm']:
1420            sigA = [sigDict[pfx+'A0'],0,sigDict[pfx+'A2'],0,0,0]
1421        elif SGData['SGLaue'] in ['6/m','6/mmm','3m1', '31m', '3']:
1422            sigA = [sigDict[pfx+'A0'],0,sigDict[pfx+'A2'],0,0,0]
1423        elif SGData['SGLaue'] in ['3R', '3mR']:
1424            sigA = [sigDict[pfx+'A0'],0,0,sigDict[pfx+'A3'],0,0]
1425        elif SGData['SGLaue'] in ['m3m','m3']:
1426            sigA = [sigDict[pfx+'A0'],0,0,0,0,0]
1427    except KeyError:
1428        sigA = [0,0,0,0,0,0]
1429    return A,sigA
1430       
1431def PrintRestraints(cell,SGData,AtPtrs,Atoms,AtLookup,textureData,phaseRest,pFile):
1432    'needs a doc string'
1433    if phaseRest:
1434        Amat = G2lat.cell2AB(cell)[0]
1435        cx,ct,cs = AtPtrs[:3]
1436        names = [['Bond','Bonds'],['Angle','Angles'],['Plane','Planes'],
1437            ['Chiral','Volumes'],['Torsion','Torsions'],['Rama','Ramas'],
1438            ['ChemComp','Sites'],['Texture','HKLs']]
1439        for name,rest in names:
1440            itemRest = phaseRest[name]
1441            if itemRest[rest] and itemRest['Use']:
1442                print >>pFile,'\n  %s %10.3f Use: %s'%(name+' restraint weight factor',itemRest['wtFactor'],str(itemRest['Use']))
1443                if name in ['Bond','Angle','Plane','Chiral']:
1444                    print >>pFile,'     calc       obs      sig   delt/sig  atoms(symOp): '
1445                    for indx,ops,obs,esd in itemRest[rest]:
1446                        try:
1447                            AtNames = G2mth.GetAtomItemsById(Atoms,AtLookup,indx,ct-1)
1448                            AtName = ''
1449                            for i,Aname in enumerate(AtNames):
1450                                AtName += Aname
1451                                if ops[i] == '1':
1452                                    AtName += '-'
1453                                else:
1454                                    AtName += '+('+ops[i]+')-'
1455                            XYZ = np.array(G2mth.GetAtomItemsById(Atoms,AtLookup,indx,cx,3))
1456                            XYZ = G2mth.getSyXYZ(XYZ,ops,SGData)
1457                            if name == 'Bond':
1458                                calc = G2mth.getRestDist(XYZ,Amat)
1459                            elif name == 'Angle':
1460                                calc = G2mth.getRestAngle(XYZ,Amat)
1461                            elif name == 'Plane':
1462                                calc = G2mth.getRestPlane(XYZ,Amat)
1463                            elif name == 'Chiral':
1464                                calc = G2mth.getRestChiral(XYZ,Amat)
1465                            print >>pFile,' %9.3f %9.3f %8.3f %8.3f   %s'%(calc,obs,esd,(obs-calc)/esd,AtName[:-1])
1466                        except KeyError:
1467                            del itemRest[rest]
1468                elif name in ['Torsion','Rama']:
1469                    print >>pFile,'  atoms(symOp)  calc  obs  sig  delt/sig  torsions: '
1470                    coeffDict = itemRest['Coeff']
1471                    for indx,ops,cofName,esd in itemRest[rest]:
1472                        AtNames = G2mth.GetAtomItemsById(Atoms,AtLookup,indx,ct-1)
1473                        AtName = ''
1474                        for i,Aname in enumerate(AtNames):
1475                            AtName += Aname+'+('+ops[i]+')-'
1476                        XYZ = np.array(G2mth.GetAtomItemsById(Atoms,AtLookup,indx,cx,3))
1477                        XYZ = G2mth.getSyXYZ(XYZ,ops,SGData)
1478                        if name == 'Torsion':
1479                            tor = G2mth.getRestTorsion(XYZ,Amat)
1480                            restr,calc = G2mth.calcTorsionEnergy(tor,coeffDict[cofName])
1481                            print >>pFile,' %8.3f %8.3f %.3f %8.3f %8.3f %s'%(calc,obs,esd,(obs-calc)/esd,tor,AtName[:-1])
1482                        else:
1483                            phi,psi = G2mth.getRestRama(XYZ,Amat)
1484                            restr,calc = G2mth.calcRamaEnergy(phi,psi,coeffDict[cofName])                               
1485                            print >>pFile,' %8.3f %8.3f %8.3f %8.3f %8.3f %8.3f %s'%(calc,obs,esd,(obs-calc)/esd,phi,psi,AtName[:-1])
1486                elif name == 'ChemComp':
1487                    print >>pFile,'     atoms   mul*frac  factor     prod'
1488                    for indx,factors,obs,esd in itemRest[rest]:
1489                        try:
1490                            atoms = G2mth.GetAtomItemsById(Atoms,AtLookup,indx,ct-1)
1491                            mul = np.array(G2mth.GetAtomItemsById(Atoms,AtLookup,indx,cs+1))
1492                            frac = np.array(G2mth.GetAtomItemsById(Atoms,AtLookup,indx,cs-1))
1493                            mulfrac = mul*frac
1494                            calcs = mul*frac*factors
1495                            for iatm,[atom,mf,fr,clc] in enumerate(zip(atoms,mulfrac,factors,calcs)):
1496                                print >>pFile,' %10s %8.3f %8.3f %8.3f'%(atom,mf,fr,clc)
1497                            print >>pFile,' Sum:                   calc: %8.3f obs: %8.3f esd: %8.3f'%(np.sum(calcs),obs,esd)
1498                        except KeyError:
1499                            del itemRest[rest]
1500                elif name == 'Texture' and textureData['Order']:
1501                    Start = False
1502                    SHCoef = textureData['SH Coeff'][1]
1503                    shModels = ['cylindrical','none','shear - 2/m','rolling - mmm']
1504                    SamSym = dict(zip(shModels,['0','-1','2/m','mmm']))
1505                    print '    HKL  grid  neg esd  sum neg  num neg use unit?  unit esd  '
1506                    for hkl,grid,esd1,ifesd2,esd2 in itemRest[rest]:
1507                        phi,beta = G2lat.CrsAng(np.array(hkl),cell,SGData)
1508                        ODFln = G2lat.Flnh(Start,SHCoef,phi,beta,SGData)
1509                        R,P,Z = G2mth.getRestPolefig(ODFln,SamSym[textureData['Model']],grid)
1510                        Z = ma.masked_greater(Z,0.0)
1511                        num = ma.count(Z)
1512                        sum = 0
1513                        if num:
1514                            sum = np.sum(Z)
1515                        print '   %d %d %d  %d %8.3f %8.3f %8d   %s    %8.3f'%(hkl[0],hkl[1],hkl[2],grid,esd1,sum,num,str(ifesd2),esd2)
1516       
1517def getCellEsd(pfx,SGData,A,covData):
1518    'needs a doc string'
1519    dpr = 180./np.pi
1520    rVsq = G2lat.calc_rVsq(A)
1521    G,g = G2lat.A2Gmat(A)       #get recip. & real metric tensors
1522    cell = np.array(G2lat.Gmat2cell(g))   #real cell
1523    cellst = np.array(G2lat.Gmat2cell(G)) #recip. cell
1524    scos = cosd(cellst[3:6])
1525    ssin = sind(cellst[3:6])
1526    scot = scos/ssin
1527    rcos = cosd(cell[3:6])
1528    rsin = sind(cell[3:6])
1529    rcot = rcos/rsin
1530    RMnames = [pfx+'A0',pfx+'A1',pfx+'A2',pfx+'A3',pfx+'A4',pfx+'A5']
1531    varyList = covData['varyList']
1532    covMatrix = covData['covMatrix']
1533    vcov = G2mth.getVCov(RMnames,varyList,covMatrix)
1534    if SGData['SGLaue'] in ['3', '3m1', '31m', '6/m', '6/mmm']:
1535        vcov[1,1] = vcov[3,3] = vcov[1,3] = vcov[3,1] = vcov[0,0]
1536        vcov[0,1] = vcov[1,0] = vcov[0,3] = vcov[3,0] = vcov[0,0]
1537        vcov[1,2] = vcov[2,1] = vcov[2,3] = vcov[3,2] = vcov[0,2]
1538    elif SGData['SGLaue'] in ['m3','m3m']:
1539        vcov[0:3,0:3] = vcov[0,0]
1540    elif SGData['SGLaue'] in ['4/m', '4/mmm']:
1541        vcov[0:2,0:2] = vcov[0,0]
1542        vcov[1,2] = vcov[2,1] = vcov[0,2]
1543    elif SGData['SGLaue'] in ['3R','3mR']:
1544        vcov[0:2,0:2] = vcov[0,0]
1545        vcov[4,4] = vcov[5,5] = vcov[3,3]
1546        vcov[0:3,3:6] = vcov[0,3]
1547        vcov[3:6,0:3] = vcov[3,0]
1548    Ax = np.array(A)
1549    Ax[3:] /= 2.
1550    drVdA = np.array([
1551        Ax[1]*Ax[2]-Ax[5]**2,
1552        Ax[0]*Ax[2]-Ax[4]**2,
1553        Ax[0]*Ax[1]-Ax[3]**2,
1554        Ax[4]*Ax[5]-Ax[2]*Ax[3],
1555        Ax[3]*Ax[5]-Ax[1]*Ax[4],
1556        Ax[3]*Ax[4]-Ax[0]*Ax[5]])
1557    srcvlsq = np.inner(drVdA,np.inner(drVdA,vcov))
1558    Vol = 1/np.sqrt(rVsq)
1559    sigVol = Vol**3*np.sqrt(srcvlsq)/2.         #ok - checks with GSAS
1560   
1561    R123 = Ax[0]*Ax[1]*Ax[2]
1562    dsasdg = np.zeros((3,6))
1563    dadg = np.zeros((6,6))
1564    for i0 in range(3):         #0  1   2
1565        i1 = (i0+1)%3           #1  2   0
1566        i2 = (i1+1)%3           #2  0   1
1567        i3 = 5-i2               #3  5   4
1568        i4 = 5-i1               #4  3   5
1569        i5 = 5-i0               #5  4   3
1570        dsasdg[i0][i1] = 0.5*scot[i0]*scos[i0]/Ax[i1]
1571        dsasdg[i0][i2] = 0.5*scot[i0]*scos[i0]/Ax[i2]
1572        dsasdg[i0][i5] = -scot[i0]/np.sqrt(Ax[i1]*Ax[i2])
1573        denmsq = Ax[i0]*(R123-Ax[i1]*Ax[i4]**2-Ax[i2]*Ax[i3]**2+(Ax[i4]*Ax[i3])**2)
1574        denom = np.sqrt(denmsq)
1575        dadg[i5][i0] = -Ax[i5]/denom-rcos[i0]/denmsq*(R123-0.5*Ax[i1]*Ax[i4]**2-0.5*Ax[i2]*Ax[i3]**2)
1576        dadg[i5][i1] = -0.5*rcos[i0]/denmsq*(Ax[i0]**2*Ax[i2]-Ax[i0]*Ax[i4]**2)
1577        dadg[i5][i2] = -0.5*rcos[i0]/denmsq*(Ax[i0]**2*Ax[i1]-Ax[i0]*Ax[i3]**2)
1578        dadg[i5][i3] = Ax[i4]/denom+rcos[i0]/denmsq*(Ax[i0]*Ax[i2]*Ax[i3]-Ax[i3]*Ax[i4]**2)
1579        dadg[i5][i4] = Ax[i3]/denom+rcos[i0]/denmsq*(Ax[i0]*Ax[i1]*Ax[i4]-Ax[i3]**2*Ax[i4])
1580        dadg[i5][i5] = -Ax[i0]/denom
1581    for i0 in range(3):
1582        i1 = (i0+1)%3
1583        i2 = (i1+1)%3
1584        i3 = 5-i2
1585        for ij in range(6):
1586            dadg[i0][ij] = cell[i0]*(rcot[i2]*dadg[i3][ij]/rsin[i2]-dsasdg[i1][ij]/ssin[i1])
1587            if ij == i0:
1588                dadg[i0][ij] = dadg[i0][ij]-0.5*cell[i0]/Ax[i0]
1589            dadg[i3][ij] = -dadg[i3][ij]*rsin[2-i0]*dpr
1590#    GSASIIpath.IPyBreak()
1591    sigMat = np.inner(dadg,np.inner(dadg,vcov))
1592    var = np.diag(sigMat)
1593    CS = np.where(var>0.,np.sqrt(var),0.)
1594    return [CS[0],CS[1],CS[2],CS[5],CS[4],CS[3],sigVol]
1595   
1596def SetPhaseData(parmDict,sigDict,Phases,RBIds,covData,RestraintDict=None,pFile=None):
1597    'needs a doc string'
1598   
1599    def PrintAtomsAndSig(General,Atoms,atomsSig):
1600        print >>pFile,'\n Atoms:'
1601        line = '   name      x         y         z      frac   Uiso     U11     U22     U33     U12     U13     U23'
1602        if General['Type'] == 'macromolecular':
1603            line = ' res no residue chain '+line
1604        cx,ct,cs,cia = General['AtomPtrs']
1605        print >>pFile,line
1606        print >>pFile,135*'-'
1607        fmt = {0:'%7s',ct:'%7s',cx:'%10.5f',cx+1:'%10.5f',cx+2:'%10.5f',cx+3:'%8.3f',cia+1:'%8.5f',
1608            cia+2:'%8.5f',cia+3:'%8.5f',cia+4:'%8.5f',cia+5:'%8.5f',cia+6:'%8.5f',cia+7:'%8.5f'}
1609        noFXsig = {cx:[10*' ','%10s'],cx+1:[10*' ','%10s'],cx+2:[10*' ','%10s'],cx+3:[8*' ','%8s']}
1610        for atyp in General['AtomTypes']:       #zero composition
1611            General['NoAtoms'][atyp] = 0.
1612        for i,at in enumerate(Atoms):
1613            General['NoAtoms'][at[ct]] += at[cx+3]*float(at[cx+5])     #new composition
1614            if General['Type'] == 'macromolecular':
1615                name = ' %s %s %s %s:'%(at[0],at[1],at[2],at[3])
1616                valstr = ' values:          '
1617                sigstr = ' sig   :          '
1618            else:
1619                name = fmt[0]%(at[ct-1])+fmt[1]%(at[ct])+':'
1620                valstr = ' values:'
1621                sigstr = ' sig   :'
1622            for ind in range(cx,cx+4):
1623                sigind = str(i)+':'+str(ind)
1624                valstr += fmt[ind]%(at[ind])                   
1625                if sigind in atomsSig:
1626                    sigstr += fmt[ind]%(atomsSig[sigind])
1627                else:
1628                    sigstr += noFXsig[ind][1]%(noFXsig[ind][0])
1629            if at[cia] == 'I':
1630                valstr += fmt[cia+1]%(at[cia+1])
1631                if '%d:%d'%(i,cia+1) in atomsSig:
1632                    sigstr += fmt[cia+1]%(atomsSig['%d:%d'%(i,cia+1)])
1633                else:
1634                    sigstr += 8*' '
1635            else:
1636                valstr += 8*' '
1637                sigstr += 8*' '
1638                for ind in range(cia+2,cia+8):
1639                    sigind = str(i)+':'+str(ind)
1640                    valstr += fmt[ind]%(at[ind])
1641                    if sigind in atomsSig:                       
1642                        sigstr += fmt[ind]%(atomsSig[sigind])
1643                    else:
1644                        sigstr += 8*' '
1645            print >>pFile,name
1646            print >>pFile,valstr
1647            print >>pFile,sigstr
1648           
1649    def PrintMomentsAndSig(General,Atoms,atomsSig):
1650        print >>pFile,'\n Magnetic Moments:'    #add magnitude & angle, etc.? TBD
1651        line = '   name      Mx        My        Mz'
1652        cx,ct,cs,cia = General['AtomPtrs']
1653        cmx = cx+4
1654        AtInfo = dict(zip(General['AtomTypes'],General['Lande g']))
1655        print >>pFile,line
1656        print >>pFile,135*'-'
1657        fmt = {0:'%7s',ct:'%7s',cmx:'%10.5f',cmx+1:'%10.5f',cmx+2:'%10.5f'}
1658        noFXsig = {cmx:[10*' ','%10s'],cmx+1:[10*' ','%10s'],cmx+2:[10*' ','%10s']}
1659        for i,at in enumerate(Atoms):
1660            if AtInfo[at[ct]]:
1661                name = fmt[0]%(at[ct-1])+fmt[1]%(at[ct])+':'
1662                valstr = ' values:'
1663                sigstr = ' sig   :'
1664                for ind in range(cmx,cmx+3):
1665                    sigind = str(i)+':'+str(ind)
1666                    valstr += fmt[ind]%(at[ind])                   
1667                    if sigind in atomsSig:
1668                        sigstr += fmt[ind]%(atomsSig[sigind])
1669                    else:
1670                        sigstr += noFXsig[ind][1]%(noFXsig[ind][0])
1671                print >>pFile,name
1672                print >>pFile,valstr
1673                print >>pFile,sigstr
1674           
1675    def PrintWavesAndSig(General,Atoms,wavesSig):
1676        cx,ct,cs,cia = General['AtomPtrs']
1677        print >>pFile,'\n Modulation waves'
1678        names = {'Sfrac':['Fsin','Fcos','Fzero','Fwid'],'Spos':['Xsin','Ysin','Zsin','Xcos','Ycos','Zcos','Tmin','Tmax','Xmax','Ymax','Zmax'],
1679            'Sadp':['U11sin','U22sin','U33sin','U12sin','U13sin','U23sin','U11cos','U22cos',
1680            'U33cos','U12cos','U13cos','U23cos'],'Smag':['MXsin','MYsin','MZsin','MXcos','MYcos','MZcos']}
1681        print >>pFile,135*'-'
1682        for i,at in enumerate(Atoms):
1683            AtomSS = at[-1]['SS1']
1684            waveType = AtomSS['waveType']
1685            for Stype in ['Sfrac','Spos','Sadp','Smag']:
1686                Waves = AtomSS[Stype]
1687                if len(Waves):
1688                    print >>pFile,' atom: %s, site sym: %s, type: %s wave type: %s:'    \
1689                        %(at[ct-1],at[cs],Stype,waveType)
1690                    for iw,wave in enumerate(Waves):
1691                        stiw = ':'+str(i)+':'+str(iw)
1692                        namstr = '  names :'
1693                        valstr = '  values:'
1694                        sigstr = '  esds  :'
1695                        if Stype == 'Spos':
1696                            nt = 6
1697                            ot = 0
1698                            if waveType in ['ZigZag','Block',] and not iw:
1699                                nt = 5
1700                                ot = 6
1701                            for j in range(nt):
1702                                name = names['Spos'][j+ot]
1703                                namstr += '%12s'%(name)
1704                                valstr += '%12.4f'%(wave[0][j])
1705                                if name+stiw in wavesSig:
1706                                    sigstr += '%12.4f'%(wavesSig[name+stiw])
1707                                else:
1708                                    sigstr += 12*' '
1709                        elif Stype == 'Sfrac':
1710                            ot = 0
1711                            if 'Crenel' in waveType and not iw:
1712                                ot = 2
1713                            for j in range(2):
1714                                name = names['Sfrac'][j+ot]
1715                                namstr += '%12s'%(names['Sfrac'][j+ot])
1716                                valstr += '%12.4f'%(wave[0][j])
1717                                if name+stiw in wavesSig:
1718                                    sigstr += '%12.4f'%(wavesSig[name+stiw])
1719                                else:
1720                                    sigstr += 12*' '
1721                        elif Stype == 'Sadp':
1722                            for j in range(12):
1723                                name = names['Sadp'][j]
1724                                namstr += '%10s'%(names['Sadp'][j])
1725                                valstr += '%10.6f'%(wave[0][j])
1726                                if name+stiw in wavesSig:
1727                                    sigstr += '%10.6f'%(wavesSig[name+stiw])
1728                                else:
1729                                    sigstr += 10*' '
1730                        elif Stype == 'Smag':
1731                            for j in range(6):
1732                                name = names['Smag'][j]
1733                                namstr += '%12s'%(names['Smag'][j])
1734                                valstr += '%12.4f'%(wave[0][j])
1735                                if name+stiw in wavesSig:
1736                                    sigstr += '%12.4f'%(wavesSig[name+stiw])
1737                                else:
1738                                    sigstr += 12*' '
1739                               
1740                    print >>pFile,namstr
1741                    print >>pFile,valstr
1742                    print >>pFile,sigstr
1743       
1744               
1745    def PrintRBObjPOAndSig(rbfx,rbsx):
1746        namstr = '  names :'
1747        valstr = '  values:'
1748        sigstr = '  esds  :'
1749        for i,px in enumerate(['Px:','Py:','Pz:']):
1750            name = pfx+rbfx+px+rbsx
1751            namstr += '%12s'%('Pos '+px[1])
1752            valstr += '%12.5f'%(parmDict[name])
1753            if name in sigDict:
1754                sigstr += '%12.5f'%(sigDict[name])
1755            else:
1756                sigstr += 12*' '
1757        for i,po in enumerate(['Oa:','Oi:','Oj:','Ok:']):
1758            name = pfx+rbfx+po+rbsx
1759            namstr += '%12s'%('Ori '+po[1])
1760            valstr += '%12.5f'%(parmDict[name])
1761            if name in sigDict:
1762                sigstr += '%12.5f'%(sigDict[name])
1763            else:
1764                sigstr += 12*' '
1765        print >>pFile,namstr
1766        print >>pFile,valstr
1767        print >>pFile,sigstr
1768       
1769    def PrintRBObjTLSAndSig(rbfx,rbsx,TLS):
1770        namstr = '  names :'
1771        valstr = '  values:'
1772        sigstr = '  esds  :'
1773        if 'N' not in TLS:
1774            print >>pFile,' Thermal motion:'
1775        if 'T' in TLS:
1776            for i,pt in enumerate(['T11:','T22:','T33:','T12:','T13:','T23:']):
1777                name = pfx+rbfx+pt+rbsx
1778                namstr += '%12s'%(pt[:3])
1779                valstr += '%12.5f'%(parmDict[name])
1780                if name in sigDict:
1781                    sigstr += '%12.5f'%(sigDict[name])
1782                else:
1783                    sigstr += 12*' '
1784            print >>pFile,namstr
1785            print >>pFile,valstr
1786            print >>pFile,sigstr
1787        if 'L' in TLS:
1788            namstr = '  names :'
1789            valstr = '  values:'
1790            sigstr = '  esds  :'
1791            for i,pt in enumerate(['L11:','L22:','L33:','L12:','L13:','L23:']):
1792                name = pfx+rbfx+pt+rbsx
1793                namstr += '%12s'%(pt[:3])
1794                valstr += '%12.3f'%(parmDict[name])
1795                if name in sigDict:
1796                    sigstr += '%12.3f'%(sigDict[name])
1797                else:
1798                    sigstr += 12*' '
1799            print >>pFile,namstr
1800            print >>pFile,valstr
1801            print >>pFile,sigstr
1802        if 'S' in TLS:
1803            namstr = '  names :'
1804            valstr = '  values:'
1805            sigstr = '  esds  :'
1806            for i,pt in enumerate(['S12:','S13:','S21:','S23:','S31:','S32:','SAA:','SBB:']):
1807                name = pfx+rbfx+pt+rbsx
1808                namstr += '%12s'%(pt[:3])
1809                valstr += '%12.4f'%(parmDict[name])
1810                if name in sigDict:
1811                    sigstr += '%12.4f'%(sigDict[name])
1812                else:
1813                    sigstr += 12*' '
1814            print >>pFile,namstr
1815            print >>pFile,valstr
1816            print >>pFile,sigstr
1817        if 'U' in TLS:
1818            name = pfx+rbfx+'U:'+rbsx
1819            namstr = '  names :'+'%12s'%('Uiso')
1820            valstr = '  values:'+'%12.5f'%(parmDict[name])
1821            if name in sigDict:
1822                sigstr = '  esds  :'+'%12.5f'%(sigDict[name])
1823            else:
1824                sigstr = '  esds  :'+12*' '
1825            print >>pFile,namstr
1826            print >>pFile,valstr
1827            print >>pFile,sigstr
1828       
1829    def PrintRBObjTorAndSig(rbsx):
1830        namstr = '  names :'
1831        valstr = '  values:'
1832        sigstr = '  esds  :'
1833        nTors = len(RBObj['Torsions'])
1834        if nTors:
1835            print >>pFile,' Torsions:'
1836            for it in range(nTors):
1837                name = pfx+'RBRTr;'+str(it)+':'+rbsx
1838                namstr += '%12s'%('Tor'+str(it))
1839                valstr += '%12.4f'%(parmDict[name])
1840                if name in sigDict:
1841                    sigstr += '%12.4f'%(sigDict[name])
1842            print >>pFile,namstr
1843            print >>pFile,valstr
1844            print >>pFile,sigstr
1845               
1846    def PrintSHtextureAndSig(textureData,SHtextureSig):
1847        print >>pFile,'\n Spherical harmonics texture: Order:' + str(textureData['Order'])
1848        names = ['omega','chi','phi']
1849        namstr = '  names :'
1850        ptstr =  '  values:'
1851        sigstr = '  esds  :'
1852        for name in names:
1853            namstr += '%12s'%(name)
1854            ptstr += '%12.3f'%(textureData['Sample '+name][1])
1855            if 'Sample '+name in SHtextureSig:
1856                sigstr += '%12.3f'%(SHtextureSig['Sample '+name])
1857            else:
1858                sigstr += 12*' '
1859        print >>pFile,namstr
1860        print >>pFile,ptstr
1861        print >>pFile,sigstr
1862        print >>pFile,'\n Texture coefficients:'
1863        SHcoeff = textureData['SH Coeff'][1]
1864        SHkeys = SHcoeff.keys()
1865        nCoeff = len(SHcoeff)
1866        nBlock = nCoeff/10+1
1867        iBeg = 0
1868        iFin = min(iBeg+10,nCoeff)
1869        for block in range(nBlock):
1870            namstr = '  names :'
1871            ptstr =  '  values:'
1872            sigstr = '  esds  :'
1873            for name in SHkeys[iBeg:iFin]:
1874                namstr += '%12s'%(name)
1875                ptstr += '%12.3f'%(SHcoeff[name])
1876                if name in SHtextureSig:
1877                    sigstr += '%12.3f'%(SHtextureSig[name])
1878                else:
1879                    sigstr += 12*' '
1880            print >>pFile,namstr
1881            print >>pFile,ptstr
1882            print >>pFile,sigstr
1883            iBeg += 10
1884            iFin = min(iBeg+10,nCoeff)
1885           
1886    print >>pFile,'\n Phases:'
1887    for phase in Phases:
1888        print >>pFile,' Result for phase: ',phase
1889        print >>pFile,135*'='
1890        Phase = Phases[phase]
1891        General = Phase['General']
1892        SGData = General['SGData']
1893        Atoms = Phase['Atoms']
1894        if Atoms and not General.get('doPawley'):
1895            cx,ct,cs,cia = General['AtomPtrs']
1896            AtLookup = G2mth.FillAtomLookUp(Atoms,cia+8)
1897        cell = General['Cell']
1898        pId = Phase['pId']
1899        pfx = str(pId)+'::'
1900        if cell[0]:
1901            A,sigA = cellFill(pfx,SGData,parmDict,sigDict)
1902            cellSig = getCellEsd(pfx,SGData,A,covData)  #includes sigVol
1903            print >>pFile,' Reciprocal metric tensor: '
1904            ptfmt = "%15.9f"
1905            names = ['A11','A22','A33','A12','A13','A23']
1906            namstr = '  names :'
1907            ptstr =  '  values:'
1908            sigstr = '  esds  :'
1909            for name,a,siga in zip(names,A,sigA):
1910                namstr += '%15s'%(name)
1911                ptstr += ptfmt%(a)
1912                if siga:
1913                    sigstr += ptfmt%(siga)
1914                else:
1915                    sigstr += 15*' '
1916            print >>pFile,namstr
1917            print >>pFile,ptstr
1918            print >>pFile,sigstr
1919            cell[1:7] = G2lat.A2cell(A)
1920            cell[7] = G2lat.calc_V(A)
1921            print >>pFile,' New unit cell:'
1922            ptfmt = ["%12.6f","%12.6f","%12.6f","%12.4f","%12.4f","%12.4f","%12.3f"]
1923            names = ['a','b','c','alpha','beta','gamma','Volume']
1924            namstr = '  names :'
1925            ptstr =  '  values:'
1926            sigstr = '  esds  :'
1927            for name,fmt,a,siga in zip(names,ptfmt,cell[1:8],cellSig):
1928                namstr += '%12s'%(name)
1929                ptstr += fmt%(a)
1930                if siga:
1931                    sigstr += fmt%(siga)
1932                else:
1933                    sigstr += 12*' '
1934            print >>pFile,namstr
1935            print >>pFile,ptstr
1936            print >>pFile,sigstr
1937        ik = 6  #for Pawley stuff below
1938        if General.get('Modulated',False):
1939            ik = 7
1940            Vec,vRef,maxH = General['SuperVec']
1941            if vRef:
1942                print >>pFile,' New modulation vector:'
1943                namstr = '  names :'
1944                ptstr =  '  values:'
1945                sigstr = '  esds  :'
1946                for var in ['mV0','mV1','mV2']:
1947                    namstr += '%12s'%(pfx+var)
1948                    ptstr += '%12.6f'%(parmDict[pfx+var])
1949                    if pfx+var in sigDict:
1950                        sigstr += '%12.6f'%(sigDict[pfx+var])
1951                    else:
1952                        sigstr += 12*' '
1953                print >>pFile,namstr
1954                print >>pFile,ptstr
1955                print >>pFile,sigstr
1956           
1957        General['Mass'] = 0.
1958        if Phase['General'].get('doPawley'):
1959            pawleyRef = Phase['Pawley ref']
1960            for i,refl in enumerate(pawleyRef):
1961                key = pfx+'PWLref:'+str(i)
1962                refl[ik] = parmDict[key]
1963                if key in sigDict:
1964                    refl[ik+1] = sigDict[key]
1965                else:
1966                    refl[ik+1] = 0
1967        else:
1968            VRBIds = RBIds['Vector']
1969            RRBIds = RBIds['Residue']
1970            RBModels = Phase['RBModels']
1971            for irb,RBObj in enumerate(RBModels.get('Vector',[])):
1972                jrb = VRBIds.index(RBObj['RBId'])
1973                rbsx = str(irb)+':'+str(jrb)
1974                print >>pFile,' Vector rigid body parameters:'
1975                PrintRBObjPOAndSig('RBV',rbsx)
1976                PrintRBObjTLSAndSig('RBV',rbsx,RBObj['ThermalMotion'][0])
1977            for irb,RBObj in enumerate(RBModels.get('Residue',[])):
1978                jrb = RRBIds.index(RBObj['RBId'])
1979                rbsx = str(irb)+':'+str(jrb)
1980                print >>pFile,' Residue rigid body parameters:'
1981                PrintRBObjPOAndSig('RBR',rbsx)
1982                PrintRBObjTLSAndSig('RBR',rbsx,RBObj['ThermalMotion'][0])
1983                PrintRBObjTorAndSig(rbsx)
1984            atomsSig = {}
1985            wavesSig = {}
1986            cx,ct,cs,cia = General['AtomPtrs']
1987            for i,at in enumerate(Atoms):
1988                names = {cx:pfx+'Ax:'+str(i),cx+1:pfx+'Ay:'+str(i),cx+2:pfx+'Az:'+str(i),cx+3:pfx+'Afrac:'+str(i),
1989                    cia+1:pfx+'AUiso:'+str(i),cia+2:pfx+'AU11:'+str(i),cia+3:pfx+'AU22:'+str(i),cia+4:pfx+'AU33:'+str(i),
1990                    cia+5:pfx+'AU12:'+str(i),cia+6:pfx+'AU13:'+str(i),cia+7:pfx+'AU23:'+str(i),
1991                    cx+4:pfx+'AMx:'+str(i),cx+5:pfx+'AMy:'+str(i),cx+6:pfx+'AMz:'+str(i)}
1992                for ind in range(cx,cx+4):
1993                    at[ind] = parmDict[names[ind]]
1994                    if ind in range(cx,cx+3):
1995                        name = names[ind].replace('A','dA')
1996                    else:
1997                        name = names[ind]
1998                    if name in sigDict:
1999                        atomsSig[str(i)+':'+str(ind)] = sigDict[name]
2000                if at[cia] == 'I':
2001                    at[cia+1] = parmDict[names[cia+1]]
2002                    if names[cia+1] in sigDict:
2003                        atomsSig['%d:%d'%(i,cia+1)] = sigDict[names[cia+1]]
2004                else:
2005                    for ind in range(cia+2,cia+8):
2006                        at[ind] = parmDict[names[ind]]
2007                        if names[ind] in sigDict:
2008                            atomsSig[str(i)+':'+str(ind)] = sigDict[names[ind]]
2009                if General['Type'] == 'magnetic':
2010                    for ind in range(cx+4,cx+7):
2011                        at[ind] = parmDict[names[ind]]
2012                        if names[ind] in sigDict:
2013                            atomsSig[str(i)+':'+str(ind)] = sigDict[names[ind]]
2014                ind = General['AtomTypes'].index(at[ct])
2015                General['Mass'] += General['AtomMass'][ind]*at[cx+3]*at[cx+5]
2016                if General.get('Modulated',False):
2017                    AtomSS = at[-1]['SS1']
2018                    waveType = AtomSS['waveType']
2019                    for Stype in ['Sfrac','Spos','Sadp','Smag']:
2020                        Waves = AtomSS[Stype]
2021                        for iw,wave in enumerate(Waves):
2022                            stiw = str(i)+':'+str(iw)
2023                            if Stype == 'Spos':
2024                                if waveType in ['ZigZag','Block',] and not iw:
2025                                    names = ['Tmin:'+stiw,'Tmax:'+stiw,'Xmax:'+stiw,'Ymax:'+stiw,'Zmax:'+stiw]
2026                                else:
2027                                    names = ['Xsin:'+stiw,'Ysin:'+stiw,'Zsin:'+stiw,
2028                                        'Xcos:'+stiw,'Ycos:'+stiw,'Zcos:'+stiw]
2029                            elif Stype == 'Sadp':
2030                                names = ['U11sin:'+stiw,'U22sin:'+stiw,'U33sin:'+stiw,
2031                                    'U12sin:'+stiw,'U13sin:'+stiw,'U23sin:'+stiw,
2032                                    'U11cos:'+stiw,'U22cos:'+stiw,'U33cos:'+stiw,
2033                                    'U12cos:'+stiw,'U13cos:'+stiw,'U23cos:'+stiw]
2034                            elif Stype == 'Sfrac':
2035                                if 'Crenel' in waveType and not iw:
2036                                    names = ['Fzero:'+stiw,'Fwid:'+stiw]
2037                                else:
2038                                    names = ['Fsin:'+stiw,'Fcos:'+stiw]
2039                            elif Stype == 'Smag':
2040                                names = ['MXsin:'+stiw,'MYsin:'+stiw,'MZsin:'+stiw,
2041                                    'MXcos:'+stiw,'MYcos:'+stiw,'MZcos:'+stiw]
2042                            for iname,name in enumerate(names):
2043                                AtomSS[Stype][iw][0][iname] = parmDict[pfx+name]
2044                                if pfx+name in sigDict:
2045                                    wavesSig[name] = sigDict[pfx+name]
2046                   
2047            PrintAtomsAndSig(General,Atoms,atomsSig)
2048            if General['Type'] == 'magnetic':
2049                PrintMomentsAndSig(General,Atoms,atomsSig)
2050            if General.get('Modulated',False):
2051                PrintWavesAndSig(General,Atoms,wavesSig)
2052           
2053       
2054        textureData = General['SH Texture']   
2055        if textureData['Order']:
2056            SHtextureSig = {}
2057            for name in ['omega','chi','phi']:
2058                aname = pfx+'SH '+name
2059                textureData['Sample '+name][1] = parmDict[aname]
2060                if aname in sigDict:
2061                    SHtextureSig['Sample '+name] = sigDict[aname]
2062            for name in textureData['SH Coeff'][1]:
2063                aname = pfx+name
2064                textureData['SH Coeff'][1][name] = parmDict[aname]
2065                if aname in sigDict:
2066                    SHtextureSig[name] = sigDict[aname]
2067            PrintSHtextureAndSig(textureData,SHtextureSig)
2068        if phase in RestraintDict and not Phase['General'].get('doPawley'):
2069            PrintRestraints(cell[1:7],SGData,General['AtomPtrs'],Atoms,AtLookup,
2070                textureData,RestraintDict[phase],pFile)
2071                   
2072################################################################################
2073##### Histogram & Phase data
2074################################################################################       
2075                   
2076def GetHistogramPhaseData(Phases,Histograms,Print=True,pFile=None,resetRefList=True):
2077    '''Loads the HAP histogram/phase information into dicts
2078
2079    :param dict Phases: phase information
2080    :param dict Histograms: Histogram information
2081    :param bool Print: prints information as it is read
2082    :param file pFile: file object to print to (the default, None causes printing to the console)
2083    :param bool resetRefList: Should the contents of the Reflection List be initialized
2084      on loading. The default, True, initializes the Reflection List as it is loaded.
2085
2086    :returns: (hapVary,hapDict,controlDict)
2087      * hapVary: list of refined variables
2088      * hapDict: dict with refined variables and their values
2089      * controlDict: dict with fixed parameters
2090    '''
2091   
2092    def PrintSize(hapData):
2093        if hapData[0] in ['isotropic','uniaxial']:
2094            line = '\n Size model    : %9s'%(hapData[0])
2095            line += ' equatorial:'+'%12.5f'%(hapData[1][0])+' Refine? '+str(hapData[2][0])
2096            if hapData[0] == 'uniaxial':
2097                line += ' axial:'+'%12.5f'%(hapData[1][1])+' Refine? '+str(hapData[2][1])
2098            line += '\n\t LG mixing coeff.: %12.4f'%(hapData[1][2])+' Refine? '+str(hapData[2][2])
2099            print >>pFile,line
2100        else:
2101            print >>pFile,'\n Size model    : %s'%(hapData[0])+ \
2102                '\n\t LG mixing coeff.:%12.4f'%(hapData[1][2])+' Refine? '+str(hapData[2][2])
2103            Snames = ['S11','S22','S33','S12','S13','S23']
2104            ptlbls = ' names :'
2105            ptstr =  ' values:'
2106            varstr = ' refine:'
2107            for i,name in enumerate(Snames):
2108                ptlbls += '%12s' % (name)
2109                ptstr += '%12.6f' % (hapData[4][i])
2110                varstr += '%12s' % (str(hapData[5][i]))
2111            print >>pFile,ptlbls
2112            print >>pFile,ptstr
2113            print >>pFile,varstr
2114       
2115    def PrintMuStrain(hapData,SGData):
2116        if hapData[0] in ['isotropic','uniaxial']:
2117            line = '\n Mustrain model: %9s'%(hapData[0])
2118            line += ' equatorial:'+'%12.1f'%(hapData[1][0])+' Refine? '+str(hapData[2][0])
2119            if hapData[0] == 'uniaxial':
2120                line += ' axial:'+'%12.1f'%(hapData[1][1])+' Refine? '+str(hapData[2][1])
2121            line +='\n\t LG mixing coeff.:%12.4f'%(hapData[1][2])+' Refine? '+str(hapData[2][2])
2122            print >>pFile,line
2123        else:
2124            print >>pFile,'\n Mustrain model: %s'%(hapData[0])+ \
2125                '\n\t LG mixing coeff.:%12.4f'%(hapData[1][2])+' Refine? '+str(hapData[2][2])
2126            Snames = G2spc.MustrainNames(SGData)
2127            ptlbls = ' names :'
2128            ptstr =  ' values:'
2129            varstr = ' refine:'
2130            for i,name in enumerate(Snames):
2131                ptlbls += '%12s' % (name)
2132                ptstr += '%12.6f' % (hapData[4][i])
2133                varstr += '%12s' % (str(hapData[5][i]))
2134            print >>pFile,ptlbls
2135            print >>pFile,ptstr
2136            print >>pFile,varstr
2137
2138    def PrintHStrain(hapData,SGData):
2139        print >>pFile,'\n Hydrostatic/elastic strain: '
2140        Hsnames = G2spc.HStrainNames(SGData)
2141        ptlbls = ' names :'
2142        ptstr =  ' values:'
2143        varstr = ' refine:'
2144        for i,name in enumerate(Hsnames):
2145            ptlbls += '%12s' % (name)
2146            ptstr += '%12.4g' % (hapData[0][i])
2147            varstr += '%12s' % (str(hapData[1][i]))
2148        print >>pFile,ptlbls
2149        print >>pFile,ptstr
2150        print >>pFile,varstr
2151
2152    def PrintSHPO(hapData):
2153        print >>pFile,'\n Spherical harmonics preferred orientation: Order:' + \
2154            str(hapData[4])+' Refine? '+str(hapData[2])
2155        ptlbls = ' names :'
2156        ptstr =  ' values:'
2157        for item in hapData[5]:
2158            ptlbls += '%12s'%(item)
2159            ptstr += '%12.3f'%(hapData[5][item]) 
2160        print >>pFile,ptlbls
2161        print >>pFile,ptstr
2162   
2163    def PrintBabinet(hapData):
2164        print >>pFile,'\n Babinet form factor modification: '
2165        ptlbls = ' names :'
2166        ptstr =  ' values:'
2167        varstr = ' refine:'
2168        for name in ['BabA','BabU']:
2169            ptlbls += '%12s' % (name)
2170            ptstr += '%12.6f' % (hapData[name][0])
2171            varstr += '%12s' % (str(hapData[name][1]))
2172        print >>pFile,ptlbls
2173        print >>pFile,ptstr
2174        print >>pFile,varstr
2175       
2176    hapDict = {}
2177    hapVary = []
2178    controlDict = {}
2179   
2180    for phase in Phases:
2181        HistoPhase = Phases[phase]['Histograms']
2182        SGData = Phases[phase]['General']['SGData']
2183        cell = Phases[phase]['General']['Cell'][1:7]
2184        A = G2lat.cell2A(cell)
2185        if Phases[phase]['General'].get('Modulated',False):
2186            SSGData = Phases[phase]['General']['SSGData']
2187            Vec,x,maxH = Phases[phase]['General']['SuperVec']
2188        pId = Phases[phase]['pId']
2189        histoList = HistoPhase.keys()
2190        histoList.sort()
2191        for histogram in histoList:
2192            try:
2193                Histogram = Histograms[histogram]
2194            except KeyError:                       
2195                #skip if histogram not included e.g. in a sequential refinement
2196                continue
2197            hapData = HistoPhase[histogram]
2198            hId = Histogram['hId']
2199            if 'PWDR' in histogram:
2200                limits = Histogram['Limits'][1]
2201                inst = Histogram['Instrument Parameters'][0]
2202                if 'C' in inst['Type'][1]:
2203                    try:
2204                        wave = inst['Lam'][1]
2205                    except KeyError:
2206                        wave = inst['Lam1'][1]
2207                    dmin = wave/(2.0*sind(limits[1]/2.0))
2208                elif 'T' in inst['Type'][0]:
2209                    dmin = limits[0]/inst['difC'][1]
2210                if Phases[phase]['General']['Type'] == 'magnetic':
2211                    dmin = max(dmin,Phases[phase]['General']['MagDmin'])
2212                pfx = str(pId)+':'+str(hId)+':'
2213                for item in ['Scale','Extinction']:
2214                    hapDict[pfx+item] = hapData[item][0]
2215                    if hapData[item][1]:
2216                        hapVary.append(pfx+item)
2217                names = G2spc.HStrainNames(SGData)
2218                HSvals = []
2219                for i,name in enumerate(names):
2220                    hapDict[pfx+name] = hapData['HStrain'][0][i]
2221                    HSvals.append(hapDict[pfx+name])
2222                    if hapData['HStrain'][1][i]:
2223                        hapVary.append(pfx+name)
2224                controlDict[pfx+'poType'] = hapData['Pref.Ori.'][0]
2225                if hapData['Pref.Ori.'][0] == 'MD':
2226                    hapDict[pfx+'MD'] = hapData['Pref.Ori.'][1]
2227                    controlDict[pfx+'MDAxis'] = hapData['Pref.Ori.'][3]
2228                    if hapData['Pref.Ori.'][2]:
2229                        hapVary.append(pfx+'MD')
2230                else:                           #'SH' spherical harmonics
2231                    controlDict[pfx+'SHord'] = hapData['Pref.Ori.'][4]
2232                    controlDict[pfx+'SHncof'] = len(hapData['Pref.Ori.'][5])
2233                    controlDict[pfx+'SHnames'] = G2lat.GenSHCoeff(SGData['SGLaue'],'0',controlDict[pfx+'SHord'],False)
2234                    controlDict[pfx+'SHhkl'] = []
2235                    try: #patch for old Pref.Ori. items
2236                        controlDict[pfx+'SHtoler'] = 0.1
2237                        if hapData['Pref.Ori.'][6][0] != '':
2238                            controlDict[pfx+'SHhkl'] = [eval(a.replace(' ',',')) for a in hapData['Pref.Ori.'][6]]
2239                        controlDict[pfx+'SHtoler'] = hapData['Pref.Ori.'][7]
2240                    except IndexError:
2241                        pass
2242                    for item in hapData['Pref.Ori.'][5]:
2243                        hapDict[pfx+item] = hapData['Pref.Ori.'][5][item]
2244                        if hapData['Pref.Ori.'][2]:
2245                            hapVary.append(pfx+item)
2246                for item in ['Mustrain','Size']:
2247                    controlDict[pfx+item+'Type'] = hapData[item][0]
2248                    hapDict[pfx+item+';mx'] = hapData[item][1][2]
2249                    if hapData[item][2][2]:
2250                        hapVary.append(pfx+item+';mx')
2251                    if hapData[item][0] in ['isotropic','uniaxial']:
2252                        hapDict[pfx+item+';i'] = hapData[item][1][0]
2253                        if hapData[item][2][0]:
2254                            hapVary.append(pfx+item+';i')
2255                        if hapData[item][0] == 'uniaxial':
2256                            controlDict[pfx+item+'Axis'] = hapData[item][3]
2257                            hapDict[pfx+item+';a'] = hapData[item][1][1]
2258                            if hapData[item][2][1]:
2259                                hapVary.append(pfx+item+';a')
2260                    else:       #generalized for mustrain or ellipsoidal for size
2261                        Nterms = len(hapData[item][4])
2262                        if item == 'Mustrain':
2263                            names = G2spc.MustrainNames(SGData)
2264                            pwrs = []
2265                            for name in names:
2266                                h,k,l = name[1:]
2267                                pwrs.append([int(h),int(k),int(l)])
2268                            controlDict[pfx+'MuPwrs'] = pwrs
2269                        for i in range(Nterms):
2270                            sfx = ';'+str(i)
2271                            hapDict[pfx+item+sfx] = hapData[item][4][i]
2272                            if hapData[item][5][i]:
2273                                hapVary.append(pfx+item+sfx)
2274                if Phases[phase]['General']['Type'] != 'magnetic':
2275                    for bab in ['BabA','BabU']:
2276                        hapDict[pfx+bab] = hapData['Babinet'][bab][0]
2277                        if hapData['Babinet'][bab][1]:
2278                            hapVary.append(pfx+bab)
2279                               
2280                if Print: 
2281                    print >>pFile,'\n Phase: ',phase,' in histogram: ',histogram
2282                    print >>pFile,135*'='
2283                    print >>pFile,' Phase fraction  : %10.4f'%(hapData['Scale'][0]),' Refine?',hapData['Scale'][1]
2284                    print >>pFile,' Extinction coeff: %10.4f'%(hapData['Extinction'][0]),' Refine?',hapData['Extinction'][1]
2285                    if hapData['Pref.Ori.'][0] == 'MD':
2286                        Ax = hapData['Pref.Ori.'][3]
2287                        print >>pFile,' March-Dollase PO: %10.4f'%(hapData['Pref.Ori.'][1]),' Refine?',hapData['Pref.Ori.'][2], \
2288                            ' Axis: %d %d %d'%(Ax[0],Ax[1],Ax[2])
2289                    else: #'SH' for spherical harmonics
2290                        PrintSHPO(hapData['Pref.Ori.'])
2291                        print >>pFile,' Penalty hkl list: '+str(controlDict[pfx+'SHhkl'])+' tolerance: %.2f'%(controlDict[pfx+'SHtoler'])
2292                    PrintSize(hapData['Size'])
2293                    PrintMuStrain(hapData['Mustrain'],SGData)
2294                    PrintHStrain(hapData['HStrain'],SGData)
2295                    if Phases[phase]['General']['Type'] != 'magnetic':
2296                        if hapData['Babinet']['BabA'][0]:
2297                            PrintBabinet(hapData['Babinet'])                       
2298                if resetRefList:
2299                    refList = []
2300                    Uniq = []
2301                    Phi = []
2302                    useExt = 'magnetic' in Phases[phase]['General']['Type'] and 'N' in inst['Type'][0]
2303                    if Phases[phase]['General'].get('Modulated',False):
2304                        ifSuper = True
2305                        HKLd = np.array(G2lat.GenSSHLaue(dmin,SGData,SSGData,Vec,maxH,A))
2306                        HKLd = G2mth.sortArray(HKLd,4,reverse=True)
2307                        for h,k,l,m,d in HKLd:
2308                            ext,mul,uniq,phi = G2spc.GenHKLf([h,k,l],SGData)
2309                            mul *= 2      # for powder overlap of Friedel pairs
2310                            if m or not ext or useExt:
2311                                if 'C' in inst['Type'][0]:
2312                                    pos = G2lat.Dsp2pos(inst,d)
2313                                    if limits[0] < pos < limits[1]:
2314                                        refList.append([h,k,l,m,mul,d, pos,0.0,0.0,0.0,0.0, 0.0,0.0,1.0,1.0,1.0])
2315                                        #... sig,gam,fotsq,fctsq, phase,icorr,prfo,abs,ext
2316                                        Uniq.append(uniq)
2317                                        Phi.append(phi)
2318                                elif 'T' in inst['Type'][0]:
2319                                    pos = G2lat.Dsp2pos(inst,d)
2320                                    if limits[0] < pos < limits[1]:
2321                                        wave = inst['difC'][1]*d/(252.816*inst['fltPath'][0])
2322                                        refList.append([h,k,l,m,mul,d, pos,0.0,0.0,0.0,0.0, 0.0,0.0,0.0,0.0,wave, 1.0,1.0,1.0])
2323                                        # ... sig,gam,fotsq,fctsq, phase,icorr,alp,bet,wave, prfo,abs,ext
2324                                        Uniq.append(uniq)
2325                                        Phi.append(phi)
2326                    else:
2327                        ifSuper = False
2328                        HKLd = np.array(G2lat.GenHLaue(dmin,SGData,A))
2329                        HKLd = G2mth.sortArray(HKLd,3,reverse=True)
2330                        for h,k,l,d in HKLd:
2331                            ext,mul,uniq,phi = G2spc.GenHKLf([h,k,l],SGData)
2332                            mul *= 2      # for powder overlap of Friedel pairs
2333                            if ext and not useExt:
2334                                continue
2335                            if 'C' in inst['Type'][0]:
2336                                pos = G2lat.Dsp2pos(inst,d)
2337                                if limits[0] < pos < limits[1]:
2338                                    refList.append([h,k,l,mul,d, pos,0.0,0.0,0.0,0.0, 0.0,0.0,1.0,1.0,1.0])
2339                                    #... sig,gam,fotsq,fctsq, phase,icorr,prfo,abs,ext
2340                                    Uniq.append(uniq)
2341                                    Phi.append(phi)
2342                            elif 'T' in inst['Type'][0]:
2343                                pos = G2lat.Dsp2pos(inst,d)
2344                                if limits[0] < pos < limits[1]:
2345                                    wave = inst['difC'][1]*d/(252.816*inst['fltPath'][0])
2346                                    refList.append([h,k,l,mul,d, pos,0.0,0.0,0.0,0.0, 0.0,0.0,0.0,0.0,wave, 1.0,1.0,1.0])
2347                                    # ... sig,gam,fotsq,fctsq, phase,icorr,alp,bet,wave, prfo,abs,ext
2348                                    Uniq.append(uniq)
2349                                    Phi.append(phi)
2350                    Histogram['Reflection Lists'][phase] = {'RefList':np.array(refList),'FF':{},'Type':inst['Type'][0],'Super':ifSuper}
2351            elif 'HKLF' in histogram:
2352                inst = Histogram['Instrument Parameters'][0]
2353                hId = Histogram['hId']
2354                hfx = ':%d:'%(hId)
2355                for item in inst:
2356                    if type(inst) is not list and item != 'Type': continue # skip over non-refined items (such as InstName)
2357                    hapDict[hfx+item] = inst[item][1]
2358                pfx = str(pId)+':'+str(hId)+':'
2359                hapDict[pfx+'Scale'] = hapData['Scale'][0]
2360                if hapData['Scale'][1]:
2361                    hapVary.append(pfx+'Scale')
2362                               
2363                extApprox,extType,extParms = hapData['Extinction']
2364                controlDict[pfx+'EType'] = extType
2365                controlDict[pfx+'EApprox'] = extApprox
2366                if 'C' in inst['Type'][0]:
2367                    controlDict[pfx+'Tbar'] = extParms['Tbar']
2368                    controlDict[pfx+'Cos2TM'] = extParms['Cos2TM']
2369                if 'Primary' in extType:
2370                    Ekey = ['Ep',]
2371                elif 'I & II' in extType:
2372                    Ekey = ['Eg','Es']
2373                elif 'Secondary Type II' == extType:
2374                    Ekey = ['Es',]
2375                elif 'Secondary Type I' == extType:
2376                    Ekey = ['Eg',]
2377                else:   #'None'
2378                    Ekey = []
2379                for eKey in Ekey:
2380                    hapDict[pfx+eKey] = extParms[eKey][0]
2381                    if extParms[eKey][1]:
2382                        hapVary.append(pfx+eKey)
2383                for bab in ['BabA','BabU']:
2384                    hapDict[pfx+bab] = hapData['Babinet'][bab][0]
2385                    if hapData['Babinet'][bab][1]:
2386                        hapVary.append(pfx+bab)
2387                Twins = hapData.get('Twins',[[np.array([[1,0,0],[0,1,0],[0,0,1]]),[1.0,False,0]],])
2388                if len(Twins) == 1:
2389                    hapDict[pfx+'Flack'] = hapData.get('Flack',[0.,False])[0]
2390                    if hapData.get('Flack',[0,False])[1]:
2391                        hapVary.append(pfx+'Flack')
2392                sumTwFr = 0.
2393                controlDict[pfx+'TwinLaw'] = []
2394                controlDict[pfx+'TwinInv'] = []
2395                NTL = 0           
2396                for it,twin in enumerate(Twins):
2397                    if 'bool' in str(type(twin[0])):
2398                        controlDict[pfx+'TwinInv'].append(twin[0])
2399                        controlDict[pfx+'TwinLaw'].append(np.zeros((3,3)))
2400                    else:
2401                        NTL += 1
2402                        controlDict[pfx+'TwinInv'].append(False)
2403                        controlDict[pfx+'TwinLaw'].append(twin[0])
2404                    if it:
2405                        hapDict[pfx+'TwinFr:'+str(it)] = twin[1]
2406                        sumTwFr += twin[1]
2407                    else:
2408                        hapDict[pfx+'TwinFr:0'] = twin[1][0]
2409                        controlDict[pfx+'TwinNMN'] = twin[1][1]
2410                    if Twins[0][1][1]:
2411                        hapVary.append(pfx+'TwinFr:'+str(it))
2412                controlDict[pfx+'NTL'] = NTL
2413                #need to make constraint on TwinFr
2414                controlDict[pfx+'TwinLaw'] = np.array(controlDict[pfx+'TwinLaw'])
2415                if len(Twins) > 1:    #force sum to unity
2416                    hapDict[pfx+'TwinFr:0'] = 1.-sumTwFr
2417                if Print: 
2418                    print >>pFile,'\n Phase: ',phase,' in histogram: ',histogram
2419                    print >>pFile,135*'='
2420                    print >>pFile,' Scale factor     : %10.4f'%(hapData['Scale'][0]),' Refine?',hapData['Scale'][1]
2421                    if extType != 'None':
2422                        print >>pFile,' Extinction  Type: %15s'%(extType),' approx: %10s'%(extApprox)
2423                        text = ' Parameters       :'
2424                        for eKey in Ekey:
2425                            text += ' %4s : %10.3e Refine? '%(eKey,extParms[eKey][0])+str(extParms[eKey][1])
2426                        print >>pFile,text
2427                    if hapData['Babinet']['BabA'][0]:
2428                        PrintBabinet(hapData['Babinet'])
2429                    if not SGData['SGInv'] and len(Twins) == 1:
2430                        print >>pFile,' Flack parameter: %10.3f'%(hapData['Flack'][0]),' Refine?',hapData['Flack'][1]
2431                    if len(Twins) > 1:
2432                        for it,twin in enumerate(Twins):
2433                            if 'bool' in str(type(twin[0])):
2434                                print >>pFile,' Nonmerohedral twin fr.: %5.3f Inv? %s Refine? '%(hapDict[pfx+'TwinFr:'+str(it)],str(controlDict[pfx+'TwinInv'][it])),Twins[0][1][1] 
2435                            else:
2436                                print >>pFile,' Twin law: %s'%(str(twin[0]).replace('\n',',')),' Twin fr.: %5.3f Refine? '%(hapDict[pfx+'TwinFr:'+str(it)]),Twins[0][1][1] 
2437                       
2438                Histogram['Reflection Lists'] = phase       
2439               
2440    return hapVary,hapDict,controlDict
2441   
2442def SetHistogramPhaseData(parmDict,sigDict,Phases,Histograms,FFtables,Print=True,pFile=None):
2443    'needs a doc string'
2444   
2445    def PrintSizeAndSig(hapData,sizeSig):
2446        line = '\n Size model:     %9s'%(hapData[0])
2447        refine = False
2448        if hapData[0] in ['isotropic','uniaxial']:
2449            line += ' equatorial:%12.5f'%(hapData[1][0])
2450            if sizeSig[0][0]:
2451                line += ', sig:%8.4f'%(sizeSig[0][0])
2452                refine = True
2453            if hapData[0] == 'uniaxial':
2454                line += ' axial:%12.4f'%(hapData[1][1])
2455                if sizeSig[0][1]:
2456                    refine = True
2457                    line += ', sig:%8.4f'%(sizeSig[0][1])
2458            line += ' LG mix coeff.:%12.4f'%(hapData[1][2])
2459            if sizeSig[0][2]:
2460                refine = True
2461                line += ', sig:%8.4f'%(sizeSig[0][2])
2462            if refine:
2463                print >>pFile,line
2464        else:
2465            line += ' LG mix coeff.:%12.4f'%(hapData[1][2])
2466            if sizeSig[0][2]:
2467                refine = True
2468                line += ', sig:%8.4f'%(sizeSig[0][2])
2469            Snames = ['S11','S22','S33','S12','S13','S23']
2470            ptlbls = ' name  :'
2471            ptstr =  ' value :'
2472            sigstr = ' sig   :'
2473            for i,name in enumerate(Snames):
2474                ptlbls += '%12s' % (name)
2475                ptstr += '%12.6f' % (hapData[4][i])
2476                if sizeSig[1][i]:
2477                    refine = True
2478                    sigstr += '%12.6f' % (sizeSig[1][i])
2479                else:
2480                    sigstr += 12*' '
2481            if refine:
2482                print >>pFile,line
2483                print >>pFile,ptlbls
2484                print >>pFile,ptstr
2485                print >>pFile,sigstr
2486       
2487    def PrintMuStrainAndSig(hapData,mustrainSig,SGData):
2488        line = '\n Mustrain model: %9s'%(hapData[0])
2489        refine = False
2490        if hapData[0] in ['isotropic','uniaxial']:
2491            line += ' equatorial:%12.1f'%(hapData[1][0])
2492            if mustrainSig[0][0]:
2493                line += ', sig:%8.1f'%(mustrainSig[0][0])
2494                refine = True
2495            if hapData[0] == 'uniaxial':
2496                line += ' axial:%12.1f'%(hapData[1][1])
2497                if mustrainSig[0][1]:
2498                     line += ', sig:%8.1f'%(mustrainSig[0][1])
2499            line += ' LG mix coeff.:%12.4f'%(hapData[1][2])
2500            if mustrainSig[0][2]:
2501                refine = True
2502                line += ', sig:%8.3f'%(mustrainSig[0][2])
2503            if refine:
2504                print >>pFile,line
2505        else:
2506            line += ' LG mix coeff.:%12.4f'%(hapData[1][2])
2507            if mustrainSig[0][2]:
2508                refine = True
2509                line += ', sig:%8.3f'%(mustrainSig[0][2])
2510            Snames = G2spc.MustrainNames(SGData)
2511            ptlbls = ' name  :'
2512            ptstr =  ' value :'
2513            sigstr = ' sig   :'
2514            for i,name in enumerate(Snames):
2515                ptlbls += '%12s' % (name)
2516                ptstr += '%12.1f' % (hapData[4][i])
2517                if mustrainSig[1][i]:
2518                    refine = True
2519                    sigstr += '%12.1f' % (mustrainSig[1][i])
2520                else:
2521                    sigstr += 12*' '
2522            if refine:
2523                print >>pFile,line
2524                print >>pFile,ptlbls
2525                print >>pFile,ptstr
2526                print >>pFile,sigstr
2527           
2528    def PrintHStrainAndSig(hapData,strainSig,SGData):
2529        Hsnames = G2spc.HStrainNames(SGData)
2530        ptlbls = ' name  :'
2531        ptstr =  ' value :'
2532        sigstr = ' sig   :'
2533        refine = False
2534        for i,name in enumerate(Hsnames):
2535            ptlbls += '%12s' % (name)
2536            ptstr += '%12.4g' % (hapData[0][i])
2537            if name in strainSig:
2538                refine = True
2539                sigstr += '%12.4g' % (strainSig[name])
2540            else:
2541                sigstr += 12*' '
2542        if refine:
2543            print >>pFile,'\n Hydrostatic/elastic strain: '
2544            print >>pFile,ptlbls
2545            print >>pFile,ptstr
2546            print >>pFile,sigstr
2547       
2548    def PrintSHPOAndSig(pfx,hapData,POsig):
2549        print >>pFile,'\n Spherical harmonics preferred orientation: Order:'+str(hapData[4])
2550        ptlbls = ' names :'
2551        ptstr =  ' values:'
2552        sigstr = ' sig   :'
2553        for item in hapData[5]:
2554            ptlbls += '%12s'%(item)
2555            ptstr += '%12.3f'%(hapData[5][item])
2556            if pfx+item in POsig:
2557                sigstr += '%12.3f'%(POsig[pfx+item])
2558            else:
2559                sigstr += 12*' ' 
2560        print >>pFile,ptlbls
2561        print >>pFile,ptstr
2562        print >>pFile,sigstr
2563       
2564    def PrintExtAndSig(pfx,hapData,ScalExtSig):
2565        print >>pFile,'\n Single crystal extinction: Type: ',hapData[0],' Approx: ',hapData[1]
2566        text = ''
2567        for item in hapData[2]:
2568            if pfx+item in ScalExtSig:
2569                text += '       %s: '%(item)
2570                text += '%12.2e'%(hapData[2][item][0])
2571                if pfx+item in ScalExtSig:
2572                    text += ' sig: %12.2e'%(ScalExtSig[pfx+item])
2573        print >>pFile,text       
2574       
2575    def PrintBabinetAndSig(pfx,hapData,BabSig):
2576        print >>pFile,'\n Babinet form factor modification: '
2577        ptlbls = ' names :'
2578        ptstr =  ' values:'
2579        sigstr = ' sig   :'
2580        for item in hapData:
2581            ptlbls += '%12s'%(item)
2582            ptstr += '%12.3f'%(hapData[item][0])
2583            if pfx+item in BabSig:
2584                sigstr += '%12.3f'%(BabSig[pfx+item])
2585            else:
2586                sigstr += 12*' ' 
2587        print >>pFile,ptlbls
2588        print >>pFile,ptstr
2589        print >>pFile,sigstr
2590       
2591    def PrintTwinsAndSig(pfx,twinData,TwinSig):
2592        print >>pFile,'\n Twin Law fractions : '
2593        ptlbls = ' names :'
2594        ptstr =  ' values:'
2595        sigstr = ' sig   :'
2596        for it,item in enumerate(twinData):
2597            ptlbls += '     twin #%d'%(it)
2598            if it:
2599                ptstr += '%12.3f'%(item[1])
2600            else:
2601                ptstr += '%12.3f'%(item[1][0])
2602            if pfx+'TwinFr:'+str(it) in TwinSig:
2603                sigstr += '%12.3f'%(TwinSig[pfx+'TwinFr:'+str(it)])
2604            else:
2605                sigstr += 12*' ' 
2606        print >>pFile,ptlbls
2607        print >>pFile,ptstr
2608        print >>pFile,sigstr
2609       
2610   
2611    PhFrExtPOSig = {}
2612    SizeMuStrSig = {}
2613    ScalExtSig = {}
2614    BabSig = {}
2615    TwinFrSig = {}
2616    wtFrSum = {}
2617    for phase in Phases:
2618        HistoPhase = Phases[phase]['Histograms']
2619        General = Phases[phase]['General']
2620        SGData = General['SGData']
2621        pId = Phases[phase]['pId']
2622        histoList = HistoPhase.keys()
2623        histoList.sort()
2624        for histogram in histoList:
2625            try:
2626                Histogram = Histograms[histogram]
2627            except KeyError:                       
2628                #skip if histogram not included e.g. in a sequential refinement
2629                continue
2630            hapData = HistoPhase[histogram]
2631            hId = Histogram['hId']
2632            pfx = str(pId)+':'+str(hId)+':'
2633            if hId not in wtFrSum:
2634                wtFrSum[hId] = 0.
2635            if 'PWDR' in histogram:
2636                for item in ['Scale','Extinction']:
2637                    hapData[item][0] = parmDict[pfx+item]
2638                    if pfx+item in sigDict:
2639                        PhFrExtPOSig.update({pfx+item:sigDict[pfx+item],})
2640                wtFrSum[hId] += hapData['Scale'][0]*General['Mass']
2641                if hapData['Pref.Ori.'][0] == 'MD':
2642                    hapData['Pref.Ori.'][1] = parmDict[pfx+'MD']
2643                    if pfx+'MD' in sigDict:
2644                        PhFrExtPOSig.update({pfx+'MD':sigDict[pfx+'MD'],})
2645                else:                           #'SH' spherical harmonics
2646                    for item in hapData['Pref.Ori.'][5]:
2647                        hapData['Pref.Ori.'][5][item] = parmDict[pfx+item]
2648                        if pfx+item in sigDict:
2649                            PhFrExtPOSig.update({pfx+item:sigDict[pfx+item],})
2650                SizeMuStrSig.update({pfx+'Mustrain':[[0,0,0],[0 for i in range(len(hapData['Mustrain'][4]))]],
2651                    pfx+'Size':[[0,0,0],[0 for i in range(len(hapData['Size'][4]))]],
2652                    pfx+'HStrain':{}})                 
2653                for item in ['Mustrain','Size']:
2654                    hapData[item][1][2] = parmDict[pfx+item+';mx']
2655#                    hapData[item][1][2] = min(1.,max(0.,hapData[item][1][2]))
2656                    if pfx+item+';mx' in sigDict:
2657                        SizeMuStrSig[pfx+item][0][2] = sigDict[pfx+item+';mx']
2658                    if hapData[item][0] in ['isotropic','uniaxial']:                   
2659                        hapData[item][1][0] = parmDict[pfx+item+';i']
2660                        if item == 'Size':
2661                            hapData[item][1][0] = min(10.,max(0.001,hapData[item][1][0]))
2662                        if pfx+item+';i' in sigDict: 
2663                            SizeMuStrSig[pfx+item][0][0] = sigDict[pfx+item+';i']
2664                        if hapData[item][0] == 'uniaxial':
2665                            hapData[item][1][1] = parmDict[pfx+item+';a']
2666                            if item == 'Size':
2667                                hapData[item][1][1] = min(10.,max(0.001,hapData[item][1][1]))                       
2668                            if pfx+item+';a' in sigDict:
2669                                SizeMuStrSig[pfx+item][0][1] = sigDict[pfx+item+';a']
2670                    else:       #generalized for mustrain or ellipsoidal for size
2671                        Nterms = len(hapData[item][4])
2672                        for i in range(Nterms):
2673                            sfx = ';'+str(i)
2674                            hapData[item][4][i] = parmDict[pfx+item+sfx]
2675                            if pfx+item+sfx in sigDict:
2676                                SizeMuStrSig[pfx+item][1][i] = sigDict[pfx+item+sfx]
2677                names = G2spc.HStrainNames(SGData)
2678                for i,name in enumerate(names):
2679                    hapData['HStrain'][0][i] = parmDict[pfx+name]
2680                    if pfx+name in sigDict:
2681                        SizeMuStrSig[pfx+'HStrain'][name] = sigDict[pfx+name]
2682                if Phases[phase]['General']['Type'] != 'magnetic':
2683                    for name in ['BabA','BabU']:
2684                        hapData['Babinet'][name][0] = parmDict[pfx+name]
2685                        if pfx+name in sigDict:
2686                            BabSig[pfx+name] = sigDict[pfx+name]               
2687               
2688            elif 'HKLF' in histogram:
2689                for item in ['Scale','Flack']:
2690                    if parmDict.get(pfx+item):
2691                        hapData[item][0] = parmDict[pfx+item]
2692                        if pfx+item in sigDict:
2693                            ScalExtSig[pfx+item] = sigDict[pfx+item]
2694                for item in ['Ep','Eg','Es']:
2695                    if parmDict.get(pfx+item):
2696                        hapData['Extinction'][2][item][0] = parmDict[pfx+item]
2697                        if pfx+item in sigDict:
2698                            ScalExtSig[pfx+item] = sigDict[pfx+item]
2699                for item in ['BabA','BabU']:
2700                    hapData['Babinet'][item][0] = parmDict[pfx+item]
2701                    if pfx+item in sigDict:
2702                        BabSig[pfx+item] = sigDict[pfx+item]
2703                if 'Twins' in hapData:
2704                    it = 1
2705                    sumTwFr = 0.
2706                    while True:
2707                        try:
2708                            hapData['Twins'][it][1] = parmDict[pfx+'TwinFr:'+str(it)]
2709                            if pfx+'TwinFr:'+str(it) in sigDict:
2710                                TwinFrSig[pfx+'TwinFr:'+str(it)] = sigDict[pfx+'TwinFr:'+str(it)]
2711                            if it:
2712                                sumTwFr += hapData['Twins'][it][1]
2713                            it += 1
2714                        except KeyError:
2715                            break
2716                    hapData['Twins'][0][1][0] = 1.-sumTwFr
2717
2718    if Print:
2719        for phase in Phases:
2720            HistoPhase = Phases[phase]['Histograms']
2721            General = Phases[phase]['General']
2722            SGData = General['SGData']
2723            pId = Phases[phase]['pId']
2724            histoList = HistoPhase.keys()
2725            histoList.sort()
2726            for histogram in histoList:
2727                try:
2728                    Histogram = Histograms[histogram]
2729                except KeyError:                       
2730                    #skip if histogram not included e.g. in a sequential refinement
2731                    continue
2732                print >>pFile,'\n Phase: ',phase,' in histogram: ',histogram
2733                print >>pFile,135*'='
2734                hapData = HistoPhase[histogram]
2735                hId = Histogram['hId']
2736                Histogram['Residuals'][str(pId)+'::Name'] = phase
2737                pfx = str(pId)+':'+str(hId)+':'
2738                hfx = ':%s:'%(hId)
2739                if 'PWDR' in histogram:
2740                    print >>pFile,' Final refinement RF, RF^2 = %.2f%%, %.2f%% on %d reflections'   \
2741                        %(Histogram['Residuals'][pfx+'Rf'],Histogram['Residuals'][pfx+'Rf^2'],Histogram['Residuals'][pfx+'Nref'])
2742                    print >>pFile,' Bragg intensity sum = %.3g'%(Histogram['Residuals'][pfx+'sumInt'])
2743               
2744                    if pfx+'Scale' in PhFrExtPOSig:
2745                        wtFr = hapData['Scale'][0]*General['Mass']/wtFrSum[hId]
2746                        sigwtFr = PhFrExtPOSig[pfx+'Scale']*wtFr/hapData['Scale'][0]
2747                        print >>pFile,' Phase fraction  : %10.5f, sig %10.5f Weight fraction  : %8.5f, sig %10.5f' \
2748                            %(hapData['Scale'][0],PhFrExtPOSig[pfx+'Scale'],wtFr,sigwtFr)
2749                    if pfx+'Extinction' in PhFrExtPOSig:
2750                        print >>pFile,' Extinction coeff: %10.4f, sig %10.4f'%(hapData['Extinction'][0],PhFrExtPOSig[pfx+'Extinction'])
2751                    if hapData['Pref.Ori.'][0] == 'MD':
2752                        if pfx+'MD' in PhFrExtPOSig:
2753                            print >>pFile,' March-Dollase PO: %10.4f, sig %10.4f'%(hapData['Pref.Ori.'][1],PhFrExtPOSig[pfx+'MD'])
2754                    else:
2755                        PrintSHPOAndSig(pfx,hapData['Pref.Ori.'],PhFrExtPOSig)
2756                    PrintSizeAndSig(hapData['Size'],SizeMuStrSig[pfx+'Size'])
2757                    PrintMuStrainAndSig(hapData['Mustrain'],SizeMuStrSig[pfx+'Mustrain'],SGData)
2758                    PrintHStrainAndSig(hapData['HStrain'],SizeMuStrSig[pfx+'HStrain'],SGData)
2759                    if Phases[phase]['General']['Type'] != 'magnetic':
2760                        if len(BabSig):
2761                            PrintBabinetAndSig(pfx,hapData['Babinet'],BabSig)
2762                   
2763                elif 'HKLF' in histogram:
2764                    Inst = Histogram['Instrument Parameters'][0]
2765                    print >>pFile,' Final refinement RF, RF^2 = %.2f%%, %.2f%% on %d reflections (%d user rejected, %d sp.gp.extinct)'   \
2766                        %(Histogram['Residuals'][pfx+'Rf'],Histogram['Residuals'][pfx+'Rf^2'],Histogram['Residuals'][pfx+'Nref'],
2767                        Histogram['Residuals'][pfx+'Nrej'],Histogram['Residuals'][pfx+'Next'])
2768                    if FFtables != None and 'N' not in Inst['Type'][0]:
2769                        PrintFprime(FFtables,hfx,pFile)
2770                    print >>pFile,' HKLF histogram weight factor = ','%.3f'%(Histogram['wtFactor'])
2771                    if pfx+'Scale' in ScalExtSig:
2772                        print >>pFile,' Scale factor : %10.4f, sig %10.4f'%(hapData['Scale'][0],ScalExtSig[pfx+'Scale'])
2773                    if hapData['Extinction'][0] != 'None':
2774                        PrintExtAndSig(pfx,hapData['Extinction'],ScalExtSig)
2775                    if len(BabSig):
2776                        PrintBabinetAndSig(pfx,hapData['Babinet'],BabSig)
2777                    if pfx+'Flack' in ScalExtSig:
2778                        print >>pFile,' Flack parameter : %10.3f, sig %10.3f'%(hapData['Flack'][0],ScalExtSig[pfx+'Flack'])
2779                    if len(TwinFrSig):
2780                        PrintTwinsAndSig(pfx,hapData['Twins'],TwinFrSig)
2781
2782################################################################################
2783##### Histogram data
2784################################################################################       
2785                   
2786def GetHistogramData(Histograms,Print=True,pFile=None):
2787    'needs a doc string'
2788   
2789    def GetBackgroundParms(hId,Background):
2790        Back = Background[0]
2791        DebyePeaks = Background[1]
2792        bakType,bakFlag = Back[:2]
2793        backVals = Back[3:]
2794        backNames = [':'+str(hId)+':Back;'+str(i) for i in range(len(backVals))]
2795        backDict = dict(zip(backNames,backVals))
2796        backVary = []
2797        if bakFlag:
2798            backVary = backNames
2799        backDict[':'+str(hId)+':nDebye'] = DebyePeaks['nDebye']
2800        backDict[':'+str(hId)+':nPeaks'] = DebyePeaks['nPeaks']
2801        debyeDict = {}
2802        debyeList = []
2803        for i in range(DebyePeaks['nDebye']):
2804            debyeNames = [':'+str(hId)+':DebyeA;'+str(i),':'+str(hId)+':DebyeR;'+str(i),':'+str(hId)+':DebyeU;'+str(i)]
2805            debyeDict.update(dict(zip(debyeNames,DebyePeaks['debyeTerms'][i][::2])))
2806            debyeList += zip(debyeNames,DebyePeaks['debyeTerms'][i][1::2])
2807        debyeVary = []
2808        for item in debyeList:
2809            if item[1]:
2810                debyeVary.append(item[0])
2811        backDict.update(debyeDict)
2812        backVary += debyeVary
2813        peakDict = {}
2814        peakList = []
2815        for i in range(DebyePeaks['nPeaks']):
2816            peakNames = [':'+str(hId)+':BkPkpos;'+str(i),':'+str(hId)+ \
2817                ':BkPkint;'+str(i),':'+str(hId)+':BkPksig;'+str(i),':'+str(hId)+':BkPkgam;'+str(i)]
2818            peakDict.update(dict(zip(peakNames,DebyePeaks['peaksList'][i][::2])))
2819            peakList += zip(peakNames,DebyePeaks['peaksList'][i][1::2])
2820        peakVary = []
2821        for item in peakList:
2822            if item[1]:
2823                peakVary.append(item[0])
2824        backDict.update(peakDict)
2825        backVary += peakVary
2826        return bakType,backDict,backVary           
2827       
2828    def GetInstParms(hId,Inst):     
2829        dataType = Inst['Type'][0]
2830        instDict = {}
2831        insVary = []
2832        pfx = ':'+str(hId)+':'
2833        insKeys = Inst.keys()
2834        insKeys.sort()
2835        for item in insKeys:
2836            insName = pfx+item
2837            instDict[insName] = Inst[item][1]
2838            if len(Inst[item]) > 2 and Inst[item][2]:
2839                insVary.append(insName)
2840        if 'C' in dataType:
2841            instDict[pfx+'SH/L'] = max(instDict[pfx+'SH/L'],0.0005)
2842        return dataType,instDict,insVary
2843       
2844    def GetSampleParms(hId,Sample):
2845        sampVary = []
2846        hfx = ':'+str(hId)+':'       
2847        sampDict = {hfx+'Gonio. radius':Sample['Gonio. radius'],hfx+'Omega':Sample['Omega'],
2848            hfx+'Chi':Sample['Chi'],hfx+'Phi':Sample['Phi']}
2849        for key in ('Temperature','Pressure','FreePrm1','FreePrm2','FreePrm3'):
2850            if key in Sample:
2851                sampDict[hfx+key] = Sample[key]
2852        Type = Sample['Type']
2853        if 'Bragg' in Type:             #Bragg-Brentano
2854            for item in ['Scale','Shift','Transparency','SurfRoughA','SurfRoughB']:
2855                sampDict[hfx+item] = Sample[item][0]
2856                if Sample[item][1]:
2857                    sampVary.append(hfx+item)
2858        elif 'Debye' in Type:        #Debye-Scherrer
2859            for item in ['Scale','Absorption','DisplaceX','DisplaceY']:
2860                sampDict[hfx+item] = Sample[item][0]
2861                if Sample[item][1]:
2862                    sampVary.append(hfx+item)
2863        return Type,sampDict,sampVary
2864       
2865    def PrintBackground(Background):
2866        Back = Background[0]
2867        DebyePeaks = Background[1]
2868        print >>pFile,'\n Background function: ',Back[0],' Refine?',bool(Back[1])
2869        line = ' Coefficients: '
2870        for i,back in enumerate(Back[3:]):
2871            line += '%10.3f'%(back)
2872            if i and not i%10:
2873                line += '\n'+15*' '
2874        print >>pFile,line
2875        if DebyePeaks['nDebye']:
2876            print >>pFile,'\n Debye diffuse scattering coefficients'
2877            parms = ['DebyeA','DebyeR','DebyeU']
2878            line = ' names :  '
2879            for parm in parms:
2880                line += '%8s refine?'%(parm)
2881            print >>pFile,line
2882            for j,term in enumerate(DebyePeaks['debyeTerms']):
2883                line = ' term'+'%2d'%(j)+':'
2884                for i in range(3):
2885                    line += '%10.3f %5s'%(term[2*i],bool(term[2*i+1]))                   
2886                print >>pFile,line
2887        if DebyePeaks['nPeaks']:
2888            print >>pFile,'\n Single peak coefficients'
2889            parms =    ['BkPkpos','BkPkint','BkPksig','BkPkgam']
2890            line = ' names :  '
2891            for parm in parms:
2892                line += '%8s refine?'%(parm)
2893            print >>pFile,line
2894            for j,term in enumerate(DebyePeaks['peaksList']):
2895                line = ' peak'+'%2d'%(j)+':'
2896                for i in range(4):
2897                    line += '%12.3f %5s'%(term[2*i],bool(term[2*i+1]))                   
2898                print >>pFile,line
2899       
2900    def PrintInstParms(Inst):
2901        print >>pFile,'\n Instrument Parameters:'
2902        insKeys = Inst.keys()
2903        insKeys.sort()
2904        iBeg = 0
2905        Ok = True
2906        while Ok:
2907            ptlbls = ' name  :'
2908            ptstr =  ' value :'
2909            varstr = ' refine:'
2910            iFin = min(iBeg+9,len(insKeys))
2911            for item in insKeys[iBeg:iFin]:
2912                if item not in ['Type','Source']:
2913                    ptlbls += '%12s' % (item)
2914                    ptstr += '%12.6f' % (Inst[item][1])
2915                    if item in ['Lam1','Lam2','Azimuth','fltPath','2-theta',]:
2916                        varstr += 12*' '
2917                    else:
2918                        varstr += '%12s' % (str(bool(Inst[item][2])))
2919            print >>pFile,ptlbls
2920            print >>pFile,ptstr
2921            print >>pFile,varstr
2922            iBeg = iFin
2923            if iBeg == len(insKeys):
2924                Ok = False
2925            else:
2926                print >>pFile,'\n'
2927       
2928    def PrintSampleParms(Sample):
2929        print >>pFile,'\n Sample Parameters:'
2930        print >>pFile,' Goniometer omega = %.2f, chi = %.2f, phi = %.2f'% \
2931            (Sample['Omega'],Sample['Chi'],Sample['Phi'])
2932        ptlbls = ' name  :'
2933        ptstr =  ' value :'
2934        varstr = ' refine:'
2935        if 'Bragg' in Sample['Type']:
2936            for item in ['Scale','Shift','Transparency','SurfRoughA','SurfRoughB']:
2937                ptlbls += '%14s'%(item)
2938                ptstr += '%14.4f'%(Sample[item][0])
2939                varstr += '%14s'%(str(bool(Sample[item][1])))
2940           
2941        elif 'Debye' in Type:        #Debye-Scherrer
2942            for item in ['Scale','Absorption','DisplaceX','DisplaceY']:
2943                ptlbls += '%14s'%(item)
2944                ptstr += '%14.4f'%(Sample[item][0])
2945                varstr += '%14s'%(str(bool(Sample[item][1])))
2946
2947        print >>pFile,ptlbls
2948        print >>pFile,ptstr
2949        print >>pFile,varstr
2950       
2951    histDict = {}
2952    histVary = []
2953    controlDict = {}
2954    histoList = Histograms.keys()
2955    histoList.sort()
2956    for histogram in histoList:
2957        if 'PWDR' in histogram:
2958            Histogram = Histograms[histogram]
2959            hId = Histogram['hId']
2960            pfx = ':'+str(hId)+':'
2961            controlDict[pfx+'wtFactor'] = Histogram['wtFactor']
2962            controlDict[pfx+'Limits'] = Histogram['Limits'][1]
2963            controlDict[pfx+'Exclude'] = Histogram['Limits'][2:]
2964            for excl in controlDict[pfx+'Exclude']:
2965                Histogram['Data'][0] = ma.masked_inside(Histogram['Data'][0],excl[0],excl[1])
2966            if controlDict[pfx+'Exclude']:
2967                ma.mask_rows(Histogram['Data'])
2968            Background = Histogram['Background']
2969            Type,bakDict,bakVary = GetBackgroundParms(hId,Background)
2970            controlDict[pfx+'bakType'] = Type
2971            histDict.update(bakDict)
2972            histVary += bakVary
2973           
2974            Inst = Histogram['Instrument Parameters'][0]
2975            Type,instDict,insVary = GetInstParms(hId,Inst)
2976            controlDict[pfx+'histType'] = Type
2977            if 'XC' in Type:
2978                if pfx+'Lam1' in instDict:
2979                    controlDict[pfx+'keV'] = 12.397639/instDict[pfx+'Lam1']
2980                else:
2981                    controlDict[pfx+'keV'] = 12.397639/instDict[pfx+'Lam']           
2982            histDict.update(instDict)
2983            histVary += insVary
2984           
2985            Sample = Histogram['Sample Parameters']
2986            Type,sampDict,sampVary = GetSampleParms(hId,Sample)
2987            controlDict[pfx+'instType'] = Type
2988            histDict.update(sampDict)
2989            histVary += sampVary
2990           
2991   
2992            if Print: 
2993                print >>pFile,'\n Histogram: ',histogram,' histogram Id: ',hId
2994                print >>pFile,135*'='
2995                Units = {'C':' deg','T':' msec'}
2996                units = Units[controlDict[pfx+'histType'][2]]
2997                Limits = controlDict[pfx+'Limits']
2998                print >>pFile,' Instrument type: ',Sample['Type']
2999                print >>pFile,' Histogram limits: %8.2f%s to %8.2f%s'%(Limits[0],units,Limits[1],units)
3000                if len(controlDict[pfx+'Exclude']):
3001                    excls = controlDict[pfx+'Exclude']
3002                    for excl in excls:
3003                        print >>pFile,' Excluded region:  %8.2f%s to %8.2f%s'%(excl[0],units,excl[1],units)   
3004                PrintSampleParms(Sample)
3005                PrintInstParms(Inst)
3006                PrintBackground(Background)
3007        elif 'HKLF' in histogram:
3008            Histogram = Histograms[histogram]
3009            hId = Histogram['hId']
3010            pfx = ':'+str(hId)+':'
3011            controlDict[pfx+'wtFactor'] = Histogram['wtFactor']
3012            Inst = Histogram['Instrument Parameters'][0]
3013            controlDict[pfx+'histType'] = Inst['Type'][0]
3014            if 'X' in Inst['Type'][0]:
3015                histDict[pfx+'Lam'] = Inst['Lam'][1]
3016                controlDict[pfx+'keV'] = 12.397639/histDict[pfx+'Lam']                   
3017    return histVary,histDict,controlDict
3018   
3019def SetHistogramData(parmDict,sigDict,Histograms,FFtables,Print=True,pFile=None):
3020    'needs a doc string'
3021   
3022    def SetBackgroundParms(pfx,Background,parmDict,sigDict):
3023        Back = Background[0]
3024        DebyePeaks = Background[1]
3025        lenBack = len(Back[3:])
3026        backSig = [0 for i in range(lenBack+3*DebyePeaks['nDebye']+4*DebyePeaks['nPeaks'])]
3027        for i in range(lenBack):
3028            Back[3+i] = parmDict[pfx+'Back;'+str(i)]
3029            if pfx+'Back;'+str(i) in sigDict:
3030                backSig[i] = sigDict[pfx+'Back;'+str(i)]
3031        if DebyePeaks['nDebye']:
3032            for i in range(DebyePeaks['nDebye']):
3033                names = [pfx+'DebyeA;'+str(i),pfx+'DebyeR;'+str(i),pfx+'DebyeU;'+str(i)]
3034                for j,name in enumerate(names):
3035                    DebyePeaks['debyeTerms'][i][2*j] = parmDict[name]
3036                    if name in sigDict:
3037                        backSig[lenBack+3*i+j] = sigDict[name]           
3038        if DebyePeaks['nPeaks']:
3039            for i in range(DebyePeaks['nPeaks']):
3040                names = [pfx+'BkPkpos;'+str(i),pfx+'BkPkint;'+str(i),
3041                    pfx+'BkPksig;'+str(i),pfx+'BkPkgam;'+str(i)]
3042                for j,name in enumerate(names):
3043                    DebyePeaks['peaksList'][i][2*j] = parmDict[name]
3044                    if name in sigDict:
3045                        backSig[lenBack+3*DebyePeaks['nDebye']+4*i+j] = sigDict[name]
3046        return backSig
3047       
3048    def SetInstParms(pfx,Inst,parmDict,sigDict):
3049        instSig = {}
3050        insKeys = Inst.keys()
3051        insKeys.sort()
3052        for item in insKeys:
3053            insName = pfx+item
3054            Inst[item][1] = parmDict[insName]
3055            if insName in sigDict:
3056                instSig[item] = sigDict[insName]
3057            else:
3058                instSig[item] = 0
3059        return instSig
3060       
3061    def SetSampleParms(pfx,Sample,parmDict,sigDict):
3062        if 'Bragg' in Sample['Type']:             #Bragg-Brentano
3063            sampSig = [0 for i in range(5)]
3064            for i,item in enumerate(['Scale','Shift','Transparency','SurfRoughA','SurfRoughB']):
3065                Sample[item][0] = parmDict[pfx+item]
3066                if pfx+item in sigDict:
3067                    sampSig[i] = sigDict[pfx+item]
3068        elif 'Debye' in Sample['Type']:        #Debye-Scherrer
3069            sampSig = [0 for i in range(4)]
3070            for i,item in enumerate(['Scale','Absorption','DisplaceX','DisplaceY']):
3071                Sample[item][0] = parmDict[pfx+item]
3072                if pfx+item in sigDict:
3073                    sampSig[i] = sigDict[pfx+item]
3074        return sampSig
3075       
3076    def PrintBackgroundSig(Background,backSig):
3077        Back = Background[0]
3078        DebyePeaks = Background[1]
3079        valstr = ' value : '
3080        sigstr = ' sig   : '
3081        refine = False
3082        for i,back in enumerate(Back[3:]):
3083            valstr += '%10.4g'%(back)
3084            if Back[1]:
3085                refine = True
3086                sigstr += '%10.4g'%(backSig[i])
3087            else:
3088                sigstr += 10*' '
3089        if refine:
3090            print >>pFile,'\n Background function: ',Back[0]
3091            print >>pFile,valstr
3092            print >>pFile,sigstr
3093        if DebyePeaks['nDebye']:
3094            ifAny = False
3095            ptfmt = "%12.3f"
3096            names =  ' names :'
3097            ptstr =  ' values:'
3098            sigstr = ' esds  :'
3099            for item in sigDict:
3100                if 'Debye' in item:
3101                    ifAny = True
3102                    names += '%12s'%(item)
3103                    ptstr += ptfmt%(parmDict[item])
3104                    sigstr += ptfmt%(sigDict[item])
3105            if ifAny:
3106                print >>pFile,'\n Debye diffuse scattering coefficients'
3107                print >>pFile,names
3108                print >>pFile,ptstr
3109                print >>pFile,sigstr
3110        if DebyePeaks['nPeaks']:
3111            print >>pFile,'\n Single peak coefficients:'
3112            parms =    ['BkPkpos','BkPkint','BkPksig','BkPkgam']
3113            line = ' peak no. '
3114            for parm in parms:
3115                line += '%14s%12s'%(parm.center(14),'esd'.center(12))
3116            print >>pFile,line
3117            for ip in range(DebyePeaks['nPeaks']):
3118                ptstr = ' %4d '%(ip)
3119                for parm in parms:
3120                    fmt = '%14.3f'
3121                    efmt = '%12.3f'
3122                    if parm == 'BkPkpos':
3123                        fmt = '%14.4f'
3124                        efmt = '%12.4f'
3125                    name = pfx+parm+';%d'%(ip)
3126                    ptstr += fmt%(parmDict[name])
3127                    if name in sigDict:
3128                        ptstr += efmt%(sigDict[name])
3129                    else:
3130                        ptstr += 12*' '
3131                print >>pFile,ptstr
3132        sumBk = np.array(Histogram['sumBk'])
3133        print >>pFile,' Background sums: empirical %.3g, Debye %.3g, peaks %.3g, Total %.3g'    \
3134            %(sumBk[0],sumBk[1],sumBk[2],np.sum(sumBk))
3135       
3136    def PrintInstParmsSig(Inst,instSig):
3137        refine = False
3138        insKeys = instSig.keys()
3139        insKeys.sort()
3140        iBeg = 0
3141        Ok = True
3142        while Ok:
3143            ptlbls = ' names :'
3144            ptstr =  ' value :'
3145            sigstr = ' sig   :'
3146            iFin = min(iBeg+9,len(insKeys))
3147            for name in insKeys[iBeg:iFin]:
3148                if name not in  ['Type','Lam1','Lam2','Azimuth','Source','fltPath']:
3149                    ptlbls += '%12s' % (name)
3150                    ptstr += '%12.6f' % (Inst[name][1])
3151                    if instSig[name]:
3152                        refine = True
3153                        sigstr += '%12.6f' % (instSig[name])
3154                    else:
3155                        sigstr += 12*' '
3156            if refine:
3157                print >>pFile,'\n Instrument Parameters:'
3158                print >>pFile,ptlbls
3159                print >>pFile,ptstr
3160                print >>pFile,sigstr
3161            iBeg = iFin
3162            if iBeg == len(insKeys):
3163                Ok = False
3164       
3165    def PrintSampleParmsSig(Sample,sampleSig):
3166        ptlbls = ' names :'
3167        ptstr =  ' values:'
3168        sigstr = ' sig   :'
3169        refine = False
3170        if 'Bragg' in Sample['Type']:
3171            for i,item in enumerate(['Scale','Shift','Transparency','SurfRoughA','SurfRoughB']):
3172                ptlbls += '%14s'%(item)
3173                ptstr += '%14.4f'%(Sample[item][0])
3174                if sampleSig[i]:
3175                    refine = True
3176                    sigstr += '%14.4f'%(sampleSig[i])
3177                else:
3178                    sigstr += 14*' '
3179           
3180        elif 'Debye' in Sample['Type']:        #Debye-Scherrer
3181            for i,item in enumerate(['Scale','Absorption','DisplaceX','DisplaceY']):
3182                ptlbls += '%14s'%(item)
3183                ptstr += '%14.4f'%(Sample[item][0])
3184                if sampleSig[i]:
3185                    refine = True
3186                    sigstr += '%14.4f'%(sampleSig[i])
3187                else:
3188                    sigstr += 14*' '
3189
3190        if refine:
3191            print >>pFile,'\n Sample Parameters:'
3192            print >>pFile,ptlbls
3193            print >>pFile,ptstr
3194            print >>pFile,sigstr
3195       
3196    histoList = Histograms.keys()
3197    histoList.sort()
3198    for histogram in histoList:
3199        if 'PWDR' in histogram:
3200            Histogram = Histograms[histogram]
3201            hId = Histogram['hId']
3202            pfx = ':'+str(hId)+':'
3203            Background = Histogram['Background']
3204            backSig = SetBackgroundParms(pfx,Background,parmDict,sigDict)
3205           
3206            Inst = Histogram['Instrument Parameters'][0]
3207            instSig = SetInstParms(pfx,Inst,parmDict,sigDict)
3208       
3209            Sample = Histogram['Sample Parameters']
3210            sampSig = SetSampleParms(pfx,Sample,parmDict,sigDict)
3211
3212            print >>pFile,'\n Histogram: ',histogram,' histogram Id: ',hId
3213            print >>pFile,135*'='
3214            print >>pFile,' PWDR histogram weight factor = '+'%.3f'%(Histogram['wtFactor'])
3215            print >>pFile,' Final refinement wR = %.2f%% on %d observations in this histogram'%(Histogram['Residuals']['wR'],Histogram['Residuals']['Nobs'])
3216            print >>pFile,' Other residuals: R = %.2f%%, Rb = %.2f%%, wRb = %.2f%% wRmin = %.2f%%'% \
3217                (Histogram['Residuals']['R'],Histogram['Residuals']['Rb'],Histogram['Residuals']['wRb'],Histogram['Residuals']['wRmin'])
3218            if Print:
3219                print >>pFile,' Instrument type: ',Sample['Type']
3220                if FFtables != None and 'N' not in Inst['Type'][0]:
3221                    PrintFprime(FFtables,pfx,pFile)
3222                PrintSampleParmsSig(Sample,sampSig)
3223                PrintInstParmsSig(Inst,instSig)
3224                PrintBackgroundSig(Background,backSig)
3225               
Note: See TracBrowser for help on using the repository browser.