rice-solar-physics / pydrad

Python tools for setting up HYDRAD runs and parsing output
https://pydrad.readthedocs.io
MIT License
4 stars 3 forks source link

Add method for saving results to HDF5 #117

Closed wtbarnes closed 3 years ago

wtbarnes commented 3 years ago

Fixes #115

wtbarnes commented 3 years ago

@jwreep @sjbradshaw

With this addition you can do the following,

from pydrad.parse import Strand
s = Strand('/path/to/hydrad')
s.to_hdf5('/path/to/results.h5', 'coordinate', 'electron_temperature', 'electron_density')

which saves the field-aligned coordinate, electron temperature, and electron density, at each time step (uninterpolated), to an HDF5 file. You can save as many variables as you want by just adding arguments to the function. By default, the only variable that is saved is the time array.

To read the file,

with h5py.File('/path/to/results.h5', 'r') as hf:
    time = hf['time'][:]
    time_unit = hf['time'].attrs['unit']  # string representation of the unit
    Te = hf['index0/electron_temperature'][:]
    Te_unit = hf['index0/electron_temperature'].attrs['unit']

You can get subsequent time steps by using 'index1', 'index2', etc.

Additionally, if you only wanted to save a certain number of time steps, you can just use the slicing syntax on Strand,

s[1:5].to_hdf5('/path/to/results.h5', 'ion_temperature', 'velocity')

This saves the ion temperature and the velocity at time steps 1 through 4.

codecov[bot] commented 3 years ago

Codecov Report

Merging #117 into master will decrease coverage by 0.51%. The diff coverage is 18.18%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #117      +/-   ##
==========================================
- Coverage   41.71%   41.20%   -0.52%     
==========================================
  Files          10       10              
  Lines         489      500      +11     
==========================================
+ Hits          204      206       +2     
- Misses        285      294       +9     
Impacted Files Coverage Δ
pydrad/parse/parse.py 31.97% <18.18%> (-0.82%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 7a484dc...d01f20d. Read the comment docs.

jwreep commented 3 years ago

That was fast!