source: trunk/GSASIIIO.py @ 1528

Last change on this file since 1528 was 1528, checked in by toby, 8 years ago

missing quotes on PWDR init.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 97.9 KB
Line 
1# -*- coding: utf-8 -*-
2########### SVN repository information ###################
3# $Date: 2014-10-17 14:51:16 +0000 (Fri, 17 Oct 2014) $
4# $Author: toby $
5# $Revision: 1528 $
6# $URL: trunk/GSASIIIO.py $
7# $Id: GSASIIIO.py 1528 2014-10-17 14:51:16Z toby $
8########### SVN repository information ###################
9'''
10*GSASIIIO: Misc I/O routines*
11=============================
12
13Module with miscellaneous routines for input and output. Many
14are GUI routines to interact with user.
15
16Includes support for image reading.
17
18Also includes base classes for data import routines.
19
20'''
21"""GSASIIIO: functions for IO of data
22   Copyright: 2008, Robert B. Von Dreele (Argonne National Laboratory)
23"""
24import wx
25import math
26import numpy as np
27import cPickle
28import sys
29import re
30import random as ran
31import GSASIIpath
32GSASIIpath.SetVersionNumber("$Revision: 1528 $")
33import GSASIIgrid as G2gd
34import GSASIIspc as G2spc
35import GSASIIlattice as G2lat
36import GSASIIpwdGUI as G2pdG
37import GSASIIElem as G2el
38import GSASIIstrIO as G2stIO
39import GSASIImapvars as G2mv
40import os
41import os.path as ospath
42
43DEBUG = False       #=True for various prints
44TRANSP = False      #=true to transpose images for testing
45npsind = lambda x: np.sin(x*np.pi/180.)
46
47def sfloat(S):
48    'Convert a string to float. An empty field is treated as zero'
49    if S.strip():
50        return float(S)
51    else:
52        return 0.0
53
54def sint(S):
55    'Convert a string to int. An empty field is treated as zero'
56    if S.strip():
57        return int(S)
58    else:
59        return 0
60
61def trim(val):
62    '''Simplify a string containing leading and trailing spaces
63    as well as newlines, tabs, repeated spaces etc. into a shorter and
64    more simple string, by replacing all ranges of whitespace
65    characters with a single space.
66
67    :param str val: the string to be simplified
68
69    :returns: the (usually) shortened version of the string
70    '''
71    return re.sub('\s+', ' ', val).strip()
72
73def makeInstDict(names,data,codes):
74    inst = dict(zip(names,zip(data,data,codes)))
75    for item in inst:
76        inst[item] = list(inst[item])
77    return inst
78
79def FileDlgFixExt(dlg,file):
80    'this is needed to fix a problem in linux wx.FileDialog'
81    ext = dlg.GetWildcard().split('|')[2*dlg.GetFilterIndex()+1].strip('*')
82    if ext not in file:
83        file += ext
84    return file
85       
86def GetPowderPeaks(fileName):
87    'Read powder peaks from a file'
88    sind = lambda x: math.sin(x*math.pi/180.)
89    asind = lambda x: 180.*math.asin(x)/math.pi
90    Cuka = 1.54052
91    File = open(fileName,'Ur')
92    Comments = []
93    peaks = []
94    S = File.readline()
95    while S:
96        if S[:1] == '#':
97            Comments.append(S[:-1])
98        else:
99            item = S.split()
100            if len(item) == 1:
101                peaks.append([float(item[0]),1.0])
102            elif len(item) > 1:
103                peaks.append([float(item[0]),float(item[0])])
104        S = File.readline()
105    File.close()
106    if Comments:
107       print 'Comments on file:'
108       for Comment in Comments: print Comment
109    Peaks = []
110    if peaks[0][0] > peaks[-1][0]:          # d-spacings - assume CuKa
111        for peak in peaks:
112            dsp = peak[0]
113            sth = Cuka/(2.0*dsp)
114            if sth < 1.0:
115                tth = 2.0*asind(sth)
116            else:
117                break
118            Peaks.append([tth,peak[1],True,False,0,0,0,dsp,0.0])
119    else:                                   #2-thetas - assume Cuka (for now)
120        for peak in peaks:
121            tth = peak[0]
122            dsp = Cuka/(2.0*sind(tth/2.0))
123            Peaks.append([tth,peak[1],True,False,0,0,0,dsp,0.0])
124    return Comments,Peaks
125
126def CheckImageFile(G2frame,imagefile):
127    '''Get an new image file name if the specified one does not
128    exist
129
130    :param wx.Frame G2frame: main GSAS-II Frame and data object
131
132    :param str imagefile: name of image file
133
134    :returns: imagefile, if it exists, or the name of a file
135      that does exist or False if the user presses Cancel
136
137    '''
138    if not ospath.exists(imagefile):
139        dlg = wx.FileDialog(G2frame, 'Bad image file name; choose name', '.', '',\
140        'Any image file (*.edf;*.tif;*.tiff;*.mar*;*.ge*;*.avg;*.sum;*.img)\
141        |*.edf;*.tif;*.tiff;*.mar*;*.ge*;*.avg;*.sum;*.img|\
142        European detector file (*.edf)|*.edf|\
143        Any detector tif (*.tif;*.tiff)|*.tif;*.tiff|\
144        MAR file (*.mar*)|*.mar*|\
145        GE Image (*.ge*;*.avg;*.sum)|*.ge*;*.avg;*.sum|\
146        ADSC Image (*.img)|*.img|\
147        All files (*.*)|*.*',wx.OPEN|wx.CHANGE_DIR)
148        try:
149            dlg.SetFilename(''+ospath.split(imagefile)[1])
150            if dlg.ShowModal() == wx.ID_OK:
151                imagefile = dlg.GetPath()
152            else:
153                imagefile = False
154        finally:
155            dlg.Destroy()
156    return imagefile
157
158def EditImageParms(parent,Data,Comments,Image,filename):
159    dlg = wx.Dialog(parent, wx.ID_ANY, 'Edit image parameters',
160                    style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
161    def onClose(event):
162        dlg.EndModal(wx.ID_OK)
163    mainsizer = wx.BoxSizer(wx.VERTICAL)
164    h,w = Image.shape[:2]
165    mainsizer.Add(wx.StaticText(dlg,wx.ID_ANY,
166                                'File '+str(filename)+'\nImage size: '+str(h)+' x '+str(w)),
167                  0,wx.ALIGN_LEFT|wx.ALL, 2)
168   
169    vsizer = wx.BoxSizer(wx.HORIZONTAL)
170    vsizer.Add(wx.StaticText(dlg,wx.ID_ANY,u'Wavelength (\xC5) '),
171               0,wx.ALIGN_LEFT|wx.ALL, 2)
172    wdgt = G2gd.ValidatedTxtCtrl(dlg,Data,'wavelength')
173    vsizer.Add(wdgt)
174    mainsizer.Add(vsizer,0,wx.ALIGN_LEFT|wx.ALL, 2)
175
176    vsizer = wx.BoxSizer(wx.HORIZONTAL)
177    vsizer.Add(wx.StaticText(dlg,wx.ID_ANY,u'Pixel size (\xb5m). Width '),
178               0,wx.ALIGN_LEFT|wx.ALL, 2)
179    wdgt = G2gd.ValidatedTxtCtrl(dlg,Data['pixelSize'],0,
180                                 size=(50,-1))
181    vsizer.Add(wdgt)
182    vsizer.Add(wx.StaticText(dlg,wx.ID_ANY,u'  Height '),
183               wx.ALIGN_LEFT|wx.ALL, 2)
184    wdgt = G2gd.ValidatedTxtCtrl(dlg,Data['pixelSize'],1,
185                                 size=(50,-1))
186    vsizer.Add(wdgt)
187    mainsizer.Add(vsizer,0,wx.ALIGN_LEFT|wx.ALL, 2)
188
189    vsizer = wx.BoxSizer(wx.HORIZONTAL)
190    vsizer.Add(wx.StaticText(dlg,wx.ID_ANY,u'Sample to detector (mm) '),
191               0,wx.ALIGN_LEFT|wx.ALL, 2)
192    wdgt = G2gd.ValidatedTxtCtrl(dlg,Data,'distance')
193    vsizer.Add(wdgt)
194    mainsizer.Add(vsizer,0,wx.ALIGN_LEFT|wx.ALL, 2)
195
196    vsizer = wx.BoxSizer(wx.HORIZONTAL)
197    vsizer.Add(wx.StaticText(dlg,wx.ID_ANY,u'Beam center (pixels). X = '),
198               0,wx.ALIGN_LEFT|wx.ALL, 2)
199    wdgt = G2gd.ValidatedTxtCtrl(dlg,Data['center'],0,
200                                 size=(75,-1))
201    vsizer.Add(wdgt)
202    vsizer.Add(wx.StaticText(dlg,wx.ID_ANY,u'  Y = '),
203               wx.ALIGN_LEFT|wx.ALL, 2)
204    wdgt = G2gd.ValidatedTxtCtrl(dlg,Data['center'],1,
205                                 size=(75,-1))
206    vsizer.Add(wdgt)
207    mainsizer.Add(vsizer,0,wx.ALIGN_LEFT|wx.ALL, 2)
208
209    vsizer = wx.BoxSizer(wx.HORIZONTAL)
210    vsizer.Add(wx.StaticText(dlg,wx.ID_ANY,u'Comments '),
211               0,wx.ALIGN_LEFT|wx.ALL, 2)
212    wdgt = G2gd.ValidatedTxtCtrl(dlg,Comments,0,size=(250,-1))
213    vsizer.Add(wdgt)
214    mainsizer.Add(vsizer,0,wx.ALIGN_LEFT|wx.ALL, 2)
215
216    btnsizer = wx.StdDialogButtonSizer()
217    OKbtn = wx.Button(dlg, wx.ID_OK, 'Continue')
218    OKbtn.SetDefault()
219    OKbtn.Bind(wx.EVT_BUTTON,onClose)
220    btnsizer.AddButton(OKbtn) # not sure why this is needed
221    btnsizer.Realize()
222    mainsizer.Add(btnsizer, 1, wx.ALIGN_CENTER|wx.ALL|wx.EXPAND, 5)
223    dlg.SetSizer(mainsizer)
224    dlg.CenterOnParent()
225    dlg.ShowModal()
226
227def GetImageData(G2frame,imagefile,imageOnly=False):
228    '''Read an image with the file reader keyed by the
229    file extension
230
231    :param wx.Frame G2frame: main GSAS-II Frame and data object.
232
233    :param str imagefile: name of image file
234
235    :param bool imageOnly: If True return only the image,
236      otherwise  (default) return more (see below)
237
238    :returns: an image as a numpy array or a list of four items:
239      Comments, Data, Npix and the Image, as selected by imageOnly
240
241    '''
242    ext = ospath.splitext(imagefile)[1]
243    Comments = []
244    if ext == '.tif' or ext == '.tiff':
245        Comments,Data,Npix,Image = GetTifData(imagefile)
246        if Npix == 0:
247            print("GetTifData failed to read "+str(filename)+" Trying PIL")
248            import scipy.misc
249            Image = scipy.misc.imread(imagefile,flatten=True)
250            Npix = Image.size
251            Comments = ['no metadata']
252            Data = {'wavelength': 0.1, 'pixelSize': [200, 200], 'distance': 100.0}
253            Data['size'] = list(Image.shape)
254            Data['center'] = [int(i/2) for i in Image.shape]
255            if not imageOnly:
256                EditImageParms(G2frame,Data,Comments,Image,imagefile)
257    elif ext == '.edf':
258        Comments,Data,Npix,Image = GetEdfData(imagefile)
259    elif ext == '.img':
260        Comments,Data,Npix,Image = GetImgData(imagefile)
261        Image[0][0] = 0
262    elif ext == '.mar3450' or ext == '.mar2300':
263        Comments,Data,Npix,Image = GetMAR345Data(imagefile)
264    elif ext in ['.sum','.avg'] or 'ge' in ext:
265        Comments,Data,Npix,Image = GetGEsumData(imagefile)
266    elif ext == '.G2img':
267        Comments,Data,Npix,Image = GetG2Image(imagefile)
268    elif ext == '.png':
269        Comments,Data,Npix,Image = GetPNGData(imagefile)
270        if not imageOnly:
271            EditImageParms(G2frame,Data,Comments,Image,imagefile)
272    if imageOnly:
273        if TRANSP:
274            return Image.T
275        else:
276            return Image
277    else:
278        if TRANSP:
279            return Comments,Data,Npix,Image.T
280        else:
281            return Comments,Data,Npix,Image
282       
283def PutG2Image(filename,Comments,Data,Npix,image):
284    'Write an image as a python pickle - might be better as an .edf file?'
285    File = open(filename,'wb')
286    cPickle.dump([Comments,Data,Npix,image],File,1)
287    File.close()
288    return
289   
290def GetG2Image(filename):
291    'Read an image as a python pickle'
292    File = open(filename,'rb')
293    Comments,Data,Npix,image = cPickle.load(File)
294    File.close()
295    return Comments,Data,Npix,image
296   
297def GetEdfData(filename,imageOnly=False):   
298    'Read European detector data edf file'
299    import struct as st
300    import array as ar
301    if not imageOnly:
302        print 'Read European detector data edf file: ',filename
303    File = open(filename,'rb')
304    fileSize = os.stat(filename).st_size
305    head = File.read(3072)
306    lines = head.split('\n')
307    sizexy = [0,0]
308    pixSize = [154,154]     #Pixium4700?
309    cent = [0,0]
310    wave = 1.54187  #default <CuKa>
311    dist = 1000.
312    head = ['European detector data',]
313    for line in lines:
314        line = line.replace(';',' ').strip()
315        fields = line.split()
316        if 'Dim_1' in line:
317            sizexy[0] = int(fields[2])
318        elif 'Dim_2' in line:
319            sizexy[1] = int(fields[2])
320        elif 'DataType' in line:
321            dType = fields[2]
322        elif 'wavelength' in line:
323            wave = float(fields[2])
324        elif 'Size' in line:
325            imSize = int(fields[2])
326        elif 'DataType' in lines:
327            dType = fields[2]
328        elif 'pixel_size_x' in line:
329            pixSize[0] = float(fields[2])
330        elif 'pixel_size_y' in line:
331            pixSize[1] = float(fields[2])
332        elif 'beam_center_x' in line:
333            cent[0] = float(fields[2])
334        elif 'beam_center_y' in line:
335            cent[1] = float(fields[2])
336        elif 'refined_distance' in line:
337            dist = float(fields[2])
338        if line:
339            head.append(line)
340        else:   #blank line at end of header
341            break 
342    File.seek(fileSize-imSize)
343    if dType == 'UnsignedShort':       
344        image = np.array(ar.array('H',File.read(imSize)),dtype=np.int32)
345    elif dType == 'UnsignedInt':
346        image = np.array(ar.array('L',File.read(imSize)),dtype=np.int32)
347    elif dType == 'UnsignedLong':
348        image = np.array(ar.array('L',File.read(imSize)),dtype=np.int32)       
349    image = np.reshape(image,(sizexy[1],sizexy[0]))
350    data = {'pixelSize':pixSize,'wavelength':wave,'distance':dist,'center':cent,'size':sizexy}
351    Npix = sizexy[0]*sizexy[1]
352    File.close()   
353    if imageOnly:
354        return image
355    else:
356        return head,data,Npix,image
357       
358def GetGEsumData(filename,imageOnly=False):
359    'Read SUM file as produced at 1-ID from G.E. images'
360    import struct as st
361    import array as ar
362    if not imageOnly:
363        print 'Read GE sum file: ',filename   
364    File = open(filename,'rb')
365    if '.sum' in filename:
366        head = ['GE detector sum data from APS 1-ID',]
367        sizexy = [2048,2048]
368    elif '.avg' in filename or '.ge' in filename:
369        head = ['GE detector avg or ge* data from APS 1-ID',]
370        sizexy = [2048,2048]
371    else:
372        head = ['GE detector raw data from APS 1-ID',]
373        File.seek(18)
374        size,nframes = st.unpack('<ih',File.read(6))
375        sizexy = [2048,2048]
376        pos = 8192
377        File.seek(pos)
378    Npix = sizexy[0]*sizexy[1]
379    if '.sum' in filename:
380        image = np.array(ar.array('f',File.read(4*Npix)),dtype=np.int32)
381    elif '.avg' in filename or '.ge' in filename:
382        image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
383    else:
384        image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
385        while nframes > 1:
386            image += np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
387            nframes -= 1
388    image = np.reshape(image,(sizexy[1],sizexy[0]))
389    data = {'pixelSize':[200,200],'wavelength':0.15,'distance':250.0,'center':[204.8,204.8],'size':sizexy} 
390    File.close()   
391    if imageOnly:
392        return image
393    else:
394        return head,data,Npix,image
395       
396def GetImgData(filename,imageOnly=False):
397    'Read an ADSC image file'
398    import struct as st
399    import array as ar
400    if not imageOnly:
401        print 'Read ADSC img file: ',filename
402    File = open(filename,'rb')
403    head = File.read(511)
404    lines = head.split('\n')
405    head = []
406    center = [0,0]
407    for line in lines[1:-2]:
408        line = line.strip()[:-1]
409        if line:
410            if 'SIZE1' in line:
411                size = int(line.split('=')[1])
412                Npix = size*size
413            elif 'WAVELENGTH' in line:
414                wave = float(line.split('=')[1])
415            elif 'BIN' in line:
416                if line.split('=')[1] == '2x2':
417                    pixel=(102,102)
418                else:
419                    pixel = (51,51)
420            elif 'DISTANCE' in line:
421                distance = float(line.split('=')[1])
422            elif 'CENTER_X' in line:
423                center[0] = float(line.split('=')[1])
424            elif 'CENTER_Y' in line:
425                center[1] = float(line.split('=')[1])
426            head.append(line)
427    data = {'pixelSize':pixel,'wavelength':wave,'distance':distance,'center':center,'size':[size,size]}
428    image = []
429    row = 0
430    pos = 512
431    File.seek(pos)
432    image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
433    image = np.reshape(image,(sizexy[1],sizexy[0]))
434#    image = np.zeros(shape=(size,size),dtype=np.int32)   
435#    while row < size:
436#        File.seek(pos)
437#        line = ar.array('H',File.read(2*size))
438#        image[row] = np.asarray(line)
439#        row += 1
440#        pos += 2*size
441    File.close()
442    if imageOnly:
443        return image
444    else:
445        return lines[1:-2],data,Npix,image
446       
447def GetMAR345Data(filename,imageOnly=False):
448    'Read a MAR-345 image plate image'
449    import array as ar
450    import struct as st
451    try:
452        import pack_f as pf
453    except:
454        msg = wx.MessageDialog(None, message="Unable to load the GSAS MAR image decompression, pack_f",
455                               caption="Import Error",
456                               style=wx.ICON_ERROR | wx.OK | wx.STAY_ON_TOP)
457        msg.ShowModal()
458        return None,None,None,None
459
460    if not imageOnly:
461        print 'Read Mar345 file: ',filename
462    File = open(filename,'rb')
463    head = File.read(4095)
464    numbers = st.unpack('<iiiiiiiiii',head[:40])
465    lines = head[128:].split('\n')
466    head = []
467    for line in lines:
468        line = line.strip()
469        if 'PIXEL' in line:
470            values = line.split()
471            pixel = (int(values[2]),int(values[4]))     #in microns
472        elif 'WAVELENGTH' in line:
473            wave = float(line.split()[1])
474        elif 'DISTANCE' in line:
475            distance = float(line.split()[1])           #in mm
476        elif 'CENTER' in line:
477            values = line.split()
478            center = [float(values[2])/10.,float(values[4])/10.]    #make in mm from pixels
479        if line: 
480            head.append(line)
481    data = {'pixelSize':pixel,'wavelength':wave,'distance':distance,'center':center}
482    for line in head:
483        if 'FORMAT' in line[0:6]:
484            items = line.split()
485            size = int(items[1])
486            Npix = size*size
487    pos = 4096
488    data['size'] = [size,size]
489    File.seek(pos)
490    line = File.read(8)
491    while 'CCP4' not in line:       #get past overflow list for now
492        line = File.read(8)
493        pos += 8
494    pos += 37
495    File.seek(pos)
496    raw = File.read()
497    File.close()
498    image = np.zeros(shape=(size,size),dtype=np.int32)
499    image = np.flipud(pf.pack_f(len(raw),raw,size,image).T)  #transpose to get it right way around & flip
500    if imageOnly:
501        return image
502    else:
503        return head,data,Npix,image
504       
505def GetPNGData(filename,imageOnly=False):
506    '''Read an image in a png format, assumes image is converted from CheMin tif file
507    so default parameters are that machine.
508    '''
509    import scipy.misc
510    Image = scipy.misc.imread(filename,flatten=True)
511    Npix = Image.size
512    Comments = ['no metadata']
513    pixy = list(Image.shape)
514    sizexy = [40,40]
515    Data = {'wavelength': 1.78892, 'pixelSize': sizexy, 'distance': 18.0,'size':pixy}
516    Data['center'] = [pixy[0]*sizexy[0]/1000,pixy[1]*sizexy[1]/2000]
517    if imageOnly:
518        return Image.T
519    else:
520        return Comments,Data,Npix,Image.T
521
522def GetTifData(filename,imageOnly=False):
523    '''Read an image in a pseudo-tif format,
524    as produced by a wide variety of software, almost always
525    incorrectly in some way.
526    '''
527    import struct as st
528    try:
529        import Image as Im
530    except ImportError:
531        try:
532            from PIL import Image as Im
533        except ImportError:
534            print "PIL/pillow Image module not present. TIFs cannot be read without this"
535            raise Exception("PIL/pillow Image module not found")
536    import array as ar
537    import ReadMarCCDFrame as rmf
538    File = open(filename,'rb')
539    dataType = 5
540    center = [None,None]
541    wavelength = None
542    distance = None
543    try:
544        Meta = open(filename+'.metadata','Ur')
545        head = Meta.readlines()
546        for line in head:
547            line = line.strip()
548            if 'dataType=' in line:
549                dataType = int(line.split('=')[1])
550        Meta.close()
551    except IOError:
552        print 'no metadata file found - will try to read file anyway'
553        head = ['no metadata file found',]
554       
555    tag = File.read(2)
556    byteOrd = '<'
557    if tag == 'II' and int(st.unpack('<h',File.read(2))[0]) == 42:     #little endian
558        IFD = int(st.unpack(byteOrd+'i',File.read(4))[0])
559    elif tag == 'MM' and int(st.unpack('>h',File.read(2))[0]) == 42:   #big endian
560        byteOrd = '>'
561        IFD = int(st.unpack(byteOrd+'i',File.read(4))[0])       
562    else:
563        lines = ['not a detector tiff file',]
564        return lines,0,0,0
565    File.seek(IFD)                                                  #get number of directory entries
566    NED = int(st.unpack(byteOrd+'h',File.read(2))[0])
567    IFD = {}
568    nSlice = 1
569    for ied in range(NED):
570        Tag,Type = st.unpack(byteOrd+'Hh',File.read(4))
571        nVal = st.unpack(byteOrd+'i',File.read(4))[0]
572        if DEBUG: print 'Try:',Tag,Type,nVal
573        if Type == 1:
574            Value = st.unpack(byteOrd+nVal*'b',File.read(nVal))
575        elif Type == 2:
576            Value = st.unpack(byteOrd+'i',File.read(4))
577        elif Type == 3:
578            Value = st.unpack(byteOrd+nVal*'h',File.read(nVal*2))
579            x = st.unpack(byteOrd+nVal*'h',File.read(nVal*2))
580        elif Type == 4:
581            if Tag in [273,279]:
582                nSlice = nVal
583                nVal = 1
584            Value = st.unpack(byteOrd+nVal*'i',File.read(nVal*4))
585        elif Type == 5:
586            Value = st.unpack(byteOrd+nVal*'i',File.read(nVal*4))
587        elif Type == 11:
588            Value = st.unpack(byteOrd+nVal*'f',File.read(nVal*4))
589        IFD[Tag] = [Type,nVal,Value]
590        if DEBUG: print Tag,IFD[Tag]
591    sizexy = [IFD[256][2][0],IFD[257][2][0]]
592    [nx,ny] = sizexy
593    Npix = nx*ny
594    if 34710 in IFD:
595        if not imageOnly:
596            print 'Read MAR CCD tiff file: ',filename
597        marFrame = rmf.marFrame(File,byteOrd,IFD)
598        image = np.flipud(np.array(np.asarray(marFrame.image),dtype=np.int32))
599        tifType = marFrame.filetitle
600        pixy = [marFrame.pixelsizeX/1000.0,marFrame.pixelsizeY/1000.0]
601        head = marFrame.outputHead()
602# extract resonable wavelength from header
603        wavelength = marFrame.sourceWavelength*1e-5
604        wavelength = (marFrame.opticsWavelength > 0) and marFrame.opticsWavelength*1e-5 or wavelength
605        wavelength = (wavelength <= 0) and None or wavelength
606# extract resonable distance from header
607        distance = (marFrame.startXtalToDetector+marFrame.endXtalToDetector)*5e-4
608        distance = (distance <= 0) and marFrame.xtalToDetector*1e-3 or distance
609        distance = (distance <= 0) and None or distance
610# extract resonable center from header
611        center = [marFrame.beamX*marFrame.pixelsizeX*1e-9,marFrame.beamY*marFrame.pixelsizeY*1e-9]
612        center = (center[0] != 0 and center[1] != 0) and center or [None,None]
613#print head,tifType,pixy
614    elif nSlice > 1:    #CheMin multislice tif file!
615        tifType = 'CheMin'
616        pixy = [40,40]
617        image = np.flipud(np.array(Im.open(filename)))*10.
618        distance = 18.0
619        center = [pixy[0]*sizexy[0]/2000,0]     #the CheMin beam stop is here
620        wavelength = 1.78892
621    elif 272 in IFD:
622        ifd = IFD[272]
623        File.seek(ifd[2][0])
624        S = File.read(ifd[1])
625        if 'PILATUS' in S:
626            tifType = 'Pilatus'
627            dataType = 0
628            pixy = [172,172]
629            File.seek(4096)
630            if not imageOnly:
631                print 'Read Pilatus tiff file: ',filename
632            image = ar.array('L',File.read(4*Npix))
633            image = np.array(np.asarray(image),dtype=np.int32)
634        else:
635            if IFD[258][2][0] == 16:
636                tifType = 'GE'
637                pixy = [200,200]
638                File.seek(8)
639                if not imageOnly:
640                    print 'Read GE-detector tiff file: ',filename
641                image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
642            elif IFD[258][2][0] == 32:
643                tifType = 'CHESS'
644                pixy = [200,200]
645                File.seek(8)
646                if not imageOnly:
647                    print 'Read CHESS-detector tiff file: ',filename
648                image = np.array(ar.array('L',File.read(4*Npix)),dtype=np.int32)
649           
650    elif 262 in IFD and IFD[262][2][0] > 4:
651        tifType = 'DND'
652        pixy = [158,158]
653        File.seek(512)
654        if not imageOnly:
655            print 'Read DND SAX/WAX-detector tiff file: ',filename
656        image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
657    elif sizexy == [1536,1536]:
658        tifType = 'APS Gold'
659        pixy = [150,150]
660        File.seek(64)
661        if not imageOnly:
662            print 'Read Gold tiff file:',filename
663        image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
664    elif sizexy == [2048,2048] or sizexy == [1024,1024] or sizexy == [3072,3072]:
665        if IFD[273][2][0] == 8:
666            if IFD[258][2][0] == 32:
667                tifType = 'PE'
668                pixy = [200,200]
669                File.seek(8)
670                if not imageOnly:
671                    print 'Read APS PE-detector tiff file: ',filename
672                if dataType == 5:
673                    image = np.array(ar.array('f',File.read(4*Npix)),dtype=np.float32)
674                else:
675                    image = np.array(ar.array('I',File.read(4*Npix)),dtype=np.int32)
676               
677        elif IFD[273][2][0] == 4096:
678            if sizexy[0] == 3072:
679                pixy =  [73,73]
680                tifType = 'MAR225'           
681            else:
682                pixy = [158,158]
683                tifType = 'MAR325'           
684            File.seek(4096)
685            if not imageOnly:
686                print 'Read MAR CCD tiff file: ',filename
687            image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
688        elif IFD[273][2][0] == 512:
689            tiftype = '11-ID-C'
690            pixy = [200,200]
691            File.seek(512)
692            if not imageOnly:
693                print 'Read 11-ID-C tiff file: ',filename
694            image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)           
695    elif sizexy == [4096,4096]:
696        if IFD[273][2][0] == 8:
697            if IFD[258][2][0] == 16:
698                tifType = 'scanCCD'
699                pixy = [9,9]
700                File.seek(8)
701                if not imageOnly:
702                    print 'Read APS scanCCD tiff file: ',filename
703                image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
704        elif IFD[273][2][0] == 4096:
705            tifType = 'Rayonix'
706            pixy = [73.242,73.242]
707            File.seek(4096)
708            if not imageOnly:
709                print 'Read Rayonix MX300HE tiff file: ',filename
710            image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
711#    elif sizexy == [960,960]:
712#        tiftype = 'PE-BE'
713#        pixy = (200,200)
714#        File.seek(8)
715#        if not imageOnly:
716#            print 'Read Gold tiff file:',filename
717#        image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
718           
719    else:
720        lines = ['not a known detector tiff file',]
721        return lines,0,0,0
722       
723    image = np.reshape(image,(sizexy[1],sizexy[0]))
724    center = (not center[0]) and [pixy[0]*sizexy[0]/2000,pixy[1]*sizexy[1]/2000] or center
725    wavelength = (not wavelength) and 0.10 or wavelength
726    distance = (not distance) and 100.0 or distance
727    data = {'pixelSize':pixy,'wavelength':wavelength,'distance':distance,'center':center,'size':sizexy}
728    File.close()   
729    if imageOnly:
730        return image
731    else:
732        return head,data,Npix,image
733   
734#def GetTifData(filename,imageOnly=False):
735#    import struct as st
736#    import array as ar
737#    File = open(filename,'rb')
738#    dataType = 5
739#    try:
740#        Meta = open(filename+'.metadata','Ur')
741#        head = Meta.readlines()
742#        for line in head:
743#            line = line.strip()
744#            if 'dataType=' in line:
745#                dataType = int(line.split('=')[1])
746#        Meta.close()
747#    except IOError:
748#        print 'no metadata file found - will try to read file anyway'
749#        head = ['no metadata file found',]
750#       
751#    tag = File.read(2)
752#    byteOrd = '<'
753#    if tag == 'II' and int(st.unpack('<h',File.read(2))[0]) == 42:     #little endian
754#        IFD = int(st.unpack(byteOrd+'i',File.read(4))[0])
755#    elif tag == 'MM' and int(st.unpack('>h',File.read(2))[0]) == 42:   #big endian
756#        byteOrd = '>'
757#        IFD = int(st.unpack(byteOrd+'i',File.read(4))[0])       
758#    else:
759#        lines = ['not a detector tiff file',]
760#        return lines,0,0,0
761#    File.seek(IFD)                                                  #get number of directory entries
762#    NED = int(st.unpack(byteOrd+'h',File.read(2))[0])
763#    IFD = {}
764#    for ied in range(NED):
765#        Tag,Type = st.unpack(byteOrd+'Hh',File.read(4))
766#        nVal = st.unpack(byteOrd+'i',File.read(4))[0]
767#        if Type == 1:
768#            Value = st.unpack(byteOrd+nVal*'b',File.read(nVal))
769#        elif Type == 2:
770#            Value = st.unpack(byteOrd+'i',File.read(4))
771#        elif Type == 3:
772#            Value = st.unpack(byteOrd+nVal*'h',File.read(nVal*2))
773#            x = st.unpack(byteOrd+nVal*'h',File.read(nVal*2))
774#        elif Type == 4:
775#            Value = st.unpack(byteOrd+nVal*'i',File.read(nVal*4))
776#        elif Type == 5:
777#            Value = st.unpack(byteOrd+nVal*'i',File.read(nVal*4))
778#        elif Type == 11:
779#            Value = st.unpack(byteOrd+nVal*'f',File.read(nVal*4))
780#        IFD[Tag] = [Type,nVal,Value]
781##        print Tag,IFD[Tag]
782#    sizexy = [IFD[256][2][0],IFD[257][2][0]]
783#    [nx,ny] = sizexy
784#    Npix = nx*ny
785#    if 272 in IFD:
786#        ifd = IFD[272]
787#        File.seek(ifd[2][0])
788#        S = File.read(ifd[1])
789#        if 'PILATUS' in S:
790#            tifType = 'Pilatus'
791#            dataType = 0
792#            pixy = (172,172)
793#            File.seek(4096)
794#            if not imageOnly:
795#                print 'Read Pilatus tiff file: ',filename
796#            image = ar.array('L',File.read(4*Npix))
797#            image = np.array(np.asarray(image),dtype=np.int32)
798#    elif 262 in IFD and IFD[262][2][0] > 4:
799#        tifType = 'DND'
800#        pixy = (158,158)
801#        File.seek(512)
802#        if not imageOnly:
803#            print 'Read DND SAX/WAX-detector tiff file: ',filename
804#        image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
805#    elif sizexy == [1536,1536]:
806#        tifType = 'APS Gold'
807#        pixy = (150,150)
808#        File.seek(64)
809#        if not imageOnly:
810#            print 'Read Gold tiff file:',filename
811#        image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
812#    elif sizexy == [2048,2048] or sizexy == [1024,1024] or sizexy == [3072,3072]:
813#        if IFD[273][2][0] == 8:
814#            if IFD[258][2][0] == 32:
815#                tifType = 'PE'
816#                pixy = (200,200)
817#                File.seek(8)
818#                if not imageOnly:
819#                    print 'Read APS PE-detector tiff file: ',filename
820#                if dataType == 5:
821#                    image = np.array(ar.array('f',File.read(4*Npix)),dtype=np.float32)
822#                else:
823#                    image = np.array(ar.array('I',File.read(4*Npix)),dtype=np.int32)
824#        elif IFD[273][2][0] == 4096:
825#            if sizexy[0] == 3072:
826#                pixy =  (73,73)
827#                tifType = 'MAR225'           
828#            else:
829#                pixy = (158,158)
830#                tifType = 'MAR325'           
831#            File.seek(4096)
832#            if not imageOnly:
833#                print 'Read MAR CCD tiff file: ',filename
834#            image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
835#        elif IFD[273][2][0] == 512:
836#            tiftype = '11-ID-C'
837#            pixy = [200,200]
838#            File.seek(512)
839#            if not imageOnly:
840#                print 'Read 11-ID-C tiff file: ',filename
841#            image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)           
842#    elif sizexy == [4096,4096]:
843#        if IFD[273][2][0] == 8:
844#            if IFD[258][2][0] == 16:
845#                tifType = 'scanCCD'
846#                pixy = (9,9)
847#                File.seek(8)
848#                if not imageOnly:
849#                    print 'Read APS scanCCD tiff file: ',filename
850#                image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
851#        elif IFD[273][2][0] == 4096:
852#            tifType = 'Rayonix'
853#            pixy = (73.242,73.242)
854#            File.seek(4096)
855#            if not imageOnly:
856#                print 'Read Rayonix MX300HE tiff file: ',filename
857#            image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
858##    elif sizexy == [960,960]:
859##        tiftype = 'PE-BE'
860##        pixy = (200,200)
861##        File.seek(8)
862##        if not imageOnly:
863##            print 'Read Gold tiff file:',filename
864##        image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
865#           
866#    else:
867#        lines = ['not a known detector tiff file',]
868#        return lines,0,0,0
869#       
870#    image = np.reshape(image,(sizexy[1],sizexy[0]))
871#    center = [pixy[0]*sizexy[0]/2000,pixy[1]*sizexy[1]/2000]
872#    data = {'pixelSize':pixy,'wavelength':0.10,'distance':100.0,'center':center,'size':sizexy}
873#    File.close()   
874#    if imageOnly:
875#        return image
876#    else:
877#        return head,data,Npix,image
878#   
879def ProjFileOpen(G2frame):
880    'Read a GSAS-II project file and load into the G2 data tree'
881    if not os.path.exists(G2frame.GSASprojectfile):
882        print ('\n*** Error attempt to open project file that does not exist:\n   '+
883               str(G2frame.GSASprojectfile))
884        return
885    file = open(G2frame.GSASprojectfile,'rb')
886    print 'load from file: ',G2frame.GSASprojectfile
887    G2frame.SetTitle("GSAS-II data tree: "+
888                     os.path.split(G2frame.GSASprojectfile)[1])
889    wx.BeginBusyCursor()
890    try:
891        while True:
892            try:
893                data = cPickle.load(file)
894            except EOFError:
895                break
896            datum = data[0]
897           
898            Id = G2frame.PatternTree.AppendItem(parent=G2frame.root,text=datum[0])
899            if 'PWDR' in datum[0]:               
900                if 'ranId' not in datum[1][0]: # patch: add random Id if not present
901                    datum[1][0]['ranId'] = ran.randint(0,sys.maxint)
902                G2frame.PatternTree.SetItemPyData(Id,datum[1][:3])  #temp. trim off junk (patch?)
903            elif datum[0].startswith('HKLF'): 
904                if 'ranId' not in datum[1][0]: # patch: add random Id if not present
905                    datum[1][0]['ranId'] = ran.randint(0,sys.maxint)
906                G2frame.PatternTree.SetItemPyData(Id,datum[1])
907            else:
908                G2frame.PatternTree.SetItemPyData(Id,datum[1])
909            for datus in data[1:]:
910                sub = G2frame.PatternTree.AppendItem(Id,datus[0])
911#patch
912                if datus[0] == 'Instrument Parameters' and len(datus[1]) == 1:
913                    if 'PWDR' in datum[0]:
914                        datus[1] = [dict(zip(datus[1][3],zip(datus[1][0],datus[1][1],datus[1][2]))),{}]
915                    else:
916                        datus[1] = [dict(zip(datus[1][2],zip(datus[1][0],datus[1][1]))),{}]
917                    for item in datus[1][0]:               #zip makes tuples - now make lists!
918                        datus[1][0][item] = list(datus[1][0][item])
919#end patch
920                G2frame.PatternTree.SetItemPyData(sub,datus[1])
921            if 'IMG' in datum[0]:                   #retrieve image default flag & data if set
922                Data = G2frame.PatternTree.GetItemPyData(G2gd.GetPatternTreeItemId(G2frame,Id,'Image Controls'))
923                if Data['setDefault']:
924                    G2frame.imageDefault = Data               
925        file.close()
926        print('project load successful')
927        G2frame.NewPlot = True
928    except:
929        msg = wx.MessageDialog(G2frame,message="Error reading file "+
930            str(G2frame.GSASprojectfile)+". This is not a GSAS-II .gpx file",
931            caption="Load Error",style=wx.ICON_ERROR | wx.OK | wx.STAY_ON_TOP)
932        msg.ShowModal()
933    finally:
934        wx.EndBusyCursor()
935        G2frame.Status.SetStatusText('To reorder tree items, use mouse RB to drag/drop them')
936   
937def ProjFileSave(G2frame):
938    'Save a GSAS-II project file'
939    if not G2frame.PatternTree.IsEmpty():
940        file = open(G2frame.GSASprojectfile,'wb')
941        print 'save to file: ',G2frame.GSASprojectfile
942        wx.BeginBusyCursor()
943        try:
944            item, cookie = G2frame.PatternTree.GetFirstChild(G2frame.root)
945            while item:
946                data = []
947                name = G2frame.PatternTree.GetItemText(item)
948                data.append([name,G2frame.PatternTree.GetItemPyData(item)])
949                item2, cookie2 = G2frame.PatternTree.GetFirstChild(item)
950                while item2:
951                    name = G2frame.PatternTree.GetItemText(item2)
952                    data.append([name,G2frame.PatternTree.GetItemPyData(item2)])
953                    item2, cookie2 = G2frame.PatternTree.GetNextChild(item, cookie2)                           
954                item, cookie = G2frame.PatternTree.GetNextChild(G2frame.root, cookie)                           
955                cPickle.dump(data,file,1)
956            file.close()
957        finally:
958            wx.EndBusyCursor()
959        print('project save successful')
960
961def SaveIntegration(G2frame,PickId,data):
962    'Save image integration results as powder pattern(s)'
963    azms = G2frame.Integrate[1]
964    X = G2frame.Integrate[2][:-1]
965    N = len(X)
966    Id = G2frame.PatternTree.GetItemParent(PickId)
967    name = G2frame.PatternTree.GetItemText(Id)
968    name = name.replace('IMG ',data['type']+' ')
969    Comments = G2frame.PatternTree.GetItemPyData(G2gd.GetPatternTreeItemId(G2frame,Id, 'Comments'))
970    if 'PWDR' in name:
971        names = ['Type','Lam','Zero','Polariz.','U','V','W','X','Y','SH/L','Azimuth'] 
972        codes = [0 for i in range(11)]
973    elif 'SASD' in name:
974        names = ['Type','Lam','Zero','Azimuth'] 
975        codes = [0 for i in range(4)]
976        X = 4.*np.pi*npsind(X/2.)/data['wavelength']    #convert to q
977    Xminmax = [X[0],X[-1]]
978    LRazm = data['LRazimuth']
979    Azms = []
980    if data['fullIntegrate'] and data['outAzimuths'] == 1:
981        Azms = [45.0,]                              #a poor man's average?
982    else:
983        for i,azm in enumerate(azms[:-1]):
984            Azms.append((azms[i+1]+azm)/2.)
985    for i,azm in enumerate(azms[:-1]):
986        Aname = name+" Azm= %.2f"%(Azms[i])
987        item, cookie = G2frame.PatternTree.GetFirstChild(G2frame.root)
988        nOcc = 0
989        while item:
990            Name = G2frame.PatternTree.GetItemText(item)
991            if Aname in Name:
992                nOcc += 1
993            item, cookie = G2frame.PatternTree.GetNextChild(G2frame.root, cookie)
994        if nOcc:
995            Aname += '(%d)'%(nOcc)
996        Sample = G2pdG.SetDefaultSample()
997        Sample['Gonio. radius'] = data['distance']
998        Sample['Omega'] = data['GonioAngles'][0]
999        Sample['Chi'] = data['GonioAngles'][1]
1000        Sample['Phi'] = data['GonioAngles'][2]
1001        if 'PWDR' in Aname:
1002            parms = ['PXC',data['wavelength'],0.0,0.99,1.0,-0.10,0.4,0.30,1.0,0.0001,Azms[i]]    #set polarization for synchrotron radiation!
1003        elif 'SASD' in Aname:
1004            Sample['Trans'] = data['SampleAbs'][0]
1005            parms = ['LXC',data['wavelength'],0.0,Azms[i]]
1006        Y = G2frame.Integrate[0][i]
1007        W = np.where(Y>0.,1./Y,1.e-6)                    #probably not true
1008        Id = G2frame.PatternTree.AppendItem(parent=G2frame.root,text=Aname)
1009        G2frame.PatternTree.SetItemPyData(G2frame.PatternTree.AppendItem(Id,text='Comments'),Comments)                   
1010        G2frame.PatternTree.SetItemPyData(G2frame.PatternTree.AppendItem(Id,text='Limits'),[tuple(Xminmax),Xminmax])
1011        if 'PWDR' in Aname:
1012            G2frame.PatternTree.SetItemPyData(G2frame.PatternTree.AppendItem(Id,text='Background'),[['chebyschev',1,3,1.0,0.0,0.0],
1013                {'nDebye':0,'debyeTerms':[],'nPeaks':0,'peaksList':[]}])
1014        inst = [dict(zip(names,zip(parms,parms,codes))),{}]
1015        for item in inst[0]:
1016            inst[0][item] = list(inst[0][item])
1017        G2frame.PatternTree.SetItemPyData(G2frame.PatternTree.AppendItem(Id,text='Instrument Parameters'),inst)
1018        if 'PWDR' in Aname:
1019            G2frame.PatternTree.SetItemPyData(G2frame.PatternTree.AppendItem(Id,text='Sample Parameters'),Sample)
1020            G2frame.PatternTree.SetItemPyData(G2frame.PatternTree.AppendItem(Id,text='Peak List'),{'sigDict':{},'peaks':[]})
1021            G2frame.PatternTree.SetItemPyData(G2frame.PatternTree.AppendItem(Id,text='Index Peak List'),{'sigDict':{},'peaks':[]})
1022            G2frame.PatternTree.SetItemPyData(G2frame.PatternTree.AppendItem(Id,text='Unit Cells List'),[])
1023            G2frame.PatternTree.SetItemPyData(G2frame.PatternTree.AppendItem(Id,text='Reflection Lists'),{})
1024        elif 'SASD' in Aname:             
1025            G2frame.PatternTree.SetItemPyData(G2frame.PatternTree.AppendItem(Id,text='Substances'),G2pdG.SetDefaultSubstances())
1026            G2frame.PatternTree.SetItemPyData(G2frame.PatternTree.AppendItem(Id,text='Sample Parameters'),Sample)
1027            G2frame.PatternTree.SetItemPyData(G2frame.PatternTree.AppendItem(Id,text='Models'),G2pdG.SetDefaultSASDModel())
1028        valuesdict = {
1029            'wtFactor':1.0,
1030            'Dummy':False,
1031            'ranId':ran.randint(0,sys.maxint),
1032            }
1033        G2frame.PatternTree.SetItemPyData(
1034            Id,[valuesdict,
1035                [np.array(X),np.array(Y),np.array(W),np.zeros(N),np.zeros(N),np.zeros(N)]])
1036    return Id       #last powder pattern generated
1037           
1038# def powderFxyeSave(G2frame,exports,powderfile):
1039#     'Save a powder histogram as a GSAS FXYE file'
1040#     head,tail = ospath.split(powderfile)
1041#     name,ext = tail.split('.')
1042#     for i,export in enumerate(exports):
1043#         filename = ospath.join(head,name+'-%03d.'%(i)+ext)
1044#         prmname = filename.strip(ext)+'prm'
1045#         prm = open(prmname,'w')      #old style GSAS parm file
1046#         PickId = G2gd.GetPatternTreeItemId(G2frame, G2frame.root, export)
1047#         Inst = G2frame.PatternTree.GetItemPyData(G2gd.GetPatternTreeItemId(G2frame, \
1048#             PickId, 'Instrument Parameters'))[0]
1049#         prm.write( '            123456789012345678901234567890123456789012345678901234567890        '+'\n')
1050#         prm.write( 'INS   BANK      1                                                               '+'\n')
1051#         prm.write(('INS   HTYPE   %sR                                                              '+'\n')%(Inst['Type'][0]))
1052#         if 'Lam1' in Inst:              #Ka1 & Ka2
1053#             prm.write(('INS  1 ICONS%10.7f%10.7f    0.0000               0.990    0     0.500   '+'\n')%(Inst['Lam1'][0],Inst['Lam2'][0]))
1054#         elif 'Lam' in Inst:             #single wavelength
1055#             prm.write(('INS  1 ICONS%10.7f%10.7f    0.0000               0.990    0     0.500   '+'\n')%(Inst['Lam'][1],0.0))
1056#         prm.write( 'INS  1 IRAD     0                                                               '+'\n')
1057#         prm.write( 'INS  1I HEAD                                                                    '+'\n')
1058#         prm.write( 'INS  1I ITYP    0    0.0000  180.0000         1                                 '+'\n')
1059#         prm.write(('INS  1DETAZM%10.3f                                                          '+'\n')%(Inst['Azimuth'][0]))
1060#         prm.write( 'INS  1PRCF1     3    8   0.00100                                                '+'\n')
1061#         prm.write(('INS  1PRCF11     %15.6g%15.6g%15.6g%15.6g   '+'\n')%(Inst['U'][1],Inst['V'][1],Inst['W'][1],0.0))
1062#         prm.write(('INS  1PRCF12     %15.6g%15.6g%15.6g%15.6g   '+'\n')%(Inst['X'][1],Inst['Y'][1],Inst['SH/L'][1]/2.,Inst['SH/L'][1]/2.))
1063#         prm.close()
1064#         file = open(filename,'w')
1065#         print 'save powder pattern to file: ',filename
1066#         x,y,w,yc,yb,yd = G2frame.PatternTree.GetItemPyData(PickId)[1]
1067#         file.write(powderfile+'\n')
1068#         file.write('Instrument parameter file:'+ospath.split(prmname)[1]+'\n')
1069#         file.write('BANK 1 %d %d CONS %.2f %.2f 0 0 FXYE\n'%(len(x),len(x),\
1070#             100.*x[0],100.*(x[1]-x[0])))
1071#         s = list(np.sqrt(1./np.array(w)))       
1072#         XYW = zip(x,y,s)
1073#         for X,Y,S in XYW:
1074#             file.write("%15.6g %15.6g %15.6g\n" % (100.*X,Y,max(S,1.0)))
1075#         file.close()
1076#         print 'powder pattern file '+filename+' written'
1077       
1078# def powderXyeSave(G2frame,exports,powderfile):
1079#     'Save a powder histogram as a Topas XYE file'
1080#     head,tail = ospath.split(powderfile)
1081#     name,ext = tail.split('.')
1082#     for i,export in enumerate(exports):
1083#         filename = ospath.join(head,name+'-%03d.'%(i)+ext)
1084#         PickId = G2gd.GetPatternTreeItemId(G2frame, G2frame.root, export)
1085#         file = open(filename,'w')
1086#         file.write('#%s\n'%(export))
1087#         print 'save powder pattern to file: ',filename
1088#         x,y,w,yc,yb,yd = G2frame.PatternTree.GetItemPyData(PickId)[1]
1089#         s = list(np.sqrt(1./np.array(w)))       
1090#         XYW = zip(x,y,s)
1091#         for X,Y,W in XYW:
1092#             file.write("%15.6g %15.6g %15.6g\n" % (X,Y,W))
1093#         file.close()
1094#         print 'powder pattern file '+filename+' written'
1095       
1096def PDFSave(G2frame,exports):
1097    'Save a PDF G(r) and S(Q) in column formats'
1098    for export in exports:
1099        PickId = G2gd.GetPatternTreeItemId(G2frame, G2frame.root, export)
1100        SQname = 'S(Q)'+export[4:]
1101        GRname = 'G(R)'+export[4:]
1102        sqfilename = ospath.join(G2frame.dirname,export.replace(' ','_')[5:]+'.sq')
1103        grfilename = ospath.join(G2frame.dirname,export.replace(' ','_')[5:]+'.gr')
1104        sqId = G2gd.GetPatternTreeItemId(G2frame, PickId, SQname)
1105        grId = G2gd.GetPatternTreeItemId(G2frame, PickId, GRname)
1106        sqdata = np.array(G2frame.PatternTree.GetItemPyData(sqId)[1][:2]).T
1107        grdata = np.array(G2frame.PatternTree.GetItemPyData(grId)[1][:2]).T
1108        sqfile = open(sqfilename,'w')
1109        grfile = open(grfilename,'w')
1110        sqfile.write('#T S(Q) %s\n'%(export))
1111        grfile.write('#T G(R) %s\n'%(export))
1112        sqfile.write('#L Q     S(Q)\n')
1113        grfile.write('#L R     G(R)\n')
1114        for q,sq in sqdata:
1115            sqfile.write("%15.6g %15.6g\n" % (q,sq))
1116        sqfile.close()
1117        for r,gr in grdata:
1118            grfile.write("%15.6g %15.6g\n" % (r,gr))
1119        grfile.close()
1120   
1121def PeakListSave(G2frame,file,peaks):
1122    'Save powder peaks to a data file'
1123    print 'save peak list to file: ',G2frame.peaklistfile
1124    if not peaks:
1125        dlg = wx.MessageDialog(G2frame, 'No peaks!', 'Nothing to save!', wx.OK)
1126        try:
1127            result = dlg.ShowModal()
1128        finally:
1129            dlg.Destroy()
1130        return
1131    for peak in peaks:
1132        file.write("%10.4f %12.2f %10.3f %10.3f \n" % \
1133            (peak[0],peak[2],peak[4],peak[6]))
1134    print 'peak list saved'
1135             
1136def IndexPeakListSave(G2frame,peaks):
1137    'Save powder peaks from the indexing list'
1138    file = open(G2frame.peaklistfile,'wa')
1139    print 'save index peak list to file: ',G2frame.peaklistfile
1140    wx.BeginBusyCursor()
1141    try:
1142        if not peaks:
1143            dlg = wx.MessageDialog(G2frame, 'No peaks!', 'Nothing to save!', wx.OK)
1144            try:
1145                result = dlg.ShowModal()
1146            finally:
1147                dlg.Destroy()
1148            return
1149        for peak in peaks:
1150            file.write("%12.6f\n" % (peak[7]))
1151        file.close()
1152    finally:
1153        wx.EndBusyCursor()
1154    print 'index peak list saved'
1155   
1156def SetNewPhase(Name='New Phase',SGData=None,cell=None):
1157    '''Create a new phase dict with default values for various parameters
1158
1159    :param str Name: Name for new Phase
1160
1161    :param dict SGData: space group data from :func:`GSASIIspc:SpcGroup`;
1162      defaults to data for P 1
1163
1164    :param list cell: unit cell parameter list; defaults to
1165      [1.0,1.0,1.0,90.,90,90.,1.]
1166
1167    '''
1168    if SGData is None: SGData = G2spc.SpcGroup('P 1')[1]
1169    if cell is None: cell=[1.0,1.0,1.0,90.,90,90.,1.]
1170    phaseData = {
1171        'ranId':ran.randint(0,sys.maxint),
1172        'General':{
1173            'Name':Name,
1174            'Type':'nuclear',
1175            'AtomPtrs':[3,1,7,9],
1176            'SGData':SGData,
1177            'Cell':[False,]+cell,
1178            'Pawley dmin':1.0,
1179            'Data plot type':'None',
1180            'SH Texture':{
1181                'Order':0,
1182                'Model':'cylindrical',
1183                'Sample omega':[False,0.0],
1184                'Sample chi':[False,0.0],
1185                'Sample phi':[False,0.0],
1186                'SH Coeff':[False,{}],
1187                'SHShow':False,
1188                'PFhkl':[0,0,1],
1189                'PFxyz':[0,0,1],
1190                'PlotType':'Pole figure'}},
1191        'Atoms':[],
1192        'Drawing':{},
1193        'Histograms':{},
1194        'Pawley ref':[],
1195        'RBModels':{},
1196        }
1197    return phaseData
1198       
1199class MultipleChoicesDialog(wx.Dialog):
1200    '''A dialog that offers a series of choices, each with a
1201    title and a wx.Choice widget. Intended to be used Modally.
1202    typical input:
1203
1204        *  choicelist=[ ('a','b','c'), ('test1','test2'),('no choice',)]
1205        *  headinglist = [ 'select a, b or c', 'select 1 of 2', 'No option here']
1206       
1207    selections are placed in self.chosen when OK is pressed
1208    '''
1209    def __init__(self,choicelist,headinglist,
1210                 head='Select options',
1211                 title='Please select from options below',
1212                 parent=None):
1213        self.chosen = []
1214        wx.Dialog.__init__(
1215            self,parent,wx.ID_ANY,head, 
1216            pos=wx.DefaultPosition,style=wx.DEFAULT_DIALOG_STYLE)
1217        panel = wx.Panel(self)
1218        mainSizer = wx.BoxSizer(wx.VERTICAL)
1219        mainSizer.Add((10,10),1)
1220        topLabl = wx.StaticText(panel,wx.ID_ANY,title)
1221        mainSizer.Add(topLabl,0,wx.ALIGN_CENTER_VERTICAL|wx.CENTER,10)
1222        self.ChItems = []
1223        for choice,lbl in zip(choicelist,headinglist):
1224            mainSizer.Add((10,10),1)
1225            self.chosen.append(0)
1226            topLabl = wx.StaticText(panel,wx.ID_ANY,' '+lbl)
1227            mainSizer.Add(topLabl,0,wx.ALIGN_LEFT,10)
1228            self.ChItems.append(wx.Choice(self, wx.ID_ANY, (100, 50), choices = choice))
1229            mainSizer.Add(self.ChItems[-1],0,wx.ALIGN_CENTER,10)
1230
1231        OkBtn = wx.Button(panel,-1,"Ok")
1232        OkBtn.Bind(wx.EVT_BUTTON, self.OnOk)
1233        cancelBtn = wx.Button(panel,-1,"Cancel")
1234        cancelBtn.Bind(wx.EVT_BUTTON, self.OnCancel)
1235        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
1236        btnSizer.Add((20,20),1)
1237        btnSizer.Add(OkBtn)
1238        btnSizer.Add((20,20),1)
1239        btnSizer.Add(cancelBtn)
1240        btnSizer.Add((20,20),1)
1241        mainSizer.Add(btnSizer,0,wx.EXPAND|wx.BOTTOM|wx.TOP, 10)
1242        panel.SetSizer(mainSizer)
1243        panel.Fit()
1244        self.Fit()
1245       
1246    def OnOk(self,event):
1247        parent = self.GetParent()
1248        if parent is not None: parent.Raise()
1249        # save the results from the choice widgets
1250        self.chosen = []
1251        for w in self.ChItems:
1252            self.chosen.append(w.GetSelection())
1253        self.EndModal(wx.ID_OK)             
1254           
1255    def OnCancel(self,event):
1256        parent = self.GetParent()
1257        if parent is not None: parent.Raise()
1258        self.chosen = []
1259        self.EndModal(wx.ID_CANCEL)             
1260           
1261def ExtractFileFromZip(filename, selection=None, confirmread=True,
1262                       confirmoverwrite=True, parent=None,
1263                       multipleselect=False):
1264    '''If the filename is a zip file, extract a file from that
1265    archive.
1266
1267    :param list Selection: used to predefine the name of the file
1268      to be extracted. Filename case and zip directory name are
1269      ignored in selection; the first matching file is used.
1270
1271    :param bool confirmread: if True asks the user to confirm before expanding
1272      the only file in a zip
1273
1274    :param bool confirmoverwrite: if True asks the user to confirm
1275      before overwriting if the extracted file already exists
1276
1277    :param bool multipleselect: if True allows more than one zip
1278      file to be extracted, a list of file(s) is returned.
1279      If only one file is present, do not ask which one, otherwise
1280      offer a list of choices (unless selection is used).
1281   
1282    :returns: the name of the file that has been created or a
1283      list of files (see multipleselect)
1284
1285    If the file is not a zipfile, return the name of the input file.
1286    If the zipfile is empty or no file has been selected, return None
1287    '''
1288    import zipfile # do this now, since we can save startup time by doing this only on need
1289    import shutil
1290    zloc = os.path.split(filename)[0]
1291    if not zipfile.is_zipfile(filename):
1292        #print("not zip")
1293        return filename
1294
1295    z = zipfile.ZipFile(filename,'r')
1296    zinfo = z.infolist()
1297
1298    if len(zinfo) == 0:
1299        #print('Zip has no files!')
1300        zlist = [-1]
1301    if selection:
1302        choices = [os.path.split(i.filename)[1].lower() for i in zinfo]
1303        if selection.lower() in choices:
1304            zlist = [choices.index(selection.lower())]
1305        else:
1306            print('debug: file '+str(selection)+' was not found in '+str(filename))
1307            zlist = [-1]
1308    elif len(zinfo) == 1 and confirmread:
1309        result = wx.ID_NO
1310        dlg = wx.MessageDialog(
1311            parent,
1312            'Is file '+str(zinfo[0].filename)+
1313            ' what you want to extract from '+
1314            str(os.path.split(filename)[1])+'?',
1315            'Confirm file', 
1316            wx.YES_NO | wx.ICON_QUESTION)
1317        try:
1318            result = dlg.ShowModal()
1319        finally:
1320            dlg.Destroy()
1321        if result == wx.ID_NO:
1322            zlist = [-1]
1323        else:
1324            zlist = [0]
1325    elif len(zinfo) == 1:
1326        zlist = [0]
1327    elif multipleselect:
1328        # select one or more from a from list
1329        choices = [i.filename for i in zinfo]
1330        dlg = G2gd.G2MultiChoiceDialog(parent,'Select file(s) to extract from zip file '+str(filename),
1331            'Choose file(s)',choices)
1332        if dlg.ShowModal() == wx.ID_OK:
1333            zlist = dlg.GetSelections()
1334        else:
1335            zlist = []
1336        dlg.Destroy()
1337    else:
1338        # select one from a from list
1339        choices = [i.filename for i in zinfo]
1340        dlg = wx.SingleChoiceDialog(parent,
1341            'Select file to extract from zip file'+str(filename),'Choose file',
1342            choices,)
1343        if dlg.ShowModal() == wx.ID_OK:
1344            zlist = [dlg.GetSelection()]
1345        else:
1346            zlist = [-1]
1347        dlg.Destroy()
1348       
1349    outlist = []
1350    for zindex in zlist:
1351        if zindex >= 0:
1352            efil = os.path.join(zloc, os.path.split(zinfo[zindex].filename)[1])
1353            if os.path.exists(efil) and confirmoverwrite:
1354                result = wx.ID_NO
1355                dlg = wx.MessageDialog(parent,
1356                    'File '+str(efil)+' already exists. OK to overwrite it?',
1357                    'Confirm overwrite',wx.YES_NO | wx.ICON_QUESTION)
1358                try:
1359                    result = dlg.ShowModal()
1360                finally:
1361                    dlg.Destroy()
1362                if result == wx.ID_NO:
1363                    zindex = -1
1364        if zindex >= 0:
1365            # extract the file to the current directory, regardless of it's original path
1366            #z.extract(zinfo[zindex],zloc)
1367            eloc,efil = os.path.split(zinfo[zindex].filename)
1368            outfile = os.path.join(zloc, efil)
1369            fpin = z.open(zinfo[zindex])
1370            fpout = file(outfile, "wb")
1371            shutil.copyfileobj(fpin, fpout)
1372            fpin.close()
1373            fpout.close()
1374            outlist.append(outfile)
1375    z.close()
1376    if multipleselect and len(outlist) >= 1:
1377        return outlist
1378    elif len(outlist) == 1:
1379        return outlist[0]
1380    else:
1381        return None
1382
1383######################################################################
1384# base classes for reading various types of data files
1385#   not used directly, only by subclassing
1386######################################################################
1387E,SGData = G2spc.SpcGroup('P 1') # data structure for default space group
1388P1SGData = SGData
1389class ImportBaseclass(object):
1390    '''Defines a base class for the reading of input files (diffraction
1391    data, coordinates,...). See :ref:`Writing a Import Routine<Import_routines>`
1392    for an explanation on how to use a subclass of this class.
1393    '''
1394    class ImportException(Exception):
1395        '''Defines an Exception that is used when an import routine hits an expected error,
1396        usually in .Reader.
1397
1398        Good practice is that the Reader should define a value in self.errors that
1399        tells the user some information about what is wrong with their file.         
1400        '''
1401        pass
1402
1403    def __init__(self,
1404                 formatName,
1405                 longFormatName=None,
1406                 extensionlist=[],
1407                 strictExtension=False,
1408                 ):
1409        self.formatName = formatName # short string naming file type
1410        if longFormatName: # longer string naming file type
1411            self.longFormatName = longFormatName
1412        else:
1413            self.longFormatName = formatName
1414        # define extensions that are allowed for the file type
1415        # for windows, remove any extensions that are duplicate, as case is ignored
1416        if sys.platform == 'windows' and extensionlist:
1417            extensionlist = list(set([s.lower() for s in extensionlist]))
1418        self.extensionlist = extensionlist
1419        # If strictExtension is True, the file will not be read, unless
1420        # the extension matches one in the extensionlist
1421        self.strictExtension = strictExtension
1422        self.errors = ''
1423        self.warnings = ''
1424        # used for readers that will use multiple passes to read
1425        # more than one data block
1426        self.repeat = False
1427        self.selections = []
1428        self.repeatcount = 0
1429        self.readfilename = '?'
1430        #print 'created',self.__class__
1431
1432    def ReInitialize(self):
1433        'Reinitialize the Reader to initial settings'
1434        self.errors = ''
1435        self.warnings = ''
1436        self.repeat = False
1437        self.repeatcount = 0
1438        self.readfilename = '?'
1439
1440    def BlockSelector(self, ChoiceList, ParentFrame=None,
1441                      title='Select a block',
1442                      size=None, header='Block Selector',
1443                      useCancel=True):
1444        ''' Provide a wx dialog to select a block if the file contains more
1445        than one set of data and one must be selected
1446        '''
1447        if useCancel:
1448            dlg = wx.SingleChoiceDialog(
1449                ParentFrame,title, header,ChoiceList)
1450        else:
1451            dlg = wx.SingleChoiceDialog(
1452                ParentFrame,title, header,ChoiceList,
1453                style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER|wx.OK|wx.CENTRE)
1454        if size: dlg.SetSize(size)
1455        dlg.CenterOnParent()
1456        if dlg.ShowModal() == wx.ID_OK:
1457            sel = dlg.GetSelection()
1458            return sel
1459        else:
1460            return None
1461        dlg.Destroy()
1462
1463    def MultipleBlockSelector(self, ChoiceList, ParentFrame=None,
1464        title='Select a block',size=None, header='Block Selector'):
1465        '''Provide a wx dialog to select a block of data if the
1466        file contains more than one set of data and one must be
1467        selected.
1468
1469        :returns: a list of the selected blocks
1470        '''
1471        dlg = wx.MultiChoiceDialog(ParentFrame,title, header,ChoiceList+['Select all'],
1472            wx.CHOICEDLG_STYLE)
1473        dlg.CenterOnParent()
1474        if size: dlg.SetSize(size)
1475        if dlg.ShowModal() == wx.ID_OK:
1476            sel = dlg.GetSelections()
1477        else:
1478            return []
1479        dlg.Destroy()
1480        selected = []
1481        if len(ChoiceList) in sel:
1482            return range(len(ChoiceList))
1483        else:
1484            return sel
1485        return selected
1486
1487    def MultipleChoicesDialog(self, choicelist, headinglist, ParentFrame=None, **kwargs):
1488        '''A modal dialog that offers a series of choices, each with a title and a wx.Choice
1489        widget. Typical input:
1490       
1491           * choicelist=[ ('a','b','c'), ('test1','test2'),('no choice',)]
1492           
1493           * headinglist = [ 'select a, b or c', 'select 1 of 2', 'No option here']
1494           
1495        optional keyword parameters are: head (window title) and title
1496        returns a list of selected indicies for each choice (or None)
1497        '''
1498        result = None
1499        dlg = MultipleChoicesDialog(choicelist,headinglist,
1500            parent=ParentFrame, **kwargs)         
1501        dlg.CenterOnParent()
1502        if dlg.ShowModal() == wx.ID_OK:
1503            result = dlg.chosen
1504        dlg.Destroy()
1505        return result
1506
1507    def ShowBusy(self):
1508        wx.BeginBusyCursor()
1509        wx.Yield() # make it happen now!
1510
1511    def DoneBusy(self):
1512        wx.EndBusyCursor()
1513        wx.Yield() # make it happen now!
1514       
1515#    def Reader(self, filename, filepointer, ParentFrame=None, **unused):
1516#        '''This method must be supplied in the child class to read the file.
1517#        if the read fails either return False or raise an Exception
1518#        preferably of type ImportException.
1519#        '''
1520#        #start reading
1521#        raise ImportException("Error occurred while...")
1522#        self.errors += "Hint for user on why the error occur
1523#        return False # if an error occurs
1524#        return True # if read OK
1525
1526    def ExtensionValidator(self, filename):
1527        '''This methods checks if the file has the correct extension
1528        Return False if this filename will not be supported by this reader
1529        Return True if the extension matches the list supplied by the reader
1530        Return None if the reader allows un-registered extensions
1531        '''
1532        if filename:
1533            ext = os.path.splitext(filename)[1]
1534            if sys.platform == 'windows': ext = ext.lower()
1535            if ext in self.extensionlist: return True
1536            if self.strictExtension: return False
1537        return None
1538
1539    def ContentsValidator(self, filepointer):
1540        '''This routine will attempt to determine if the file can be read
1541        with the current format.
1542        This will typically be overridden with a method that
1543        takes a quick scan of [some of]
1544        the file contents to do a "sanity" check if the file
1545        appears to match the selected format.
1546        Expected to be called via self.Validator()
1547        '''
1548        #filepointer.seek(0) # rewind the file pointer
1549        return True
1550
1551    def CIFValidator(self, filepointer):
1552        '''A :meth:`ContentsValidator` for use to validate CIF files.
1553        '''
1554        for i,l in enumerate(filepointer):
1555            if i >= 1000: return True
1556            '''Encountered only blank lines or comments in first 1000
1557            lines. This is unlikely, but assume it is CIF anyway, since we are
1558            even less likely to find a file with nothing but hashes and
1559            blank lines'''
1560            line = l.strip()
1561            if len(line) == 0: # ignore blank lines
1562                continue 
1563            elif line.startswith('#'): # ignore comments
1564                continue 
1565            elif line.startswith('data_'): # on the right track, accept this file
1566                return True
1567            else: # found something invalid
1568                self.errors = 'line '+str(i+1)+' contains unexpected data:\n'
1569                self.errors += '  '+str(l)
1570                self.errors += '  Note: a CIF should only have blank lines or comments before'
1571                self.errors += '        a data_ statement begins a block.'
1572                return False 
1573
1574class ImportPhase(ImportBaseclass):
1575    '''Defines a base class for the reading of files with coordinates
1576
1577    Objects constructed that subclass this (in import/G2phase_*.py etc.) will be used
1578    in :meth:`GSASII.GSASII.OnImportPhase`.
1579    See :ref:`Writing a Import Routine<Import_Routines>`
1580    for an explanation on how to use this class.
1581
1582    '''
1583    def __init__(self,formatName,longFormatName=None,extensionlist=[],
1584        strictExtension=False,):
1585        # call parent __init__
1586        ImportBaseclass.__init__(self,formatName,longFormatName,
1587            extensionlist,strictExtension)
1588        self.Phase = None # a phase must be created with G2IO.SetNewPhase in the Reader
1589        self.Constraints = None
1590
1591    def PhaseSelector(self, ChoiceList, ParentFrame=None,
1592        title='Select a phase', size=None,header='Phase Selector'):
1593        ''' Provide a wx dialog to select a phase if the file contains more
1594        than one phase
1595        '''
1596        return self.BlockSelector(ChoiceList,ParentFrame,title,
1597            size,header)
1598
1599class ImportStructFactor(ImportBaseclass):
1600    '''Defines a base class for the reading of files with tables
1601    of structure factors.
1602
1603    Structure factors are read with a call to :meth:`GSASII.GSASII.OnImportSfact`
1604    which in turn calls :meth:`GSASII.GSASII.OnImportGeneric`, which calls
1605    methods :meth:`ExtensionValidator`, :meth:`ContentsValidator` and
1606    :meth:`Reader`.
1607
1608    See :ref:`Writing a Import Routine<Import_Routines>`
1609    for an explanation on how to use import classes in general. The specifics
1610    for reading a structure factor histogram require that
1611    the ``Reader()`` routine in the import
1612    class need to do only a few things: It
1613    should load :attr:`RefDict` item ``'RefList'`` with the reflection list,
1614    and set :attr:`Parameters` with the instrument parameters
1615    (initialized with :meth:`InitParameters` and set with :meth:`UpdateParameters`).
1616    '''
1617    def __init__(self,formatName,longFormatName=None,extensionlist=[],
1618        strictExtension=False,):
1619        ImportBaseclass.__init__(self,formatName,longFormatName,
1620            extensionlist,strictExtension)
1621
1622        # define contents of Structure Factor entry
1623        self.Parameters = []
1624        'self.Parameters is a list with two dicts for data parameter settings'
1625        self.InitParameters()
1626        self.RefDict = {'RefList':[],'FF':[],'Super':0}
1627        self.Banks = []             #for multi bank data (usually TOF)
1628        '''self.RefDict is a dict containing the reflection information, as read from the file.
1629        Item 'RefList' contains the reflection information. See the
1630        :ref:`Single Crystal Reflection Data Structure<XtalRefl_table>`
1631        for the contents of each row. Dict element 'FF'
1632        contains the form factor values for each element type; if this entry
1633        is left as initialized (an empty list) it will be initialized as needed later.
1634        '''
1635    def ReInitialize(self):
1636        'Reinitialize the Reader to initial settings'
1637        ImportBaseclass.ReInitialize(self)
1638        self.InitParameters()
1639        self.Banks = []             #for multi bank data (usually TOF)
1640        self.RefDict = {'RefList':[],'FF':[],'Super':0}
1641       
1642    def InitParameters(self):
1643        'initialize the instrument parameters structure'
1644        Lambda = 0.70926
1645        HistType = 'SXC'
1646        self.Parameters = [{'Type':[HistType,HistType], # create the structure
1647                            'Lam':[Lambda,Lambda]
1648                            }, {}]
1649        'Parameters is a list with two dicts for data parameter settings'
1650
1651    def UpdateParameters(self,Type=None,Wave=None):
1652        'Revise the instrument parameters'
1653        if Type is not None:
1654            self.Parameters[0]['Type'] = [Type,Type]
1655        if Wave is not None:
1656            self.Parameters[0]['Lam'] = [Wave,Wave]           
1657                       
1658######################################################################
1659class ImportPowderData(ImportBaseclass):
1660    '''Defines a base class for the reading of files with powder data.
1661
1662    Objects constructed that subclass this (in import/G2pwd_*.py etc.) will be used
1663    in :meth:`GSASII.GSASII.OnImportPowder`.
1664    See :ref:`Writing a Import Routine<Import_Routines>`
1665    for an explanation on how to use this class.
1666    '''
1667    def __init__(self,
1668                 formatName,
1669                 longFormatName=None,
1670                 extensionlist=[],
1671                 strictExtension=False,
1672                 ):
1673        ImportBaseclass.__init__(self,formatName,
1674                                            longFormatName,
1675                                            extensionlist,
1676                                            strictExtension)
1677        self.clockWd = None  # used in TOF
1678        self.ReInitialize()
1679       
1680    def ReInitialize(self):
1681        'Reinitialize the Reader to initial settings'
1682        ImportBaseclass.ReInitialize(self)
1683        self.powderentry = ['',None,None] #  (filename,Pos,Bank)
1684        self.powderdata = [] # Powder dataset
1685        '''A powder data set is a list with items [x,y,w,yc,yb,yd]:
1686                np.array(x), # x-axis values
1687                np.array(y), # powder pattern intensities
1688                np.array(w), # 1/sig(intensity)^2 values (weights)
1689                np.array(yc), # calc. intensities (zero)
1690                np.array(yb), # calc. background (zero)
1691                np.array(yd), # obs-calc profiles
1692        '''                           
1693        self.comments = []
1694        self.idstring = ''
1695        self.Sample = G2pdG.SetDefaultSample()
1696        self.GSAS = None     # used in TOF
1697        self.repeat_instparm = True # Should a parm file be
1698        #                             used for multiple histograms?
1699        self.instparm = None # name hint from file of instparm to use
1700        self.instfile = '' # full path name to instrument parameter file
1701        self.instbank = '' # inst parm bank number
1702        self.instmsg = ''  # a label that gets printed to show
1703                           # where instrument parameters are from
1704        self.numbanks = 1
1705        self.instdict = {} # place items here that will be transferred to the instrument parameters
1706######################################################################
1707class ImportSmallAngleData(ImportBaseclass):
1708    '''Defines a base class for the reading of files with small angle data.
1709    See :ref:`Writing a Import Routine<Import_Routines>`
1710    for an explanation on how to use this class.
1711    '''
1712    def __init__(self,formatName,longFormatName=None,extensionlist=[],
1713        strictExtension=False,):
1714           
1715        ImportBaseclass.__init__(self,formatName,longFormatName,extensionlist,
1716            strictExtension)
1717        self.ReInitialize()
1718       
1719    def ReInitialize(self):
1720        'Reinitialize the Reader to initial settings'
1721        ImportBaseclass.ReInitialize(self)
1722        self.smallangleentry = ['',None,None] #  (filename,Pos,Bank)
1723        self.smallangledata = [] # SASD dataset
1724        '''A small angle data set is a list with items [x,y,w,yc,yd]:
1725                np.array(x), # x-axis values
1726                np.array(y), # powder pattern intensities
1727                np.array(w), # 1/sig(intensity)^2 values (weights)
1728                np.array(yc), # calc. intensities (zero)
1729                np.array(yd), # obs-calc profiles
1730                np.array(yb), # preset bkg
1731        '''                           
1732        self.comments = []
1733        self.idstring = ''
1734        self.Sample = G2pdG.SetDefaultSample()
1735        self.GSAS = None     # used in TOF
1736        self.clockWd = None  # used in TOF
1737        self.numbanks = 1
1738        self.instdict = {} # place items here that will be transferred to the instrument parameters
1739######################################################################
1740class ExportBaseclass(object):
1741    '''Defines a base class for the exporting of GSAS-II results.
1742
1743    This class is subclassed in the various exports/G2export_*.py files. Those files
1744    are imported in :meth:`GSASII.GSASII._init_Exports` which defines the
1745    appropriate menu items for each one and the .Exporter method is called
1746    directly from the menu item.
1747   
1748    '''
1749    def __init__(self,
1750                 G2frame,
1751                 formatName,
1752                 extension,
1753                 longFormatName=None,
1754                 ):
1755        self.G2frame = G2frame
1756        self.formatName = formatName # short string naming file type
1757        self.extension = extension
1758        if longFormatName: # longer string naming file type
1759            self.longFormatName = longFormatName
1760        else:
1761            self.longFormatName = formatName
1762        self.OverallParms = {}
1763        self.Phases = {}
1764        self.Histograms = {}
1765        self.powderDict = {}
1766        self.xtalDict = {}
1767        self.parmDict = {}
1768        self.sigDict = {}
1769        # updated in InitExport:
1770        self.currentExportType = None # type of export that has been requested
1771        # updated in ExportSelect (when used):
1772        self.phasenam = None # a list of selected phases
1773        self.histnam = None # a list of selected histograms
1774        self.filename = None # name of file to be written (single export) or template (multiple files)
1775        self.dirname = '' # name of directory where file(s) will be written
1776        self.fullpath = '' # name of file being written -- full path
1777       
1778        # items that should be defined in a subclass of this class
1779        self.exporttype = []  # defines the type(s) of exports that the class can handle.
1780        # The following types are defined: 'project', "phase", "powder", "single"
1781        self.multiple = False # set as True if the class can export multiple phases or histograms
1782        # self.multiple is ignored for "project" exports
1783
1784    def InitExport(self,event):
1785        '''Determines the type of menu that called the Exporter and
1786        misc initialization.
1787        '''
1788        self.filename = None # name of file to be written (single export)
1789        self.dirname = '' # name of file to be written (multiple export)
1790        if event:
1791            self.currentExportType = self.G2frame.ExportLookup.get(event.Id)
1792
1793    def MakePWDRfilename(self,hist):
1794        '''Make a filename root (no extension) from a PWDR histogram name
1795
1796        :param str hist: the histogram name in data tree (starts with "PWDR ")
1797        '''
1798        file0 = ''
1799        file1 = hist[5:]
1800        # replace repeated blanks
1801        while file1 != file0:
1802            file0 = file1
1803            file1 = file0.replace('  ',' ').strip()
1804        file0 = file1.replace('Azm= ','A')
1805        # if angle has unneeded decimal places on aziumuth, remove them
1806        if file0[-3:] == '.00': file0 = file0[:-3]
1807        file0 = file0.replace('.','_')
1808        file0 = file0.replace(' ','_')
1809        return file0
1810
1811    def ExportSelect(self,AskFile='ask'):
1812        '''Selects histograms or phases when needed. Sets a default file name when
1813        requested in self.filename; always sets a default directory in self.dirname.
1814
1815        :param bool AskFile: Determines how this routine processes getting a
1816          location to store the current export(s).
1817         
1818          * if AskFile is 'ask' (default option), get the name of the file to be written;
1819            self.filename and self.dirname are always set. In the case where
1820            multiple files must be generated, the export routine should do this
1821            based on self.filename as a template.
1822          * if AskFile is 'dir', get the name of the directory to be used;
1823            self.filename is not used, but self.dirname is always set. The export routine
1824            will always generate the file name.
1825          * if AskFile is 'single', get only the name of the directory to be used when
1826            multiple items will be written (as multiple files) are used
1827            *or* a complete file name is requested when a single file
1828            name is selected. self.dirname is always set and self.filename used
1829            only when a single file is selected.
1830          * if AskFile is 'default', creates a name of the file to be used from
1831            the name of the project (.gpx) file. If the project has not been saved,
1832            then the name of file is requested.
1833            self.filename and self.dirname are always set. In the case where
1834            multiple file names must be generated, the export routine should do this
1835            based on self.filename.
1836          * if AskFile is 'default-dir', sets self.dirname from the project (.gpx)
1837            file. If the project has not been saved, then a directory is requested.
1838            self.filename is not used.
1839
1840        :returns: True in case of an error
1841        '''
1842       
1843        numselected = 1
1844        if self.currentExportType == 'phase':
1845            if len(self.Phases) == 0:
1846                self.G2frame.ErrorDialog(
1847                    'Empty project',
1848                    'Project does not contain any phases.')
1849                return True
1850            elif len(self.Phases) == 1:
1851                self.phasenam = self.Phases.keys()
1852            elif self.multiple: 
1853                choices = sorted(self.Phases.keys())
1854                phasenum = G2gd.ItemSelector(choices,self.G2frame,multiple=True)
1855                if phasenum is None: return True
1856                self.phasenam = [choices[i] for i in phasenum]
1857                if not self.phasenam: return True
1858                numselected = len(self.phasenam)
1859            else:
1860                choices = sorted(self.Phases.keys())
1861                phasenum = G2gd.ItemSelector(choices,self.G2frame)
1862                if phasenum is None: return True
1863                self.phasenam = [choices[phasenum]]
1864                numselected = len(self.phasenam)
1865        elif self.currentExportType == 'single':
1866            if len(self.xtalDict) == 0:
1867                self.G2frame.ErrorDialog(
1868                    'Empty project',
1869                    'Project does not contain any single crystal data.')
1870                return True
1871            elif len(self.xtalDict) == 1:
1872                self.histnam = self.xtalDict.values()
1873            elif self.multiple:
1874                choices = sorted(self.xtalDict.values())
1875                hnum = G2gd.ItemSelector(choices,self.G2frame,multiple=True)
1876                if not hnum: return True
1877                self.histnam = [choices[i] for i in hnum]
1878                numselected = len(self.histnam)
1879            else:
1880                choices = sorted(self.xtalDict.values())
1881                hnum = G2gd.ItemSelector(choices,self.G2frame)
1882                if hnum is None: return True
1883                self.histnam = [choices[hnum]]
1884                numselected = len(self.histnam)
1885        elif self.currentExportType == 'powder':
1886            if len(self.powderDict) == 0:
1887                self.G2frame.ErrorDialog(
1888                    'Empty project',
1889                    'Project does not contain any powder data.')
1890                return True
1891            elif len(self.powderDict) == 1:
1892                self.histnam = self.powderDict.values()
1893            elif self.multiple:
1894                choices = sorted(self.powderDict.values())
1895                hnum = G2gd.ItemSelector(choices,self.G2frame,multiple=True)
1896                if not hnum: return True
1897                self.histnam = [choices[i] for i in hnum]
1898                numselected = len(self.histnam)
1899            else:
1900                choices = sorted(self.powderDict.values())
1901                hnum = G2gd.ItemSelector(choices,self.G2frame)
1902                if hnum is None: return True
1903                self.histnam = [choices[hnum]]
1904                numselected = len(self.histnam)
1905        elif self.currentExportType == 'image':
1906            if len(self.Histograms) == 0:
1907                self.G2frame.ErrorDialog(
1908                    'Empty project',
1909                    'Project does not contain any images.')
1910                return True
1911            elif len(self.Histograms) == 1:
1912                self.histnam = self.Histograms.keys()
1913            else:
1914                choices = sorted(self.Histograms.keys())
1915                hnum = G2gd.ItemSelector(choices,self.G2frame,multiple=self.multiple)
1916                if self.multiple:
1917                    if not hnum: return True
1918                    self.histnam = [choices[i] for i in hnum]
1919                else:
1920                    if hnum is None: return True
1921                    self.histnam = [choices[hnum]]
1922                numselected = len(self.histnam)
1923        if self.currentExportType == 'map':
1924            # search for phases with maps
1925            mapPhases = []
1926            choices = []
1927            for phasenam in sorted(self.Phases):
1928                phasedict = self.Phases[phasenam] # pointer to current phase info           
1929                if len(phasedict['General']['Map'].get('rho',[])):
1930                    mapPhases.append(phasenam)
1931                    if phasedict['General']['Map'].get('Flip'):
1932                        choices.append('Charge flip map: '+str(phasenam))
1933                    elif phasedict['General']['Map'].get('MapType'):
1934                        choices.append(
1935                            str(phasedict['General']['Map'].get('MapType'))
1936                            + ' map: ' + str(phasenam))
1937                    else:
1938                        choices.append('unknown map: '+str(phasenam))
1939            # select a map if needed
1940            if len(mapPhases) == 0:
1941                self.G2frame.ErrorDialog(
1942                    'Empty project',
1943                    'Project does not contain any maps.')
1944                return True
1945            elif len(mapPhases) == 1:
1946                self.phasenam = mapPhases
1947            else: 
1948                phasenum = G2gd.ItemSelector(choices,self.G2frame,multiple=self.multiple)
1949                if self.multiple:
1950                    if not phasenum: return True
1951                    self.phasenam = [mapPhases[i] for i in phasenum]
1952                else:
1953                    if phasenum is None: return True
1954                    self.phasenam = [mapPhases[phasenum]]
1955            numselected = len(self.phasenam)
1956
1957        # items selected, now set self.dirname and usually self.filename
1958        if AskFile == 'ask' or (AskFile == 'single' and numselected == 1) or (
1959            AskFile == 'default' and not self.G2frame.GSASprojectfile
1960            ):
1961            filename = self.askSaveFile()
1962            if not filename: return True
1963            self.dirname,self.filename = os.path.split(filename)
1964        elif AskFile == 'dir' or AskFile == 'single' or (
1965            AskFile == 'default-dir' and not self.G2frame.GSASprojectfile
1966            ):
1967            self.dirname = self.askSaveDirectory()
1968            if not self.dirname: return True
1969        elif AskFile == 'default-dir' or AskFile == 'default':
1970            self.dirname,self.filename = os.path.split(
1971                os.path.splitext(self.G2frame.GSASprojectfile)[0] + self.extension
1972                )
1973        else:
1974            raise Exception('This should not happen!')
1975       
1976    def loadParmDict(self):
1977        '''Load the GSAS-II refinable parameters from the tree into a dict (self.parmDict). Update
1978        refined values to those from the last cycle and set the uncertainties for the
1979        refined parameters in another dict (self.sigDict).
1980
1981        Expands the parm & sig dicts to include values derived from constraints.
1982        '''
1983        self.parmDict = {}
1984        self.sigDict = {}
1985        rigidbodyDict = {}
1986        covDict = {}
1987        consDict = {}
1988        Histograms,Phases = self.G2frame.GetUsedHistogramsAndPhasesfromTree()
1989        if self.G2frame.PatternTree.IsEmpty(): return # nothing to do
1990        item, cookie = self.G2frame.PatternTree.GetFirstChild(self.G2frame.root)
1991        while item:
1992            name = self.G2frame.PatternTree.GetItemText(item)
1993            if name == 'Rigid bodies':
1994                 rigidbodyDict = self.G2frame.PatternTree.GetItemPyData(item)
1995            elif name == 'Covariance':
1996                 covDict = self.G2frame.PatternTree.GetItemPyData(item)
1997            elif name == 'Constraints':
1998                 consDict = self.G2frame.PatternTree.GetItemPyData(item)
1999            item, cookie = self.G2frame.PatternTree.GetNextChild(self.G2frame.root, cookie)
2000        rbVary,rbDict =  G2stIO.GetRigidBodyModels(rigidbodyDict,Print=False)
2001        self.parmDict.update(rbDict)
2002        rbIds = rigidbodyDict.get('RBIds',{'Vector':[],'Residue':[]})
2003        Natoms,atomIndx,phaseVary,phaseDict,pawleyLookup,FFtables,BLtables =  G2stIO.GetPhaseData(
2004            Phases,RestraintDict=None,rbIds=rbIds,Print=False)
2005        self.parmDict.update(phaseDict)
2006        hapVary,hapDict,controlDict =  G2stIO.GetHistogramPhaseData(
2007            Phases,Histograms,Print=False,resetRefList=False)
2008        self.parmDict.update(hapDict)
2009        histVary,histDict,controlDict =  G2stIO.GetHistogramData(Histograms,Print=False)
2010        self.parmDict.update(histDict)
2011        self.parmDict.update(zip(
2012            covDict.get('varyList',[]),
2013            covDict.get('variables',[])))
2014        self.sigDict = dict(zip(
2015            covDict.get('varyList',[]),
2016            covDict.get('sig',[])))
2017        # expand to include constraints: first compile a list of constraints
2018        constList = []
2019        for item in consDict:
2020            if item.startswith('_'): continue
2021            constList += consDict[item]
2022        # now process the constraints
2023        G2mv.InitVars()
2024        constDict,fixedList,ignored = G2stIO.ProcessConstraints(constList)
2025        varyList = covDict.get('varyListStart')
2026        if varyList is None and len(constDict) == 0:
2027            # no constraints can use varyList
2028            varyList = covDict.get('varyList')
2029        elif varyList is None:
2030            # old GPX file from before pre-constraint varyList is saved
2031            print ' *** Old refinement: Please use Calculate/Refine to redo  ***'
2032            raise Exception(' *** Export aborted ***')
2033        else:
2034            varyList = list(varyList)
2035        try:
2036            groups,parmlist = G2mv.GroupConstraints(constDict)
2037            G2mv.GenerateConstraints(groups,parmlist,varyList,constDict,fixedList,self.parmDict)
2038        except:
2039            # this really should not happen
2040            print ' *** ERROR - constraints are internally inconsistent ***'
2041            errmsg, warnmsg = G2mv.CheckConstraints(varyList,constDict,fixedList)
2042            print 'Errors',errmsg
2043            if warnmsg: print 'Warnings',warnmsg
2044            raise Exception(' *** CIF creation aborted ***')
2045        # add the constrained values to the parameter dictionary
2046        G2mv.Dict2Map(self.parmDict,varyList)
2047        # and add their uncertainties into the esd dictionary (sigDict)
2048        if covDict.get('covMatrix') is not None:
2049            self.sigDict.update(G2mv.ComputeDepESD(covDict['covMatrix'],covDict['varyList'],self.parmDict))
2050
2051    def loadTree(self):
2052        '''Load the contents of the data tree into a set of dicts
2053        (self.OverallParms, self.Phases and self.Histogram as well as self.powderDict
2054        & self.xtalDict)
2055       
2056        * The childrenless data tree items are overall parameters/controls for the
2057          entire project and are placed in self.OverallParms
2058        * Phase items are placed in self.Phases
2059        * Data items are placed in self.Histogram. The key for these data items
2060          begin with a keyword, such as PWDR, IMG, HKLF,... that identifies the data type.
2061        '''
2062        self.OverallParms = {}
2063        self.powderDict = {}
2064        self.xtalDict = {}
2065        self.Phases = {}
2066        self.Histograms = {}
2067        if self.G2frame.PatternTree.IsEmpty(): return # nothing to do
2068        histType = None       
2069        if self.currentExportType == 'phase':
2070            # if exporting phases load them here
2071            sub = G2gd.GetPatternTreeItemId(self.G2frame,self.G2frame.root,'Phases')
2072            if not sub:
2073                print 'no phases found'
2074                return True
2075            item, cookie = self.G2frame.PatternTree.GetFirstChild(sub)
2076            while item:
2077                phaseName = self.G2frame.PatternTree.GetItemText(item)
2078                self.Phases[phaseName] =  self.G2frame.PatternTree.GetItemPyData(item)
2079                item, cookie = self.G2frame.PatternTree.GetNextChild(sub, cookie)
2080            return
2081        elif self.currentExportType == 'single':
2082            histType = 'HKLF'
2083        elif self.currentExportType == 'powder':
2084            histType = 'PWDR'
2085        elif self.currentExportType == 'image':
2086            histType = 'IMG'
2087
2088        if histType: # Loading just one kind of tree entry
2089            item, cookie = self.G2frame.PatternTree.GetFirstChild(self.G2frame.root)
2090            while item:
2091                name = self.G2frame.PatternTree.GetItemText(item)
2092                if name.startswith(histType):
2093                    if self.Histograms.get(name): # there is already an item with this name
2094                        print('Histogram name '+str(name)+' is repeated. Renaming')
2095                        if name[-1] == '9':
2096                            name = name[:-1] + '10'
2097                        elif name[-1] in '012345678':
2098                            name = name[:-1] + str(int(name[-1])+1)
2099                        else:                           
2100                            name += '-1'
2101                    self.Histograms[name] = {}
2102                    # the main info goes into Data, but the 0th
2103                    # element contains refinement results, carry
2104                    # that over too now.
2105                    self.Histograms[name]['Data'] = self.G2frame.PatternTree.GetItemPyData(item)[1]
2106                    self.Histograms[name][0] = self.G2frame.PatternTree.GetItemPyData(item)[0]
2107                    item2, cookie2 = self.G2frame.PatternTree.GetFirstChild(item)
2108                    while item2: 
2109                        child = self.G2frame.PatternTree.GetItemText(item2)
2110                        self.Histograms[name][child] = self.G2frame.PatternTree.GetItemPyData(item2)
2111                        item2, cookie2 = self.G2frame.PatternTree.GetNextChild(item, cookie2)
2112                item, cookie = self.G2frame.PatternTree.GetNextChild(self.G2frame.root, cookie)
2113            # index powder and single crystal histograms by number
2114            for hist in self.Histograms:
2115                if hist.startswith("PWDR"): 
2116                    d = self.powderDict
2117                elif hist.startswith("HKLF"): 
2118                    d = self.xtalDict
2119                else:
2120                    return                   
2121                i = self.Histograms[hist].get('hId')
2122                if i is None and not d.keys():
2123                    i = 0
2124                elif i is None or i in d.keys():
2125                    i = max(d.keys())+1
2126                d[i] = hist
2127            return
2128        # else standard load: using all interlinked phases and histograms
2129        self.Histograms,self.Phases = self.G2frame.GetUsedHistogramsAndPhasesfromTree()
2130        item, cookie = self.G2frame.PatternTree.GetFirstChild(self.G2frame.root)
2131        while item:
2132            name = self.G2frame.PatternTree.GetItemText(item)
2133            item2, cookie2 = self.G2frame.PatternTree.GetFirstChild(item)
2134            if not item2: 
2135                self.OverallParms[name] = self.G2frame.PatternTree.GetItemPyData(item)
2136            item, cookie = self.G2frame.PatternTree.GetNextChild(self.G2frame.root, cookie)
2137        # index powder and single crystal histograms
2138        for hist in self.Histograms:
2139            i = self.Histograms[hist]['hId']
2140            if hist.startswith("PWDR"): 
2141                self.powderDict[i] = hist
2142            elif hist.startswith("HKLF"): 
2143                self.xtalDict[i] = hist
2144
2145    def dumpTree(self,mode='type'):
2146        '''Print out information on the data tree dicts loaded in loadTree
2147        '''
2148        print '\nOverall'
2149        if mode == 'type':
2150            def Show(arg): return type(arg)
2151        else:
2152            def Show(arg): return arg
2153        for key in self.OverallParms:
2154            print '  ',key,Show(self.OverallParms[key])
2155        print 'Phases'
2156        for key1 in self.Phases:
2157            print '    ',key1,Show(self.Phases[key1])
2158        print 'Histogram'
2159        for key1 in self.Histograms:
2160            print '    ',key1,Show(self.Histograms[key1])
2161            for key2 in self.Histograms[key1]:
2162                print '      ',key2,Show(self.Histograms[key1][key2])
2163
2164    def defaultSaveFile(self):
2165        return os.path.abspath(
2166            os.path.splitext(self.G2frame.GSASprojectfile
2167                             )[0]+self.extension)
2168       
2169    def askSaveFile(self):
2170        '''Ask the user to supply a file name
2171
2172        :returns: a file name (str) or None if Cancel is pressed
2173        '''
2174        defnam = os.path.splitext(
2175            os.path.split(self.G2frame.GSASprojectfile)[1]
2176            )[0]+self.extension
2177        dlg = wx.FileDialog(
2178            self.G2frame, 'Input name for file to write', '.', defnam,
2179            self.longFormatName+' (*'+self.extension+')|*'+self.extension,
2180            wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT|wx.CHANGE_DIR)
2181        dlg.CenterOnParent()
2182        try:
2183            if dlg.ShowModal() == wx.ID_OK:
2184                filename = dlg.GetPath()
2185                # make sure extension is correct
2186                filename = os.path.splitext(filename)[0]+self.extension
2187            else:
2188                filename = None
2189        finally:
2190            dlg.Destroy()
2191        return filename
2192
2193    def askSaveDirectory(self):
2194        '''Ask the user to supply a directory name. Path name is used as the
2195        starting point for the next export path search.
2196
2197        :returns: a directory name (str) or None if Cancel is pressed
2198        '''
2199        if self.G2frame.exportDir:
2200            startdir = self.G2frame.exportDir
2201        elif self.G2frame.GSASprojectfile:
2202            startdir = os.path.split(self.G2frame.GSASprojectfile)[0]
2203        elif self.G2frame.dirname:
2204            startdir = self.G2frame.dirname
2205        else:
2206            startdir = ''
2207        dlg = wx.DirDialog(
2208            self.G2frame, 'Input directory where file(s) will be written', startdir,
2209            wx.DD_DEFAULT_STYLE)
2210        dlg.CenterOnParent()
2211        try:
2212            if dlg.ShowModal() == wx.ID_OK:
2213                filename = dlg.GetPath()
2214                self.G2frame.exportDir = filename
2215            else:
2216                filename = None
2217        finally:
2218            dlg.Destroy()
2219        return filename
2220
2221    # Tools for file writing.
2222    def OpenFile(self,fil=None,mode='w'):
2223        '''Open the output file
2224
2225        :param str fil: The name of the file to open. If None (default)
2226          the name defaults to self.dirname + self.filename.
2227          If an extension is supplied, it is not overridded,
2228          but if not, the default extension is used.
2229        :returns: the file object opened by the routine which is also
2230          saved as self.fp
2231        '''
2232        if not fil:
2233            if not os.path.splitext(self.filename)[1]:
2234                self.filename += self.extension
2235            fil = os.path.join(self.dirname,self.filename)
2236        self.fullpath = fil
2237        self.fp = open(fil,mode)
2238        return self.fp
2239
2240    def Write(self,line):
2241        '''write a line of output, attaching a line-end character
2242
2243        :param str line: the text to be written.
2244        '''
2245        self.fp.write(line+'\n')
2246    def CloseFile(self,fp=None):
2247        '''Close a file opened in OpenFile
2248
2249        :param file fp: the file object to be closed. If None (default)
2250          file object self.fp is closed.
2251        '''
2252        if fp is None:
2253            fp = self.fp
2254            self.fp = None
2255        fp.close()
2256    # Tools to pull information out of the data arrays
2257    def GetCell(self,phasenam):
2258        """Gets the unit cell parameters and their s.u.'s for a selected phase
2259
2260        :param str phasenam: the name for the selected phase
2261        :returns: `cellList,cellSig` where each is a 7 element list corresponding
2262          to a, b, c, alpha, beta, gamma, volume where `cellList` has the
2263          cell values and `cellSig` has their uncertainties.
2264        """
2265        phasedict = self.Phases[phasenam] # pointer to current phase info
2266        try:
2267            pfx = str(phasedict['pId'])+'::'
2268            A,sigA = G2stIO.cellFill(pfx,phasedict['General']['SGData'],self.parmDict,self.sigDict)
2269            cellSig = G2stIO.getCellEsd(pfx,
2270                                        phasedict['General']['SGData'],A,
2271                                        self.OverallParms['Covariance'])  # returns 7 vals, includes sigVol
2272            cellList = G2lat.A2cell(A) + (G2lat.calc_V(A),)
2273            return cellList,cellSig
2274        except KeyError:
2275            cell = phasedict['General']['Cell'][1:]
2276            return cell,7*[0]
2277   
2278    def GetAtoms(self,phasenam):
2279        """Gets the atoms associated with a phase. Can be used with standard
2280        or macromolecular phases
2281
2282        :param str phasenam: the name for the selected phase
2283        :returns: a list of items for eac atom where each item is a list containing:
2284          label, typ, mult, xyz, and td, where
2285
2286          * label and typ are the atom label and the scattering factor type (str)
2287          * mult is the site multiplicity (int)
2288          * xyz is contains a list with four pairs of numbers:
2289            x, y, z and fractional occupancy and
2290            their standard uncertainty (or a negative value)
2291          * td is contains a list with either one or six pairs of numbers:
2292            if one number it is U\ :sub:`iso` and with six numbers it is
2293            U\ :sub:`11`, U\ :sub:`22`, U\ :sub:`33`, U\ :sub:`12`, U\ :sub:`13` & U\ :sub:`23`
2294            paired with their standard uncertainty (or a negative value)
2295        """
2296        phasedict = self.Phases[phasenam] # pointer to current phase info           
2297        cx,ct,cs,cia = phasedict['General']['AtomPtrs']
2298        cfrac = cx+3
2299        fpfx = str(phasedict['pId'])+'::Afrac:'       
2300        atomslist = []
2301        for i,at in enumerate(phasedict['Atoms']):
2302            if phasedict['General']['Type'] == 'macromolecular':
2303                label = '%s_%s_%s_%s'%(at[ct-1],at[ct-3],at[ct-4],at[ct-2])
2304            else:
2305                label = at[ct-1]
2306            fval = self.parmDict.get(fpfx+str(i),at[cfrac])
2307            fsig = self.sigDict.get(fpfx+str(i),-0.009)
2308            mult = at[cs+1]
2309            typ = at[ct]
2310            xyz = []
2311            for j,v in enumerate(('x','y','z')):
2312                val = at[cx+j]
2313                pfx = str(phasedict['pId'])+'::dA'+v+':'+str(i)
2314                sig = self.sigDict.get(pfx,-0.000009)
2315                xyz.append((val,sig))
2316            xyz.append((fval,fsig))
2317            td = []
2318            if at[cia] == 'I':
2319                pfx = str(phasedict['pId'])+'::AUiso:'+str(i)
2320                val = self.parmDict.get(pfx,at[cia+1])
2321                sig = self.sigDict.get(pfx,-0.0009)
2322                td.append((val,sig))
2323            else:
2324                for i,var in enumerate(('AU11','AU22','AU33','AU12','AU13','AU23')):
2325                    pfx = str(phasedict['pId'])+'::'+var+':'+str(i)
2326                    val = self.parmDict.get(pfx,at[cia+2+i])
2327                    sig = self.sigDict.get(pfx,-0.0009)
2328                    td.append((val,sig))
2329            atomslist.append((label,typ,mult,xyz,td))
2330        return atomslist
2331######################################################################
2332
2333def ReadCIF(URLorFile):
2334    '''Open a CIF, which may be specified as a file name or as a URL using PyCifRW
2335    (from James Hester).
2336    The open routine gets confused with DOS names that begin with a letter and colon
2337    "C:\dir\" so this routine will try to open the passed name as a file and if that
2338    fails, try it as a URL
2339
2340    :param str URLorFile: string containing a URL or a file name. Code will try first
2341      to open it as a file and then as a URL.
2342
2343    :returns: a PyCifRW CIF object.
2344    '''
2345    import CifFile as cif # PyCifRW from James Hester
2346
2347    # alternate approach:
2348    #import urllib
2349    #ciffile = 'file:'+urllib.pathname2url(filename)
2350   
2351    try:
2352        fp = open(URLorFile,'r')
2353        cf = cif.ReadCif(fp)
2354        fp.close()
2355        return cf
2356    except IOError:
2357        return cif.ReadCif(URLorFile)
2358
2359if __name__ == '__main__':
2360    app = wx.PySimpleApp()
2361    frm = wx.Frame(None) # create a frame
2362    frm.Show(True)
2363    filename = '/tmp/notzip.zip'
2364    filename = '/tmp/all.zip'
2365    #filename = '/tmp/11bmb_7652.zip'
2366   
2367    #selection=None, confirmoverwrite=True, parent=None
2368    #print ExtractFileFromZip(filename, selection='11bmb_7652.fxye',parent=frm)
2369    print ExtractFileFromZip(filename,multipleselect=True)
2370                             #confirmread=False, confirmoverwrite=False)
2371
2372    # choicelist=[ ('a','b','c'),
2373    #              ('test1','test2'),('no choice',)]
2374    # titles = [ 'a, b or c', 'tests', 'No option here']
2375    # dlg = MultipleChoicesDialog(
2376    #     choicelist,titles,
2377    #     parent=frm)
2378    # if dlg.ShowModal() == wx.ID_OK:
2379    #     print 'Got OK'
Note: See TracBrowser for help on using the repository browser.