---
description: Example Python script to load an HDF5 file.
---

# Python Scripts

### Loading Data into Python Lists.

```python
import h5py
import numpy as np
import statistics

h5_filenames = []
h5_filenames.append( "file.h5" )

data_ts = []
data_lat = []
data_lon = []
data_pol = []
data_unpol = []

for h5_filename in h5_filenames:
    h5_file = h5py.File(h5_filename, "r+")
    for key in list(h5_file.keys()):
        grps = h5_file[key]
        for grp in list(grps):
            if grp == "Rawdata":
                ts = h5_file[key + "/Rawdata"]
                for t in ts:
                    d = key + "/Rawdata/" + t
                    data = h5_file[d]

                    t = float(data.attrs["lna_temperature_degC"])
                    t_kelvin = t + 273.15

                    data_hh = t_kelvin*statistics.median( np.array(data[2]) / np.array(data[0]) )
                    data_vv = t_kelvin*statistics.median( np.array(data[3]) / np.array(data[1]) )
                    data_u = t_kelvin*statistics.median( np.array(data[4]) / (np.sqrt(np.array(data[0])) * np.sqrt(np.array(data[1]))) )
                    data_v = t_kelvin*statistics.median( np.array(data[5]) / (np.sqrt(np.array(data[0])) * np.sqrt(np.array(data[1]))) )

                    temp_tot = (0.5 * (data_hh + data_vv))
                    temp_q = 1.0 * (0.5 * (data_hh - data_vv))
                    temp_pol = np.sqrt(data_u * data_u + temp_q * temp_q + data_v * data_v)
                    temp_unpol = temp_tot - temp_pol

                    if temp_unpol<300:
                        data_pol.append(temp_pol)
                        data_unpol.append(temp_unpol)
                        data_ts.append(float(data.attrs["timestamp_utc"]))
                        data_lat.append(float(data.attrs["position"][0]))
                        data_lon.append(float(data.attrs["position"][1]))
    h5_file.close()
```

### Convert HDF5 to CSV&#x20;

```python
import csv
import pandas as pd

csv_filename = "file.csv"

headerList = ["ts", "lat", "lon", "pol", "unpol"]
with open(csv_filename, 'w') as file:
    dw = csv.DictWriter(file, delimiter=',',  fieldnames=headerList)
    dw.writeheader()
    for i in range(len(data_ts)):
        dw.writerow(dict(zip(headerList, [data_ts[i], data_lat[i], data_lon[i], data_pol[i], data_unpol[i]])))
```

### Plot Data using Plotly

```python
import pandas as pd
import plotly.express as px

# Remember: plotly express will need to open a browser tab to display the map!

# Here insert your own mapbox token please! A "pay-as-you-go" account gives plenty of free accesses.
# Instructions on how to obtain one are here:
# https://docs.mapbox.com/api/accounts/tokens/
mapbox_token = "your_token_here"

csv_filename = "file.csv"

df = pd.read_csv(csv_filename)
df.head()
px.set_mapbox_access_token(mapbox_token)

cs_min, cs_max = np.percentile(data_unpol, [5, 95])

fig = px.scatter_mapbox(df, lon="lon", lat="lat", color="unpol", size_max=10,
                           color_continuous_scale="Jet",
                           range_color=(cs_min, cs_max),
                           mapbox_style="satellite", zoom=17,
                           title="T_B (K)",
                           )
fig.update_layout(width=950, height=900)
fig.show()
```

