22
22
from wavefront import *
23
23
from deconvolution import *
24
24
from cost_func import *
25
+ import os
26
+ import processing
25
27
26
28
27
29
class patch_pd (object ):
28
- def __init__ (self ,pd_data ,Del ,co_num ,output_wf ,output_mtf ):
30
+ def __init__ (self ,pd_data ,Del ,co_num ,output_wf ,output_mtf , parallel = True ):
29
31
30
32
31
33
self .data = fits .getdata (pd_data )
@@ -42,15 +44,56 @@ def __init__(self,pd_data,Del,co_num,output_wf,output_mtf):
42
44
self .Imk = self .data [1 ,:,:]/ self .mean_imk
43
45
44
46
self .output_WF = np .zeros ((2048 ,2048 ))
45
- self .output_mtf = np .zeros ((2048 ,2048 ))
46
-
47
-
47
+ self .output_MTF = np .zeros ((2048 ,2048 ))
48
+ self .parallel = parallel
49
+ #@staticmethod
50
+ def run_pd (self ,k ):
51
+
52
+ im0 = self .patch [k ,:,:,0 ]
53
+ imk = self .patch [k ,:,:,1 ]
54
+ im0 = im0 / im0 .mean ()
55
+ imk = imk / imk .mean ()
56
+ imk = imreg (im0 ,imk )
57
+
58
+
59
+ im0 = apo2d (im0 ,10 )
60
+ imk = apo2d (imk ,10 )
61
+
62
+ d0 ,dk = FT (im0 ,imk )
63
+ gam = 1 # Gamma(d0,dk,M_gamma)
64
+ p0 = np .zeros (self .co_num )
65
+ fit = cost_func (self .Del ,0.5 ,1e-10 ,10 ,self .co_num ,0.5 ,617.3e-6 , 140 ,4125.3 ,0.5 )
66
+ Mask = aperture .mask_pupil (fit .telescope .pupil_size (),fit .size )
67
+ noise_temp = noise_mask_high (fit .size ,fit .cut_off )
68
+ noise_filter = fftshift (noise_temp )
69
+ def Minimise (coefficients ):
70
+ A_f = wavefront .pupil_foc (coefficients ,fit .size ,fit .telescope .pupil_size (),self .co_num )
71
+ A_def = wavefront .pupil_defocus (coefficients ,fit .size ,fit .del_z ,fit .telescope .pupil_size (),self .co_num )
72
+ psf_foc = wavefront .PSF (Mask ,A_f ,False )
73
+ psf_defoc = wavefront .PSF (Mask ,A_def ,False )
74
+ t0 = wavefront .OTF (psf_foc )
75
+ tk = wavefront .OTF (psf_defoc )
76
+ q ,q2 = PD .Q_matrix (t0 ,tk ,fit .reg ,gam )
77
+ F_m = PD .F_M (q2 ,d0 , dk ,t0 ,tk ,noise_filter ,gam )
78
+ E_metric = PD .Error_metric (t0 ,tk ,d0 ,dk ,q ,noise_filter )
79
+ L_m = PD .L_M (E_metric ,fit .size )
80
+ return L_m
81
+
82
+
83
+
84
+ Minimize_partial = partial (Minimise )
85
+ mini = minimize (Minimize_partial ,p0 ,method = 'L-BFGS-B' )
86
+ result = fit .Minimize_res (mini .x )
87
+ patch_wfe = result [1 ]
88
+ patch_mtf = MTF (fftshift (result [0 ]))
89
+ return patch_wfe ,patch_mtf
48
90
49
91
def fit_patch (self ):
50
92
51
- upper = 1700
52
- Nx = np .arange (300 ,upper ,self .Del )
53
- Ny = np .arange (300 ,upper ,self .Del )
93
+ upper = 1700
94
+ Nx = np .arange (300 ,upper ,self .Del )
95
+ Ny = np .arange (300 ,upper ,self .Del )
96
+ if not self .parallel :
54
97
for n1 in Nx :
55
98
for n2 in Ny :
56
99
@@ -60,9 +103,7 @@ def fit_patch(self):
60
103
im0 = im0 / im0 .mean ()
61
104
imk = imk / imk .mean ()
62
105
imk = imreg (im0 ,imk )
63
-
64
-
65
-
106
+
66
107
im0 = apo2d (im0 ,10 )
67
108
imk = apo2d (imk ,10 )
68
109
@@ -91,16 +132,68 @@ def Minimise(coefficients):
91
132
L_m = PD .L_M (E_metric ,fit .size )
92
133
return L_m
93
134
94
-
135
+
95
136
Minimise_partial = partial (Minimise )
96
137
mini = scipy .optimize .minimize (Minimise_partial ,p0 ,method = 'L-BFGS-B' )
97
138
98
139
result = fit .Minimize_res (mini .x )
99
140
self .output_WF [n2 :n2 + self .Del ,n1 :n1 + self .Del ] = result [1 ]
100
- self .output_mtf [n2 :n2 + self .Del ,n1 :n1 + self .Del ] = MTF (fftshift (result [0 ]))
141
+ self .output_MTF [n2 :n2 + self .Del ,n1 :n1 + self .Del ] = MTF (fftshift (result [0 ]))
101
142
hdu = fits .PrimaryHDU (self .output_WF )
102
143
hdu .writeto (self .output_wf ,overwrite = True )
144
+ hdu = fits .PrimaryHDU (self .output_mtf )
145
+ hdu .writeto (self .output_mtf ,overwrite = True )
146
+ else :
147
+ self .patch = tools .prepare_patches (self .data ,self .Del ,self .Im0 ,self .Imk )
148
+ n_workers = min (6 , os .cpu_count ())
149
+ self .args_list = [i for i in range (len (self .patch ))]
150
+ self .results_parallel = list (processing .MP .simultaneous (self .run_pd , self .args_list , workers = n_workers ))
151
+
152
+ def plot_results (self ,output ):
153
+
154
+ # change here the format of the output
155
+ if not self .parallel :
156
+ data_mtf = self .output_MTF
157
+ data_wfe = self .output_WF
103
158
159
+ if self .parallel :
160
+ ## call here the stitching function
161
+ data_mtf ,data_wfe = tools .stitch_patches (self .results_parallel ,self .Del )
162
+
163
+
164
+ fig , ax = plt .subplots (1 ,2 ,figsize = (10 ,10 ))
165
+
166
+ im = ax [1 ].imshow (data_mtf ,vmin = 0 ,vmax = 1 ,origin = 'lower' ,cmap = 'gray' )
167
+ ax [1 ].set_title ('MTF' )
168
+ divider = make_axes_locatable (ax [1 ])
169
+ cax = divider .append_axes ('right' ,pad = 0.05 ,size = 0.03 )
170
+ cbar1 = plt .colorbar (im ,cax = cax )
171
+ cbar1 .ax .tick_params (labelsize = 16 )
172
+ cbar1 .set_label ('MTF' ,fontsize = 16 )
173
+ ax [1 ].set_xlabel ('[Pixels]' )
174
+ major_ticks = np .arange (0 , 2048 ,350 )
175
+ major_ticks_y = np .arange (0 , 2048 ,350 )
176
+ ax [1 ].set_xticks (major_ticks )
177
+ ax [1 ].set_yticks (major_ticks_y )
178
+ ax [1 ].tick_params (labelsize = 4 )
179
+
180
+
181
+
182
+ im2 = ax [0 ].imshow (data_wfe / (2 * np .pi ),vmin = - 0.5 ,vmax = 1.4 ,origin = 'lower' ,cmap = 'gray' )
183
+ ax [0 ].set_xlabel ('[Pixels]' )
184
+ ax [0 ].set_title ('WF error[$\lambda$]' )
185
+ divider2 = make_axes_locatable (ax [0 ])
186
+ cax2 = divider2 .append_axes ('right' ,pad = 0.05 ,size = 0.03 ) #size is the width of the color bar and pad is the fraction of the new axis
187
+ cbar2 = plt .colorbar (im2 ,cax = cax2 )
188
+ cbar2 .ax .tick_params (labelsize = 16 )
189
+ ax [0 ].tick_params (labelbottom = False ,labelsize = 16 )
190
+ plt .subplots_adjust (wspace = None , hspace = 0.1 )
191
+ ax [0 ].set_xticks (major_ticks )
192
+ ax [0 ].set_yticks (major_ticks_y )
193
+ ax [0 ].set_ylabel ('[Pixels]' )
194
+ #plt.subplots_adjust(wspace=None, hspace=-0.1)
195
+ #plt.axis('off')
196
+ plt .savefig (output ,dpi = 300 )
104
197
105
198
if (__name__ == '__main__' ):
106
199
parser = argparse .ArgumentParser (description = 'PD on sub-fields' )
@@ -109,7 +202,10 @@ def Minimise(coefficients):
109
202
parser .add_argument ('-d' ,'--Del' , help = 'Del' ,default = 265 )
110
203
parser .add_argument ('-ow' ,'--ow' , help = 'output_WFE' )
111
204
parser .add_argument ('-om' ,'--om' , help = 'output MTF' )
205
+ parser .add_argument ('-r' ,'--res' , help = 'results' )
206
+ parser .add_argument ('-p' ,'--parallel' ,choices = ['True' ,'False' ],default = True )
112
207
parsed = vars (parser .parse_args ())
113
- st = patch_pd (pd_data = '{0}' .format (parsed ['input' ]),Del = int (parsed ['Del' ]),co_num = int (parsed ['Z' ]),output_wf = '{0}' .format (parsed ['ow' ]),output_mtf = '{0}' .format (parsed ['om' ]))
208
+ st = patch_pd (pd_data = '{0}' .format (parsed ['input' ]),Del = int (parsed ['Del' ]),co_num = int (parsed ['Z' ]),output_wf = '{0}' .format (parsed ['ow' ]),output_mtf = '{0}' .format (parsed ['om' ]), parallel = bool ( parsed [ 'parallel' ]) )
114
209
st .fit_patch ()
210
+ st .plot_results (output = '{0}' .format (parsed ['res' ]))
115
211
0 commit comments