# Updating only header information for MetaImage/NRRD

**URL:** <https://discourse.slicer.org/t/updating-only-header-information-for-metaimage-nrrd/15115>\
**Category:** Development\
**Created:** [December 17, 2020, 3:06pm UTC](https://discourse.slicer.org/t/updating-only-header-information-for-metaimage-nrrd/15115 "2020-12-17T15:06:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [December 17, 2020, 3:06pm UTC](https://discourse.slicer.org/t/updating-only-header-information-for-metaimage-nrrd/15115/1 "2020-12-17T15:06:33Z")

</div>

I’m using MetaImage and NRRD file types with custom header fields to store additional information about my volume. I am currently interested in updating the values of these custom fields, but I have only had success with writing the updated header field which also rewrites the image data.

I’ve currently been using SimpleITK to update the fields as I was unable to find an option in Slicer to save the custom fields.

```python
import SimpleITK as sitk
image = sitk.ReadImage("path/to/image.mhd")
image.SetMetaData("MyCustomField", "MyCustomValue")
writer = sitk.ImageFileWriter()
writer.SetFileName("same/path/to/image.mhd")
writer.Execute(image) # also writes out the image raw file

```

I understand that likely only updating the header is possible when using split file formats like mhd+raw or nhdr+raw. Or is there a method with MHA and NRRD that can update the header fields and shift the bits so not to write the image data again?

Does anyone know of a method of only updating a field for the header file without the obvious opening of the file manually and writing it as simple text.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 17, 2020, 3:18pm UTC](https://discourse.slicer.org/t/updating-only-header-information-for-metaimage-nrrd/15115/2 "2020-12-17T15:18:37Z")

</div>

I don’t know any file IO API that would support shift operation. A common technique to work around this limitation is to write a placeholder field with the maximum allowed length (e.g., some value with lots of padding spaces) and overwrite that field when the final length is known. Maybe the segmentation storage file format discussion today at 11am EST would be interesting for you.

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [December 17, 2020, 3:21pm UTC](https://discourse.slicer.org/t/updating-only-header-information-for-metaimage-nrrd/15115/3 "2020-12-17T15:21:09Z")

</div>

Being that these images are captured using [PlusToolkit](https://plustoolkit.github.io/) I have also tried the [EditSequenceFile](http://perk-software.cs.queensu.ca/plus/doc/nightly/user/ApplicationEditSequenceFile.html) command line tool for updating the custom header field however it also writes the image data file (RAW) or at least I notice that the image data file has a new “Date Modified” time which makes me think it has been written down again.

```auto
EditSequenceFile.exe
--source-seq-file="C:\Users\JamesButler\Documents\FusedVolume.mhd"
--output-seq-file="C:\Users\JamesButler\Documents\FusedVolume.mhd"
--operation=UPDATE_FIELD_VALUE --field-name=MyCustomHeaderFieldn --updated-field-value=MyCustomValue
--maintain-custom-headers MyCustomHeaderField

```

Maybe all these methods SimpleITK/EditSequenceFile all depend on the same ITK writer which doesn’t currently support writing just the header file?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 17, 2020, 3:43pm UTC](https://discourse.slicer.org/t/updating-only-header-information-for-metaimage-nrrd/15115/4 "2020-12-17T15:43:45Z")

</div>

We had to reimplement sequence file writers to allow streamed compressed writing, so we don’t rely on what ITK writer can do (for mha for sure, but I think for nrrd as well). If it currently rewrites the RAW file then it should not be hard to fix it.

---

<div class="post-metadata">

**Author:** ![dzenanz](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dzenanz/32/1992_2.png) [@dzenanz](https://discourse.slicer.org/u/dzenanz)\
**Post date:** [December 17, 2020, 8:14pm UTC](https://discourse.slicer.org/t/updating-only-header-information-for-metaimage-nrrd/15115/5 "2020-12-17T20:14:57Z")

</div>

Those custom implementations are in [IGSIO](https://github.com/IGSIO/IGSIO/tree/73faef0140106e9abf7cb5fc9f0832c197a8c207/Source/SequenceIO) now.

---

<div class="post-metadata">

**Author:** ![dzenanz](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dzenanz/32/1992_2.png) [@dzenanz](https://discourse.slicer.org/u/dzenanz)\
**Post date:** [December 24, 2020, 4:42pm UTC](https://discourse.slicer.org/t/updating-only-header-information-for-metaimage-nrrd/15115/6 "2020-12-24T16:42:25Z")

</div>

Updates created:

> <https://github.com/IGSIO/IGSIO/pull/21>

> <https://github.com/PlusToolkit/PlusLib/pull/753>

and a related bug identified:

> <https://github.com/IGSIO/IGSIO/issues/20>
>
> Custom header fields need := separator, while standard fields use : . Current source ignores this distinction. Reading then writing file...
