def find_ridges(x): if 'time' in x.dims: x = x.isel(time=0) return find_ridges_spherical_hessian(x, sigma=4, tolerance_threshold=0.0015e-3, scheme='second_order', return_eigvectors=True)[return_idx]
ftle_local = ftle_local.isel(time=0) ftle = ftle.isel(time=0) from skimage.filters import threshold_otsu, threshold_local from skimage.segmentation import watershed from skimage.feature import peak_local_max from scipy import ndimage as ndi block_size = 301 local_thresh = ftle_local.copy(data=threshold_local(ftle_local.values, block_size, offset=-.8)) binary_local = ftle_local > local_thresh ftle_local_high=binary_local distance = binary_local ridges, eigmin, eigvectors = find_ridges_spherical_hessian(ftle, sigma=1.2, scheme='second_order', angle=20 * coarse_factor, return_eigvectors=True) ridges = ridges.copy(data=skeletonize(ridges.values)) ridges.plot() plt.show() ridges = filter_ridges(ridges, ftle, criteria=['mean_intensity', 'major_axis_length'], thresholds=[1.2, 30 / coarse_factor]) ridges = ridges.where(~xr.ufuncs.isnan(ridges), 0) mslpres_coarse = mslpres_coarse.interp(latitude=ridges.latitude.values, longitude=ridges.longitude.values, method='linear') dpdx = mslpres_coarse.isel(time=-1).differentiate('longitude') dpdy = mslpres_coarse.isel(time=-1).differentiate('latitude')
days = config['lcs_time_len'] / 4 print(file) da = xr.open_dataarray(file) da = np.log(da) / days ridges_l = [] for idx, time in enumerate(da.time.values): print(f'Doing time {idx} of {da.time.values.shape[0]}') da_ = da.sel(time=time) # ridges, _ = find_ridges_spherical_hessian(da_, # sigma=1, # scheme='second_order', # angle=20) if relaxed: ridges, eigmin = find_ridges_spherical_hessian( da_, sigma=None, tolerance_threshold=0.5, return_eigvectors=False) ridges = ridges.where(eigmin < eigmin.quantile(.2)).where( da_ > 1.5) else: ridges, eigmin = find_ridges_spherical_hessian( da_, tolerance_threshold=0.0015e-3, sigma=None, scheme='second_order') ridges = ridges.where(eigmin < -.8e-11, 0) ridges = filter_ridges(ridges, da_,
tmp.plot() plt.show() def_tensor = flowmap_gradient(da, da, sigma=1.5) def_tensor = def_tensor.stack({'points': ['latitude', 'longitude']}) def_tensor = def_tensor.dropna(dim='points') vals = def_tensor.transpose(..., 'points').values vals = vals.reshape([2, 2, def_tensor.shape[-1]]) def_tensor_norm_tmp = norm(vals, axis=(0, 1), ord=2) def_tensor_norm_tmp = def_tensor.isel(derivatives=0).drop('derivatives').copy( data=def_tensor_norm_tmp) def_tensor_norm_tmp = def_tensor_norm_tmp.unstack('points') def_tensor_norm_tmp.plot() plt.show() ridges_tmp, eivals_temp, eigvectors_tmp = find_ridges_spherical_hessian( da, sigma=1.2, scheme='second_order', angle=50, return_eigvectors=True) u_tmp = eigvectors_tmp.isel(eigvectors=1) * da / 30 v_tmp = eigvectors_tmp.isel(eigvectors=0) * da / 30 u_tmp_lowres = u_tmp.coarsen(latitude=5, longitude=5, boundary='trim').mean() v_tmp_lowres = v_tmp.coarsen(latitude=5, longitude=5, boundary='trim').mean() fig, axs = plt.subplots( 1, 2, subplot_kw={'projection': ccrs.PlateCarree()}, ) da.plot(ax=axs[0], transform=ccrs.PlateCarree(), add_colorbar=False, robust=True) da.plot(ax=axs[1], add_colorbar=False, robust=True)
import matplotlib.pyplot as plt from skimage.filters import hessian import cartopy.crs as ccrs def gen_data(xx, yy): return np.cos(xx * np.pi/180)*np.cos(12*yy * np.pi/180) dshape = [150, 100] da = xr.DataArray(np.ones(dshape), dims=['latitude', 'longitude'], coords={ 'latitude': np.linspace(-50, 0, 150), 'longitude': np.linspace(-90, -30, 100)} ) xx, yy = np.meshgrid(da.longitude.values, da.latitude.values) da = da.copy(data=gen_data(xx, yy)) ridges, eigmin, eigvectors , gradient= find_ridges_spherical_hessian(da, sigma=None, scheme='second_order', return_eigvectors=True) ridges = ridges.where(eigmin<eigmin.quantile(.1)) # gradient = gradient.copy(data=np.gradient(da.values)) # gradient = gradient.unstack() np.abs(ridges).plot() da.plot() plt.show() fig, ax = plt.subplots(subplot_kw={'projection': ccrs.PlateCarree()}) da.plot.contour(ax=ax) da.where(ridges>0).plot(add_colorbar=True, ax=ax) ax.quiver(eigvectors.longitude.values, eigvectors.latitude.values, eigvectors.isel(eigvectors=0).values * eigmin.values,
transform=ccrs.PlateCarree(), add_colorbar=False) ax.set_title('') plt.savefig('../tempfigs/fig3_for_scheme.png', dpi=600, transparent=True, bbox_inches='tight', pad_inches=0) plt.close() ftle = ftle.isel(time=0) ridges, _ = find_ridges_spherical_hessian(ftle, sigma=1.2, scheme='second_order', angle=15, ds_wind=ds.isel(time=-1).sel( latitude=ftle.latitude, longitude=ftle.longitude, method='nearest')) valleys, _ = find_ridges_spherical_hessian(-ftle, sigma=1.2, scheme='second_order', angle=15, ds_wind=ds.isel(time=-1).sel( latitude=ftle.latitude, longitude=ftle.longitude, method='nearest')) ridges.plot() plt.show() # ridges = ridges.where(ridges < -9e-10, 0) # Warning: sensitive to sigma # ridges = ridges.where(ridges >= -9e-10, 1)
import cartopy.crs as ccrs import matplotlib.pyplot as plt import cmasher as cmr import numpy as np datapath = '/home/gab/phd/data/case_study_helen/' files = glob.glob(datapath + 'ftle_1989*') da = xr.open_mfdataset(files).to_array().isel(variable=0).drop('variable').load() ridges_list = [] for idx, time in enumerate(da.time.values): da_ = da.isel(time=idx) da_ = np.log(da_) ridges, eigmin, ridges_, eigvectors, gradient, angle = find_ridges_spherical_hessian(da_, sigma=None, tolerance_threshold=0.5, return_eigvectors=True) ridges_valid = ridges.where(eigmin < eigmin.quantile(.2)).where(da_ > 1.5) fig, ax = plt.subplots(1, 1, subplot_kw={'projection': ccrs.NearsidePerspective(0, 45)}) da_.plot(transform=ccrs.PlateCarree(), ax=ax, cmap=cmr.freeze, vmin=0, vmax=3, add_colorbar=False) ridges_valid.where(xr.ufuncs.isnan(ridges_valid), 1).plot(transform=ccrs.PlateCarree(), alpha=.5, ax=ax, cmap='Reds', add_colorbar=False) ax.coastlines(color='red') plt.savefig(f'figs/{idx:02d}.png', dpi=400) plt.close() ridges_list.append(ridges_valid) ridges = xr.concat(ridges_list, dim='time').drop('quantile') ridges = ridges.where(xr.ufuncs.isnan(ridges), 1) ridges.isel(time=0).plot()
preprocess=lambda x: x.isel(time=[-1])).assign_coords(time=das.time.values) y_traj = xr.open_mfdataset( y_traj_files, preprocess=lambda x: x.isel(time=[-1])).assign_coords(time=das.time.values) das = das['__xarray_dataarray_variable__'].load() for time in das.time.values: ftle_ = .5 * np.log(das.sel(time=time)) (ftle_).plot( vmin=0, vmax=3, ) plt.show() x_traj_ = x_traj.sel(time=time)['u'] y_traj_ = y_traj.sel(time=time)['u'] protoridges, eigmin = find_ridges_spherical_hessian( ftle_, tolerance_threshold=0.01e-3, sigma=None, scheme='second_order') ridges = protoridges.where(eigmin < -1.2e-10, 0) ridges = ridges.where(ftle_ > 1.2, 0) ridges = ridges.where(ridges == 0, 1) ridges = filter_ridges(ridges, ftle_, criteria=[ 'mean_intensity', 'area', ], thresholds=[.8, 20]) # ridges = ridges.where(ftle_ > .9) # ridges.where(ftle_>1).plot() # plt.show()