# ds.sel(time=core_pp.get_subdates(pd.to_datetime(ds.time.values), start_end_date=('06-01', '08-31'))).mean(dim='time').plot() #%% import make_country_mask orography = os.path.join(user_dir, 'surfdrive/ERA5/input_raw/Orography.nc') xarray, Country = make_country_mask.create_mask(var_filename, kwrgs_load={'selbox': selbox}, level='Countries') if domain == 'USCA': mask_US_CA = np.logical_or(xarray.values == Country.US, xarray.values == Country.CA) elif domain == 'US': mask_US_CA = xarray.values == Country.US # xr_mask = xarray.where(mask_US_CA) xr_mask = xarray.where(make_country_mask.binary_erosion(mask_US_CA)) # xr_mask = xarray.where(make_country_mask.binary_erosion(np.nan_to_num(xr_mask))) xr_mask.values[~np.isnan(xr_mask)] = 1 xr_mask = find_precursors.xrmask_by_latlon(xr_mask, upper_right=(270, 63)) # mask small Western US Island xr_mask = find_precursors.xrmask_by_latlon(xr_mask, bottom_left=(228, 58)) # add Rocky mask geo_surf_height = core_pp.import_ds_lazy( orography, var='z_NON_CDM', selbox=selbox) / 9.81 geo_surf_height = geo_surf_height.drop('time').drop('realization') plot_maps.plot_corr_maps(geo_surf_height, cmap=plt.cm.Oranges, clevels=np.arange(0, 2600, 500)) max_height = 1500 mask_Rockies = geo_surf_height < max_height plot_maps.plot_labels(mask_Rockies)
'color': colors, 'edgecolors': 'black' } ] ]] # regions= list(np.unique(xrclustered)[~np.isnan(np.unique(xrclustered))]) # if region == 'USCA': # dic = {4:3, 3:4} # else: # dic = {2:3, 3:2} # xrclustered = find_precursors.view_or_replace_labels(xrclustered.copy(), regions, # [int(dic.get(n, n)) for n in regions]) if region == 'USCA': mask_cl_n = find_precursors.view_or_replace_labels(xrclustered.copy(), [1, 5]) mask_cl_n = make_country_mask.binary_erosion(~np.isnan(mask_cl_n)) mask_cl_s = ~np.isnan( find_precursors.view_or_replace_labels(xrclustered.copy(), [2])) mask_cl = ~np.logical_or(mask_cl_n, mask_cl_s) elif region == 'USCAnew': mask_cl = find_precursors.view_or_replace_labels(xrclustered.copy(), [1, 4]) mask_cl = np.isnan(mask_cl) elif region == 'init': mask_cl_e = find_precursors.view_or_replace_labels(xrclustered.copy(), [3]) mask_cl_e = make_country_mask.binary_erosion(~np.isnan(mask_cl_e)) mask_cl_w = ~np.isnan( find_precursors.view_or_replace_labels(xrclustered.copy(), [1])) mask_cl = ~np.logical_or(mask_cl_w, mask_cl_e) title = np.array([['Clustered simultaneous high temperature events']])
# xarray, Country = make_country_mask.create_mask(var_filename, kwrgs_load={'selbox':selbox}, level='Countries') # mask_US = (xarray.values == Country.US) # mask_US = make_country_mask.binary_erosion(mask_US) # mask_US = make_country_mask.binary_erosion(mask_US) # mask_US = make_country_mask.binary_opening(mask_US) # xr_mask = xarray.where(mask_US) # xr_mask.values[mask_US] = 1 # xr_mask = cl.mask_latlon(xr_mask, latmax=63, lonmax=270) selbox = (232, 295, 25, 50) xr_mask = core_pp.import_ds_lazy( user_dir + '/surfdrive/Scripts/rasterio/mask_North_America_0.25deg_orig.nc', var='lsm', selbox=selbox) xr_mask.values = make_country_mask.binary_erosion(xr_mask.values) plot_maps.plot_labels(xr_mask) # In[9]: # ============================================================================= # Clustering co-occurence of anomalies # ============================================================================= q = [80, 85, 90, 95] n_clusters = [2, 3, 4, 5, 6, 7, 8] tfreq = 1 from time import time t0 = time() xrclustered, results = cl.dendogram_clustering(var_filename, mask=xr_mask, kwrgs_load={ 'tfreq': tfreq,