forked from NSLS-II/lsdc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
albulaUtils.py
122 lines (105 loc) · 3.57 KB
/
albulaUtils.py
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
try:
import dectris.albula
except ImportError as e:
print('albula library import error: %s' %e)
import logging
logger = logging.getLogger(__name__)
logger.info('reading albulaUtils')
from functools import singledispatch
from time import sleep
import h5py
from pathlib import Path
global albulaFrame, albulaSubFrame, currentMasterH5
albulaFrame = None
albulaSubframeFrame = None
currentMasterH5 = None
imgSeries = None
global seriesDict
seriesDict = {}
def startup_albula():
global albulaFrame,albulaSubFrame
if (albulaFrame == None or albulaSubFrame == None):
logger.debug('starting up albula')
albulaFrame = dectris.albula.openMainFrame()
albulaFrame.disableClose()
albulaSubFrame = albulaFrame.openSubFrame()
albulaSubFrame.setColorMode("Heat")
def albulaClose(): #not used
global albulaFrame,albulaSubFrame
if (albulaSubFrame != None):
albulaSubFrame.close()
if (albulaFrame != None):
albulaFrame.close()
def albulaDispImage(Dimage):
global albulaFrame,albulaSubFrame
startup_albula()
try:
albulaSubFrame.loadImage(Dimage)
except dectris.albula.DNoObject:
albulaFrame = dectris.albula.openMainFrame()
albulaSubFrame = albulaFrame.openSubFrame()
albulaSubFrame.loadImage(Dimage)
def albulaConv(filename,imgNum=1):
series = dectris.albula.DImageSeries()
series.open(filename)
img = series[imgNum]
dectris.albula.DImageWriter.write(img,"testOut.tif")
def albulaDispH5(filename,imgNum=1):
if not (filename in seriesDict.keys()):
seriesDict[filename] = dectris.albula.DImageSeries(filename)
img = seriesDict[filename][imgNum]
albulaDispImage(img)
@singledispatch
def albulaDispFile(filename):
print(type(filename))
raise Exception("type not supported, only str, list, or tuple")
@albulaDispFile.register(str)
def _albulaDispFile(filename):
global albulaFrame,albulaSubFrame,currentMasterH5
startup_albula()
try:
logger.info('loading file %s'% filename)
albulaSubFrame.loadFile(filename)
currentMasterH5 = ""
except dectris.albula.DNoObject:
albulaFrame = dectris.albula.openMainFrame()
albulaSubFrame = albulaFrame.openSubFrame()
albulaSubFrame.loadFile(filename)
@albulaDispFile.register(tuple)
@albulaDispFile.register(list)
def _albulaDispFile(filename):
global albulaFrame,albulaSubFrame,currentMasterH5,imgSeries
startup_albula()
try:
if not (currentMasterH5 == filename[0]):
logger.info('reading file: %s' % filename[0])
albulaSubFrame.loadFile(filename[0])
currentMasterH5 = filename[0]
sleep(0.5) # Sleep to allow Albula to load file. Otherwise the following goTo() is ignored
logger.debug('reading image number %s' % filename[1])
albulaSubFrame.goTo(filename[1])
except dectris.albula.DNoObject:
albulaFrame = dectris.albula.openMainFrame()
albulaSubFrame = albulaFrame.openSubFrame()
imgSeries = dectris.albula.DimageSeries(filename[0])
albulaSubFrame.loadImage(imgSeries[filename[1]])
currentMasterH5 = filename[0]
except Exception as e:
logger.error(f'Albula exception: {e}')
def validate_master_HDF5_file(filename):
"""
Validate master HDF5 by checking if data files exist and can be read
"""
path = Path(filename)
if not path.exists():
return False
try:
if 'master' in path.stem and path.suffix == '.h5':
with h5py.File(path) as f:
for key in f['entry']['data'].keys():
f['entry']['data'][key]
return True
else:
return False
except KeyError:
return False