2.20. Example: post processing of saved field dataΒΆ

Suppose we have saved spatially resolved fields (as, for example, in Example 2: Computing the time development of a system), and we would like to read those from the data file to process the data further.

We can use the nmagpp tool if it provides the required functionality.

Alternatively, we can write a Python script that:

  1. reads the data from the _dat.h5 file
  2. carries out the required post processing and/or saves the data in (another) format.

The program read_h5.py demonstrates how to read the saved configuration with id=0 of the m_Py subfield, and to print this to the screen.

import nmag

#read data, positions, and sites from h5 file
m=nmag.get_subfield_from_h5file('bar_dat.h5','m_Py',id=0)
pos=nmag.get_subfield_positions_from_h5file('bar_dat.h5','m_Py')
site=nmag.get_subfield_sites_from_h5file('bar_dat.h5','m_Py')

#can carry out some sanity checks (but is not necessary)
assert m.shape == pos.shape
assert len(m) == len(site)

#print the data
for i in range(len(m)):
    print site[i], pos[i], m[i]

The functions get_subfield_from_h5file, get_subfield_positions_from_h5file and get_subfield_sites_from_h5file allow in principle to retrieve all the field data from the h5 files and stores this in the variables m, pos, and site, respectively.

The program, when run like this:

$ nsim read_h5.py

in the Example 2: Computing the time development of a system directory, produces output starting as follows (assuming the bar_dat.h5 file exists):

[0] [ 0.  0.  0.] [ 0.70710677  0.          0.70710677]
[1] [  3.00000000e-09   0.00000000e+00   0.00000000e+00] [ 0.70710677  0.          0.70710677]
[2] [  6.00000000e-09   0.00000000e+00   0.00000000e+00] [ 0.70710677  0.          0.70710677]
[3] [  9.00000000e-09   0.00000000e+00   0.00000000e+00] [ 0.70710677  0.          0.70710677]
[4] [  1.20000000e-08   0.00000000e+00   0.00000000e+00] [ 0.70710677  0.          0.70710677]
[5] [  1.50000000e-08   0.00000000e+00   0.00000000e+00] [ 0.70710677  0.          0.70710677]
[6] [  1.80000000e-08   0.00000000e+00   0.00000000e+00] [ 0.70710677  0.          0.70710677]
[7] [  2.10000000e-08   0.00000000e+00   0.00000000e+00] [ 0.70710677  0.          0.70710677]
[8] [  2.40000000e-08   0.00000000e+00   0.00000000e+00] [ 0.70710677  0.          0.70710677]
[9] [  2.70000000e-08   0.00000000e+00   0.00000000e+00] [ 0.70710677  0.          0.70710677]
[10] [  3.00000000e-08   0.00000000e+00   0.00000000e+00] [ 0.70710677  0.          0.70710677]
[11] [  3.00000000e-08   3.00000000e-08   1.00000000e-07] [ 0.70710677  0.          0.70710677]
[12] [  3.00000000e-08   2.70000000e-08   1.00000000e-07] [ 0.70710677  0.          0.70710677]

We can see that the Site index is (here) just an integer, the position (in nanometre) is shown as a triplet of three scalars, and the normalised magnetisation is also a vector with three components.

The data (in the arrays m, site and position in this example) can be manipulated as explained in the NumPy documentation, because it is of type numpy array. Numpy provides a powerful matrix processing environment.