Analyze and compress a Dataset
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
import sys
from pathlib import Path
from enstools.compression.analyzer.analyzer import analyze_dataset
WARNING: eccodes c-library not found, grib file support not available!
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...Analyze dataset using default constrains
[3]:
encoding, metrics = analyze_dataset(dataset)
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
Analyze dataset using custom constrains
[4]:
encoding, metrics = analyze_dataset(dataset,
constrains="correlation_I:3,ssim_I:1",)
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
WARNING: Only absolute and norm2 modes properly tested
[5]:
encoding
[5]:
{'air': 'lossy,sz3,abs,1.67'}
Save the file using the encoding found in the analysis.
[6]:
from enstools.io import write
[7]:
# Define a path for the temporary file
file_path = Path("tmp.nc")
[8]:
write(dataset, file_path, compression=encoding)
[9]:
if file_path.exists():
file_path.unlink()