Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 2 additions & 44 deletions pygmt/clib/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def dataarray_to_matrix(grid):
inc = [abs(i) for i in inc]
grid = grid.sortby(variables=list(grid.dims), ascending=True)

matrix = as_c_contiguous(grid[::-1].to_numpy())
matrix = np.ascontiguousarray(grid[::-1].to_numpy())
region = [float(i) for i in region]
inc = [float(i) for i in inc]
return matrix, region, inc
Expand Down Expand Up @@ -200,53 +200,11 @@ def vectors_to_arrays(vectors):
for vector in vectors:
vec_dtype = str(getattr(vector, "dtype", ""))
array = np.asarray(a=vector, dtype=dtypes.get(vec_dtype))
arrays.append(as_c_contiguous(array))
arrays.append(np.ascontiguousarray(array))

return arrays


def as_c_contiguous(array):
"""
Ensure a numpy array is C contiguous in memory.

If the array is not C contiguous, a copy will be necessary.

Parameters
----------
array : 1-D array
The numpy array

Returns
-------
array : 1-D array
Array is C contiguous order.

Examples
--------

>>> import numpy as np
>>> data = np.array([[1, 2], [3, 4], [5, 6]])
>>> x = data[:, 0]
>>> x
array([1, 3, 5])
>>> x.flags.c_contiguous
False
>>> new_x = as_c_contiguous(x)
>>> new_x
array([1, 3, 5])
>>> new_x.flags.c_contiguous
True
>>> x = np.array([8, 9, 10])
>>> x.flags.c_contiguous
True
>>> as_c_contiguous(x).flags.c_contiguous
True
"""
if not array.flags.c_contiguous:
return array.copy(order="C")
return array


def sequence_to_ctypes_array(
sequence: Sequence | None, ctype, size: int
) -> ctp.Array | None:
Expand Down
3 changes: 1 addition & 2 deletions pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import xarray as xr
from pygmt.clib.conversion import (
array_to_datetime,
as_c_contiguous,
dataarray_to_matrix,
sequence_to_ctypes_array,
strings_to_ctypes_array,
Expand Down Expand Up @@ -1499,7 +1498,7 @@ def virtualfile_from_matrix(self, matrix):
# collected and the memory freed. Creating it in this context manager
# guarantees that the copy will be around until the virtual file is
# closed.
matrix = as_c_contiguous(matrix)
matrix = np.ascontiguousarray(matrix)
rows, columns = matrix.shape

family = "GMT_IS_DATASET|GMT_VIA_MATRIX"
Expand Down