Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
enstools-compression
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
w2w
enstools-compression
Merge requests
!21
Update file compress_dataset_without_enstools_write.ipynb
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Update file compress_dataset_without_enstools_write.ipynb
dev-extend-documentation
into
main
Overview
0
Commits
3
Pipelines
3
Changes
1
Open
Oriol.Tinto
requested to merge
dev-extend-documentation
into
main
7 months ago
Overview
0
Commits
3
Pipelines
3
Changes
1
Expand
Update documentation following the suggestions from the article reviewers.
0
0
Merge request reports
Viewing commit
843b4018
Prev
Next
Show latest version
1 file
+
1
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
843b4018
Update file compress_dataset_without_enstools_write.ipynb
· 843b4018
Oriol.Tinto
authored
7 months ago
examples/notebooks/compress_dataset_without_enstools_write.ipynb
+
1
−
0
Options
%% 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
Besides enstools-encoding, this example relies on
**xarray**
,
**pooch**
and
**netCDF4**
in order to use xarray's tutorial data.
%% 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
from
pathlib
import
Path
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
=
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
()
```
Loading