Skip to content
Snippets Groups Projects
Commit 23b8b5b8 authored by Oriol.Tinto's avatar Oriol.Tinto
Browse files

Merge branch...

Merge branch '2-example-compress-a-dataset-without-using-enstools-io-write-is-outdated-and-needs-update' into 'main'

Resolve "Example "Compress a Dataset without using enstools.io.write" is outdated and needs update"

Closes #2

See merge request !14
parents 21d4038e 79fa7b3a
No related branches found
No related tags found
1 merge request!14Resolve "Example "Compress a Dataset without using enstools.io.write" is outdated and needs update"
Pipeline #18461 passed
%% Cell type:markdown id:2b61cda9-cd2d-4487-b656-93374fa406d6 tags:
# Compress a Dataset without using enstools.io.write
An example on how to use the analyzer to analyze a **dataset** and use the results to store it in a compressed file
%% Cell type:markdown id:bc1750af-27da-4ec1-a9a4-9b9646532a6f tags:
### Imports
%% Cell type:code id:d00551a0-599d-4362-b0dd-9861f4919c11 tags:
``` python
import xarray as xr
import sys
from pathlib import Path
from enstools.encoding.api import FilterEncodingForXarray
from enstools.encoding.api import DatasetEncoding
```
%% Cell type:markdown id:ce6fd1e9-8e01-425e-9f47-dc86d35b502e tags:
### Download some data
%% Cell type:code id:9aff74be-8f44-40f1-966b-6c23a33f748f tags:
``` python
dataset_name = "air_temperature"
dataset = xr.tutorial.open_dataset(dataset_name)
dataset
```
%% Cell type:markdown id:ed6e459d-7009-441a-887b-3f5b0acd583f tags:
### Define the compression specifications using the proper compression string format
%% Cell type:code id:d57c590d-1dae-4286-99ce-1a2592863626 tags:
``` python
compression='lossy,sz,abs,0.14'
```
%% Cell type:markdown id:8f6dd18c-1385-4108-8add-fad34ee13d90 tags:
### Use enstools.encoding to get the proper encodings for xarray.
%% Cell type:code id:c9979157-a86e-4daf-8aec-3be88eb33432 tags:
``` python
encoding = FilterEncodingForXarray(dataset=dataset, compression=compression)
encoding = DatasetEncoding(dataset=dataset, compression=compression)
```
%% Cell type:markdown id:1ec9dc55-5c3f-4ab2-a95f-a2db104ace21 tags:
### Use Xarray to store a compressed netCDF
%% Cell type:code id:a022c4ef-e697-42c1-a73e-86c3769635ba tags:
``` python
# Define a path for the temporary file
file_path = Path("tmp.nc")
```
%% Cell type:code id:0e8f6b3c-2af9-47ea-a644-1f85718d0972 tags:
``` python
# Save dataset to a netcdf file
# Please note that to use the HDF5 filters it is mandatory to use the h5netcdf engine.
dataset.to_netcdf(file_path, engine="h5netcdf",encoding=encoding)
```
%% Cell type:code id:35925c92-fbcb-4249-8f8b-b9dc53be9f2d tags:
``` python
# Remove temporary file
if file_path.exists():
file_path.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