Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a unit test to catch the errors with dimensions of size 1 being read in incorrectly by cdf_to_xarray #290

Open
bryan-harter opened this issue Jan 14, 2025 · 0 comments

Comments

@bryan-harter
Copy link
Member

See this PR here: #289

We accidentally were squeezing things, we want to make sure we don't do that.

Here was code I was testing with:

import os
import numpy as np
import xarray as xr

from cdflib.xarray import cdf_to_xarray, xarray_to_cdf

if os.path.exists("hello.cdf"):
    os.remove("hello.cdf")

sample_global_attributes = {
    "Project": "Hail Mary",
    "Source_name": "Thin Air",
    "Discipline": "None",
    "Data_type": "counts",
    "Descriptor": "Midichlorians in unicorn blood",
    "Data_version": "3.14",
    "Logical_file_id": "SEVENTEEN",
    "PI_name": "Darth Vader",
    "PI_affiliation": "Dark Side",
    "TEXT": "AHHHHH",
    "Instrument_type": "Banjo",
    "Mission_group": "Impossible",
    "Logical_source": ":)",
    "Logical_source_description": ":(",
}
sample_variable_attributes = {
    "CATDESC": "data",
    "DISPLAY_TYPE": "spectrogram",
    "FIELDNAM": "test",
    "FORMAT": "test",
    "UNITS": "test",
    "VALIDMIN": 0,
    "VALIDMAX": 10,
    "FILLVAL": np.int64(-9223372036854775808),
}
sample_data_variable_attributes = sample_variable_attributes | {"VAR_TYPE": "data", "LABLAXIS": "test"}
sample_support_variable_attributes = sample_variable_attributes | {"VAR_TYPE": "support_data"}

var_data = [[1, 2, 3]]
var_dims = ["epoch", "direction"]
data = xr.Variable(var_dims, var_data, sample_data_variable_attributes | {"DEPEND_0": "epoch", "DEPEND_1": "direction"})
support_data = [1, 2, 3]
support_dims = ["direction"]
support = xr.Variable(
    support_dims, support_data, sample_support_variable_attributes | {"DEPEND_1": "direction"}
)
epoch_data = [1]
epoch_dims = ["epoch"]
epoch = xr.Variable(epoch_dims, epoch_data, sample_support_variable_attributes)
dir_data = [1, 2, 3]
dir_dims = ["direction"]
direction = xr.Variable(dir_dims, dir_data, sample_support_variable_attributes)

ds = xr.Dataset(
    data_vars={"data": data, "epoch": epoch, "direction": direction, "support": support}, attrs=sample_global_attributes
)

xarray_to_cdf(ds, "hello.cdf", auto_fix_depends=False, terminate_on_warning=True)

x = cdf_to_xarray("hello.cdf")

print(x)

if os.path.exists("hello.cdf"):
    os.remove("hello.cdf")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Backlog
Development

No branches or pull requests

1 participant