else: ax.text(xtick, ytick, label, fontsize=14, horizontalalignment='center', verticalalignment='center', transform=ccrs.Geodetic()) # Get slice of data at the 0th timestep - plot this contour line separately # because it will be thicker than the other contour lines p = ds.HGT.isel(time=0) # Use geocat-viz utility function to handle the no-shown-data # artifact of 0 and 360-degree longitudes slon = gvutil.xr_add_cyclic_longitudes(p, "lon") # Plot contour data at pressure level 5500 at the first timestep p = slon.plot.contour(ax=ax, transform=ccrs.PlateCarree(), linewidths=1.5, levels=[5500], colors='black', add_labels=False) # Create a color list for each of the next 18 contours colorlist = [ "crimson", "green", "blue", "yellow", "cyan", "hotpink", "crimson", "skyblue", "navy", "lightyellow", "mediumorchid", "orange", "slateblue", "palegreen", "magenta", "springgreen", "pink", "forestgreen", "violet" ]
import geocat.datafiles as gdf from geocat.viz import cmaps as gvcmaps from geocat.viz import util as gvutil ############################################################################### # Read in data: # Open a netCDF data file using xarray default engine and load the data into xarrays ds = xr.open_dataset(gdf.get("netcdf_files/atmos.nc"), decode_times=False) # Extract slice of data V = ds.V.isel(time=0, lev=3) # Fix the artifact of not-shown-data around 0 and 360-degree longitudes V = gvutil.xr_add_cyclic_longitudes(V, "lon") ############################################################################### # Plot: # Generate figure (set its size (width, height) in inches) fig = plt.figure(figsize=(10, 6)) # Generate axes using Cartopy and draw coastlines ax = plt.axes(projection=ccrs.PlateCarree()) ax.coastlines(linewidths=0.5, alpha=0.6) # Use geocat.viz.util convenience function to set axes limits & tick values gvutil.set_axes_limits_and_ticks(ax, xlim=(-180, 180), ylim=(-90, 90),
import xarray as xr import cartopy.crs as ccrs import matplotlib.pyplot as plt import geocat.datafiles as gdf from geocat.viz import util as gvutil ############################################################################### # Read in data: # Open a netCDF data file using xarray default engine and load the data into xarrays ds = xr.open_dataset(gdf.get("netcdf_files/atmos.nc"), decode_times=False) t = ds.TS.isel(time=0) ############################################################################### # Fix the artifact of not-shown-data around 0 and 360-degree longitudes wrap_t = gvutil.xr_add_cyclic_longitudes(t, "lon") ############################################################################### #Plot: # Generate figure (set its size (width, height) in inches) fig = plt.figure(figsize=(10, 10)) # Generate axes using Cartopy and draw coastlines with ax = plt.axes( projection=ccrs.Orthographic(central_longitude=-120, central_latitude=50)) # Set extent to include latitudes between 0 and 90, and longitude between # 0 and -180 only ax.set_extent([0, -180, 0, 90], ccrs.PlateCarree()) ax.set_global()
import matplotlib.path as mpath import matplotlib.ticker as mticker import geocat.datafiles as gdf from geocat.viz import util as gvutil ############################################################################### # Read in data: # Open a netCDF data file using xarray default engine and load the data into xarrays ds = xr.open_dataset(gdf.get("netcdf_files/uv300.nc")) U = ds.U[1, :, :] ############################################################################### # Fix the artifact of not-shown-data around 0 and 360-degree longitudes wrap_U = gvutil.xr_add_cyclic_longitudes(U, "lon") ############################################################################### # Plot: # Generate axes, using Cartopy, drawing coastlines, and adding features fig = plt.figure(figsize=(10, 10)) projection = ccrs.NorthPolarStereo() ax = plt.axes(projection=projection) ax.add_feature(cfeature.LAND, facecolor='lightgray') # Set map boundary to include latitudes between 0 and 40 and longitudes # between -180 and 180 only gvutil.set_map_boundary(ax, [-180, 180], [0, 40], south_pad=1) # Set draw_labels to False so that you can manually manipulate it later
import geocat.datafiles as gdf import geocat.viz.util as gvutil ############################################################################### # Read in data: # Open a netCDF data file using xarray default engine and load the data into xarrays ds = xr.open_dataset(gdf.get("netcdf_files/h_avg_Y0191_D000.00.nc"), decode_times=False) data0 = ds.T.isel(time=0, drop=True).isel(z_t=0, drop=True) data1 = ds.T.isel(time=0, drop=True).isel(z_t=5, drop=True) data2 = ds.S.isel(time=0, drop=True).isel(z_t=0, drop=True) data3 = ds.S.isel(time=0, drop=True).isel(z_t=3, drop=True) data0 = gvutil.xr_add_cyclic_longitudes(data0, "lon_t") data1 = gvutil.xr_add_cyclic_longitudes(data1, "lon_t") data2 = gvutil.xr_add_cyclic_longitudes(data2, "lon_t") data3 = gvutil.xr_add_cyclic_longitudes(data3, "lon_t") data = [[data0, data1], [data2, data3]] ############################################################################### # Plot without extra whitespace: projection = ccrs.NorthPolarStereo() fig, axs = plt.subplots(2, 2, figsize=(8, 8), subplot_kw=dict(projection=projection)) # Format axes and inset axes for color bars
ds = xr.open_dataset(gdf.get("netcdf_files/b003_TS_200-299.nc"), decode_times=False) # Extract slice of data at first timestep TS_0 = ds.TS.isel(time=0).drop('time') # Calculate zonal mean mean = TS_0.mean(dim='lon') # Calculate deviation from time average by finding the temperatures averaged # over all timesteps. Then that average is subtracted from the first timestep time_avg = ds.TS.mean(dim='time') time_dev = TS_0 - time_avg # Fix the artifact of not-shown-data around 0 and 360-degree longitudes TS_0 = gvutil.xr_add_cyclic_longitudes(TS_0, "lon") time_dev = gvutil.xr_add_cyclic_longitudes(time_dev, "lon") ############################################################################## # Plot: # Specify projection for maps proj = ccrs.PlateCarree() # Generate figure (set its size (width, height) in inches) fig = plt.figure(figsize=(8, 8)) # Create girdspec for layout, width_ratio is used to make the plots on the # right narrower than the ones on the left grid = fig.add_gridspec(ncols=2, nrows=2,
# Open a netCDF data file using xarray default engine and # load the data into xarrays ds = xr.open_dataset(gdf.get("netcdf_files/slp.1963.nc"), decode_times=False) # Get data from the 21st timestep pressure = ds.slp[21, :, :] # Translate float values to short values pressure = pressure.astype('float32') # Convert Pa to hPa data pressure = pressure * 0.01 # Fix the artifact of not-shown-data around 0 and 360-degree longitudes wrap_pressure = gvutil.xr_add_cyclic_longitudes(pressure, "lon") ############################################################################### def findLocalExtrema(da, highVal=0, lowVal=1000, eType='Low'): """ Utility function to find local low/high field variable coordinates on a contour map. To classify as a local high, the data point must be greater than highVal, and to classify as a local low, the data point must be less than lowVal. Args: da: (:class:`xarray.DataArray`): Xarray data array containing the lat, lon, and field variable (ex. pressure) data values highVal (:class:`int`): Data value that the local high must be greater than to qualify as a "local high" location. Default highVal is 0. lowVal (:class:`int`):
import geocat.datafiles as gdf from geocat.viz import cmaps as gvcmaps import geocat.viz.util as gvutil ############################################################################### # Read in data: # Open a netCDF data file using xarray default engine and load the data into xarrays ds = xr.open_dataset(gdf.get("netcdf_files/uv300.nc")) # Extract data from second timestep time_0 = ds.isel(time=0).drop_vars('time') time_1 = ds.isel(time=1).drop_vars('time') # Ensure longitudes range from 0 to 360 degrees U_0 = gvutil.xr_add_cyclic_longitudes(time_0.U, "lon") U_1 = gvutil.xr_add_cyclic_longitudes(time_1.U, "lon") ############################################################################### # Create helper functions: def format_linegraph_axes(ax): """ Format the axes limits, tick marks, and tick labels for the line graphs Args: ax (:class: 'matplotlib.Axes'): The set of axes to be manipulated """ # Use geocat.viz.util convenience function to set axes tick values
# Read in data: # Open a netCDF data file using xarray default engine and load the data into xarrays ds = xr.open_dataset( gdf.get("netcdf_files/atmos.nc"), decode_times=False ) # Disable time decoding due to missing necessary metadata # Extract a slice of the data ds = ds.isel(time=0).drop("time") ############################################################################### # Data Masking: # Use xarray.DataArray.where() function to mask out land and then ocean data land_only = ds.TS.where(ds.ORO == 1.0) ocean_only = ds.TS.where(ds.ORO == 0.0) land_only = gvutil.xr_add_cyclic_longitudes(land_only, "lon") ocean_only = gvutil.xr_add_cyclic_longitudes(ocean_only, "lon") ############################################################################### # Plot Ocean Only: # Generate figure (set its size (width, height) in inches) plt.figure(figsize=(10, 6)) # Generate axes using Cartopy and draw coastlines projection = ccrs.PlateCarree() ax = plt.axes(projection=projection) ax.coastlines(linewidth=0.5, resolution="110m") # Import an NCL colormap, truncating it by using geocat.viz.util convenience function newcmp = gvutil.truncate_colormap(gvcmaps.BlAqGrYeOrRe,
import matplotlib.pyplot as plt import geocat.datafiles as gdf from geocat.viz import cmaps as gvcmaps from geocat.viz import util as gvutil ############################################################################### # Read in data: # Open a netCDF data file using xarray default engine and load the data into xarrays ds = xr.open_dataset(gdf.get("netcdf_files/atmos.nc"), decode_times=False) # Extract variable v = ds.V.isel(time=0, lev=3) # Fix the artifact of not-shown-data around 0 and 360-degree longitudes wrap_v = gvutil.xr_add_cyclic_longitudes(v, "lon") ############################################################################### # Plot: # Generate figure (set its size (width, height) in inches) fig = plt.figure(figsize=(10, 10)) # Generate axes using Cartopy and draw coastlines ax = plt.axes(projection=ccrs.PlateCarree()) ax.coastlines(linewidths=0.5) # Import an NCL colormap newcmp = gvcmaps.wgne15 # Contourf-plot data (for filled contours)
import geocat.datafiles as gdf from geocat.viz import util as gvutil ############################################################################## # Read in data: # Open a netCDF data file using xarray and load the data into xarrays # Disable time decoding due to missing necessary metadata ds = xr.open_dataset(gdf.get("netcdf_files/atmos.nc"), decode_times=False) # Extract a slice of the data at first time step ds = ds.isel(time=0).drop("time") TS = ds.TS # Fix the artifact of not-shown-data around 0 and 360-degree longitudes TS = gvutil.xr_add_cyclic_longitudes(TS, "lon") ############################################################################## # Plot: # Generate figure (set its size (width, height) in inches) fig = plt.figure(figsize=(10, 6)) # Generate axes using Cartopy and draw land masses, coastlines, and lakes ax = plt.axes(projection=ccrs.PlateCarree()) ax.add_feature(cfeature.LAND, facecolor='lightgray', zorder=1) ax.add_feature(cfeature.COASTLINE, linewidth=0.5, zorder=1) ax.add_feature(cfeature.LAKES, linewidth=0.5, edgecolor='black', facecolor='None',
import matplotlib.gridspec as gridspec import numpy as np import xarray as xr import geocat.datafiles as gdf import geocat.viz.util as gvutil ############################################################################## # Read in data: # Open a netCDF data file using xarray default engine and load the data into # xarrays ds = xr.open_dataset(gdf.get("netcdf_files/h_avg_Y0191_D000.00.nc"), decode_times=False) # Ensure longitudes range from 0 to 360 degrees t = gvutil.xr_add_cyclic_longitudes(ds.T, "lon_t") # Selecting the first time step and then the three levels of interest t = t.isel(time=0) t_1 = t.isel(z_t=0) t_2 = t.isel(z_t=1) t_6 = t.isel(z_t=5) ############################################################################## # Plot: fig = plt.figure(figsize=(8, 12)) grid = gridspec.GridSpec(nrows=3, ncols=1, figure=fig) # Choose the map projection proj = ccrs.PlateCarree()
############################################################################### # Read in data: # Open a netCDF data file using xarray default engine and load the data into xarrays ds = xr.open_dataset(gdf.get("netcdf_files/b003_TS_200-299.nc"), decode_times=False) x = ds.TS # Apply mean reduction from coordinates as performed in NCL's dim_rmvmean_n_Wrap(x,0) # Apply this only to x.isel(time=0) because NCL plot plots only for time=0 newx = x.mean('time') newx = x.isel(time=0) - newx # Fix the artifact of not-shown-data around 0 and 360-degree longitudes newx = gvutil.xr_add_cyclic_longitudes(newx, "lon") ############################################################################### # Plot: # Generate figure (set its size (width, height) in inches) plt.figure(figsize=(12, 7.2)) # Generate axes using Cartopy projection projection = ccrs.PlateCarree() ax = plt.axes(projection=projection) # Use global map and draw coastlines ax.set_global() ax.coastlines(linewidth=0.5, resolution="110m")
import geocat.datafiles as gdf from geocat.viz import cmaps as gvcmaps import geocat.viz.util as gvutil ############################################################################### # Read in data: # Open a netCDF data file using xarray default engine and load the data into xarrays ds = xr.open_dataset(gdf.get("netcdf_files/uv300.nc")) # Extract data from second timestep ds = ds.isel(time=1).drop_vars('time') # Ensure longitudes range from 0 to 360 degrees U = gvutil.xr_add_cyclic_longitudes(ds.U, "lon") V = gvutil.xr_add_cyclic_longitudes(ds.V, "lon") # Thin data to only include every fourth values U = U[::4, ::4] V = V[::4, ::4] # Calculate the magnitude of the winds magnitude = np.sqrt(U.data**2 + V.data**2) ############################################################################### # Plot: # Create sublots and specify their projections projection = ccrs.PlateCarree() fig, axs = plt.subplots(2,
import geocat.datafiles as gdf from geocat.viz import util as gvutil ############################################################################### # Read in data: # Open a netCDF data file using xarray default engine and load the data into xarrays ds = xr.open_dataset(gdf.get("netcdf_files/b003_TS_200-299.nc"), decode_times=False) # Extract slice of the data temp = ds.TS.isel(time=43).drop_vars(names=['time']) # Convert from Celsius to Kelvin temp.data = temp.data - 273.15 # Fix the artifact of not-shown-data around 0 and 360-degree longitudes temp = gvutil.xr_add_cyclic_longitudes(temp, "lon") ############################################################################### # Plot: # Generate figure (set its size (width, height) in inches) plt.figure(figsize=(12, 6)) # Generate axes using Cartopy projection projection = ccrs.PlateCarree() ax = plt.axes(projection=projection) ax.set_extent([-180, 180, -70, 70], crs=projection) # Draw land ax.add_feature(cfeature.LAND, color='silver')
from geocat.viz import util as gvutil from geocat.viz import cmaps as gvcmaps ############################################################################### # Read in data: # Open a netCDF data file using xarray default engine and load the data into xarrays sst = xr.open_dataset(gdf.get("netcdf_files/sst8292a.nc")) olr = xr.open_dataset(gdf.get("netcdf_files/olr7991a.nc")) # Extract data for December 1982 sst = sst.isel(time=11, drop=True).SSTA olr = olr.isel(time=47, drop=True).OLRA # Fix the artifact of not-shown-data around 0 and 360-degree longitudes sst = gvutil.xr_add_cyclic_longitudes(sst, 'lon') olr = gvutil.xr_add_cyclic_longitudes(olr, 'lon') ############################################################################### # Plot: # Generate figure and axes plt.figure(figsize=(8, 8)) # Set axes projection ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=-160)) ax.set_extent([100, 300, -60, 60], crs=ccrs.PlateCarree()) # Load in color map and specify contour levels cmap = gvcmaps.BlWhRe sst_levels = np.arange(-5.5, 6, 0.5)