source: trunk/imports/G2img_MAR.py

Last change on this file was 5577, checked in by toby, 3 weeks ago

finish docs reformatting with changes to G2tools and GSASIIscripts; ReadTheDocs? html now looks good

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