Author Topic: Parameters in rh file  (Read 74 times)

JonasTFab

  • Member
  • Posts: 7
Parameters in rh file
« on: 28. March 2021, 16:23:25 »
Is it possible to print all variable names that is stored in data when calling data = rh15d.Rh15dout(path) in python? It would be nice to have a function like data.keys() that print all variables stored in data.
I tried this

for i in data.files:
    print(i, "\n\n")

but it does not print the variable names, only information related to each variable. I tried to look it up at https://github.com/ITA-Solar/helita/blob/master/helita/sim/rh15d.py but with no luck.

Elena Mamonova

  • Member
  • Posts: 5
Re: Parameters in rh file
« Reply #1 on: 28. March 2021, 17:44:18 »
I might have misunderstood your intention, but you can just print files attribute:

Code: [Select]
print(data.files)

If you want just variable names and their properties, then you could do:

Code: [Select]
for f in range(0, len(data.files)):
     print(f"file #{f + 1}: {data.files[f].data_vars}\n")

that will produce something like this:

Code: [Select]
file #1: Data variables:
    Cij_line         (line, x, y, height) float32 ...
    Cji_line         (line, x, y, height) float32 ...
    Rij_continuum    (continuum, x, y, height) float32 ...
    Rij_line         (line, x, y, height) float32 ...
    Rji_continuum    (continuum, x, y, height) float32 ...
    Rji_line         (line, x, y, height) float32 ...
    populations      (level, x, y, height) float32 ...
    populations_LTE  (level, x, y, height) float32 ...

file #2: Data variables:
    electron_density   (x, y, height) float64 ...
    element_abundance  (element) float64 ...
    element_weight     (element) float64 ...
    height_scale       (x, y, height) float32 ...
    muz                (ray) float64 ...
    temperature        (x, y, height) float32 ...
    velocity_z         (x, y, height) float32 ...
    wmu                (ray) float64 ...

file #3: Data variables:
    atom_groups            (phony_dim_8) |S8 ...
    ...

Finally, if you only need variable names, then you could list them like this:

Code: [Select]
for v in data.files[0].data_vars:
    print(v)

that will print:

Code: [Select]
Cij_line
Cji_line
Rij_continuum
Rij_line
Rji_continuum
Rji_line
populations
populations_LTE

JonasTFab

  • Member
  • Posts: 7
Re: Parameters in rh file
« Reply #2 on: 28. March 2021, 20:36:20 »
Ah sorry, what I mean't is that I want to know what each of the index in data.files represents. For instance, I figured that data.files[0] produce the same outputs as data.atom_MG so I guess data.files[0] should have atom_MG as label. But how do I find this labeling?

souvikb

  • Member
  • Posts: 9
Re: Parameters in rh file
« Reply #3 on: 29. March 2021, 15:20:55 »
Hi,

As I understand, there could be two possible options.

Option 1. You can always do a ncdump -h output_ray.hdf5 or output_aux.hdf5 in the terminal, i.e. in your output directory and it will list the groups, attributes, and variables for each of the HDF5 files that you provide as inputs.

But I understand that you would want to have a pythonic way of doing the same. The thing is rh15dout is written as a class that has several instances and attributes so if you simply do a print(data.files) it outputs whatever it has written from the beginning. So I am not 100% sure if that is what you want, but you can always print that to find the list of all variables present in data.files using the way Elena mentioned.  But if you want to achieve the "labels" as you describe, I use the following:

Option 2: Using h5py

In your output directory simply load individual files using h5py.
  import h5py
  data = h5py.File('output_aux.hdf5','r')
  for group in data.keys():
       print(group)

This will print out all the groups you have, something like atom_MG or atom_XX or molecule_XX.
The same is possible with output_indata.hdf5.

Remember, that output_ax.hdf5 and output_indata.hdf5 do not directly provide you access to variables present inside them. You first have to access the groups such as atom_XX in output_aux or atmos, height, mpi, and input in output_indata.hdf5. The output_ray.hdf5 file, for example, has no groups (but one root group itself) so doing a data_ray.keys(), will list all possible variables right away that you have in there. This is the way the outputs are written using RH1.5D.

And finally when you want to access the variables inside a group as in atom_XX or atmos from either output_aux.hdf5 or output_indata.hdf5, you can do something like a "nested keys()" (this is NOT the right terminology but I like to use it  :D :D :D) like this:

say you read aux file in data:

  data = h5py.File('output_aux.hdf5','r')
  for group in data.keys():
       print('This is a group named', group, 'and it contains the following variables/datasets:')
       for dset in data[group].keys():
            print(dset)
________________________________________________________________________

This will print:
This is a group named atom_MG and it contains the following variables/datasets:
continuum
height
level
line
x
y

I hope it helps you a bit? Maybe there could be a better way that someone else might propose?
Cheers,
PS- Sorry I still do not know how add code blocks or highlight in this portal. So I simply wrote it as it is, unlike the way shown by Elena  ;D ;D ;D

« Last Edit: 29. March 2021, 15:25:02 by souvikb »

Thore E. Moe

  • Member
  • Posts: 17
  • Gruppelærer AST2000
Re: Parameters in rh file
« Reply #4 on: 29. March 2021, 16:05:04 »
Souvik, if you want to write code-blocks here you can enclose them with [#code] and [#/code] (without the #'s; that's just a dirty fix for me not knowing the proper way to escape the brackets).

[#code]
>>> print('hello world')
hello world
[#/code]

without # becomes:

Code: [Select]
>>> print('hello world')
hello world

souvikb

  • Member
  • Posts: 9
Re: Parameters in rh file
« Reply #5 on: 29. March 2021, 16:07:37 »
Thanks, Thore!  :)

JonasTFab

  • Member
  • Posts: 7
Re: Parameters in rh file
« Reply #6 on: 29. March 2021, 19:08:43 »
Okey I think I get it now. Thanks for the help :)

souvikb

  • Member
  • Posts: 9
Re: Parameters in rh file
« Reply #7 on: 29. March 2021, 19:20:13 »
Great! Happy to help.