COMPASS  5.0.0
End-to-end AO simulation tool using GPU acceleration
layers_test.py
1 """
2 Created on Wed Oct 5 14:28:23 2016
3 
4 @author: fferreira
5 """
6 import sys, os
7 import numpy as np
8 import h5py
9 import matplotlib.pyplot as plt
10 import matplotlib
11 plt.ion()
12 from guardians import gamora, drax
13 
14 datapath = "/home/fferreira/Data/"
15 fname_layers = "roket_8m_12layers_gamma1.h5" # File with all layers
16 buferr_ref = drax.get_err(datapath + fname_layers)
17 f_layers = h5py.File(datapath + fname_layers)
18 nlayers = f_layers.attrs["_Param_atmos__nscreens"]
19 
20 fname_layer_i = []
21 name = "roket_8m_12layers"
22 for i in range(nlayers):
23  fname_layer_i.append(name + "_%d.h5" % (i))
24 
25 files = []
26 for f in fname_layer_i:
27  files.append(h5py.File(datapath + f))
28 
29 print("--------------------------------------------")
30 print("file ", fname_layers, " :")
31 print(" nlayers : ", f_layers.attrs["_Param_atmos__nscreens"])
32 print(" frac : ", f_layers.attrs["_Param_atmos__frac"])
33 print("--------------------------------------------")
34 
35 nmodes = f_layers["P"][:].shape[0]
36 contributors = [
37  "tomography", "bandwidth", "non linearity", "noise", "filtered modes", "aliasing"
38 ]
39 Lambda_tar = f_layers.attrs["_Param_target__Lambda"][0]
40 fracs = f_layers.attrs["_Param_atmos__frac"]
41 alts = f_layers.attrs["_Param_atmos__alt"]
42 frac_per_layer = dict()
43 i = 0
44 for a in alts:
45  frac_per_layer[a] = fracs[i]
46  i += 1
47 
48 frac = []
49 buferr_layers = drax.get_err(datapath + fname_layer_i[0]) * 0.
50 for k in range(len(files)):
51  frac.append(frac_per_layer[files[k].attrs["_Param_atmos__alt"][0]])
52  buferr_layers += drax.get_err(datapath + fname_layer_i[k]) * np.sqrt(
53  frac_per_layer[files[k].attrs["_Param_atmos__alt"][0]])
54 
55 C_layers = np.zeros((buferr_layers.shape[0], buferr_layers.shape[0]))
56 for k in range(len(files)):
57  C_layers += (
58  frac[k] * drax.get_covmat_contrib(datapath + fname_layer_i[k], contributors))
59 print("contributors : ", contributors)
60 
61 # Column 1 : with correlation, column 2 : independence assumption
62 err_layers = np.zeros((nmodes, 2))
63 
64 err_layer_i = np.zeros((nmodes, 2 * nlayers))
65 
66 err_layers[:, 0] = drax.variance(f_layers, contributors, method="Default")
67 err_layers[:, 1] = drax.variance(f_layers, contributors, method="Independence")
68 l = 0
69 for f in files:
70  err_layer_i[:, l] = drax.variance(f, contributors, method="Default")
71  err_layer_i[:, l + 1] = drax.variance(f, contributors, method="Independence")
72  l += 2
73 
74 #err_layer1p2 = varianceMultiFiles([f_layer1,f_layer2], frac_per_layer, contributors)
75 inderr = np.zeros(nmodes)
76 derr = np.zeros(nmodes)
77 for l in range(nlayers):
78  inderr += frac[l] * err_layer_i[:, 2 * l + 1]
79  derr += frac[l] * err_layer_i[:, 2 * l]
80 
81 otftel_ref, otf2_ref, psf_ref, gpu = gamora.psf_rec_Vii(datapath + fname_layers)
82 otftel_sum, otf2_sum, psf_sum, gpu = gamora.psf_rec_Vii(datapath + fname_layers,
83  err=buferr_layers)
84 
85 # Plots
86 plt.figure(1)
87 plt.subplot(2, 1, 1)
88 plt.semilogy(err_layers[:, 1])
89 plt.semilogy(inderr)
90 plt.legend(["%d layers" % nlayers, "Layers sum"])
91 plt.xlabel("Modes #")
92 plt.ylabel("Variance [mic^2]")
93 plt.title("Variance with independence assumption")
94 plt.subplot(2, 1, 2)
95 plt.plot(drax.cumulativeSR(err_layers[:, 1], Lambda_tar))
96 plt.plot(drax.cumulativeSR(inderr, Lambda_tar))
97 plt.legend(["%d layers" % nlayers, "Layers sum"])
98 plt.xlabel("Modes #")
99 plt.ylabel("SR")
100 plt.title("Resulting SR")
101 
102 plt.figure(2)
103 plt.subplot(2, 1, 1)
104 plt.semilogy(err_layers[:, 0])
105 plt.semilogy(derr)
106 plt.legend(["%d layers" % nlayers, "Layers sum"])
107 plt.xlabel("Modes #")
108 plt.ylabel("Variance [mic^2]")
109 plt.title("Variance with correlation")
110 plt.subplot(2, 1, 2)
111 plt.plot(drax.cumulativeSR(err_layers[:, 0], Lambda_tar))
112 plt.plot(drax.cumulativeSR(derr, Lambda_tar))
113 plt.legend(["%d layers" % nlayers, "Layers sum"])
114 plt.xlabel("Modes #")
115 plt.ylabel("SR")
116 plt.title("Resulting SR")
117 
118 RASC = 180 / np.pi * 3600.
119 #pixsize = (Lambda_tar*1e-6 / 8. * RASC) * 16./64.
120 #lambda/(Nfft*pupdiam/D)
121 pixsize = Lambda_tar * 1e-6 / (psf_ref.shape[0] * 8. / 640) * RASC
122 x = (np.arange(psf_ref.shape[0]) - psf_ref.shape[0] / 2) * pixsize / (
123  Lambda_tar * 1e-6 / 8. * RASC)
124 font = {'family': 'normal', 'weight': 'bold', 'size': 22}
125 
126 matplotlib.rc('font', **font)
127 
128 plt.figure()
129 plt.semilogy(x, psf_ref[psf_ref.shape[0] / 2, :], color="blue")
130 plt.semilogy(x, psf_sum[psf_sum.shape[0] / 2, :], color="red")
131 plt.xlabel("Angle [units of lambda/D]")
132 plt.ylabel("Normalized intensity")
133 plt.legend(["12-layers PSF", "Sum of 12 layers PSF"])