|
| 1 | +def cmap_zerocent_scale(plot,scale_factor): |
| 2 | + """ |
| 3 | + Center colormap at zero and scale relative to existing |maximum| value, |
| 4 | + given plot object and scale_factor, a number of type float. |
| 5 | + Returns new colormap limits as new_clim. |
| 6 | + """ |
| 7 | + curr_clim = plot.get_clim() |
| 8 | + new_clim = (scale_factor*np.max(np.abs(curr_clim)))*np.array([-1,1]) |
| 9 | + plot.set_clim(new_clim) |
| 10 | + return new_clim |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +def plot_mask(*args,ax=None,color): |
| 15 | + """ |
| 16 | + Plot mask, given input parameters: |
| 17 | + - X, Y: (optional) coordinates as 1-D or 2-D NumPy arrays or xarray DataArrays |
| 18 | + - mask: 2-D array of boolean values (True/False or 1/0), NumPy or xarray |
| 19 | + - axes: axes to plot on, defaults to current axes |
| 20 | + - color: a string indicating a color in Matplotlib, or a 3-element tuple or NumPy array indicating RGB color values |
| 21 | + |
| 22 | + Returns plot_obj, the plot object of the mask |
| 23 | + """ |
| 24 | + if len(args) == 1: |
| 25 | + mask = args[0] |
| 26 | + else: |
| 27 | + X = args[0] |
| 28 | + Y = args[1] |
| 29 | + mask = args[2] |
| 30 | + # set alpha values to 1 where mask is plotted, 0 otherwise |
| 31 | + if str(type(mask))[0:5] == 'xarray': |
| 32 | + mask = mask.values |
| 33 | + # get color for mask |
| 34 | + if isinstance(color,str): |
| 35 | + import matplotlib.colors as mcolors |
| 36 | + color_rgb = mcolors.to_rgb(color) |
| 37 | + elif (isinstance(color,tuple)) and (len(color) == 3): |
| 38 | + color_rgb = np.asarray(color) |
| 39 | + elif (isinstance(color,np.ndarray)) and (len(color) == 3): |
| 40 | + color_rgb = color |
| 41 | + else: |
| 42 | + raise TypeError("input parameter 'color' has incorrect type or number of elements") |
| 43 | + # create a colormap using a 2x4 array with two RGBA entries |
| 44 | + # the RGB entries are the same in each row |
| 45 | + # in the 1st row alpha=0, in the 2nd row alpha=1 |
| 46 | + cmap_array = np.hstack((np.tile(color_rgb,(2,1)),np.array([[0],[1]]))) |
| 47 | + from matplotlib.colors import ListedColormap |
| 48 | + colormap = ListedColormap(cmap_array) |
| 49 | + # get axis limits of existing plot |
| 50 | + if ax is None: |
| 51 | + ax = plt.gca() |
| 52 | + existing_xlim = ax.get_xlim() |
| 53 | + existing_ylim = ax.get_ylim() |
| 54 | + # plot mask using colormap just created, with alpha=1 where mask=1 or True |
| 55 | + if len(args) == 1: |
| 56 | + plot_obj = ax.pcolormesh(mask,cmap=colormap,vmin=0.,vmax=1.,zorder=50) |
| 57 | + else: |
| 58 | + plot_obj = ax.pcolormesh(X,Y,mask,cmap=colormap,vmin=0.,vmax=1.,zorder=50) |
| 59 | + # set axis limits of mask to axis limits of existing plot |
| 60 | + ax.set_xlim(existing_xlim) |
| 61 | + ax.set_ylim(existing_ylim) |
| 62 | + |
| 63 | + return plot_obj |
| 64 | + |
| 65 | + |
| 66 | + |
1 | 67 | def geos_vel_compute(dens_press_filename,grid_filename="~/Downloads/ECCO_V4r4_PODAAC/ECCO_L4_GEOMETRY_LLC0090GRID_V4R4/GRID_GEOMETRY_ECCO_V4r4_native_llc0090.nc",fc_filename="llc_13tile_fc.txt"): |
2 | 68 | """ |
3 | 69 | This routine computes geostrophic velocities from an input netCDF file containing ECCO v4r4 density and pressure anomalies on the native llc90 grid. |
|
0 commit comments