mixedlayerheatcontent.py 2.6 KB
Newer Older
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
# coding=utf-8
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 <virginie.guemas@bsc.es>
    :contributor: Javier Vegas-Regidor<javier.vegas@bsc.es>

    :created: February 2012
    :last modified: June 2016

Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
    :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
    """

    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.exp_manager.get_chunk_list():
            job_list.append(MixedLayerHeatContent(diags.data_manager, startdate, member, chunk))
        """
        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)