source: trunk/imports/G2img_GE.py @ 2296

Last change on this file since 2296 was 2296, checked in by vondreele, 6 years ago

make frame numbers 4 place zero filled per Bjorn's request.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 6.3 KB
Line 
1# -*- coding: utf-8 -*-
2########### SVN repository information ###################
3# $Date: 2016-05-26 16:59:28 +0000 (Thu, 26 May 2016) $
4# $Author: vondreele $
5# $Revision: 2296 $
6# $URL: trunk/imports/G2img_GE.py $
7# $Id: G2img_GE.py 2296 2016-05-26 16:59:28Z vondreele $
8########### SVN repository information ###################
9'''
10*Module G2img_GE: summed GE image file*
11---------------------------------------
12
13This shows an example of an importer that will handle files with
14more than a single image.
15
16'''
17
18import sys
19import os
20import numpy as np
21import GSASIIIO as G2IO
22import GSASIIpath
23GSASIIpath.SetVersionNumber("$Revision: 2296 $")
24class GE_ReaderClass(G2IO.ImportImage):
25    '''Routine to read a GE image, typically from APS Sector 1.
26       
27        The image files may be of form .geX (where X is ' ', 1, 2, 3 or 4),
28        which is a raw image from the detector. These files may contain more
29        than one image and have a rudimentary header.
30        Files with extension .sum or .cor are 4 byte integers/pixel, one image/file.
31        Files with extension .avg are 2 byte integers/pixel, one image/file.
32    '''
33
34    def __init__(self):
35        super(self.__class__,self).__init__( # fancy way to self-reference
36            extensionlist=('.sum','.cor','.avg','.ge','.ge1','.ge2','.ge3','.ge4'),
37            strictExtension=True,
38            formatName = 'GE image',
39            longFormatName = 'Summed GE image file'
40            )
41
42    def ContentsValidator(self, filepointer):
43        '''just a test on file size
44        '''
45        if '.sum' not in str(filepointer):
46            try:
47                statinfo = os.stat(str(filepointer).split("'")[1])
48                fsize = statinfo.st_size
49                self.nimages = (fsize-8192)/(2*2048**2)
50            except:
51                return False    #bad file size
52        return True
53       
54    def Reader(self,filename,filepointer, ParentFrame=None, **kwarg):
55        '''Read using GE file reader, :func:`GetGEsumData`
56        '''
57        #rdbuffer = kwarg.get('buffer')
58        imagenum = kwarg.get('blocknum')
59        #sum = kwarg.get('sum')
60        if imagenum is None: imagenum = 1
61        self.Comments,self.Data,self.Npix,self.Image,more = \
62            GetGEsumData(filename,imagenum=imagenum)
63        if self.Npix == 0 or not self.Comments:
64            return False
65        self.LoadImage(ParentFrame,filename,imagenum)
66        self.repeatcount = imagenum
67        self.repeat = more
68        return True
69
70class GEsum_ReaderClass(G2IO.ImportImage):
71    '''Routine to read multiple GE images & sum them, typically from APS Sector 1.
72       
73        The image files may be of form .geX (where X is ' ', 1, 2, 3 or 4),
74        which is a raw image from the detector. These files may contain more
75        than one image and have a rudimentary header.
76        Files with extension .sum or .cor are 4 byte integers/pixel, one image/file.
77        Files with extension .avg are 2 byte integers/pixel, one image/file.
78    '''
79
80    def __init__(self):
81        super(self.__class__,self).__init__( # fancy way to self-reference
82            extensionlist=('.ge1','.ge2','.ge3','.ge4'),
83            strictExtension=True,
84            formatName = 'GE multi-image summed',
85            longFormatName = 'sum of GE multi-image file'
86            )
87
88    def ContentsValidator(self, filepointer):
89        '''just a test on file size
90        '''
91        try:
92            statinfo = os.stat(str(filepointer).split("'")[1])
93            fsize = statinfo.st_size
94            nimages = (fsize-8192)/(2*2048**2)
95        except:
96            return False    #bad file size
97        return True
98       
99    def Reader(self,filename,filepointer, ParentFrame=None, **kwarg):
100        '''Read using GE file reader, :func:`GetGEsumData`
101        '''
102        #rdbuffer = kwarg.get('buffer')
103        imagenum = kwarg.get('blocknum')
104        if imagenum is None: imagenum = 1
105        self.Comments,self.Data,self.Npix,self.Image,more = \
106            GetGEsumData(filename,imagenum=imagenum,sum=True)
107        if self.Npix == 0 or not self.Comments:
108            return False
109        self.LoadImage(ParentFrame,filename,imagenum)
110        self.repeatcount = imagenum
111        self.repeat = more
112        return True
113
114def GetGEsumData(filename,imagenum=1,sum=False):
115    '''Read G.E. detector images from various files as produced at 1-ID and
116    with Detector Pool detector. Also sums multiple image files if desired
117    '''
118    import struct as st
119    import array as ar
120    more = False
121    File = open(filename,'rb')
122    if '.sum' in filename or '.cor' in filename:
123        head = ['GE detector sum or cor data from APS 1-ID',]
124        sizexy = [2048,2048]
125        Npix = sizexy[0]*sizexy[1]
126        image = np.array(ar.array('f',File.read(4*Npix)),dtype=np.int32)
127    elif '.avg' in filename:
128        head = ['GE detector avg or ge* data from APS 1-ID',]
129        sizexy = [2048,2048]
130        Npix = sizexy[0]*sizexy[1]
131        image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
132    else:
133        head = ['GE detector raw data',]
134        File.seek(18)
135        size,nframes = st.unpack('<ih',File.read(6))
136        # number of frames seems to be 3 for single-image files
137        if size != 2048:
138            print('Warning GE image size unexpected: '+str(size))
139            return 0,0,0,0,False # probably should quit now
140        if imagenum > nframes:
141            print('Error: attempt to read image #'+str(imagenum)+
142                  ' from file with '+str(nframes)+' images.')
143            return 0,0,0,0,False
144        elif imagenum < nframes:
145            more = True
146        sizexy = [2048,2048]
147        Npix = sizexy[0]*sizexy[1]
148        pos = 8192 + (imagenum-1)*2*Npix
149        File.seek(pos)
150        image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
151        if len(image) != sizexy[1]*sizexy[0]:
152            print('not enough images while reading GE file: '+filename+'image #'+str(imagenum))
153            return 0,0,0,0,False           
154        head += ['file: '+filename+' image #'+str(imagenum),]
155        if sum:    #will ignore imagenum
156            while nframes > 1: #OK, this will sum the frames.
157                image += np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
158                nframes -= 1
159            more = False
160    image = np.reshape(image,(sizexy[1],sizexy[0]))
161    data = {'pixelSize':[200,200],'wavelength':0.15,'distance':250.0,'center':[204.8,204.8],'size':sizexy}
162    File.close()
163    if GSASIIpath.GetConfigValue('debug'):
164        print 'Read GE file: '+filename+' image #'+'%04d'%(imagenum)
165    return head,data,Npix,image,more
Note: See TracBrowser for help on using the repository browser.