Skip to content

Commit

Permalink
Merge pull request #205 from pysat/tst/34_omni
Browse files Browse the repository at this point in the history
TST: routines for omni
  • Loading branch information
jklenzing authored Sep 8, 2023
2 parents c8514d6 + 2d7fa10 commit 8e96fa7
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/).

## [0.X.X] - 2023-XX-XX
* Bug Fix
* New window needs to be integer for calculate_imf_steadiness
* Documentation
* Added example of how to export data for archival
* Maintenance
* Implemented unit tests for cleaning warnings
* Use pip install for readthedocs
* Moved references and acknowledgements to methods files
* Added tests for OMNI HRO routines

## [0.0.5] - 2023-06-27
* New Instruments
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Python 3.6+.
| numpy | | |
| pandas | | |
| requests | | |
| scipy>=1.4.0 | | |
| xarray | | |

## PyPi Installation
Expand Down
1 change: 1 addition & 0 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Python 3.6+ and pysat 3.1.0+.
numpy
pandas
requests
scipy>=1.4.0
xarray
================== =================

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ dependencies = [
"pandas",
"pysat >= 3.1",
"requests",
"scipy >= 1.4",
"xarray"
]

Expand Down
25 changes: 6 additions & 19 deletions pysatNASA/instruments/methods/omni.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def calculate_imf_steadiness(inst, steady_window=15, min_window_frac=0.75,
sample_rate = int(rates[inst.tag])
max_wnum = np.floor(steady_window / sample_rate)
if max_wnum != steady_window / sample_rate:
steady_window = max_wnum * sample_rate
steady_window = int(max_wnum * sample_rate)
pysat.logger.warning(" ".join(("sample rate is not a factor of the",
"statistical window")))
pysat.logger.warning(" ".join(("new statistical window is",
Expand All @@ -141,24 +141,11 @@ def calculate_imf_steadiness(inst, steady_window=15, min_window_frac=0.75,

# Calculate the running circular standard deviation of the clock angle
circ_kwargs = {'high': 360.0, 'low': 0.0, 'nan_policy': 'omit'}
try:
ca_std = \
inst['clock_angle'].rolling(min_periods=min_wnum,
window=steady_window,
center=True).apply(stats.circstd,
kwargs=circ_kwargs,
raw=True)
except TypeError:
pysat.logger.warn(' '.join(['To automatically remove NaNs from the',
'calculation, please upgrade to scipy 1.4',
'or newer.']))
circ_kwargs.pop('nan_policy')
ca_std = \
inst['clock_angle'].rolling(min_periods=min_wnum,
window=steady_window,
center=True).apply(stats.circstd,
kwargs=circ_kwargs,
raw=True)
ca_std = inst['clock_angle'].rolling(min_periods=min_wnum,
window=steady_window,
center=True).apply(stats.circstd,
kwargs=circ_kwargs,
raw=True)
inst['clock_angle_std'] = pds.Series(ca_std, index=inst.data.index)

# Determine how long the clock angle and IMF magnitude are steady
Expand Down
33 changes: 32 additions & 1 deletion pysatNASA/tests/test_omni_hro.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Unit tests for OMNI HRO special functions."""

import datetime as dt
import logging
import numpy as np
import warnings

Expand Down Expand Up @@ -145,7 +146,7 @@ def test_clock_angle_std(self):
return

def test_dayside_recon(self):
"""Test the IMF steadiness standard deviation calculation."""
"""Test the dayside reconnection calculation."""

# Run the clock angle and steadiness routines
omni.calculate_clock_angle(self.test_inst)
Expand All @@ -163,6 +164,36 @@ def test_dayside_recon(self):
assert np.all(test_diff < 1.0e-6)
return

def test_time_shift_to_magnetic_poles(self):
"""Test the time shift routines."""

# Choose values to result in 1 hour shift
self.test_inst['Vx'] = 6371.2
self.test_inst['BSN_x'] = 3600.0

old_index = self.test_inst.index.copy()
omni.time_shift_to_magnetic_poles(self.test_inst)

# Check shifted index
assert (old_index[0] - self.test_inst.index[0]).seconds == 3600
# Check new cadence
assert (self.test_inst.index[1] - self.test_inst.index[0]).seconds == 60
return

def test_calculate_imf_steadiness_warnings(self, caplog):
"""Test imf steadiness routine."""

omni.calculate_clock_angle(self.test_inst)
with caplog.at_level(logging.INFO, logger='pysat'):
omni.calculate_imf_steadiness(self.test_inst, steady_window=5.1,
min_window_frac=0.8)
captured = caplog.text
warn_msgs = ["sample rate is not a factor",
"new statistical window"]
for msg in warn_msgs:
assert msg in captured
return


class TestDeprecation(object):
"""Unit tests for deprecation warnings in `pysat.instrument.omni_hro`."""
Expand Down

0 comments on commit 8e96fa7

Please sign in to comment.