# coding=utf-8 import os from earthdiagnostics.diagnostic import Diagnostic from earthdiagnostics import cdftools from earthdiagnostics.utils import Utils, TempFile class MixedLayerHeatContent(Diagnostic): """ Compute mixed layer heat content :original author: Virginie Guemas :contributor: Javier Vegas-Regidor :created: February 2012 :last modified: June 2016 """ def __init__(self, data_manager, startdate, member, chunk): Diagnostic.__init__(self, data_manager) self.startdate = startdate self.member = member self.chunk = chunk self.required_vars = ['so', 'mlotst'] self.generated_vars = ['scvertsum'] def __str__(self): return 'Mixed layer heat content Startdate: {0} Member: {1} Chunk: {2}'.format(self.startdate, self.member, self.chunk) @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: None :type options: list[str] :return: """ if len(options) > 1: raise Exception('The mixed layer ocean heat content diagnostic has no options') job_list = list() for startdate, member, chunk in diags.get_chunk_list(): job_list.append(MixedLayerHeatContent(diags.data_manager, startdate, member, chunk)) return job_list def compute(self): """ Runs the diagnostic """ temperature_file = self.data_manager.get_file('ocean', 'thetao', self.startdate, self.member, self.chunk) mlotst_file = self.data_manager.get_file('ocean', 'mlotst', self.startdate, self.member, self.chunk) Utils.nco.ncks(input=mlotst_file, output=temperature_file, options='-A -v mlotst') temp = TempFile.get() cdftools.run('cdfmxlheatc', input=temperature_file, output=temp) os.remove(temperature_file) Utils.rename_variables(temp, {'x': 'i', 'y': 'j', 'somxlheatc': 'ohcvsumlotst'}, False, True) Utils.setminmax(temp, 'ohcvsumlotst') self.data_manager.send_file(temp, 'ocean', 'ohcvsumlotst', self.startdate, self.member, self.chunk)