Source code for earthdiagnostics.ocean.areamoc

# coding=utf-8
"""Compute an Atlantic MOC index from the average"""
import os

import numpy as np

from earthdiagnostics.box import Box
from earthdiagnostics.constants import Basins
from earthdiagnostics.diagnostic import Diagnostic, DiagnosticIntOption, DiagnosticBasinOption
from earthdiagnostics.modelingrealm import ModelingRealms
from earthdiagnostics.utils import Utils, TempFile


[docs]class AreaMoc(Diagnostic): """ Compute an Atlantic MOC index Averages the meridional overturning in a latitude band between 1km and 2km or any other index averaging the meridional overturning in a given basin and a given domain :original author: Virginie Guemas <virginie.guemas@bsc.es> :contributor: Javier Vegas-Regidor<javier.vegas@bsc.es> :created: March 2012 :last modified: June 2016 :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 basin: basin to compute :type basin: Basin :param box: box to compute :type box: Box """ alias = 'mocarea' "Diagnostic alias for the configuration file" vsftmyz = 'vsftmyz' def __init__(self, data_manager, startdate, member, chunk, basin, box): Diagnostic.__init__(self, data_manager) self.basin = basin self.startdate = startdate self.member = member self.chunk = chunk self.required_vars = ['vo'] self.generated_vars = ['vsftmyz'] self.box = box 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 and \ self.basin == other.basin and self.box == other.box def __str__(self): return 'Area MOC Startdate: {0} Member: {1} Chunk: {2} Box: {3} Basin: {4}'.format(self.startdate, self.member, self.chunk, self.box, self.basin)
[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: minimum latitude, maximum latitude, minimum depth, maximum depth, basin=Global :type options: list[str] :return: """ options_available = (DiagnosticIntOption('min_lat'), DiagnosticIntOption('max_lat'), DiagnosticIntOption('min_depth'), DiagnosticIntOption('max_depth'), DiagnosticBasinOption('basin', Basins().Global)) options = cls.process_options(options, options_available) box = Box() box.min_lat = options['min_lat'] box.max_lat = options['max_lat'] 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(AreaMoc(diags.data_manager, startdate, member, chunk, options['basin'], box)) return job_list
[docs] def request_data(self): """Request data required by the diagnostic""" self.variable_file = self.request_chunk(ModelingRealms.ocean, AreaMoc.vsftmyz, self.startdate, self.member, self.chunk)
[docs] def declare_data_generated(self): """Declare data to be generated by the diagnostic""" self.results = self.declare_chunk(ModelingRealms.ocean, AreaMoc.vsftmyz, self.startdate, self.member, self.chunk, box=self.box)
[docs] def compute(self): """Run the diagnostic""" nco = Utils.nco cdo = Utils.cdo temp = TempFile.get() temp2 = TempFile.get() Utils.copy_file(self.variable_file.local_file, temp) handler = Utils.open_cdf(temp) if 'i' in handler.dimensions: handler.close() nco.ncwa(input=temp, output=temp, options=('-O -a i',)) handler = Utils.open_cdf(temp) basin_index = np.where(handler.variables['basin'][:] == self.basin.name) if 'lat' in handler.variables: lat_name = 'lat' else: lat_name = 'latitude' var_lat = handler.variables[lat_name] lat_values = var_lat[:] lat_type = var_lat.dtype lat_units = var_lat.units lat_long_name = var_lat.long_name handler.close() if len(basin_index) == 0: raise Exception('Basin {0} not defined in file') basin_index = basin_index[0][0] # To select basin and remove dimension nco.ncwa(input=temp, output=temp, options=('-O -d basin,{0} -a basin'.format(basin_index),)) source = Utils.open_cdf(temp) destiny = Utils.open_cdf(temp2, 'w') Utils.copy_dimension(source, destiny, 'time') Utils.copy_dimension(source, destiny, 'lev') Utils.copy_dimension(source, destiny, 'j', new_names={'j': lat_name}) lat_variable = destiny.createVariable(lat_name, lat_type, lat_name) lat_variable[:] = lat_values[:] lat_variable.units = lat_units lat_variable.long_name = lat_long_name Utils.copy_variable(source, destiny, 'lev') Utils.copy_variable(source, destiny, 'time') Utils.copy_variable(source, destiny, 'vsftmyz', new_names={'j': lat_name}) source.close() destiny.close() nco.ncks(input=temp2, output=temp, options=('-d lev,{0:.1f},{1:.1f} -d {4},{2:.1f},{3:.1f}'.format(self.box.min_depth, self.box.max_depth, self.box.min_lat, self.box.max_lat, lat_name),)) cdo.vertmean(input=temp, output=temp2) os.remove(temp) nco.ncap2(input=temp2, output=temp2, options=('-s "coslat[{0}]=cos({0}[{0}]*3.141592657/180.0)"'.format(lat_name),)) nco.ncwa(input=temp2, output=temp2, options=('-w coslat -a {0}'.format(lat_name),)) nco.ncks(input=temp2, output=temp2, options=('-v vsftmyz,time',)) self.results.set_local_file(temp2)