1 | # -*- coding: utf-8 -*- |
---|
2 | ########### SVN repository information ################### |
---|
3 | # $Date: 2017-05-02 15:03:41 +0000 (Tue, 02 May 2017) $ |
---|
4 | # $Author: vondreele $ |
---|
5 | # $Revision: 2817 $ |
---|
6 | # $URL: trunk/imports/G2img_MAR.py $ |
---|
7 | # $Id: G2img_MAR.py 2817 2017-05-02 15:03:41Z vondreele $ |
---|
8 | ########### SVN repository information ################### |
---|
9 | ''' |
---|
10 | *Module G2img_MAR: MAR image files* |
---|
11 | -------------------------------------- |
---|
12 | ''' |
---|
13 | |
---|
14 | import GSASIIobj as G2obj |
---|
15 | import GSASIIpath |
---|
16 | import numpy as np |
---|
17 | GSASIIpath.SetVersionNumber("$Revision: 2817 $") |
---|
18 | class MAR_ReaderClass(G2obj.ImportImage): |
---|
19 | '''Routine to read several MAR formats, .mar3450,.mar2300,.mar2560 |
---|
20 | ''' |
---|
21 | def __init__(self): |
---|
22 | super(self.__class__,self).__init__( # fancy way to self-reference |
---|
23 | extensionlist=('.mar3450','.mar2300','.mar2560'), |
---|
24 | strictExtension=True, |
---|
25 | formatName = 'MAR image', |
---|
26 | longFormatName = 'MAR Research 345, 230 and 256 image files' |
---|
27 | ) |
---|
28 | |
---|
29 | def ContentsValidator(self, filepointer): |
---|
30 | '''no test at this time |
---|
31 | ''' |
---|
32 | return True |
---|
33 | |
---|
34 | def Reader(self,filename,filepointer, ParentFrame=None, **unused): |
---|
35 | self.Comments,self.Data,self.Npix,self.Image = GetMAR345Data(filename) |
---|
36 | if self.Npix == 0 or not self.Comments: |
---|
37 | return False |
---|
38 | self.LoadImage(ParentFrame,filename) |
---|
39 | return True |
---|
40 | |
---|
41 | def GetMAR345Data(filename,imageOnly=False): |
---|
42 | 'Read a MAR-345 image plate image' |
---|
43 | try: |
---|
44 | import pack_f as pf |
---|
45 | except: |
---|
46 | print '**** ERROR - Unable to load the GSAS MAR image decompression, pack_f' |
---|
47 | return None,None,None,None |
---|
48 | |
---|
49 | if not imageOnly: |
---|
50 | print 'Read Mar345 file: ',filename |
---|
51 | File = open(filename,'rb') |
---|
52 | head = File.read(4095) |
---|
53 | lines = head[128:].split('\n') |
---|
54 | head = [] |
---|
55 | for line in lines: |
---|
56 | line = line.strip() |
---|
57 | if 'PIXEL' in line: |
---|
58 | values = line.split() |
---|
59 | pixel = (int(values[2]),int(values[4])) #in microns |
---|
60 | elif 'WAVELENGTH' in line: |
---|
61 | wave = float(line.split()[1]) |
---|
62 | elif 'DISTANCE' in line: |
---|
63 | distance = float(line.split()[1]) #in mm |
---|
64 | if not distance: |
---|
65 | distance = 500. |
---|
66 | elif 'CENTER' in line: |
---|
67 | values = line.split() |
---|
68 | center = [float(values[2])/10.,float(values[4])/10.] #make in mm from pixels |
---|
69 | if line: |
---|
70 | head.append(line) |
---|
71 | data = {'pixelSize':pixel,'wavelength':wave,'distance':distance,'center':center} |
---|
72 | for line in head: |
---|
73 | if 'FORMAT' in line[0:6]: |
---|
74 | items = line.split() |
---|
75 | sizex = int(items[1]) |
---|
76 | Npix = int(items[3]) |
---|
77 | sizey = int(Npix/sizex) |
---|
78 | pos = 4096 |
---|
79 | data['size'] = [sizex,sizey] |
---|
80 | File.seek(pos) |
---|
81 | line = File.read(8) |
---|
82 | while 'CCP4' not in line: #get past overflow list for now |
---|
83 | line = File.read(8) |
---|
84 | pos += 8 |
---|
85 | pos += 37 |
---|
86 | File.seek(pos) |
---|
87 | raw = File.read() |
---|
88 | File.close() |
---|
89 | image = np.zeros(shape=(sizex,sizey),dtype=np.int32) |
---|
90 | |
---|
91 | image = np.flipud(pf.pack_f(len(raw),raw,sizex,sizey,image).T) #transpose to get it right way around & flip |
---|
92 | if imageOnly: |
---|
93 | return image |
---|
94 | else: |
---|
95 | return head,data,Npix,image |
---|
96 | |
---|