Skip to content

Commit

Permalink
Merge pull request #307 from informatics-lab/cftime-1.1.1-support
Browse files Browse the repository at this point in the history
Add cftime 1.1.1 support
  • Loading branch information
andrewgryan committed Mar 17, 2020
2 parents edaf128 + 8427220 commit ab9eacf
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
2 changes: 1 addition & 1 deletion forest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
.. automodule:: forest.presets
"""
__version__ = '0.13.0'
__version__ = '0.13.1'

from .config import *
from . import (
Expand Down
2 changes: 2 additions & 0 deletions forest/gridded_forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def _to_datetime(d):
return d
if isinstance(d, cftime.DatetimeNoLeap):
return datetime(d.year, d.month, d.day, d.hour, d.minute, d.second)
elif isinstance(d, cftime.DatetimeGregorian):
return datetime(d.year, d.month, d.day, d.hour, d.minute, d.second)
elif isinstance(d, str):
try:
return datetime.strptime(d, "%Y-%m-%d %H:%M:%S")
Expand Down
8 changes: 6 additions & 2 deletions forest/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,5 +335,9 @@ def locate(self, initial_time):

__getitem__ = locate

def key(self, time):
return "{:%Y-%m-%d %H:%M:%S}".format(time)
@staticmethod
def key(time):
try:
return "{:%Y-%m-%d %H:%M:%S}".format(time)
except TypeError:
return time.strftime("%Y-%m-%d %H:%M:%S")
33 changes: 20 additions & 13 deletions test/test_gridded_forecast.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest
import cftime
from datetime import datetime
from unittest.mock import Mock, call, patch, sentinel
import unittest
Expand All @@ -18,25 +20,30 @@ def test(self):
self.assertEqual(value, [])


@pytest.mark.parametrize("given,expect", [
pytest.param('2019-10-10 01:02:34',
datetime(2019, 10, 10, 1, 2, 34),
id="str with space"),
pytest.param('2019-10-10T01:02:34',
datetime(2019, 10, 10, 1, 2, 34),
id="iso8601"),
pytest.param(np.datetime64('2019-10-10T11:22:33'),
datetime(2019, 10, 10, 11, 22, 33),
id="datetime64"),
pytest.param(cftime.DatetimeGregorian(2019, 10, 10, 11, 22, 33),
datetime(2019, 10, 10, 11, 22, 33),
id="cftime.DatetimeGregorian"),
])
def test__to_datetime(given, expect):
assert gridded_forecast._to_datetime(given) == expect


class Test_to_datetime(unittest.TestCase):
def test_datetime(self):
dt = datetime.now()
result = gridded_forecast._to_datetime(dt)
self.assertEqual(result, dt)

def test_str_with_space(self):
result = gridded_forecast._to_datetime('2019-10-10 01:02:34')
self.assertEqual(result, datetime(2019, 10, 10, 1, 2, 34))

def test_str_iso8601(self):
result = gridded_forecast._to_datetime('2019-10-10T01:02:34')
self.assertEqual(result, datetime(2019, 10, 10, 1, 2, 34))

def test_datetime64(self):
dt = np.datetime64('2019-10-10T11:22:33')
result = gridded_forecast._to_datetime(dt)
self.assertEqual(result, datetime(2019, 10, 10, 11, 22, 33))

def test_unsupported(self):
with self.assertRaisesRegex(Exception, 'Unknown value'):
gridded_forecast._to_datetime(12)
Expand Down
9 changes: 9 additions & 0 deletions test/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest
import os
import netCDF4
import cftime
import numpy as np
import numpy.testing as npt
import datetime as dt
Expand Down Expand Up @@ -320,6 +321,14 @@ def test_4d_variable(tmpdir):
npt.assert_array_equal(expect["y"], result["y"])


@pytest.mark.parametrize("value,expect", [
(dt.datetime(2020, 1, 1), "2020-01-01 00:00:00"),
(cftime.DatetimeGregorian(2020, 1, 1), "2020-01-01 00:00:00")
])
def test_series_locator_key(value, expect):
assert series.SeriesLocator.key(value) == expect


class TestSeries(unittest.TestCase):
def setUp(self):
self.path = "test-series.nc"
Expand Down

0 comments on commit ab9eacf

Please sign in to comment.