From d0fc1fbafc0906dc68c4ae14c72a4f72ef7c6773 Mon Sep 17 00:00:00 2001 From: Qusai Al Shidi Date: Mon, 10 Aug 2020 12:53:57 -0400 Subject: [PATCH] Added interp_nans to tools.py --- DOCUMENTATION.markdown | 22 ++++++++++++++++++++++ swmfpy/tools.py | 18 ++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/DOCUMENTATION.markdown b/DOCUMENTATION.markdown index a7414ad..ec31d36 100644 --- a/DOCUMENTATION.markdown +++ b/DOCUMENTATION.markdown @@ -15,6 +15,7 @@ * [replace\_command](#.swmfpy.paramin.replace_command) * [read\_command](#.swmfpy.paramin.read_command) * [swmfpy.tools](#.swmfpy.tools) + * [interp\_nans](#.swmfpy.tools.interp_nans) * [carrington\_rotation\_number](#.swmfpy.tools.carrington_rotation_number) * [swmfpy.tecplottools](#.swmfpy.tecplottools) * [apply\_equations](#.swmfpy.tecplottools.apply_equations) @@ -500,6 +501,27 @@ values for the parameters. Tools to be used in swmfpy functions and classes. Some of the functions are *hidden functions*. + +#### interp\_nans + +```python +interp_nans(x_vals, y_vals) +``` + +Returns a numpy array with NaNs interpolated. + +**Arguments**: + + x_vals (np.array): + x values to interpolate. + y_vals (np.array): + y values including NaNs. + + +**Returns**: + +- `(np.array)` - numpy array with NaNs interpolated. + #### carrington\_rotation\_number diff --git a/swmfpy/tools.py b/swmfpy/tools.py index 08b6b88..ab9c269 100644 --- a/swmfpy/tools.py +++ b/swmfpy/tools.py @@ -6,6 +6,24 @@ __author__ = 'Qusai Al Shidi' __email__ = 'qusai@umich.edu' import datetime as dt +import numpy as np + + +def interp_nans(x_vals, y_vals): + """Returns a numpy array with NaNs interpolated. + + Args: + x_vals (np.array): + x values to interpolate. + y_vals (np.array): + y values including NaNs. + + Returns: + (np.array): numpy array with NaNs interpolated. + """ + + nonans = np.nonzero(np.isnan(y_vals) == 0) + return np.interp(x_vals, x_vals[nonans], y_vals[nonans]) def carrington_rotation_number(the_time='now'): -- GitLab