Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement correctly type from NXdetector in hdf? #33

Open
RGBYCP opened this issue Jan 21, 2022 · 3 comments
Open

How to implement correctly type from NXdetector in hdf? #33

RGBYCP opened this issue Jan 21, 2022 · 3 comments

Comments

@RGBYCP
Copy link

RGBYCP commented Jan 21, 2022

Dear Nexus community,
Could I ask here for advice, please?

NXdetector has an entry called type
https://manual.nexusformat.org/classes/base_classes/NXdetector.html

"Description of type such as He3 gas cylinder, He3 PSD, scintillator, fission chamber, proportion counter, ion chamber, ccd, pixel, image plate, CMOS, …"

How do I implement this with h5py correctly?
The manual for h5py does not give an example for string datasets. https://docs.h5py.org/en/stable/strings.html#storing-strings

My approach:

uv_spectra=grp_uv.create_group("uv_spectra")
uv_spectra.attrs["NX_class"] = 'NXdetector'
string_dt = h5py.special_dtype(vlen=str)
uv_spectra_type=uv_spectra.create_dataset('type', data='ccd', dtype=string_dt)

But I don't know if this is the correct way.
Thank you for your help.

@rayosborn
Copy link

That is the correct way of storing variable-length strings, but I think it's the h5py default, so you probably don't have to specify a dtype at all. You could also store the string as fixed-length byte strings, because NeXus doesn't specify how strings are stored. However, variable-length strings would allow you to change the value more easily if you need to and are more flexible in handling Unicode.

I would also like to suggest you have a look at the nexusformat package, which would help to ensure that you write NeXus-compliant files. It's uses h5py, but handles things like setting the class variables automatically and is more succinct. Your four lines above could be written as:

uv_grp['uv_spectra'] = NXdetector(type='ccd')

There is more documentation here.

@RGBYCP
Copy link
Author

RGBYCP commented Jan 21, 2022

Dear @rayosborn

Thank you very much for your quick help.
Thanks for your confirmation of how to do this with h5py. I tried it without the dtype, it works as well. Thank you for the explanation about the two string types. I will then opt for the moment for the variable-length string.

Thanks for pointing out to the nexusformat package. I saw it before, but I know already h5py and it was suggested to me that I use h5py. I will make a suggestion to use the nexusformat.

Again, thank you for your help.

@rayosborn
Copy link

The nexusformat package is a thin layer on top of h5py that ensures that certain NeXus rules are obeyed, understands NeXus classes, and how to construct NXdata groups, so I personally think it makes sense to use it, particularly if you are new to NeXus, but then I am biased. You can try it out in an online Jupyter notebook if you want to see what it does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants