Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
thorbjoernl committed Aug 7, 2024
1 parent 86a37b7 commit 3567416
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ def __init__(
lines = csvfile.readlines()
self._revision = self._revision_string_from_lines(lines)


for _hidx in range(HEADER_LINE_NO - 1):
self._header.append(lines.pop(0))
# get fields from header line although csv can do that as well since we might want to adjust these names
Expand Down Expand Up @@ -264,13 +263,13 @@ def __init__(
value, station, lat, lon, alt, start, end, Flag.VALID, np.nan
)
bar.close()

def _revision_string_from_lines(self, lines: list[str]) -> str:
return hashlib.md5("".join(lines).encode()).hexdigest()

def metadata(self):
return dict(revision = self._revision)
return dict(revision=self._revision)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def __init__(
value, station, lat, lon, alt, start, end, Flag.VALID, np.nan
)
bar.close()

def _revison_str_from_lines(self, lines: list[str]):
"""Returns a revision string for a list of text lines. The revision
string is the hash resulting from joinint the lines together.
Expand All @@ -210,14 +210,14 @@ def _revison_str_from_lines(self, lines: list[str]):
An md5 hex digest.
"""
return hashlib.md5("".join(lines).encode()).hexdigest()

def metadata(self):
metadata = dict()
if self._revisionstr is not None:
metadata["revision"] = self._revisionstr

return metadata

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

Expand Down
7 changes: 3 additions & 4 deletions src/pyaro_readers/ascii2netcdf/Ascii2NetcdfTimeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,22 @@ def iterate_files(self):
if os.path.exists(file_path):
yield file_path


def metadata(self):
metadata = dict()
date = datetime.datetime.min
for f in self.iterate_files():
with xr.open_dataset(f) as d:
hist: str = d.attrs.get("last_changed", "")

datestr = hist.split("//")[0]
new_date = datetime.datetime.strptime(datestr, "%a %b %d %H:%M:%S %Y")
if new_date > date:
date = new_date

metadata["revision"] = str(date)

return metadata

def _is_year_in_filters(self, year):
start_year = np.datetime64(f"{year}-01-01 00:00:00")
end_year = np.datetime64(f"{year}-12-31 23:59:59")
Expand Down
5 changes: 3 additions & 2 deletions src/pyaro_readers/harpreader/harpreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from pyaro_readers.units_helpers import UALIASES
import datetime
from functools import cache

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -105,12 +106,12 @@ def metadata(self):
for f in self._files:
with xr.open_dataset(f) as d:
hist: str = d.attrs.get("history", "")

datestr = ":".join(hist.split(":")[:3])
new_date = datetime.datetime.strptime(datestr, "%a %b %d %H:%M:%S %Y")
if new_date > date:
date = new_date

metadata["revision"] = str(date)

return metadata
Expand Down
16 changes: 9 additions & 7 deletions src/pyaro_readers/netcdf_rw/Netcdf_RWTimeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,29 @@ def metadata(self):
date = datetime.datetime.min
for f in self.iterate_files():
with xr.open_dataset(f) as d:
hist = d.attrs.get("last_changed", None)
hist = d.attrs.get("last_changed", None)

if hist is not None:
datestr = hist.split("//")[0]

else:
hist = d.attrs.get("history", "")[-1]
datestr = " ".join(hist.split(" ")[:2])
datestr = " ".join(hist.split(" ")[:2])


try:
new_date = datetime.datetime.strptime(datestr, "%a %b %d %H:%M:%S %Y")
new_date = datetime.datetime.strptime(
datestr, "%a %b %d %H:%M:%S %Y"
)
except:
new_date = datetime.datetime.strptime(datestr, "%Y-%m-%d %H:%M:%S")

if new_date > date:
date = new_date

metadata["revision"] = str(date)

return metadata
return metadata

def _read_json(self, file, empty):
filepath = os.path.join(self._directory, file)
res = empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def _process_open_file(
def metadata(self):
metadata = dict()
return metadata

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

Expand All @@ -205,7 +205,7 @@ def _lookup_function(self):
return lambda lat, lon: geo.lookup_nearest(lat, lon)["ISO_A2_EH"]


class NILUPMFAbsorptionTimeseriesEngine(AutoFilterReaderEngine.AutoFilterEngine):#
class NILUPMFAbsorptionTimeseriesEngine(AutoFilterReaderEngine.AutoFilterEngine): #
def reader_class(self):
return NILUPMFAbsorptionReader

Expand Down
15 changes: 11 additions & 4 deletions src/pyaro_readers/nilupmfebas/EbasPmfReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ def __init__(
else:
# filename is something else
raise EBASPMFReaderException(f"No such file or directory: {filename}")

def metadata(self):
metadata = dict()
metadata["revision"] = str(self._revision)
metadata["revision"] = str(self._revision)
return metadata

def read_file_basic(
self,
filename: Path | str,
Expand All @@ -105,7 +105,14 @@ def read_file(self, filename: Path | str, vars_to_read: list[str] = None):
"""Read EBAS NASA Ames file and put the data in the object"""

_file_dummy = self.read_file_basic(filename)
self._revision = max([self._revision, datetime.datetime.strptime(_file_dummy.meta["revision_date"], "%Y%m%d%H%M%S")])
self._revision = max(
[
self._revision,
datetime.datetime.strptime(
_file_dummy.meta["revision_date"], "%Y%m%d%H%M%S"
),
]
)

matrix = _file_dummy.meta["matrix"]
vars_read_in_file = []
Expand Down
16 changes: 12 additions & 4 deletions tests/test_AERONETSDATimeSeriesReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ def test_dl_data_tared(self):
self.assertEqual(count, 421984)
self.assertEqual(len(ts.stations()), 94)
self.assertIn("revision", ts.metadata())
self.assertEqual(ts.metadata()["revision"], "a4a2de1ef3a22514256c19adefb9e0d5")
self.assertEqual(
ts.metadata()["revision"], "a4a2de1ef3a22514256c19adefb9e0d5"
)

def test_dl_data_unzipped(self):
if not self.external_resource_available(TEST_URL):
Expand All @@ -66,7 +68,9 @@ def test_dl_data_unzipped(self):
self.assertEqual(count, 79944)
self.assertEqual(len(ts.stations()), 4)
self.assertIn("revision", ts.metadata())
self.assertEqual(ts.metadata()["revision"], "0c6171c9664840b4f4def0616dbe5b21")
self.assertEqual(
ts.metadata()["revision"], "0c6171c9664840b4f4def0616dbe5b21"
)

def test_dl_data_zipped(self):
if not self.external_resource_available(TEST_ZIP_URL):
Expand All @@ -85,7 +89,9 @@ def test_dl_data_zipped(self):
self.assertEqual(len(ts.stations()), 4)

self.assertIn("revision", ts.metadata())
self.assertEqual(ts.metadata()["revision"], "0c6171c9664840b4f4def0616dbe5b21")
self.assertEqual(
ts.metadata()["revision"], "0c6171c9664840b4f4def0616dbe5b21"
)

def test_aeronet_data_zipped(self):
if not os.path.exists("/lustre"):
Expand All @@ -107,7 +113,9 @@ def test_aeronet_data_zipped(self):
self.assertGreaterEqual(len(ts.stations()), 4)

self.assertIn("revision", ts.metadata())
self.assertEqual(ts.metadata()["revision"], "d9ba8e645a625e86dc981aeedc54c02d")
self.assertEqual(
ts.metadata()["revision"], "d9ba8e645a625e86dc981aeedc54c02d"
)

def test_init(self):
engine = pyaro.list_timeseries_engines()["aeronetsdareader"]
Expand Down
12 changes: 9 additions & 3 deletions tests/test_AERONETTimeSeriesReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def test_dl_data_unzipped(self):
count += len(ts.data(var))
self.assertEqual(count, 49965)
self.assertEqual(len(ts.stations()), 4)
self.assertEqual(ts.metadata()["revision"], "035e8b31b2ffdf34703206061aa13ebf")
self.assertEqual(
ts.metadata()["revision"], "035e8b31b2ffdf34703206061aa13ebf"
)

def test_dl_data_zipped(self):
if not self.external_resource_available(TEST_ZIP_URL):
Expand All @@ -61,7 +63,9 @@ def test_dl_data_zipped(self):
count += len(ts.data(var))
self.assertEqual(count, 49965)
self.assertEqual(len(ts.stations()), 4)
self.assertEqual(ts.metadata()["revision"], "035e8b31b2ffdf34703206061aa13ebf")
self.assertEqual(
ts.metadata()["revision"], "035e8b31b2ffdf34703206061aa13ebf"
)

def test_aeronet_data_zipped(self):
if not os.path.exists("/lustre"):
Expand All @@ -81,7 +85,9 @@ def test_aeronet_data_zipped(self):
count += len(ts.data(var))
self.assertGreaterEqual(count, 49965)
self.assertGreaterEqual(len(ts.stations()), 4)
self.assertEqual(ts.metadata()["revision"], "0cc1923c0c9860307343e5f4edd3d5cd")
self.assertEqual(
ts.metadata()["revision"], "0cc1923c0c9860307343e5f4edd3d5cd"
)

def test_init(self):
engine = pyaro.list_timeseries_engines()["aeronetsunreader"]
Expand Down
2 changes: 1 addition & 1 deletion tests/test_Ascii2NetcdfTimeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ def test_3read(self):
len(data.values[data.values > 4]), 1
) # one day (21.05. with extreme SO2)

self.assertIn("revision", ts.metadata())
self.assertIn("revision", ts.metadata())
5 changes: 2 additions & 3 deletions tests/test_HARPReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_1read(self):
self.assertGreaterEqual(len(ts.stations()), 1)

self.assertIn("revision", ts.metadata())
self.assertEqual(ts.metadata()["revision"], '2024-03-26 15:01:36')
self.assertEqual(ts.metadata()["revision"], "2024-03-26 15:01:36")

def test_2open_directory(self):
if os.path.exists(self.testdata_dir):
Expand All @@ -49,9 +49,8 @@ def test_2open_directory(self):
assert isinstance(data.units, str)
self.assertGreaterEqual(len(ts.variables()), 2)
self.assertGreaterEqual(len(ts.stations()), 7)

self.assertIn("revision", ts.metadata())


else:
pass
Expand Down

0 comments on commit 3567416

Please sign in to comment.