def setup_axes(ax, plot_bbox): ax.coastlines(linewidth=0.4, color='#222222') for spine in ax.spines.values(): spine.set_edgecolor('#222222') spine.set_linewidth(0.4) region = maps.get_countries('/home/liam/Downloads').loc['United States of America'].geometry maps.features.add_polygons(ax, region, outline=True, zorder=100, linewidth=1, edgecolor='k') ax.set_extent(plot_bbox)
import matplotlib.pyplot as plt grids = { 'C60-global': xr.open_dataset('/extra-space/C60_global/comparison_grid.nc'), 'C180-global': xr.open_dataset('/extra-space/C180_global/comparison_grid.nc'), 'C180e-US': xr.open_dataset('/extra-space/C180e_US/comparison_grid.nc'), } masks = {} for grid_name, grid in grids.items(): xc = grid['grid_boxes_centers'].isel(XY=0).values yc = grid['grid_boxes_centers'].isel(XY=1).values region = maps.get_countries('/home/liam/Downloads').loc[ 'United States of America'].geometry.simplify(0.1) masks[grid_name] = maps.mask_outside(xc, yc, region) simulations = { 'C60-global': xr.open_dataset('/extra-space/C60_global/GCHP_NO2_July2018.nc'), 'C180-global': xr.open_dataset('/extra-space/C180_global/GCHP_NO2_July2018.nc'), 'C180e-US': xr.open_dataset('/extra-space/C180e_US/GCHP_NO2_July2018.nc'), } tropomi = { 'C180-global': xr.open_dataset('/extra-space/C180_global/TROPOMI_NO2_July2018.nc'), 'C180e-US': xr.open_dataset('/extra-space/C180e_US/TROPOMI_NO2_July2018.nc'),
def compare_grid(grid): grid_xe = grid['lon_b'] grid_ye = grid['lat_b'] grid_xc = grid['lon'] grid_yc = grid['lat'] resolution = np.ones_like(grid_xc) * np.nan xx, yy = np.meshgrid(np.linspace(-180, 180, 1000), np.linspace(-90, 90, 500), indexing='ij') pts = [] data = [] region = maps.get_countries( '/home/liam/Downloads/').loc['United States of America'].geometry in_region = [] face5 = [] for nf in tqdm(range(6)): xe = grid_xe[nf, ...] ye = grid_ye[nf, ...] n = xe.shape[0] logical_x = xe[n // 2, n // 2] logical_y = ye[n // 2, n // 2] laea = pyproj.Proj(f'+proj=laea +lon_0={logical_x} +lat_0={logical_y}') ll = laea.to_latlong() transform = pyproj.Transformer.from_proj(ll, laea, always_xy=True).transform xe_laea, ye_laea = transform(xe, ye) xy_laea = get_minor_xy(xe_laea, ye_laea) for i in range(n - 1): for j in range(n - 1): resolution[nf, i, j] = np.sqrt( shapely.geometry.Polygon(xy_laea[i, j, ...]).area / 1e6) if region.contains( shapely.geometry.Point(grid_xc[nf, i, j], grid_yc[nf, i, j])): in_region.append(resolution[nf, i, j]) if nf == 5: face5.append(resolution[nf, i, j]) pts.extend( np.moveaxis( (grid_xc[nf, ...].flatten(), grid_yc[nf, ...].flatten()), 0, -1)) data.extend(resolution[nf, ...].flatten()) print( f'region stats:\n-- min: {np.min(in_region)}\n-- max: {np.max(in_region)}\n-- avg: {np.mean(in_region)}\n-- count: {len(in_region)}' ) print( f'face5 stats:\n-- min: {np.min(face5)}\n-- max: {np.max(face5)}\n-- avg: {np.mean(face5)}\n-- count: {len(face5)}' ) # pcm = ax.pcolormesh(xe, ye, resolution, transform=ccrs.PlateCarree(), norm=norm, cmap=cmap) # ax.contourf(grid_xc[nf,...], grid_yc[nf,...], resolution, levels=[25, 50, 100, 200, 400, 800, 1600],transform=ccrs.PlateCarree(), norm=norm, cmap=cmap) data = np.array(data) pts = np.array(pts) interp = scipy.interpolate.LinearNDInterpolator(pts, data) xi = np.moveaxis((xx.flatten(), yy.flatten()), 0, -1) d = interp(xi) d = d.reshape(xx.shape) return grid_xe, grid_ye, resolution, grid_xc, grid_yc, xx, yy, d
ds1 = xr.open_dataset( '/home/liam/Downloads/foobar/C180e-US/GCHP_NO2_July2018.nc') ds2 = xr.open_dataset( '/home/liam/Downloads/foobar/C180-global/GCHP_NO2_July2018.nc') grid = xr.open_dataset('/home/liam/Downloads/scratch/comparison_grid.nc') project = pyproj.Transformer.from_crs('epsg:4326', 'epsg:2163', always_xy=True).transform xc = grid['grid_boxes_centers'].isel(XY=0).values yc = grid['grid_boxes_centers'].isel(XY=1).values xc, yc = project(xc, yc) US = maps.get_countries('/home/liam/Downloads').loc[ 'United States of America'].geometry.convex_hull US = shapely.ops.transform(project, US) mask = maps.mask_outside(xc, yc, US) da1 = ds1.GCHP_NO2.values[~mask] da2 = ds2.GCHP_NO2.values[~mask] keep = np.isfinite(da1) & np.isfinite(da2) da1 = da1[keep] da2 = da2[keep] def plot_lognorm(dist, **kwargs): dist = np.log(dist) np.random.choice(dist.size, 50000, replace=False)
grid_a = xr.open_dataset( f'/extra-space/sg-stats/Sept/{A_GRID}/grid_box_outlines_and_centers.nc' ) grid_b = xr.open_dataset( f'/extra-space/sg-stats/Sept/{B_GRID}/grid_box_outlines_and_centers.nc' ) stacked_grid_a = grid_a.drop(['xe', 'ye', 'XdimE', 'YdimE'])\ .stack(boxes=['nf', 'Ydim', 'Xdim'])\ .transpose('boxes', 'POLYGON_PTS', 'XY') stacked_grid_b = grid_b.drop(['xe', 'ye', 'XdimE', 'YdimE']) \ .stack(boxes=['nf', 'Ydim', 'Xdim']) \ .transpose('boxes', 'POLYGON_PTS', 'XY') US = maps.get_countries('/home/liam/Downloads').loc[ 'United States of America'].geometry.buffer(1).simplify(0.5) masked_stacked_indexes_a = mask(grid_a.grid_boxes_centers, US) masked_stacked_indexes_b = mask(grid_b.grid_boxes_centers, US) M = tessellate_a_to_b( stacked_grid_a.grid_boxes.isel(boxes=masked_stacked_indexes_a), stacked_grid_b.grid_boxes.isel(boxes=masked_stacked_indexes_b), ) M = full_tessellation_matrix( M, masked_stacked_indexes_a, masked_stacked_indexes_b, stacked_grid_a.sizes['boxes'], stacked_grid_b.sizes['boxes'],
# Open grid def file grid = xr.open_dataset(args['grid_def']) import maps if args['region'] == 'California': region = maps.get_provinces_and_states(args['shapefiles']).loc['California'].geometry stats_text_pos = dict( x=0.5, y=0.75, horizontalalignment='left', verticalalignment='top', ) road_params = dict(linewidth=0.25, edgecolor=matplotlib.colors.to_rgba('snow', 0.5)) width=1.575 elif args['region'] == 'US': region = maps.get_countries(args['shapefiles']).loc['United States of America'].geometry stats_text_pos = dict( x=0.03, y=0.05, horizontalalignment='left', verticalalignment='bottom', ) road_params = dict(linewidth=0.25, edgecolor=matplotlib.colors.to_rgba('snow', 0.9), alpha=0.6) width=3.27 # width=4.72441/2 crs = ccrs.epsg(2163) plt.figure(figsize=maps.figsize_fitting_polygon(region, crs, width=width), dpi=300) ax = plt.axes(projection=crs) maps.set_extent(ax, region) maps.features.format_page(ax, linewidth_axis_spines=0)