# Writing into SEG2/H5

**URL:** <https://discourse.obspy.org/t/writing-into-seg2-h5/1458>\
**Category:** Usage Questions\
**Created:** [July 29, 2022, 3:51pm UTC](https://discourse.obspy.org/t/writing-into-seg2-h5/1458 "2022-07-29T15:51:04Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![P\_ALD](https://discourse.obspy.org/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@P\_ALD](https://discourse.obspy.org/u/P_ALD)\
**Post date:** [July 29, 2022, 3:51pm UTC](https://discourse.obspy.org/t/writing-into-seg2-h5/1458/1 "2022-07-29T15:51:04Z")

</div>

Hi everyone,

Currently, I am working on a dataset with SEG2 format that has important headers within each waveform’s stats. Obspy reads them and I can correct the faulty traces but , as it is said in the documentation, I cannot write the filtered data into the SEG2 format.

I tried saving in different formats but I am missing important headers like: Channel number, source location, receiver location, …! Any Idea how I can get around this issue?

Another option for me is writing as H5 files but again, Obspy is not supporting it. I would appreciate it if somebody can guide me

Best,

---

<div class="post-metadata">

**Author:** ![trichter](https://discourse.obspy.org/user_avatar/discourse.obspy.org/trichter/32/226_2.png) [@trichter](https://discourse.obspy.org/u/trichter)\
**Post date:** [August 2, 2022, 2:41pm UTC](https://discourse.obspy.org/t/writing-into-seg2-h5/1458/2 "2022-08-02T14:41:48Z")

</div>

Hi,

to guarantee persistence of waveform metadata I personally use obspyh5:

> **[GitHub - trichter/obspyh5: HDF5 write/read support for ObsPy](https://github.com/trichter/obspyh5)**
>
> HDF5 write/read support for ObsPy

For a more standardized writing to HDF5 format, see the asdf file format. I did not use it and I do not know if it supports arbitrary metadata (stats attributes) as obspyh5.

> **[Seismic Data](http://seismic-data.org/)**

---

<div class="post-metadata">

**Author:** ![P\_ALD](https://discourse.obspy.org/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@P\_ALD](https://discourse.obspy.org/u/P_ALD)\
**Post date:** [August 2, 2022, 3:53pm UTC](https://discourse.obspy.org/t/writing-into-seg2-h5/1458/3 "2022-08-02T15:53:08Z")

</div>

Thank you for your response. Actually, I have tried it before, but I get the following error:

 ![error](https://discourse.obspy.org/uploads/default/original/1X/0563fd1520d78cb7df205c0e75940e985c8d9257.png)

1. no matter what the name is! I get the error " name already exists". However, it creates the H5 file in the directory.

2. The created file is still not readable by obspy. Am I missing something?

---

<div class="post-metadata">

**Author:** ![trichter](https://discourse.obspy.org/user_avatar/discourse.obspy.org/trichter/32/226_2.png) [@trichter](https://discourse.obspy.org/u/trichter)\
**Post date:** [August 2, 2022, 6:52pm UTC](https://discourse.obspy.org/t/writing-into-seg2-h5/1458/4 "2022-08-02T18:52:07Z")

</div>

Interesting. Can you post the full error traceback (preferable as text inside triple back ticks ```)? Can you also post the output of `print(st)`?

The file cannot be read, because it is corrupted due to the error upon writing.

---

<div class="post-metadata">

**Author:** ![P\_ALD](https://discourse.obspy.org/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@P\_ALD](https://discourse.obspy.org/u/P_ALD)\
**Post date:** [August 2, 2022, 7:19pm UTC](https://discourse.obspy.org/t/writing-into-seg2-h5/1458/5 "2022-08-02T19:19:25Z")

</div>

This is the print(st) result:

 ![image](https://discourse.obspy.org/uploads/default/original/1X/1ffbf0e378e808d4b9968b1d64ed8ca487b3d954.png)

---

<div class="post-metadata">

**Author:** ![P\_ALD](https://discourse.obspy.org/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@P\_ALD](https://discourse.obspy.org/u/P_ALD)\
**Post date:** [August 2, 2022, 7:20pm UTC](https://discourse.obspy.org/t/writing-into-seg2-h5/1458/6 "2022-08-02T19:20:00Z")

</div>

Here is the full error:

 ![1](https://discourse.obspy.org/uploads/default/original/1X/5c6964616fc31c50e1a21d56486158633563e352.png)

---

<div class="post-metadata">

**Author:** ![P\_ALD](https://discourse.obspy.org/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@P\_ALD](https://discourse.obspy.org/u/P_ALD)\
**Post date:** [August 2, 2022, 7:20pm UTC](https://discourse.obspy.org/t/writing-into-seg2-h5/1458/7 "2022-08-02T19:20:33Z")

</div>

![2](https://discourse.obspy.org/uploads/default/original/1X/eb00c20b07e491033b7a0542b2e03d0302d1dd7c.png)

---

<div class="post-metadata">

**Author:** ![trichter](https://discourse.obspy.org/user_avatar/discourse.obspy.org/trichter/32/226_2.png) [@trichter](https://discourse.obspy.org/u/trichter)\
**Post date:** [August 3, 2022, 7:16am UTC](https://discourse.obspy.org/t/writing-into-seg2-h5/1458/8 "2022-08-03T07:16:47Z")

</div>

Hi,

thanks.

As a fast work-around, call

```python
import obspyh5
obspyh5.set_index('waveforms/{trc_num:03d}')

```

before writing the files.

Back-ground: HDF5 uses indices for writing data. The index is created by using seed id, starttime and endtime. In your case the empty seed id appears to trigger a bug.

I’ll try to push a fix to obspyh5 soonish.

---

<div class="post-metadata">

**Author:** ![P\_ALD](https://discourse.obspy.org/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@P\_ALD](https://discourse.obspy.org/u/P_ALD)\
**Post date:** [August 3, 2022, 2:13pm UTC](https://discourse.obspy.org/t/writing-into-seg2-h5/1458/9 "2022-08-03T14:13:41Z")

</div>

Thank you, Tom, for your time.

It works right now. However, I noticed that the stats of the SEG2 file are not preserved. Is there anyway I can import those attributes to the created H5?

 ![image](https://discourse.obspy.org/uploads/default/original/1X/45ee9d929f0c1a0440f2d2c19843ce43b7933663.png)

---

<div class="post-metadata">

**Author:** ![P\_ALD](https://discourse.obspy.org/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@P\_ALD](https://discourse.obspy.org/u/P_ALD)\
**Post date:** [August 3, 2022, 2:14pm UTC](https://discourse.obspy.org/t/writing-into-seg2-h5/1458/10 "2022-08-03T14:14:18Z")

</div>

Here is what I get:

 ![image](https://discourse.obspy.org/uploads/default/original/1X/d381faacbfd256a316c157bc2a6ff2bf56c9554c.png)

---

<div class="post-metadata">

**Author:** ![trichter](https://discourse.obspy.org/user_avatar/discourse.obspy.org/trichter/32/226_2.png) [@trichter](https://discourse.obspy.org/u/trichter)\
**Post date:** [August 3, 2022, 2:48pm UTC](https://discourse.obspy.org/t/writing-into-seg2-h5/1458/11 "2022-08-03T14:48:19Z")

</div>

Yes, the format specific header is ignored by default. One way to write seg2 header anyway is to trick obspyh5 with

```python
for tr in stream:
    del tr.stats._format

```

In this way, obspyh5 does not know anymore that seg2 is the format specific header.  
Alternatively, you can simply copy the header to another entry:

```python
for tr in stream:
    tr.stats.seg = tr.stats.seg2

```

By the way, writing traces with empty SEED id is working with the freshly published obspyh5 version 0.6.0.

---

<div class="post-metadata">

**Author:** ![P\_ALD](https://discourse.obspy.org/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@P\_ALD](https://discourse.obspy.org/u/P_ALD)\
**Post date:** [August 3, 2022, 5:36pm UTC](https://discourse.obspy.org/t/writing-into-seg2-h5/1458/12 "2022-08-03T17:36:19Z")

</div>

Thanks, Tom, I really appreciate your support.

---

<div class="post-metadata">

**Author:** ![SorawatSiangpipop](https://discourse.obspy.org/user_avatar/discourse.obspy.org/sorawatsiangpipop/32/583_2.png) [@SorawatSiangpipop](https://discourse.obspy.org/u/SorawatSiangpipop)\
**Post date:** [October 4, 2022, 6:54am UTC](https://discourse.obspy.org/t/writing-into-seg2-h5/1458/13 "2022-10-04T06:54:53Z")

</div>

Hi, I would like to ask what I have done wrong in this case. I tried and I’ve got  
import obspyh5  
obspyh5.set\_index(‘waveforms/{trc\_num:03d}’)  
 ![image](https://discourse.obspy.org/uploads/default/original/1X/9cffee73524aae4ec4e75abf458802cf8b8b08f4.png)

---

<div class="post-metadata">

**Author:** ![SorawatSiangpipop](https://discourse.obspy.org/user_avatar/discourse.obspy.org/sorawatsiangpipop/32/583_2.png) [@SorawatSiangpipop](https://discourse.obspy.org/u/SorawatSiangpipop)\
**Post date:** [October 4, 2022, 7:01am UTC](https://discourse.obspy.org/t/writing-into-seg2-h5/1458/14 "2022-10-04T07:01:44Z")

</div>

I’m working .mseed files

---

<div class="post-metadata">

**Author:** ![megies](https://discourse.obspy.org/user_avatar/discourse.obspy.org/megies/32/9_2.png) [@megies](https://discourse.obspy.org/u/megies)\
**Post date:** [October 4, 2022, 7:47am UTC](https://discourse.obspy.org/t/writing-into-seg2-h5/1458/15 "2022-10-04T07:47:40Z")

</div>

don’t know obpsyh5 so can’t say much, but either you or that other library is trying to write a stream in `"H5"` (or is that `HS` in screenshot?) format, which is not a format that obspy ships with natively, so either that write attempt is a mistake or `"H5"` write support should have been installed by a third party library through our plugin system but it wasn’t.

---

<div class="post-metadata">

**Author:** ![trichter](https://discourse.obspy.org/user_avatar/discourse.obspy.org/trichter/32/226_2.png) [@trichter](https://discourse.obspy.org/u/trichter)\
**Post date:** [October 4, 2022, 8:23am UTC](https://discourse.obspy.org/t/writing-into-seg2-h5/1458/16 "2022-10-04T08:23:28Z")

</div>

Yes, obspyh5 registers `"H5"` read/write as Obspy plugin. Did you install obspyh5 properly, i.e. with `pip install obspyh5`?
