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
Commits
9ec58d9c
Commit
9ec58d9c
authored
2 years ago
by
Oriol Tintó
Browse files
Options
Downloads
Patches
Plain Diff
Adding example with xarray accessors.
parent
1318e02e
No related branches found
No related tags found
1 merge request
!10
Prepare release 2023.4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/notebooks/accessors.ipynb
+196
-0
196 additions, 0 deletions
examples/notebooks/accessors.ipynb
with
196 additions
and
0 deletions
examples/notebooks/accessors.ipynb
0 → 100644
+
196
−
0
View file @
9ec58d9c
{
"cells": [
{
"cell_type": "markdown",
"id": "2b61cda9-cd2d-4487-b656-93374fa406d6",
"metadata": {
"pycharm": {
"name": "#%% md\n"
},
"tags": []
},
"source": [
"# Using the enstools-compression accessors for Xarray\n",
"\n",
"An example on how to use the different accessors available for Xarray."
]
},
{
"cell_type": "markdown",
"id": "bc1750af-27da-4ec1-a9a4-9b9646532a6f",
"metadata": {
"pycharm": {
"name": "#%% md\n"
},
"tags": []
},
"source": [
"## Get some dummy data"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "d00551a0-599d-4362-b0dd-9861f4919c11",
"metadata": {
"pycharm": {
"is_executing": true,
"name": "#%%\n"
},
"tags": []
},
"outputs": [],
"source": [
"import xarray\n",
"from pathlib import Path"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "9aff74be-8f44-40f1-966b-6c23a33f748f",
"metadata": {
"pycharm": {
"name": "#%%\n"
},
"tags": []
},
"outputs": [],
"source": [
"dataset_name = \"air_temperature\"\n",
"dataset = xarray.tutorial.open_dataset(dataset_name)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "a022c4ef-e697-42c1-a73e-86c3769635ba",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"# Define a path for the temporary file\n",
"output_file = Path(\"compressed.nc\")"
]
},
{
"cell_type": "markdown",
"id": "a1684f6e",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"## Import the accessors"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "2f5f6571",
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
},
"pycharm": {
"is_executing": true,
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"from enstools.compression import xr_accessor"
]
},
{
"cell_type": "markdown",
"id": "26f148e6-ad70-45f3-85e9-c4403ffd7a6a",
"metadata": {},
"source": [
"# Use the accessors\n",
"## Method to_compressed_netcdf"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "0906b0d0-90b7-4d77-969b-6ca30ced0bd7",
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'Dataset' object has no attribute 'to_compressed_netcdf'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[10], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mdataset\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mto_compressed_netcdf\u001b[49m(output_file, compression\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mlossy,sz,pw_rel,1.e-5\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
"File \u001b[0;32m/scratch/o/Oriol.Tinto/compression-kernel/venv/lib/python3.10/site-packages/xarray/core/common.py:278\u001b[0m, in \u001b[0;36mAttrAccessMixin.__getattr__\u001b[0;34m(self, name)\u001b[0m\n\u001b[1;32m 276\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m suppress(\u001b[38;5;167;01mKeyError\u001b[39;00m):\n\u001b[1;32m 277\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m source[name]\n\u001b[0;32m--> 278\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mAttributeError\u001b[39;00m(\n\u001b[1;32m 279\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mtype\u001b[39m(\u001b[38;5;28mself\u001b[39m)\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;132;01m!r}\u001b[39;00m\u001b[38;5;124m object has no attribute \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mname\u001b[38;5;132;01m!r}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 280\u001b[0m )\n",
"\u001b[0;31mAttributeError\u001b[0m: 'Dataset' object has no attribute 'to_compressed_netcdf'"
]
}
],
"source": [
"dataset.to_compressed_netcdf(output_file, compression=\"lossy,sz,pw_rel,1.e-5\")"
]
},
{
"cell_type": "markdown",
"id": "204ae92f",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"## Remove Temporary files"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "462f511c",
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
},
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"# Remove temporary files\n",
"if output_file.exists():\n",
" output_file.unlink()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "enstools-compression-2022.12.1",
"language": "python",
"name": "enstools-compression-2022.12.1"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
%% Cell type:markdown id:2b61cda9-cd2d-4487-b656-93374fa406d6 tags:
# Using the enstools-compression accessors for Xarray
An example on how to use the different accessors available for Xarray.
%% Cell type:markdown id:bc1750af-27da-4ec1-a9a4-9b9646532a6f tags:
## Get some dummy data
%% Cell type:code id:d00551a0-599d-4362-b0dd-9861f4919c11 tags:
```
python
import
xarray
from
pathlib
import
Path
```
%% Cell type:code id:9aff74be-8f44-40f1-966b-6c23a33f748f tags:
```
python
dataset_name
=
"
air_temperature
"
dataset
=
xarray
.
tutorial
.
open_dataset
(
dataset_name
)
```
%% Cell type:code id:a022c4ef-e697-42c1-a73e-86c3769635ba tags:
```
python
# Define a path for the temporary file
output_file
=
Path
(
"
compressed.nc
"
)
```
%% Cell type:markdown id:a1684f6e tags:
## Import the accessors
%% Cell type:code id:2f5f6571 tags:
```
python
from
enstools.compression
import
xr_accessor
```
%% Cell type:markdown id:26f148e6-ad70-45f3-85e9-c4403ffd7a6a tags:
# Use the accessors
## Method to_compressed_netcdf
%% Cell type:code id:0906b0d0-90b7-4d77-969b-6ca30ced0bd7 tags:
```
python
dataset
.
to_compressed_netcdf
(
output_file
,
compression
=
"
lossy,sz,pw_rel,1.e-5
"
)
```
%% Output
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[10], line 1
----> 1 dataset.to_compressed_netcdf(output_file, compression="lossy,sz,pw_rel,1.e-5")
File /scratch/o/Oriol.Tinto/compression-kernel/venv/lib/python3.10/site-packages/xarray/core/common.py:278, in AttrAccessMixin.__getattr__(self, name)
276 with suppress(KeyError):
277 return source[name]
--> 278 raise AttributeError(
279 f"{type(self).__name__!r} object has no attribute {name!r}"
280 )
AttributeError: 'Dataset' object has no attribute 'to_compressed_netcdf'
%% Cell type:markdown id:204ae92f tags:
## Remove Temporary files
%% Cell type:code id:462f511c tags:
```
python
# Remove temporary files
if
output_file
.
exists
():
output_file
.
unlink
()
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment