Skip to content

Commit 9cf503a

Browse files
authored
Merge pull request #1126 from cbegeman/do-not-require-tseries-files-if-cached-output
Allow time series tasks to run when history files are not present but cached files are
2 parents 5726aeb + cda2fe1 commit 9cf503a

File tree

4 files changed

+43
-32
lines changed

4 files changed

+43
-32
lines changed

mpas_analysis/ocean/index_nino34.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,8 @@ def _plot_size_y_axis(self, x, ys, xmin, xmax):
779779

780780
# find maximum value of three curves plotted
781781
maxY = -1E20
782+
if len(mask) == 0:
783+
return maxY
782784
for y in ys:
783785
maxY = max(y[mask].max(), maxY)
784786
# check the function interpolated to the max/min as well

mpas_analysis/ocean/streamfunction_moc.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,15 +1007,16 @@ def _compute_moc_time_series_analysismember(self):
10071007
self.historyStreams,
10081008
'timeSeriesStatsMonthlyOutput')
10091009

1010-
mocRegion = np.zeros(len(inputFiles))
1010+
ntimes = int(12 * (self.endYear - self.startYear + 1))
1011+
mocRegion = np.zeros(ntimes)
10111012
moc = None
10121013
refTopDepth = None
1013-
times = np.zeros(len(inputFiles))
1014-
computed = np.zeros(len(inputFiles), bool)
1014+
times = np.zeros(ntimes)
1015+
computed = np.zeros(ntimes, bool)
10151016

10161017
continueOutput = os.path.exists(outputFileName)
10171018
if continueOutput:
1018-
self.logger.info(' Read in previously computed MOC time series')
1019+
self.logger.info(f' Read in previously computed MOC time series {outputFileName}')
10191020
with open_mpas_dataset(fileName=outputFileName,
10201021
calendar=self.calendar,
10211022
timeVariableNames=None,
@@ -1028,32 +1029,30 @@ def _compute_moc_time_series_analysismember(self):
10281029

10291030
if moc is None:
10301031
sizes = dsMOCIn.sizes
1031-
moc = np.zeros((len(inputFiles), sizes['depth'],
1032-
sizes['lat']))
1032+
moc = np.zeros((ntimes, sizes['depth'],
1033+
sizes['lat']))
10331034
refTopDepth = dsMOCIn.depth.values
10341035

10351036
# first, copy all computed data
1036-
for inIndex in range(dsMOCIn.sizes['Time']):
1037-
1038-
mask = np.logical_and(
1039-
dsMOCIn.year[inIndex].values == years,
1040-
dsMOCIn.month[inIndex].values == months)
1041-
1042-
outIndex = np.where(mask)[0][0]
1043-
1044-
mocRegion[outIndex] = dsMOCIn.mocAtlantic26[inIndex]
1045-
moc[outIndex, :, :] = dsMOCIn.mocAtlantic[inIndex, :, :]
1046-
times[outIndex] = dsMOCIn.Time[inIndex]
1047-
computed[outIndex] = True
1037+
outIndex = 0
1038+
for load_year in np.arange(self.startYear, self.endYear + 1):
1039+
for load_month in np.arange(1, 13):
1040+
mask = np.logical_and(dsMOCIn.year.values == load_year,
1041+
dsMOCIn.month.values == load_month)
1042+
if np.sum(mask) >= 1:
1043+
inIndex = np.where(mask)[0][0]
1044+
mocRegion[outIndex] = dsMOCIn.mocAtlantic26[inIndex]
1045+
moc[outIndex, :, :] = dsMOCIn.mocAtlantic[inIndex, :, :]
1046+
times[outIndex] = dsMOCIn.Time[inIndex]
1047+
computed[outIndex] = True
1048+
1049+
outIndex += 1
10481050

10491051
if np.all(computed):
10501052
# no need to waste time writing out the data set again
10511053
return dsMOCIn
10521054

10531055
for timeIndex, fileName in enumerate(inputFiles):
1054-
if computed[timeIndex]:
1055-
continue
1056-
10571056
dsLocal = open_mpas_dataset(
10581057
fileName=fileName,
10591058
calendar=self.calendar,
@@ -1067,12 +1066,15 @@ def _compute_moc_time_series_analysismember(self):
10671066

10681067
self.logger.info(' date: {:04d}-{:02d}'.format(date.year,
10691068
date.month))
1069+
computedIndex = 12 * (date.year - self.startYear) + date.month - 1
1070+
if computed[computedIndex]:
1071+
continue
10701072

10711073
# hard-wire region=0 (Atlantic) for now
10721074
indRegion = 0
10731075
mocVar = dsLocal.timeMonthly_avg_mocStreamvalLatAndDepthRegion
10741076
mocTop = mocVar[indRegion, :, :].values
1075-
mocRegion[timeIndex] = np.amax(mocTop[:, indlat26])
1077+
mocRegion[computedIndex] = np.amax(mocTop[:, indlat26])
10761078

10771079
if moc is None:
10781080
sizes = dsLocal.sizes
@@ -1087,7 +1089,8 @@ def _compute_moc_time_series_analysismember(self):
10871089
refTopDepth = np.zeros(nVertLevels + 1)
10881090
refTopDepth[1:nVertLevels + 1] = refBottomDepth[0:nVertLevels]
10891091

1090-
moc[timeIndex, 0:-1, :] = mocTop
1092+
moc[computedIndex, 0:-1, :] = mocTop
1093+
10911094

10921095
description = 'Max MOC Atlantic streamfunction nearest to RAPID ' \
10931096
'Array latitude (26.5N)'

mpas_analysis/shared/analysis_task.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,8 @@ def flush(self):
602602
pass
603603

604604

605-
def update_time_bounds_from_file_names(config, section, componentName):
605+
def update_time_bounds_from_file_names(config, section, componentName,
606+
allow_cache=True):
606607
"""
607608
Update the start and end years and dates for time series, climatologies or
608609
climate indices based on the years actually available in the list of files.
@@ -658,7 +659,7 @@ def update_time_bounds_from_file_names(config, section, componentName):
658659
return
659660

660661
if len(inputFiles) == 0:
661-
raise ValueError('No input files found for stream {} in {} between '
662+
print('Warning: No input files found for stream {} in {} between '
662663
'{} and {}'.format(streamName, componentName,
663664
requestedStartYear,
664665
requestedEndYear))
@@ -680,12 +681,16 @@ def update_time_bounds_from_file_names(config, section, componentName):
680681
endYear = years[lastIndex]
681682

682683
if startYear != requestedStartYear or endYear != requestedEndYear:
683-
raise ValueError(
684-
"{} start and/or end year different from requested\n"
685-
"requested: {:04d}-{:04d}\n"
686-
"actual: {:04d}-{:04d}\n".format(
687-
section, requestedStartYear, requestedEndYear, startYear,
688-
endYear))
684+
message = ("{} start and/or end year different from requested\n"
685+
"requested: {:04d}-{:04d}\n"
686+
"actual: {:04d}-{:04d}\n".format(
687+
section, requestedStartYear, requestedEndYear, startYear,
688+
endYear)
689+
)
690+
if allow_cache:
691+
print(f'Warning: {message}')
692+
else:
693+
raise ValueError(message)
689694

690695
startDate = '{:04d}-01-01_00:00:00'.format(startYear)
691696
config.set(section, 'startDate', startDate)

mpas_analysis/test/test_mpas_climatology_task.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ def test_update_climatology_bounds_and_create_symlinks(self):
168168
with self.assertRaisesRegex(ValueError,
169169
'climatology start and/or end year '
170170
'different from requested'):
171-
update_time_bounds_from_file_names(config, 'climatology', 'ocean')
171+
update_time_bounds_from_file_names(config, 'climatology', 'ocean',
172+
allow_cache=False)
172173

173174
def test_subtask_run_analysis(self):
174175
mpasClimatologyTask = self.setup_task()

0 commit comments

Comments
 (0)