Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 2 additions & 41 deletions cdflib/xarray/xarray_to_cdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import re
from datetime import datetime
from typing import Any, Dict, List, Tuple, Union, cast
from warnings import warn

import numpy as np
import numpy.typing as npt
Expand Down Expand Up @@ -885,11 +884,6 @@ def xarray_to_cdf(
record_dimensions: List[str] = ["record0"],
compression: int = 0,
nan_to_fillval: bool = True,
from_unixtime: bool = False,
from_datetime: bool = False,
unixtime_to_cdftt2000: bool = False,
datetime_to_cdftt2000: bool = True,
datetime64_to_cdftt2000: bool = True,
) -> None:
"""
This function converts XArray Dataset objects into CDF files.
Expand All @@ -904,11 +898,6 @@ def xarray_to_cdf(
record_dimensions (list of str, optional): If the code cannot determine which dimensions should be made into CDF records, you may provide a list of them here
compression (int, optional): The level of compression to gzip the data in the variables. Default is no compression, standard is 6.
nan_to_fillval (bool, optional): Convert all np.nan and np.datetime64('NaT') to the standard CDF FILLVALs.
from_datetime (bool, optional, deprecated): Same as the datetime_to_cdftt2000 option
from_unixtime (bool, optional, deprecated): Same as the unixtime_to_cdftt2000 option
datetime_to_cdftt2000 (bool, optional, deprecated): Whether or not to convert variables named "epoch" or "epoch_X" to CDF_TT2000 from datetime objects
datetime64_to_cdftt2000 (bool, optional, deprecated): Whether or not to convert variables named "epoch" or "epoch_X" to CDF_TT2000 from the numpy datetime64
unixtime_to_cdftt2000 (bool, optional, deprecated): Whether or not to convert variables named "epoch" or "epoch_X" to CDF_TT2000 from unixtime
Returns:
None, but generates a CDF file

Expand Down Expand Up @@ -1038,30 +1027,6 @@ def xarray_to_cdf(

"""

if from_unixtime or unixtime_to_cdftt2000:
warn(
"from_unixtime and unixtime_to_cdftt2000 will eventually be phased out. Instead, use the more descriptive unix_time_to_cdf_time.",
DeprecationWarning,
stacklevel=2,
)

if from_datetime or datetime_to_cdftt2000:
warn(
"The from_datetime and datetime_to_cdftt2000 are obsolete. Python datetime objects are automatically converted to a CDF time. If you do not wish datetime objects to be converted, cast them to a different type prior to calling xarray_to_cdf()",
DeprecationWarning,
stacklevel=2,
)

if datetime64_to_cdftt2000:
warn(
"datetime64_to_cdftt2000 will eventually be phased out. Instead, datetime64 types will automatically be converted into a CDF time type. If you do not wish datetime64 arrays to be converted, cast them to a different type prior to calling xarray_to_cdf()",
DeprecationWarning,
stacklevel=2,
)

if unixtime_to_cdftt2000 or from_unixtime:
unix_time_to_cdf_time = True

if os.path.isfile(file_name):
_warn_or_except(f"{file_name} already exists, cannot create CDF file. Returning...", terminate_on_warning)
return
Expand Down Expand Up @@ -1150,9 +1115,7 @@ def xarray_to_cdf(
elif d[var].attrs["CDF_DATA_TYPE"] == "CDF_EPOCH16":
cdf_epoch16 = True

if (_is_datetime_array(d[var].data) and datetime_to_cdftt2000) or (
_is_datetime64_array(d[var].data) and datetime64_to_cdftt2000
):
if _is_datetime_array(d[var].data) or _is_datetime64_array(d[var].data):
var_data = _datetime_to_cdf_time(d[var], cdf_epoch=cdf_epoch, cdf_epoch16=cdf_epoch16)
elif unix_time_to_cdf_time:
if _is_istp_epoch_variable(var) or (
Expand All @@ -1164,9 +1127,7 @@ def xarray_to_cdf(
var_att_dict = {}
for att in d[var].attrs:
var_att_dict[att] = d[var].attrs[att]
if (_is_datetime_array(d[var].attrs[att]) and datetime_to_cdftt2000) or (
_is_datetime64_array(d[var].attrs[att]) and datetime64_to_cdftt2000
):
if _is_datetime_array(d[var].attrs[att]) or _is_datetime64_array(d[var].attrs[att]):
att_data = _datetime_to_cdf_time(d[var], cdf_epoch=cdf_epoch, cdf_epoch16=cdf_epoch16, attribute_name=att)
var_att_dict[att] = [att_data, DATATYPES_TO_STRINGS[cdf_data_type]]
elif unix_time_to_cdf_time:
Expand Down
23 changes: 2 additions & 21 deletions tests/test_xarray_reader_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@ def test_smoke(cdf_path, tmp_path):


def test_datetime64_conversion():
# This tests out the datetime64_to_cdftt2000 conversion and then back again,
# verifying that everything writes correctly
pytest.importorskip("xarray")
var_data = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
Expand All @@ -369,25 +368,7 @@ def test_datetime64_conversion():
os.remove("hello.cdf")


def test_datetime64_no_conversion():
# This tests out the expected behavior when no datetime conversion is used,
# even though datetime64 variables are being read into the CDF file
pytest.importorskip("xarray")
var_data = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
var_dims = ["epoch", "direction"]
data = xr.Variable(var_dims, var_data)
epoch_data = [np.datetime64(1, "s"), np.datetime64(2, "s"), np.datetime64(3, "s")]
epoch_dims = ["epoch"]
epoch = xr.Variable(epoch_dims, epoch_data)
ds = xr.Dataset(data_vars={"data": data, "epoch": epoch})
xarray_to_cdf(ds, "hello.cdf", datetime64_to_cdftt2000=False)
x = cdf_to_xarray("hello.cdf", to_datetime=False)
assert x["epoch"][0] == 1000000000 # Seconds is converted to nanoseconds in the file, but otherwise left untouched
os.remove("hello.cdf")


def test_datetime64_conversion_odd_units():
# This tests out the datetime64_to_cdftt2000 conversion and then back again,
# verifying that everything writes correctly.
# This time, it uses days as the base unit, and verifies that it comes back out again as days.
pytest.importorskip("xarray")
Expand All @@ -398,7 +379,7 @@ def test_datetime64_conversion_odd_units():
epoch_dims = ["epoch"]
epoch = xr.Variable(epoch_dims, epoch_data)
ds = xr.Dataset(data_vars={"data": data, "epoch": epoch})
xarray_to_cdf(ds, "hello.cdf", datetime64_to_cdftt2000=True)
xarray_to_cdf(ds, "hello.cdf")
x = cdf_to_xarray("hello.cdf", to_datetime=True)
assert x["epoch"][1] == np.datetime64("2000-01-02")
os.remove("hello.cdf")
Expand All @@ -415,7 +396,7 @@ def test_numpy_string_array():
epoch_dims = ["epoch"]
epoch = xr.Variable(epoch_dims, epoch_data)
ds = xr.Dataset(data_vars={"data": data, "epoch": epoch})
xarray_to_cdf(ds, "hello.cdf", datetime64_to_cdftt2000=True)
xarray_to_cdf(ds, "hello.cdf")
x = cdf_to_xarray("hello.cdf", to_datetime=True)
assert x["data"][2] == "c"
os.remove("hello.cdf")
Loading