Commit 425b6925 authored by Jorn Baayen's avatar Jorn Baayen

Fix PIMixin

parent 22e4e8ba
......@@ -435,7 +435,7 @@ class Timeseries:
else:
contains_ensemble = False
if self.__contains_ensemble == False:
# Only overwrite when _contains_ensemble was False before
# Only overwrite when _contains_ensemble was False before
self.__contains_ensemble = contains_ensemble
# Define the times, and floor the global forecast_datetime to the
......@@ -537,7 +537,7 @@ class Timeseries:
ensemble_member][variable] == miss_val] = np.nan
unit = header.find('pi:units', ns).text
self.__set_unit(variable, unit=unit, ensemble_member=ensemble_member)
self.set_unit(variable, unit=unit, ensemble_member=ensemble_member)
if make_virtual_ensemble:
# Make references to the original input series for the virtual
......@@ -681,7 +681,7 @@ class Timeseries:
for ensemble_member in range(len(self.__values)):
for variable in self.__values[ensemble_member].keys():
location_parameter_id = self.__data_config.pi_variable_ids(variable)
unit = self.__get_unit(variable, ensemble_member)
unit = self.get_unit(variable, ensemble_member)
self.__add_header(variable, location_parameter_id, ensemble_member=ensemble_member, miss_val=-999, unit=unit)
for ensemble_member in range(len(self.__values)):
......@@ -711,7 +711,7 @@ class Timeseries:
# Update the header, which may have changed
el = header.find('pi:units', ns)
el.text = self.__get_unit(variable, ensemble_member)
el.text = self.get_unit(variable, ensemble_member)
# No values to be written, so the entire element is removed from
# the XML, and the loop restarts.
......@@ -773,6 +773,12 @@ class Timeseries:
"""
return self.__contains_ensemble
@contains_ensemble.setter
def contains_ensemble(self, contains_ensemble):
if not contains_ensemble:
assert self.ensemble_size == 1
self.__contains_ensemble = contains_ensemble
@property
def ensemble_size(self):
"""
......@@ -780,6 +786,14 @@ class Timeseries:
"""
return self.__ensemble_size
@ensemble_size.setter
def ensemble_size(self, ensemble_size):
if ensemble_size > 1:
assert self.contains_ensemble
self.__ensemble_size = ensemble_size
while self.__ensemble_size > len(self.__values):
self.__values.append({})
@property
def start_datetime(self):
"""
......@@ -801,12 +815,17 @@ class Timeseries:
"""
return self.__forecast_datetime
@forecast_datetime.setter
def forecast_datetime(self, forecast_datetime):
self.__forecast_datetime = forecast_datetime
self.__forecast_index = self.__times.index(forecast_datetime)
@property
def forecast_index(self):
"""
Forecast time (t0) index.
"""
return self.__forecast_index
return self.__forecast_index
@property
def dt(self):
......@@ -815,6 +834,10 @@ class Timeseries:
"""
return self.__dt
@dt.setter
def dt(self, dt):
self.__dt = dt
@property
def times(self):
"""
......@@ -822,6 +845,12 @@ class Timeseries:
"""
return self.__times
@times.setter
def times(self, times):
self.__times = times
self.__start_datetime = times[0]
self.__end_datetime = times[-1]
@property
def timezone(self):
"""
......@@ -829,6 +858,10 @@ class Timeseries:
"""
return self.__timezone
@timezone.setter
def timezone(self, timezone):
self.__timezone = timezone
def get(self, variable, ensemble_member=0):
"""
Look up a time series.
......@@ -851,10 +884,10 @@ class Timeseries:
"""
self.__values[ensemble_member][variable] = new_values
if unit is None:
unit = self.__get_unit(variable, ensemble_member)
self.__set_unit(variable, unit, ensemble_member)
unit = self.get_unit(variable, ensemble_member)
self.set_unit(variable, unit, ensemble_member)
def __get_unit(self, variable, ensemble_member=0):
def get_unit(self, variable, ensemble_member=0):
"""
Look up the unit of a time series.
......@@ -868,7 +901,7 @@ class Timeseries:
except KeyError:
return 'unit_unknown'
def __set_unit(self, variable, unit, ensemble_member=0):
def set_unit(self, variable, unit, ensemble_member=0):
"""
Set the unit of a time series.
......
......@@ -295,8 +295,6 @@ class PIMixin(OptimizationProblem):
self.__timeseries_export.times = self.__timeseries_import.times[self.__timeseries_import.forecast_index:]
# Write other time settings
self.__timeseries_export.start_datetime = self.__timeseries_import.forecast_datetime
self.__timeseries_export.end_datetime = self.__timeseries_import.end_datetime
self.__timeseries_export.forecast_datetime = self.__timeseries_import.forecast_datetime
self.__timeseries_export.dt = self.__timeseries_import.dt
self.__timeseries_export.timezone = self.__timeseries_import.timezone
......@@ -304,11 +302,6 @@ class PIMixin(OptimizationProblem):
# Write the ensemble properties for the export file.
self.__timeseries_export.ensemble_size = self.ensemble_size
self.__timeseries_export.contains_ensemble = self.__timeseries_import.contains_ensemble
while self.__timeseries_export.ensemble_size > len(self.__timeseries_export.values):
self.__timeseries_export.values.append({})
# Transfer units from import timeseries
self.__timeseries_export.units = self.__timeseries_import.units
# Start looping over the ensembles for extraction of the output values.
times = self.times()
......@@ -338,13 +331,13 @@ class PIMixin(OptimizationProblem):
# Check if ID mapping is present
try:
location_parameter_id = self.__timeseries_export._data_config.pi_variable_ids(variable)
location_parameter_id = self.__data_config.pi_variable_ids(variable)
except KeyError:
logger.debug('PIMixIn: variable {} has no mapping defined in rtcDataConfig so cannot be added to the output file.'.format(variable))
continue
# Add series to output file
self.__timeseries_export.set(variable, values, ensemble_member=ensemble_member)
self.__timeseries_export.set(variable, values, unit=self.__timeseries_import.get_unit(variable, ensemble_member=ensemble_member), ensemble_member=ensemble_member)
# Write output file to disk
self.__timeseries_export.write()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment