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
Imports
[1]:
import xarray as xr
from pathlib import Path
from enstools.encoding.api import DatasetEncoding
Download some data
[2]:
dataset_name = "air_temperature"
dataset = xr.tutorial.open_dataset(dataset_name)
dataset
[2]:
<xarray.Dataset>
Dimensions: (lat: 25, time: 2920, lon: 53)
Coordinates:
* lat (lat) float32 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0
* lon (lon) float32 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0
* time (time) datetime64[ns] 2013-01-01 ... 2014-12-31T18:00:00
Data variables:
air (time, lat, lon) float32 ...
Attributes:
Conventions: COARDS
title: 4x daily NMC reanalysis (1948)
description: Data is from NMC initialized reanalysis\n(4x/day). These a...
platform: Model
references: http://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reanaly...Define the compression specifications using the proper compression string format
[3]:
compression='lossy,sz,abs,0.14'
Use enstools.encoding to get the proper encodings for xarray.
[4]:
encoding = DatasetEncoding(dataset=dataset, compression=compression)
Use Xarray to store a compressed netCDF
[5]:
# Define a path for the temporary file
file_path = Path("tmp.nc")
[6]:
# 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)
[7]:
# Remove temporary file
if file_path.exists():
file_path.unlink()