memory_size_check

PlasmaCalcs.tools.arrays.memory_size_check(array, *, MBmax=UNSET, dtype_min=UNSET, safety=1, errmsg=None)

raise MemorySizeError if array is too large.

Too large if safety * array.nbytes/1024**2 > MBmax.
array: array, e.g. numpy array
must have .size and .dtype attributes which tell number of elements and numpy dtype.
Or, can be an xarray.Dataset, in which case will add up the size of all DataArrays.
MBmax: UNSET, None, or int
maximum size allowed, in Megabytes.
UNSET –> use DEFAULTS.ARRAY_MBYTES_MAX
None –> no limit (equivalent to “skip this check”)
dtype_min: UNSET or numpy dtype
calculate memory as if array elements will have at least as many bytes as dtype_min.itemsize.
(if array elements already have more bytes than this, use array.dtype instead.)
UNSET –> use DEFAULTS.ARRAY_MEMORY_CHECK_DTYPE_MIN
None –> don’t impose any minimum, just use array.dtype.
safety: number
safety factor; safety * array.nbytes/1024**2 is the actual limit.
errmsg: None or str
if provided, if raising MemorySizeError, use this for the error message,
after formatting with .format(nMB=nMB, MBmax=MBmax, safety=safety, nMB_total=nMB * safety,
dtype=dtype used, shape=array.shape, size=array.size).
if None, use:
“array too large: {nMB:.1e} MB > MBmax / safety. (MBmax={MBmax} MB, safety={safety})”.