def flux_tube_radial_profile(region, minmax_pt, surf, peak_or_pit, other_arr=None, mask=None): arr = surf.arr wdist = wraparound_dist_vec(*arr.shape) from field_trace import _level_set if other_arr is None: other_arr = arr seed_points = get_seed_points(region, arr, int(np.sqrt(len(region))), reverse=(peak_or_pit == 'peak')) radial_profile = [] nx, ny = arr.shape for seed in seed_points: seed_flux = arr[seed] nbr_func = lambda t: _cp.neighbors6(t[0], t[1], nx, ny) lset = _level_set(arr, level_val=seed_flux, position=seed, neighbors_func=nbr_func) lset = lset.intersection(region) if not lset: continue if mask is not None: mask[lset.xs, lset.ys] = True lset_arr = other_arr[lset.xs, lset.ys] lset_mean = lset_arr.mean() lset_err = lset_arr.std() / np.sqrt(len(lset_arr)) lset_dists = wdist(lset.xs, lset.ys, minmax_pt[0], minmax_pt[1]) dists_mean = lset_dists.mean() dists_err = lset_dists.std() / np.sqrt(len(lset_dists)) radial_profile.append( (seed, seed_flux, lset_mean, lset_err, dists_mean, dists_err)) return radial_profile
def expand_region_circ(arr, region, minmax, extra=0.0): wdist = wraparound_dist_vec(*arr.shape) region_arr = np.array(list(region), dtype=np.int) xs = region_arr[:,0] ys = region_arr[:,1] dists = wdist(minmax[0], minmax[1], xs, ys) maxdist = max(dists) return expand_region_to_radius(arr, region, maxdist+extra, minmax)
def get_dist_map(regions_0, regions_1, nx, ny): wdist = wraparound_dist_vec(nx, ny) dist_map = np.empty((len(regions_0), len(regions_1)), dtype=np.float_) x0, y0 = regions_0[:,0], regions_0[:,1] x1, y1 = regions_1[:,0], regions_1[:,1] for i, region0 in enumerate(regions_0): dist_map[i, :] = wdist(region0[0], region0[1], x1, y1) return dist_map
def flux_tube_radial_scatter(region, minmax_pt, arr): wdist = wraparound_dist_vec(*arr.shape) region = list(region) dists = [] vals = [] region_arr = np.array(region, dtype=np.int) xs = region_arr[:,0] ys = region_arr[:,1] dists = wdist(minmax_pt[0], minmax_pt[1], xs, ys) vals = arr[xs, ys] return (dists, vals)
def expand_region_to_radius(arr, region, radius, minmax): nx, ny = arr.shape wdist = wraparound_dist_vec(nx, ny) frontier = set() new_region = set(region) for pt in region: nbrs = _cp.neighbors6(pt[0], pt[1], nx, ny) for nbr in nbrs: if nbr not in region and wdist(minmax[0], minmax[1], nbr[0], nbr[1]) <= radius: frontier.add(nbr) new_region.add(nbr) # new_region.update(frontier) while frontier: new_frontier = set() for pt in frontier: nbrs = _cp.neighbors6(pt[0], pt[1], nx, ny) for nbr in nbrs: if nbr not in new_region and wdist(minmax[0], minmax[1], nbr[0], nbr[1]) <= radius: new_frontier.add(nbr) frontier = new_frontier new_region.update(frontier) return new_region
def mag_shear_rad_spokes(psi_arr): nx, ny = psi_arr.shape wdist = wraparound_dist_vec(nx, ny) # mshear = mag_shear(psi_arr) psi_grad_x, psi_grad_y = gradient(psi_arr) bmag = np.sqrt(psi_grad_x**2 + psi_grad_y**2) surf = _cp.TopoSurface(psi_arr) regions = surf.get_minmax_regions() pl.ion() pl.figure() for (minmax, pss, type), region in regions.items(): br, btheta = cartesian_to_polar(-psi_grad_y, psi_grad_x, minmax[0], minmax[1]) if len(region) < 100: continue region = expand_region_circ(psi_arr, region, minmax, extra=10.0) spokes = flux_tube_radial_spokes(region, minmax, psi_arr, type) pl.clf() # max_2_bmags = [] max_2_btheta = [] for spoke in spokes: sp_arr = np.array(spoke) xs, ys = sp_arr[:,0], sp_arr[:,1] # bmags = bmag[xs, ys] bthetas = btheta[xs, ys] max_2_btheta.append((bthetas.max(), bthetas, spoke)) max_2_btheta.sort(key=lambda x: x[0], reverse=True) spokes = [sp for (m, bthetas, sp) in max_2_btheta] btheta_cpy = np.zeros_like(btheta) bmag_cpy = np.zeros_like(bmag) br_cpy = np.zeros_like(br) xs, ys = zip(*region) btheta_cpy[xs, ys] = btheta[xs, ys] bmag_cpy[xs, ys] = bmag[xs, ys] br_cpy[xs, ys] = br[xs, ys] for spoke in spokes: sp_arr = np.array(spoke) xs, ys = sp_arr[:,0], sp_arr[:,1] dists = wdist(minmax[0], minmax[1], xs, ys) bthetas = btheta[xs, ys] # mshears = mshear[xs, ys] pl.subplot(221) pl.plot(dists, bthetas, 'o-') # pl.subplot(222) # pl.plot(dists, mshears, 's-') pl.subplot(222) pl.plot(dists[1:], bthetas[1:]/dists[1:], 'd-') pl.subplot(221) pl.grid() pl.title(r'$B_{\theta}$ vs. $r$') # pl.subplot(222) # pl.grid() # pl.title(r'$\nabla_{\perp} B$ vs. $r$') pl.subplot(222) pl.grid() pl.title(r'$B_{\theta}/r$ vs. $r$') pl.subplot(234) pl.imshow(btheta_cpy, interpolation='nearest', cmap='hot') pl.title(r'$B_{\theta}$') pl.subplot(235) pl.imshow(br_cpy, interpolation='nearest', cmap='hot') pl.title(r'$B_{r}$') pl.subplot(236) pl.imshow(bmag_cpy, interpolation='nearest', cmap='hot') pl.title(r'$|B|$') raw_input('enter to continue') pl.clf() pl.close('all')