Skip to content

Commit

Permalink
WIP after reading config file
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Jurgen Griesfeller committed May 27, 2024
1 parent ed522ca commit f0c39ea
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/pyaro_readers/pyaro2pyaerocom/Pyaro2PyaerocomReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)
from tqdm import tqdm
from pyaro_readers.units_helpers import UALIASES
import configparser

from pathlib import Path
import re
Expand All @@ -28,6 +29,8 @@

INI_MASK = "*.ini"

VAR_INI = "variables.ini"


class Pyaro2PyaerocomReaderException(Exception):
pass
Expand Down Expand Up @@ -63,6 +66,7 @@ def __init__(
self._opts = pyaro_opts
self._variables = {}
self._metadata = {}
self.reader_config = {}
blubb = PyaroReadOptions()

# find ini files in current directory for a list of supported readers
Expand All @@ -71,7 +75,20 @@ def __init__(
ini_files = glob.glob(pattern)
self.supported_readers = []
for _ini in ini_files:
self.supported_readers.append(os.path.basename(_ini).replace(".ini", ""))
if os.path.basename(_ini) == VAR_INI:
self.var_config = self._ini_to_dict(_ini)
else:
_reader_name = os.path.basename(_ini).replace(".ini", "")
self.supported_readers.append(_reader_name)
self.reader_config[_reader_name] = self._ini_to_dict(_ini)
# Now add the units to the reader config
for _reader in self.reader_config:
for _var in self.reader_config[_reader]:
if "unit" not in self.reader_config[_reader][_var]:
try:
self.reader_config[_reader][_var]["unit"] = self.var_config[_var]["unit"]
except KeyError:
pass

# variable include filter comes like this
# {'variables': {'include': ['PM10_density']}}
Expand All @@ -88,6 +105,24 @@ def read_file(self, filename: [Path, str], vars_to_read: list[str] = None):
pass
return None

def _ini_to_dict(self, filename: [Path, str]) -> dict[str, Any]:
# read configurations
_config_ini = configparser.ConfigParser()
_config_ini.read(filename)
_reader_name = os.path.basename(filename).replace(".ini", "")
# convert to dict for simplicity
ret_data = {}
# ret_data[_reader_name] = {}
for _section in _config_ini.sections():
ret_data[_section] = {}
_items = _config_ini.items(_section)
for _item in _items:
ret_data[_section][_item[0]] = _item[
1
].split(",")

return ret_data

def _unfiltered_data(self, varname) -> Data:
return self._data[varname]

Expand Down
1 change: 1 addition & 0 deletions tests/test_Pyaro2PyaerocomReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def test_1supported_pyaro_readers(self):
with pyaro.open_timeseries(self.engine, read_options, filters=[]) as ts:
self.assertGreaterEqual(len(ts.supported_readers), 1)
assert "nilupmfebas" in ts.supported_readers
assert "nilupmfebas" in ts.reader_config

# def test_1open_single_file(self):
# with pyaro.open_timeseries(self.engine, self.file, filters=[]) as ts:
Expand Down

0 comments on commit f0c39ea

Please sign in to comment.