Skip to content
Snippets Groups Projects
Commit 9ec58d9c authored by Oriol Tintó's avatar Oriol Tintó
Browse files

Adding example with xarray accessors.

parent 1318e02e
No related branches found
No related tags found
1 merge request!10Prepare release 2023.4
%% Cell type:markdown id:2b61cda9-cd2d-4487-b656-93374fa406d6 tags:
# Using the enstools-compression accessors for Xarray
An example on how to use the different accessors available for Xarray.
%% Cell type:markdown id:bc1750af-27da-4ec1-a9a4-9b9646532a6f tags:
## Get some dummy data
%% Cell type:code id:d00551a0-599d-4362-b0dd-9861f4919c11 tags:
``` python
import xarray
from pathlib import Path
```
%% Cell type:code id:9aff74be-8f44-40f1-966b-6c23a33f748f tags:
``` python
dataset_name = "air_temperature"
dataset = xarray.tutorial.open_dataset(dataset_name)
```
%% Cell type:code id:a022c4ef-e697-42c1-a73e-86c3769635ba tags:
``` python
# Define a path for the temporary file
output_file = Path("compressed.nc")
```
%% Cell type:markdown id:a1684f6e tags:
## Import the accessors
%% Cell type:code id:2f5f6571 tags:
``` python
from enstools.compression import xr_accessor
```
%% Cell type:markdown id:26f148e6-ad70-45f3-85e9-c4403ffd7a6a tags:
# Use the accessors
## Method to_compressed_netcdf
%% Cell type:code id:0906b0d0-90b7-4d77-969b-6ca30ced0bd7 tags:
``` python
dataset.to_compressed_netcdf(output_file, compression="lossy,sz,pw_rel,1.e-5")
```
%% Output
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[10], line 1
----> 1 dataset.to_compressed_netcdf(output_file, compression="lossy,sz,pw_rel,1.e-5")
File /scratch/o/Oriol.Tinto/compression-kernel/venv/lib/python3.10/site-packages/xarray/core/common.py:278, in AttrAccessMixin.__getattr__(self, name)
276 with suppress(KeyError):
277 return source[name]
--> 278 raise AttributeError(
279 f"{type(self).__name__!r} object has no attribute {name!r}"
280 )
AttributeError: 'Dataset' object has no attribute 'to_compressed_netcdf'
%% Cell type:markdown id:204ae92f tags:
## Remove Temporary files
%% Cell type:code id:462f511c tags:
``` python
# Remove temporary files
if output_file.exists():
output_file.unlink()
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment