Coverage for mlair/reference_models/reference_model_intellio3_v1.py: 38%
36 statements
« prev ^ index » next coverage.py v6.4.2, created at 2022-12-02 15:24 +0000
« prev ^ index » next coverage.py v6.4.2, created at 2022-12-02 15:24 +0000
1"""
2Extract forecasts from intelliO3 and store them for MLAir
4"""
6__author__ = "Felix Kleinert"
7__date__ = "2021-01-29"
9import os
10import xarray as xr
11import shutil
13from mlair.configuration.path_config import check_path_and_create
14from mlair.reference_models.abstract_reference_model import AbstractReferenceB2share
17class IntelliO3_ts_v1(AbstractReferenceB2share):
18 """
19 Reference handler that extracts IntelliO3-ts v1.0 forecasts (Kleinert, 2021).
21 IntelliO3 forecasts can be used as a competitive model within MLAir. Downloads the IntelliO3 tar-ball and extracts
22 the forecasts.
24 Kleinert, F., Leufen, L. H., and Schultz, M. G.: IntelliO3-ts v1.0: a neural network approach to predict
25 near-surface ozone concentrations in Germany, Geosci. Model Dev., 14, 1–25,
26 https://doi.org/10.5194/gmd-14-1-2021, 2021.
27 """
29 def __init__(self, ref_name: str, ref_store_path: str = None):
30 """
31 :param ref_name: Desired Name of reference forecast
32 :type ref_name: str
33 :param ref_store_path: Path to store reference forecasts
34 :type ref_store_path: str
35 """
36 super().__init__(b2share_hosturl="https://b2share.eudat.eu",
37 b2share_bucket="0cae9db2-f388-4136-8d28-9d9c5665d641",
38 b2share_key="IntelliO3-ts.tar.gz",
39 )
40 self.ref_name = ref_name
41 if ref_store_path is None:
42 ref_store_path = f"{self.ref_name}/"
43 self.ref_store_path = ref_store_path
44 self.tmp_extract_path = "tmp_downloads/"
45 self.orig_forecast_path = "IntelliO3-ts/IntelliO3-ts_network/forecasts/"
46 self.file_pattern = "forecasts_DE?????_test.nc"
48 def untar_forecasts(self):
49 """
50 Extracts IntelliO3 forecasts from tar-ball.
51 """
52 cmd = f"tar -xf {self.tmp_extract_path}{self.b2share_key} --directory {self.tmp_extract_path} --wildcards --no-anchored '{self.orig_forecast_path}{self.file_pattern}'"
53 os.system(cmd)
55 def file_list(self):
56 """
57 :return: base dir of tmp path and list of forecast files
58 :rtype: tuple(str, list(str))
59 """
60 for base_dir, dirs, file_names in os.walk(self.tmp_extract_path + self.orig_forecast_path):
61 pass
62 return base_dir, file_names
64 def read_and_drop(self, sel_coords: dict = None):
65 """
66 Reads original forecast files, renames coord type and store forecasts as NetCdf4 files
67 :param sel_coords:
68 """
69 if sel_coords is None:
70 sel_coords = {'type': ['CNN']}
71 in_path, files = self.file_list()
72 check_path_and_create(self.ref_store_path)
73 for infile in files:
74 data = xr.open_dataarray(os.path.join(in_path, infile))
75 data = data.sel(**sel_coords)
76 data.coords['type'] = ['nn']
77 data.to_netcdf(os.path.join(self.ref_store_path, infile))
79 def make_reference_available_locally(self, remove_tmp_dir: bool = True):
80 """
82 :return:
83 :rtype:
84 """
85 if not self.is_reference_available_locally(self.ref_store_path):
86 if not os.path.exists(self.tmp_extract_path+self.b2share_key):
87 self.download_from_b2share(tmp_download_path=self.tmp_extract_path)
88 self.untar_forecasts()
89 self.read_and_drop()
90 if remove_tmp_dir:
91 shutil.rmtree(self.tmp_extract_path)
94if __name__ == '__main__':
95 io3 = IntelliO3_ts_v1('IntelliO3-ts')
96 io3.make_reference_available_locally()