Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import numpy as np
from basins import Basins
from box import Box
from earthdiagnostics import Diagnostic, Utils, TempFile
class AreaMoc(Diagnostic):
"""
Compute the MOC for oceanic basins
Created in March 2012
Author : vguemas@ic3.cat
# :param input_file: input grid_V file namez
# :type input_file: str
# :param output_file: output file name (=> 2D, depth-y)
# :param output_file: str
# :return:
"""
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
@classmethod
def generate_jobs(cls, diags, options):
num_options = len(options) - 1
if num_options < 4:
raise Exception('You must specify the box to use')
if num_options > 5:
raise Exception('You must specify between 4 and 5 parameters for area moc diagnostic')
box = Box()
box.min_lat = int(options[1])
box.max_lat = int(options[2])
box.min_depth = int(options[3])
box.max_depth = int(options[4])
if num_options > 4:
basin = Basins.parse(options[5])
else:
basin = Basins.Global
job_list = list()
for startdate in diags.startdates:
for member in diags.members:
for chunk in range(1, diags.chunks + 1):
job_list.append(AreaMoc(diags.data_manager, startdate, member, chunk, basin, box))
return job_list
def compute(self):
"""
Compute an Atlantic MOC index by averaging 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
Created in March 2012 Author : vguemas@ic3.cat
:param input_file: input moc file name
:param lat_min: latitude min
:param lat_max: latitude max
:param output_file: output file name ( => index )
:param depth_min: depth min
:param depth_max: depth max
:param basin: basin
:return:
"""
nco = Utils.nco
cdo = Utils.cdo
temp2 = TempFile.get()
temp = self.data_manager.get_file('ocean', 'vsftmyz', self.startdate, self.member, self.chunk)
handler = Utils.openCdf(temp)
if 'i' in handler.dimensions:
handler.close()
nco.ncwa(input=temp, output=temp, options='-O -a i')
handler = Utils.openCdf(temp)
basin_index = np.where(handler.variables['basin'][:] == self.basin.fullname)
lat_values = handler.variables['lat'][:]
lat_type = handler.variables['lat'].dtype
lat_units = handler.variables['lat'].units
lat_long_name = handler.variables['lat'].long_name
handler.close()
if len(basin_index) == 0:
raise Exception('Basin {0} not defined in file')
basin_index = basin_index[0][0]
nco.ncks(input=temp, output=temp, options='-O -d basin,{0}'.format(basin_index))
# To remove basin dimension
nco.ncwa(input=temp, output=temp, options='-O -a basin')
nco.ncks(input=temp, output=temp, options='-O -v vsftmyz,time,lev')
handler = Utils.openCdf(temp)
handler.renameDimension('j', 'lat')
lat_variable = handler.createVariable('lat', lat_type, 'lat')
lat_variable[:] = lat_values[:]
lat_variable.units = lat_units
lat_variable.long_name = lat_long_name
handler.close()
nco.ncks(input=temp, output=temp2,
options='-O -d lev,{0:.1f},{1:.1f} -d lat,{2:.1f},{3:.1f}'.format(self.box.min_depth,
self.box.max_depth,
self.box.min_lat,
self.box.max_lat))
cdo.vertmean(input=temp2, output=temp)
nco.ncap2(input=temp, output=temp, options='-O -s "coslat[lat]=cos(lat[lat]*3.141592657/180.0)"')
nco.ncwa(input=temp, output=temp, options='-w coslat -a lat')
nco.ncks(input=temp, output=temp, options='-O -v vsftmyz,time')
self.data_manager.send_file(temp, 'ocean', 'vsftmyz', self.startdate, self.member, self.chunk, box=self.box)