Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions esmvaltool/diag_scripts/weighting/weighted_temperature_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@
import matplotlib.pyplot as plt
import numpy as np
from cartopy.mpl.ticker import LatitudeFormatter, LongitudeFormatter
from climwip.io_functions import (
log_provenance,
read_model_data,
)
from climwip.io_functions import log_provenance, read_model_data

from esmvaltool.diag_scripts.shared import (
run_diagnostic,
get_plot_filename,
get_diagnostic_filename,
get_plot_filename,
run_diagnostic,
)
from esmvaltool.diag_scripts.weighting.plot_utilities import (
calculate_percentiles,
Expand All @@ -30,6 +27,37 @@
logger = logging.getLogger(os.path.basename(__file__))


def set_antimeridian(dataarray, to: str):
"""Flip the antimeridian (i.e. longitude discontinuity) between Europe
(i.e., [0, 360)) and the Pacific (i.e., [-180, 180)).

Parameters
----------
- dataarray : xarray.DataArray
- to : string, {'pacific', 'europe'}
* 'europe': Longitude will be in [0, 360)
* 'pacific': Longitude will be in [-180, 180)

Returns
-------
dataarray : xarray.DataArray
"""
lon = dataarray['lon']

if to.lower() == 'europe':
dataarray = dataarray.assign_coords(lon=lon % 360)
elif to.lower() == 'pacific':
dataarray = dataarray.assign_coords(lon=((lon + 180) % 360) - 180)
else:
errmsg = "to has to be one of ['europe', 'pacific'] not {}".format(to)
raise ValueError(errmsg)

idx = np.argmin(dataarray['lon'].values)
dataarray = dataarray.roll(lon=-idx, roll_coords=True)
dataarray['lon'].attrs = lon.attrs
return dataarray


def mapplot(dataarray, cfg, title_pattern, filename_part, ancestors,
**colormesh_args):
"""Visualize weighted temperature."""
Expand All @@ -44,6 +72,9 @@ def mapplot(dataarray, cfg, title_pattern, filename_part, ancestors,
proj = ccrs.PlateCarree(central_longitude=0)
figure, axes = plt.subplots(subplot_kw={'projection': proj})

dataarray = set_antimeridian(dataarray, cfg.get('antimeridian', 'pacific'))
dataarray = dataarray.dropna('lon', how='all').dropna('lat', how='all')

dataarray.plot.pcolormesh(
ax=axes,
transform=ccrs.PlateCarree(),
Expand Down Expand Up @@ -104,7 +135,7 @@ def visualize_and_save_difference(temperature_difference, cfg: dict,
ancestors: list):
"""Wrap mapplot: temperature difference between weighted and unweighted."""
title_pattern = '\n'.join([
'Difference: weighted minus unweighted {metric} temperature',
'Weighted minus unweighted {metric} temperature',
r'{period} ($\degree$C)',
])
filename_part = 'temperature_change_difference_map'
Expand Down
4 changes: 2 additions & 2 deletions esmvaltool/recipes/recipe_climwip_test_basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ preprocessors:
mask_landsea:
mask_out: sea
extract_region:
start_longitude: 0.0
start_longitude: -10.0
end_longitude: 39.0
start_latitude: 30.0
end_latitude: 76.25
Expand Down Expand Up @@ -134,5 +134,5 @@ diagnostics:
weights: 'weights.nc'
# optional arguments
model_aggregation: mean # [ mean (default) | median | integer in (0, 100) ]
xticks: [0, 10, 20, 30, 40] # if not given ticks will be set automatically
xticks: [-10, 0, 10, 20, 30, 40] # if not given ticks will be set automatically
yticks: [30, 40, 50, 60, 70, 80]