---
jupytext:
  formats: md:myst
  text_representation:
    extension: .md
    format_name: myst
kernelspec:
  name: python3
  display_name: Python 3
---

```{include} ../../../_includes/license_block.md
```
# xarray GAMIC backend

In this example, we read GAMIC (HDF5) data files using the xradar `gamic` backend.

```{code-cell} python
import glob
import wradlib as wrl
import wradlib_data
import warnings
from IPython.display import display

import matplotlib.pyplot as plt
import numpy as np
import xradar as xd
import xarray as xr

warnings.filterwarnings("ignore")

```

## Load GAMIC HDF5 Volume Data

```{code-cell} python
fpath = "hdf5/DWD-Vol-2_99999_20180601054047_00.h5"
f = wradlib_data.DATASETS.fetch(fpath)
vol = xd.io.open_gamic_datatree(f)
```

## Inspect RadarVolume

```{code-cell} python
display(vol)
```

## Inspect root group

The `sweep` dimension contains the number of scans in this radar volume. Further the dataset consists of variables (location coordinates, time_coverage) and attributes (Conventions, metadata).

```{code-cell} python
vol.root
```

## Inspect sweep group(s)

The sweep-groups can be accessed via their respective keys. The dimensions consist of `range` and `time` with added coordinates `azimuth`, `elevation`, `range` and `time`. There will be variables like radar moments (DBZH etc.) and sweep-dependent metadata (like `fixed_angle`, `sweep_mode` etc.).

```{code-cell} python
display(vol["sweep_0"])
```

## Georeferencing

```{code-cell} python
swp = vol["sweep_0"].to_dataset(inherit="all_coords")
swp = swp.assign_coords(sweep_mode=swp.sweep_mode)
```

```{code-cell} python
swp = swp.wrl.georef.georeference()
display(swp)
```

## Inspect radar moments

The DataArrays can be accessed by key or by attribute. Each DataArray has dimensions and coordinates of its parent dataset. There are attributes connected which are defined by ODIM_H5 standard.

```{code-cell} python
display(swp.DBZH)
```

## Create simple plot

Using xarray features a simple plot can be created like this. Note the `sortby('time')` method, which sorts the radials by time.

For more details on plotting radar data see under [Visualization](../../visualisation/plotting).

```{code-cell} python
swp.DBZH.sortby("time").plot(x="range", y="time", add_labels=False)
```

```{code-cell} python
fig = plt.figure(figsize=(10, 10))
pm = swp.DBZH.wrl.vis.plot(crs={"latmin": 3e3}, fig=fig)
```

## Retrieve explicit group


```{code-cell} python
swp_b = xr.open_dataset(
    f, engine="gamic", group="sweep_9", backend_kwargs=dict(reindex_angle=False)
)
display(swp_b)
```
