-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpybats.html
577 lines (564 loc) · 41 KB
/
pybats.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
<!DOCTYPE html>
<html lang="en" data-content_root="./">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.18.1: http://docutils.sourceforge.net/" />
<title>PyBats - SWMF & BATS-R-US Analysis Tools — SpacePy v0.7.0 Manual</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=b76e3c8a" />
<link rel="stylesheet" type="text/css" href="_static/sphinxdoc.css?v=92e3d466" />
<link rel="stylesheet" type="text/css" href="_static/graphviz.css?v=fd3f3429" />
<link rel="stylesheet" type="text/css" href="_static/plot_directive.css" />
<script src="_static/documentation_options.js?v=fe7df9b0"></script>
<script src="_static/doctools.js?v=9a2dae69"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<script type="text/javascript" src="_static/copybutton.js"></script>
<link rel="icon" href="_static/spacepy_favicon.ico"/>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="pycdf - Python interface to CDF files" href="pycdf.html" />
<link rel="prev" title="OMNI" href="omni.html" />
</head><body>
<div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px">
<a href="index.html"><img src="_static/spacepy_logo.jpg" border="0" alt="spacepy_logo"/></a>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="pycdf.html" title="pycdf - Python interface to CDF files"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="omni.html" title="OMNI"
accesskey="P">previous</a> |</li>
<li><a href="https://spacepy.github.io/"">homepage</a>| </li>
<li><a href="https://github.com/spacepy/spacepy">development</a>| </li>
<li><a href="search.html">search</a>| </li>
<li><a href="index.html">documentation </a> »</li>
<li class="nav-item nav-item-this"><a href="">PyBats - SWMF & BATS-R-US Analysis Tools</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="pybats-swmf-bats-r-us-analysis-tools">
<h1>PyBats - SWMF & BATS-R-US Analysis Tools<a class="headerlink" href="#pybats-swmf-bats-r-us-analysis-tools" title="Link to this heading">¶</a></h1>
<nav class="contents local" id="table-of-contents">
<p class="topic-title">Table of Contents</p>
<ul class="simple">
<li><p><a class="reference internal" href="#pybats-module" id="id2">PyBats Module</a></p>
<ul>
<li><p><a class="reference internal" href="#pybats" id="id3">pybats</a></p></li>
<li><p><a class="reference internal" href="#class-idlfile" id="id4">class IdlFile</a></p></li>
<li><p><a class="reference internal" href="#class-imfinput" id="id5">class ImfInput</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#bats-submodule" id="id6">Bats Submodule</a></p>
<ul>
<li><p><a class="reference internal" href="#bats2d" id="id7">Bats2d</a></p></li>
</ul>
</li>
</ul>
</nav>
<p>PyBats! An open source Python-based interface for reading, manipulating,
and visualizing BATS-R-US and SWMF output.
For more information on the SWMF, please visit the
<a class="reference external" href="http://csem.engin.umich.edu">Center for Space Environment Modeling</a>.</p>
<p>See also the <a class="reference internal" href="autosummary/spacepy.pybats.html#module-spacepy.pybats" title="spacepy.pybats"><code class="xref py py-obj docutils literal notranslate"><span class="pre">full</span> <span class="pre">API</span> <span class="pre">documentation</span></code></a>.</p>
<section id="pybats-module">
<h2><a class="toc-backref" href="#id2" role="doc-backlink">PyBats Module</a><a class="headerlink" href="#pybats-module" title="Link to this heading">¶</a></h2>
<section id="pybats">
<h3><a class="toc-backref" href="#id3" role="doc-backlink">pybats</a><a class="headerlink" href="#pybats" title="Link to this heading">¶</a></h3>
<section id="introduction">
<h4>Introduction<a class="headerlink" href="#introduction" title="Link to this heading">¶</a></h4>
<p>At its most fundamental level, PyBats provides access to output files written
by the Space Weather Modeling Framework and the codes contained within.
The key task performed by PyBats is loading simulation data into a Spacepy
data model object so that the user can move on to the important tasks of
analyzing and visualizing the values. The secondary goal of PyBats is to make
common tasks performed with these data as easy as possible. The result is that
most SWMF output can be opened and visualized using only a few lines of code.
Many complicated tasks, such as field line integration, is included as well.</p>
</section>
<section id="organization">
<h4>Organization<a class="headerlink" href="#organization" title="Link to this heading">¶</a></h4>
<p>Many output files in the SWMF share a common format. Objects to handle broad
formats like these are found in the base module. The base module has classes
to handle SWMF <em>input</em> files, as well.</p>
<p>The rest of the classes are organized by code, i.e. classes and functions
specifically relevant to BATS-R-US can be found in
<a class="reference internal" href="autosummary/spacepy.pybats.bats.html#module-spacepy.pybats.bats" title="spacepy.pybats.bats"><code class="xref py py-mod docutils literal notranslate"><span class="pre">spacepy.pybats.bats</span></code></a>. Whenever a certain code included in the SWMF
requires a independent class or a subclass from the PyBats base module, it
will receive its own submodule.</p>
</section>
<section id="conventions-and-prefixes">
<h4>Conventions and Prefixes<a class="headerlink" href="#conventions-and-prefixes" title="Link to this heading">¶</a></h4>
<p>Nearly every class in PyBats inherits from <a class="reference internal" href="autosummary/spacepy.datamodel.SpaceData.html#spacepy.datamodel.SpaceData" title="spacepy.datamodel.SpaceData"><code class="xref py py-class docutils literal notranslate"><span class="pre">spacepy.datamodel.SpaceData</span></code></a>
so it is important for users to understand how to employ and explore SpaceData
objects. There are a few exceptions, so always pay close attention to the
docstrings and examples. Legacy code that does not adhere to this pattern is
slowly being brought up-to-date with each release.</p>
<p>Visualization methods have two prefixes: <em>plot_</em> and <em>add_</em>. Whenever a method
begins with <em>plot_</em>, a quick-look product will be created that is not highly-
configurable. These methods are meant to yeild either simple
diagnostic plots or static, often-used products. There are few methods that
use this prefix. The prefix <em>add_</em> is always followed by <em>plot_type</em>; it
indicates a plotting method that is highly configurable and meant to be
combined with other <em>add_</em>-like methods and matplotlib commands.</p>
<p>Common calculations, such as calculating Alfven wave speeds of MHD results,
are strewn about PyBats’ classes. They are always given the method prefix
<em>calc_</em>, i.e. <em>calc_alfven</em>. Methods called <em>calc_all</em> will search for all
class methods with the <em>calc_</em> prefix and call them.</p>
</section>
<section id="submodules">
<h4>Submodules<a class="headerlink" href="#submodules" title="Link to this heading">¶</a></h4>
<p>There are submodules for most models included within the SWMF. The classes
and methods contained within are code-specific, yielding power and
convenience at the cost of flexibility. A few of the submodules are helper
modules- they are not code specific, but rather provide functionality not
related to an SWMF-included code. See <a class="reference internal" href="autosummary/spacepy.pybats.html#module-spacepy.pybats" title="spacepy.pybats"><code class="xref py py-obj docutils literal notranslate"><span class="pre">pybats</span> <span class="pre">API</span></code></a>.</p>
</section>
<section id="top-level-classes-functions">
<h4>Top-Level Classes & Functions<a class="headerlink" href="#top-level-classes-functions" title="Link to this heading">¶</a></h4>
<p>Top-level PyBats classes handle common-format input and output from the SWMF
and are very flexible. However, they do little beyond open files for the user.</p>
<p>There are several functions found in the top-level module. These are mostly
convenience functions for customizing plots.</p>
</section>
</section>
<section id="class-idlfile">
<h3><a class="toc-backref" href="#id4" role="doc-backlink">class IdlFile</a><a class="headerlink" href="#class-idlfile" title="Link to this heading">¶</a></h3>
<p>The IdlFile class of pybats.</p>
<section id="id1">
<h4>Introduction<a class="headerlink" href="#id1" title="Link to this heading">¶</a></h4>
<p>An object class that reads/parses an IDL-formatted output file from the
SWMF and places it into a <a class="reference internal" href="autosummary/spacepy.pybats.PbData.html#spacepy.pybats.PbData" title="spacepy.pybats.PbData"><code class="xref py py-class docutils literal notranslate"><span class="pre">spacepy.pybats.PbData</span></code></a> object.</p>
<p>Usage:
>>>data = spacepy.pybats.IdlFile(‘binary_file.out’)</p>
<p>See <a class="reference internal" href="autosummary/spacepy.pybats.PbData.html#spacepy.pybats.PbData" title="spacepy.pybats.PbData"><code class="xref py py-class docutils literal notranslate"><span class="pre">spacepy.pybats.PbData</span></code></a> for information on how to explore
data contained within the returned object.</p>
<p>This class serves as a parent class to SWMF component-specific derivative
classes that do more preprocessing of the data before returning the
object. Hence, using this class to read binary files is typically not
the most efficient way to proceed. Look for a PyBats sub module that suits
your specific needs, or use this base object to write your own.</p>
</section>
<section id="multi-frame-files">
<h4>Multi-Frame Files<a class="headerlink" href="#multi-frame-files" title="Link to this heading">¶</a></h4>
<p>Typically, Idl-formatted data has a single <em>frame</em>, or a single snapshot
worth of data (a <code class="docutils literal notranslate"><span class="pre">*.out</span></code> file). However, it is possible to (externally)
combine many of these files together such that a time series of data frames
are contained within a single file (<code class="docutils literal notranslate"><span class="pre">*.outs</span></code> files). This class can read
these files, but only one data frame can be made available at a time. This
prevents very large memory requirements.</p>
<p>These files are opened and handled similarly to regular IDL-formatted
files with some important differences. Upon instantiation, the user
may select which data frame to open with the <em>iframe</em> kwarg. The default
action is to open the first frame. The user may learn more about the
number of frames within the file and their associated epoch/iteration
information by examining the top level <em>attrs</em> (see below.)
The user may switch to any arbitrary frame using the <code class="docutils literal notranslate"><span class="pre">switch_frame(iframe)</span></code>
object method. This will load the relevant data from disk into the
object, overwriting the previous contents.</p>
<p>If the user has created any new variables using the data from a certain
data frame, those new values will not be updated automatically. An
exception is for any <em>self.calc_</em> type methods that are set to update
automatically.</p>
</section>
<section id="top-level-attributes">
<h4>Top Level Attributes<a class="headerlink" href="#top-level-attributes" title="Link to this heading">¶</a></h4>
<p>Critical file information can be found via the top-level object attributes
(e.g., accessing the dictionary <code class="docutils literal notranslate"><span class="pre">self.attrs</span></code>). All IdlFile objects and
child classes begin with at least these attributes:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 25%" />
<col style="width: 75%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>Attribute Name</p></th>
<th class="head"><p>Description</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>file</p></td>
<td><p>Path/Name of file represented by object</p></td>
</tr>
<tr class="row-odd"><td><p>iter/time/runtime</p></td>
<td><p>Iteration/datetime/runtime of the current frame</p></td>
</tr>
<tr class="row-even"><td><p>iters/time/runtimes</p></td>
<td><p>Lists of all iterations/times of each data frame</p></td>
</tr>
<tr class="row-odd"><td><p>range</p></td>
<td><p>The range of iterations/epochs covered in file</p></td>
</tr>
<tr class="row-even"><td><p>ndim</p></td>
<td><p>Number of spatial dimensions covered by the data</p></td>
</tr>
<tr class="row-odd"><td><p>nframe</p></td>
<td><p>The total number of data frame within the file</p></td>
</tr>
<tr class="row-even"><td><p>iframe</p></td>
<td><p>The current frame loaded (zero-based)</p></td>
</tr>
<tr class="row-odd"><td><p>format</p></td>
<td><p>The format of the file, either binary or ascii</p></td>
</tr>
<tr class="row-even"><td><p>header</p></td>
<td><p>The raw string header of the file</p></td>
</tr>
</tbody>
</table>
</section>
<section id="notes">
<h4>Notes<a class="headerlink" href="#notes" title="Link to this heading">¶</a></h4>
<p>PyBats assumes little endian byte ordering because
this is what most machines use. However, there is an autodetect feature
such that, if PyBats doesn’t make sense of the first read (a record length
entry, or RecLen), it will proceed using big endian ordering. If this
doesn’t work, the error will manifest itself through the “struct” package
as an “unpack requires a string of argument length ‘X’”.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 0.5.0: </span>Unstructured data are presented as in the files. When reading
3D magnetosphere files, this preserves the 3D block structure,
as required for the BATSRUS interpolator in the <a class="reference external" href="https://github.com/nasa/kamodo">Kamodo
Heliophysics model readers package</a>. Before 0.5.0, binary
unstructured data were sorted in an attempt to put nearby
positions close to each other in the data arrays. This sorting
was nondeterministic and has been removed; see
<a class="reference internal" href="autosummary/spacepy.pybats.bats.Bats2d.html#spacepy.pybats.bats.Bats2d.extract" title="spacepy.pybats.bats.Bats2d.extract"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extract()</span></code></a> and
<a class="reference internal" href="autosummary/spacepy.pybats.qotree.QTree.html#spacepy.pybats.qotree.QTree" title="spacepy.pybats.qotree.QTree"><code class="xref py py-class docutils literal notranslate"><span class="pre">QTree</span></code></a> for processing
adjacent cells. (ASCII data were never sorted.)</p>
</div>
</section>
</section>
<section id="class-imfinput">
<h3><a class="toc-backref" href="#id5" role="doc-backlink">class ImfInput</a><a class="headerlink" href="#class-imfinput" title="Link to this heading">¶</a></h3>
<p>A class to read, write, manipulate, and visualize solar wind upstream
input files for SWMF simulations. More about such files can be found
in the SWMF/BATS-R-US documentation for the \#SOLARWINDFILE command.</p>
<p>Creating an <a class="reference internal" href="autosummary/spacepy.pybats.ImfInput.html#spacepy.pybats.ImfInput" title="spacepy.pybats.ImfInput"><code class="xref py py-class docutils literal notranslate"><span class="pre">ImfInput</span></code></a> object is simple:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">spacepy</span> <span class="kn">import</span> <span class="n">pybats</span>
<span class="gp">>>> </span><span class="n">obj</span><span class="o">=</span><span class="n">pybats</span><span class="o">.</span><span class="n">ImfInput</span><span class="p">(</span><span class="n">filename</span><span class="o">=</span><span class="s1">'test.dat'</span><span class="p">,</span> <span class="n">load</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<p>Upon instantiation, if <em>filename</em> is a valid file AND kwarg <em>load</em> is set
to boolean True, the contents of <em>filename</em> are loaded into the object
and no other work needs to be done.</p>
<p>If <em>filename</em> is False or <em>load</em> is False, a blank <a class="reference internal" href="autosummary/spacepy.pybats.ImfInput.html#spacepy.pybats.ImfInput" title="spacepy.pybats.ImfInput"><code class="xref py py-class docutils literal notranslate"><span class="pre">ImfInput</span></code></a> file
is created for the user to manipulate.
The user can set the time array and the
associated data values (see <em>obj.attrs[‘var’]</em> for a list) to any values
desired and use the method <em>obj.write()</em> to dump the contents to an SWMF
formatted input file. See the documentation for the write method for
more details.</p>
<p>Like most <a class="reference internal" href="autosummary/spacepy.pybats.html#module-spacepy.pybats" title="spacepy.pybats"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pybats</span></code></a> objects, you may interact with
<a class="reference internal" href="autosummary/spacepy.pybats.ImfInput.html#spacepy.pybats.ImfInput" title="spacepy.pybats.ImfInput"><code class="xref py py-class docutils literal notranslate"><span class="pre">ImfInput</span></code></a> objects as if they were specialized dictionaries.
Access data like so:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">obj</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
<span class="go">['bx', 'by', 'bz', 'vx', 'vy', 'vz', 'n', 't']</span>
<span class="gp">>>> </span><span class="n">density</span><span class="o">=</span><span class="n">obj</span><span class="p">[</span><span class="s1">'n'</span><span class="p">]</span>
</pre></div>
</div>
<p>Adding new data entries is equally simple so long as you have the values
and the name for the values:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">>>> </span><span class="n">u</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">obj</span><span class="p">[</span><span class="s1">'ux'</span><span class="p">]</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">obj</span><span class="p">[</span><span class="s1">'uy'</span><span class="p">]</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">obj</span><span class="p">[</span><span class="s1">'uz'</span><span class="p">]</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">obj</span><span class="p">[</span><span class="s1">'u'</span><span class="p">]</span><span class="o">=</span><span class="n">u</span>
</pre></div>
</div>
<p>If new data entries are added as <a class="reference internal" href="autosummary/spacepy.datamodel.dmarray.html#spacepy.datamodel.dmarray" title="spacepy.datamodel.dmarray"><code class="xref py py-class docutils literal notranslate"><span class="pre">dmarray</span></code></a>
objects, the <code class="docutils literal notranslate"><span class="pre">label</span></code> and <code class="docutils literal notranslate"><span class="pre">units</span></code> attributes can be set to enhance plotting.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">spacepy</span> <span class="kn">import</span> <span class="n">datamodel</span>
<span class="gp">>>> </span><span class="n">u</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">obj</span><span class="p">[</span><span class="s1">'ux'</span><span class="p">]</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">obj</span><span class="p">[</span><span class="s1">'uy'</span><span class="p">]</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">obj</span><span class="p">[</span><span class="s1">'uz'</span><span class="p">]</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">obj</span><span class="p">[</span><span class="s1">'u'</span><span class="p">]</span><span class="o">=</span> <span class="n">datamodel</span><span class="o">.</span><span class="n">dmarray</span><span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="p">{</span><span class="s1">'units'</span><span class="p">:</span> <span class="s1">'$km/s$'</span><span class="p">,</span> <span class="s1">'label'</span><span class="p">:</span> <span class="s1">'|U|'</span><span class="p">})</span>
</pre></div>
</div>
<section id="concerning-variable-naming-order">
<h4>Concerning Variable Naming & Order<a class="headerlink" href="#concerning-variable-naming-order" title="Link to this heading">¶</a></h4>
<p>By default, IMF files contain the following variables in this order:</p>
<p>Year, month, day, hour, minute, second, millisecond, bx, by, bz,
vx, vy, vz, n, t</p>
<p>If the variable order changes, or if new state variables are included
(e.g., species-specific densities for multi-ion simulations), the
#VAR entry must be included in the solar wind file. While the SWMF
documentation refers to density and temperature values as having names
‘dens’ or ‘temp’, <strong>only ‘n’, ‘t’, or MHD state variable names as defined
in the BATS-R-US equation file are accepted.</strong>. In multi-ion simulations,
‘n’ is the total number density; all other densities must sum to ‘n’.</p>
<p>To illustrate, consider this example of converting a single fluid input
file to a multi-fluid input file where density is split evenly across
two ion species: a solar wind fluid and an ionosphere fluid that has
minimal density upstream of the Earth. Each fluid has an explicit state
variable name defined in the BATS-R-US equation module that configures the
multi-fluid configuration.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">spacepy</span> <span class="kn">import</span> <span class="n">pybats</span><span class="p">,</span> <span class="n">datamodel</span>
<span class="gp">>>> </span><span class="n">imf</span> <span class="o">=</span> <span class="n">pybats</span><span class="o">.</span><span class="n">ImfInput</span><span class="p">(</span><span class="s1">'tests/data/pybats_test/imf_single.dat'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">imf</span><span class="p">[</span><span class="s1">'IonoRho'</span><span class="p">]</span> <span class="o">=</span> <span class="n">datamodel</span><span class="o">.</span><span class="n">dmarray</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">imf</span><span class="p">[</span><span class="s1">'n'</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span> <span class="o">+</span> <span class="mf">0.01</span><span class="p">,</span>
<span class="gp">... </span><span class="p">{</span><span class="s1">'units'</span><span class="p">:</span> <span class="s1">'$cm^{-3}$'</span><span class="p">,</span> <span class="s1">'label'</span><span class="p">:</span> <span class="s1">'$</span><span class="se">\\</span><span class="s1">rho_</span><span class="si">{Iono}</span><span class="s1">$'</span><span class="p">})</span>
<span class="gp">>>> </span><span class="n">imf</span><span class="p">[</span><span class="s1">'SwRho'</span><span class="p">]</span> <span class="o">=</span> <span class="n">imf</span><span class="p">[</span><span class="s1">'n'</span><span class="p">]</span> <span class="o">-</span> <span class="n">imf</span><span class="p">[</span><span class="s1">'IonoRho'</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">imf</span><span class="p">[</span><span class="s1">'SwRho'</span><span class="p">]</span><span class="o">.</span><span class="n">attrs</span><span class="p">[</span><span class="s1">'label'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'$</span><span class="se">\\</span><span class="s1">rho_</span><span class="si">{Sw}</span><span class="s1">$'</span>
<span class="gp">>>> </span><span class="n">imf</span><span class="o">.</span><span class="n">quicklook</span><span class="p">(</span> <span class="p">[</span><span class="s1">'bz'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'n'</span><span class="p">,</span> <span class="s1">'SwRho'</span><span class="p">,</span> <span class="s1">'IonoRho'</span><span class="p">],</span> <span class="s1">'pram'</span><span class="p">])</span>
</pre></div>
</div>
<table class="docutils align-default">
<tbody>
<tr class="row-odd"><td><p>Kwarg</p></td>
<td><p>Description</p></td>
</tr>
<tr class="row-even"><td><p>filename</p></td>
<td><p>Set the input/output file name.</p></td>
</tr>
<tr class="row-odd"><td><p>load</p></td>
<td><p>Read file upon instantiation? Defaults to <strong>True</strong></p></td>
</tr>
<tr class="row-even"><td><p>npoints</p></td>
<td><p>For empty data sets, sets number of points (default is 0)</p></td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 0.5.0: </span>Default variable names for temperature and density are now ‘t’ and ‘n’.</p>
</div>
</section>
</section>
</section>
<section id="bats-submodule">
<h2><a class="toc-backref" href="#id6" role="doc-backlink">Bats Submodule</a><a class="headerlink" href="#bats-submodule" title="Link to this heading">¶</a></h2>
<section id="bats2d">
<h3><a class="toc-backref" href="#id7" role="doc-backlink">Bats2d</a><a class="headerlink" href="#bats2d" title="Link to this heading">¶</a></h3>
<p>A child class of <a class="reference internal" href="autosummary/spacepy.pybats.IdlFile.html#spacepy.pybats.IdlFile" title="spacepy.pybats.IdlFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">IdlFile</span></code></a> tailored to 2D BATS-R-US output.</p>
<section id="calculations">
<h4>Calculations<a class="headerlink" href="#calculations" title="Link to this heading">¶</a></h4>
<p>New values can be added via the addition of new keys. For example,
a user could add radial distance to an equatorial Bats2d object as follows:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">spacepy.pybats</span> <span class="kn">import</span> <span class="n">bats</span>
<span class="gp">>>> </span><span class="n">mhd</span> <span class="o">=</span> <span class="n">bats</span><span class="o">.</span><span class="n">Bats2d</span><span class="p">(</span><span class="s1">'tests/data/pybats_test/z=0_sine.out'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">mhd</span><span class="p">[</span><span class="s1">'rad'</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span> <span class="n">mhd</span><span class="p">[</span><span class="s1">'x'</span><span class="p">]</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">mhd</span><span class="p">[</span><span class="s1">'y'</span><span class="p">]</span><span class="o">**</span><span class="mi">2</span> <span class="p">)</span>
</pre></div>
</div>
<p>Note, however, that if the user switches the data frame in a .outs file
to access data from a different epoch, these values will need to be
updated.</p>
<p>An exception to this is built-in <code class="docutils literal notranslate"><span class="pre">calc_*</span></code> methods, which perform common
MHD/fluid dynamic calculations (i.e., Alfven wave speed, vorticity, and
more.) These values are updated when the data frame is switched (see the
<code class="docutils literal notranslate"><span class="pre">switch_frame</span></code> method).</p>
<p>For example, to calculate number density and the fraction of the total
density for each species in a multi-fluid simulation,</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">mhd</span> <span class="o">=</span> <span class="n">bats</span><span class="o">.</span><span class="n">Bats2d</span><span class="p">(</span><span class="s1">'tests/data/pybats_test/cut_multispecies.out'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">mhd</span><span class="o">.</span><span class="n">calc_ndens</span><span class="p">()</span>
</pre></div>
</div>
<p>In an interactive session, users can tab-complete to explore the possible
built-in calculation methods.</p>
</section>
<section id="plotting">
<h4>Plotting<a class="headerlink" href="#plotting" title="Link to this heading">¶</a></h4>
<p>While users can employ Matplotlib to plot values, a set of built-in
methods are available to expedite plotting. These are the
<code class="docutils literal notranslate"><span class="pre">add_<plot</span> <span class="pre">type></span></code> methods. These methods always have the following
keyword arguments that allow users to optionally build more complicated
plots: <em>target</em> and <em>loc</em>. The <em>target</em> kwarg tells the plotting method
where to place the plot and can either be a Matplotlib figure or axes
object. If it’s an axes object, <em>loc</em> sets the subplot location using
the typical matplotlib syntax (e.g., <code class="docutils literal notranslate"><span class="pre">loc=121</span></code>). The default behavior is
to create a new figure and axes object.</p>
<p>This approach allows a user to over-plot contours, field lines, and
other plot artists as well as combine different subplots onto a single
figure. Continuing with our example above, let’s plot the grid layout
for our file as well as equatorial pressure and flow streamlines:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="gp">>>> </span><span class="n">fig</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">Figure</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">8</span><span class="p">,</span><span class="mi">6</span><span class="p">))</span>
<span class="gp">>>> </span><span class="n">mhd</span><span class="o">.</span><span class="n">add_grid_plot</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">fig</span><span class="p">,</span> <span class="n">loc</span><span class="o">=</span><span class="mi">121</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">mhd</span><span class="o">.</span><span class="n">add_contour</span><span class="p">(</span><span class="s1">'x'</span><span class="p">,</span><span class="s1">'y'</span><span class="p">,</span><span class="s1">'p'</span><span class="p">,</span> <span class="n">target</span><span class="o">=</span><span class="n">fig</span><span class="p">,</span> <span class="n">loc</span><span class="o">=</span><span class="mi">122</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">mhd</span><span class="o">.</span><span class="n">add_stream_scatter</span><span class="p">(</span><span class="s1">'ux'</span><span class="p">,</span> <span class="s1">'uy'</span><span class="p">,</span> <span class="n">target</span><span class="o">=</span><span class="n">fig</span><span class="p">,</span> <span class="n">loc</span><span class="o">=</span><span class="mi">122</span><span class="p">)</span>
</pre></div>
</div>
<p>Useful plotting methods include the following:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 25%" />
<col style="width: 75%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>Plot Method</p></th>
<th class="head"><p>Description</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>add_grid_plot</p></td>
<td><p>Create a quick-look diagram of the grid layout</p></td>
</tr>
<tr class="row-odd"><td><p>add_contour</p></td>
<td><p>Create a contour plot of a given variable</p></td>
</tr>
<tr class="row-even"><td><p>add_pcolor</p></td>
<td><p>Add a p-color (no-interpolation contour) plot</p></td>
</tr>
<tr class="row-odd"><td><p>add_stream_scatter</p></td>
<td><p>Scatter stream traces (any vector field)</p></td>
</tr>
<tr class="row-even"><td><p>add_b_magsphere</p></td>
<td><p>Add magnetic field lines for X-Z plane cuts</p></td>
</tr>
<tr class="row-odd"><td><p>add_planet</p></td>
<td><p>Add a simple black/white planet at the origin</p></td>
</tr>
<tr class="row-even"><td><p>add_body</p></td>
<td><p>Add an inner boundary at the origin</p></td>
</tr>
</tbody>
</table>
</section>
<section id="extracting-and-stream-tracing">
<h4>Extracting and Stream Tracing<a class="headerlink" href="#extracting-and-stream-tracing" title="Link to this heading">¶</a></h4>
<p>Extracting values via interpolation to arbitrary points and creating
stream traces through any vector field (e.g., velocity or magnetic field)
are aided via the use of the following object methods:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 25%" />
<col style="width: 75%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>Method</p></th>
<th class="head"><p>Description</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>extract</p></td>
<td><p>Interpolate to arbitrary points and extract values</p></td>
</tr>
<tr class="row-odd"><td><p>get_stream</p></td>
<td><p>Integrate stream lines through vector fields</p></td>
</tr>
</tbody>
</table>
<p>Be sure to read the docstring information of <a class="reference internal" href="autosummary/spacepy.pybats.IdlFile.html#spacepy.pybats.IdlFile" title="spacepy.pybats.IdlFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">IdlFile</span></code></a> to
see how to handle multi-frame files (.outs) and for a list of critical
attributes.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 0.5.0: </span>Unstructured data are now presented as in the files. See
<a class="reference internal" href="autosummary/spacepy.pybats.IdlFile.html#spacepy.pybats.IdlFile" title="spacepy.pybats.IdlFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">IdlFile</span></code></a> for details.</p>
</div>
</section>
</section>
</section>
</section>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="index.html">
<img class="logo" src="_static/logo.png" alt="Logo"/>
</a></p>
<div>
<h3><a href="index.html">Table of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">PyBats - SWMF & BATS-R-US Analysis Tools</a><ul>
<li><a class="reference internal" href="#pybats-module">PyBats Module</a><ul>
<li><a class="reference internal" href="#pybats">pybats</a><ul>
<li><a class="reference internal" href="#introduction">Introduction</a></li>
<li><a class="reference internal" href="#organization">Organization</a></li>
<li><a class="reference internal" href="#conventions-and-prefixes">Conventions and Prefixes</a></li>
<li><a class="reference internal" href="#submodules">Submodules</a></li>
<li><a class="reference internal" href="#top-level-classes-functions">Top-Level Classes & Functions</a></li>
</ul>
</li>
<li><a class="reference internal" href="#class-idlfile">class IdlFile</a><ul>
<li><a class="reference internal" href="#id1">Introduction</a></li>
<li><a class="reference internal" href="#multi-frame-files">Multi-Frame Files</a></li>
<li><a class="reference internal" href="#top-level-attributes">Top Level Attributes</a></li>
<li><a class="reference internal" href="#notes">Notes</a></li>
</ul>
</li>
<li><a class="reference internal" href="#class-imfinput">class ImfInput</a><ul>
<li><a class="reference internal" href="#concerning-variable-naming-order">Concerning Variable Naming & Order</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#bats-submodule">Bats Submodule</a><ul>
<li><a class="reference internal" href="#bats2d">Bats2d</a><ul>
<li><a class="reference internal" href="#calculations">Calculations</a></li>
<li><a class="reference internal" href="#plotting">Plotting</a></li>
<li><a class="reference internal" href="#extracting-and-stream-tracing">Extracting and Stream Tracing</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<div>
<h4>Previous topic</h4>
<p class="topless"><a href="omni.html"
title="previous chapter">OMNI</a></p>
</div>
<div>
<h4>Next topic</h4>
<p class="topless"><a href="pycdf.html"
title="next chapter">pycdf - Python interface to CDF files</a></p>
</div>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/pybats.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<search id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="pycdf.html" title="pycdf - Python interface to CDF files"
>next</a> |</li>
<li class="right" >
<a href="omni.html" title="OMNI"
>previous</a> |</li>
<li><a href="https://spacepy.github.io/"">homepage</a>| </li>
<li><a href="https://github.com/spacepy/spacepy">development</a>| </li>
<li><a href="search.html">search</a>| </li>
<li><a href="index.html">documentation </a> »</li>
<li class="nav-item nav-item-this"><a href="">PyBats - SWMF & BATS-R-US Analysis Tools</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2011-2024, The SpacePy Team.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.3.7.
</div>
</body>
</html>