# Access Ping360 data for post processing, Python

**URL:** <https://discuss.bluerobotics.com/t/access-ping360-data-for-post-processing-python/10416>\
**Category:** Ping Sonar Devices\
**Tags:** sonar, ping360\
**Created:** [August 12, 2021, 1:31pm UTC](https://discuss.bluerobotics.com/t/access-ping360-data-for-post-processing-python/10416 "2021-08-12T13:31:06Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![EliotBR](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.bluerobotics.com/eliotbr/32/6937_2.png) [@EliotBR](https://discuss.bluerobotics.com/u/EliotBR)\
**Post date:** [August 13, 2021, 2:27am UTC](https://discuss.bluerobotics.com/t/access-ping360-data-for-post-processing-python/10416/2 "2021-08-13T02:27:28Z")

</div>

Hi @Winter, welcome to the forum 🙂

> [@Winter](#):
>
> I am looking into the decode\_pingviewer\_sensor\_log\_file.py script found here: [Needs Python 3.7+ · GitHub](https://gist.github.com/patrickelectric/c49de5b5cdecdadb88ae875e240dc044), however I cant seem to figure out how to access the sonar data by itself or what format this is in.

I’d recommend you instead use/refer to the [`decode_sensor_binary_log.py`](https://github.com/bluerobotics/ping-viewer/blob/master/examples/decode_sensor_binary_log.py) file from our Ping Viewer repository, since it actually gets updated. I believe the one you linked to was originally a one-off example - not sure where you found that link, but if it’s on our forums or in our documentation then please let me know so I can update it 🙂

The `.bin` files produced by Ping Viewer are described in the [Ping Viewer documentation](https://docs.bluerobotics.com/ping-viewer/logging-and-user-files/#sensor-logs), as:

> [@Ping Viewer - Sensor Logs](#):
>
> The sensor log files (.bin) contain all of the raw binary communications received from the device. These binary communications are in [Ping Protocol](https://github.com/bluerobotics/ping-protocol) format. The logs can be [replayed](https://docs.bluerobotics.com/ping-viewer/replay-data/) by Ping Viewer application. To know more about the binary structure, please check [binary log file structure](https://docs.bluerobotics.com/ping-viewer/binary-structure/).

The relevant data message specification for the ping360 is [`device_data`](https://docs.bluerobotics.com/ping-protocol/pingmessage-ping360/#2300-device-data), in the Ping Protocol docs.

The `decode_sensor_binary_log.py` script parses a binary log file into [`PingMessage`](https://github.com/bluerobotics/ping-python/blob/master/brping/pingmessage.py#L13) instances (using/from the [`bluerobotics-ping`](https://github.com/bluerobotics/ping-python) library), and filters them into only the messages containing scan data. For the Ping360 that’s the `device_data` message, which has a `data` attribute that is a `bytearray` of the intensity values returned by the sonar. You can index into that directly if you want to (e.g. `decoded_message.data[0]` for the first intensity value), but for processing purposes I’d recommend converting the data to a numpy array instead (e.g. `np.frombuffer(decoded_message.data, np.uint8)`).

You can look at the message specification for the other available attributes, but of particular note are the `angle` (for direction) and `sample_period` (used to determine distance).

A few gotchas:

1. the ping angles are measured in gradians, so the `angle` attribute is a value from 0 to 399
2. the ping360 zero angle is at the back where the penetrator is, so ‘forwards’ (the direction of the arrow on the bottom of the Ping360) is at angle 200 gradians
3. Sonar (and all time of flight sensors) measure the time it takes for the pulse to _return_, so while d\_{sound} = v\_{sound} \* t\_{sample}, the target is half of that: d\_{target} = \frac{d\_{sound}}{2}

The following function can be used to determine the target distance per sample:

```python
def meters_per_sample(ping_message, v_sound=1500):
    """ Returns the target distance per sample, in meters. 
    
    'ping_message' is the message being analysed.
    'v_sound' is the operating speed of sound [m/s]. Default 1500.

    """
    # sample_period is in 25ns increments
    # time of flight includes there and back, so divide by 2
    return v_sound * ping_message.sample_period * 12.5e-9

```

If you have the sample index (from within the `data` array) the distance at the start of the sample time is just

```python
sample_start_distance = sample_index * meters_per_sample(ping_message, v_sound)

```

(i.e. the first sample (index 0) has distance 0m).  
Alternatively, you can get the distance to the middle of the sample time with

```python
sample_average_distance = (sample_index + 0.5) * meters_per_sample(ping_message, v_sound)

```

We’re currently looking to add some more thorough examples of using our ping libraries - is there anything in particular you’d be interested in? 🙂

---

_[View the full topic](https://discuss.bluerobotics.com/t/access-ping360-data-for-post-processing-python/10416)._
