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

read and write sparse matrix with intersection weights using xarray #126

Open
cchwala opened this issue Jul 5, 2023 · 3 comments
Open

Comments

@cchwala
Copy link
Contributor

cchwala commented Jul 5, 2023

The calculation of the sparse intersection weights via calc_intersect_weights is fairly slow (approx. 4 minutes for 4000 CMLs and the 900x900 RADOLAN grid) and the resulting weight matrix would be large in dense format. Hence, it is often required to write the sparse intersection weights to disk and read them back in later to reuse them. Writing sparse data to a file e.g. in COO format is possible, but we want to keep the linkage to the indices that we have in the xarray.DataArray of the sparse weights matrix.

What we need is a function to read and write a xarray.DataArray, containing a sparse matrix, to NetCDF or zarr.

@cchwala
Copy link
Contributor Author

cchwala commented Jul 5, 2023

There is a solution that could actually cover our use case in pangeo-data/xESMF#222 (comment)

@cchwala
Copy link
Contributor Author

cchwala commented Jul 5, 2023

At the end of this notebook Sparse arrays and the CESM land model component I learned that there is also a CF convention for storing sparse data via Compression by Gathering. Not sure if this helps us. But there is some implementation, at least for MultiIndex for this in cf-xarray according to their docs using cfxr.encode_multi_index_as_compress and cfxr.decode_compress_to_multi_index.

@cchwala
Copy link
Contributor Author

cchwala commented Jul 10, 2023

A simple solution is to do something like

sparse.save_npz('sparse_intersect_weights.npz', sparse_intersect_weights)

and store the coordinates of the xarray.DataArray separately

Then one can load like this

 def load_sparse_intersect_weights():
    ds_intersect_weights = xr.open_dataset('intersect_weights_coords.nc')
    da_intersect_weights = xr.DataArray(
        data = sparse.load_npz('sparse_intersect_weights.npz'),
        coords = ds_intersect_weights.coords,
    )
        
    return da_intersect_weights

da_intersect_weights = load_sparse_intersect_weights()
da_intersect_weights

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

No branches or pull requests

1 participant