openPMD / openPMD-api

:floppy_disk: C++ & Python API for Scientific I/O
https://openpmd-api.readthedocs.io
GNU Lesser General Public License v3.0
134 stars 51 forks source link

Using Maps/Dictionaries for attributes corresponding to `axisLabels` #1447

Open DerNils-git opened 1 year ago

DerNils-git commented 1 year ago

Regarding the openPMD-standard attributes corresponding to specific axes (e.g. gridSpacing, globalGridOffset) of a mesh should be ordered in the same way as the axisLabels in their array. Currently this is just a suggestion to implementors which totally makes sense.

Guaranteeing that axisLabels match axis specific attributes is possible by storing these attributes e.g. in a C++ std::map<std::sting,double>. That makes it impossible to mess up axis specific attributes and axis labels.

Currently the implementor has to make sure that the order is correct which does not guarantee the correct order such that the following implementation mistakes are possible

openPMD::Mesh mesh{<SOME/MESH>};

std::vector<std::string> axisLabels = {"x","y","z"};
mesh.setAxisLabels(axisLabels);
std::vector<double> gridSpacing = {dx,dz,dy};
mesh.setGridSpacing(gridSpacing);

A safer way would be

openPMD::Mesh mesh{<SOME/MESH>};

std::vector<std::string> axisLabels = {"x","z","y"};
mesh.setAxisLabels(axisLabels);
std::map<std::string, double> gridSpacing = {{"x",dx},{"y",dy},{"z",dz}};
mesh.setGridSpacing(gridSpacing);

In this implementation additional safeguards could be added within openPMD such that attributes can only be added for axes that are already stored in axisLabels.

Possible Problem: I am not sure how well a python dictionary interacts with a C++ std::map. This might be an issue in the python API of openPMD.

franzpoeschel commented 1 year ago

At first glance, this sounds like a sensible and non-intrusive addition to the API. Will have a look