diff --git a/experiment/meteo_france_data/scm_models_data/abstract_study.py b/experiment/meteo_france_data/scm_models_data/abstract_study.py index 65fe393b3c03cd24679782f7489221196f038197..d5aeb58f59d90fc064f1073176eb917309cf1651 100644 --- a/experiment/meteo_france_data/scm_models_data/abstract_study.py +++ b/experiment/meteo_france_data/scm_models_data/abstract_study.py @@ -35,6 +35,11 @@ with redirect_stdout(f): class AbstractStudy(object): """ + A Study is defined by: + - a variable class that correspond to the meteorogical quantity of interest + - an altitude of interest + - a start and a end year + Les fichiers netcdf de SAFRAN et CROCUS sont autodocumentés (on peut les comprendre avec ncdump -h notamment). """ REANALYSIS_FOLDER = 'alp_flat/reanalysis' @@ -96,13 +101,10 @@ class AbstractStudy(object): @property def _year_to_daily_time_serie_array(self) -> OrderedDict: # Map each year to a matrix of size 365-nb_days_consecutive+1 x nb_massifs - variables = [self.instantiate_variable_object(variable_array) for variable_array in - self.year_to_variable_array.values()] - year_to_variable = dict(zip(self.ordered_years, variables)) year_to_daily_time_serie_array = OrderedDict() for year in self.ordered_years: # Check daily data - daily_time_serie = year_to_variable[year].daily_time_serie_array + daily_time_serie = self.year_to_variable_object[year].daily_time_serie_array assert daily_time_serie.shape[0] in [365, 366] assert daily_time_serie.shape[1] == len(ZS_INT_MASK) # Filter only the data corresponding to the altitude of interest @@ -110,29 +112,31 @@ class AbstractStudy(object): year_to_daily_time_serie_array[year] = daily_time_serie return year_to_daily_time_serie_array - def instantiate_variable_object(self, variable_array) -> AbstractVariable: - return self.variable_class(variable_array) + """ Load Variables and Datasets """ @cached_property - def year_to_variable_array(self) -> OrderedDict: + def year_to_variable_object(self) -> OrderedDict: # Map each year to the variable array path_files, ordered_years = self.ordered_years_and_path_files if self.multiprocessing: with Pool(NB_CORES) as p: - variables = p.map(self.load_variables, path_files) + variables = p.map(self.load_variable_object, path_files) else: - variables = [self.load_variables(path_file) for path_file in path_files] + variables = [self.load_variable_object(path_file) for path_file in path_files] return OrderedDict(zip(ordered_years, variables)) - def load_variables(self, path_file): + def instantiate_variable_object(self, variable_array) -> AbstractVariable: + return self.variable_class(variable_array) + + def load_variable_array(self, dataset): + return np.array(dataset.variables[self.load_keyword()]) + + def load_variable_object(self, path_file): dataset = Dataset(path_file) - keyword = self.load_keyword() - if isinstance(keyword, str): - return np.array(dataset.variables[keyword]) - else: - return [np.array(dataset.variables[k]) for k in keyword] + variable_array = self.load_variable_array(dataset) + return self.instantiate_variable_object(variable_array) def load_keyword(self): return self.variable_class.keyword() diff --git a/experiment/meteo_france_data/scm_models_data/safran/safran.py b/experiment/meteo_france_data/scm_models_data/safran/safran.py index cd718e2239e4f147935d89f6fff0d52f853e16ce..28c47d8a225137711c5f3ac1be96fbcd701334c6 100644 --- a/experiment/meteo_france_data/scm_models_data/safran/safran.py +++ b/experiment/meteo_france_data/scm_models_data/safran/safran.py @@ -42,6 +42,9 @@ class SafranTotalPrecip(CumulatedStudy, Safran): def __init__(self, *args, **kwargs): super().__init__(SafranTotalPrecipVariable, *args, **kwargs) + def load_variable_array(self, dataset): + return [np.array(dataset.variables[k]) for k in self.load_keyword()] + def instantiate_variable_object(self, variable_array) -> AbstractVariable: variable_array_snowfall, variable_array_rainfall = variable_array return self.variable_class(variable_array_snowfall, variable_array_rainfall, self.nb_consecutive_days) diff --git a/experiment/meteo_france_data/visualization/hypercube_visualization/main_hypercube_visualization.py b/experiment/meteo_france_data/visualization/hypercube_visualization/main_hypercube_visualization.py index e39c379084d6ebff3f8c2667c0cb0a832f3d4d29..2669bf070f6cdf3a82a537b686ad33e0fcec792c 100644 --- a/experiment/meteo_france_data/visualization/hypercube_visualization/main_hypercube_visualization.py +++ b/experiment/meteo_france_data/visualization/hypercube_visualization/main_hypercube_visualization.py @@ -54,7 +54,7 @@ def full_quantity_altitude_hypercube(): def fast_altitude_hypercube(): - save_to_file = False + save_to_file = True only_first_one = False fast = True altitudes = ALL_ALTITUDES[2:4] diff --git a/test/test_experiment/test_SCM_study.py b/test/test_experiment/test_SCM_study.py index 206bdf6dd1e3d37eaf01f843476107f107231f85..eeed25f937ffeca2b6f885fc7855b5c3ea44bb2c 100644 --- a/test/test_experiment/test_SCM_study.py +++ b/test/test_experiment/test_SCM_study.py @@ -33,8 +33,7 @@ class TestSCMAllStudy(unittest.TestCase): altitudes=sample(set(ALL_ALTITUDES), k=nb_sample), nb_days=nb_days): self.assertTrue('day' in study.variable_name) first_path_file = study.ordered_years_and_path_files[0][0] - variable_array = study.load_variables(path_file=first_path_file) - variable_object = study.instantiate_variable_object(variable_array) + variable_object = study.load_variable_object(path_file=first_path_file) self.assertEqual((365, 263), variable_object.daily_time_serie_array.shape, msg='{} days for type {}'.format(nb_days, get_display_name_from_object_type(type(variable_object))))