Skip to content

Commit

Permalink
Fixed docstrings, examples
Browse files Browse the repository at this point in the history
  • Loading branch information
nickssl committed Apr 17, 2024
1 parent 8ae8dd1 commit b65ff85
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 50 deletions.
53 changes: 28 additions & 25 deletions pytplot/replace_data.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
# Copyright 2020 Regents of the University of Colorado. All Rights Reserved.
# Released under the MIT license.
# This software was developed at the University of Colorado's Laboratory for Atmospheric and Space Physics.
# Verify current version before use at: https://github.com/MAVENSDC/PyTplot

import pytplot
import numpy as np
from pytplot import tplot_utilities as utilities
from copy import deepcopy
from collections import OrderedDict
import logging


def replace_data(tplot_name, new_data):
"""
This function will replace all of the data in a tplot variable
Parameters:
tplot_name : str
The name of the tplot variable
new_data : np array (or something that can be converted to a np.array)
The data to replace the
Returns:
None
Examples:
>>> # Copy Variable 1 into a new Variable 2
>>> import pytplot
>>> pytplot.replace_data("Variable1", [[1,2,3,4],[5,6,7,8]])
This function will replace all of the data in a tplot variable.
Parameters
----------
tplot_name : str
The name of the tplot variable.
new_data : array_like
The new data to replace the existing data.
This can be any object that can be converted into an np.array.
Returns
-------
None
Examples
--------
>>> # Replace data into an existing variable
>>> import pytplot
>>> pytplot.store_data("v1", data={'x':[1,2,3,4],'y':[1,2,3,4]})
>>> print(pytplot.get_data('v1'))
>>> pytplot.replace_data("v1",[5,6,7,8])
>>> print(pytplot.get_data('v1'))
"""

# if old name input is a number, convert to corresponding name
Expand All @@ -44,11 +43,15 @@ def replace_data(tplot_name, new_data):
shape_old = pytplot.data_quants[tplot_name].values.shape
shape_new = new_data_np.shape
if shape_old != shape_new:
logging.info(f"Dimensions do not match for replace data. {shape_new} does not equal {shape_old}. Returning...")
logging.info(
f"Dimensions do not match for replace data. {shape_new} does not equal {shape_old}. Returning..."
)
return

pytplot.data_quants[tplot_name].values = new_data_np

pytplot.data_quants[tplot_name].attrs['plot_options']['yaxis_opt']['y_range'] = utilities.get_y_range(pytplot.data_quants[tplot_name])
pytplot.data_quants[tplot_name].attrs["plot_options"]["yaxis_opt"]["y_range"] = (
utilities.get_y_range(pytplot.data_quants[tplot_name])
)

return
48 changes: 23 additions & 25 deletions pytplot/replace_metadata.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
# Copyright 2020 Regents of the University of Colorado. All Rights Reserved.
# Released under the MIT license.
# This software was developed at the University of Colorado's Laboratory for Atmospheric and Space Physics.
# Verify current version before use at: https://github.com/MAVENSDC/PyTplot

import pytplot
import numpy as np
from pytplot import tplot_utilities as utilities
from copy import deepcopy
from collections import OrderedDict
import logging


def replace_metadata(tplot_name, new_metadata):
"""
This function will replace all the metadata in a tplot variable
Parameters:
tplot_name : str
The name of the tplot variable
new_metadata : A dictionary with metadata values. A deep copy will be performed so that
no references to new_metadata are retained.
Returns:
None
Examples:
>>> # Copy Variable 1 metadata into Variable 2, which must already exist
>>> import pytplot
>>> meta = pytplot_get_data('Variable1', metadata=True)
>>> pytplot.replace_metadata("Variable2", meta)
This function will replace all the metadata in a tplot variable.
Parameters
----------
tplot_name : str
The name of the tplot variable.
new_metadata : dict
A dictionary with metadata values. A deep copy will be performed so that
no references to new_metadata are retained.
Returns
-------
None
Examples
--------
>>> # Copy Variable 1 metadata into Variable 2, which must already exist
>>> import pytplot
>>> meta = pytplot_get_data('Variable1', metadata=True)
>>> pytplot.replace_metadata("Variable2", meta)
"""

# if old name input is a number, convert to corresponding name
Expand All @@ -42,6 +38,8 @@ def replace_metadata(tplot_name, new_metadata):
return

pytplot.data_quants[tplot_name].attrs = deepcopy(new_metadata)
pytplot.data_quants[tplot_name].attrs['plot_options']['yaxis_opt']['y_range'] = utilities.get_y_range(pytplot.data_quants[tplot_name])
pytplot.data_quants[tplot_name].attrs["plot_options"]["yaxis_opt"]["y_range"] = (
utilities.get_y_range(pytplot.data_quants[tplot_name])
)

return

0 comments on commit b65ff85

Please sign in to comment.