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