source: trunk/imports/G2img_MAR.py @ 3146

Last change on this file since 3146 was 3146, checked in by vondreele, 4 years ago

add time to G2img_MAR.py, fix open bug in G2pwd_fxye.py

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