How to instantiate repeating subsections for "results"-section

Hi.
I’m writing a parser, and I would like to create several, let’s say 4, instantiations of the repeating subsection results.properties.structures.structure_original.species. So that when people click on species, the numbers 0 to 3 appear underneath and behind each of these 4 numbers are the quantities name, chemical_symbols, concentration etc. It would be great if someone could show me how to code this. Thanks a lot in advance!
Best
Fabian

Hi Fabian,

Generally, you can create as many (sub-)sections as you want by simply looping over some index and using m_create. Let me illustrate with a dummy example:

from nomad.datamodel.results import Results, Properties, Structure
from nomad.datamodel.optimade import Species
...
...
sec_results = Results()
sec_properties = sec_results.m_create(Properties)
sec_structure = sec_properties.m_create(Structure)
for i in range(4):
   sec_species = sec_structure.m_create(Species)
   sec_species.name = f'My example species number {i}'
   ... (populate sec_species with other quantities)

m_create will essentially create as many sections in the parent section you are calling it out. This for loop will (m_)create a new section every time it loops.

There is an alternative and equally valid way, which is using m_add_sub_section, in a similar manner as you will be using setattr in Python. In the for loop of the dummy example above, you could also do:

for i in range(4):
   sec_species = Species(
      name=f'My example species number {i}',
      ... (populate sec_species with other quantities)
   )
   sec_structure.m_add_sub_section(Structure.species, sec_species)   


Said so, parsers are normally handling EntryArchive.run, rather than EntryArchive.results. There is another layer of the so-called normalizers that act on top of what the parsers are populating. I will not be surprised that you populate something in the parser for results, and then it gets overwritten by what the /nomad/normalizing/ modules are doing.

In this concrete example, I think you are safe. But I just wanted to mention it in case you face that you are writing something in the parser for results, but for some reason it is not showing in the EntryArchive which is being generated.

If this happens, please contact us again, we will be happy to help you further :slightly_smiling_face:

All the best,
Jose