source: trunk/imports/G2img_MAR.py @ 3144

Last change on this file since 3144 was 3144, checked in by vondreele, 5 years ago

fix MAR345 & CBF importers to work for python 3.6 as well as python 2.7; revised importers & new fortran decompression routines for py3.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 3.4 KB
Line 
1# -*- coding: utf-8 -*-
2########### SVN repository information ###################
3# $Date: 2017-10-30 20:39:15 +0000 (Mon, 30 Oct 2017) $
4# $Author: vondreele $
5# $Revision: 3144 $
6# $URL: trunk/imports/G2img_MAR.py $
7# $Id: G2img_MAR.py 3144 2017-10-30 20:39:15Z 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 sys
17import struct as st
18import GSASIIobj as G2obj
19import GSASIIpath
20import numpy as np
21GSASIIpath.SetVersionNumber("$Revision: 3144 $")
22class 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
45def 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    image = np.zeros(shape=(sizex,sizey),dtype=np.int32)   
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    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.