[2001] | 1 | # -*- coding: utf-8 -*- |
---|
| 2 | ########### SVN repository information ################### |
---|
[2133] | 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 $ |
---|
[2001] | 8 | ########### SVN repository information ################### |
---|
| 9 | ''' |
---|
| 10 | *Module G2img_MAR: MAR image files* |
---|
| 11 | -------------------------------------- |
---|
| 12 | ''' |
---|
| 13 | |
---|
[3136] | 14 | from __future__ import division, print_function |
---|
| 15 | import platform |
---|
[3146] | 16 | import time |
---|
[2817] | 17 | import GSASIIobj as G2obj |
---|
[2001] | 18 | import GSASIIpath |
---|
[2817] | 19 | import numpy as np |
---|
[2133] | 20 | GSASIIpath.SetVersionNumber("$Revision: 3146 $") |
---|
[2817] | 21 | class MAR_ReaderClass(G2obj.ImportImage): |
---|
[2068] | 22 | '''Routine to read several MAR formats, .mar3450,.mar2300,.mar2560 |
---|
| 23 | ''' |
---|
[2001] | 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 | |
---|
[3136] | 32 | def ContentsValidator(self, filename): |
---|
[2001] | 33 | '''no test at this time |
---|
| 34 | ''' |
---|
| 35 | return True |
---|
| 36 | |
---|
[3136] | 37 | def Reader(self,filename, ParentFrame=None, **unused): |
---|
[2817] | 38 | self.Comments,self.Data,self.Npix,self.Image = GetMAR345Data(filename) |
---|
[2001] | 39 | if self.Npix == 0 or not self.Comments: |
---|
| 40 | return False |
---|
[2015] | 41 | self.LoadImage(ParentFrame,filename) |
---|
[2001] | 42 | return True |
---|
[2817] | 43 | |
---|
| 44 | def GetMAR345Data(filename,imageOnly=False): |
---|
| 45 | 'Read a MAR-345 image plate image' |
---|
| 46 | try: |
---|
| 47 | import pack_f as pf |
---|
| 48 | except: |
---|
[3136] | 49 | print ('**** ERROR - Unable to load the GSAS MAR image decompression, pack_f') |
---|
[2817] | 50 | return None,None,None,None |
---|
| 51 | |
---|
| 52 | if not imageOnly: |
---|
[3136] | 53 | print ('Read Mar345 file: '+filename) |
---|
[2817] | 54 | File = open(filename,'rb') |
---|
[3136] | 55 | head = File.read(4095).decode(encoding='latin-1') |
---|
[2817] | 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) |
---|
[3136] | 84 | line = File.read(8).decode(encoding='latin-1') |
---|
[2817] | 85 | while 'CCP4' not in line: #get past overflow list for now |
---|
[3136] | 86 | line = File.read(8).decode(encoding='latin-1') |
---|
[2817] | 87 | pos += 8 |
---|
| 88 | pos += 37 |
---|
| 89 | File.seek(pos) |
---|
[3144] | 90 | image = np.zeros(shape=(sizex,sizey),dtype=np.int32) |
---|
[3146] | 91 | time0 = time.time() |
---|
[3136] | 92 | if '2' in platform.python_version_tuple()[0]: |
---|
| 93 | raw = File.read() |
---|
[3144] | 94 | image = np.flipud(pf.pack_f(len(raw),raw,sizex,sizey,image).T) #transpose to get it right way around & flip |
---|
[3136] | 95 | else: |
---|
[3144] | 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 |
---|
[3146] | 98 | print ('image read time: %.3f'%(time.time()-time0)) |
---|
[2817] | 99 | File.close() |
---|
| 100 | if imageOnly: |
---|
| 101 | return image |
---|
| 102 | else: |
---|
| 103 | return head,data,Npix,image |
---|
| 104 | |
---|