Compress a file using the Command Line Interface
An example on how to compress a netcdf file using enstools-compression’s CLI.
Prepare something to compress
[1]:
import xarray
from pathlib import Path
from enstools.io import write, read
WARNING: eccodes c-library not found, grib file support not available!
[2]:
dataset_name = "air_temperature"
dataset = xarray.tutorial.open_dataset(dataset_name)
[3]:
# Define a path for the temporary file
input_file = Path("input.nc")
output_file = Path("compressed.nc")
[4]:
# Save dataset to a netcdf file
write(dataset, input_file)
/home/docs/checkouts/readthedocs.org/user_builds/enstools-compression/envs/latest/lib/python3.8/site-packages/enstools/io/writer.py:146: SerializationWarning: saving variable air with floating point data as an integer dtype without any _FillValue to use for NaNs
task = ds.to_netcdf(file_path, engine=engine, encoding=dataset_encoding, compute=compute, format=format)
Compress the file
[5]:
! enstools-compression compress "input.nc" -o "compressed.nc" --compression 'lossy,sz,abs,0.14'
WARNING: eccodes c-library not found, grib file support not available!
Open compressed file and check that the content can be read
[6]:
with read("compressed.nc") as ds:
print(ds.values)
<bound method Mapping.values of <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 dask.array<chunksize=(2920, 25, 53), meta=np.ndarray>
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...>
Remove Temporary files
[7]:
# Remove files
if input_file.exists():
input_file.unlink()
if output_file.exists():
output_file.unlink()