def __init__(self, tp, method, target_array, n_layer, layer_alt, tt_track_present, lgs_track_present, offset_present, fit_unsensed, fit_tt_track, fit_lgs_track, fit_offset, fit_L0, L0, r0, roi_belowGround, roi_envelope, zeroSep_locations, allMapPos, xy_separations): self.method = method self.zeroSep_locations = zeroSep_locations self.remove_tt = tp.remove_tt self.target_array = target_array self.zeroSep_cov = tp.zeroSep_cov self.print_fitting = tp.print_fitting self.gs_combs = tp.combs self.theo_cov = False self.output_fitted_array = tp.output_fitted_array self.count = 0 self.fit_unsensed = fit_unsensed if lgs_track_present==True or fit_lgs_track==True: self.using_lgs = True else: self.using_lgs=False if self.fit_unsensed == True: n_layer += 1 layer_alt = numpy.append(layer_alt, 1e20) r0 = numpy.append(r0, r0[0]) L0 = numpy.append(L0, L0[0]) if self.target_array=='Covariance Map ROI': if self.method=='Direct Fit': self.generationParams = covariance_roi(tp.pupil_mask, tp.subap_diam, tp.wavelength, tp.tel_diam, tp.n_subap, tp.gs_dist, tp.gs_pos, n_layer, layer_alt, L0, allMapPos, xy_separations, tp.map_axis, styc_method=tp.styc_method, wind_profiling=False, tt_track_present=tt_track_present, lgs_track_present=lgs_track_present, offset_present=offset_present, fit_layer_alt=False, fit_tt_track=fit_tt_track, fit_lgs_track=fit_lgs_track, fit_offset=fit_offset, fit_L0=fit_L0) if self.method=='L3S Fit': onesMat, wfsMat_1, wfsMat_2, allMapPos_acrossMap, selector, xy_separations_acrossMap = roi_referenceArrays( tp.pupil_mask, tp.gs_pos, tp.tel_diam, tp.pupil_mask.shape[0]-1, tp.roi_envelope) self.generationParams = covariance_roi_l3s(tp.pupil_mask, tp.subap_diam, tp.wavelength, tp.tel_diam, tp.n_subap, tp.gs_dist, tp.gs_pos, n_layer, layer_alt, L0, allMapPos_acrossMap, xy_separations_acrossMap, tp.map_axis, tp.roi_belowGround, tp.roi_envelope, styc_method=tp.styc_method, wind_profiling=False, lgs_track_present=lgs_track_present, offset_present=offset_present, fit_layer_alt=False, fit_lgs_track=fit_lgs_track, fit_offset=fit_offset, fit_L0=fit_L0) else: rem_tt = False l3s1_trans = False self.gs_combs += tp.n_wfs if self.method=='L3S Fit': l3s1_trans = True if tp.remove_tt==True: rem_tt = True self.generationParams = covariance_matrix(tp.pupil_mask, tp.subap_diam, tp.wavelength, tp.tel_diam, tp.n_subap, tp.gs_dist, tp.gs_pos, n_layer, layer_alt, r0, L0, styc_method=tp.styc_method, wind_profiling=False, tt_track_present=tt_track_present, lgs_track_present=lgs_track_present, offset_present=offset_present, fit_layer_alt=False, fit_tt_track=fit_tt_track, fit_lgs_track=fit_lgs_track, fit_offset=fit_offset, fit_L0=fit_L0, matrix_xy=tp.matrix_xy, huge_matrix=tp.huge_matrix, l3s1_transform=l3s1_trans, l3s1_matrix=tp.l3s1_matrix, remove_tt=rem_tt, remove_tt_matrix=tp.remove_tt_matrix) if self.target_array=='Covariance Map': self.n_wfs = tp.n_wfs self.nx_subap = tp.nx_subap self.n_subap = tp.n_subap self.n_subap_from_pupilMask = tp.n_subap_from_pupilMask self.pupil_mask = tp.pupil_mask self.map_axis = tp.map_axis self.mm = tp.mm self.mmc = tp.mmc self.md = tp.md
# nx_subap = numpy.array([40]*n_wfs) # """HARMONI""" # tel_diam = 39.0 # obs_diam = 4.0 # subap_diam = numpy.array([0.507]*n_wfs) # n_subap = numpy.array([4260]*n_wfs) # nx_subap = numpy.array([74]*n_wfs) pupil_mask = make_pupil_mask('circle', n_subap, nx_subap[0], obs_diam, tel_diam) matrix_region_ones = numpy.ones((n_subap[0], n_subap[0])) mm, mmc, md = get_mappingMatrix(pupil_mask, matrix_region_ones) onesMat, wfsMat_1, wfsMat_2, allMapPos, selector, xy_separations = roi_referenceArrays( pupil_mask, gs_pos, tel_diam, roi_belowGround, roi_envelope) params = covariance_roi(pupil_mask, subap_diam, wavelength, tel_diam, n_subap, gs_alt, gs_pos, n_layer, layer_alt, L0, allMapPos, xy_separations, roi_axis, styc_method=True,
def __init__(self, turb_results, fit_method, roi_offsets, frame_rate, num_offsets, offset_step, wind_roi_belowGround, wind_roi_envelope, wind_map_axis, zeroSep_cov, zeroSep_locations, include_temp0, mult_neg_offset, separate_pos_neg_offsets, reduce_SL, print_fitting): if reduce_SL==True: self.n_wfs = 3 self.combs = 3 self.selector = numpy.array(([[0,1], [0,2], [1,2]])) self.lgs_track = (turb_results.lgs_track[:3]+turb_results.lgs_track[:3])/2 roi_width = (2*wind_roi_envelope) + 1 roi_length = turb_results.pupil_mask.shape[0] + wind_roi_belowGround self.zeroSep_locations = roi_zeroSep_locations(3, roi_width, roi_length, wind_map_axis, wind_roi_belowGround) else: self.n_wfs = turb_results.n_wfs self.combs = turb_results.combs self.selector = turb_results.selector self.lgs_track = turb_results.lgs_track self.zeroSep_locations = zeroSep_locations self.fit_method = fit_method self.roi_offsets = roi_offsets self.num_offsets = num_offsets self.offset_step = offset_step self.wind_roi_belowGround = wind_roi_belowGround self.wind_roi_envelope = wind_roi_envelope self.wind_map_axis = wind_map_axis self.zeroSep_cov = zeroSep_cov self.include_temp0 = include_temp0 self.mult_neg_offset = mult_neg_offset self.separate_pos_neg_offsets = separate_pos_neg_offsets self.reduce_SL = reduce_SL self.print_fitting = print_fitting self.air_mass = turb_results.air_mass self.gs_pos = turb_results.gs_pos[:self.n_wfs] self.tel_diam = turb_results.tel_diam self.n_subap = turb_results.n_subap[:self.n_wfs] self.n_subap_from_pupilMask = turb_results.n_subap_from_pupilMask[:self.n_wfs] self.nx_subap = turb_results.nx_subap[:self.n_wfs] self.gs_dist = turb_results.gs_dist[:self.n_wfs] self.shwfs_shift = turb_results.shwfs_shift[:self.n_wfs] self.shwfs_rot = turb_results.shwfs_rot[:self.n_wfs] self.subap_diam = turb_results.subap_diam[:self.n_wfs] self.pupil_mask = turb_results.pupil_mask self.wavelength = turb_results.wavelength[:self.n_wfs] self.styc_method = turb_results.styc_method self.tt_track = turb_results.tt_track self.tt_track_present = turb_results.tt_track_present self.lgs_track_present = turb_results.lgs_track_present self.offset_present = turb_results.offset_present #account for air mass cn2 = turb_results.Cn2 turb_cn2 = cn2.copy() self.Cn2 = cn2 * turb_results.air_mass self.Cn2[turb_cn2==turb_results.cn2_noiseFloor] = turb_results.cn2_noiseFloor r0 = calc_r0(self.Cn2, turb_results.wavelength[0]) self.observable_bins = turb_results.observable_bins self.delete_index = numpy.where(self.Cn2[:self.observable_bins]==turb_results.cn2_noiseFloor)[0] self.r0 = self.reduce_layers(r0, self.delete_index, self.observable_bins) self.L0 = self.reduce_layers(turb_results.L0, self.delete_index, self.observable_bins) self.layer_alt = self.reduce_layers(numpy.array(turb_results.layer_alt) * self.air_mass, self.delete_index, self.observable_bins) self.n_layer = self.layer_alt.shape[0] self.frame_rate = frame_rate self.iteration = 0 #These imported tools are the key to calculating the covariance map ROI and its analytically generated model during fitting onesMat, wfsMat_1, wfsMat_2, self.allMapPos, selector, self.xy_separations = roi_referenceArrays(self.pupil_mask, self.gs_pos, self.tel_diam, self.wind_roi_belowGround, self.wind_roi_envelope) if self.wind_map_axis=='x and y': self.length = int(self.wind_roi_belowGround + self.pupil_mask.shape[0])*2 else: self.length = int(self.wind_roi_belowGround + self.pupil_mask.shape[0]) if self.fit_method=='L3S Fit': t, t, t, self.allMapPos_acrossMap, t, self.xy_separations_acrossMap = roi_referenceArrays(self.pupil_mask, self.gs_pos, self.tel_diam, self.pupil_mask.shape[0]-1, self.wind_roi_envelope)
# n_subap = numpy.array([1248]*n_wfs) # nx_subap = numpy.array([40]*n_wfs) # """HARMONI""" # tel_diam = 39.0 # obs_diam = 4.0 # subap_diam = numpy.array([0.507]*n_wfs) # n_subap = numpy.array([4260]*n_wfs) # nx_subap = numpy.array([74]*n_wfs) pupil_mask = make_pupil_mask('circle', n_subap, nx_subap[0], obs_diam, tel_diam) matrix_region_ones = numpy.ones((n_subap[0], n_subap[0])) mm, mmc, md = get_mappingMatrix(pupil_mask, matrix_region_ones) onesMat, wfsMat_1, wfsMat_2, allMapPos, selector, xy_separations = roi_referenceArrays(pupil_mask, gs_pos, tel_diam, roi_belowGround, roi_envelope) onesMat, wfsMat_1, wfsMat_2, allMapPos_acrossMap, selector, xy_separations_acrossMap = roi_referenceArrays(pupil_mask, gs_pos, tel_diam, pupil_mask.shape[0]-1, roi_envelope) s1 = time.time() params = covariance_roi_l3s(pupil_mask, subap_diam, wavelength, tel_diam, n_subap, gs_alt, gs_pos, n_layer, layer_alt, L0, allMapPos_acrossMap, xy_separations_acrossMap, roi_axis, roi_belowGround, roi_envelope, styc_method=True, lgs_track_present=False, offset_present=False, fit_layer_alt=True, fit_lgs_track=False, fit_offset=False, fit_L0=False, wind_profiling=True) print('Conf: {}'.format(time.time() - s1)) s = time.time() r = params._make_covariance_roi_l3s_(layer_alt, r0, L0, lgs_track=lgs_track, shwfs_shift=shwfs_shift, shwfs_rot=shwfs_rot, delta_xSep=delta_xSep, delta_ySep=delta_ySep) f = time.time()
def __init__(self, turb_results, wind_config_file): """Fixes variables from config_file that are to be used to complete turbulence profiling. Parameters: config_file (dict): fitting specifications set in imported yaml configuration file.""" self.turb_results = turb_results self.tel_diam = self.turb_results.tel_diam self.n_wfs = self.turb_results.n_wfs self.n_subap = self.turb_results.n_subap self.n_subap_from_pupilMask = turb_results.n_subap_from_pupilMask self.nx_subap = self.turb_results.nx_subap self.pupil_mask = self.turb_results.pupil_mask self.shwfs_centroids = self.turb_results.shwfs_centroids self.gs_pos = self.turb_results.gs_pos self.n_layer = self.turb_results.n_layer self.observable_bins = self.turb_results.observable_bins self.offset_present = self.turb_results.offset_present self.cn2_noiseFloor = self.turb_results.cn2_noiseFloor self.L0 = self.turb_results.L0 self.tt_track = self.turb_results.tt_track self.lgs_track = self.turb_results.lgs_track self.combs = self.turb_results.combs self.selector = self.turb_results.selector self.shwfs_shift = self.turb_results.shwfs_shift self.shwfs_rot = self.turb_results.shwfs_rot target_conf = wind_config_file.configDict["OFFSET TARGET ARRAY"] self.input_shwfs_centroids = target_conf["input_shwfs_centroids"] self.roi_via_matrix = target_conf["roi_via_matrix"] self.reduce_SL = target_conf["reduce_SL"] self.zeroSep_cov = target_conf["zeroSep_cov"] self.input_frame_count = target_conf["input_frame_count"] self.num_offsets = target_conf["num_offsets"] self.offset_step = target_conf["offset_step"] self.temporal_step = numpy.round( float(self.offset_step) / float(self.num_offsets)).astype('int') self.include_temp0 = target_conf["include_temp0"] self.minus_negativeTemp = target_conf["minus_negativeTemp"] self.separate_pos_neg_offsets = target_conf["separate_pos_neg_offsets"] self.wind_roi_belowGround = target_conf["roi_belowGround"] self.wind_roi_envelope = target_conf["roi_envelope"] self.wind_map_axis = target_conf["map_axis"] self.wind_mapping_type = target_conf["mapping_type"] fit_params = wind_config_file.configDict["FITTING ALGORITHM"] self.delta_xSep = numpy.array( fit_params["delta_xSep"]).astype('float64') self.delta_ySep = numpy.array( fit_params["delta_ySep"]).astype('float64') if len(self.delta_xSep) != self.n_layer: raise Exception('Check length of wind parameters with n_layer.') if self.reduce_SL == True: if self.n_wfs != 4: raise Exception( 'To reduce the data there must be 4 GSs in a square asterism.' ) if self.offset_present == True: raise Exception( 'When reducing square layout optical alignment must be assumed.' ) fit_conf = wind_config_file.configDict["FITTING ALGORITHM"] self.fitting_type = fit_conf["type"] self.fit_layer_alt = fit_conf["fit_layer_alt"] self.fit_deltaXYseps = fit_conf["fit_deltaXYseps"] self.print_fiting = fit_conf["print_fitting"] self.onesMat, self.wfsMat_1, self.wfsMat_2, self.allMapPos, self.selector, self.xy_separations = roi_referenceArrays( self.pupil_mask, self.gs_pos, self.tel_diam, self.wind_roi_belowGround, self.wind_roi_envelope) if self.reduce_SL == True: # stop self.selector = numpy.array(([[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [3, 2]])) if self.zeroSep_cov == False: roi_width = (2 * self.wind_roi_envelope) + 1 roi_length = self.pupil_mask.shape[0] + self.wind_roi_belowGround self.zeroSep_locations = roi_zeroSep_locations( self.combs, roi_width, roi_length, self.wind_map_axis, self.wind_roi_belowGround) else: self.zeroSep_locations = numpy.nan if self.minus_negativeTemp == True: self.mult_neg_offset = -1. else: self.mult_neg_offset = 1. if wind_config_file.configDict["FITTING ALGORITHM"][ "type"] == 'direct': self.fit_method = 'Direct Fit' self.direct_wind = True self.l3s_wind = False self.l3s1_matrix = numpy.nan if wind_config_file.configDict["FITTING ALGORITHM"]["type"] == 'l3s': self.fit_method = 'L3S Fit' self.l3s_wind = True self.direct_wind = False if self.turb_results.l3s_fit == True: self.l3s1_matrix = self.turb_results.l3s1_matrix else: self.l3s1_matrix = transform_matrix( self.n_subap_from_pupilMask, self.n_wfs) if self.turb_results.covariance_map == True or self.turb_results.covariance_map_roi == True: self.mm = self.turb_results.mm self.mmc = self.turb_results.mmc self.md = self.turb_results.md else: matrix_region_ones = numpy.ones((self.n_subap_from_pupilMask[0], self.n_subap_from_pupilMask[0])) self.mm, self.mmc, self.md = get_mappingMatrix( self.pupil_mask, matrix_region_ones) print('\n' + '############ WIND PROFILING PARAMETERS SECURE #############')
def calculate_roi_covariance(shwfs_centroids, gs_pos, pupil_mask, tel_diam, roi_belowGround, roi_envelope, roi_axis, mapping_type): """Takes SHWFS centroids and directly calculates the covariance map ROI (does not require going via covariance matrix). Parameters: shwfs_centroids (ndarray): SHWFS centroid measurements. gs_pos (ndarray): GS asterism in telescope FoV. pupil_mask (ndarray): mask of SHWFS sub-apertures within the telescope pupil. tel_diam (float): diameter of telescope pupil. roi_belowGround (int): number of sub-aperture separations the ROI encapsulates 'below-ground'. roi_envelope (int): number of sub-aperture separations either side of the ROI. roi_axis (str): in which axis to express ROI ('x', 'y', 'x+y' or 'x and y') mapping_type (str): how to calculate overall sub-aperture separation covariance ('mean' or 'median') Returns: roi_covariance (ndarray): covariance map ROI. time_taken (float): time taken to complete calculation.""" covMapDim = pupil_mask.shape[0] * 2 - 1 n_subap = numpy.array([int(pupil_mask.sum())] * gs_pos.shape[0]) mm, sa_mm, sb_mm, allMapPos, selector, xy_separations = roi_referenceArrays( numpy.rot90(pupil_mask, 2), gs_pos, tel_diam, roi_belowGround, roi_envelope) timeStart = time.time() #subtracts mean at each sub-aperture axis (first step in calculating cross-covariance). shwfs_centroids = (shwfs_centroids - shwfs_centroids.mean(0)).T if roi_axis == 'x' or roi_axis == 'y' or roi_axis == 'x+y': roi_covariance = numpy.zeros( (allMapPos.shape[0] * allMapPos.shape[1], allMapPos.shape[2])) if roi_axis == 'x and y': roi_covariance = numpy.zeros( (allMapPos.shape[0] * allMapPos.shape[1], allMapPos.shape[2] * 2)) wfs1_n_subap = n_subap[0] wfs2_n_subap = n_subap[0] mm_subapPos = allMapPos[:, :, :, 1] + allMapPos[:, :, :, 0] * covMapDim for i in range(allMapPos.shape[0]): roi_ones = numpy.ones(allMapPos[i, :, :, 0].shape) roi_ones[numpy.where(allMapPos[i, :, :, 0] == 2 * covMapDim)] = 0 num_roi_baselines = int(roi_ones.sum()) arange_baselines = numpy.arange(num_roi_baselines) + 1 roi_ones_arange = roi_ones.copy() roi_ones_arange[roi_ones == 1] = arange_baselines av = numpy.ones(roi_ones.shape) #integer shift for each GS combination subap1_comb_shift = selector[i][0] * 2 * wfs1_n_subap subap2_comb_shift = selector[i][1] * 2 * wfs1_n_subap if roi_axis != 'y': roi_cov_xx = numpy.zeros(roi_ones.shape) if roi_axis != 'x': roi_cov_yy = numpy.zeros(roi_ones.shape) for j in range(1, num_roi_baselines + 1): # print(j) roi_loc = numpy.where(roi_ones_arange == j) roi_baseline = mm_subapPos[i, roi_loc[0], roi_loc[1]] subaps1 = sa_mm[:, roi_baseline][numpy.where( mm[:, roi_baseline] == 1)] + subap1_comb_shift subaps2 = sb_mm[:, roi_baseline][numpy.where( mm[:, roi_baseline] == 1)] + subap2_comb_shift num_subaps = subaps1.shape[0] # stop if roi_axis != 'y': if mapping_type == 'mean': cova = numpy.mean((shwfs_centroids[subaps1] * (shwfs_centroids[subaps2])).sum(1) / (shwfs_centroids.shape[1] - 1)) if mapping_type == 'median': cova = numpy.median((shwfs_centroids[subaps1] * (shwfs_centroids[subaps2])).sum(1) / (shwfs_centroids.shape[1] - 1)) roi_cov_xx[roi_loc[0], roi_loc[1]] = cova if roi_axis != 'x': if mapping_type == 'mean': cova = numpy.mean( (shwfs_centroids[subaps1 + wfs1_n_subap] * (shwfs_centroids[subaps2 + wfs2_n_subap])).sum(1) / (shwfs_centroids.shape[1] - 1)) if mapping_type == 'median': cova = numpy.median( (shwfs_centroids[subaps1 + wfs1_n_subap] * (shwfs_centroids[subaps2 + wfs2_n_subap])).sum(1) / (shwfs_centroids.shape[1] - 1)) roi_cov_yy[roi_loc[0], roi_loc[1]] = cova if roi_axis == 'x': roi_covariance[i * allMapPos.shape[1]:(i + 1) * allMapPos.shape[1]] = roi_cov_xx if roi_axis == 'y': roi_covariance[i * allMapPos.shape[1]:(i + 1) * allMapPos.shape[1]] = roi_cov_yy if roi_axis == 'x+y': roi_covariance[i * allMapPos.shape[1]:(i + 1) * allMapPos.shape[1]] = (roi_cov_xx + roi_cov_yy) / 2. if roi_axis == 'x and y': roi_covariance[i * allMapPos.shape[1]:(i + 1) * allMapPos.shape[1]] = numpy.hstack( (roi_cov_xx, roi_cov_yy)) timeStop = time.time() time_taken = timeStop - timeStart return roi_covariance, time_taken
def __init__(self, tp, method, target_array, n_wfs, pupil_mask, subap_diam, wavelength, tel_diam, nx_subap, n_subap, gs_alt, gs_pos, allMapPos, selector, xy_separations, n_layer, layer_alt, track_present, offset_present, fit_track, fit_offset, fit_L0, L0, r0, roi_belowGround, map_axis, roi_envelope, zeroSep_cov, zeroSep_locations, mm, mmc, md, transform_matrix, matrix_xy, huge_matrix, styc_method, print_fitting=True): # d=no self.method = method self.target_array = target_array self.n_wfs = n_wfs self.pupil_mask = pupil_mask self.nx_subap = nx_subap self.n_subap = n_subap self.combs = int(comb(self.n_wfs, 2, exact=True)) self.roi_width = int((2 * roi_envelope) + 1) self.roi_length = int(pupil_mask.shape[0] + roi_belowGround) self.roi_envelope = roi_envelope self.roi_belowGround = roi_belowGround self.n_layer = n_layer self.layer_alt = layer_alt self.zeroSep_cov = zeroSep_cov self.zeroSep_locations = zeroSep_locations self.map_axis = map_axis self.mm = mm self.mmc = mmc self.md = md self.transform_matrix = transform_matrix self.print_fitting = print_fitting self.count = 0 if self.target_array == 'Covariance Map ROI': if self.method == 'Direct Fit': self.generationParams = covariance_roi( tp.pupil_mask, tp.subap_diam, tp.wavelength, tp.tel_diam, tp.n_subap, tp.gs_alt, tp.gs_pos, tp.allMapPos, tp.xy_separations, n_layer, layer_alt, False, tp.track_present, tp.offset_present, fit_track, fit_offset, fit_L0, L0, tp.map_axis, tp.styc_method) if self.method == 'L3S Fit' or self.method == '2SL Fit': onesMat, wfsMat_1, wfsMat_2, allMapPos_acrossMap, selector, xy_separations_acrossMap = roi_referenceArrays( pupil_mask, gs_pos, tel_diam, pupil_mask.shape[0] - 1, roi_envelope) self.generationParams = covariance_roi_l3s( pupil_mask, subap_diam, wavelength, tel_diam, n_subap, gs_alt, gs_pos, allMapPos_acrossMap, xy_separations_acrossMap, self.n_layer, self.layer_alt, False, offset_present, self.roi_belowGround, self.roi_envelope, fit_offset, fit_L0, L0, map_axis, styc_method) else: self.generationParams = covariance_matrix(pupil_mask, subap_diam, wavelength, tel_diam, n_subap, gs_alt, gs_pos, n_layer, layer_alt, False, track_present, offset_present, fit_track, fit_offset, fit_L0, r0, L0, styc_method, matrix_xy=matrix_xy, huge_matrix=huge_matrix)