-
Notifications
You must be signed in to change notification settings - Fork 39
Magnetopause script updates #326
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
Closed
Closed
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
077b6c2
Added documentation strings to magnetopause scripts and fixed plottin…
jreimi b95d2c0
Add checks for starting point input, change ragged arrays to lists, a…
jreimi de4f3f8
Change the streamline tracing to use analysator's fieldtracer instead…
jreimi 6ca082e
2d and 3d magnetopause scripts: Add function find_magnetopause() that…
jreimi 79abb14
Moved magnetopause functions to analysator/pyCalculations
jreimi a200580
Add magnetopause functions and surface function to calculations.py
jreimi 99e37c9
Change surface triangles so that the normals have the same direction.
jreimi 35cf3fb
Changed function names for clarity, added possible keyword argumets f…
jreimi 6225f0d
Added example files for using magnetopause functions and plotting the…
jreimi 58e84bf
Added yt dependency for the 2d script
alhom 7303815
function and file name changes and removal of unnecessary surface mak…
jreimi 40842f0
Changed magnetopause stuff names in the sphinx directory to (hopefull…
jreimi 1a3502f
Added fix for toctree
jreimi 6eb059b
Function name changes, removed unused import
jreimi 6a69fac
Fix to surface making and an attempt to make the subsolar area nicer
jreimi 4340cc9
More possible fixes to surface making, function name changes also to …
jreimi dc08ad2
Removed streamline magnetopause from scripts in sphinx
jreimi da57da3
Move streamline magnetopause script usage examples to scripts
jreimi 1fc418f
Updated sphinx
jreimi f6bff74
Added blank lines
jreimi 35965fe
fixes for sphinx
jreimi a5a68b5
more fixes for sphinx
jreimi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| plot_streamline_magnetopause_2d | ||
| ------------------------------- | ||
|
|
||
| .. automodule:: plot_streamline_magnetopause_2d | ||
| :members: | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| plot_streamline_magnetopause_3d | ||
| ------------------------------- | ||
|
|
||
| .. automodule:: plot_streamline_magnetopause_3d | ||
| :members: | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
analysator/pyCalculations/magnetopause_sw_streamline_2d.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| ''' | ||
| Finds the magnetopause position by tracing streamlines of the plasma flow for two-dimensional Vlasiator runs. Needs the yt package. | ||
| ''' | ||
|
|
||
| import numpy as np | ||
| import analysator as pt | ||
| import yt | ||
|
|
||
|
|
||
| def interpolate(streamline, x_points): | ||
| """Interpolates a single streamline for make_magnetopause(). | ||
|
|
||
| :param streamline: a single streamline to be interpolated | ||
| :param x_points: points in the x-axis to use for interpolation | ||
| :returns: the streamline as numpy array of x,z coordinate points where the x-axis coordinates are the points given to the function | ||
| """ | ||
|
|
||
| arr = np.array(streamline) | ||
|
|
||
| # set arrays for interpolation | ||
| xp = arr[:,0][::-1] | ||
| zp = arr[:,2][::-1] | ||
|
|
||
| #interpolate z coordinates | ||
| z_points = np.interp(x_points, xp, zp, left=np.NaN, right=np.NaN) | ||
|
|
||
| return np.array([x_points, z_points]) | ||
|
|
||
|
|
||
| def make_streamlines(vlsvfile, streamline_seeds=None,seeds_n=200, seeds_x0=20*6371000, seeds_range=[-5*6371000, 5*6371000], streamline_length=40*6371000): | ||
| """Traces streamlines of velocity field using the yt package. | ||
|
|
||
| :param vlsvfile: directory and file name of .vlsv data file to use for VlsvReader | ||
| :kword streamline_seeds: optional streamline starting points in numpy array (coordinates in meters including the y-coordinate 0.0) | ||
| :kword seeds_n: instead of streamline_seeds provide a number of streamlines to be traced | ||
| :kword seeds_x0: instead of streamline_seeds provide an x-coordinate for streamline starting points | ||
| :kword seeds_range: instead of streamline_seeds provide [min, max] range to use for streamline starting point z-coordinates | ||
| :kword streamline_length: streamline length | ||
|
|
||
| :returns: streamlines as numpy array | ||
| """ | ||
|
|
||
| # bulk file | ||
| f = pt.vlsvfile.VlsvReader(file_name=vlsvfile) | ||
|
|
||
| # get box coordinates from data | ||
| [xmin, ymin, zmin, xmax, ymax, zmax] = f.get_spatial_mesh_extent() | ||
| [xsize, ysize, zsize] = f.get_spatial_mesh_size() | ||
| simext =[xmin,xmax,ymin,ymax,zmin,zmax] | ||
| sizes = np.array([xsize,ysize,zsize]) | ||
| boxcoords=list(simext) | ||
|
|
||
| cellids = f.read_variable("CellID") | ||
|
|
||
| #Read the data from vlsv-file | ||
| Vx = f.read_variable("v", operator="x") | ||
| Vz = f.read_variable("v", operator="z") | ||
|
|
||
|
|
||
| #Re-shape variable data | ||
| Vxs=Vx[np.argsort(cellids)].reshape(f.get_spatial_mesh_size(), order="F") | ||
| Vys = np.zeros_like(Vxs) | ||
| Vzs=Vz[np.argsort(cellids)].reshape(f.get_spatial_mesh_size(), order="F") | ||
|
|
||
| data=dict(Vx=Vxs,Vy=Vys,Vz=Vzs) | ||
|
|
||
| #Create starting points for streamlines if they are not given | ||
| if streamline_seeds == None: | ||
| streamline_seeds = np.array([[seeds_x0, 0 ,i] for i in np.linspace(seeds_range[0], seeds_range[1], seeds_n)]) | ||
|
|
||
| #streamline_seeds = np.array(streamline_seeds) | ||
| #dataset in yt-form | ||
| yt_dataset = yt.load_uniform_grid( | ||
| data, | ||
| sizes, | ||
| bbox=np.array([[boxcoords[0], boxcoords[1]], | ||
| [boxcoords[2],boxcoords[3]], | ||
| [boxcoords[4],boxcoords[5]]])) | ||
|
|
||
|
|
||
| #data, seeds, dictionary positions, step size | ||
| streamlines = yt.visualization.api.Streamlines(yt_dataset, streamline_seeds, | ||
| "Vx", "Vy", "Vz", length=streamline_length, direction=1) | ||
|
|
||
| #trace the streamlines with yt | ||
| streamlines.integrate_through_volume() | ||
| # return streamline positions | ||
| return np.array(streamlines.streamlines) | ||
|
|
||
|
|
||
| def make_magnetopause(streamlines, end_x=-15*6371000, x_point_n=50): | ||
| """Finds the mangetopause location based on streamlines. | ||
|
|
||
| :param streams: streamlines (coordinates in m) | ||
| :kword end_x: tail end x-coordinate (how far along the negative x-axis the magnetopause is calculated) | ||
| :kword x_point_n: integer, how many x-axis points the magnetopause will be divided in between the subsolar point and tail | ||
|
|
||
| :returns: the magnetopause position as coordinate points in numpy array | ||
| """ | ||
|
|
||
| RE = 6371000 | ||
|
|
||
| streampoints = np.reshape(streamlines, (streamlines.shape[0]*streamlines.shape[1], 3)) #all the points in one array | ||
|
|
||
| ## find the subsolar dayside point in the positive x-axis | ||
| ## do this by finding a stremline point on positive x axis closest to the Earth | ||
| x_axis_points = streampoints[(streampoints[:,2]< RE) & (streampoints[:,2]> -RE) & (streampoints[:,0]> 0)] | ||
| subsolar_x =np.min(x_axis_points[:,0]) | ||
|
|
||
| ## define points in the x axis where to find magnetopause points on the yz-plane | ||
| x_points = np.linspace(subsolar_x, end_x, x_point_n) | ||
|
|
||
| ## interpolate more exact points for streamlines at exery x_point | ||
| new_streampoints = np.zeros((len(x_points), len(streamlines), 1)) # new array for keeping interpolated streamlines in form streamlines_new[x_point, streamline, z-coordinate] | ||
|
|
||
| for i,stream in enumerate(streamlines): | ||
| interpolated_streamline = interpolate(stream, x_points) | ||
| for j in range(0, len(x_points)): | ||
| new_streampoints[j, i,:] = interpolated_streamline[1,j] | ||
|
|
||
|
|
||
| ## start making the magnetopause | ||
| ## in each x_point, find the closest streamline to x-axis in the positive and negative z-axis | ||
|
|
||
| pos_z_mpause = np.zeros((len(x_points), 2)) | ||
| neg_z_mpause = np.zeros((len(x_points), 2)) | ||
|
|
||
| for i, x_point in enumerate(x_points): | ||
| pos = new_streampoints[i, new_streampoints[i,:] > 0] | ||
| neg = new_streampoints[i, new_streampoints[i,:] < 0] | ||
|
|
||
| if (pos.size == 0) or (neg.size == 0): | ||
| raise ValueError('No streamlines found for x axis point, try adding streamlines or checking the x_points') | ||
|
|
||
| # find points closest to x-axis and save found points | ||
| pos_z_mpause[i] = [x_point, pos[pos.argmin()]] | ||
| neg_z_mpause[i] = [x_point, neg[neg.argmax()]] | ||
|
|
||
| magnetopause = np.concatenate((pos_z_mpause[::-1], np.array([[subsolar_x, 0]]), neg_z_mpause)) | ||
|
|
||
| return magnetopause | ||
|
|
||
|
|
||
| def find_magnetopause_sw_streamline_2d(vlsvfile, streamline_seeds=None, seeds_n=200, seeds_x0=20*6371000, seeds_range=[-5*6371000, 5*6371000], streamline_length=45*6371000, end_x=-15*6371000, x_point_n=50): | ||
| """Finds the magnetopause position by tracing streamlines of the velocity field for 2d runs. | ||
|
|
||
| :param vlsvfile: directory and file name of .vlsv data file to use for VlsvReader | ||
| :kword streamline_seeds: optional streamline starting points in numpy array (coordinates in meters including the y-coordinate 0.0) | ||
| :kword seeds_n: instead of streamline_seeds provide a number of streamlines to be traced | ||
| :kword seeds_x0: instead of streamline_seeds provide an x-coordinate for streamline starting points | ||
| :kword seeds_range: instead of streamline_seeds provide [min, max] range to use for streamline starting point z-coordinates | ||
| :kword streamline_length: streamline length for tracing | ||
| :kword end_x: tail end x-coordinate (how far along the negative x-axis the magnetopause is calculated) | ||
| :kword x_point_n: integer, how many x-axis points the magnetopause will be divided in between the subsolar point and tail | ||
|
|
||
| :returns: the magnetopause position as coordinate points in numpy array | ||
| """ | ||
|
|
||
| streamlines = make_streamlines(vlsvfile, streamline_seeds, seeds_n, seeds_x0, seeds_range, streamline_length) | ||
| magnetopause = make_magnetopause(streamlines, end_x, x_point_n) | ||
|
|
||
| return magnetopause |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restore these to point towards your plot scripts