Newer
Older
from earthdiagnostics import cdftools
from earthdiagnostics.box import Box
from earthdiagnostics.constants import Basins
from earthdiagnostics.diagnostic import Diagnostic, DiagnosticOption, DiagnosticIntOption, DiagnosticDomainOption, \
DiagnosticBoolOption, DiagnosticBasinOption, DiagnosticVariableOption
from earthdiagnostics.utils import Utils, TempFile
from earthdiagnostics.modelingrealm import ModelingRealms
class RegionMean(Diagnostic):
"""
Chooses vertical level in ocean, or vertically averages between
2 or more ocean levels
:original author: Javier Vegas-Regidor <javier.vegas@bsc.es>
:created: January 2017
:param data_manager: data management object
:type data_manager: DataManager
:param startdate: startdate
:type startdate: str
:param member: member number
:type member: int
:param chunk: chunk's number
:type chunk: int
:param variable: variable to average
:type variable: str
:param box: box used to restrict the vertical mean
:type box: Box
"""
alias = 'regmean'
"Diagnostic alias for the configuration file"
def __init__(self, data_manager, startdate, member, chunk, domain, variable, grid_point, box, save3d, basin, variance, grid):
Diagnostic.__init__(self, data_manager)
self.startdate = startdate
self.member = member
self.chunk = chunk
self.domain = domain
self.variable = variable
self.grid_point = grid_point.upper()
self.required_vars = [variable]
self.generated_vars = [variable + 'vmean']
def __eq__(self, other):
return self.startdate == other.startdate and self.member == other.member and self.chunk == other.chunk and \
self.box == other.box and self.variable == other.variable
def __str__(self):
return 'Vertical mean Startdate: {0} Member: {1} Chunk: {2} Variable: {3} ' \
'Box: {4}'.format(self.startdate, self.member, self.chunk, self.variable, self.box)
@classmethod
def generate_jobs(cls, diags, options):
"""
Creates a job for each chunk to compute the diagnostic
:param diags: Diagnostics manager class
:type diags: Diags
:param options: variable, minimum depth (level), maximum depth (level)
:type options: list[str]
:return:
"""
options_available = (DiagnosticDomainOption('domain'),
DiagnosticVariableOption('variable'),
DiagnosticOption('grid_point', ''),
DiagnosticBasinOption('basin', Basins.Global),
DiagnosticIntOption('min_depth', 0),
DiagnosticIntOption('max_depth', 0),
DiagnosticBoolOption('save3D', True),
DiagnosticBoolOption('variance', False),
DiagnosticOption('grid', ''))
options = cls.process_options(options, options_available)
box = Box()
box.min_depth = options['min_depth']
box.max_depth = options['max_depth']
job_list = list()
for startdate, member, chunk in diags.config.experiment.get_chunk_list():
job_list.append(RegionMean(diags.data_manager, startdate, member, chunk,
options['domain'], options['variable'], options['grid_point'], box,
options['save3D'], options['basin'], options['variance'], options['grid']))
return job_list
def compute(self):
"""
Runs the diagnostic
"""
variable_file = self.data_manager.get_file(self.domain, self.variable, self.startdate, self.member, self.chunk,
grid=self.grid)
handler = Utils.openCdf(variable_file)
self.save3d &= 'lev' in handler.dimensions
handler.close()
cdfmean_options = [self.variable, self.grid_point, 0, 0, 0, 0, self.box.min_depth, self.box.max_depth]
if self.variance:
cdfmean_options += ['-var']
if self.basin != Basins.Global:
cdfmean_options.append('-M')
cdfmean_options.append('mask_regions.3d.nc')
cdftools.run('cdfmean', input=variable_file, output=mean_file, options=cdfmean_options)
Utils.rename_variables(mean_file, {'gdept': 'lev', 'gdepw': 'lev'}, must_exist=False, rename_dimension=True)
if self.box.min_depth == 0:
box_save = None
else:
box_save = self.box
self.send_var('mean', False, box_save, mean_file)
self.send_var('mean', True, box_save, mean_file)
if self.variance:
self.send_var('var', False, box_save, mean_file)
self.send_var('var', True, box_save, mean_file)
os.remove(mean_file)
def send_var(self, var, threed, box_save, mean_file):
if threed:
if not self.save3d:
return False
original_name = '{0}_{1}'.format(var, self.variable)
final_name = '{1}3d{0}'.format(var, self.variable)
levels = ',lev'
else:
original_name = '{0}_3D{1}'.format(var, self.variable)
final_name = '{1}{0}'.format(var, self.variable)
levels = ''
temp2 = TempFile.get()
Utils.nco.ncks(input=mean_file, output=temp2, options='-O -v {0},lat,lon{1}'.format(original_name, levels))
self.send_file(temp2, ModelingRealms.ocean, final_name, self.startdate, self.member, self.chunk,
box=box_save, rename_var=original_name, region=self.basin, grid=self.grid)