def Reader(self, filename, ParentFrame=None, **unused):
        'Read a csv file'
        x = []
        y = []
        w = []
        fp = open(filename, 'r')
        for i, S in enumerate(fp):
            vals = S.replace(',', ' ').replace(';', ' ').split()
            if len(vals) < 2 and i > 0:
                print('Line ' + str(i + 1) + ' cannot be read:\n\t' + S)
                continue
            try:
                x.append(float(vals[0]))
                f = float(vals[1])
                if f <= 0.0:
                    y.append(0.0)
                    w.append(0.0)
                elif len(vals) == 3:
                    y.append(float(vals[1]))
                    w.append(1.0 / float(vals[2])**2)
                else:
                    y.append(float(vals[1]))
                    w.append(1.0 / float(vals[1]))
                err = False
            except ValueError:
                err = True
                msg = 'Error parsing number in line ' + str(i + 1)
            except:
                err = True
                msg = 'Error in line ' + str(i + 1)
            if err and i > 0:
                if GSASIIpath.GetConfigValue('debug'):
                    print(msg)
                    print(S.strip())
                break
        fp.close()
        N = len(x)
        self.powderdata = [
            np.array(x),  # x-axis values
            np.array(y),  # powder pattern intensities
            np.array(w),  # 1/sig(intensity)^2 values (weights)
            np.zeros(N),  # calc. intensities (zero)
            np.zeros(N),  # calc. background (zero)
            np.zeros(N),  # obs-calc profiles
        ]
        self.powderentry[0] = filename
        #self.powderentry[1] = pos # bank offset (N/A here)
        #self.powderentry[2] = 1 # xye file only has one bank
        self.idstring = ospath.basename(filename)
        # scan comments for temperature
        Temperature = 300
        for S in self.comments:
            if 'Temp' in S.split('=')[0]:
                try:
                    Temperature = float(S.split('=')[1])
                except:
                    pass
        self.Sample['Temperature'] = Temperature

        return True
Пример #2
0
def GetColumnMetadata(reader):
    '''Add metadata to an image from a column-type metadata file
    using :func:`readColMetadata`
    
    :param reader: a reader object from reading an image
    
    '''
    if not GSASIIpath.GetConfigValue('Column_Metadata_directory'): return
    parParms = readColMetadata(reader.readfilename)
    if not parParms: return # check for read failure
    specialKeys = ('filename',"polarization", "center", "distance", "pixelSize", "wavelength",)
    reader.Comments = ['Metadata from {} assigned by {}'.format(parParms['par file'],parParms['lbls file'])]
    for key in parParms:
        if key in specialKeys+('par file','lbls file'): continue
        reader.Comments += ["{} = {}".format(key,parParms[key])]
    if "polarization" in parParms:
        reader.Data['PolaVal'][0] = parParms["polarization"]
    else:
        reader.Data['PolaVal'][0] = 0.99
    if "center" in parParms:
        reader.Data['center'] = parParms["center"]
    if "pixelSize" in parParms:
        reader.Data['pixelSize'] = parParms["pixelSize"]
    if "wavelength" in parParms:
        reader.Data['wavelength'] = parParms['wavelength']
    else:
        G2Print('Error: wavelength not defined in {}'.format(parParms['lbls file']))
    if "distance" in parParms:
        reader.Data['distance'] = parParms['distance']
        reader.Data['setdist'] = parParms['distance']
    else:
        G2Print('Error: distance not defined in {}'.format(parParms['lbls file']))
Пример #3
0
    def Reader(self, filename, ParentFrame=None, **kwarg):
        '''Scan file structure using :meth:`visit` and map out locations of image(s)
        then read one image using :meth:`readDataset`. Save map of file structure in
        buffer arg, if used. 
        '''
        imagenum = kwarg.get('blocknum')
        if imagenum is None: imagenum = 1
        self.buffer = kwarg.get('buffer', {})
        try:
            fp = h5py.File(filename, 'r')
            if not self.buffer.get('init'):
                self.buffer['init'] = True
                self.Comments = self.visit(fp)
                if imagenum > len(self.buffer['imagemap']):
                    self.errors = 'No valid images found in file'
                    return False

            self.Data, self.Npix, self.Image = self.readDataset(fp, imagenum)
            if self.Npix == 0:
                self.errors = 'No valid images found in file'
                return False
            self.LoadImage(ParentFrame, filename, imagenum)
            self.repeatcount = imagenum
            self.repeat = imagenum < len(self.buffer['imagemap'])
            if GSASIIpath.GetConfigValue('debug'):
                print('Read image #' + str(imagenum) + ' from file ' +
                      filename)
            return True
        except IOError:
            print('cannot open file ' + filename)
            return False
        finally:
            fp.close()
Пример #4
0
def LoadDefaultExpressions():
    '''Read a configuration file with default expressions from all files named
    DefaultExpressions.txt found in the path. Duplicates are removed and
    expressions are sorted alphabetically
    '''
    global defaultExpressions
    if defaultExpressions is not None: return # run this routine only once
    defaultExpressions = sorted(list(set(GSASIIpath.LoadConfigFile('DefaultExpressions.txt'))))
Пример #5
0
def RereadImageData(ImageReaderlist,imagefile,ImageTag=None,FormatName=''):
    '''Read a single image with an image importer. This is called to 
    reread an image after it has already been imported, so it is not 
    necessary to reload metadata.

    Based on :func:`GetImageData.GetImageData` which this can replace
    where imageOnly=True

    :param list ImageReaderlist: list of Reader objects for images
    :param str imagefile: name of image file
    :param int/str ImageTag: specifies a particular image to be read from a file.
      First image is read if None (default).
    :param str formatName: the image reader formatName

    :returns: an image as a numpy array
    '''
    # determine which formats are compatible with this file
    primaryReaders = []
    secondaryReaders = []
    for rd in ImageReaderlist:
        flag = rd.ExtensionValidator(imagefile)
        if flag is None: 
            secondaryReaders.append(rd)
        elif flag:
            if not FormatName:
                primaryReaders.append(rd)
            elif FormatName == rd.formatName:
                primaryReaders.append(rd)
    if len(secondaryReaders) + len(primaryReaders) == 0:
        G2Print('Error: No matching format for file '+imagefile)
        raise Exception('No image read')
    errorReport = ''
    if not imagefile:
        return
    for rd in primaryReaders+secondaryReaders:
        rd.ReInitialize() # purge anything from a previous read
        rd.errors = "" # clear out any old errors
        if not rd.ContentsValidator(imagefile): # rejected on cursory check
            errorReport += "\n  "+rd.formatName + ' validator error'
            if rd.errors: 
                errorReport += ': '+rd.errors
                continue
        flag = rd.Reader(imagefile,None,blocknum=ImageTag)
        if flag: # this read succeeded
            if rd.Image is None:
                raise Exception('No image read. Strange!')
            if GSASIIpath.GetConfigValue('Transpose'):
                G2Print ('Warning: Transposing Image!')
                rd.Image = rd.Image.T
            #rd.readfilename = imagefile
            return rd.Image
    else:
        G2Print('Error reading file '+imagefile)
        G2Print('Error messages(s)\n'+errorReport)
        raise Exception('No image read')
Пример #6
0
def LookupFromTable(dist, parmList):
    '''Interpolate image parameters for a supplied distance value

    :param float dist: distance to use for interpolation
    :returns: a list with 2 items:
      * a dict with interpolated parameter values,
      * the closest imctrl
    '''
    cols, parms, IMfileList, ParmList, nonInterpVars, ControlsTable, MaskTable = parmList
    x = np.array([float(i) for i in parms[0]])
    closest = abs(x - dist).argmin()
    D = {'setdist': dist}
    imctfile = IMfileList[closest]
    for c in range(1, cols - 1):
        lbl = ParmList[c]
        if lbl in nonInterpVars:
            if lbl in [
                    'outChannels',
            ]:
                D[lbl] = int(float(parms[c][closest]))
            else:
                D[lbl] = float(parms[c][closest])
        else:
            y = np.array([float(i) for i in parms[c]])
            D[lbl] = np.interp(dist, x, y)
    # full integration when angular range is 0
    D['fullIntegrate'] = (D['LRazimuth_min'] == D['LRazimuth_max'])
    # conversion for paired values
    for a, b in ('center_x', 'center_y'), ('LRazimuth_min',
                                           'LRazimuth_max'), ('IOtth_min',
                                                              'IOtth_max'):
        r = a.split('_')[0]
        D[r] = [D[a], D[b]]
        if r in [
                'LRazimuth',
        ]:
            D[r] = [int(D[a]), int(D[b])]
        del D[a]
        del D[b]
    interpDict, imgctrl = D, imctfile
    if GSASIIpath.GetConfigValue('debug'):
        print('DBG_interpolated values: ', interpDict)
    f = os.path.split(imgctrl)[1]
    ImageControls = ControlsTable[f]
    ImageControls.update(interpDict)
    ImageControls['showLines'] = True
    ImageControls['ring'] = []
    ImageControls['rings'] = []
    ImageControls['ellipses'] = []
    ImageControls['setDefault'] = False
    for i in 'range', 'size', 'GonioAngles':
        if i in ImageControls: del ImageControls[i]
    ImageMasks = MaskTable.get(f)
    return ImageControls, ImageMasks
Пример #7
0
def InitMP(allowMP=True):
    '''Called to initialize use of Multiprocessing
    '''
    global useMP, ncores
    if ncores is not None: return useMP, ncores
    useMP = False
    if not allowMP:
        G2fil.G2Print('Multiprocessing disabled')
        ncores = 0
        return useMP, ncores
    ncores = GSASIIpath.GetConfigValue('Multiprocessing_cores', 0)
    if ncores < 0: ncores = mp.cpu_count() // 2
    if ncores > 1:
        useMP = True
    if useMP:
        G2fil.G2Print('Multiprocessing with {} cores enabled'.format(ncores))
    return useMP, ncores
Пример #8
0
    def LoadProject(self, fil):
        '''Load the Covariance entry from a .GPX file to the tree.
        see :func:`GSASIIIO.ProjFileOpen`
        '''
        G2frame = self
        filep = open(fil, 'rb')
        shortname = os.path.splitext(os.path.split(fil)[1])[0]

        wx.BeginBusyCursor()
        Phases = None
        try:
            while True:
                try:
                    data = cPickleLoad(filep)
                except EOFError:
                    break
                if not data[0][0].startswith('Covariance'): continue
                Covar = data[0]
                #GSASIIpath.IPyBreak_base()
                #if self.PWDRfilter is not None: # implement filter
                #    if self.PWDRfilter not in data[0][0]: continue
                Covar[0] = shortname + ' Covariance'
                Id = G2frame.GPXtree.AppendItem(parent=G2frame.root,
                                                text=Covar[0])
                G2frame.GPXtree.SetItemPyData(Id, Covar[1])
                break
            else:
                print("{} does not have refinement results".format(shortname))
        except Exception as errmsg:
            if GSASIIpath.GetConfigValue('debug'):
                print('\nError reading GPX file:', errmsg)
                import traceback
                print(traceback.format_exc())
            msg = wx.MessageDialog(G2frame,
                                   message="Error reading file " + str(fil) +
                                   ". This is not a current GSAS-II .gpx file",
                                   caption="Load Error",
                                   style=wx.ICON_ERROR | wx.OK
                                   | wx.STAY_ON_TOP)
            msg.ShowModal()
        finally:
            filep.close()
            wx.EndBusyCursor()
        self.GPXtree.Expand(self.root)
Пример #9
0
in this module are used. 
'''
########### SVN repository information ###################
# $Date: 2020-07-15 13:08:04 -0700 (Wed, 15 Jul 2020) $
# $Author: vondreele $
# $Revision: 4521 $
# $URL: https://subversion.xray.aps.anl.gov/pyGSAS/trunk/GSASIImpsubs.py $
# $Id: GSASIImpsubs.py 4521 2020-07-15 20:08:04Z vondreele $
########### SVN repository information ###################
from __future__ import division, print_function
import multiprocessing as mp
import numpy as np
import numpy.ma as ma
import GSASIIpath

GSASIIpath.SetVersionNumber("$Revision: 4521 $")
import GSASIIpwd as G2pwd
import GSASIIfiles as G2fil

sind = lambda x: np.sin(x * np.pi / 180.)
cosd = lambda x: np.cos(x * np.pi / 180.)
tand = lambda x: np.tan(x * np.pi / 180.)
#asind = lambda x: 180.*np.arcsin(x)/np.pi
#acosd = lambda x: 180.*np.arccos(x)/np.pi
#atan2d = lambda y,x: 180.*np.arctan2(y,x)/np.pi

ncores = None


def ResetMP():
    '''Call after changing Config var 'Multiprocessing_cores' to force a resetting
Пример #10
0
onlineVideos.append('https://anl.box.com/v/MerohedraltwinrefinementinGSAS')
onlineVideos.append('https://anl.box.com/v/ParametricFitting')
onlineVideos.append('https://anl.box.com/v/SequentialRefinementofSmallAng')
onlineVideos.append('https://anl.box.com/v/SequentialTutorial')
onlineVideos.append('https://anl.box.com/v/SimpleMagnetic')
onlineVideos.append('https://anl.box.com/v/SimTutorial-')
onlineVideos.append('https://anl.box.com/v/SmallAngleSizeDistribution')
onlineVideos.append('https://anl.box.com/v/StackingFaults-I')
onlineVideos.append('https://anl.box.com/v/StartingGSAS')
onlineVideos.append('https://anl.box.com/v/Strainfittingof2DdatainGSAS-II')
onlineVideos.append('https://anl.box.com/v/Textureanalysisof2DdatainGSAS-')
onlineVideos.append('https://anl.box.com/v/TOFSequentialSinglePeakFit')
#onlineVideos.append('

if __name__ == '__main__':
    GSASIIpath.SetBinaryPath()
    import GSASIIctrlGUI as G2G
    G2BaseURL = G2G.G2BaseURL
    tutorialIndex = G2G.tutorialIndex
    tutURL = G2BaseURL + '/Tutorials'
    outname = os.path.join(GSASIIpath.path2GSAS2, 'help', 'Tutorials.html')

    dirList = [l[0] for l in tutorialIndex if len(l) >= 3]

    # loop through directories in Tutorials repository
    dirs = [
        d[:-1] for d in GSASIIpath.svnList(tutURL, False).split('\n')
        if d and d[-1] == '/'
    ]
    for d in dirs:
        if d not in dirList:
Пример #11
0
#!/usr/bin/env python
"""main Absorb routines
   Copyright: 2009, Robert B. Von Dreele (Argonne National Laboratory)
"""
from __future__ import division, print_function
import platform
import math
import wx
import numpy as np
import sys
import matplotlib as mpl
import GSASIIpath
GSASIIpath.SetVersionNumber("$Revision: 4339 $")
import GSASIIElem as G2elem
import GSASIIElemGUI as G2elemGUI

try:
    wx.NewIdRef
    wx.NewId = wx.NewIdRef
except AttributeError:
    pass

if '2' in platform.python_version_tuple()[0]:
    Gktheta = unichr(0x3b8)
    Gklambda = unichr(0x3bb)
    GkDelta = unichr(0x0394)
    Pwr10 = unichr(0x0b9) + unichr(0x2070)
    Pwr20 = unichr(0x0b2) + unichr(0x2070)
    Pwrm1 = unichr(0x207b) + unichr(0x0b9)
    Pwrm2 = unichr(0x207b) + unichr(0x0b2)
    Pwrm6 = unichr(0x207b) + unichr(0x2076)
Пример #12
0
def GetSFRMData(self, filename):
    'Read cbf compressed binarydetector data sfrm file'

    if GSASIIpath.GetConfigValue('debug'):
        print('Read cbf compressed binary detector data sfrm file: ' +
              filename)
    File = open(filename, 'rb')
    sizexy = [0, 0]
    pixSize = [135.3, 135.3]  #Pixium4700?
    cent = [0, 0]
    wave = 1.54187  #default <CuKa>
    dist = 250.
    stream = File.read()
    if 'bytes' in str(type(stream)):
        stream = stream.decode('latin-1')
    starter = 'IMG: '
    meanwaves = {
        'Cu': 1.54051,
        'Ti': 2.74841,
        'Cr': 2.28962,
        'Fe': 1.93597,
        'Co': 1.78892,
        'Mo': 0.70926,
        'Ag': 0.559363
    }
    imageBeg = stream.find(starter) + 4
    head = np.array(list(stream[:imageBeg].split('CFR:')[0]))
    head = head.reshape(-1, 80)
    lines = []
    for line in head:
        line = ''.join(line)
        lines.append(line)
        fields = line.split(':')[1].split()
        if 'TARGET' in line:
            wave = meanwaves[fields[0]]
            target = fields[0].capitalize()
        elif 'DISTANC' in line:
            dist = float(fields[1]) * 10.
        elif 'ANGLES' in line:
            twoth = float(fields[0])
        elif 'CENTER' in line:
            cent = [float(fields[0]), float(fields[1])]
        elif 'NROWS' in line:
            sizexy[1] = int(fields[0])
        elif 'NCOLS' in line:
            sizexy[0] = int(fields[0])
        elif 'FORMAT' in line:
            frmt = int(fields[0])
        elif 'HDRBLKS' in line:
            imageBeg = 512 * int(fields[0])
        elif 'NOVERFL' in line:
            Nunder = int(fields[0])
            N2byte = 2 * int(fields[1])
            if N2byte % 16:
                N2byte = (N2byte // 16 + 1) * 16
            N4byte = 4 * int(fields[2])
            if N4byte % 16:
                N4byte = (N4byte // 16 + 1) * 16
    if frmt == 86:
        lines = [
            'FORMAT 86 Bruker files currently not readible by GSAS-II',
        ]
        return lines, 0, 0, 0
    nxy = sizexy[0] * sizexy[1]
    cent = [cent[0] * pixSize[0] / 1000., cent[1] * pixSize[1] / 1000.]
    cent[0] += dist * np.tan(np.pi * twoth / 180.)
    File.seek(imageBeg)
    img = File.read(nxy)
    img2byte = File.read(N2byte)
    img4byte = File.read(N4byte)
    time0 = time.time()
    img = np.array(np.frombuffer(img, dtype='u1'), dtype=np.int32)
    img2byte = np.array(np.frombuffer(img2byte, dtype='u2'), dtype=np.int32)
    img4byte = np.array(np.frombuffer(img4byte, dtype='u4'), dtype=np.int32)
    ins2byte = np.argwhere(img == 255)
    for j, i in enumerate(list(ins2byte)):
        img[i] = img2byte[j]
    ins4byte = np.argwhere(img == 65535)
    for j, i in enumerate(list(ins4byte)):
        img[i] = img4byte[j]
    image = np.reshape(img, (sizexy[1], sizexy[0]))
    print('import time: %.3f' % (time.time() - time0))
    data = {
        'pixelSize': pixSize,
        'wavelength': wave,
        'distance': dist,
        'center': cent,
        'size': sizexy,
        'target': target,
        'tilt': -twoth,
        'rotation': 90.,
        'twoth': str(round(twoth, 1))
    }
    data['pixLimit'] = 5
    data['calibdmin'] = 1.0
    data['cutoff'] = .5
    Npix = sizexy[0] * sizexy[1]

    return lines, data, Npix, image
current project.

'''
from __future__ import division, print_function
import platform
import sys
if '2' in platform.python_version_tuple()[0]:
    import cPickle
else:
    import pickle as cPickle
import random as ran
import GSASIIobj as G2obj
import GSASIIIO as G2IO
import GSASIIstrIO as G2stIO
import GSASIIpath
GSASIIpath.SetVersionNumber("$Revision: 3828 $")


class PhaseReaderClass(G2obj.ImportPhase):
    'Opens a .GPX file and pulls out a selected phase'

    def __init__(self):
        super(self.__class__,
              self).__init__(  # fancy way to say ImportPhase.__init__
                  extensionlist=('.gpx', ),
                  strictExtension=True,
                  formatName='GSAS-II gpx',
                  longFormatName='GSAS-II project (.gpx file) import')

    def ContentsValidator(self, filename):
        "Test if the 1st section can be read as a cPickle block, if not it can't be .GPX!"
Пример #14
0
# idea: select image file type & set filter from that
#
from __future__ import division, print_function
import os
import copy
import glob
import time
import re
import math
import sys
import wx
import wx.lib.mixins.listctrl as listmix
import wx.grid as wg
import numpy as np
import GSASIIpath
GSASIIpath.SetBinaryPath(True)
GSASIIpath.SetVersionNumber("$Revision: 4491 $")
import GSASIIIO as G2IO
import GSASIIctrlGUI as G2G
import GSASIIobj as G2obj
import GSASIIpy3 as G2py3
import GSASIIimgGUI as G2imG
import GSASIIfiles as G2fil
import GSASIIscriptable as G2sc
import multiprocessing as mp

try:  # fails during doc build
    wxMainFrameStyle = wx.DEFAULT_FRAME_STYLE ^ wx.CLOSE_BOX
except:
    wxMainFrameStyle = None
Пример #15
0
# $Id: G2pwd_xye.py 1620 2014-12-27 17:14:59Z vondreele $
########### SVN repository information ###################
'''
*Module G2pwd_FP: FullProf .dat data*
-------------------------------------

Routine to read in powder data from a FullProf .dat file

'''

from __future__ import division, print_function
import os.path as ospath
import numpy as np
import GSASIIobj as G2obj
import GSASIIpath
GSASIIpath.SetVersionNumber("$Revision: 1620 $")


class fp_ReaderClass(G2obj.ImportPowderData):
    'Routines to import powder data from a FullProf 1-10 column .dat file'

    def __init__(self):
        super(self.__class__, self).__init__(  # fancy way to self-reference
            extensionlist=('.dat', ),
            strictExtension=False,
            formatName='FullProf .dat',
            longFormatName='FullProf 1-10 column .dat powder data file')
        self.scriptable = True

    # Validate the contents -- make sure we only have valid lines
    def ContentsValidator(self, filename):
    def Reader(self, filename, ParentFrame=None, **kwarg):
        '''Read powder data from a CIF.
        If multiple datasets are requested, use self.repeat and buffer caching.
        '''

        # Define lists of data names used for holding powder diffraction data
        # entries of a type that are not implemented are commented out in case
        # we will want them later.
        xDataItems = (  # "x-axis" data names"
            ('_pd_meas_2theta_range_min', '_pd_meas_2theta_range_max',
             '_pd_meas_2theta_range_inc'),
            ('_pd_proc_2theta_range_min', '_pd_proc_2theta_range_max',
             '_pd_proc_2theta_range_inc'),
            '_pd_meas_2theta_scan',
            '_pd_meas_time_of_flight',
            '_pd_proc_2theta_corrected',
            #'_pd_proc_d_spacing',
            #'_pd_proc_energy_incident',
            #'_pd_proc_energy_detection',
            '_pd_proc_recip_len_q',
            #'_pd_proc_wavelength',
        )
        intDataItems = (  # intensity axis data names
            '_pd_meas_counts_total',
            #'_pd_meas_counts_background',
            #'_pd_meas_counts_container',
            '_pd_meas_intensity_total',
            #'_pd_meas_intensity_background',
            #'_pd_meas_intensity_container',
            '_pd_proc_intensity_net',
            '_pd_proc_intensity_total',
            #'_pd_proc_intensity_bkg_calc',
            #'_pd_proc_intensity_bkg_fix',
            '_pd_calc_intensity_net',  # allow computed patterns as input data?
            '_pd_calc_intensity_total',
        )

        ESDDataItems = (  # items that can be used to compute uncertainties for obs data
            '_pd_proc_ls_weight', '_pd_meas_counts_total')

        ModDataItems = (  # items that modify the use of the data
            '_pd_meas_step_count_time',
            '_pd_meas_counts_monitor',
            '_pd_meas_intensity_monitor',
            '_pd_proc_intensity_norm',
            '_pd_proc_intensity_incident',
        )
        rdbuffer = kwarg.get('buffer')
        cf = None
        choicelist = None
        selections = None
        # reload previously saved values
        if self.repeat and rdbuffer is not None:
            cf = rdbuffer.get('lastcif')
            choicelist = rdbuffer.get('choicelist')
            print('debug: Reuse previously parsed CIF')
            selections = rdbuffer.get('selections')
        if cf is None:
            if GSASIIpath.GetConfigValue('debug'):
                print("Starting parse of {} as CIF".format(filename))
            cf = G2obj.ReadCIF(filename)
            if GSASIIpath.GetConfigValue('debug'): print("CIF file parsed")
        # scan all blocks for sets of data
        if choicelist is None:
            choicelist = []
            for blk in cf.keys():
                blkkeys = [
                    k.lower() for k in cf[blk].keys()
                ]  # save a list of the data items, since we will use it often
                # scan through block for x items
                xldict = {}
                for x in xDataItems:
                    if type(
                            x
                    ) is tuple:  # check for the presence of all three items that define a range of data
                        if not all([i in blkkeys for i in x]): continue
                        try:
                            items = [float(cf[blk][xi]) for xi in x]
                            l = 1 + int(0.5 + (items[1] - items[0]) / items[2])
                        except:
                            continue
                    else:
                        if x not in blkkeys: continue
                        l = len(cf[blk][x])
                    if xldict.get(l) is None:
                        xldict[l] = [x]
                    else:
                        xldict[l].append(x)
                # now look for matching intensity items
                yldict = {}
                suldict = {}
                for y in intDataItems:
                    if y in blkkeys:
                        l = len(cf[blk][y])
                        if yldict.get(l) is None:
                            yldict[l] = [y]
                        else:
                            yldict[l].append(y)
                        # now check if the first item has an uncertainty
                        if cif.get_number_with_esd(cf[blk][y][0])[1] is None:
                            continue
                        if suldict.get(l) is None:
                            suldict[l] = [y]
                        else:
                            suldict[l].append(y)
                for y in ESDDataItems:
                    if y in blkkeys:
                        l = len(cf[blk][y])
                        if suldict.get(l) is None:
                            suldict[l] = [y]
                        else:
                            suldict[l].append(y)
                modldict = {}
                for y in ModDataItems:
                    if y in blkkeys:
                        l = len(cf[blk][y])
                        if modldict.get(l) is None:
                            modldict[l] = [y]
                        else:
                            modldict[l].append(y)
                for l in xldict:
                    if yldict.get(l) is None: continue
                    choicelist.append([
                        blk, l, xldict[l], yldict[l],
                        suldict.get(l, []),
                        modldict.get(l, [])
                    ])
                    #print blk,l,xldict[l],yldict[l],suldict.get(l,[]),modldict.get(l,[])
            print("CIF file scanned for blocks with data")
        if not choicelist:
            selblk = None  # no block to choose
            self.errors = "No powder diffraction blocks found"
            return False
        elif len(choicelist) == 1:  # only one choice
            selblk = 0
        elif self.repeat and selections is not None:
            # we were called to repeat the read
            print('debug: repeat #', self.repeatcount, 'selection',
                  selections[self.repeatcount])
            selblk = selections[self.repeatcount]
            self.repeatcount += 1
            if self.repeatcount >= len(selections): self.repeat = False
        else:  # choose from options
            # compile a list of choices for the user
            choices = []
            for blk, l, x, y, su, mod in choicelist:
                sx = x[0]
                if len(x) > 1: sx += '...'
                sy = y[0]
                if len(y) > 1: sy += '...'
                choices.append('Block ' + str(blk) + ', ' + str(l) +
                               ' points. X=' + sx + ' & Y=' + sy)
            selections = G2IO.MultipleBlockSelector(
                choices,
                ParentFrame=ParentFrame,
                title='Select dataset(s) to read from the list below',
                size=(600, 100),
                header='Dataset Selector')
            if len(selections) == 0:
                self.errors = "Abort: block not selected"
                return False
            selblk = selections[0]  # select first in list
            if len(selections) > 1:  # prepare to loop through again
                self.repeat = True
                self.repeatcount = 1
                if rdbuffer is not None:
                    rdbuffer['selections'] = selections
                    rdbuffer[
                        'lastcif'] = cf  # save the parsed cif for the next loop
                    rdbuffer[
                        'choicelist'] = choicelist  # save the parsed choices for the future

        # got a selection, now read it
        # do we need to ask which fields to read?
        blk, l, xch, ych, such, modch = choicelist[selblk]
        xi, yi, sui, modi = 0, 0, 0, 0
        if len(xch) > 1 or len(ych) > 1 or len(such) > 1 or len(modch) > 0:
            choices = []
            chlbls = []
            chlbls.append('Select the scanned data item')
            xchlst = []
            for x in xch:
                if type(x) is tuple:
                    xchlst.append(x[0])
                else:
                    xchlst.append(x)
            choices.append(xchlst)
            chlbls.append('Select the intensity data item')
            choices.append(ych)
            chlbls.append('Select the data item to be used for weighting')
            choices.append(such)
            chlbls.append('Divide intensities by data item')
            choices.append(['none'] + modch)
            res = G2IO.MultipleChoicesSelector(choices, chlbls)
            if not res:
                self.errors = "Abort: data items not selected"
                return False
            xi, yi, sui, modi = res

            # now read in the values
            # x-values
            self.powderentry[0] = filename
            #self.powderentry[1] = pos # bank offset (N/A here)
            #self.powderentry[2] = 1 # xye file only has one bank
            self.idstring = os.path.basename(filename) + ': ' + blk
            if cf[blk].get('_diffrn_radiation_probe'):
                if cf[blk]['_diffrn_radiation_probe'] == 'neutron':
                    self.instdict['type'] = 'PNC'
                    #if cf[blk].get('_pd_meas_time_of_flight'): self.instdict['type'] = 'PNT' # not supported yet
                else:
                    self.instdict['type'] = 'PXC'
            if cf[blk].get('_diffrn_radiation_wavelength'):
                val = cf[blk]['_diffrn_radiation_wavelength']
                wl = []
                if type(val) is list:
                    for v in val:
                        w, e = cif.get_number_with_esd(v)
                        if w: wl.append(w)
                else:
                    w, e = cif.get_number_with_esd(val)
                    if w: wl.append(w)
                if wl:
                    if len(wl) > 1:
                        self.instdict['wave'] = wl
                    else:
                        self.instdict['wave'] = wl[0]
            if cf[blk].get('_diffrn_ambient_temperature'):
                val = cf[blk]['_diffrn_ambient_temperature']
                w, e = cif.get_number_with_esd(val)
                if w:
                    self.Sample['Temperature'] = w
        xcf = xch[xi]
        if type(xcf) is tuple:
            vals = [float(cf[blk][ixi]) for ixi in xcf]
            x = np.array([
                (i * vals[2] + vals[0])
                for i in range(1 + int(0.5 + (vals[1] - vals[0]) / vals[2]))
            ])
        else:
            vl = []
            for val in cf[blk].get(xcf, '?'):
                v, e = cif.get_number_with_esd(val)
                if v is None:  # not parsed
                    vl.append(np.NaN)
                else:
                    vl.append(v)
            x = np.array(vl)
            if 'recip_len_q' in xcf and 'wave' in self.instdict:
                wl = self.instdict['wave']
                x = 2. * asind(wl * x / (4. * np.pi))
        # y-values
        ycf = ych[yi]
        vl = []
        v2 = []
        for val in cf[blk].get(ycf, '?'):
            v, e = cif.get_number_with_esd(val)
            if v is None:  # not parsed
                vl.append(np.NaN)
                v2.append(np.NaN)
            else:
                vl.append(v)
                if e is None:
                    v2.append(np.sqrt(v))
                else:
                    v2.append(max(e, 1.0))
        y = np.array(vl)
        w = 1. / np.array(v2)**2
        # weights
        if sui == -1:
            # no weights
            vl = np.zeros(len(x)) + 1.
        else:
            vl = []
            sucf = such[sui]
            if sucf == '_pd_proc_ls_weight':
                for val in cf[blk].get(sucf, '?'):
                    v, e = cif.get_number_with_esd(val)
                    if v is None:  # not parsed
                        vl.append(0.)
                    else:
                        vl.append(v)
            elif sucf == '_pd_proc_intensity_total':
                for val in cf[blk].get(sucf, '?'):
                    v, e = cif.get_number_with_esd(val)
                    if v is None:  # not parsed
                        vl.append(0.)
                    elif v <= 0:
                        vl.append(1.)
                    else:
                        vl.append(1. / v)
            elif sucf == '_pd_meas_counts_total':
                for val in cf[blk].get(sucf, '?'):
                    v, e = cif.get_number_with_esd(val)
                    if v is None:  # not parsed
                        vl.append(0.)
                    elif v <= 0:
                        vl.append(1.)
                    else:
                        vl.append(1. / v)
            else:
                for val in cf[blk].get(sucf, '?'):
                    v, e = cif.get_number_with_esd(val)
                    if v is None or e is None:  # not parsed or no ESD
                        vl.append(np.NaN)
                    elif e <= 0:
                        vl.append(1.)
                    else:
                        vl.append(1. / (e * e))
#            w = np.array(vl)
# intensity modification factor
        if modi >= 1:
            ycf = modch[modi - 1]
            vl = []
            for val in cf[blk].get(ycf, '?'):
                v, e = cif.get_number_with_esd(val)
                if v is None:  # not parsed
                    vl.append(np.NaN)
                else:
                    vl.append(v)
            y /= np.array(vl)
            w /= np.array(vl)
        N = len(x)
        print("CIF file, read from selected block")

        self.errors = "Error while storing read values"
        self.powderdata = [
            np.array(x),  # x-axis values
            np.array(y),  # powder pattern intensities
            np.array(w),  # 1/sig(intensity)^2 values (weights)
            np.zeros(N),  # calc. intensities (zero)
            np.zeros(N),  # calc. background (zero)
            np.zeros(N),  # obs-calc profiles
        ]
        return True
Пример #17
0
    def __init__(self, GPXfile=None):
        '''
        Initialize the setup using an existing GPXfile
        '''
        if not GPXfile:
            # TO DO: Raise name error
            raise NameError('Must input some GPX file')
        # Initallizing variables from input file GPXfile
        G2path.SetBinaryPath()
        varyList = []
        parmDict = {}
        symmetry = {}
        Controls = G2stIO.GetControls(GPXfile)
        calcControls = {}
        calcControls.update(Controls)
        constrDict, fixedList = G2stIO.GetConstraints(GPXfile)
        restraintDict = G2stIO.GetRestraints(GPXfile)
        Histograms, Phases = G2stIO.GetUsedHistogramsAndPhases(GPXfile)
        rigidbodyDict = G2stIO.GetRigidBodies(GPXfile)
        rbIds = rigidbodyDict.get('RBIds', {'Vector': [], 'Residue': []})
        rbVary, rbDict = G2stIO.GetRigidBodyModels(rigidbodyDict, Print=False)
        (Natoms, atomIndx, phaseVary, phaseDict, pawleyLookup, FFtables,
         BLtables, MFtables, maxSSwave) = G2stIO.GetPhaseData(Phases,
                                                              restraintDict,
                                                              rbIds,
                                                              Print=False)
        calcControls['atomIndx'] = atomIndx
        calcControls['Natoms'] = Natoms
        calcControls['FFtables'] = FFtables
        calcControls['BLtables'] = BLtables
        calcControls['MFtables'] = MFtables
        calcControls['maxSSwave'] = maxSSwave
        hapVary, hapDict, controlDict = G2stIO.GetHistogramPhaseData(
            Phases, Histograms, Print=False)
        calcControls.update(controlDict)
        histVary, histDict, controlDict = G2stIO.GetHistogramData(Histograms,
                                                                  Print=False)
        calcControls.update(controlDict)
        varyList = rbVary + phaseVary + hapVary + histVary
        parmDict.update(rbDict)
        parmDict.update(phaseDict)
        parmDict.update(hapDict)
        parmDict.update(histDict)
        G2stIO.GetFprime(calcControls, Histograms)
        # define phase symmetries, currently only works for one phase
        phase = G2stIO.GetPhaseNames(GPXfile)
        phaseData = {}
        for name in phase:
            phaseData[name] = G2stIO.GetAllPhaseData(GPXfile, name)
            symmetry[name] = phaseData[name]['General']['SGData']['SGSys']

        # Save the instance parameters
        self._Histograms = Histograms
        self._varyList = varyList
        self._parmDict = parmDict
        self._Phases = Phases
        self._calcControls = calcControls
        self._pawleyLookup = pawleyLookup
        self._restraintDict = restraintDict
        self._rigidbodyDict = rigidbodyDict
        self._rbIds = rbIds
        self._symmetry = symmetry
        self._phase = phase
        try:
            self._lowerLimit = Histograms[list(
                Histograms.keys())[0]][u'Limits'][1][0]
            self._upperLimit = Histograms[list(
                Histograms.keys())[0]][u'Limits'][1][1]
            self._tth = Histograms[list(Histograms.keys())[0]]['Data'][0][0:-1]
            self._tthsample = self._tth[np.where(
                (self._tth > self._lowerLimit)
                & (self._tth < self._upperLimit) == True)]  # noqa: E712
            self._SingleXtal = False
        except:  # noqa: E722
            self._tth = None
            self._MaxDiffSig = None
            self._Fosq = 0
            self._FoSig = 0
            self._Fcsq = 0
            self._SingleXtal = True
Пример #18
0
LogInfo = {'Logging':False, 'Tree':None, 'LastPaintAction':None}
'Contains values that are needed in the module for past actions & object location'

# TODO:
# Might want to save the last displayed screen with some objects to make sure
# the commands are executed in a sensible order
# 1) save tree press under tab press item.
# 2) save tree & tab for button press

# TODO:
# probably need an object for saving calls and arguments to call specific functions/methods.
# The items to be called need to register themselves so that they can be found
#   This needs to be done with a Register(function,'unique-string') call after every def
#   and then a LogCall('unique-string',pargs,kwargs) call inside the routine

debug = GSASIIpath.GetConfigValue('logging_debug')
#===========================================================================
# objects for logging variables
def _l2s(lst,separator='+'):
    'Combine a list of objects into a string, with a separator'
    s = ''
    for i in lst: 
        if s != '': s += separator
        s += '"'+str(i)+'"'
    return s

class LogEntry(object):
    '''Base class to define logging objects. These store information on events
    in a manner that can be pickled and saved -- direct references to wx objects
    is not allowed.
Пример #19
0
    def OnTimerLoop(self, event):
        '''A method that is called every :meth:`PollTime` seconds that is
        used to check for new files and process them. Integrates new images.
        Also optionally sets up and computes PDF. 
        This is called only after the "Start" button is pressed (then its label reads "Pause").
        '''

        if GSASIIpath.GetConfigValue('debug'):
            import datetime
            print("DBG_Timer tick at {:%d %b %Y %H:%M:%S}\n".format(
                datetime.datetime.now()))
        if self.PreventTimerReEntry: return
        self.PreventTimerReEntry = True
        self.ShowMatchingFiles(None)
        if not self.currImageList:
            self.PreventTimerReEntry = False
            return
        updateList = False

        # get input for integration
        imgprms = mskprms = None
        if not self.params['TableMode']:
            # read in image controls/masks, used below in loop. In Table mode
            # we will get this image-by image.
            gpxinp = G2sc.G2Project(self.gpxin[3])
            print('reading template project', gpxinp.filename)
            img = gpxinp.image(self.imprm[1].GetStringSelection())
            imgprms = img.getControls(True)
            if self.maskfl[1].GetStringSelection().strip():
                img = gpxinp.image(self.maskfl[1].GetStringSelection())
                mskprms = img.getMasks()
        # setup shared input for PDF computation (for now will not be table mode)
        xydata = {}
        if self.params['ComputePDF']:
            pdfEntry = self.pdfSel.GetStringSelection()
            try:
                PDFobj = gpxinp.pdf(pdfEntry)
            except KeyError:
                print("PDF entry not found: {}".format(pdfEntry))
            # update with GUI input
            for i, lbl in enumerate(
                ('Sample Bkg.', 'Container', 'Container Bkg.')):
                name = self.pbkg[i][1].GetStringSelection()
                try:
                    xydata[lbl] = gpxinp.histogram(name).data['data']
                except AttributeError:
                    pass
                PDFobj.data['PDF Controls'][lbl]['Mult'] = self.pbkg[i][6]
                PDFobj.data['PDF Controls'][lbl]['Name'] = name
        else:
            PDFobj = None
        if self.MPpool:
            self.MPpool.imap_unordered(
                ProcessImageMP, self.ArgGen(PDFobj, imgprms, mskprms, xydata))
        else:
            for intArgs in self.ArgGen(PDFobj, imgprms, mskprms, xydata):
                newImage = intArgs[0]
                print('processing ', newImage)
                ProcessImage(*intArgs)
                updateList = True
        for newImage in self.currImageList:
            self.ProcessedList.append(newImage)
        if updateList: self.ShowMatchingFiles(None)
        self.PreventTimerReEntry = False
        self.Raise()
Пример #20
0
# $Date: 2019-12-15 11:52:48 -0800 (Sun, 15 Dec 2019) $
# $Author: toby $
# $Revision: 4203 $
# $URL: https://subversion.xray.aps.anl.gov/pyGSAS/trunk/GSASIIElemGUI.py $
# $Id: GSASIIElemGUI.py 4203 2019-12-15 19:52:48Z toby $
########### SVN repository information ###################
'''
*GSASIIElemGUI: GUI to select and delete element lists*
-------------------------------------------------------

Module to select elements from a periodic table and
to delete an element from a list of selected elements.
'''
from __future__ import division, print_function
import GSASIIpath
GSASIIpath.SetVersionNumber("$Revision: 4203 $")
import wx
import os
import wx.lib.colourselect as wscs


class PickElement(wx.Dialog):
    '''Makes periodic table widget for picking element. Modes:
        oneOnly if True element symbols are provided, otherwise select valence
        ifNone if True show None button
        ifMag if True present magnetic scatters only
        multiple if True multiple elements can be selected
    '''
    Elem = None

    def _init_ctrls(self, prnt, ifMag=False):
# $Id: G2pwd_fxye.py 3742 2018-11-26 21:01:03Z toby $
########### SVN repository information ###################
'''
*Module G2pwd_fxye: GSAS data files*
------------------------------------
Routine to read in powder data in a variety of formats
that are defined for GSAS.

'''
from __future__ import division, print_function
import os.path as ospath
import platform
import numpy as np
import GSASIIobj as G2obj
import GSASIIpath
GSASIIpath.SetVersionNumber("$Revision: 3742 $")


class GSAS_ReaderClass(G2obj.ImportPowderData):
    'Routines to import powder data from a GSAS files'

    def __init__(self):
        super(self.__class__, self).__init__(  # fancy way to self-reference
            extensionlist=('.fxye', '.raw', '.gsas', '.gda', '.gsa', '.gss',
                           '.RAW', '.GSAS', '.GDA', '.GSA'),
            strictExtension=False,
            formatName='GSAS powder data',
            longFormatName='GSAS powder data files (.fxye, .raw, .gsas...)')
        self.clockWd = {}
        self.TimeMap = {}
        self.dnames = []
Пример #22
0
    def Reader(self, filename, ParentFrame=None, **unused):
        'Read a FullProf file'
        x = []
        y = []
        w = []
        gotCcomment = False
        begin = True
        steps = False
        Stop = False
        N = 0
        fp = open(filename, 'r')
        for i, S in enumerate(fp):
            self.errors = 'Error reading line: ' + str(i + 1)
            # Allow a block of comments delimited by /* and */
            # or (GSAS style) each comment line can begin with '#' or '!'
            if S.lstrip()[0] in [
                    "'",
                    '#',
                    '!',
            ]:
                self.comments.append(S[:-1])
                continue  # store comments, if any
            if begin:
                if gotCcomment and S.find('*/') > -1:
                    self.comments.append(S[:-1])
                    begin = False
                    continue
                if S.strip().startswith('/*'):
                    self.comments.append(S[:-1])
                    gotCcomment = True
                    continue
            # look for a line with start, steps etc. in 1st 4 lines
            if not steps:
                vals = S.replace(',', ' ').split(None, 4)
                if 'lambda' in S:
                    self.instdict['wave'] = float(vals[1])
                    continue
                elif len(vals) >= 3:
                    try:
                        start = float(vals[0])
                        step = float(vals[1])
                        stop = float(vals[2])
                        steps = True
                        begin = False
                        if len(vals) > 3:
                            self.comments.append(vals[3][:-1])
                    except:
                        print('Skipping line ', S)
                    continue
                elif i < 3:
                    print('Skipping header line ', S)
                    continue
            # should be a valid line to read
            vals = S.split()  #data strings
            try:
                for j in range(len(vals)):
                    x.append(start + N * step)
                    f = float(vals[j])
                    if f <= 0.0:
                        y.append(0.0)
                        w.append(0.0)
                    else:
                        y.append(float(vals[j]))
                        w.append(1.0 / float(vals[j]))
                    if x[-1] >= stop:
                        Stop = True
                        break
                    N += 1
                if Stop:
                    break
            except ValueError:
                msg = 'Error parsing number in line ' + str(i + 1)
                if GSASIIpath.GetConfigValue('debug'):
                    print(msg)
                    print(S.strip())
                break
            except:
                msg = 'Error in line ' + str(i + 1)
                if GSASIIpath.GetConfigValue('debug'):
                    print(msg)
                    print(S.strip())
                break
        fp.close()
        N = len(x)
        if N <= 1: return False
        self.powderdata = [
            np.array(x),  # x-axis values
            np.array(y),  # powder pattern intensities
            np.array(w),  # 1/sig(intensity)^2 values (weights)
            np.zeros(N),  # calc. intensities (zero)
            np.zeros(N),  # calc. background (zero)
            np.zeros(N),  # obs-calc profiles
        ]
        self.powderentry[0] = filename
        #self.powderentry[1] = pos # bank offset (N/A here)
        #self.powderentry[2] = 1 # xye file only has one bank
        self.idstring = ospath.basename(filename)
        # scan comments for temperature
        Temperature = 300
        for S in self.comments:
            if 'Temp' in S.split('=')[0]:
                try:
                    Temperature = float(S.split('=')[1])
                except:
                    pass
        self.Sample['Temperature'] = Temperature

        return True
Пример #23
0
def Refine(GPXfile,dlg=None,makeBack=True,refPlotUpdate=None):
    'Global refinement -- refines to minimize against all histograms'
    import GSASIImpsubs as G2mp
    G2mp.InitMP()
    import pytexture as ptx
    ptx.pyqlmninit()            #initialize fortran arrays for spherical harmonics

    printFile = open(ospath.splitext(GPXfile)[0]+'.lst','w')
    G2stIO.ShowBanner(printFile)
    varyList = []
    parmDict = {}
    G2mv.InitVars()
    Controls = G2stIO.GetControls(GPXfile)
    G2stIO.ShowControls(Controls,printFile)
    calcControls = {}
    calcControls.update(Controls)
    constrDict,fixedList = G2stIO.GetConstraints(GPXfile)
    restraintDict = G2stIO.GetRestraints(GPXfile)
    Histograms,Phases = G2stIO.GetUsedHistogramsAndPhases(GPXfile)
    if not Phases:
        G2fil.G2Print (' *** ERROR - you have no phases to refine! ***')
        G2fil.G2Print (' *** Refine aborted ***')
        return False,'No phases'
    if not Histograms:
        G2fil.G2Print (' *** ERROR - you have no data to refine with! ***')
        G2fil.G2Print (' *** Refine aborted ***')
        return False,'No data'
    rigidbodyDict = G2stIO.GetRigidBodies(GPXfile)
    rbIds = rigidbodyDict.get('RBIds',{'Vector':[],'Residue':[]})
    rbVary,rbDict = G2stIO.GetRigidBodyModels(rigidbodyDict,pFile=printFile)
    (Natoms,atomIndx,phaseVary,phaseDict,pawleyLookup,FFtables,BLtables,MFtables,
         maxSSwave) = G2stIO.GetPhaseData(Phases,restraintDict,rbIds,pFile=printFile)
    calcControls['atomIndx'] = atomIndx
    calcControls['Natoms'] = Natoms
    calcControls['FFtables'] = FFtables
    calcControls['BLtables'] = BLtables
    calcControls['MFtables'] = MFtables
    calcControls['maxSSwave'] = maxSSwave
    hapVary,hapDict,controlDict = G2stIO.GetHistogramPhaseData(Phases,Histograms,pFile=printFile)
    TwConstr,TwFixed = G2stIO.makeTwinFrConstr(Phases,Histograms,hapVary)
    constrDict += TwConstr
    fixedList += TwFixed
    calcControls.update(controlDict)
    histVary,histDict,controlDict = G2stIO.GetHistogramData(Histograms,pFile=printFile)
    calcControls.update(controlDict)
    varyList = rbVary+phaseVary+hapVary+histVary
    parmDict.update(rbDict)
    parmDict.update(phaseDict)
    parmDict.update(hapDict)
    parmDict.update(histDict)
    G2stIO.GetFprime(calcControls,Histograms)
    # do constraint processing
    varyListStart = tuple(varyList) # save the original varyList before dependent vars are removed
    msg = G2mv.EvaluateMultipliers(constrDict,parmDict)
    if msg:
        return False,'Unable to interpret multiplier(s): '+msg
    try:
        G2mv.GenerateConstraints(varyList,constrDict,fixedList,parmDict)
        #print(G2mv.VarRemapShow(varyList))
        #print('DependentVars',G2mv.GetDependentVars())
        #print('IndependentVars',G2mv.GetIndependentVars())
    except G2mv.ConstraintException:
        G2fil.G2Print (' *** ERROR - your constraints are internally inconsistent ***')
        #errmsg, warnmsg = G2mv.CheckConstraints(varyList,constrDict,fixedList)
        #print 'Errors',errmsg
        #if warnmsg: print 'Warnings',warnmsg
        return False,' Constraint error'
#    print G2mv.VarRemapShow(varyList)

    ifSeq = False
    printFile.write('\n Refinement results:\n')
    printFile.write(135*'-'+'\n')
    try:
        covData = {}
        IfOK,Rvals,result,covMatrix,sig = RefineCore(Controls,Histograms,Phases,restraintDict,
            rigidbodyDict,parmDict,varyList,calcControls,pawleyLookup,ifSeq,printFile,dlg,
            refPlotUpdate=refPlotUpdate)
        if IfOK:
            sigDict = dict(zip(varyList,sig))
            newCellDict = G2stMth.GetNewCellParms(parmDict,varyList)
            newAtomDict = G2stMth.ApplyXYZshifts(parmDict,varyList)
            covData = {'variables':result[0],'varyList':varyList,'sig':sig,'Rvals':Rvals,
                       'varyListStart':varyListStart,
                       'covMatrix':covMatrix,'title':GPXfile,'newAtomDict':newAtomDict,
                       'newCellDict':newCellDict,'freshCOV':True}
            # add the uncertainties into the esd dictionary (sigDict)
            sigDict.update(G2mv.ComputeDepESD(covMatrix,varyList,parmDict))
            G2mv.PrintIndependentVars(parmDict,varyList,sigDict,pFile=printFile)
            G2stMth.ApplyRBModels(parmDict,Phases,rigidbodyDict,True)
            G2stIO.SetRigidBodyModels(parmDict,sigDict,rigidbodyDict,printFile)
            G2stIO.SetPhaseData(parmDict,sigDict,Phases,rbIds,covData,restraintDict,printFile)
            G2stIO.SetHistogramPhaseData(parmDict,sigDict,Phases,Histograms,calcControls['FFtables'],pFile=printFile)
            G2stIO.SetHistogramData(parmDict,sigDict,Histograms,calcControls['FFtables'],pFile=printFile)
            G2stIO.SetUsedHistogramsAndPhases(GPXfile,Histograms,Phases,rigidbodyDict,covData,makeBack)
            printFile.close()
            G2fil.G2Print (' Refinement results are in file: '+ospath.splitext(GPXfile)[0]+'.lst')
            G2fil.G2Print (' ***** Refinement successful *****')
        else:
            G2fil.G2Print ('****ERROR - Refinement failed')
            raise G2obj.G2Exception('****ERROR - Refinement failed')
    except G2obj.G2RefineCancel as Msg:
        printFile.close()
        G2fil.G2Print (' ***** Refinement stopped *****')
        return False,Msg.msg
    except G2obj.G2Exception as Msg:  # cell metric error, others?
        printFile.close()
        G2fil.G2Print (' ***** Refinement error *****')
        return False,Msg.msg

#for testing purposes, create a file for testderiv
    if GSASIIpath.GetConfigValue('debug'):   # and IfOK:
#needs: values,HistoPhases,parmDict,varylist,calcControls,pawleyLookup
        fl = open(ospath.splitext(GPXfile)[0]+'.testDeriv','wb')
        cPickle.dump(result[0],fl,1)
        cPickle.dump([Histograms,Phases,restraintDict,rigidbodyDict],fl,1)
        cPickle.dump([constrDict,fixedList,G2mv.GetDependentVars()],fl,1)
        cPickle.dump(parmDict,fl,1)
        cPickle.dump(varyList,fl,1)
        cPickle.dump(calcControls,fl,1)
        cPickle.dump(pawleyLookup,fl,1)
        fl.close()
    if dlg:
        return True,Rvals
Пример #24
0
try:
    import GSASIIpath
    print('import of GSASIIpath completed')
except Exception as err:
    print('\n' + 75 * '=')
    print('Failed with import of GSASIIpath. This is unexpected.')
    print('GSAS-II will not run without correcting this. Contact [email protected]')
    print(err)
    print(75 * '=')
    sys.exit()

for a in sys.argv[1:]:
    if 'allbin' in a.lower() or 'server' in a.lower():
        print('Loading all binaries with command...')
        if not GSASIIpath.svnSwitchDir('AllBinaries', '', g2home + 'Binaries/',
                                       None, True):
            print('Binary load failed')
            sys.exit()
        break
else:
    GSASIIpath.DownloadG2Binaries(g2home)

#===========================================================================
# test if the compiled files load correctly
#===========================================================================

script = """  # commands that test each module can at least be loaded & run something in pyspg
try:
    import GSASIIpath
    GSASIIpath.SetBinaryPath()
    import pyspg
Пример #25
0
# $Id: G2img_CheMin.py 3136 2017-10-23 16:39:16Z vondreele $
########### SVN repository information ###################
'''
*Module G2img_png: png image file*
---------------------------------------

Routine to read an image in .png (Portable Network Graphics) format.
For now, the only known use of this is with converted Mars Rover (CheMin)
tif files, so default parameters are for that.

'''

from __future__ import division, print_function
import GSASIIobj as G2obj
import GSASIIpath
GSASIIpath.SetVersionNumber("$Revision: 3136 $")


class png_ReaderClass(G2obj.ImportImage):
    '''Reads standard PNG images; parameters are set to those of the
    Mars Rover (CheMin) diffractometer.
    '''
    def __init__(self):
        super(self.__class__, self).__init__(  # fancy way to self-reference
            extensionlist=('.png', ),
            strictExtension=True,
            formatName='PNG image',
            longFormatName='PNG image from CheMin')

    def ContentsValidator(self, filename):
        '''no test at this time
Пример #26
0
def SeqRefine(GPXfile,dlg,refPlotUpdate=None):
    '''Perform a sequential refinement -- cycles through all selected histgrams,
    one at a time
    '''
    import GSASIImpsubs as G2mp
    G2mp.InitMP()
    import pytexture as ptx
    ptx.pyqlmninit()            #initialize fortran arrays for spherical harmonics

    printFile = open(ospath.splitext(GPXfile)[0]+'.lst','w')
    G2fil.G2Print ('Starting Sequential Refinement')
    G2stIO.ShowBanner(printFile)
    Controls = G2stIO.GetControls(GPXfile)
    G2stIO.ShowControls(Controls,printFile,SeqRef=True)
    restraintDict = G2stIO.GetRestraints(GPXfile)
    Histograms,Phases = G2stIO.GetUsedHistogramsAndPhases(GPXfile)
    if not Phases:
        G2fil.G2Print (' *** ERROR - you have no phases to refine! ***')
        G2fil.G2Print (' *** Refine aborted ***')
        return False,'No phases'
    if not Histograms:
        G2fil.G2Print (' *** ERROR - you have no data to refine with! ***')
        G2fil.G2Print (' *** Refine aborted ***')
        return False,'No data'
    rigidbodyDict = G2stIO.GetRigidBodies(GPXfile)
    rbIds = rigidbodyDict.get('RBIds',{'Vector':[],'Residue':[]})
    rbVary,rbDict = G2stIO.GetRigidBodyModels(rigidbodyDict,pFile=printFile)
    G2mv.InitVars()
    (Natoms,atomIndx,phaseVary,phaseDict,pawleyLookup,FFtables,BLtables,MFtables,
         maxSSwave) = G2stIO.GetPhaseData(Phases,restraintDict,rbIds,
                                    Print=False,pFile=printFile,seqRef=True)
    for item in phaseVary:
        if '::A0' in item:
            G2fil.G2Print ('**** WARNING - lattice parameters should not be refined in a sequential refinement ****')
            G2fil.G2Print ('****           instead use the Dij parameters for each powder histogram            ****')
            return False,'Lattice parameter refinement error - see console message'
        if '::C(' in item:
            G2fil.G2Print ('**** WARNING - phase texture parameters should not be refined in a sequential refinement ****')
            G2fil.G2Print ('****           instead use the C(L,N) parameters for each powder histogram               ****')
            return False,'Phase texture refinement error - see console message'
    if 'Seq Data' in Controls:
        histNames = Controls['Seq Data']
    else: # patch from before Controls['Seq Data'] was implemented? 
        histNames = G2stIO.GetHistogramNames(GPXfile,['PWDR',])
    if Controls.get('Reverse Seq'):
        histNames.reverse()
    SeqResult = G2stIO.GetSeqResult(GPXfile)
#    SeqResult = {'SeqPseudoVars':{},'SeqParFitEqList':[]}
    Histo = {}
    NewparmDict = {}
    G2stIO.SetupSeqSavePhases(GPXfile)
    for ihst,histogram in enumerate(histNames):
        if GSASIIpath.GetConfigValue('Show_timing'): t1 = time.time()
        G2fil.G2Print('\nRefining with '+str(histogram))
        G2mv.InitVars()
        (Natoms,atomIndx,phaseVary,phaseDict,pawleyLookup,
             FFtables,BLtables,MFtables,maxSSwave) = G2stIO.GetPhaseData(
                 Phases,restraintDict,rbIds,
                 Print=False,pFile=printFile,seqRef=True)
        ifPrint = False
        if dlg:
            dlg.SetTitle('Residual for histogram '+str(ihst))
        calcControls = {}
        calcControls['atomIndx'] = atomIndx
        calcControls['Natoms'] = Natoms
        calcControls['FFtables'] = FFtables
        calcControls['BLtables'] = BLtables
        calcControls['MFtables'] = MFtables
        calcControls['maxSSwave'] = maxSSwave
        if histogram not in Histograms:
            G2fil.G2Print("Error: not found!")
            continue
    #TODO - implement "Fix FXU" for seq refinement here - done?
        hId = Histograms[histogram]['hId']
        redphaseVary = phaseCheck(phaseVary,Phases,histogram)
        Histo = {histogram:Histograms[histogram],}
        hapVary,hapDict,controlDict = G2stIO.GetHistogramPhaseData(Phases,Histo,Print=False)
        calcControls.update(controlDict)
        histVary,histDict,controlDict = G2stIO.GetHistogramData(Histo,False)
        calcControls.update(controlDict)
        varyList = rbVary+redphaseVary+hapVary+histVary
#        if not ihst:
            # save the initial vary list, but without histogram numbers on parameters
        saveVaryList = varyList[:]
        for i,item in enumerate(saveVaryList):
            items = item.split(':')
            if items[1]:
                items[1] = ''
            item = ':'.join(items)
            saveVaryList[i] = item
        if not ihst:
            SeqResult['varyList'] = saveVaryList
        else:
            SeqResult['varyList'] = list(set(SeqResult['varyList']+saveVaryList))
        parmDict = {}
        parmDict.update(rbDict)
        parmDict.update(phaseDict)
        parmDict.update(hapDict)
        parmDict.update(histDict)
        if Controls['Copy2Next']:   # update with parms from last histogram
            #parmDict.update(NewparmDict) # don't use in case extra entries would cause a problem
            for parm in NewparmDict:
                if parm in parmDict:
                    parmDict[parm] = NewparmDict[parm]
        elif histogram in SeqResult:  # update phase from last seq ref
            NewparmDict = SeqResult[histogram].get('parmDict',{})
            for parm in NewparmDict:
                if '::' in parm and parm in parmDict:
                    parmDict[parm] = NewparmDict[parm]
            
        G2stIO.GetFprime(calcControls,Histo)
        # do constraint processing
        #reload(G2mv) # debug
        constrDict,fixedList = G2stIO.GetConstraints(GPXfile)
        varyListStart = tuple(varyList) # save the original varyList before dependent vars are removed
        msg = G2mv.EvaluateMultipliers(constrDict,parmDict)
        if msg:
            return False,'Unable to interpret multiplier(s): '+msg
        try:
            groups,parmlist = G2mv.GenerateConstraints(varyList,constrDict,fixedList,parmDict,SeqHist=hId)
#            if GSASIIpath.GetConfigValue('debug'): print("DBG_"+
#                G2mv.VarRemapShow(varyList,True))
#            print('DependentVars',G2mv.GetDependentVars())
#            print('IndependentVars',G2mv.GetIndependentVars())
            constraintInfo = (groups,parmlist,constrDict,fixedList,ihst)
        except G2mv.ConstraintException:
            G2fil.G2Print (' *** ERROR - your constraints are internally inconsistent ***')
            #errmsg, warnmsg = G2mv.CheckConstraints(varyList,constrDict,fixedList)
            #print 'Errors',errmsg
            #if warnmsg: print 'Warnings',warnmsg
            return False,' Constraint error'
        #print G2mv.VarRemapShow(varyList)
        if not ihst:
            # first histogram to refine against
            firstVaryList = []
            for item in varyList:
                items = item.split(':')
                if items[1]:
                    items[1] = ''
                item = ':'.join(items)
                firstVaryList.append(item)
            newVaryList = firstVaryList
        else:
            newVaryList = []
            for item in varyList:
                items = item.split(':')
                if items[1]:
                    items[1] = ''
                item = ':'.join(items)
                newVaryList.append(item)
        if newVaryList != firstVaryList and Controls['Copy2Next']:
            # variable lists are expected to match between sequential refinements when Copy2Next is on
            #print '**** ERROR - variable list for this histogram does not match previous'
            #print '     Copy of variables is not possible'
            #print '\ncurrent histogram',histogram,'has',len(newVaryList),'variables'
            combined = list(set(firstVaryList+newVaryList))
            c = [var for var in combined if var not in newVaryList]
            p = [var for var in combined if var not in firstVaryList]
            G2fil.G2Print('*** Variables change ***')
            for typ,vars in [('Removed',c),('Added',p)]:
                line = '  '+typ+': '
                if vars:
                    for var in vars:
                        if len(line) > 70:
                            G2fil.G2Print(line)
                            line = '    '
                        line += var + ', '
                else:
                        line += 'none, '
                G2fil.G2Print(line[:-2])
            firstVaryList = newVaryList

        ifSeq = True
        printFile.write('\n Refinement results for histogram: %s\n'%histogram)
        printFile.write(135*'-'+'\n')
        try:
            IfOK,Rvals,result,covMatrix,sig = RefineCore(Controls,Histo,Phases,restraintDict,
                rigidbodyDict,parmDict,varyList,calcControls,pawleyLookup,ifSeq,printFile,dlg,
                refPlotUpdate=refPlotUpdate)
            G2fil.G2Print ('  wR = %7.2f%%, chi**2 = %12.6g, reduced chi**2 = %6.2f, last delta chi = %.4f'%(
                Rvals['Rwp'],Rvals['chisq'],Rvals['GOF']**2,Rvals['DelChi2']))
            # add the uncertainties into the esd dictionary (sigDict)
            if not IfOK:
                G2fil.G2Print('***** Sequential refinement failed at histogram '+histogram,mode='warn')
                break
            sigDict = dict(zip(varyList,sig))
            # the uncertainties for dependent constrained parms into the esd dict
            sigDict.update(G2mv.ComputeDepESD(covMatrix,varyList,parmDict))

            # a dict with values & esds for dependent (constrained) parameters - avoid extraneous holds
            depParmDict = {i:(parmDict[i],sigDict[i]) for i in varyListStart if i in sigDict and i not in varyList}
            newCellDict = copy.deepcopy(G2stMth.GetNewCellParms(parmDict,varyList))
            newAtomDict = copy.deepcopy(G2stMth.ApplyXYZshifts(parmDict,varyList))
            histRefData = {
                'variables':result[0],'varyList':varyList,'sig':sig,'Rvals':Rvals,
                'varyListStart':varyListStart,
                'covMatrix':covMatrix,'title':histogram,'newAtomDict':newAtomDict,
                'newCellDict':newCellDict,'depParmDict':depParmDict,
                'constraintInfo':constraintInfo,
                'parmDict':parmDict}
            SeqResult[histogram] = histRefData
            G2stMth.ApplyRBModels(parmDict,Phases,rigidbodyDict,True)
#            G2stIO.SetRigidBodyModels(parmDict,sigDict,rigidbodyDict,printFile)
            G2stIO.SetHistogramPhaseData(parmDict,sigDict,Phases,Histo,None,ifPrint,printFile)
            G2stIO.SetHistogramData(parmDict,sigDict,Histo,None,ifPrint,printFile)
            G2stIO.SaveUpdatedHistogramsAndPhases(GPXfile,Histo,Phases,rigidbodyDict,histRefData)
            NewparmDict = {}
            # make dict of varied parameters in current histogram, renamed to
            # next histogram, for use in next refinement.
            if Controls['Copy2Next'] and ihst < len(histNames)-1:
                hId = Histo[histogram]['hId'] # current histogram
                nexthId = Histograms[histNames[ihst+1]]['hId']
                for parm in set(list(varyList)+list(varyListStart)):
                    items = parm.split(':')
                    if len(items) < 3: 
                        continue
                    if str(hId) in items[1]:
                        items[1] = str(nexthId)
                        newparm = ':'.join(items)
                        NewparmDict[newparm] = parmDict[parm]
                    else:
                        if items[2].startswith('dA'): parm = parm.replace(':dA',':A') 
                        NewparmDict[parm] = parmDict[parm]
                    
        except G2obj.G2RefineCancel as Msg:
            printFile.close()
            G2fil.G2Print (' ***** Refinement stopped *****')
            return False,Msg.msg
        except G2obj.G2Exception as Msg:  # cell metric error, others?
            printFile.close()
            G2fil.G2Print (' ***** Refinement error *****')
            return False,Msg.msg
        if GSASIIpath.GetConfigValue('Show_timing'):
            t2 = time.time()
            G2fil.G2Print("Fit step time {:.2f} sec.".format(t2-t1))
            t1 = t2
    SeqResult['histNames'] = [itm for itm in G2stIO.GetHistogramNames(GPXfile,['PWDR',]) if itm in SeqResult.keys()]
    G2stIO.SetSeqResult(GPXfile,Histograms,SeqResult)
    printFile.close()
    G2fil.G2Print (' Sequential refinement results are in file: '+ospath.splitext(GPXfile)[0]+'.lst')
    G2fil.G2Print (' ***** Sequential refinement successful *****')
    return True,'Success'
Пример #27
0
of slightly incorrect pseudo-TIF formats used at instruments around the world.
Note that the name ``G2img_1TIF`` is used so that this file will
sort to the top of the image formats and thus show up first in the menu.
(It is the most common, alas).

'''

from __future__ import division, print_function
import struct as st
import GSASIIobj as G2obj
import GSASIIpath
import GSASIIfiles as G2fil
import numpy as np
import time
DEBUG = False
GSASIIpath.SetVersionNumber("$Revision: 4112 $")


class TIF_ReaderClass(G2obj.ImportImage):
    '''Reads TIF files using a routine (:func:`GetTifData`) that looks
    for files that can be identified from known instruments and will
    correct for slightly incorrect TIF usage. If that routine fails,
    it will be read with a standard TIF reader, which can handle compression
    and other things less commonly used at beamlines. 
    '''
    def __init__(self):
        super(self.__class__, self).__init__(  # fancy way to self-reference
            extensionlist=('.tif', '.tiff'),
            strictExtension=False,
            formatName='TIF image',
            longFormatName='Various .tif and pseudo-TIF formats')
Пример #28
0
'''
*Module G2img_HDF5: summed HDF5 image file*
-------------------------------------------

Reads all images found in a HDF5 file.

'''

from __future__ import division, print_function
try:
    import h5py
except ImportError:
    h5py = None
import GSASIIobj as G2obj
import GSASIIpath
GSASIIpath.SetVersionNumber("$Revision: 3361 $")


class HDF5_Reader(G2obj.ImportImage):
    '''Routine to read a HD5 image, typically from APS Sector 6.
    B. Frosik/SDM.
    '''
    dsetlist = []
    buffer = {}
    init = False

    def __init__(self):
        if h5py is None:
            self.UseReader = False
            print('HDF5 Reader skipped because h5py library is not installed')
            import os, sys
Пример #29
0
================================

Ellipse fitting & image integration

'''

import math
import time
import numpy as np
import numpy.linalg as nl
import numpy.ma as ma
import polymask as pm
from scipy.optimize import leastsq
import copy
import GSASIIpath
GSASIIpath.SetVersionNumber("$Revision$")
import GSASIIplot as G2plt
import GSASIIlattice as G2lat
import GSASIIpwd as G2pwd
import GSASIIspc as G2spc
import fellipse as fel

# trig functions in degrees
sind = lambda x: math.sin(x * math.pi / 180.)
asind = lambda x: 180. * math.asin(x) / math.pi
tand = lambda x: math.tan(x * math.pi / 180.)
atand = lambda x: 180. * math.atan(x) / math.pi
atan2d = lambda y, x: 180. * math.atan2(y, x) / math.pi
cosd = lambda x: math.cos(x * math.pi / 180.)
acosd = lambda x: 180. * math.acos(x) / math.pi
rdsq2d = lambda x, p: round(1.0 / math.sqrt(x), p)
Пример #30
0
def GetTifData(filename):
    '''Read an image in a pseudo-tif format,
    as produced by a wide variety of software, almost always
    incorrectly in some way. 
    '''
    import struct as st
    import array as ar
    import ReadMarCCDFrame as rmf
    image = None
    File = open(filename, 'rb')
    dataType = 5
    center = [None, None]
    wavelength = None
    distance = None
    polarization = None
    samplechangerpos = None
    try:
        Meta = open(filename + '.metadata', 'Ur')
        head = Meta.readlines()
        for line in head:
            line = line.strip()
            try:
                if '=' not in line: continue
                keyword = line.split('=')[0].strip()
                if 'dataType' == keyword:
                    dataType = int(line.split('=')[1])
                elif 'wavelength' == keyword.lower():
                    wavelength = float(line.split('=')[1])
                elif 'distance' == keyword.lower():
                    distance = float(line.split('=')[1])
                elif 'polarization' == keyword.lower():
                    polarization = float(line.split('=')[1])
                elif 'samplechangercoordinate' == keyword.lower():
                    samplechangerpos = float(line.split('=')[1])
            except:
                G2fil.G2Print('error reading metadata: ' + line)
        Meta.close()
    except IOError:
        G2fil.G2Print('no metadata file found - will try to read file anyway')
        head = [
            'no metadata file found',
        ]

    tag = File.read(2)
    if 'bytes' in str(type(tag)):
        tag = tag.decode('latin-1')
    byteOrd = '<'
    if tag == 'II' and int(st.unpack('<h',
                                     File.read(2))[0]) == 42:  #little endian
        IFD = int(st.unpack(byteOrd + 'i', File.read(4))[0])
    elif tag == 'MM' and int(st.unpack('>h',
                                       File.read(2))[0]) == 42:  #big endian
        byteOrd = '>'
        IFD = int(st.unpack(byteOrd + 'i', File.read(4))[0])
    else:
        #        print (tag)
        lines = [
            'not a detector tiff file',
        ]
        return lines, 0, 0, 0
    File.seek(IFD)  #get number of directory entries
    NED = int(st.unpack(byteOrd + 'h', File.read(2))[0])
    IFD = {}
    nSlice = 1
    if DEBUG: print('byteorder:', byteOrd)
    for ied in range(NED):
        Tag, Type = st.unpack(byteOrd + 'Hh', File.read(4))
        nVal = st.unpack(byteOrd + 'i', File.read(4))[0]
        if DEBUG: print('Try:', Tag, Type, nVal)
        if Type == 1:
            Value = st.unpack(byteOrd + nVal * 'b', File.read(nVal))
        elif Type == 2:
            Value = st.unpack(byteOrd + 'i', File.read(4))
        elif Type == 3:
            Value = st.unpack(byteOrd + nVal * 'h', File.read(nVal * 2))
            st.unpack(byteOrd + nVal * 'h', File.read(nVal * 2))
        elif Type == 4:
            if Tag in [273, 279]:
                nSlice = nVal
                nVal = 1
            Value = st.unpack(byteOrd + nVal * 'i', File.read(nVal * 4))
        elif Type == 5:
            Value = st.unpack(byteOrd + nVal * 'i', File.read(nVal * 4))
        elif Type == 11:
            Value = st.unpack(byteOrd + nVal * 'f', File.read(nVal * 4))
        IFD[Tag] = [Type, nVal, Value]
        if DEBUG: print(Tag, IFD[Tag])
    sizexy = [IFD[256][2][0], IFD[257][2][0]]
    [nx, ny] = sizexy
    Npix = nx * ny
    time0 = time.time()
    if 34710 in IFD:
        G2fil.G2Print('Read MAR CCD tiff file: ' + filename)
        marFrame = rmf.marFrame(File, byteOrd, IFD)
        image = np.flipud(np.array(np.asarray(marFrame.image), dtype=np.int32))
        tifType = marFrame.filetitle
        pixy = [marFrame.pixelsizeX / 1000.0, marFrame.pixelsizeY / 1000.0]
        head = marFrame.outputHead()
        # extract resonable wavelength from header
        wavelength = marFrame.sourceWavelength * 1e-5
        wavelength = (marFrame.opticsWavelength >
                      0) and marFrame.opticsWavelength * 1e-5 or wavelength
        wavelength = (wavelength <= 0) and None or wavelength
        # extract resonable distance from header
        distance = (marFrame.startXtalToDetector +
                    marFrame.endXtalToDetector) * 5e-4
        distance = (distance <= marFrame.startXtalToDetector *
                    5e-4) and marFrame.xtalToDetector * 1e-3 or distance
        distance = (distance <= 0) and None or distance
        # extract resonable center from header
        center = [
            marFrame.beamX * marFrame.pixelsizeX * 1e-9,
            marFrame.beamY * marFrame.pixelsizeY * 1e-9
        ]
        center = (center[0] != 0 and center[1] != 0) and center or [None, None]
#print head,tifType,pixy
    elif nSlice > 1:  #CheMin multislice tif file!
        try:
            import Image as Im
        except ImportError:
            try:
                from PIL import Image as Im
            except ImportError:
                G2fil.G2Print(
                    "PIL/pillow Image module not present. This TIF cannot be read without this"
                )
                #raise Exception("PIL/pillow Image module not found")
                lines = [
                    'not a detector tiff file',
                ]
                return lines, 0, 0, 0
        tifType = 'CheMin'
        pixy = [40., 40.]
        image = np.flipud(np.array(Im.open(filename))) * 10.
        distance = 18.0
        center = [pixy[0] * sizexy[0] / 2000, 0]  #the CheMin beam stop is here
        wavelength = 1.78892
    elif 272 in IFD:
        ifd = IFD[272]
        File.seek(ifd[2][0])
        S = File.read(ifd[1])
        if b'PILATUS' in S:
            tifType = 'Pilatus'
            dataType = 0
            pixy = [172., 172.]
            File.seek(4096)
            G2fil.G2Print('Read Pilatus tiff file: ' + filename)
            image = np.array(np.frombuffer(File.read(4 * Npix),
                                           dtype=np.int32),
                             dtype=np.int32)
        else:
            if IFD[258][2][0] == 16:
                if sizexy == [3888, 3072] or sizexy == [3072, 3888]:
                    tifType = 'Dexela'
                    pixy = [74.8, 74.8]
                    G2fil.G2Print('Read Dexela detector tiff file: ' +
                                  filename)
                else:
                    tifType = 'GE'
                    pixy = [200., 200.]
                    G2fil.G2Print('Read GE-detector tiff file: ' + filename)
                File.seek(8)
                image = np.array(np.frombuffer(File.read(2 * Npix),
                                               dtype=np.uint16),
                                 dtype=np.int32)
            elif IFD[258][2][0] == 32:
                # includes CHESS & Pilatus files from Area Detector
                tifType = 'CHESS'
                pixy = [200., 200.]
                File.seek(8)
                G2fil.G2Print('Read as 32-bit unsigned (CHESS) tiff file: ' +
                              filename)
                image = np.array(ar.array('I', File.read(4 * Npix)),
                                 dtype=np.uint32)
    elif 270 in IFD:
        File.seek(IFD[270][2][0])
        S = File.read(IFD[273][2][0] - IFD[270][2][0])
        if b'ImageJ' in S:
            tifType = 'ImageJ'
            dataType = 0
            pixy = [200., 200.] * IFD[277][2][0]
            File.seek(IFD[273][2][0])
            G2fil.G2Print('Read ImageJ tiff file: ' + filename)
            if IFD[258][2][0] == 32:
                image = File.read(4 * Npix)
                image = np.array(np.frombuffer(image, dtype=byteOrd + 'i4'),
                                 dtype=np.int32)
            elif IFD[258][2][0] == 16:
                image = File.read(2 * Npix)
                pixy = [109.92, 109.92]  #for LCLS ImageJ tif files
                image = np.array(np.frombuffer(image, dtype=byteOrd + 'u2'),
                                 dtype=np.int32)
        else:  #gain map from  11-ID-C?
            pixy = [200., 200.]
            tifType = 'Gain map'
            image = File.read(4 * Npix)
            image = np.array(np.frombuffer(image, dtype=byteOrd + 'f4') * 1000,
                             dtype=np.int32)

    elif 262 in IFD and IFD[262][2][0] > 4:
        tifType = 'DND'
        pixy = [158., 158.]
        File.seek(512)
        G2fil.G2Print('Read DND SAX/WAX-detector tiff file: ' + filename)
        image = np.array(np.frombuffer(File.read(2 * Npix), dtype=np.uint16),
                         dtype=np.int32)
    elif sizexy == [1536, 1536]:
        tifType = 'APS Gold'
        pixy = [150., 150.]
        File.seek(64)
        G2fil.G2Print('Read Gold tiff file:' + filename)
        image = np.array(np.frombuffer(File.read(2 * Npix), dtype=np.uint16),
                         dtype=np.int32)
    elif sizexy == [2048, 2048] or sizexy == [1024, 1024
                                              ] or sizexy == [3072, 3072]:
        if IFD[273][2][0] == 8:
            if IFD[258][2][0] == 32:
                tifType = 'PE'
                pixy = [200., 200.]
                File.seek(8)
                G2fil.G2Print('Read APS PE-detector tiff file: ' + filename)
                if dataType == 5:
                    image = np.array(np.frombuffer(File.read(4 * Npix),
                                                   dtype=np.float32),
                                     dtype=np.int32)  #fastest
                else:
                    image = np.array(np.frombuffer(File.read(4 * Npix),
                                                   dtype=np.int32),
                                     dtype=np.int32)
            elif IFD[258][2][0] == 16:
                tifType = 'MedOptics D1'
                pixy = [46.9, 46.9]
                File.seek(8)
                G2fil.G2Print('Read MedOptics D1 tiff file: ' + filename)
                image = np.array(np.frombuffer(File.read(2 * Npix),
                                               dtype=np.uint16),
                                 dtype=np.int32)

        elif IFD[273][2][0] == 4096:
            if sizexy[0] == 3072:
                pixy = [73., 73.]
                tifType = 'MAR225'
            else:
                pixy = [158., 158.]
                tifType = 'MAR325'
            File.seek(4096)
            G2fil.G2Print('Read MAR CCD tiff file: ' + filename)
            image = np.array(np.frombuffer(File.read(2 * Npix),
                                           dtype=np.uint16),
                             dtype=np.int32)
        elif IFD[273][2][0] == 512:
            tifType = '11-ID-C'
            pixy = [200., 200.]
            File.seek(512)
            G2fil.G2Print('Read 11-ID-C tiff file: ' + filename)
            image = np.array(np.frombuffer(File.read(2 * Npix),
                                           dtype=np.uint16),
                             dtype=np.int32)

    elif sizexy == [4096, 4096]:
        if IFD[273][2][0] == 8:
            if IFD[258][2][0] == 16:
                tifType = 'scanCCD'
                pixy = [9., 9.]
                File.seek(8)
                G2fil.G2Print('Read APS scanCCD tiff file: ' + filename)
                image = np.array(ar.array('H', File.read(2 * Npix)),
                                 dtype=np.int32)
            elif IFD[258][2][0] == 32:
                tifType = 'PE4k'
                pixy = [100., 100.]
                File.seek(8)
                G2fil.G2Print('Read PE 4Kx4K tiff file: ' + filename)
                image = np.array(
                    np.frombuffer(File.read(4 * Npix), dtype=np.float32) /
                    2.**4,
                    dtype=np.int32)
        elif IFD[273][2][0] == 4096:
            tifType = 'Rayonix'
            pixy = [73.242, 73.242]
            File.seek(4096)
            G2fil.G2Print('Read Rayonix MX300HE tiff file: ' + filename)
            image = np.array(np.frombuffer(File.read(2 * Npix),
                                           dtype=np.uint16),
                             dtype=np.int32)
    elif sizexy == [391, 380]:
        pixy = [109.92, 109.92]
        File.seek(8)
        image = np.array(np.frombuffer(File.read(2 * Npix), dtype=np.int16),
                         dtype=np.int32)
    elif sizexy == [380, 391]:
        File.seek(110)
        pixy = [109.92, 109.92]
        image = np.array(np.frombuffer(File.read(Npix), dtype=np.uint8),
                         dtype=np.int32)
    elif sizexy == [825, 830]:
        pixy = [109.92, 109.92]
        File.seek(8)
        image = np.array(np.frombuffer(File.read(Npix), dtype=np.uint8),
                         dtype=np.int32)
    elif sizexy == [1800, 1800]:
        pixy = [109.92, 109.92]
        File.seek(110)
        image = np.array(np.frombuffer(File.read(Npix), dtype=np.uint8),
                         dtype=np.int32)
    elif sizexy == [2880, 2880]:
        pixy = [150., 150.]
        File.seek(8)
        dt = np.dtype(np.float32)
        dt = dt.newbyteorder(byteOrd)
        image = np.array(np.frombuffer(File.read(Npix * 4), dtype=dt),
                         dtype=np.int32)
#    elif sizexy == [960,960]:
#        tiftype = 'PE-BE'
#        pixy = (200,200)
#        File.seek(8)
#        if not imageOnly:
#            print 'Read Gold tiff file:',filename
#        image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)

    if image is None:
        lines = [
            'not a known detector tiff file',
        ]
        return lines, 0, 0, 0

    if sizexy[1] * sizexy[0] != image.size:  # test is resize is allowed
        lines = [
            'not a known detector tiff file',
        ]
        return lines, 0, 0, 0
    if GSASIIpath.GetConfigValue('debug'):
        G2fil.G2Print('image read time: %.3f' % (time.time() - time0))
    image = np.reshape(image, (sizexy[1], sizexy[0]))
    center = (not center[0]) and [
        pixy[0] * sizexy[0] / 2000, pixy[1] * sizexy[1] / 2000
    ] or center
    wavelength = (not wavelength) and 0.10 or wavelength
    distance = (not distance) and 100.0 or distance
    polarization = (not polarization) and 0.99 or polarization
    samplechangerpos = (not samplechangerpos) and 0.0 or samplechangerpos
    data = {
        'pixelSize': pixy,
        'wavelength': wavelength,
        'distance': distance,
        'center': center,
        'size': sizexy,
        'setdist': distance,
        'PolaVal': [polarization, False],
        'samplechangerpos': samplechangerpos
    }
    File.close()
    return head, data, Npix, image