1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
#/*##########################################################################
#
# The PyMca X-Ray Fluorescence Toolkit
#
# Copyright (c) 2004-2019 European Synchrotron Radiation Facility
#
# This file is part of the PyMca X-ray Fluorescence Toolkit developed at
# the ESRF by the Software group.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#############################################################################*/
__author__ = "V.A. Sole - ESRF Data Analysis"
__contact__ = "sole@esrf.fr"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
from . import DataObject
from PyMca5.PyMcaIO import EdfFile
import types
import sys
import os
import numpy
import logging
SOURCE_TYPE = "EdfFile"
_logger = logging.getLogger(__name__)
class EdfFileDataSource(object):
def __init__(self,nameInput, fastedf=False):
if type(nameInput) == list:
nameList = nameInput
else:
nameList = [nameInput]
if sys.version < '3.0':
stringTypes = [types.StringType, types.UnicodeType]
else:
stringTypes = [type("a"), type(eval('b"a"'))]
for name in nameList:
if type(name) not in stringTypes:
raise TypeError("Constructor needs string as first argument")
self.sourceName = nameInput
self.sourceType = SOURCE_TYPE
self.__sourceNameList = nameList
#this is to be added
#self._fastedf = True
self._fastedf = fastedf
if fastedf:
_logger.warning("fastedf is unsafe!")
self.refresh()
def refresh(self):
self._sourceObjectList=[]
for name in self.__sourceNameList:
self._sourceObjectList.append(EdfFile.EdfFile(name, access='rb', fastedf=self._fastedf))
self.__lastKeyInfo = {}
def getSourceInfo(self):
"""
Returns information about the EdfFile object created by
SetSource, to give application possibility to know about
it before loading.
Returns a dictionary with the key "KeyList" (list of all available keys
in this source). Each element in "KeyList" has the form 'n1.n2' where
n1 is the source number and n2 image number in file starting at 1.
"""
return self.__getSourceInfo()
def __getSourceInfo(self):
SourceInfo={}
SourceInfo["SourceType"]=SOURCE_TYPE
SourceInfo["KeyList"]=[]
i = 0
for sourceObject in self._sourceObjectList:
i+=1
nimages = sourceObject.GetNumImages()
for n in range(nimages):
SourceInfo["KeyList"].append("%d.%d" % (i,n+1))
SourceInfo["Size"]=len(SourceInfo["KeyList"])
return SourceInfo
def getKeyInfo(self,key):
if key in self.getSourceInfo()['KeyList']:
return self.__getKeyInfo(key)
else:
#should we raise a KeyError?
_logger.debug("Error key not in list ")
return {}
def __getKeyInfo(self,key):
try:
index,image = key.split(".")
index = int(index)-1
image = int(image)-1
except:
#should we rise an error?
_logger.debug("Error trying to interpret key = %s", key)
return {}
sourceObject = self._sourceObjectList[index]
info= sourceObject.GetStaticHeader(image)
info.update(sourceObject.GetHeader(image))
info["SourceType"] = SOURCE_TYPE
#doubts about if refer to the list or to the individual file
info["SourceName"] = self.sourceName
info["Key"] = key
#specific info of interest
info['FileName'] = sourceObject.FileName
info["rows"] = info['Dim_2']
info["cols"] = info['Dim_1']
info["type"] = info['DataType']
if 'MCA start ch' in info:
info['Channel0'] = int(info['MCA start ch'])
else:
info['Channel0'] = 0
if not ( 'McaCalib' in info):
if ('MCA a' in info) and\
('MCA b' in info) and\
('MCA c' in info):
info['McaCalib'] = [float(info['MCA a']),
float(info['MCA b']),
float(info['MCA c'])]
else:
info['McaCalib'] = [ 0.0, 1.0, 0.0]
else:
if type(info['McaCalib']) in [type(" ")]:
info['McaCalib'] = info['McaCalib'].replace("[","")
info['McaCalib'] = info['McaCalib'].replace("]","")
cala, calb, calc = info['McaCalib'].split(",")
info['McaCalib'] = [float(cala),
float(calb),
float(calc)]
self.__lastKeyInfo[key] = os.path.getmtime(sourceObject.FileName)
return info
def getDataObject(self,key,selection=None):
"""
selection: a dictionary with the keys pos and size: (x), (x,y) or (x,y,z)
tuples defining a roi
If not defined, takes full array
"""
sourcekeys = self.getSourceInfo()['KeyList']
#a key corresponds to an image
key_split= key.split(".")
image_key= key_split[0]+"."+key_split[1]
if image_key not in sourcekeys:
#if image_key == "0.0":
# #this is in fact a special selection: The SUM
# pass
#else:
raise KeyError("Key %s not in source keys" % image_key)
#create data object
data = DataObject.DataObject()
data.info = self.__getKeyInfo(image_key)
data.info ['selection'] = selection
data.info['selectiontype'] = "2D"
index = key_split[0]
image = key_split[1]
index = int(index)-1
image = int(image)-1
MCAIMP = 0
if len(key_split) == 4:
_logger.debug("mca like selection")
#print data.info
if 1:
MCAIMP = 1
if key_split[2].upper() == 'R':
pos = (0, int(key_split[3]))
size = (int(data.info['Dim_1']), 1)
elif key_split[2].upper() == 'C':
pos = (int(key_split[3]), 0)
size = (1,int(data.info['Dim_2']))
data.info['selectiontype'] = "1D"
else:
_logger.debug("mca like selection not yet implemented")
pos = None
size = None
data.info['selectiontype'] = "1D"
elif selection is None:
pos = None
size = None
else:
if "pos" in selection:
data.info["pos"]=selection['pos']
else:
data.info['pos']=None
if "size" in selection:
data.info["size"]=selection['size']
else:
data.info['size']=None
pos = data.info['pos']
size = data.info['size']
sourceObject = self._sourceObjectList[index]
data.data=sourceObject.GetData(image,Pos=pos,Size=size)
data.info['rows'], data.info['cols'] = data.data.shape[0:2]
if data.info['selectiontype'] == "1D":
if MCAIMP:
data.y = [numpy.ravel(data.data[:]).astype(numpy.float64)]
else:
if key_split[2].upper() == 'C':
data.y=[data.data[:,int(key_split[3])-1].astype(numpy.float64)]
elif key_split[2].upper() == 'R':
data.y=[data.data[int(key_split[3])-1, :].astype(numpy.float64)]
else:
raise ValueError("Unknown key %s" % key)
ch0 = int(data.info['Channel0'])
data.x = [ch0+numpy.arange(len(data.y[0])).astype(numpy.float64)]
data.m = None
data.data = None
#print "data.x.shape ", data.x[0].shape
#print "data.y.shape ", data.y[0].shape
return data
def isUpdated(self, sourceName, key):
#sourceName is redundant?
index,image = key.split(".")
index = int(index)-1
lastmodified = os.path.getmtime(self.__sourceNameList[index])
if lastmodified != self.__lastKeyInfo[key]:
self.__lastKeyInfo[key] = lastmodified
return True
else:
return False
source_types = { SOURCE_TYPE: EdfFileDataSource}
def DataSource(name="", source_type=SOURCE_TYPE):
try:
sourceClass = source_types[source_type]
except KeyError:
#ERROR invalid source type
raise TypeError("Invalid Source Type, source type should be one of %s" %\
source_types.keys())
return sourceClass(name)
if __name__ == "__main__":
import time
try:
sourcename=sys.argv[1]
key =sys.argv[2]
except:
_logger.error("Usage: EdfFileDataSource <file> <key>")
sys.exit()
#one can use this:
obj = EdfFileDataSource(sourcename)
#or this:
obj = DataSource(sourcename)
#data = obj.getData(key,selection={'pos':(10,10),'size':(40,40)})
#data = obj.getDataObject(key,selection={'pos':None,'size':None})
t0 = time.time()
data = obj.getDataObject(key,selection=None)
print("elapsed = ",time.time() - t0)
print("info = ",data.info)
if data.data is not None:
print("data shape = ",data.data.shape)
print(numpy.ravel(data.data)[0:10])
else:
print(data.y[0].shape)
print(numpy.ravel(data.y[0])[0:10])
data = obj.getDataObject('1.1',selection=None)
r = int(key.split('.')[-1])
print(" data[%d,0:10] = " % (r-1),data.data[r-1 ,0:10])
print(" data[0:10,%d] = " % (r-1),data.data[0:10, r-1])
|