/
utilMask.py
801 lines (640 loc) · 24.3 KB
/
utilMask.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 6 10:40:11 2021
@author: jerome
"""
import numpy as np
import matplotlib.pyplot as plt
from wpg.generators import build_gauss_wavefront
from wpg.wavefront import Wavefront
from PIL import Image
from PIL import ImageDraw
import cv2
# plt.style.reload_library()
# plt.style.use(['science','no-latex'])#,'ieee'])
# %%
def gratingEfficiencyDEL(m,beta,delta,d,k,b,theta = 0):
""" DOES NOT WORK - NEED TO CHECK """
''''
Return the diffraction efficiency of X-rays incident on an arbitrary grating
with rectangular wire cross sections (method from Delvaille et al. (1980)):
m: order of diffracted beam
beta: imaginary part of refractive index
delta: (1-real part) of refractive index
d: grating periodicity
k: photon energy in units of (hbar*c)
b: thickness of mask
theta: incident angle
'''
a = d # square grating
phi = (2*np.pi*m)/(d*np.cos(theta))
print("phi: {}".format(phi))
X_0 = 0
X_1 = a*np.cos(theta) - b*np.sin(theta)
X_2 = a*np.cos(theta)
X_3 = d*np.cos(theta) - b*np.sin(theta)
X_4 = d*np.cos(theta)
Z_0 = 0
Z_1 = 0
Z_2 = b/np.cos(theta)
Z_3 = b/np.cos(theta)
C_0 = 0
C_1 = 1/(np.sin(theta)*np.cos(theta))
C_2 = 0
C_3 = -1/(np.sin(theta)*np.cos(theta))
R_0 = (1/k)*(((C_0*k*beta)**2 + (k*C_0*delta + phi)**2)**(1/2))
R_1 = (1/k)*(((C_1*k*beta)**2 + (k*C_1*delta + phi)**2)**(1/2))
R_2 = (1/k)*(((C_2*k*beta)**2 + (k*C_2*delta + phi)**2)**(1/2))
R_3 = (1/k)*(((C_3*k*beta)**2 + (k*C_3*delta + phi)**2)**(1/2))
F_0 = (1/k*R_0)*np.exp(-1j*np.cos((beta*C_0)/R_0)+1j*k*(Z_0 - C_0 * X_0)*(delta + 1j*beta))*(np.exp(1j*X_1*(k*C_0*(delta+1j*beta)+phi))-(np.exp(1j*X_0*(k*C_0*(delta+1j*beta)+phi))))
F_1 = (1/k*R_1)*np.exp(-1j*np.cos((beta*C_1)/R_1)+1j*k*(Z_1 - C_1 * X_1)*(delta + 1j*beta))*(np.exp(1j*X_2*(k*C_1*(delta+1j*beta)+phi))-(np.exp(1j*X_1*(k*C_1*(delta+1j*beta)+phi))))
F_2 = (1/k*R_2)*np.exp(-1j*np.cos((beta*C_2)/R_2)+1j*k*(Z_2 - C_2 * X_2)*(delta + 1j*beta))*(np.exp(1j*X_3*(k*C_2*(delta+1j*beta)+phi))-(np.exp(1j*X_2*(k*C_2*(delta+1j*beta)+phi))))
F_3 = (1/k*R_3)*np.exp(-1j*np.cos((beta*C_3)/R_3)+1j*k*(Z_3 - C_3 * X_3)*(delta + 1j*beta))*(np.exp(1j*X_4*(k*C_3*(delta+1j*beta)+phi))-(np.exp(1j*X_3*(k*C_3*(delta+1j*beta)+phi))))
print("F_0: {}".format(F_0))
print("F_1: {}".format(F_1))
print("F_2: {}".format(F_2))
print("F_3: {}".format(F_3))
n = (abs(F_0 + F_1 + F_2 + F_3)**2)*((d*np.cos(theta))**(-2))
print("Diffraction Efficiency [n]: {}".format(n))
return n
# %%
def gratingEfficiencyHARV(m,b,d,G = 0):
''''
Return the theoretical diffraction efficiency of X-rays incident on an arbitrary grating
with rectangular wire cross sections (method from Harvey et al (2019)):
m: order of diffracted beam
b: slit size
d: grating periodicity
G: grating type - 0 = amplitude grating, 1 = phase grating
'''
if G == 0:
n = ((b**2)/(d**2))*(np.sinc((m*b)/d)**2)
elif G == 1:
if b != d/2:
print("Error: Ratio of slit size to periodicity must be 1:2 for phase grating")
import sys
sys.exit()
else :
n = np.sinc(m/2)**2
# print("absolute grating efficiency: {}".format(n))
return n
# %%
def plotEfficiency():
ampEff=[]
phaEff=[]
pEff_0 = []
pEff_1 = []
pEff_2 = []
pEff_3 = []
pEff_4 = []
sEff_0 = []
sEff_1 = []
sEff_2 = []
sEff_3 = []
sEff_4 = []
Eff_0 = []
Eff_1 = []
Eff_2 = []
Eff_3 = []
Eff_4 = []
oEff = []
period = np.array(range(1,200))
# print("period: {}".format(period))
slit = np.array(range(1,200))
order = range(1,5)
for p in period:
E1 = gratingEfficiencyHARV(1,100e-9,1e-9*p,G=0)
pEff_1.append(E1)
E2 = gratingEfficiencyHARV(2,100e-9,1e-9*p,G=0)
pEff_2.append(E2)
E3 = gratingEfficiencyHARV(3,100e-9,1e-9*p,G=0)
pEff_3.append(E3)
E4 = gratingEfficiencyHARV(4,100e-9,1e-9*p,G=0)
pEff_4.append(E4)
# plt.plot(period, pEff_0, label = "m=0")
plt.plot(period, pEff_1, label = "m=1")
plt.plot(period, pEff_2, label = "m=2")
plt.plot(period, pEff_3, label = "m=3")
plt.plot(period, pEff_4, label = "m=4")
plt.title("\u03B7\u2098 vs W\u209A (W\u209B=100 nm)")
plt.xlabel("Period Width (W\u209A) [nm]")
plt.ylabel("Grating Efficiency (\u03B7\u2098)")
plt.legend()
plt.show()
for s in slit:
# E0 = gratingEfficiencyHARV(0,1e-9*s,100e-9,G=0)
# sEff_0.append(E0)
E1 = gratingEfficiencyHARV(1,1e-9*s,100e-9,G=0)
sEff_1.append(E1)
E2 = gratingEfficiencyHARV(2,1e-9*s,100e-9,G=0)
sEff_2.append(E2)
E3 = gratingEfficiencyHARV(3,1e-9*s,100e-9,G=0)
sEff_3.append(E3)
E4 = gratingEfficiencyHARV(4,1e-9*s,100e-9,G=0)
sEff_4.append(E4)
# plt.plot(slit,sEff_0, label="m=0")
plt.plot(slit,sEff_1, label="m=1")
plt.plot(slit,sEff_2, label="m=2")
plt.plot(slit,sEff_3, label="m=3")
plt.plot(slit,sEff_4, label="m=4")
plt.title("\u03B7\u2098 vs W\u209B (W\u209A=100 nm)")
plt.xlabel("Slit Width (W\u209B) [nm]")
plt.ylabel("Grating Efficiency (\u03B7\u2098)")
plt.legend()
plt.show()
for s in slit:
E0 = gratingEfficiencyHARV(0,1e-9*s,1e-9*p,G=0)
Eff_0.append(E0)
E1 = gratingEfficiencyHARV(1,1e-9*s,1e-9*p,G=0)
Eff_1.append(E1)
E2 = gratingEfficiencyHARV(2,1e-9*s,1e-9*p,G=0)
Eff_2.append(E2)
E3 = gratingEfficiencyHARV(3,1e-9*s,1e-9*p,G=0)
Eff_3.append(E3)
E4 = gratingEfficiencyHARV(4,1e-9*s,1e-9*p,G=0)
Eff_4.append(E4)
plt.plot(slit*0.005,Eff_0, label="m=0")
plt.plot(slit*0.005,Eff_1, label="m=1")
plt.plot(slit*0.005,Eff_2, label="m=2")
plt.plot(slit*0.005,Eff_3, label="m=3")
plt.plot(slit*0.005,Eff_4, label="m=4")
plt.title("\u03B7\u2098 vs W\u209B/W\u209A")
plt.yscale("log")
plt.ylim(1e-4,1)
plt.xlabel("W\u209B/W\u209A")
plt.ylabel("Grating Efficiency (\u03B7\u2098)")
plt.legend()
plt.show()
print("Shape of Eff_0: {}".format(np.shape(Eff_0)))
print("Shape of Eff_1: {}".format(np.shape(Eff_1)))
print("Shape of Eff_2: {}".format(np.shape(Eff_2)))
print("Shape of Eff_3: {}".format(np.shape(Eff_3)))
print("Shape of Eff_4: {}".format(np.shape(Eff_4)))
tEff = np.array(Eff_0)+np.array(Eff_1)+np.array(Eff_2)+np.array(Eff_3)+np.array(Eff_4)
totEff = np.array(Eff_1)+np.array(Eff_2)+np.array(Eff_3)+np.array(Eff_4)
print("Shape of totEff: {}".format(np.shape(totEff)))
plt.plot(slit*0.005,totEff, label = "m>0")
plt.plot(slit*0.005,tEff, label = "m≥0")
plt.plot(slit*0.005,Eff_0, label = "m=0")
plt.title("\u03B7\u209C\u2092\u209C vs W\u209B/W\u209A")
plt.xlabel("W\u209B/W\u209A")
plt.ylabel("Total Grating Efficiency (\u03B7\u209C\u2092\u209C)")
plt.legend()
plt.show()
for o in order:
E = gratingEfficiencyHARV(o,1e-9,2e-9,G=1)
oEff.append(E)
ooEff=np.array([oEff[0],
oEff[0]+oEff[1],
oEff[0]+oEff[1]+oEff[2],
oEff[0]+oEff[1]+oEff[2]+oEff[3]])
print("Shape of oEff: {}".format(np.shape(oEff)))
print("Shape of ooEff: {}".format(np.shape(ooEff)))
plt.plot(order,oEff, label = "\u03B7\u2098")
plt.plot(order,ooEff, label = "\u03B7\u209C\u2092\u209C")
plt.title("\u03B7 vs m (phase grating)")
plt.xlabel("Diffraction Order (m)")
plt.ylabel("Grating Efficiency (\u03B7)")
plt.legend()
plt.show()
# plt.plot(period, pEff, label="period (slit=100nm)")
# plt.plot(slit, sEff, label="slit (period=200nm)")
# plt.title("Grating Efficiency")
# plt.xlabel("Size of slit/period [nm]")
# plt.ylabel("Grating Efficiency (m=1)")
# plt.legend()
# plt.show()
# for p in period:
# for s in slit:
# E = gratingEfficiencyHARV(1,1e-9*s,1e-9*p,G=0)
# ampEff.append(E)
# print("Shape of ampEff: {}".format(np.shape(ampEff)))
# # s = np.reshape(stk.arS,(4,stk.mesh.nx,stk.mesh.ny))
# plt.plot(ampEff)
# plt.show()
# # Eff = np.reshape(ampEff,(3,)))
# # lamda = 6.7e-9
# # p = 200e-9
# # # m = 10
# # NA = 0.1 #m*lamda/p
# # # print("NA: {}".format(NA))
# # m = NA*p/lamda
# # print("m: {}".format(m))
# %%
def getImageData(filename):
im = cv2.imread(filename, cv2.IMREAD_ANYDEPTH ) # open any bit depth image (more readable than -1)
# show each image (can comment out for speed)
#plt.imshow(im)
#plt.show()
# convert each image to array
im = np.array(im)
return im
# %%
def get_rect(x, y, width, height, angle):
rect = np.array([(0, 0), (width, 0), (width, height), (0, height), (0, 0)])
theta = (np.pi / 180.0) * angle
R = np.array([[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)]])
offset = np.array([x, y])
transformed_rect = np.dot(rect, R) + offset
return transformed_rect
# %%
def getOrders(I, m):
## DOESNT WORK ##
"""
I : Intensity array
m : maximum order to be analysed
"""
s = np.shape(I)
print("shape of intensity array: {}".format(s))
N_ord = int((2*m)+1)
# import image_slicer
# image_slicer.slice(I, N_ord)
# # M = np.reshape(I,(N_ord,
# # (s[0])/N_ord,
# # (s[1])/N_ord ))
if s[0]/N_ord != int:
newshape = int(s[0]/N_ord)*N_ord
diff = s[0] - newshape
print("difference in shape: {}".format(diff))
Inew = I[0:s[1],int(diff/2):int(newshape+diff/2)]
plt.imshow(Inew)
plt.title("Inew")
print("New shape of I: {}".format(np.shape(Inew)))
print("New size of I: {}".format(np.size(Inew)))
M = np.reshape(Inew, (N_ord, int(s[1]), int((s[0]/N_ord))))
else:
M = np.reshape(I, (N_ord, int(s[1]), int((s[0]/N_ord))))
print("New shape of intensity array: {}".format(np.shape(M)))
plt.imshow(M[0])
plt.title("test")
plt.show()
for i in range(N_ord):
Mi = M[i,:,:]
plt.imshow(Mi)
plt.title(i)
plt.show()
# for i in 2*m + 1:
# m_i = np.reshape(M[i],((s2[0])/(2*m+1),(s2[1])/(2*m+1)))
# print(m_i)
# return m_i
# return i
# %%
def getFarField(D,lam):
"""
"""
r = (D**2)/(4*lam)
print("Distance to Far-field [m]: {}".format(r))
return r
# %%
def getEfficiency(path1, path2, path3, m = 1, pickle = 1, pathm0 = None, pathm1 = None, pathm2 = None): #, intV = 300):
""" Get the diffraction efficiency of a mask from intensity before mask,
at exit plane & after propagation.
params:
path1: intensity/wavefield before mask
path2: intensity/wavefield at mask exit plane
path3: intensity/wavefield after propagation
m: maximum order to be analysed (accepts 0<m<5)
pickle: input to be analysed. 0 = intensity tif files, 1 = wavefield pickle files
pathm0: Save path for m=0 order intensity
pathm1: Save path for m=1 order intensity
pathm2: Save path for m=2 order intensity
returns:
efficiency of each order up to maximum """
from wfAnalyseWave import pixelsize
if pickle == 1:
import pickle
with open(path1, 'rb') as wav:
w1 = pickle.load(wav)
with open(path2, 'rb') as wav:
w2 = pickle.load(wav)
with open(path3, 'rb') as wav:
w3 = pickle.load(wav)
wf1 = Wavefront(srwl_wavefront=w1)
wf2 = Wavefront(srwl_wavefront=w2)
wf3 = Wavefront(srwl_wavefront=w3)
p1 = pixelsize(wf1)
p2 = pixelsize(wf2)
p3 = pixelsize(wf3)
pR1 = p1[0]/p2[0]
pR2 = p1[0]/p3[0]
pR3 = p2[0]/p3[0]
print("pixel size at mask [m]: {}".format(p1))
print("pixel size after mask [m]: {}".format(p2))
print("pixel size after propagation [m]: {}".format(p3))
print("ratio of pixel sizes (p1/p2): {}".format(pR1))
print("ratio of pixel sizes (p1/p3): {}".format(pR2))
print("ratio of pixel sizes (p2/p3): {}".format(pR3))
""" Intensity from wavefield """
I0 = wf1.get_intensity()
I1 = wf2.get_intensity()
I2 = wf3.get_intensity()
""" Total intensity at each plane """
I0_tot = np.sum(I0)/(p1[0]*p1[1]) #*p1[0]#6.25e-09*s0[0]*s0[1]
I1_tot = np.sum(I1)/(p2[0]*p2[1]) #*p2[0]#*s1[0]*s1[1]
I2_tot = np.sum(I2)/(p3[0]*p3[1]) #*p3[0]#*s2[0]*s1[1]
else:
""" Intensity from tif file """
I0 = path1 #getImageData("/home/jerome/Documents/MASTERS/data/wavefields/Efficiency/intensityIN.tif")
I1 = path2 #getImageData('/home/jerome/Documents/MASTERS/data/wavefields/Efficiency/intensityEX_1-2.tif')
I2 = path3 #getImageData('/home/jerome/Documents/MASTERS/data/wavefields/Efficiency/intensityPR_1-2.tif') #getImageData('/home/jerome/WPG/intensityTot_maskprop.tif')
""" Total intensity at each plane """
I0_tot = np.sum(I0) #*p1[0]#6.25e-09*s0[0]*s0[1]
I1_tot = np.sum(I1) #*p2[0]#*s1[0]*s1[1]
I2_tot = np.sum(I2) #*p3[0]#*s2[0]*s1[1]
s0 = np.shape(I0)
s1 = np.shape(I1)
s2 = np.shape(I2)
print("Shape of I (at mask): {}".format(s0))
print("Shape of I (after mask): {}".format(s1))
print("Shape of I (after propagation): {}".format(s2))
F0 = s0[0]/s1[0]
F1 = s0[0]/s2[0]
F2 = s1[0]/s2[0]
print("pixel ratio (I0/I1): {}".format(F0))
print("pixel ratio (I0/I2): {}".format(F1))
print("pixel ratio (I1/I2): {}".format(F2))
if F0 != 1.0:
print("WARNING! Number of pixels in intensity files does not match! Efficiency values may not be accurate!")
if F1 != 1.0:
print("WARNING! Number of pixels in intensity files does not match! Efficiency values may not be accurate!")
if F2 != 1.0:
print("WARNING! Number of pixels in intensity files does not match! Efficiency values may not be accurate!")
Ir0 = (I1_tot/I0_tot)#(F0**2)*(I1_tot/I0_tot) # ratio of intensity before & after mask
Ir1 = (I2_tot/I0_tot) #(F1**2)*(I2_tot/I0_tot) # ratio of intensity before & after mask
Ir2 = (I2_tot/I1_tot) #(F2**2)*(I2_tot/I1_tot) # ratio of intensity before & after mask
print("Intensity Ratio I_ex/I_in: {}".format(Ir0))
print("Intensity Ratio I_prop/I_in: {}".format(Ir1))
print("Intensity Ratio I_prop/I_exit: {}".format(Ir2))
plt.imshow(I0)
plt.title("at mask")
plt.colorbar()
plt.show()
plt.imshow(I1)
plt.title("After mask")
plt.colorbar()
plt.show()
plt.imshow(I2)
plt.title("after propagation")
plt.colorbar()
plt.show()
print(" ")
print("-----Total Intensity-----")
print("At mask: {}".format(I0_tot))
print("After mask: {}".format(I1_tot))
print("After propagation: {}".format(I2_tot))
""" Defining region of interest to inspect separate orders """
Mi = int((s2[0]/2)-300) #initial position for order sampling
Mf = int((s2[0]/2)+300) #final position for order sampling
print("coordinates for start and end of each order: {}".format((Mi,Mf)))
"""Finding each order"""
intV = int(s2[0]/(2*m+1)) #500 # Number of pixels for segmentation interval
if m >= 1:
# region for m=0
ROI_0 = ((int((s2[0]/2)-(intV/2)),Mi),((int((s2[0]/2)+(intV/2))),Mf))
# region for m=+1
ROI_1 = ((ROI_0[1][0], Mi),(ROI_0[1][0] + intV, Mf))
# region for m=-1
ROI_n1 =((ROI_0[0][0]-intV, Mi),(ROI_0[0][0], Mf))
if m >= 2:
# region for m=+2
ROI_2 = ((ROI_1[1][0], Mi),(ROI_1[1][0] + intV, Mf))
# region for m=-2
ROI_n2 = ((ROI_n1[0][0]-intV, Mi),(ROI_n1[0][0], Mf))
if m >= 3:
# region for m=+3
ROI_3 = ((ROI_2[1][0], Mi),(ROI_2[1][0] + intV, Mf))
# region for m=-3
ROI_n3 = ((ROI_n2[0][0]-intV, Mi),(ROI_n2[0][0], Mf))
if m >= 4:
# region for m=+4
ROI_4 = ((ROI_3[1][0], Mi),(ROI_3[1][0] + intV, Mf))
# region for m=-4
ROI_n4 = ((ROI_n3[0][0]-intV, Mi),(ROI_n3[0][0], Mf))
x0_0,y0_0 = ROI_0[0][0], ROI_0[0][1]
x1_0,y1_0 = ROI_0[1][0], ROI_0[1][1]
x0_1,y0_1 = ROI_1[0][0], ROI_1[0][1]
x1_1,y1_1 = ROI_1[1][0], ROI_1[1][1]
x0_n1,y0_n1 = ROI_n1[0][0], ROI_n1[0][1]
x1_n1,y1_n1 = ROI_n1[1][0], ROI_n1[1][1]
try:
x0_2,y0_2 = ROI_2[0][0], ROI_2[0][1]
x1_2,y1_2 = ROI_2[1][0], ROI_2[1][1]
x0_n2,y0_n2 = ROI_n2[0][0], ROI_n2[0][1]
x1_n2,y1_n2 = ROI_n2[1][0], ROI_n2[1][1]
x0_3,y0_3 = ROI_3[0][0], ROI_3[0][1]
x1_3,y1_3 = ROI_3[1][0], ROI_3[1][1]
x0_n3,y0_n3 = ROI_n3[0][0], ROI_n3[0][1]
x1_n3,y1_n3 = ROI_n3[1][0], ROI_n3[1][1]
x0_4,y0_4 = ROI_4[0][0], ROI_4[0][1]
x1_4,y1_4 = ROI_4[1][0], ROI_4[1][1]
x0_n4,y0_n4 = ROI_n4[0][0], ROI_n4[0][1]
x1_n4,y1_n4 = ROI_n4[1][0], ROI_n4[1][1]
except NameError:
pass
A_0 = I2[y0_0:y1_0,x0_0:x1_0]
A_1 = I2[y0_1:y1_1,x0_1:x1_1]
A_n1 = I2[y0_n1:y1_n1,x0_n1:x1_n1]
try:
A_2 = I2[y0_2:y1_2,x0_2:x1_2]
A_n2 = I2[y0_n2:y1_n2,x0_n2:x1_n2]
A_3 = I2[y0_3:y1_3,x0_3:x1_3]
A_n3 = I2[y0_n3:y1_n3,x0_n3:x1_n3]
A_4 = I2[y0_4:y1_4,x0_4:x1_4]
A_n4 = I2[y0_n4:y1_n4,x0_n4:x1_n4]
except NameError:
pass
plt.imshow(A_0)
plt.title('m=0')
plt.colorbar()
if pathm0 != None:
print("Saving m=0 figure to path: {}".format(pathm0))
plt.savefig(pathm0)
plt.show()
plt.imshow(A_1)
plt.title('m=+1')
plt.colorbar()
if pathm1 != None:
print("Saving m=1 figure to path: {}".format(pathm1))
plt.savefig(pathm1)
plt.show()
plt.imshow(A_n1)
plt.title('m=-1')
plt.colorbar()
plt.show()
try:
plt.imshow(A_2)
plt.title('m=+2')
plt.colorbar()
if pathm2 != None:
print("Saving m=2 figure to path: {}".format(pathm2))
plt.savefig(pathm2)
plt.show()
plt.imshow(A_n2)
plt.title('m=-2')
plt.colorbar()
plt.show()
plt.imshow(A_3)
plt.title('m=+3')
plt.colorbar()
plt.show()
plt.imshow(A_n3)
plt.title('m=-3')
plt.colorbar()
plt.show()
plt.imshow(A_4)
plt.title('m=+4')
plt.colorbar()
plt.show()
plt.imshow(A_n4)
plt.title('m=-4')
plt.colorbar()
plt.show()
except NameError:
pass
Im_0 = np.sum(A_0)
Im_1 = np.sum(A_1)
Im_n1 = np.sum(A_n1)
try:
Im_2 = np.sum(A_2)/Ir2
Im_n2 = np.sum(A_n2)/Ir2
Im_3 = np.sum(A_3)/Ir2
Im_n3 = np.sum(A_n3)/Ir2
Im_4 = np.sum(A_4)/Ir2
Im_n4 = np.sum(A_n4)/Ir2
except NameError:
pass
print(" ")
print("----- Intensity of m = 0-----")
print("Im_1: {}".format(Im_0))
print(" ")
print("----- Intensity of m = +1-----")
print("Im_1: {}".format(Im_1))
print(" ")
print("----- Intensity of m = -1-----")
print("Im_n1: {}".format(Im_n1))
try:
print(" ")
print("----- Intensity of m = +2-----")
print("Im_2: {}".format(Im_2))
print(" ")
print("----- Intensity of m = -2-----")
print("Im_n2: {}".format(Im_n2))
print(" ")
print("----- Intensity of m = +3-----")
print("Im_3: {}".format(Im_3))
print(" ")
print("----- Intensity of m = -3-----")
print("Im_n3: {}".format(Im_n3))
print(" ")
print("----- Intensity of m = +4-----")
print("Im_4: {}".format(Im_4))
print(" ")
print("----- Intensity of m = -4-----")
print("Im_n4: {}".format(Im_n4))
except NameError:
pass
if pickle == 1:
""" Get Efficiency of each order """ # Not sure if should be dividing by total intensity at mask or after mask
E0 = (Im_0/I0_tot)/p3[0] #p3[0]*(Im_0/I0_tot)
E1 = (Im_1/I0_tot)/p3[0] # p3[0]*(Im_1/I0_tot)/p3[0] #
En1 = (Im_n1/I0_tot)/p3[0] # p3[0]*(Im_n1/I0_tot)/p3[0] #
try:
E2 = p3[0]*(Im_2/I0_tot)
En2 = p3[0]*(Im_n2/I0_tot)
E3 = p3[0]*(Im_3/I0_tot)
En3 = p3[0]*(Im_n3/I0_tot)
E4 = p3[0]*(Im_4/I0_tot)
En4 = p3[0]*(Im_n4/I0_tot)
except NameError:
pass
else:
""" Get Efficiency of each order """ # Not sure if should be dividing by total intensity at mask or after mask
E0 = (Im_0/I0_tot)
E1 = (Im_1/I0_tot)
En1 = (Im_n1/I0_tot)
try:
E2 = (Im_2/I0_tot)
En2 = (Im_n2/I0_tot)
E3 = (Im_3/I0_tot)
En3 = (Im_n3/I0_tot)
E4 = (Im_4/I0_tot)
En4 = (Im_n4/I0_tot)
except NameError:
pass
print(" ")
print("Efficiency of m=0 order: {}".format(E0))
print("Efficiency of m=+1 order: {}".format(E1))
print("Efficiency of m=-1 order: {}".format(En1))
try:
print("Efficiency of m=+2 order: {}".format(E2))
print("Efficiency of m=-2 order: {}".format(En2))
print("Efficiency of m=+3 order: {}".format(E3))
print("Efficiency of m=-3 order: {}".format(En3))
print("Efficiency of m=+4 order: {}".format(E4))
print("Efficiency of m=-4 order: {}".format(En4))
except NameError:
pass
# %%
def test():
""" Testing theoretical efficiency """
m=1 #order of diffracted beam
beta = 1.76493861*1e-3 #imaginary part of refractive index
delta = 1-(2.068231*1e-2) #(1-real part) of refractive index
d = 200e-9 #grating periodicity
k = 197.3 * 6.7 #photon energy in units of (hbar*c)
b = 72e-9
theta = 0 #np.pi/2 #incident angle
# print(" ")
# print("-----Grating efficiency (DEL)-----")
# gratingEfficiencyDEL(m, beta, delta, d, k, b,theta)
print(" ")
print("-----Grating efficiency (amplitude)-----")
gratingEfficiencyHARV(1,100e-9,200e-9,G=0)
print(" ")
print("-----Grating efficiency (phase)-----")
gratingEfficiencyHARV(1,100e-9,200e-9,G=1)
""" Testing simulated efficiency """
eMin = 1e8
Nx = 150
Ny = 150
Nz = 1
xMin = -10e-6
xMax = 10e-6
yMin = -10e-6
yMax = 10e-6
zMin = 100
# Fx = 1/2
# Fy = 1/2
print(" ")
print('Running Test:')
print('building wavefront...')
w = build_gauss_wavefront(Nx,Ny,Nz,eMin/1000,xMin,xMax,yMin,yMax,1,1e-6,1e-6,1)
wf0 = Wavefront(srwl_wavefront=w)
""" Intensity from test Gaussian """
# I = wf0.get_intensity()
directory = "/home/jerome/Documents/MASTERS/data/wavefields/Efficiency/"
""" Load pickled Wavefronts """
path1 = directory + "incident.pkl"
path2 = directory + "exit_TH10.pkl"
path3 = directory + "prop_TH10.pkl"
""" Intensity from tif file """
I0 = getImageData(directory + "intensityIN.tif")
I1 = getImageData(directory + "intensityEX_1-2.tif")
I2 = getImageData(directory + "intensityPR_1-2.tif") #getImageData('/hom
pathm0 = directory + "zeroOrder"
pathm1 = directory + "firstOrder"
pathm2 = directory + "secondOrder"
# getEfficiency(I0,I1,I2,3,0)#,540)
getEfficiency(path1,path2,path3,2,1, pathm0=pathm0)#,540)
# %%
def testReshape():
I = getImageData('/home/jerome/Documents/MASTERS/data/wavefields/Efficiency/intensityPR_1-4.tif')
plt.imshow(I)
plt.title("Intensity")
getOrders(I,3)
# %%
if __name__ == '__main__':
test()
# testReshape()