Data object methods§
Ozzy is implemented according to xarray's accessor model1. All the ozzy functionality built on top of xarray data objects (Datasets or DataArrays) can therefore be accessed via
xarray.DataArray.ozzy.<method>
xarray.Dataset.ozzy.<method>
Example
Saving a Dataset object:
import ozzy as oz
ds = oz.Dataset()
ds.ozzy.save('test.h5')
# -> Saved file "test.h5"
# -> 'save' took: 0:00:00.212650
Note for developers
Ozzy's custom methods are defined in two accessor classes:
ozzy.accessors.OzzyDataset
ozzy.accessors.OzzyDataArray
Strictly speaking, the path to each method should be for example ozzy.accessors.[OzzyDataset|OzzyDataArray].<method>
. However, this documentation page presents the methods as if they were under xarray.[Dataset|DataArray].ozzy.<method>
, which is effectively how the user can access them.
The methods in each accessor class can access the actual data object via <data_obj>.ozzy._obj
. This is only relevant when defining new methods in the accessor classes.
Example
import xarray as xr
import ozzy as oz
ds = xr.Dataset()
assert ds == ds.ozzy._obj
# True
Methods§
Bases: *mixins
coord_to_physical_distance
§
coord_to_physical_distance(
coord,
n0,
units="m",
new_coord=None,
new_label=None,
set_as_default=True,
)
Convert a coordinate to physical units based on the plasma density \(n_0\).
This function calculates the skin depth based on the provided n0
value and scales the specified coordinate coord
by the skin depth. The scaled coordinate is assigned a new name (new_coord
or a default name) and added to the dataset as a new coordinate. The new coordinate can also be assigned a custom label (new_label
).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
Name of coordinate to convert. |
required |
|
float
|
Value for the plasma electron density used to calculate the skin depth, in \(\mathrm{cm}^{-3}\). |
required |
|
str
|
The name to assign to the new coordinate. If not provided, a default name is generated based on |
None
|
|
str
|
The label ( |
None
|
|
str
|
The physical units for the new coordinate. Must be either |
'm'
|
|
bool
|
If |
True
|
Returns:
Type | Description |
---|---|
DataArray
|
A new DataArray with the additional converted coordinate. |
Examples:
Converting normalized time units to propagation distance
import ozzy as oz
da = oz.DataArray([3,4,5], coords={'t': [0,1,2]}, dims='t')
da_m = da.ozzy.coord_to_physical_distance('t', 1e18, new_coord='z') # z in m
da_cm = da.ozzy.coord_to_physical_distance('t', 1e18, units='cm', new_coord='z') # z in cm
Convert \(r\) coordinate to centimeters with new label
import ozzy as oz
import numpy as np
da = oz.DataArray({'var': np.random.rand(5, 10)},
coords={'x2': np.linspace(0, 1, 10)})
n0 = 1e17 # cm^-3
da_new = da.ozzy.coord_to_physical_distance('x2', n0, new_coord='r', units='cm')
fft
§
fft(axes=None, dims=None, **kwargs)
Calculate the Fast Fourier Transform (FFT) of a DataArray
along specified dimensions.
Warning
This method has not been thoroughly checked for accuracy yet. Please double-check your results with a different FFT function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
list[int]
|
The integer indices of the axes to take FFT along. |
None
|
|
list[str]
|
Dimensions along which to compute the FFT. If provided, this takes precedence over |
None
|
|
Additional keyword arguments passed to |
{}
|
Returns:
Type | Description |
---|---|
DataArray
|
The FFT result as a new DataArray. |
Examples:
1D FFT
import ozzy as oz
import numpy as np
# Create a 1D DataArray
x = np.linspace(0, 10, 100)
da = oz.DataArray(np.sin(2 * np.pi * x), coords=[x], dims=['x'], pic_data_type='grid')
# Compute the 1D FFT
da_fft = da.ozzy.fft(dims=['x'])
# -> 'fft' took: 0:00:00.085525
2D FFT
import ozzy as oz
import numpy as np
# Create a 2D DataArray
x = np.linspace(0, 10, 100)
y = np.linspace(0, 5, 50)
X, Y = np.meshgrid(x, y)
da = oz.DataArray(np.sin(2 * np.pi * X) * np.cos(2 * np.pi * Y),
coords=[y, x], dims=['y', 'x'], pic_data_type='grid')
# Compute the 2D FFT
da_fft = da.ozzy.fft(dims=['x', 'y'])
# -> 'fft' took: 0:00:00.006278
save
§
save(path)
Save data object to an HDF5 (default) or NetCDF file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The path to save the file to. Specify the file ending as |
required |
Examples:
Save empty DataArray
import ozzy as oz
ds = oz.DataArray()
ds.ozzy.save('empty_file.h5')
# -> Saved file "empty_file.h5"
# -> 'save' took: 0:00:00.197806
Bases: *mixins
coord_to_physical_distance
§
coord_to_physical_distance(
coord,
n0,
units="m",
new_coord=None,
new_label=None,
set_as_default=True,
)
Convert a coordinate to physical units based on the plasma density \(n_0\).
This function calculates the skin depth based on the provided n0
value and scales the specified coordinate coord
by the skin depth. The scaled coordinate is assigned a new name (new_coord
or a default name) and added to the dataset as a new coordinate. The new coordinate can also be assigned a custom label (new_label
).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
Name of coordinate to convert. |
required |
|
float
|
Value for the plasma electron density used to calculate the skin depth, in \(\mathrm{cm}^{-3}\). |
required |
|
str
|
The name to assign to the new coordinate. If not provided, a default name is generated based on |
None
|
|
str
|
The label ( |
None
|
|
str
|
The physical units for the new coordinate. Must be either |
'm'
|
|
bool
|
If |
True
|
Returns:
Type | Description |
---|---|
Dataset
|
A new Dataset with the additional converted coordinate. |
Examples:
Converting normalized time units to propagation distance
import ozzy as oz
ds = oz.Dataset(data_vars={'var1': [3,4,5]}, coords={'t': [0,1,2]}, dims='t')
ds_m = ds.ozzy.coord_to_physical_distance('t', 1e18, new_coord='z') # z in m
ds_cm = ds.ozzy.coord_to_physical_distance('t', 1e18, units='cm', new_coord='z') # z in cm
Convert \(r\) coordinate to centimeters with new label
import ozzy as oz
import numpy as np
ds = oz.Dataset({'var': np.random.rand(5, 10)},
coords={'x2': np.linspace(0, 1, 10)})
n0 = 1e17 # cm^-3
ds_new = ds.ozzy.coord_to_physical_distance('x2', n0, new_coord='r', units='cm')
fft
§
fft(data_var, axes=None, dims=None, **kwargs)
Calculate the Fast Fourier Transform (FFT) of a variable in a Dataset
along specified dimensions. Take FFT of variable in Dataset along specified axes.
Warning
This method has not been thoroughly checked for accuracy yet. Please double-check your results with a different FFT function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The data variable to take FFT of. |
required |
|
list[int]
|
The integer indices of the axes to take FFT along. |
None
|
|
list[str]
|
Dimensions along which to compute the FFT. If provided, this takes precedence over |
None
|
|
Additional keyword arguments passed to |
{}
|
Returns:
Type | Description |
---|---|
DataArray
|
The FFT result as a new DataArray. |
Examples:
1D FFT
import ozzy as oz
import numpy as np
# Create a 1D variable in a [Dataset][xarray.Dataset]
x = np.linspace(0, 10, 100)
da = oz.Dataset(data_vars = {'f_x' : np.sin(2 * np.pi * x)}, coords=[x], dims=['x'], pic_data_type='grid')
# Compute the 1D FFT
da_fft = da.ozzy.fft('f_x', dims=['x'])
# -> 'fft' took: 0:00:00.085525
2D FFT
import ozzy as oz
import numpy as np
# Create a 2D DataArray
x = np.linspace(0, 10, 100)
y = np.linspace(0, 5, 50)
X, Y = np.meshgrid(x, y)
da = oz.Dataset(data_vars = {'f_xy': np.sin(2 * np.pi * X) * np.cos(2 * np.pi * Y)},
coords=[y, x], dims=['y', 'x'], pic_data_type='grid')
# Compute the 2D FFT
da_fft = da.ozzy.fft(dims=['x', 'y'])
# -> 'fft' took: 0:00:00.006278
save
§
save(path)
Save data object to an HDF5 (default) or NetCDF file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The path to save the file to. Specify the file ending as |
required |
Examples:
Save empty Dataset
import ozzy as oz
ds = oz.Dataset()
ds.ozzy.save('empty_file.h5')
# -> Saved file "empty_file.h5"
# -> 'save' took: 0:00:00.197806