Skip to content

Commit fcc4dab

Browse files
authored
Merge pull request #1134 from xylar/fix-el-nino-main-vs-control
Fix control run support for El Nino 3.4 Index
2 parents 02bd644 + 3a75889 commit fcc4dab

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

mpas_analysis/ocean/index_nino34.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ def run_task(self):
221221
ninoIndexNumber))
222222
varName = self.variableList[0]
223223
regionSST = ds[varName]
224+
self.logger.debug('Main run SST dims=%s shape=%s',
225+
getattr(regionSST, 'dims', None),
226+
getattr(regionSST, 'shape', None))
224227
nino34Main = self._compute_nino34_index(regionSST, calendar)
225228

226229
# Compute the observational index over the entire time range
@@ -270,7 +273,14 @@ def run_task(self):
270273
dsRef = add_standard_regions_and_subset(
271274
dsRef, self.controlConfig, regionShortNames=[regionToPlot])
272275

276+
# we want to collapse the nOceanRegions dimension (same as main)
277+
if 'nOceanRegions' in dsRef.dims:
278+
dsRef = dsRef.isel(nOceanRegions=0)
279+
273280
regionSSTRef = dsRef[varName]
281+
self.logger.debug('Control run SST dims=%s shape=%s',
282+
getattr(regionSSTRef, 'dims', None),
283+
getattr(regionSSTRef, 'shape', None))
274284
nino34Ref = self._compute_nino34_index(regionSSTRef, calendar)
275285

276286
nino34s = [nino34Subset, nino34Main[2:-3], nino34Ref[2:-3]]
@@ -499,7 +509,19 @@ def _running_mean(self, inputData, wgts):
499509
sp = (len(wgts) - 1) // 2
500510
runningMean = inputData.copy()
501511
for k in range(sp, nt - (sp + 1)):
502-
runningMean[k] = sum(wgts * inputData[k - sp:k + sp + 1].values)
512+
windowValues = np.asarray(inputData[k - sp:k + sp + 1].values)
513+
if windowValues.shape[0] != len(wgts):
514+
raise ValueError(
515+
'Unexpected running-mean window shape. '
516+
f'Expected first dimension {len(wgts)} but got {windowValues.shape}. '
517+
f'inputData dims={getattr(inputData, "dims", None)} '
518+
f'shape={getattr(inputData, "shape", None)}')
519+
520+
if windowValues.ndim == 1:
521+
runningMean[k] = np.sum(wgts * windowValues)
522+
else:
523+
# weighted sum over the first axis (the running-mean window)
524+
runningMean[k] = np.tensordot(wgts, windowValues, axes=(0, 0))
503525

504526
return runningMean
505527

0 commit comments

Comments
 (0)