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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# coding=utf-8
import os
from bscearth.utils.date import parse_date, add_months, chunk_start_date, chunk_end_date
from earthdiagnostics.datafile import NetCDFFile, StorageStatus, LocalStatus
from earthdiagnostics.datamanager import DataManager
from earthdiagnostics.utils import TempFile, Utils
from datetime import datetime
from earthdiagnostics.variable_type import VariableType
class ObsReconManager(DataManager):
"""
Data manager class for CMORized experiments
"""
def __init__(self, config):
super(ObsReconManager, self).__init__(config)
data_folders = self.config.data_dir.split(':')
self.config.data_dir = None
for data_folder in data_folders:
if os.path.isdir(os.path.join(data_folder, self.config.data_type, self.experiment.institute.lower(),
self.experiment.model.lower())):
self.config.data_dir = data_folder
break
if not self.config.data_dir:
raise Exception('Can not find model data')
if self.config.data_type in ('obs', 'recon') and self.experiment.chunk_size != 1:
raise Exception('For obs and recon data chunk_size must be always 1')
# noinspection PyUnusedLocal
def get_leadtimes(self, domain, variable, startdate, member, leadtimes, frequency=None, vartype=VariableType.MEAN):
aggregation_path = self.get_var_url(variable, startdate, frequency, None, vartype)
startdate = parse_date(startdate)
start_chunk = chunk_start_date(startdate, self.experiment.num_chunks, self.experiment.chunk_size,
'month', self.experiment.calendar)
end_chunk = chunk_end_date(start_chunk, self.experiment.chunk_size, 'month', self.experiment.calendar)
thredds_subset = THREDDSSubset(aggregation_path, "", variable, startdate, end_chunk)
selected_months = ','.join([str(add_months(startdate, i, self.experiment.calendar).month) for i in leadtimes])
temp = TempFile.get()
if self.config.data_type == 'exp':
select_months = '-selmonth,{0} {1}'.format(selected_months, thredds_subset)
selected_years = ','.join([str(add_months(startdate, i, self.experiment.calendar).year) for i in leadtimes])
Utils.cdo.selyear(selected_years, input=select_months, output=temp)
else:
Utils.cdo.selmonth(selected_months, input=thredds_subset, output=temp)
return temp
# noinspection PyUnusedLocal
def file_exists(self, domain, var, startdate, member, chunk, grid=None, box=None, frequency=None,
vartype=VariableType.MEAN):
"""
Copies a given file from the CMOR repository to the scratch folder and returns the path to the scratch's copy
:param domain: CMOR domain
:type domain: str
:param var: variable name
:type var: str
:param startdate: file's startdate
:type startdate: str
:param member: file's member
:type member: int
:param chunk: file's chunk
:type chunk: int
:param grid: file's grid (only needed if it is not the original)
:type grid: str
:param box: file's box (only needed to retrieve sections or averages)
:type box: Box
:param frequency: file's frequency (only needed if it is different from the default)
:type frequency: Frequency
:param vartype: Variable type (mean, statistic)
:type vartype: VariableType
:return: path to the copy created on the scratch folder
:rtype: str
"""
aggregation_path = self.get_var_url(var, startdate, frequency, box, vartype)
start_chunk = chunk_start_date(parse_date(startdate), chunk, self.experiment.chunk_size, 'month',
self.experiment.calendar)
end_chunk = chunk_end_date(start_chunk, self.experiment.chunk_size, 'month', self.experiment.calendar)
thredds_subset = THREDDSSubset(aggregation_path, "", var, start_chunk, end_chunk)
return thredds_subset
def get_file_path(self, startdate, domain, var, frequency, vartype,
box=None, grid=None):
"""
Returns the path to a concrete file
:param startdate: file's startdate
:type startdate: str
:param domain: file's domain
:type domain: str
:param var: file's var
:type var: str
:param frequency: file's frequency
:type frequency: Frequency
:param box: file's box
:type box: Box
:param grid: file's grid
:type grid: str
:return: path to the file
:rtype: str
:param vartype: Variable type (mean, statistic)
:type vartype: VariableType
"""
if not frequency:
frequency = self.config.frequency
var = self._get_final_var_name(box, var)
folder_path = self._get_folder_path(frequency, domain, var, grid, vartype)
file_name = self._get_file_name(var, startdate)
filepath = os.path.join(folder_path, file_name)
return filepath
def _get_folder_path(self, frequency, domain, variable, grid, vartype):
if self.config.data_type == 'exp':
var_folder = self.get_varfolder(domain, variable, grid)
else:
var_folder = variable
folder_path = os.path.join(self.config.data_dir, self.config.data_type,
self.experiment.institute.lower(),
self.experiment.model.lower(),
frequency.folder_name(vartype),
var_folder)
return folder_path
def get_year(self, domain, var, startdate, member, year, grid=None, box=None, vartype=VariableType.MEAN):
"""
Ge a file containing all the data for one year for one variable
:param domain: variable's domain
:type domain: str
:param var: variable's name
:type var: str
:param startdate: startdate to retrieve
:type startdate: str
:param member: member to retrieve
:type member: int
:param year: year to retrieve
:type year: int
:param grid: variable's grid
:type grid: str
:param box: variable's box
:type box: Box
:param vartype: Variable type (mean, statistic)
:type vartype: VariableType
:return:
"""
aggregation_path = self.get_var_url(var, startdate, None, box, vartype)
thredds_subset = THREDDSSubset(aggregation_path, "", var, datetime(year, 1, 1), datetime(year+1, 1, 1))
return thredds_subset.download()
def get_var_url(self, var, startdate, frequency, box, vartype):
"""
Get url for dataset
:param var: variable to retrieve
:type var: str
:param startdate: startdate to retrieve
:type startdate: str
:param frequency: frequency to get:
:type frequency: Frequency | None
:param box: box to get
:type box: Box
:param vartype: type of variable
:type vartype: VariableType
:return:
"""
if not frequency:
frequency = self.config.frequency
var = self._get_final_var_name(box, var)
full_path = os.path.join(self.config.data_dir, self.config.data_type, self.experiment.institute,
self.experiment.model, frequency.folder_name(vartype))
full_path = os.path.join(full_path, var, self._get_file_name(var, startdate))
return full_path
def _get_file_name(self, var, startdate):
if startdate:
if self.config.data_type != 'exp':
startdate = startdate[0:6]
return '{0}_{1}.nc'.format(var, startdate)
else:
return '{0}.nc'.format(var)
def link_file(self, domain, var, cmor_var, startdate, member, chunk=None, grid=None,
frequency=None, year=None, date_str=None, move_old=False, vartype=VariableType.MEAN):
"""
Creates the link of a given file from the CMOR repository.
:param cmor_var:
:param move_old:
:param date_str:
:param year: if frequency is yearly, this parameter is used to give the corresponding year
:type year: int
:param domain: CMOR domain
:type domain: str
:param var: variable name
:type var: str
:param startdate: file's startdate
:type startdate: str
:param member: file's member
:type member: int
:param chunk: file's chunk
:type chunk: int
:param grid: file's grid (only needed if it is not the original)
:type grid: str
:param frequency: file's frequency (only needed if it is different from the default)
:type frequency: str
:param vartype: Variable type (mean, statistic)
:type vartype: VariableType
:return: path to the copy created on the scratch folder
:rtype: str
"""
# THREDDSManager does not require links
pass
def request_chunk(self, domain, var, startdate, member, chunk, grid=None, box=None, frequency=None,
vartype=VariableType.MEAN):
"""
Copies a given file from the CMOR repository to the scratch folder and returns the path to the scratch's copy
:param vartype:
:param domain: CMOR domain
:type domain: Domain
:param var: variable name
:type var: str
:param startdate: file's startdate
:type startdate: str
:param member: file's member
:type member: int
:param chunk: file's chunk
:type chunk: int
:param grid: file's grid (only needed if it is not the original)
:type grid: str|NoneType
:param box: file's box (only needed to retrieve sections or averages)
:type box: Box
:param frequency: file's frequency (only needed if it is different from the default)
:type frequency: Frequency|NoneType
:return: path to the copy created on the scratch folder
:rtype: str
"""
var = self._get_final_var_name(box, var)
filepath = self.get_file_path(startdate, domain, var, frequency, vartype, grid, box)
return self._get_file_from_storage(filepath)
def declare_chunk(self, domain, var, startdate, member, chunk, grid=None, region=None, box=None, frequency=None,
vartype=VariableType.MEAN, diagnostic=None):
"""
Copies a given file from the CMOR repository to the scratch folder and returns the path to the scratch's copy
:param diagnostic:
:param region:
:param domain: CMOR domain
:type domain: Domain
:param var: variable name
:type var: str
:param startdate: file's startdate
:type startdate: str
:param member: file's member
:type member: int
:param chunk: file's chunk
:type chunk: int
:param grid: file's grid (only needed if it is not the original)
:type grid: str|NoneType
:param box: file's box (only needed to retrieve sections or averages)
:type box: Box
:param frequency: file's frequency (only needed if it is different from the default)
:type frequency: Frequency|NoneType
:param vartype: Variable type (mean, statistic)
:type vartype: VariableType
:return: path to the copy created on the scratch folder
:rtype: str
"""
if not frequency:
frequency = self.config.frequency
original_name = var
cmor_var = self.variable_list.get_variable(var)
if cmor_var:
var = cmor_var.short_name
final_name = var
filepath = self.get_file_path(startdate, domain, final_name, frequency, vartype, box, grid)
netcdf_file = self._declare_generated_file(filepath, domain, final_name, cmor_var, self.config.data_convention,
region, diagnostic, grid, vartype, original_name)
netcdf_file.frequency = frequency
return netcdf_file