source: trunk/imports/G2pwd_Panalytical.py @ 3136

Last change on this file since 3136 was 3136, checked in by vondreele, 5 years ago

make GSAS-II python 3.6 compliant & preserve python 2.7 use;changes:
do from future import division, print_function for all GSAS-II py sources
all menu items revised to be py 2.7/3.6 compliant
all wx.OPEN --> wx.FD_OPEN in file dialogs
all integer divides (typically for image pixel math) made explicit with ; ambiguous ones made floats as appropriate
all print "stuff" --> print (stuff)
all print >> pFile,'stuff' --> pFile.writeCIFtemplate('stuff')
all read file opens made explicit 'r' or 'rb'
all cPickle imports made for py2.7 or 3.6 as cPickle or _pickle; test for '2' platform.version_tuple[0] for py 2.7
define cPickleload to select load(fp) or load(fp,encoding='latin-1') for loading gpx files; provides cross compatibility between py 2.7/3.6 gpx files
make dict.keys() as explicit list(dict.keys()) as needed (NB: possible source of remaining py3.6 bugs)
make zip(a,b) as explicit list(zip(a,b)) as needed (NB: possible source of remaining py3.6 bugs)
select unichr/chr according test for '2' platform.version_tuple[0] for py 2.7 (G2pwdGUI * G2plot) for special characters
select wg.EVT_GRID_CELL_CHANGE (classic) or wg.EVT_GRID_CELL_CHANGED (phoenix) in grid Bind
maxint --> maxsize; used in random number stuff
raise Exception,"stuff" --> raise Exception("stuff")
wx 'classic' sizer.DeleteWindows?() or 'phoenix' sizer.Clear(True)
wx 'classic' SetToolTipString?(text) or 'phoenix' SetToolTip?(wx.ToolTip?(text)); define SetToolTipString?(self,text) to handle the choice in plots
status.SetFields? --> status.SetStatusText?
'classic' AddSimpleTool? or 'phoenix' self.AddTool? for plot toolbar; Bind different as well
define GetItemPydata? as it doesn't exist in wx 'phoenix'
allow python versions 2.7 & 3.6 to run GSAS-II
Bind override commented out - no logging capability (NB: remove all logging code?)
all import ContentsValidator? open filename & test if valid then close; filepointer removed from Reader
binary importers (mostly images) test for 'byte' type & convert as needed to satisfy py 3.6 str/byte rules

File size: 4.7 KB
RevLine 
[2552]1# -*- coding: utf-8 -*-
2########### SVN repository information ###################
3# $Date: $
4# $Author: von dreele $
5# $Revision: $
6# $URL: $
7# $Id: $
8########### SVN repository information ###################
[2817]9
[3136]10from __future__ import division, print_function
[2552]11import os.path as ospath
12import xml.etree.ElementTree as ET
13import numpy as np
[2817]14import GSASIIobj as G2obj
[2552]15import GSASIIpath
16GSASIIpath.SetVersionNumber("$Revision: $")
[2817]17class Panalytical_ReaderClass(G2obj.ImportPowderData):
[2552]18    '''Routines to import powder data from a Pananalytical.xrdm (xml) file.
19   
20    '''
21    def __init__(self):
22        super(self.__class__,self).__init__( # fancy way to self-reference
23            extensionlist=('.xrdml','.xml'),
24            strictExtension=True,
25            formatName = 'Panalytical xrdml (xml)',
26            longFormatName = 'Panalytical powder data as *.xrdml'
27            )
[2835]28        self.scriptable = True
[2552]29        self.vals = None
30        self.stepsize = None
31        self.skip = 0
32        self.root = None
33
34    # Validate the contents -- make sure we only have valid lines and set
35    # values we will need for later read.
[3136]36    def ContentsValidator(self, filename):
37        fp = open(filename,'r')
[2552]38        self.vals = None
39        self.stepsize = None
[3136]40        fp.seek(0)
[2552]41        try:
[3136]42            self.root = ET.parse(fp).getroot()
[2552]43            tag = self.root.tag
44            tag = tag.split('}')[0]+'}'
45            self.root.find(tag+'comment')
46        except:
47            self.errors = 'Bad xml file'
[3136]48            fp.close()
[2552]49            return False
[3136]50        fp.close()
[2552]51        return True
52           
[3136]53    def Reader(self,filename, ParentFrame=None, **kwarg):
[2552]54        'Read a Panalytical .xrdml (.xml) file; already in self.root'
55        blockNum = kwarg.get('blocknum',0)
56        self.idstring = ospath.basename(filename) + ' Scan '+str(blockNum)
57        x = []
58        y = []
59        w = []
60        tag = self.root.tag
61        tag = tag.split('}')[0]+'}'
62        sample = self.root.find(tag+'sample')
63        self.idstring = ospath.basename(filename) + ' Scan '+str(blockNum)
[3053]64        data = self.root.find(tag+'xrdMeasurement')
[2552]65        wave = data.find(tag+'usedWavelength')
66        incident = data.find(tag+'incidentBeamPath')
67        radius = float(incident.find(tag+'radius').text)
68        tube = incident.find(tag+'xRayTube')
[3053]69        scans = data.findall(tag+'scan')
70        if len(scans) > 1:
71            self.repeat = True
72        if blockNum-1 == len(scans):
73            self.repeat = False
74            return False
75        scan = scans[blockNum-1]
[2552]76        header = scan.find(tag+'header')
77        dataPoints = scan.find(tag+'dataPoints')
78        self.comments.append('Gonio. radius=%.2f'%(radius))
79        self.Sample['Gonio. radius'] = radius
80        if sample.find(tag+'id').text:
81            self.comments.append('Sample name='+sample.find(tag+'id').text)
[3052]82        try:
83            self.comments.append('Date/TimeStart='+header.find(tag+'startTimeStamp').text)
84            self.comments.append('Date/TimeEnd='+header.find(tag+'endTimeStamp').text)
85            self.comments.append('xray tube='+tube.attrib['name'])
86        except AttributeError:
87            pass
[2552]88        self.comments.append('Ka1=%s'%(wave.find(tag+'kAlpha1').text))
89        self.comments.append('Ka2=%s'%(wave.find(tag+'kAlpha2').text))
90        self.comments.append('Ka2/Ka1=%s'%(wave.find(tag+'ratioKAlpha2KAlpha1').text))
91        self.comments.append('Kb=%s'%(wave.find(tag+'kBeta').text))
92        self.comments.append('Voltage='+tube.find(tag+'tension').text)
93        self.comments.append('Current='+tube.find(tag+'current').text)
94        limits = dataPoints.find(tag+'positions')
95        startPos = float(limits.find(tag+'startPosition').text)
96        endPos= float(limits.find(tag+'endPosition').text)
97        y = np.fromstring(dataPoints.find(tag+'intensities').text,sep=' ')
98        N = y.shape[0]
99        x = np.linspace(startPos,endPos,N)
100        w = np.where(y>0,1./y,1.)
101        self.powderdata = [
102            np.array(x), # x-axis values
103            np.array(y), # powder pattern intensities
104            np.array(w), # 1/sig(intensity)^2 values (weights)
105            np.zeros(N), # calc. intensities (zero)
106            np.zeros(N), # calc. background (zero)
107            np.zeros(N), # obs-calc profiles
108            ]
109        conditions = scan.find(tag+'nonAmbientPoints')
110        if conditions:
111            kind = conditions.attrib['type']
112            if kind == 'Temperature':
113                Temperature = float(conditions.find(tag+'nonAmbientValues').text.split()[-1])
114                self.Sample['Temperature'] = Temperature
115        return True
Note: See TracBrowser for help on using the repository browser.