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
12 changes: 12 additions & 0 deletions mpas_analysis/ocean/meridional_overturning_circulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
days_to_datetime

from ..shared.analysis_task import setup_task
from ..shared.climatology import climatology


def moc_streamfunction(config): # {{{
Expand Down Expand Up @@ -275,6 +276,17 @@ def _compute_moc_climo_postprocess(config, runStreams, variableMap, calendar,
startDate=dictClimo['startDateClimo'],
endDate=dictClimo['endDateClimo'])

changed, startYear, endYear = \
climatology.update_start_end_year(ds, config, calendar)
if changed:
# update the file name in case the start and end years changed
outputFileClimo = \
'{}/mocStreamfunction_years{:04d}-{:04d}.nc'.format(
outputDirectory, startYear, endYear)

dictClimo['startYearClimo'] = startYear
dictClimo['endYearClimo'] = endYear

# Compute annual climatology
annualClimatology = ds.mean('Time')

Expand Down
6 changes: 2 additions & 4 deletions mpas_analysis/ocean/ocean_modelvsobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,8 @@ def ocn_modelvsobs(config, field):
startDate=startDate,
endDate=endDate)

startYear = days_to_datetime(ds.Time.min().values, calendar=calendar).year
endYear = days_to_datetime(ds.Time.max().values, calendar=calendar).year
config.set('climatology', 'startYear', str(startYear))
config.set('climatology', 'endYear', str(endYear))
changed, startYear, endYear = \
climatology.update_start_end_year(ds, config, calendar)

monthlyClimatology = climatology.compute_monthly_climatology(ds, calendar)

Expand Down
6 changes: 2 additions & 4 deletions mpas_analysis/sea_ice/modelvsobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,8 @@ def seaice_modelvsobs(config, streamMap=None, variableMap=None):
# Compute climatologies (first motnhly and then seasonally)
print " Compute seasonal climatologies..."

startYear = days_to_datetime(ds.Time.min().values, calendar=calendar).year
endYear = days_to_datetime(ds.Time.max().values, calendar=calendar).year
config.set('climatology', 'startYear', str(startYear))
config.set('climatology', 'endYear', str(endYear))
changed, startYear, endYear = \
climatology.update_start_end_year(ds, config, calendar)

monthlyClimatology = climatology.compute_monthly_climatology(ds, calendar)

Expand Down
53 changes: 53 additions & 0 deletions mpas_analysis/shared/climatology/climatology.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import os
import numpy
import netCDF4
from warnings import warn

from ..mpas_xarray import mpas_xarray
from ..constants import constants
Expand Down Expand Up @@ -475,6 +476,58 @@ def compute_seasonal_climatology(monthlyClimatology, monthValues,
return seasonalClimatology


def update_start_end_year(ds, config, calendar):
"""
Given a monthly climatology, compute a seasonal climatology weighted by
the number of days in each month (on the no-leap-year calendar).

Parameters
----------
ds : instance of xarray.DataSet
A data set from which start and end years will be determined

config : instance of MpasAnalysisConfigParser
Contains configuration options

calendar: {'gregorian', 'gregorian_noleap'}
The name of one of the calendars supported by MPAS cores

Returns
-------
changed : bool
Whether the start and end years were changed

startYear, endYear : int
The start and end years of the data set

Authors
-------
Xylar Asay-Davis

Last Modified
-------------
03/25/2017
"""
requestedStartYear = config.getint('climatology', 'startYear')
requestedEndYear = config.getint('climatology', 'endYear')

startYear = days_to_datetime(ds.Time.min().values, calendar=calendar).year
endYear = days_to_datetime(ds.Time.max().values, calendar=calendar).year
changed = False
if startYear != requestedStartYear or endYear != requestedEndYear:
warn("climatology start and/or end year different from requested\n"
"requestd: {:04d}-{:04d}\n"
"actual: {:04d}-{:04d}\n".format(requestedStartYear,
requestedEndYear,
startYear,
endYear))
config.set('climatology', 'startYear', str(startYear))
config.set('climatology', 'endYear', str(endYear))
changed = True

return changed, startYear, endYear


def _get_comparison_lat_lon(comparisonLatRes, comparisonLonRes):
'''
Returns the lat and lon arrays defining the corners of the comparison
Expand Down