Newer
Older
Javier Vegas-Regidor
committed
import os
from autosubmit.config.log import Log
from earthdiagnostics import cdftools
from earthdiagnostics.diagnostic import Diagnostic
from earthdiagnostics.utils import Utils, TempFile
Javier Vegas-Regidor
committed
class MixedLayerSaltContent(Diagnostic):
"""
Compute mixed layer salt content
:original author: Virginie Guemas <virginie.guemas@bsc.es>
:contributor: Javier Vegas-Regidor<javier.vegas@bsc.es>
: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 salt content Startdate: {0} Member: {1} Chunk: {2}'.format(self.startdate, self.member,
self.chunk)
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 salt content diagnostic has no options')
for startdate in diags.startdates:
for member in diags.members:
for chunk in range(1, diags.chunks + 1):
job_list.append(MixedLayerSaltContent(diags.datamanager, startdate, member, chunk))
Javier Vegas-Regidor
committed
Javier Vegas-Regidor
committed
nco = Utils.nco
salinity_file = self.data_manager.get_file('ocean', 'so', 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=salinity_file, options='-A -v mlotst')
Javier Vegas-Regidor
committed
ntime = int(cdo.ntime(input=salinity_file)[0])
Javier Vegas-Regidor
committed
for time in range(ntime):
Log.info('Running time {0}', time)
temp = TempFile.get()
temp2 = TempFile.get()
nco.ncks(input=salinity_file, output=temp, options='-O -d time,{0}'.format(time))
cdftools.run('cdfmxlsaltc', input=temp, output=temp2)
os.remove(temp)
files.append(temp2)
Javier Vegas-Regidor
committed
temp = TempFile.get()
cdo.cat(input=' '.join(files), output=temp,)
nco.ncks(input=salinity_file, output=temp, options='-A -v time')
Javier Vegas-Regidor
committed
for temp_file in files:
os.remove(temp_file)
os.remove(salinity_file)
Utils.rename_variables(temp, {'x': 'i', 'y': 'j', 'somxlsaltc': 'scvertsum'}, False, True)
Utils.setminmax(temp, 'scvsum')
self.data_manager.send_file(temp, 'ocean', 'scvsum', self.startdate, self.member, self.chunk)