def find_seabreezes(region, start_year, final_year, lcstimelen): years = range(start_year, final_year) # ---- File reading and formatting ---- # departures = read_nc_files( region=region, basepath='/group_workspaces/jasmin4/upscale/gmpp/convzones/', filename='SL_repelling_{year}_departuretimelen' + f'_{lcstimelen}_v2.nc', year_range=years, reverseLat=True).isel(time=slice(None, None)) ftle_array = read_nc_files( region=region, basepath='/group_workspaces/jasmin4/upscale/gmpp/convzones/', filename='SL_repelling_{year}_lcstimelen' + f'_{lcstimelen}_v2.nc', year_range=years).isel(time=slice(None, None)) landSea_mask = xr.open_dataarray( '/gws/nopw/j04/primavera1/observations/ERA5/landSea_mask.nc') landSea_mask = landSea_mask.squeeze('time').drop('time') landSea_mask.coords['longitude'] = ( landSea_mask.coords['longitude'].values + 180) % 360 - 180 landSea_mask = landSea_mask.sortby('latitude').sortby('longitude') landSea_mask = landSea_mask.interp_like(ftle_array, method='nearest') landSea_mask.values = landSea_mask.values.round(0) departures.coords['time'] = \ pd.date_range(start=f"{start_year}-01-01T00:00:00", periods=departures.time.shape[0], freq='6H') departures = departures.sortby('latitude') ftle_array = xr.apply_ufunc(lambda x: np.log(x), ftle_array**0.5) threshold = ftle_array.quantile(0.6) dep_x, dep_y = departures.x_departure, departures.y_departure dep_lat, dep_lon = dep_y.where(ftle_array > threshold, np.nan), dep_x.where( ftle_array > threshold, np.nan) # dep_lat = dep_y.copy(data=dep_lat) # dep_lon = dep_x.copy(data=dep_lon) dep_mask = xr.full_like(dep_x, 1) # Selecting only continental points (sort of) dep_lat = dep_lat.where(landSea_mask == 1, drop=True) dep_lon = dep_lon.where(landSea_mask == 1, drop=True) times = dep_lat.time.values array_list = apply_binary_mask(times, dep_lat, dep_lon, landSea_mask) seabreeze = dep_lat.copy(data=np.stack(array_list)) breezetime = seabreeze.where( seabreeze == 1, drop=True).groupby('time.hour').sum('time').stack( points=['latitude', 'longitude']).groupby('points').apply( np.argmax).unstack() # seabreeze = dep_lat.groupby('time').apply(dummy) seabreeze.to_netcdf( f'/group_workspaces/jasmin4/upscale/gmpp/convzones/seabreeze_{start_year}_{final_year}_{region}_lcstimelen_{lcstimelen}.nc' ) return seabreeze
def filter(region, years): ftle_array = read_nc_files( region=region, basepath='/group_workspaces/jasmin4/upscale/gmpp/convzones/', filename='SL_repelling_{year}_lcstimelen_1.nc', year_range=years) ftle_array = ftle_array.expand_dims('dummy') encoder = xru.autoencoder(n_components=7, dims_to_reduce=['dummy'], alongwith=['time'], mode='emd', parallel=True).fit(ftle_array) transformed = encoder.transform(ftle_array) transformed = transformed.transpose('time', 'dim_to_groupby', 'encoded_dims') new_values = transformed.values.reshape(transformed.shape[0], ftle_array.shape[2], ftle_array.shape[3], transformed.shape[2]) new_array = xr.DataArray(new_values, dims=['time', 'latitude', 'longitude', 'modes'], coords=[ transformed.time.values, ftle_array.latitude.values, ftle_array.longitude.values, transformed.encoded_dims.values ])
import os if __name__ == '__main__': region = 'SACZ_big' years = range(1995, 2005) lcstimelen = 16 basin = 'Uruguai' season = 'DJF' basin_origin = 'amazon' MAG = xr.open_dataset('~/phdlib/convlib/data/xarray_mair_grid_basins.nc') mask = MAG[basin] departures = read_nc_files( region=region, basepath='/group_workspaces/jasmin4/upscale/gmpp/convzones/', filename='SL_repelling_{year}_departuretimelen' + f'_{lcstimelen}_v2.nc', year_range=years, season=season, set_date=True, lcstimelen=lcstimelen, binary_mask=mask) ftle_array = read_nc_files( region=region, basepath='/group_workspaces/jasmin4/upscale/gmpp/convzones/', filename='SL_repelling_{year}_lcstimelen' + f'_{lcstimelen}_v2.nc', year_range=years, season=season, binary_mask=mask, lcstimelen=lcstimelen, set_date=True) pr = read_nc_files(region=region, basepath='/gws/nopw/j04/primavera1/observations/ERA5/',
def _identify_features(self, array): array = (array - array.min()) / array.max() for time in array.time.values: array2D = array.sel(time=time).values array2D.shape() blobs_doh = blob_doh(array2D, max_sigma=30, threshold=.01) print(';a') pass def track(self, array): identified_array = self._identify_features(array) if __name__ == '__main__': array = read_nc_files(year_range=range(2000, 2001)) array = array.sel(latitude=slice(-30, None), longitude=slice(-60, -30), time=slice(None, 10)) threshold = array.quantile(0.7) array = array.where(array > threshold, 0) array = array.where(array < threshold, 1) #labeled_array = xr.apply_ufunc(lambda x: measure.label(x), array) labeled_array = xr.apply_ufunc(lambda x: measure.label(x), array.groupby('time')) array = array.expand_dims(groups=np.unique(labeled_array.values)) array_of_sizes = np.zeros( [len(array.time.values), len(array.groups.values)])
region = 'SACZ_big' years = range(2020, 2021) lcstimelen = 6 basin = 'Tiete' season = 'DJF' MAG = xr.open_dataset('data/xarray_mair_grid_basins.nc') MAG = MAG[basin] MAG = MAG.rename({'lat': 'latitude', 'lon': 'longitude'}) datapath_local = '/home/gab/phd/data/' ftle_array = read_nc_files(region=region, basepath=datapath_local + 'FTLE_ERA5/', filename='SL_attracting_{year}_lcstimelen' + f'_{lcstimelen}_v2.nc', year_range=years, season=None, lcstimelen=lcstimelen, set_date=True) ftle_array = xr.apply_ufunc(lambda x: np.log(x), ftle_array** 0.5) * 6 / lcstimelen #ftle_array.roll(time=-2, roll_coords=False) u = read_nc_files(region={ 'latitude': slice(10, -70), 'longitude': slice(-100, -1) }, basepath=datapath_local + '/ERA5/ERA5', filename='viwve_ERA5_6hr_{year}010100-{year}123118.nc', year_range=years, transformLon=True, reverseLat=False,
import xarray as xr from convlib.xr_tools import read_nc_files, createDomains import numpy as np area = {'latitude': slice(-15, -10), 'longitude': slice(-50, -38)} region = "SACZ_big" years = range(1995, 2005) lcstimelen = 8 # 8 season = "DJF" CZ = 1 ftle_array = read_nc_files( region=region, basepath='/group_workspaces/jasmin4/upscale/gmpp/convzones/', filename='SL_repelling_{year}_lcstimelen' + f'_{lcstimelen}_v2.nc', year_range=years, season=season) ftle_array = xr.apply_ufunc(lambda x: np.log(x), ftle_array**0.5) ftle_array = ftle_array.sel(area).groupby('time').mean() ftle_array.to_netcdf('/home/users/gmpp/sacz_index.nc')
axarr[2].coastlines() # axarr.add_feature(states_provinces, edgecolor='gray') plt.savefig(f'./tempfigs/SL{time}.png') plt.close() domains = dict(AITCZ=dict(latitude=slice(-5, 15), longitude=slice(-50, -13)), SACZ=dict(latitude=slice(-40, -5), longitude=slice(-62, -20))) if __name__ == '__main__': region = "SACZ_small" lcs_time_len = 1 arr1 = read_nc_files( region=region, basepath='/group_workspaces/jasmin4/upscale/gmpp/convzones/', filename='SL_repelling_{year}' + f'_lcstimelen_1_v2.nc', year_range=range(1990, 2000)) arr4 = read_nc_files( region=region, basepath='/group_workspaces/jasmin4/upscale/gmpp/convzones/', filename='SL_repelling_{year}' + f'_lcstimelen_4_v2.nc', year_range=range(1990, 2000)) # arr = xr.open_dataarray('/home/users/gmpp/out/SL_repelling_1980_1998.nc') sel_hour = 0 hours = [pd.to_datetime(x).hour for x in arr.time.values] #arr = xr.apply_ufunc(lambda x: np.log(x), arr ** 0.5) array_mean = arr.where( np.array(hours).reshape(arr.shape[0], 1, 1) == sel_hour).groupby(
def seabreeze(): region = 'NEBR' lcstimelen = 1 vmin = 0 vmax = 0.6 start_year, final_year = 1995, 1996 years = range(start_year, final_year) # seabreeze = xr.open_dataarray(f'/group_workspaces/jasmin4/upscale/gmpp/convzones/seabreeze_{start_year}_{final_year}_{region}.nc') seabreeze = find_seabreezes(region, start_year, final_year, lcstimelen) pr_array = read_nc_files( region=region, basepath='/gws/nopw/j04/primavera1/observations/ERA5/', filename='pr_ERA5_6hr_{year}010100-{year}123118.nc', year_range=years, transformLon=True, reverseLat=True) pr_array = pr_array.sortby('latitude') * 6 * 3600 # total mm in 6h pr_array = pr_array.sel(latitude=seabreeze.latitude, longitude=seabreeze.longitude, time=seabreeze.time, method='nearest') masked_precip = pr_array.where(seabreeze == 1, 0) # # for time in seabreeze.time: # fig = plt.figure(figsize=[10, 10]) # gs = gridspec.GridSpec(1, 1, width_ratios=[1]) # # axs = {'mask': fig.add_subplot(gs[0, 0], projection=proj)} # # seabreeze.sel(time=time).plot(transform=ccrs.PlateCarree(), add_colorbar=True, # ax=axs['mask'], vmin=0, vmax=1) # axs['mask'].add_feature(cfeature.NaturalEarthFeature( # 'cultural', 'admin_1_states_provinces_lines', scale='50m', # edgecolor='gray', facecolor='none')) # axs['mask'].add_feature(cfeature.RIVERS) # axs['mask'].add_feature(cfeature.COASTLINE) # plt.savefig('tempfigs/seabreeze/{}.png'.format(time.values)) # plt.close() prec_frac = masked_precip.groupby('time.hour').sum( 'time') / pr_array.groupby('time.hour').sum('time') total = 4 * seabreeze.groupby('time.hour').sum( 'time') / seabreeze.time.values.shape[0] prec_frac = prec_frac / total # total = total - total.mean('hour') hours = total.hour.values proj = ccrs.PlateCarree() fig = plt.figure(figsize=[20, 20]) gs = gridspec.GridSpec(2, 3, width_ratios=[1, 1, 0.05]) axs = {} axs[hours[0]] = fig.add_subplot(gs[0, 0], projection=proj) axs[hours[1]] = fig.add_subplot(gs[0, 1], projection=proj) axs[hours[2]] = fig.add_subplot(gs[1, 0], projection=proj) axs[hours[3]] = fig.add_subplot(gs[1, 1], projection=proj) # Disable axis ticks for ax in axs.values(): ax.tick_params(bottom=False, labelbottom=False, left=False, labelleft=False) # Add titles for name, ax in axs.items(): ax.set_title(name) for hour in hours: plot = prec_frac.sel(hour=hour).plot.contourf( ax=axs[hour], transform=ccrs.PlateCarree(), add_colorbar=False, add_labels=False, levels=5, vmax=4, vmin=0, cmap='Blues') CS = total.sel(hour=hour).plot.contour(ax=axs[hour], levels=5, vmin=vmin, vmax=vmax, cmap="Reds") axs[hour].clabel(CS, inline=True, fontsize=15) axs[hour].coastlines(color='black') axs[hour].add_feature( cfeature.NaturalEarthFeature('cultural', 'admin_1_states_provinces_lines', scale='50m', edgecolor='gray', facecolor='none')) cbar_gs = gridspec.GridSpecFromSubplotSpec(1, 1, subplot_spec=gs[:, 2], wspace=2.5) cax = fig.add_subplot(cbar_gs[0]) plt.colorbar(plot, cax) plt.savefig( f'tempfigs/seabreeze/seabreeze_{region}_lcstimelen_{lcstimelen}.png') plt.close() total.stack(points=['latitude', 'longitude']).mean('points').plot() plt.savefig('tempfigs/seabreeze/ciclo_diurno.png')
def precip_fraction(region, years): if region == 'AITCZ': proj = ccrs.PlateCarree() figsize1 = (20, 11) figsize2 = (30, 20) elif 'SACZ' in region: figsize1 = (20, 11) figsize2 = (20, 20) proj = ccrs.Orthographic(-40, -20) print('*---- Reading files ----*') ftle_array = read_nc_files( region=region, basepath='/group_workspaces/jasmin4/upscale/gmpp/convzones/', filename='SL_repelling_{year}_lcstimelen_1.nc', year_range=years) pr_array = read_nc_files( region=region, basepath='/gws/nopw/j04/primavera1/observations/ERA5/', filename='pr_ERA5_6hr_{year}010100-{year}123118.nc', year_range=years, transformLon=True, reverseLat=True) pr_array = pr_array.sortby('latitude') * 6 * 3600 # total mm in 6h ftle_array = xr.apply_ufunc(lambda x: np.log(x), ftle_array**0.5) threshold = ftle_array.quantile(0.6) pr_array = pr_array.sel(latitude=ftle_array.latitude, longitude=ftle_array.longitude, method='nearest') masked_precip = pr_array.where(ftle_array > threshold, 0) # ------ Total precip ------ # fig = plt.figure(figsize=figsize1) gs = gridspec.GridSpec(1, 3, width_ratios=[1, 1, 0.02]) axs = {} axs['Total'] = fig.add_subplot(gs[0, 0], projection=proj) axs['Conv. zone events'] = fig.add_subplot(gs[0, 1], projection=proj) # Disable axis ticks for ax in axs.values(): ax.tick_params(bottom=False, labelbottom=False, left=False, labelleft=False) # Add titles for name, ax in axs.items(): ax.set_title(name) vmin = 0 vmax = pr_array.sum('time').quantile(0.95) masked_precip.sum('time').plot(ax=axs['Conv. zone events'], transform=ccrs.PlateCarree(), add_colorbar=False, add_labels=False, vmin=vmin, vmax=vmax, cmap='nipy_spectral') total = pr_array.sum('time').plot(ax=axs['Total'], transform=ccrs.PlateCarree(), add_colorbar=False, add_labels=False, vmin=vmin, vmax=vmax, cmap='nipy_spectral') axs['Total'].coastlines(color='black') axs['Conv. zone events'].coastlines(color='black') cbar_gs = gridspec.GridSpecFromSubplotSpec(1, 1, subplot_spec=gs[:, 2], wspace=2.5) cax = fig.add_subplot(cbar_gs[0]) plt.colorbar(total, cax) plt.savefig(f'tempfigs/diagnostics/total_{region}.png') plt.close() # ---- Seasonal plots ---- # fraction = masked_precip / pr_array fraction = fraction.groupby('time.season').mean('time') seasons = fraction.season.values fig = plt.figure(figsize=figsize2) gs = gridspec.GridSpec(2, 3, width_ratios=[1, 1, 0.05]) axs = {} axs[seasons[0]] = fig.add_subplot(gs[0, 0], projection=proj) axs[seasons[1]] = fig.add_subplot(gs[0, 1], projection=proj) axs[seasons[2]] = fig.add_subplot(gs[1, 0], projection=proj) axs[seasons[3]] = fig.add_subplot(gs[1, 1], projection=proj) # Disable axis ticks for ax in axs.values(): ax.tick_params(bottom=False, labelbottom=False, left=False, labelleft=False) # Add titles for name, ax in axs.items(): ax.set_title(name) vmin = 0 vmax = fraction.quantile(0.99) for season in seasons: plot = fraction.sel(season=season).plot(ax=axs[season], transform=ccrs.PlateCarree(), add_colorbar=False, add_labels=False, vmin=vmin, vmax=vmax, cmap='nipy_spectral') axs[season].coastlines(color='black') cbar_gs = gridspec.GridSpecFromSubplotSpec(1, 1, subplot_spec=gs[:, 2], wspace=2.5) cax = fig.add_subplot(cbar_gs[0]) plt.colorbar(plot, cax) plt.savefig(f'tempfigs/diagnostics/seasons_{region}.png') plt.close()