# Issue with MED to XDMF Conversion in FESTIM Workshop 8

**URL:** <https://festim.discourse.group/t/issue-with-med-to-xdmf-conversion-in-festim-workshop-8/125>\
**Category:** User Support\
**Created:** [July 9, 2025, 10:33am UTC](https://festim.discourse.group/t/issue-with-med-to-xdmf-conversion-in-festim-workshop-8/125 "2025-07-09T10:33:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrea](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@Andrea](https://festim.discourse.group/u/Andrea)\
**Post date:** [July 9, 2025, 10:33am UTC](https://festim.discourse.group/t/issue-with-med-to-xdmf-conversion-in-festim-workshop-8/125/1 "2025-07-09T10:33:47Z")

</div>

Hello,

I’m learing how to use FESTIM and currently working on Workshop 8. I downloaded the `mesh.med` file from GitHub, but when I run the script, it doesn’t work.  
Here’s the code I’m using:

```
Import meshio

def convert_med_to_xdmf(
    med_file,
    cell_file="mesh_domains.xdmf",
    facet_file="mesh_boundaries.xdmf",
    cell_type="tetra",
    facet_type="triangle",
):
    """Converts a MED mesh to XDMF
    Args:
        med_file (str): the name of the MED file
        cell_file (str, optional): the name of the file containing the
            volume markers. Defaults to "mesh_domains.xdmf".
        facet_file (str, optional): the name of the file containing the
            surface markers.. Defaults to "mesh_boundaries.xdmf".
        cell_type (str, optional): The topology of the cells. Defaults to "tetra".
        facet_type (str, optional): The topology of the facets. Defaults to "triangle".
    Returns:
        dict, dict: the correspondance dict, the cell types
    """
    msh = meshio.read(med_file)

    correspondance_dict = msh.cell_tags

    cell_data_types = msh.cell_data_dict["cell_tags"].keys()

    for mesh_block in msh.cells:
        if mesh_block.type == cell_type:

            meshio.write_points_cells(
                cell_file,
                msh.points,
                [mesh_block],
                cell_data={"f": [-1 * msh.cell_data_dict["cell_tags"][cell_type]]},
            )
        elif mesh_block.type == facet_type:
            meshio.write_points_cells(
                facet_file,
                msh.points,
                [mesh_block],
                cell_data={"f": [-1 * msh.cell_data_dict["cell_tags"][facet_type]]},
            )

    return correspondance_dict, cell_data_types
correspondance_dict, cell_data_types = convert_med_to_xdmf("mesh.med", cell_file="mesh_domains.xdmf", facet_file="mesh_boundaries.xdmf")

print(correspondance_dict)

```

However, I get the following error:

```
File "/home/ndrea_i_aio/festim/CAD integration.py", line 47, in <module>
  correspondance_dict, cell_data_types = convert_med_to_xdmf("mesh.med", cell_file="mesh_domains.xdmf", facet_file="mesh_boundaries.xdmf")

File "/home/ndrea_i_aio/festim/CAD integration.py", line 27, in convert_med_to_xdmf
  cell_data_types = msh.cell_data_dict["cell_tags"].keys()
KeyError: 'cell_tags'

```

How can I solve this issue?

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![VVKulagin](https://yyz2.discourse-cdn.com/free1/user_avatar/festim.discourse.group/vvkulagin/32/51_2.png) [@VVKulagin](https://festim.discourse.group/u/VVKulagin)\
**Post date:** [July 9, 2025, 11:14am UTC](https://festim.discourse.group/t/issue-with-med-to-xdmf-conversion-in-festim-workshop-8/125/2 "2025-07-09T11:14:57Z")

</div>

Hi @Andrea!

I could not reproduce your issue neither via pulling the Jupyter book from git, nor via making a standalone script.

Would you mind checking the downloaded med-file (or sharing it here) and the installed dependencies (e.g. environment the installation procedure of which is described on the Workshop main page)? The task also works fine in [Binder](https://mybinder.org/v2/gh/festim-dev/FESTIM-workshop/main)

---

<div class="post-metadata">

**Author:** ![Andrea](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@Andrea](https://festim.discourse.group/u/Andrea)\
**Post date:** [July 10, 2025, 10:23am UTC](https://festim.discourse.group/t/issue-with-med-to-xdmf-conversion-in-festim-workshop-8/125/3 "2025-07-10T10:23:51Z")

</div>

Thank you for your prompt reply!

I’m linking the downloaded `.med` file below.  
I installed `meshio` using Conda, following the instruction provided on [https://github.com/nschloe/meshio](https://github.com/nschloe/meshio), and I verified that all dependencies are installed correctly.

I hope this answers all your questions—please let me know if you need anything else.  
[mesh.zip](https://festim.discourse.group/uploads/short-url/zI9K9ZczpbCpTf6w5bripaKC9NN.zip) (1.8 MB)

---

<div class="post-metadata">

**Author:** ![remidm](https://yyz2.discourse-cdn.com/free1/user_avatar/festim.discourse.group/remidm/32/4_2.png) [@remidm](https://festim.discourse.group/u/remidm)\
**Post date:** [July 10, 2025, 1:34pm UTC](https://festim.discourse.group/t/issue-with-med-to-xdmf-conversion-in-festim-workshop-8/125/4 "2025-07-10T13:34:22Z")

</div>

@Andrea what cells do you have in your mesh? tetrahedra? triangles?

By default, `cell_type` defaults to `"tetra"` and `facet_type` defaults to `"triangle"`.

For exampe, if you have a 2D mesh with triangles for cells then you need

```python
convert_med_to_xdmf(
    med_file,
    cell_file="mesh_domains.xdmf",
    facet_file="mesh_boundaries.xdmf",
    cell_type="triangle",
    facet_type="line",
)

```

---

<div class="post-metadata">

**Author:** ![remidm](https://yyz2.discourse-cdn.com/free1/user_avatar/festim.discourse.group/remidm/32/4_2.png) [@remidm](https://festim.discourse.group/u/remidm)\
**Post date:** [July 10, 2025, 1:43pm UTC](https://festim.discourse.group/t/issue-with-med-to-xdmf-conversion-in-festim-workshop-8/125/5 "2025-07-10T13:43:13Z")

</div>

Also, what version of `meshio` are you using?

---

<div class="post-metadata">

**Author:** ![Andrea](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@Andrea](https://festim.discourse.group/u/Andrea)\
**Post date:** [July 10, 2025, 3:33pm UTC](https://festim.discourse.group/t/issue-with-med-to-xdmf-conversion-in-festim-workshop-8/125/6 "2025-07-10T15:33:26Z")

</div>

Thank you very much for your help.

I’m using version 5.3.5 of `meshio`. I replaced the `mesh.med` file from [GitHub](https://github.com/festim-dev/FESTIM-workshop/tree/main/book/content/task08) with the one used in the [Binder](https://hub.gesis.mybinder.org/user/festim-dev-festim-workshop-d4luzq1u/lab/tree/book/content/task08.ipynb) environment, and now everything works correctly.

Thanks again for the support!

---

<div class="post-metadata">

**Author:** ![remidm](https://yyz2.discourse-cdn.com/free1/user_avatar/festim.discourse.group/remidm/32/4_2.png) [@remidm](https://festim.discourse.group/u/remidm)\
**Post date:** [July 10, 2025, 3:48pm UTC](https://festim.discourse.group/t/issue-with-med-to-xdmf-conversion-in-festim-workshop-8/125/7 "2025-07-10T15:48:00Z")

</div>

@Andrea glad that things are now working!

The files in Github and Binder are the exact same files (because Binder is nothing more than a copy of the github repository with the installed dependencies).

The issue was somewhere else probably.

😃
