-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslitsOperations.py
116 lines (101 loc) · 4.44 KB
/
slitsOperations.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
from SEDSS.SEDSupplements import CLIMessage
from SEDSS.SEDSupport import readFile
from MSDataWriter import MSDataWriter
from PIL import Image
import numpy as np
import glob
import os
import re
import log
import ntpath
import math
class slitsOperations:
def __init__(self, imgFullPath, tTheta, metadata, theta = None):
"""
This class does the follwoing:
- reads configuration file
- initiate some parameters
- read the image collected from detector
- calls the function that calculates 2ϴ on the detector
- calls the function that calculates the intinsity avrage of the slites on the detector
- if theta is being provided to the slitsOperations class, then it will be written in the exp file.
"""
self.metadata = metadata
#CLIMessage("{}".format(self.metadata))
path, fileName = ntpath.split(imgFullPath) # extract image file name and path
log.info("Initalizing slitsOperations class for image: {}".format(fileName))
self.imgFullPath = imgFullPath # image full path
self.tTheta = tTheta # 2ϴ
self.theta = theta #ϴ
scanToolconfigFile = self.metadata["ScanToolCFGFile"]
self.configFile = readFile(scanToolconfigFile).readJSON() # reading conigration file
self.XAxisRange = self.configFile["slitsConfiguration"]["XAxisRange"] # range value on x axis of the detector
self.X = self.configFile["slitsConfiguration"]["X"] # Center of the Slit on X axis
self.Y = self.configFile["slitsConfiguration"]["Y"] # Slits positions on Y axis
self.R = self.configFile["slitsConfiguration"]["sampleToDetDistance"] # Sample to detector distance.
self.twoThetaOff = self.configFile["slitsConfiguration"]["2thetaOff"] # 2ϴ offset.
self.initZeroPixPos = self.configFile["slitsConfiguration"]["initZeroPixPos"] # direct pixel position when detector at 0 angle
self.data = {}
self.slitsPixelIntinisty = []
self.slitsPixelIntinistyAvr = 0
self.twoThetaOnSlit = 0
log.info("Local image path: {}".format(self.imgFullPath))
# self.readImage()
# self.calc2ThetaSlitIntinsity()
try:
self.readImage()
self.calc2ThetaSlitIntinsity()
except:
log.error("unable to read or handel the image: {}".format(self.imgFullPath))
log.warning("one image has been ignored!!")
CLIMessage("Unable to collect image from the source or handling it!!", "E")
pass
# self.readImage()
# self.calc2ThetaSlitIntinsity()
def readImage(self):
log.info("Reading the image ...")
im=Image.open(self.imgFullPath)
#CLIMessage ("{}".format(im), "I")
#CLIMessage("Image format : {}".format(im.format), "I")
#CLIMessage ("Image size: {}".format(im.mode), "I")
log.info("Converting image to nparray")
self.imageArray = np.asarray(im, dtype=np.int32)
log.info("Deleting the raw image")
try:
os.remove(self.imgFullPath)
except:
CLIMessage("unable to delete the raw image {}".format(imgFullPath), "W")
"""
image dimintions: 487x619 (x,y)
after converting to nparray it is 619x487 (y,x)
the detector is rotated 90 degrees (y,x)
"""
log.info("Slit X position: {}, X Range: {}-{}".format(self.X, self.X - self.XAxisRange,
self.X + self.XAxisRange))
def calc2ThetaSlitIntinsity(self):
"""
This method calcultes:
- the two theta on the detector
- the avrage intinsity of a givien slit
"""
self.data = {}
for i in range(len(self.Y)):
self.slitsPixelIntinisty = []
for j in range((self.X - self.XAxisRange), (self.X + self.XAxisRange)+1): # range starts from (-x to x )
self.slitsPixelIntinisty.append(self.imageArray[j, self.Y[i]])
self.slitsPixelIntinistyAvr = sum(self.slitsPixelIntinisty)/len(self.slitsPixelIntinisty)
#self.twoThetaOnSlit = self.tTheta + (3.170 - (self.Y[i] * 0.0133))
self.twoThetaOnSlit = self.tTheta + self.twoThetaOff + math.atan (((self.initZeroPixPos - self.Y[i]) * 0.172 )/self.R) * 180 / math.pi
self.data["slitID"] = i
self.data["slitYPosition"] = self.Y[i]
self.data["slitXPosition"] = self.X
self.data["slitXRange"] = self.XAxisRange
self.data["twoThetaOnSlit"] = self.twoThetaOnSlit
self.data["slitsPixelIntinistyAvr"] = self.slitsPixelIntinistyAvr
if self.theta != None:
self.data["theta"] = self.theta
log.info("Writing collected data in the experimental file.")
MSDataWriter(self.data, self.metadata)
log.info("SlitID#: {}, Y position: {},"\
" Slit pixels intinsity: {}, 2ϴ on slit: {}, pixels intinsity averege: {}".
format(i, self.Y[i],self.slitsPixelIntinisty, self.twoThetaOnSlit, self.slitsPixelIntinistyAvr))