Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ability to plot Pfsspy field lines #12

Merged
merged 16 commits into from
Jun 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/12.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added :meth:`~sunkit_pyvista.plotter.SunpyPlotter.plot_field_lines` method which allows for plotting of magnetic field lines from `pfsspy` through pyvista.
62 changes: 62 additions & 0 deletions examples/skip_plot_field_lines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
================================
Plotting Field Lines from pfsspy
================================

sunkit-pyvista can be used to plot field lines from `pfsspy`.
"""

import numpy as np
import pfsspy
from pfsspy import tracing
from pfsspy.sample_data import get_gong_map

import astropy.units as u
from astropy.constants import R_sun
from astropy.coordinates import SkyCoord
from sunpy.data.sample import AIA_193_IMAGE
from sunpy.map import Map

from sunkit_pyvista import SunpyPlotter

###############################################################################
# We will firstly use an AIA 193 image from the sunpy sample data as the base image.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be an issue if the AIA map and the GONG map were not taken at the same time. I will see if I can add a GONG map to the pfsspy sample data that is taken at the same observation date as the AIA map. Lets not block this PR on this point, but we should fix it before releasing the package!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh okay, yes

m = Map(AIA_193_IMAGE)

# Start by creating a plotter
plotter = SunpyPlotter()

# Plot a map
plotter.plot_map(m)
# Add an arrow to show the solar rotation axis
plotter.plot_solar_axis()

# We load a gong_map from pfsspy
gong_fname = get_gong_map()
gong_map = Map(gong_fname)

# Define the number of grid points in rho and solar surface rarius
nrho = 35
rss = 2.5

# Create 5 points spaced between lat={-90, 90} degrees
lat = np.linspace(-np.pi / 2, np.pi / 2, 8, endpoint=False)
# Create 5 points spaced between long={0, 180} degrees
lon = np.linspace(0, 2 * np.pi, 8, endpoint=False)
# Make a 2D grid from these 1D points
lat, lon = np.meshgrid(lat, lon, indexing='ij')
# Create lon, lat and radial coordinate values by using a pfsspy
# and trace them using tracer
lat, lon = lat.ravel() * u.rad, lon.ravel() * u.rad
radius = 1.2
tracer = tracing.PythonTracer()
input_ = pfsspy.Input(gong_map, nrho, rss)
output_ = pfsspy.pfss(input_)
seeds = SkyCoord(lon, lat, radius*R_sun,
frame=gong_map.coordinate_frame)
field_lines = tracer.trace(seeds, output_)

# We plot the field lines
plotter.plot_field_lines(field_lines)

plotter.show()
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ setup_requires =
install_requires =
pyvista>=0.30.1
sunpy[map]>=3.0.0
pfsspy>=0.6.6

[options.extras_require]
tests =
Expand Down
17 changes: 17 additions & 0 deletions sunkit_pyvista/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,20 @@ def plot_solar_axis(self, length=2.5, arrow_kwargs={}, **kwargs):
scale='auto',
**defaults)
self.plotter.add_mesh(arrow, **kwargs)

def plot_field_lines(self, field_lines, **kwargs):
"""
Plots the field lines from `pfsspy`.

Parameters
----------
field_lines : `pfsspy.fieldline.FieldLines`
Field lines to be plotted.
**kwargs :
Keyword arguments are handed to `pyvista.Plotter.add_mesh`.
"""
for field_line in field_lines:
grid = self._coords_to_xyz(field_line.coords.ravel())
jeffreypaul15 marked this conversation as resolved.
Show resolved Hide resolved
jeffreypaul15 marked this conversation as resolved.
Show resolved Hide resolved
field_line_mesh = pv.StructuredGrid(grid[:, 0], grid[:, 1], grid[:, 2])
color = {0: 'black', -1: 'tab:blue', 1: 'tab:red'}.get(field_line.polarity)
self.plotter.add_mesh(field_line_mesh, color=color, **kwargs)