-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2688 from sfinkens/fix-time-parameters-decoding
Decode time parameters in CF Reader
- Loading branch information
Showing
4 changed files
with
303 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# Copyright (c) 2023 Satpy developers | ||
# | ||
# This file is part of satpy. | ||
# | ||
# satpy is free software: you can redistribute it and/or modify it under the | ||
# terms of the GNU General Public License as published by the Free Software | ||
# Foundation, either version 3 of the License, or (at your option) any later | ||
# version. | ||
# | ||
# satpy is distributed in the hope that it will be useful, but WITHOUT ANY | ||
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | ||
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License along with | ||
# satpy. If not, see <http://www.gnu.org/licenses/>. | ||
"""CF decoding.""" | ||
import copy | ||
import json | ||
from datetime import datetime | ||
|
||
|
||
def decode_attrs(attrs): | ||
"""Decode CF-encoded attributes to Python object. | ||
Converts timestamps to datetime and strings starting with "{" to | ||
dictionary. | ||
Args: | ||
attrs (dict): Attributes to be decoded | ||
Returns (dict): Decoded attributes | ||
""" | ||
attrs = copy.deepcopy(attrs) | ||
_decode_dict_type_attrs(attrs) | ||
_decode_timestamps(attrs) | ||
return attrs | ||
|
||
|
||
def _decode_dict_type_attrs(attrs): | ||
for key, val in attrs.items(): | ||
attrs[key] = _str2dict(val) | ||
|
||
|
||
def _str2dict(val): | ||
"""Convert string to dictionary.""" | ||
if isinstance(val, str) and val.startswith("{"): | ||
val = json.loads(val, object_hook=_datetime_parser_json) | ||
return val | ||
|
||
|
||
def _decode_timestamps(attrs): | ||
for key, value in attrs.items(): | ||
timestamp = _str2datetime(value) | ||
if timestamp: | ||
attrs[key] = timestamp | ||
|
||
|
||
def _datetime_parser_json(json_dict): | ||
"""Traverse JSON dictionary and parse timestamps.""" | ||
for key, value in json_dict.items(): | ||
timestamp = _str2datetime(value) | ||
if timestamp: | ||
json_dict[key] = timestamp | ||
return json_dict | ||
|
||
|
||
def _str2datetime(string): | ||
"""Convert string to datetime object.""" | ||
try: | ||
return datetime.fromisoformat(string) | ||
except (TypeError, ValueError): | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# Copyright (c) 2023 Satpy developers | ||
# | ||
# This file is part of satpy. | ||
# | ||
# satpy is free software: you can redistribute it and/or modify it under the | ||
# terms of the GNU General Public License as published by the Free Software | ||
# Foundation, either version 3 of the License, or (at your option) any later | ||
# version. | ||
# | ||
# satpy is distributed in the hope that it will be useful, but WITHOUT ANY | ||
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | ||
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License along with | ||
# satpy. If not, see <http://www.gnu.org/licenses/>. | ||
"""Tests for CF decoding.""" | ||
from datetime import datetime | ||
|
||
import pytest | ||
|
||
import satpy.cf.decoding | ||
|
||
|
||
class TestDecodeAttrs: | ||
"""Test decoding of CF-encoded attributes.""" | ||
|
||
@pytest.fixture() | ||
def attrs(self): | ||
"""Get CF-encoded attributes.""" | ||
return { | ||
"my_integer": 0, | ||
"my_float": 0.0, | ||
"my_list": [1, 2, 3], | ||
"my_timestamp1": "2000-01-01", | ||
"my_timestamp2": "2000-01-01 12:15:33", | ||
"my_timestamp3": "2000-01-01 12:15:33.123456", | ||
"my_dict": '{"a": {"b": [1, 2, 3]}, "c": {"d": "2000-01-01 12:15:33.123456"}}' | ||
} | ||
|
||
@pytest.fixture() | ||
def expected(self): | ||
"""Get expected decoded results.""" | ||
return { | ||
"my_integer": 0, | ||
"my_float": 0.0, | ||
"my_list": [1, 2, 3], | ||
"my_timestamp1": datetime(2000, 1, 1), | ||
"my_timestamp2": datetime(2000, 1, 1, 12, 15, 33), | ||
"my_timestamp3": datetime(2000, 1, 1, 12, 15, 33, 123456), | ||
"my_dict": {"a": {"b": [1, 2, 3]}, | ||
"c": {"d": datetime(2000, 1, 1, 12, 15, 33, 123456)}} | ||
} | ||
|
||
def test_decoding(self, attrs, expected): | ||
"""Test decoding of CF-encoded attributes.""" | ||
res = satpy.cf.decoding.decode_attrs(attrs) | ||
assert res == expected | ||
|
||
def test_decoding_doesnt_modify_original(self, attrs): | ||
"""Test that decoding doesn't modify the original attributes.""" | ||
satpy.cf.decoding.decode_attrs(attrs) | ||
assert isinstance(attrs["my_dict"], str) |
Oops, something went wrong.