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
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 0x7fcb93d55a90>]
_images/900c3dadb81dd1ba2d3ee6fbf1cda6413745a28e7832dbf9ad282b33af7f9fb0.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 0x7fcb91831590>
_images/11f0e00dcc0eef0a2fe3f633741ebc0fa33cafcf91e877c6c845ebc6ba5db126.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 0x7fcb93a48690>
_images/2271a7d2931599b1f7f661f4a84753ee362b633233416e17c2f43a36e7425518.png

2D slice of 3D AMR data

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

VDF

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