Skip to content

Commit 79bf413

Browse files
authored
Merge pull request #1066 from xylar/add-ocean-region-names-coord
Add `regionNames` coordinate to NetCDF output with "standard" region names
2 parents 16f76a1 + caddb2d commit 79bf413

16 files changed

+214
-103
lines changed

docs/developers_guide/api.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ Ocean tasks
9494
WoaTransects
9595
WoceTransects
9696

97+
Ocean subtasks
98+
--------------
99+
97100
.. currentmodule:: mpas_analysis.ocean.compute_anomaly_subtask
98101

99102
.. autosummary::
@@ -116,6 +119,19 @@ Ocean tasks
116119
PlotHovmollerSubtask
117120

118121

122+
Ocean utilities
123+
---------------
124+
125+
.. currentmodule:: mpas_analysis.ocean.utility
126+
127+
.. autosummary::
128+
:toctree: generated/
129+
130+
add_standard_regions_and_subset
131+
get_standard_region_names
132+
compute_zmid
133+
134+
119135
Sea ice tasks
120136
-------------
121137

docs/users_guide/config/regions.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ within MPAS-Analysis using region mask files::
1515

1616
# list of region names (needs to be in the same order as region indices in
1717
# time-series stats)
18-
regions = ['arctic', 'equatorial', 'so', 'nino3', 'nino4', 'nino3.4', 'global']
19-
# list of plot titles (needs to be in the same order as region indices in
20-
# time-series stats)
21-
plotTitles = ['Arctic', 'Equatorial (15S-15N)', 'Southern Ocean', 'Nino 3',
18+
regionShortNames = ['arctic', 'equatorial', 'so', 'nino3', 'nino4', 'nino3.4',
19+
'global']
20+
# list of full names (e.g. for plot titles) same order as regionShortNames
21+
regionNames = ['Arctic', 'Equatorial (15S-15N)', 'Southern Ocean', 'Nino 3',
2222
'Nino 4', 'Nino 3.4', 'Global Ocean']
2323

2424

docs/users_guide/tasks/timeSeriesOHCAnomaly.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ The following configuration options are available for this task::
2121
## options related to plotting time series of ocean heat content (OHC)
2222
## anomalies from year 1
2323

24-
# list of regions to plot from the region list in [regions] below
25-
regions = ['global']
24+
# list of region shrot names to plot from the region list in [regions] above
25+
regionShortNames = ['global']
2626

2727
# approximate depths (m) separating plots of the upper, middle and lower ocean
2828
depths = [700, 2000]

docs/users_guide/tasks/timeSeriesSST.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ The following configuration options are available for this task::
2020
[timeSeriesSST]
2121
## options related to plotting time series of sea surface temperature (SST)
2222

23-
# list of regions to plot from the region list in [regions] below
24-
regions = ['global']
23+
# list of region shrot names to plot from the region list in [regions] above
24+
regionShortNames = ['global']
2525

2626
# Number of points over which to compute moving average (e.g., for monthly
2727
# output, movingAveragePoints=12 corresponds to a 12-month moving average

docs/users_guide/tasks/timeSeriesSalinityAnomaly.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ The following configuration options are available for this task::
2020
[hovmollerSalinityAnomaly]
2121
## options related to plotting time series of salinity vs. depth
2222

23-
# list of regions to plot from the region list in [regions] below
24-
regions = ['global']
23+
# list of region shrot names to plot from the region list in [regions] above
24+
regionShortNames = ['global']
2525

2626
# Number of points over which to compute moving average(e.g., for monthly
2727
# output, movingAveragePoints=12 corresponds to a 12-month moving average

docs/users_guide/tasks/timeSeriesTemperatureAnomaly.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ The following configuration options are available for this task::
2020
[hovmollerTemperatureAnomaly]
2121
## options related to plotting time series of potential temperature vs. depth
2222

23-
# list of regions to plot from the region list in [regions] below
24-
regions = ['global']
23+
# list of region shrot names to plot from the region list in [regions] above
24+
regionShortNames = ['global']
2525

2626
# Number of points over which to compute moving average(e.g., for monthly
2727
# output, movingAveragePoints=12 corresponds to a 12-month moving average

mpas_analysis/default.cfg

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,11 @@ endYear = 20
358358

359359
# list of region names (needs to be in the same order as region indices in
360360
# time-series stats)
361-
regions = ['arctic', 'equatorial', 'so', 'nino3', 'nino4', 'nino3.4', 'global']
362-
# list of plot titles (needs to be in the same order as region indices in
363-
# time-series stats)
364-
plotTitles = ['Arctic', 'Equatorial (15S-15N)', 'Southern Ocean', 'Nino 3',
365-
'Nino 4', 'Nino 3.4', 'Global Ocean']
361+
regionShortNames = ['arctic', 'equatorial', 'so', 'nino3', 'nino4', 'nino3.4',
362+
'global']
363+
# list of full names (e.g. for plot titles) same order as regionShortNames
364+
regionNames = ['Arctic', 'Equatorial (15S-15N)', 'Southern Ocean', 'Nino 3',
365+
'Nino 4', 'Nino 3.4', 'Global Ocean']
366366

367367

368368
[plot]
@@ -608,8 +608,8 @@ concentrationAltibergSH = Altiberg/Altiberg_1991-2017_20180308.nc
608608
## options related to plotting time series of ocean heat content (OHC)
609609
## anomalies from year 1
610610

611-
# list of regions to plot from the region list in [regions] below
612-
regions = ['global']
611+
# list of region short names to plot from the region list in [regions] above
612+
regionShortNames = ['global']
613613

614614
# approximate depths (m) separating plots of the upper, middle and lower ocean
615615
depths = [700, 2000]
@@ -672,8 +672,8 @@ movingAveragePoints = 12
672672
[hovmollerTemperatureAnomaly]
673673
## options related to plotting time series of potential temperature vs. depth
674674

675-
# list of regions to plot from the region list in [regions] below
676-
regions = ['global']
675+
# list of region short names to plot from the region list in [regions] above
676+
regionShortNames = ['global']
677677

678678
# Number of points over which to compute moving average(e.g., for monthly
679679
# output, movingAveragePoints=12 corresponds to a 12-month moving average
@@ -706,8 +706,8 @@ contourLevelsResult = np.arange(-5.0, 5.51, 0.5)
706706
[hovmollerSalinityAnomaly]
707707
## options related to plotting time series of salinity vs. depth
708708

709-
# list of regions to plot from the region list in [regions] below
710-
regions = ['global']
709+
# list of region short names to plot from the region list in [regions] above
710+
regionShortNames = ['global']
711711

712712
# Number of points over which to compute moving average(e.g., for monthly
713713
# output, movingAveragePoints=12 corresponds to a 12-month moving average
@@ -743,7 +743,7 @@ contourLevelsResult = np.arange(-2., 2.01, 0.2)
743743
## options related to plotting time series of sea surface temperature (SST)
744744

745745
# list of regions to plot from the region list in [regions] below
746-
regions = ['global']
746+
regionShortNames = ['global']
747747

748748
# Number of points over which to compute moving average (e.g., for monthly
749749
# output, movingAveragePoints=12 corresponds to a 12-month moving average

mpas_analysis/ocean/compute_anomaly_subtask.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
from mpas_analysis.shared.time_series import \
2929
compute_moving_avg_anomaly_from_start
3030

31+
from mpas_analysis.ocean.utility import add_standard_regions_and_subset
32+
3133

3234
class ComputeAnomalySubtask(AnalysisTask):
3335
"""
@@ -184,6 +186,9 @@ def run_task(self):
184186
movingAveragePoints=self.movingAveragePoints,
185187
alter_dataset=self.alter_dataset)
186188

189+
if 'nOceanRegions' in ds.dims or 'nOceanRegionsTmp' in ds.dims:
190+
ds = add_standard_regions_and_subset(ds, config)
191+
187192
outFileName = self.outFileName
188193
if not os.path.isabs(outFileName):
189194
baseDirectory = build_config_full_path(

mpas_analysis/ocean/index_nino34.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
from mpas_analysis.shared import AnalysisTask
3939
from mpas_analysis.shared.html import write_image_xml
4040

41+
from mpas_analysis.ocean.utility import add_standard_regions_and_subset
42+
4143

4244
class IndexNino34(AnalysisTask):
4345
"""
@@ -129,7 +131,7 @@ def setup_and_check(self):
129131
regionToPlot = config.get('indexNino34', 'region')
130132

131133
if regionToPlot not in ['nino3.4', 'nino3', 'nino4']:
132-
raise ValueError('Unexpectes El Nino Index region {}'.format(
134+
raise ValueError('Unexpected El Nino Index region {}'.format(
133135
regionToPlot))
134136
ninoIndexNumber = regionToPlot[4:]
135137

@@ -193,9 +195,7 @@ def run_task(self):
193195

194196
# regionIndex should correspond to NINO34 in surface weighted Average
195197
# AM
196-
regions = config.getexpression('regions', 'regions')
197198
regionToPlot = config.get('indexNino34', 'region')
198-
regionIndex = regions.index(regionToPlot)
199199

200200
# Load data:
201201
ds = open_mpas_dataset(fileName=self.inputFile,
@@ -204,6 +204,11 @@ def run_task(self):
204204
startDate=startDate,
205205
endDate=endDate)
206206

207+
ds = add_standard_regions_and_subset(ds, config,
208+
regionShortNames=[regionToPlot])
209+
# we want to collapse the nOceanRegions dimension
210+
ds = ds.isel(nOceanRegions=0)
211+
207212
# Observations have been processed to the nino34Index prior to reading
208213
dsObs = xr.open_dataset(dataPath, decode_cf=False, decode_times=False)
209214
# add the days between 0001-01-01 and the refDate so we have a new
@@ -215,7 +220,7 @@ def run_task(self):
215220
self.logger.info(' Compute El Nino {} Index...'.format(
216221
ninoIndexNumber))
217222
varName = self.variableList[0]
218-
regionSST = ds[varName].isel(nOceanRegions=regionIndex)
223+
regionSST = ds[varName]
219224
nino34Main = self._compute_nino34_index(regionSST, calendar)
220225

221226
# Compute the observational index over the entire time range
@@ -262,7 +267,10 @@ def run_task(self):
262267
calendar=calendar,
263268
variableList=self.variableList)
264269

265-
regionSSTRef = dsRef[varName].isel(nOceanRegions=regionIndex)
270+
dsRef = add_standard_regions_and_subset(
271+
dsRef, self.controlConfig, regionShortNames=[regionToPlot])
272+
273+
regionSSTRef = dsRef[varName]
266274
nino34Ref = self._compute_nino34_index(regionSSTRef, calendar)
267275

268276
nino34s = [nino34Subset, nino34Main[2:-3], nino34Ref[2:-3]]

mpas_analysis/ocean/plot_depth_integrated_time_series_subtask.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
from mpas_analysis.shared.time_series import compute_moving_avg, \
3232
combine_time_series_with_ncrcat
3333

34+
from mpas_analysis.ocean.utility import add_standard_regions_and_subset
35+
3436

3537
class PlotDepthIntegratedTimeSeriesSubtask(AnalysisTask):
3638
"""
@@ -157,7 +159,8 @@ def __init__(self, parentTask, regionName, inFileName, outFileLabel,
157159

158160
if subtaskName is None:
159161
suffix = regionName[0].upper() + regionName[1:]
160-
subtaskName = 'plotDepthIntegratedTimeSeries{}'.format(suffix)
162+
suffix = suffix.replace(' ', '_')
163+
subtaskName = f'plotDepthIntegratedTimeSeries{suffix}'
161164

162165
# first, call the constructor from the base class (AnalysisTask)
163166
super(PlotDepthIntegratedTimeSeriesSubtask, self).__init__(
@@ -205,7 +208,7 @@ def setup_and_check(self):
205208
if self.controlConfig is not None:
206209
# we need to know what file to read from the control run so
207210
# an absolute path won't work
208-
assert(not os.path.isabs(self.inFileName))
211+
assert not os.path.isabs(self.inFileName)
209212

210213
baseDirectory = build_config_full_path(
211214
self.controlConfig, 'output', 'timeSeriesSubdirectory')
@@ -217,7 +220,7 @@ def setup_and_check(self):
217220
'runs', 'preprocessedReferenceRunName')
218221
if preprocessedReferenceRunName != 'None':
219222

220-
assert(not os.path.isabs(self.inFileName))
223+
assert not os.path.isabs(self.inFileName)
221224

222225
baseDirectory = build_config_full_path(
223226
config, 'output', 'timeSeriesSubdirectory')
@@ -263,11 +266,6 @@ def run_task(self):
263266

264267
mainRunName = config.get('runs', 'mainRunName')
265268

266-
plotTitles = config.getexpression('regions', 'plotTitles')
267-
allRegionNames = config.getexpression('regions', 'regions')
268-
regionIndex = allRegionNames.index(self.regionName)
269-
regionNameInTitle = plotTitles[regionIndex]
270-
271269
startDate = config.get('timeSeries', 'startDate')
272270
endDate = config.get('timeSeries', 'endDate')
273271

@@ -279,7 +277,17 @@ def run_task(self):
279277
timeVariableNames=None,
280278
startDate=startDate,
281279
endDate=endDate)
282-
ds = ds.isel(nOceanRegionsTmp=regionIndex)
280+
if 'regionNames' in ds.coords:
281+
# we added region names already
282+
ds = ds.set_xindex('regionNames')
283+
ds = ds.sel(regionNames=self.regionName)
284+
regionNameInTitle = ds.regionNames.values
285+
else:
286+
# we need to add region names and select the right region by short
287+
# name
288+
ds = add_standard_regions_and_subset(
289+
ds, config, regionShortNames=[self.regionName])
290+
regionNameInTitle = ds.regionNames.values[0]
283291

284292
depths = ds.depth.values
285293

@@ -436,7 +444,17 @@ def run_task(self):
436444
timeVariableNames=None,
437445
startDate=controlStartDate,
438446
endDate=controlEndDate)
439-
dsRef = dsRef.isel(nOceanRegionsTmp=regionIndex)
447+
448+
if 'regionNames' in dsRef.coords:
449+
# we added region names already
450+
dsRef = dsRef.set_xindex('regionNames')
451+
dsRef = dsRef.sel(regionNames=self.regionName)
452+
else:
453+
# we need to add region names and select the right region by
454+
# short name
455+
dsRef = add_standard_regions_and_subset(
456+
dsRef, self.controlConfig,
457+
regionShortNames=[self.regionName])
440458

441459
color = config.get('timeSeries', 'controlColor')
442460

@@ -468,8 +486,9 @@ def run_task(self):
468486

469487
fig = timeseries_analysis_plot(
470488
config=config, dsvalues=timeSeries, calendar=calendar,
471-
title=title, xlabel=xLabel, ylabel=yLabel, movingAveragePoints=None,
472-
lineColors=lineColors, lineStyles=lineStyles, markers=lineMarkers,
489+
title=title, xlabel=xLabel, ylabel=yLabel,
490+
movingAveragePoints=None, lineColors=lineColors,
491+
lineStyles=lineStyles, markers=lineMarkers,
473492
lineWidths=lineWidths, legendText=legendText, maxPoints=maxPoints,
474493
firstYearXTicks=firstYearXTicks, yearStrideXTicks=yearStrideXTicks)
475494

0 commit comments

Comments
 (0)