Skip to content

Commit

Permalink
Merge pull request #135 from ocefpaf/future_proof
Browse files Browse the repository at this point in the history
Future proof pocean core by fixing all warnings and future deprecations
  • Loading branch information
ocefpaf authored Feb 5, 2025
2 parents b32b467 + 6a33fcc commit 4257910
Show file tree
Hide file tree
Showing 16 changed files with 72 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ jobs:
- name: Tests
run: >
python pocean/tests/download_test_data.py
&& python -m pytest --disable-warnings
&& python -m pytest --pyargs pocean
6 changes: 4 additions & 2 deletions pocean/cf.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#!python
import datetime
import itertools
import os
import re
from datetime import datetime

from . import logger
from .dataset import EnhancedDataset
from .utils import all_subclasses, is_url

datetime.UTC = datetime.timezone.utc


class CFDataset(EnhancedDataset):
default_fill_value = False
Expand Down Expand Up @@ -182,7 +184,7 @@ def nc_attributes(self):
return {
"global": {
"Conventions": "CF-1.6",
"date_created": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:00Z"),
"date_created": datetime.datetime.now(datetime.UTC).strftime("%Y-%m-%dT%H:%M:00Z"),
}
}

Expand Down
1 change: 0 additions & 1 deletion pocean/dsg/profile/om.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!python
from collections import OrderedDict
from copy import copy

Expand Down
8 changes: 4 additions & 4 deletions pocean/dsg/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!python

from datetime import datetime
import datetime

import pandas as pd
from shapely.geometry import (
Expand All @@ -14,6 +12,8 @@
from pocean import logger as L # noqa
from pocean.utils import dict_update, get_default_axes, unique_justseen

datetime.UTC = datetime.timezone.utc


def get_calculated_attributes(df, axes=None, history=None):
"""Functions to automate netCDF attribute generation from the data itself
Expand Down Expand Up @@ -187,7 +187,7 @@ def get_creation_attributes(history=None):
:param history: text initializing audit trail for modifications to the original data (optional, string)
:return: dictionary of global attributes
"""
nc_create_ts = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
nc_create_ts = datetime.datetime.now(datetime.UTC).strftime("%Y-%m-%dT%H:%M:%SZ")

attrs = {
"attributes": {
Expand Down
4 changes: 2 additions & 2 deletions pocean/tests/download_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

def download_test_data():
url = "https://github.com/pyoceans/pocean-core/releases/download"
version = "2024.04"
version = "2025.01"

fname = pooch.retrieve(
url=f"{url}/{version}/test_data.zip",
known_hash="sha256:28be36e8e0ec90a8faf5ee3f4d52638bdeb2cbcbeac8c823de680cf84aa34940",
known_hash="sha256:41180c6bc6017de935250c9e8c1bbb407507049baebd767692c4f74fb8d662a8",
)

here = Path(__file__).resolve().parent
Expand Down
1 change: 0 additions & 1 deletion pocean/tests/dsg/profile/test_profile_om.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!python
import logging
import os
import tempfile
Expand Down
17 changes: 12 additions & 5 deletions pocean/tests/dsg/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
#!python
import datetime
import os
import unittest
from datetime import datetime, timedelta

import pandas as pd
import pytest
import pytz
from dateutil.parser import parse as dtparse

from pocean import logger as L # noqa
from pocean.cf import CFDataset
from pocean.dsg import utils

datetime.UTC = datetime.timezone.utc

# RuntimeWarning: invalid value encountered in cast is fine here.
ignore_invalid_value_cast = pytest.mark.filterwarnings("ignore::RuntimeWarning")


class TestDsgUtils(unittest.TestCase):
geo = pd.DataFrame({"x": [-1, -2, -3, -4], "y": [1, 2, 3, 4]})
Expand Down Expand Up @@ -139,13 +145,14 @@ def test_get_temporal_meta_from_times(self):
def test_get_creation(self):
meta = utils.get_creation_attributes(history="DID THINGS")

now = datetime.utcnow().replace(tzinfo=pytz.utc)
now = datetime.datetime.now(datetime.UTC).replace(tzinfo=pytz.utc)

assert (now - dtparse(meta["attributes"]["date_created"])) < timedelta(minutes=1)
assert (now - dtparse(meta["attributes"]["date_issued"])) < timedelta(minutes=1)
assert (now - dtparse(meta["attributes"]["date_modified"])) < timedelta(minutes=1)
assert (now - dtparse(meta["attributes"]["date_created"])) < datetime.timedelta(minutes=1)
assert (now - dtparse(meta["attributes"]["date_issued"])) < datetime.timedelta(minutes=1)
assert (now - dtparse(meta["attributes"]["date_modified"])) < datetime.timedelta(minutes=1)
assert "DID THINGS" in meta["attributes"]["history"]

@ignore_invalid_value_cast
def test_wrap_dateline(self):
ncfile = os.path.join(
os.path.dirname(os.path.dirname(__file__)), "resources/wrapping_dateline.nc"
Expand Down
10 changes: 10 additions & 0 deletions pocean/tests/dsg/timeseries/test_timeseries_om.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
import unittest

import numpy as np
import pytest

from pocean import logger
from pocean.dsg import OrthogonalMultidimensionalTimeseries
from pocean.tests.dsg.test_new import test_is_mine

# RuntimeWarning: invalid value encountered in cast is fine here.
ignore_invalid_value_cast = pytest.mark.filterwarnings("ignore::RuntimeWarning")

logger.level = logging.INFO
logger.handlers = [logging.StreamHandler()]

Expand Down Expand Up @@ -51,6 +55,7 @@ def test_omp_load(self):
OrthogonalMultidimensionalTimeseries(self.single).close()
OrthogonalMultidimensionalTimeseries(self.multi).close()

@ignore_invalid_value_cast
def test_timeseries_omt_dataframe_single(self):
fid, single_tmp = tempfile.mkstemp(suffix=".nc")
with OrthogonalMultidimensionalTimeseries(self.single) as s:
Expand All @@ -76,6 +81,7 @@ def test_timeseries_omt_dataframe_multi(self):
os.close(fid)
os.remove(single_tmp)

@ignore_invalid_value_cast
def test_timeseries_omt_dataframe_unique_dims(self):
fid, single_tmp = tempfile.mkstemp(suffix=".nc")
with OrthogonalMultidimensionalTimeseries(self.single) as s:
Expand All @@ -89,6 +95,7 @@ def test_timeseries_omt_dataframe_unique_dims(self):
os.close(fid)
os.remove(single_tmp)

@ignore_invalid_value_cast
def test_timeseries_omt_reduce_dims(self):
fid, single_tmp = tempfile.mkstemp(suffix=".nc")
with OrthogonalMultidimensionalTimeseries(self.single) as s:
Expand All @@ -102,6 +109,7 @@ def test_timeseries_omt_reduce_dims(self):
os.close(fid)
os.remove(single_tmp)

@ignore_invalid_value_cast
def test_timeseries_omt_no_z(self):
fid, single_tmp = tempfile.mkstemp(suffix=".nc")
with OrthogonalMultidimensionalTimeseries(self.single) as s:
Expand All @@ -120,6 +128,7 @@ def test_timeseries_omt_no_z(self):
os.close(fid)
os.remove(single_tmp)

@ignore_invalid_value_cast
def test_timeseries_omt_no_z_no_station(self):
fid, single_tmp = tempfile.mkstemp(suffix=".nc")
with OrthogonalMultidimensionalTimeseries(self.single) as s:
Expand All @@ -136,6 +145,7 @@ def test_timeseries_omt_no_z_no_station(self):
os.close(fid)
os.remove(single_tmp)

@ignore_invalid_value_cast
def test_supplying_attributes(self):
fid, single_tmp = tempfile.mkstemp(suffix=".nc")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import numpy as np
import pandas as pd
import pytest

from pocean import logger
from pocean.dsg import OrthogonalMultidimensionalTimeseriesProfile
Expand All @@ -14,6 +15,9 @@
logger.level = logging.INFO
logger.handlers = [logging.StreamHandler()]

# RuntimeWarning: invalid value encountered in cast is fine here.
ignore_invalid_value_cast = pytest.mark.filterwarnings("ignore::RuntimeWarning")


class TestOrthogonalMultidimensionalTimeseriesProfile(unittest.TestCase):
def test_omtp_multi(self):
Expand Down Expand Up @@ -237,6 +241,7 @@ def test_detach_z_with_station(self):
os.close(fid)
os.remove(tmpfile)

@ignore_invalid_value_cast
def test_detach_z_no_station(self):
df = pd.DataFrame(
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!python
import logging
import os
import tempfile
Expand All @@ -7,6 +6,7 @@

import netCDF4 as nc4
import pandas as pd
import pytest
from numpy.testing import assert_array_equal as npeq

from pocean import logger
Expand All @@ -17,6 +17,9 @@
logger.level = logging.INFO
logger.handlers = [logging.StreamHandler()]

# RuntimeWarning: invalid value encountered in cast is fine here.
ignore_invalid_value_cast = pytest.mark.filterwarnings("ignore::RuntimeWarning")


class TestRaggedTimeseriesProfile(unittest.TestCase):
def test_csv_to_nc_single(self):
Expand Down Expand Up @@ -179,6 +182,7 @@ def test_csv_to_nc_single_reduce(self):
os.close(fid)
os.remove(tmpfile)

@ignore_invalid_value_cast
def test_rtp_single(self):
filepath = os.path.join(os.path.dirname(__file__), "resources", "r-ctd-single.nc")

Expand Down
5 changes: 5 additions & 0 deletions pocean/tests/dsg/trajectory/test_trajectory_cr.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
logger.level = logging.INFO
logger.handlers = [logging.StreamHandler()]

# RuntimeWarning: invalid value encountered in cast is fine here.
ignore_invalid_value_cast = pytest.mark.filterwarnings("ignore::RuntimeWarning")


@pytest.mark.parametrize(
"fp",
Expand Down Expand Up @@ -89,6 +92,7 @@ def test_crt_dataframe_unlimited_dim(self):
os.close(fid)
os.remove(tmpnc)

@ignore_invalid_value_cast
def test_crt_dataframe_oot_A(self):
axes = {"t": "time", "x": "lon", "y": "lat", "z": "depth", "sample": "sample"}
fid, tmpnc = tempfile.mkstemp(suffix=".nc")
Expand All @@ -113,6 +117,7 @@ def test_crt_dataframe_oot_A(self):
os.close(fid)
os.remove(tmpnc)

@ignore_invalid_value_cast
def test_crt_dataframe_oot_B(self):
axes = {
"t": "time",
Expand Down
6 changes: 6 additions & 0 deletions pocean/tests/dsg/trajectory/test_trajectory_im.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import unittest

import numpy as np
import pytest
from dateutil.parser import parse as dtparse

from pocean import logger
Expand All @@ -15,8 +16,12 @@
logger.level = logging.INFO
logger.handlers = [logging.StreamHandler()]

# RuntimeWarning: invalid value encountered in cast is fine here.
ignore_invalid_value_cast = pytest.mark.filterwarnings("ignore::RuntimeWarning")


class TestIncompleteMultidimensionalTrajectory(unittest.TestCase):
@ignore_invalid_value_cast
def test_im_single_row(self):
filepath = os.path.join(os.path.dirname(__file__), "resources", "im-singlerow.nc")

Expand Down Expand Up @@ -75,6 +80,7 @@ def test_imt_multi(self):
os.close(fid)
os.remove(tmpfile)

@ignore_invalid_value_cast
def test_imt_multi_not_string(self):
filepath = os.path.join(os.path.dirname(__file__), "resources", "im-multiple-nonstring.nc")

Expand Down
11 changes: 11 additions & 0 deletions pocean/tests/dsg/trajectoryProfile/test_trajectoryProfile_cr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import unittest

import numpy as np
import pytest
from dateutil.parser import parse as dtparse
from shapely.wkt import loads as wktloads

Expand All @@ -15,6 +16,9 @@
L.level = logging.INFO
L.handlers = [logging.StreamHandler()]

# RuntimeWarning: invalid value encountered in cast is fine here.
ignore_invalid_value_cast = pytest.mark.filterwarnings("ignore::RuntimeWarning")


class TestContinousRaggedTrajectoryProfile(unittest.TestCase):
def setUp(self):
Expand All @@ -32,6 +36,7 @@ def test_crtp_load(self):
ContiguousRaggedTrajectoryProfile(self.multi).close()
ContiguousRaggedTrajectoryProfile(self.missing_time).close()

@ignore_invalid_value_cast
def test_crtp_dataframe_single(self):
axes = {
"t": "time",
Expand All @@ -51,6 +56,7 @@ def test_crtp_dataframe_single(self):
os.close(fid)
os.remove(tmpnc)

@ignore_invalid_value_cast
def test_crtp_dataframe_single_unique_dims(self):
axes = {
"t": "time",
Expand Down Expand Up @@ -89,6 +95,7 @@ def test_crtp_dataframe_multi(self):
os.close(fid)
os.remove(tmpnc)

@ignore_invalid_value_cast
def test_crtp_dataframe_missing_time(self):
axes = {
"t": "precise_time",
Expand All @@ -108,6 +115,7 @@ def test_crtp_dataframe_missing_time(self):
os.close(fid)
os.remove(tmpnc)

@ignore_invalid_value_cast
def test_crtp_calculated_metadata_single(self):
axes = {
"t": "time",
Expand Down Expand Up @@ -173,6 +181,7 @@ def test_crtp_calculated_metadata_multi(self):
assert traj4.profiles[19].x == -44
assert traj4.profiles[19].y == 47

@ignore_invalid_value_cast
def test_crtp_calculated_metadata_missing_time(self):
axes = {
"t": "time",
Expand All @@ -199,6 +208,7 @@ def test_crtp_calculated_metadata_missing_time(self):
assert np.isclose(first_loc[1], 43.5022166666667)
assert len(traj.profiles) == 13

@ignore_invalid_value_cast
def test_crtp_just_missing_time(self):
axes = {
"t": "time",
Expand All @@ -224,6 +234,7 @@ def test_crtp_just_missing_time(self):
assert np.isclose(first_loc[1], 43.5022166666667)
assert len(traj.profiles) == 13

@ignore_invalid_value_cast
def test_crtp_just_missing_locations(self):
axes = {
"t": "time",
Expand Down
Loading

0 comments on commit 4257910

Please sign in to comment.