3 Created on Wed Oct 9 14:03:29 2017
13 import carmaWrap
as ch
16 import matplotlib.pyplot
as plt
17 import hdf5_util
as h5u
20 sys.path.append(
'/home/sdurand/hracode/codes/PYRCADO/Python')
21 import PYRCADOCALIB
as pyrcalib
22 from astropy.io
import fits
23 from numba
import autojit
26 print(
"TEST SHESHA\n closed loop: call loop(int niter)")
28 if (len(sys.argv) != 2):
29 error =
'command line should be:"python -i test.py parameters_filename"\n with "parameters_filename" the path to the parameters file'
30 raise StandardError(error)
33 param_file = sys.argv[1]
34 if (param_file.split(
'.')[-1] ==
"py"):
35 filename = param_file.split(
'/')[-1]
36 param_path = param_file.split(filename)[0]
37 sys.path.insert(0, param_path)
38 exec(
"import %s as config" % filename.split(
".py")[0])
39 sys.path.remove(param_path)
46 raise ValueError(
"Parameter file extension must be .py or .h5")
48 print(
"param_file is", param_file)
50 if (hasattr(config,
"simul_name")):
51 if (config.simul_name
is None):
54 simul_name = config.simul_name
55 print(
"simul name is", simul_name)
61 if (simul_name !=
""):
63 param_dict = h5u.params_dictionary(config)
64 matricesToLoad = h5u.checkMatricesDataBase(os.environ[
"SHESHA_ROOT"] +
"/data/",
73 c = ch.carmaWrap_context(devices=config.p_loop.devices)
76 wfs, tel = ao.wfs_init(config.p_wfss, config.p_atmos, config.p_tel, config.p_geom,
77 config.p_target, config.p_loop, config.p_dms)
81 atm = ao.atmos_init(c, config.p_atmos, config.p_tel, config.p_geom, config.p_loop,
82 config.p_wfss, wfs, config.p_target, clean=clean,
87 dms = ao.dm_init(config.p_dms, config.p_wfss, wfs, config.p_geom, config.p_tel)
91 tar = ao.target_init(c, tel, config.p_target, config.p_atmos, config.p_geom,
92 config.p_tel, config.p_dms, config.p_wfss)
96 rtc = ao.rtc_init(tel, wfs, config.p_wfss, dms, config.p_dms, config.p_geom,
97 config.p_rtc, config.p_atmos, atm, config.p_tel, config.p_loop,
98 clean=clean, simul_name=simul_name, load=matricesToLoad)
101 h5u.validDataBase(os.environ[
"SHESHA_ROOT"] +
"/data/", matricesToLoad)
103 print(
"====================")
105 print(
"====================")
106 print(
"objects initialzed on GPU:")
107 print(
"--------------------------------------------------------")
117 size = im[0].data.shape[0]
118 pyr_im_cube = np.zeros((nb_im, size, size), dtype=np.float32)
119 for i
in range(nb_im):
120 pyr_im_cube[i] = im[i].data
127 np.identity(size / bin_factor, dtype=np.float32), bin_factor, axis=0)
130 def calib_pyr(centers, wfs_numbers, bin_factor=1, crop_factor=0):
134 offset = np.zeros((2, 4))
136 npup = config.p_wfss[wfs_numbers]._validsubsx.shape[0]
141 subx = config.p_wfss[wfs_numbers]._validsubsx[npup * (i) / 4:npup * (i + 1) / 4]
143 suby = config.p_wfss[wfs_numbers]._validsubsy[npup * (i) / 4:npup * (i + 1) / 4]
145 center_compass = [((np.max(subx) - np.min(subx)) / 2.) + np.min(subx),
146 ((np.max(suby) - np.min(suby)) / 2.) + np.min(suby)]
149 np.int32((centers[j[i]][0] - crop_factor / 2.) / bin_factor) -
151 np.int32((centers[j[i]][1] - crop_factor / 2.) / bin_factor) -
168 path =
'/home/sdurand/RecordPyrImages_2017_06_06_07h49/pyrImageCube.fits'
177 pup = np.zeros((npup / 4, 4))
180 pup[:, i] = valid_pixel[(npup / 4) * j[i]:(npup / 4) * (j[i] + 1)]
181 tot = np.sum(pup, axis=1)
184 gx = (pup[:, 0] + pup[:, 2] - (pup[:, 1] + pup[:, 3])) / t
185 gy = (pup[:, 0] + pup[:, 1] - (pup[:, 2] + pup[:, 3])) / t
188 slope = np.append(gx, gy) * (
189 (config.p_wfss[0].pyr_ampl * config.p_wfss[0].Lambda * 1e-6) /
190 config.p_tel.diam) * (180 / np.pi) * 3600
199 im_crop = im[np.int32((size / 2.) - (taille_sortie / 2.)):np.int32(
200 (size / 2.) + (taille_sortie / 2)),
201 np.int32((size / 2.) - (taille_sortie / 2.)):np.int32(
202 (size / 2.) + (taille_sortie / 2.))]
210 bin_factor = np.int32(bin_factor)
212 size_bin = size / bin_factor
213 binimage = np.zeros((size_bin, size_bin), dtype=np.float32)
216 xx, yy = np.meshgrid(a, a)
219 for i
in range(size):
220 for j
in range(size):
221 binimage[xx[i, j], yy[i, j]] += im[i, j]
222 return binimage / (bin_factor**2)
228 bin_factor = np.int32(bin_factor)
232 binimage = ((P.T).dot(im)).dot(P)
235 return binimage / (bin_factor**2)
238 def loop(n, d_valid_pix=[], d_P=[], offset=[],
239 bool_fake_wfs=np.zeros(len(config.p_wfss)), bin_factor=[], crop_factor=[],
241 print(
"----------------------------------------------------")
242 print(
"iter# | S.E. SR | L.E. SR | Est. Rem. | framerate")
243 print(
"----------------------------------------------------")
249 if (config.p_controllers[0].type_control ==
"geo"):
250 for t
in range(config.p_target.ntargets):
251 tar.atmos_trace(t, atm, tel)
252 rtc.docontrol_geo(0, dms, tar, 0)
253 rtc.applycontrol(0, dms)
257 for t
in range(config.p_target.ntargets):
258 tar.atmos_trace(t, atm, tel)
262 for w
in range(len(config.p_wfss)):
263 wfs.sensors_trace(w,
"all", tel, atm, dms)
265 if (config.p_wfss[w].type_wfs ==
'pyrhr'):
266 if (bin_factor[fake_it] > 1):
271 pyr_im.shape[0] - crop_factor[w])
274 pyr_im_crop =
crop_im(cube_im[i],
275 cube_im[i].shape[0] - 2).astype(
278 d_imhr = ch.carmaWrap_obj_Float2D(
279 ch.carmaWrap_context(), data=pyr_im_crop /
280 (bin_factor[fake_it]**2))
281 d_imlr = d_P[fake_it].gemm(d_imhr,
't',
'n').gemm(
286 d_imlr = ch.carmaWrap_obj_Float2D(
287 ch.carmaWrap_context(),
290 d_imlr = ch.carmaWrap_obj_Float2D(
291 ch.carmaWrap_context(),
295 w, d_imlr, d_valid_pix[fake_it][0],
296 d_valid_pix[fake_it][1])
298 elif (config.p_wfss[w].type_wfs ==
'sh'):
304 wfs.sensors_compimg(w)
310 rtc.applycontrol(0, dms)
312 if ((i + 1) % 100 == 0):
313 strehltmp = tar.get_strehl(0)
314 print(i + 1,
"\t", strehltmp[0],
"\t", strehltmp[1])
316 print(
" loop execution time:", t1 - t0,
" (", n,
"iterations), ", (t1 - t0) / n,
317 "(mean) ", n / (t1 - t0),
"Hz")
324 bool_fake_wfs = np.zeros(len(config.p_wfss), dtype=np.int32)
329 crop_factor = np.zeros(sum(bool_fake_wfs))
330 size_c = np.zeros(sum(bool_fake_wfs))
331 centers_fake_wfs = []
333 offset = np.zeros((2, 4, sum(bool_fake_wfs)))
334 size = np.zeros(sum(bool_fake_wfs))
335 bin_factor = np.zeros(sum(bool_fake_wfs))
349 if (bool_fake_wfs[w] == 1):
350 if (config.p_wfss[w].type_wfs ==
'pyrhr'):
351 centers_fake_wfs.append(pyrcalib.giveMeTheCalibs()[1][
'centers'])
354 path =
'/home/sdurand/RecordPyrImages_2017_06_06_07h49/pyrImageCube.fits'
359 fake_pos = np.where(bool_fake_wfs == 1)
360 for f
in range(sum(bool_fake_wfs)):
361 crop_factor[f] = size[f] - ((size[f] / bin_factor[f]) * bin_factor[f])
362 size_c[f] = size[f] - crop_factor[f]
364 if (config.p_wfss[fake_pos[f]].type_wfs ==
'pyrhr'):
365 offset[:, :, f] =
calib_pyr(centers_fake_wfs[f], fake_pos[f],
366 bin_factor=bin_factor[f],
367 crop_factor=crop_factor[f])
369 ch.carmaWrap_obj_Float2D(ch.carmaWrap_context(), data=
create_P(
370 bin_factor[f], size_c[f])))
378 if (bool_fake_wfs[w] == 1):
379 if (config.p_wfss[w].type_wfs ==
'pyrhr'):
380 npup = config.p_wfss[w]._validsubsx.shape[0]
381 valid_pix = np.zeros((2, npup), dtype=np.int32)
383 ch.carmaWrap_obj_Float2D(ch.carmaWrap_context(),
384 data=
create_P(bin_factor[w], size_c[w])))
385 valid_pix[0, :] = np.int32(config.p_wfss[w]._validsubsx + offset[0, :, w].repeat(
386 config.p_wfss[w]._nvalid))
387 valid_pix[1, :] = np.int32(config.p_wfss[w]._validsubsy + offset[1, :, w].repeat(
388 config.p_wfss[w]._nvalid))
392 ch.carmaWrap_obj_Int1D(ch.carmaWrap_context(), data=valid_pix[0, :]),
393 ch.carmaWrap_obj_Int1D(ch.carmaWrap_context(), data=valid_pix[1, :])
395 loop(100, d_valid_pix, d_P, offset=offset, bool_fake_wfs=bool_fake_wfs,
396 cube_im=pyr_im_cube, bin_factor=bin_factor,
397 crop_factor=crop_factor)
399 elif (config.p_wfss[w].type_wfs ==
'sh'):