source: trunk/imports/G2img_GE.py @ 2068

Last change on this file since 2068 was 2068, checked in by toby, 6 years ago

cleanup image reader documetation

  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
1# -*- coding: utf-8 -*-
2########### SVN repository information ###################
3# $Date: 2014-12-27 11:14:59 -0600 (Sat, 27 Dec 2014) $
4# $Author: $
5# $Revision: $
6# $URL: $
7# $Id: $
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: $")
24class GEsum_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        '''no test at this time
44        '''
45        return True
46       
47    def Reader(self,filename,filepointer, ParentFrame=None, **kwarg):
48        '''Read using GE file reader, :func:`GetGEsumData`
49        '''
50        #rdbuffer = kwarg.get('buffer')
51        imagenum = kwarg.get('blocknum')
52        if imagenum is None: imagenum = 1
53        self.Comments,self.Data,self.Npix,self.Image,more = GetGEsumData(filename,imagenum=imagenum)
54        if self.Npix == 0 or not self.Comments:
55            return False
56        self.LoadImage(ParentFrame,filename,imagenum)
57        self.repeat = more
58        return True
59
60def GetGEsumData(filename,imagenum=1):
61    '''Read G.E. detector images from various files as produced at 1-ID and
62    with Detector Pool detector.
63    '''
64    import struct as st
65    import array as ar
66    more = False
67    File = open(filename,'rb')
68    if '.sum' in filename or '.cor' in filename:
69        head = ['GE detector sum or cor data from APS 1-ID',]
70        sizexy = [2048,2048]
71        Npix = sizexy[0]*sizexy[1]
72        image = np.array(ar.array('f',File.read(4*Npix)),dtype=np.int32)
73    elif '.avg' in filename:
74        head = ['GE detector avg or ge* data from APS 1-ID',]
75        sizexy = [2048,2048]
76        Npix = sizexy[0]*sizexy[1]
77        image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
78    else:
79        head = ['GE detector raw data',]
80        File.seek(18)
81        size,nframes = st.unpack('<ih',File.read(6))
82        # number of frames seems to be 3 for single-image files
83        if size != 2048:
84            print('Warning GE image size unexpected: '+str(size))
85            return 0,0,0,0,False # probably should quit now
86        if imagenum > nframes:
87            print('Error: attempt to read image #'+str(imagenum)+
88                  ' from file with '+str(nframes)+' images.')
89            return 0,0,0,0,False
90        elif imagenum < nframes:
91            more = True
92        sizexy = [2048,2048]
93        Npix = sizexy[0]*sizexy[1]
94        pos = 8192 + (imagenum-1)*2*Npix
95        File.seek(pos)
96        image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
97        if len(image) != sizexy[1]*sizexy[0]:
98            print('not enough images while reading GE file: '+filename+'image #'+str(imagenum))
99            return 0,0,0,0,False           
100        head += ['file: '+filename+' image #'+str(imagenum),]
101        #while nframes > 1:
102        #    print 'adding'
103        #    image += np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
104        #    nframes -= 1
105    image = np.reshape(image,(sizexy[1],sizexy[0]))
106    data = {'pixelSize':[200,200],'wavelength':0.15,'distance':250.0,'center':[204.8,204.8],'size':sizexy}
107    self.repeatcount = imagenum
108    File.close()
109    if GSASIIpath.GetConfigValue('debug'):
110        print 'Read GE file: '+filename+' image #'+str(imagenum)
111    return head,data,Npix,image,more
Note: See TracBrowser for help on using the repository browser.