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 | |
---|
13 | Routine to read a summed GE image from APS Sector 1 |
---|
14 | |
---|
15 | ''' |
---|
16 | |
---|
17 | import sys |
---|
18 | import os |
---|
19 | import numpy as np |
---|
20 | import GSASIIIO as G2IO |
---|
21 | import GSASIIpath |
---|
22 | GSASIIpath.SetVersionNumber("$Revision: $") |
---|
23 | class GEsum_ReaderClass(G2IO.ImportImage): |
---|
24 | def __init__(self): |
---|
25 | super(self.__class__,self).__init__( # fancy way to self-reference |
---|
26 | extensionlist=('.sum','.cor','.avg','.ge','.ge1','.ge2','.ge3','.ge4'), |
---|
27 | strictExtension=True, |
---|
28 | formatName = 'GE image', |
---|
29 | longFormatName = 'Summed GE image file' |
---|
30 | ) |
---|
31 | |
---|
32 | def ContentsValidator(self, filepointer): |
---|
33 | '''no test at this time |
---|
34 | ''' |
---|
35 | return True |
---|
36 | |
---|
37 | def Reader(self,filename,filepointer, ParentFrame=None, **kwarg): |
---|
38 | '''Read using GE file reader |
---|
39 | ''' |
---|
40 | #rdbuffer = kwarg.get('buffer') |
---|
41 | imagenum = kwarg.get('blocknum') |
---|
42 | if imagenum is None: imagenum = 1 |
---|
43 | self.Comments,self.Data,self.Npix,self.Image,more = GetGEsumData(filename,imagenum=imagenum) |
---|
44 | if self.Npix == 0 or not self.Comments: |
---|
45 | return False |
---|
46 | self.LoadImage(ParentFrame,filename,imagenum) |
---|
47 | self.repeat = more |
---|
48 | return True |
---|
49 | |
---|
50 | def GetGEsumData(filename,imagenum=1): |
---|
51 | '''Read G.E. detector images from various files as produced at 1-ID and |
---|
52 | with Detector Pool detector. |
---|
53 | ''' |
---|
54 | import struct as st |
---|
55 | import array as ar |
---|
56 | more = False |
---|
57 | File = open(filename,'rb') |
---|
58 | if '.sum' in filename or '.cor' in filename: |
---|
59 | head = ['GE detector sum or cor data from APS 1-ID',] |
---|
60 | sizexy = [2048,2048] |
---|
61 | Npix = sizexy[0]*sizexy[1] |
---|
62 | image = np.array(ar.array('f',File.read(4*Npix)),dtype=np.int32) |
---|
63 | elif '.avg' in filename: |
---|
64 | head = ['GE detector avg or ge* data from APS 1-ID',] |
---|
65 | sizexy = [2048,2048] |
---|
66 | Npix = sizexy[0]*sizexy[1] |
---|
67 | image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32) |
---|
68 | else: |
---|
69 | head = ['GE detector raw data',] |
---|
70 | File.seek(18) |
---|
71 | size,nframes = st.unpack('<ih',File.read(6)) |
---|
72 | # number of frames seems to be 3 for single-image files |
---|
73 | if size != 2048: |
---|
74 | print('Warning GE image size unexpected: '+str(size)) |
---|
75 | return 0,0,0,0,False # probably should quit now |
---|
76 | if imagenum > nframes: |
---|
77 | print('Error: attempt to read image #'+str(imagenum)+ |
---|
78 | ' from file with '+str(nframes)+' images.') |
---|
79 | return 0,0,0,0,False |
---|
80 | elif imagenum < nframes: |
---|
81 | more = True |
---|
82 | sizexy = [2048,2048] |
---|
83 | Npix = sizexy[0]*sizexy[1] |
---|
84 | pos = 8192 + (imagenum-1)*2*Npix |
---|
85 | File.seek(pos) |
---|
86 | image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32) |
---|
87 | if len(image) != sizexy[1]*sizexy[0]: |
---|
88 | print('not enough images while reading GE file: '+filename+'image #'+str(imagenum)) |
---|
89 | return 0,0,0,0,False |
---|
90 | head += ['file: '+filename+' image #'+str(imagenum),] |
---|
91 | #while nframes > 1: |
---|
92 | # print 'adding' |
---|
93 | # image += np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32) |
---|
94 | # nframes -= 1 |
---|
95 | image = np.reshape(image,(sizexy[1],sizexy[0])) |
---|
96 | data = {'pixelSize':[200,200],'wavelength':0.15,'distance':250.0,'center':[204.8,204.8],'size':sizexy} |
---|
97 | data['ImageTag'] = imagenum |
---|
98 | File.close() |
---|
99 | if GSASIIpath.GetConfigValue('debug'): |
---|
100 | print 'Read GE file: '+filename+' image #'+str(imagenum) |
---|
101 | return head,data,Npix,image,more |
---|