Skip to content

Conversation

@jphastings
Copy link

@jphastings jphastings commented Sep 12, 2025

This change allows for CDF instances (eg. loaded from S3 buckets) to be converted to xarray Datasets, without being forced to retrieve the remote dataset, then load it from disk.

# Load the CDF from an S3 bucket or a URL in chunks
cdf = cdflib.CDF(some_cdf_url, s3_read_method=3)

# Use the loaded CDF when converting to an xarray
dataset = cdf_to_xarray(cdf)

fixes #312

I've implemented this as a breaking change to keep the function simple, but I'd be happy to rework that first argument as filename_path_or_cdf: str | Path | CDF and convert internally if you wanted to maintain API compatibility.

🚨 Breaking change 🚨

cdflib.xarray.cdf_to_xarray now requires a CDF object, not a filename.

- cdf_to_xarray("some.cdf")
+ cdf_to_xarray(CDF("some.cdf"))

This change allows for CDF files loaded from S3 buckets to be converted to xarray Datasets, without being forced to retrieve the remote dataset, then load it from disk.

cdf_to_xarray now requires a CDF object, not a filename. `cdf_to_xarray("some.cdf")` would now be `cdf_to_xarray(CDF("some.cdf"))`
@bryan-harter
Copy link
Member

Ah yeah, this is a great idea! But that said, I think we should keep the function backwards compatible for now, since cdf_to_xarray is used in quite a few places. Maybe we could just add a check at the beginning of the function like "if not isinstance(file, CDF) then file=CDF(file)"?

@jphastings
Copy link
Author

I’ll make the change and push shortly 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Convert in-memory cdflib.CDF into xarray?

2 participants