# coding=utf-8
"""Compute the mixed layer depth"""
import os
from earthdiagnostics import cdftools
from earthdiagnostics.diagnostic import Diagnostic
from earthdiagnostics.modelingrealm import ModelingRealms
from earthdiagnostics.utils import Utils, TempFile
[docs]class Mxl(Diagnostic):
"""
Compute the mixed layer depth
: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
"""
alias = 'mxl'
"Diagnostic alias for the configuration file"
def __init__(self, data_manager, startdate, member, chunk):
Diagnostic.__init__(self, data_manager)
self.startdate = startdate
self.member = member
self.chunk = chunk
def __eq__(self, other):
if self._different_type(other):
return False
return self.startdate == other.startdate and self.member == other.member and self.chunk == other.chunk
def __str__(self):
return 'Mixed layer Startdate: {0} Member: {1} Chunk: {2}'.format(self.startdate, self.member, self.chunk)
[docs] @classmethod
def generate_jobs(cls, diags, options):
"""
Create a job for each chunk to compute the diagnostic
:param diags: Diagnostics manager class
:type diags: Diags
:param options: None
:type options: list[str]
:return:
"""
if len(options) > 1:
raise Exception('The mxl diagnostic has no options')
job_list = list()
for startdate, member, chunk in diags.config.experiment.get_chunk_list():
job_list.append(Mxl(diags.data_manager, startdate, member, chunk))
return job_list
[docs] def request_data(self):
"""Request data required by the diagnostic"""
self.thetao_file = self.request_chunk(ModelingRealms.ocean, 'thetao', self.startdate, self.member, self.chunk)
self.so_file = self.request_chunk(ModelingRealms.ocean, 'so', self.startdate, self.member, self.chunk)
[docs] def declare_data_generated(self):
"""Declare data to be generated by the diagnostic"""
self.mlotst_file = self.declare_chunk(ModelingRealms.ocean, 'mlotst', self.startdate, self.member, self.chunk)
[docs] def compute(self):
"""Run the diagnostic"""
temp = TempFile.get()
cdftools.run('cdfmxl', input_file=[self.thetao_file, self.so_file], output_file=temp, options='-nc4')
temp2 = TempFile.get()
source = Utils.open_cdf(temp)
destiny = Utils.open_cdf(temp2, 'w')
Utils.copy_variable(source, destiny, 'somxl010', must_exist=True, add_dimensions=True)
Utils.copy_variable(source, destiny, 'lat', must_exist=False)
Utils.copy_variable(source, destiny, 'latitude', must_exist=False)
Utils.copy_variable(source, destiny, 'lon', must_exist=False)
Utils.copy_variable(source, destiny, 'longitude', must_exist=False)
source.close()
destiny.close()
self.mlotst_file.set_local_file(temp2, rename_var='somxl010')
os.remove(temp)