Skip to content

Commit

Permalink
Add submesoscale velocity to MOC
Browse files Browse the repository at this point in the history
  • Loading branch information
xylar committed Sep 15, 2022
1 parent db16d3b commit 48f5822
Showing 1 changed file with 66 additions and 16 deletions.
82 changes: 66 additions & 16 deletions mpas_analysis/ocean/streamfunction_moc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# distributed with this code, or at
# https://raw.githubusercontent.com/MPAS-Dev/MPAS-Analysis/master/LICENSE
#
import xarray

import xarray as xr
import numpy as np
import netCDF4
Expand Down Expand Up @@ -253,6 +253,9 @@ def __init__(self, parentTask, mpasClimatologyTask, maskSubtask):

parentTask.add_subtask(self)

self.includeBolus = None
self.includeSubmesoscale = None

def setup_and_check(self):
"""
Perform steps to set up the analysis and check for errors in the setup.
Expand Down Expand Up @@ -305,11 +308,24 @@ def setup_and_check(self):
# the old name
self.includeBolus = self.namelist.getbool(
'config_use_standardgm')
try:
self.includeSubmesoscale = \
self.namelist.getbool('config_submesoscale_enable')
except KeyError:
# an old run without submesoscale
self.includeSubmesoscale = False

if self.includeBolus:
variableList.extend(
['timeMonthly_avg_normalGMBolusVelocity',
'timeMonthly_avg_vertGMBolusVelocityTop'])

if self.includeSubmesoscale:
variableList.extend(
['timeMonthly_avg_normalMLEvelocity',
'timeMonthly_avg_vertMLEVelocityTop'])


self.mpasClimatologyTask.add_variables(variableList=variableList,
seasons=['ANN'])

Expand Down Expand Up @@ -507,19 +523,28 @@ def _compute_moc_climo_postprocess(self):
if 'Time' in annualClimatology.dims:
annualClimatology = annualClimatology.isel(Time=0)

# rename some variables for convenience
annualClimatology = annualClimatology.rename(
{'timeMonthly_avg_normalVelocity': 'avgNormalVelocity',
'timeMonthly_avg_vertVelocityTop': 'avgVertVelocityTop'})

if self.includeBolus:
annualClimatology['avgNormalVelocity'] = \
annualClimatology['timeMonthly_avg_normalVelocity'] + \
annualClimatology['avgNormalVelocity'] + \
annualClimatology['timeMonthly_avg_normalGMBolusVelocity']

annualClimatology['avgVertVelocityTop'] = \
annualClimatology['timeMonthly_avg_vertVelocityTop'] + \
annualClimatology['avgVertVelocityTop'] + \
annualClimatology['timeMonthly_avg_vertGMBolusVelocityTop']
else:
# rename some variables for convenience
annualClimatology = annualClimatology.rename(
{'timeMonthly_avg_normalVelocity': 'avgNormalVelocity',
'timeMonthly_avg_vertVelocityTop': 'avgVertVelocityTop'})

if self.includeSubmesoscale:
annualClimatology['avgNormalVelocity'] = \
annualClimatology['avgNormalVelocity'] + \
annualClimatology['timeMonthly_avg_normalMLEvelocity']

annualClimatology['avgVertVelocityTop'] = \
annualClimatology['avgVertVelocityTop'] + \
annualClimatology['timeMonthly_avg_vertMLEVelocityTop']

# Convert to numpy arrays
# (can result in a memory error for large array size)
Expand Down Expand Up @@ -843,6 +868,9 @@ def __init__(self, parentTask, startYear, endYear, maskSubtask):
self.startYear = startYear
self.endYear = endYear

self.includeBolus = None
self.includeSubmesoscale = None

def setup_and_check(self):
"""
Perform steps to set up the analysis and check for errors in the setup.
Expand Down Expand Up @@ -890,11 +918,24 @@ def setup_and_check(self):
# the old name
self.includeBolus = self.namelist.getbool(
'config_use_standardgm')

try:
self.includeSubmesoscale = \
self.namelist.getbool('config_submesoscale_enable')
except KeyError:
# an old run without submesoscale
self.includeSubmesoscale = False

if self.includeBolus:
self.variableList.extend(
['timeMonthly_avg_normalGMBolusVelocity',
'timeMonthly_avg_vertGMBolusVelocityTop'])

if self.includeSubmesoscale:
self.variableList.extend(
['timeMonthly_avg_normalMLEvelocity',
'timeMonthly_avg_vertMLEVelocityTop'])

def run_task(self):
"""
Process MOC analysis member data if available, or compute MOC at
Expand Down Expand Up @@ -1045,7 +1086,7 @@ def _compute_moc_time_series_analysismember(self):
except ValueError:
raise IOError('No MPAS-O restart file found: need at '
'least one restart file for MOC calculation')
with xarray.open_dataset(restartFile) as dsRestart:
with xr.open_dataset(restartFile) as dsRestart:
refBottomDepth = dsRestart.refBottomDepth.values
nVertLevels = len(refBottomDepth)
refTopDepth = np.zeros(nVertLevels + 1)
Expand Down Expand Up @@ -1204,19 +1245,28 @@ def _compute_moc_time_series_postprocess(self):
self.logger.info(' date: {:04d}-{:02d}'.format(date.year,
date.month))

# rename some variables for convenience
dsLocal = dsLocal.rename(
{'timeMonthly_avg_normalVelocity': 'avgNormalVelocity',
'timeMonthly_avg_vertVelocityTop': 'avgVertVelocityTop'})

if self.includeBolus:
dsLocal['avgNormalVelocity'] = \
dsLocal['timeMonthly_avg_normalVelocity'] + \
dsLocal['avgNormalVelocity'] + \
dsLocal['timeMonthly_avg_normalGMBolusVelocity']

dsLocal['avgVertVelocityTop'] = \
dsLocal['timeMonthly_avg_vertVelocityTop'] + \
dsLocal['avgVertVelocityTop'] + \
dsLocal['timeMonthly_avg_vertGMBolusVelocityTop']
else:
# rename some variables for convenience
dsLocal = dsLocal.rename(
{'timeMonthly_avg_normalVelocity': 'avgNormalVelocity',
'timeMonthly_avg_vertVelocityTop': 'avgVertVelocityTop'})

if self.includeSubmesoscale:
dsLocal['avgNormalVelocity'] = \
dsLocal['avgNormalVelocity'] + \
dsLocal['timeMonthly_avg_normalMLEvelocity']

dsLocal['avgVertVelocityTop'] = \
dsLocal['avgVertVelocityTop'] + \
dsLocal['timeMonthly_avg_vertMLEVelocityTop']

horizontalVel = dsLocal.avgNormalVelocity.values
verticalVel = dsLocal.avgVertVelocityTop.values
Expand Down

0 comments on commit 48f5822

Please sign in to comment.