Repository: PlasmaPy/PlasmaPy

Issue:
Computation of gyroradius from Plasmapy, in case of missing temperature should return NaNs instead of ValueError
<!--

Thanks for opening an issue! To help us react appropriately to your report,
please first look through https://github.com/PlasmaPy/PlasmaPy/issues and check
whether no other issue already describes your problem.

For more general "how do I do X?" type questions, please speak to us in real
time on https://app.element.io/#/room/#plasmapy:openastronomy.org or ask on
GitHub Discussions: https://github.com/plasmapy/plasmapy/discussions . We'll be
more than happy to help! :)

-->

## Feature request

<!--

Please include details of the feature you would like to see, why you would
like to see it/the use case

-->
At present if I do the following, a ```ValueError``` is raised:
```python
>>> from plasmapy.formulary.parameters import gyroradius
>>> gyroradius(5 * u.nT, particle='p+', T_i=numpy.nan * u.K)
ValueError: Must give Vperp or T_i, but not both, as arguments to gyroradius
```
which is the wrong kind of error being raised. Error arises because temperature (```T_i```) data is NaN and not because the user is providing both ```T_i``` and ```Vperp```., as the text accompanying the error claims.

Another problem with this is that if we are trying to calculate gyroradius from a spacecraft data and at at any specific time, if both the magnetic field and (```B```) and ```T_i``` or ```Vperp``` data are missing, it raises the ```ValueError``` and stops the code from executing.

If instead of doing that, the code has the option to just set such values of gyroradius to ```numpy.nan``` (as is already happening when ```B``` is ```numpy.nan```), it increases its functionality and doesn't force the user to feed clean data.

I have also attached a screenshot of the error!
![gyroradius_error](https://user-images.githubusercontent.com/25327981/108413585-0d055b80-71e0-11eb-9902-9c679e974805.png)


Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/PlasmaPy/PlasmaPy/pull/1187?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PlasmaPy) Report
> :exclamation: No coverage uploaded for pull request base (`main@5f5717c`). [Click here to learn what that means](https://docs.codecov.io/docs/error-reference?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PlasmaPy#section-missing-base-commit).
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/PlasmaPy/PlasmaPy/pull/1187/graphs/tree.svg?width=650&height=150&src=pr&token=LzZ5mnXVad&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PlasmaPy)](https://codecov.io/gh/PlasmaPy/PlasmaPy/pull/1187?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PlasmaPy)

```diff
@@           Coverage Diff           @@
##             main    #1187   +/-   ##
=======================================
  Coverage        ?   96.98%           
=======================================
  Files           ?       71           
  Lines           ?     7022           
  Branches        ?        0           
=======================================
  Hits            ?     6810           
  Misses          ?      212           
  Partials        ?        0           
```



------

[Continue to review full report at Codecov](https://codecov.io/gh/PlasmaPy/PlasmaPy/pull/1187?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PlasmaPy).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PlasmaPy)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/PlasmaPy/PlasmaPy/pull/1187?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PlasmaPy). Last update [5f5717c...3f1d96d](https://codecov.io/gh/PlasmaPy/PlasmaPy/pull/1187?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PlasmaPy). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PlasmaPy).

alfkoehn: @StanczakDominik I don't understand why the `pre-commit.ci` fails, it says

`black....................................................................Failed`
`- hook id: black`
`- files were modified by this hook`
`reformatted plasmapy/formulary/tests/test_parameters.py`

I have no idea what was actually reformatted, so I don't know what to change to let this check pass... Any hints are greatly appreciated 
StanczakDominik: Iirc the magic incantation for this is...

pre-commit.ci autofix
alfkoehn: No idea about magic incantation, sorry :D
StanczakDominik: Hah, sorry! This part is a bit new, and we need to document it better.

Patch:
diff --git a/changelog/1187.feature.rst b/changelog/1187.feature.rst
new file mode 100644
index 00000000..06f5cd6c
--- /dev/null
+++ b/changelog/1187.feature.rst
@@ -0,0 +1,3 @@
+The behaviour of the function `~plasmapy.formulary.parameters.gyroradius` has
+been changed: if NaN-values are provided for T_i or Vperp, no longer is a
+slightly misleading error raised, but simply NaN is returned.
diff --git a/plasmapy/formulary/parameters.py b/plasmapy/formulary/parameters.py
index 1335175c..60f2f7ea 100644
--- a/plasmapy/formulary/parameters.py
+++ b/plasmapy/formulary/parameters.py
@@ -1198,7 +1198,7 @@ def gyroradius(
 
     # check 1: ensure either Vperp or T_i invalid, keeping in mind that
     # the underlying values of the astropy quantity may be numpy arrays
-    if np.any(np.logical_not(np.logical_xor(isfinite_Vperp, isfinite_Ti))):
+    if np.any(np.logical_and(isfinite_Vperp, isfinite_Ti)):
         raise ValueError(
             "Must give Vperp or T_i, but not both, as arguments to gyroradius"
         )


Test patch:
diff --git a/plasmapy/formulary/tests/test_parameters.py b/plasmapy/formulary/tests/test_parameters.py
index 6f9bfd19..fcbb60d4 100644
--- a/plasmapy/formulary/tests/test_parameters.py
+++ b/plasmapy/formulary/tests/test_parameters.py
@@ -802,6 +802,11 @@ def test_gyroradius():
 
     assert gyroradius(B, "e-", Vperp=25 * u.m / u.s).unit.is_equivalent(u.m)
 
+    # test for possiblity to allow nan for input values
+    assert np.isnan(gyroradius(np.nan * u.T, particle="e-", T_i=1 * u.K))
+    assert np.isnan(gyroradius(1 * u.T, particle="e-", T_i=np.nan * u.K))
+    assert np.isnan(gyroradius(1 * u.T, particle="e-", Vperp=np.nan * u.m / u.s))
+
     Vperp = 1e6 * u.m / u.s
     Bmag = 1 * u.T
     omega_ce = gyrofrequency(Bmag, "e-")