Example usage

Here we will demonstrate how to use pyvlasiator to process Vlasiator data.

Downloading demo data

If you don’t have Vlasiator data to start with, you can download demo data with the following:

import requests
import tarfile
import os

url = "https://raw.githubusercontent.com/henry2004y/vlsv_data/master/testdata.tar.gz"
testfiles = url.rsplit("/", 1)[1]
r = requests.get(url, allow_redirects=True)
open(testfiles, "wb").write(r.content)
path = "./data"
if not os.path.exists(path):
   os.makedirs(path)
with tarfile.open(testfiles) as file:
   file.extractall(path)
os.remove(testfiles)

Loading VLSV data

Read meta data

from pyvlasiator.vlsv import Vlsv

file = "data/bulk.1d.vlsv"
meta = Vlsv(file)
meta
File       : bulk.1d.vlsv
Time       : 10.0000
Dimension  : 1
Max AMR lvl: 0
Has VDF    : True
Variables  : ['CellID', 'proton/vg_ptensor_diagonal', 'proton/vg_ptensor_offdiagonal', 'proton/vg_rho', 'proton/vg_v', 'vg_b_vol', 'vg_boundarytype']

Read parameters

t = meta.read_parameter("time")
t
np.float64(10.0)

Read variable

data = meta.read_variable("CellID")
data
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10], dtype=uint64)

Plotting

To enable plotting functionalities, we need to first import the corresponding submodule:

import pyvlasiator.plot

1D

meta.plot("proton/vg_rho")
[<matplotlib.lines.Line2D at 0x7fa125acb230>]
_images/b59bd0a78ba1bf013f5917a538b9b7dbcebc09a25b0b7686ce9572182caa8866.png

2D

file = "data/bulk.2d.vlsv"
meta = Vlsv(file)
meta.pcolormesh("vg_b_vol", comp=1)
meta.streamplot("proton/vg_v", comp="xy", color="w")
<matplotlib.streamplot.StreamplotSet at 0x7fa123756a50>
_images/fd5d36d0aa7b85d1ad993b6e2aa9c876abbea421a1511f204e0be08c5a9dc47d.png

Field solver grid variables are also supported via the same APIs:

meta.pcolormesh("fg_b", comp=1)
meta.streamplot("fg_b", comp="xy", color="w")
<matplotlib.streamplot.StreamplotSet at 0x7fa1235a9d10>
_images/44bd4e906699ef8c2be77a1946174b6bae027be8f04037dc4fa97a94270c9367.png

2D slice of 3D AMR data

file = "data/bulk.amr.vlsv"
meta = Vlsv(file)
meta.pcolormesh("proton/vg_rho")
<matplotlib.collections.QuadMesh at 0x7fa12042f4d0>
_images/a73c55913391c4c14215bdf8eec670c4cb3638b6e9e3c491dc81100f7d0aa6c7.png

VDF

file = "data/bulk.1d.vlsv"
meta = Vlsv(file)
loc = [2.0, 0.0, 0.0]
meta.vdfslice(loc)
<matplotlib.collections.QuadMesh at 0x7fa1204d3610>
_images/5937e9ffe4a2b3575dc8558b885a30b8c4bb742e79af223e62ba6bd6ff7e486e.png