ngect.utils module
- ngect.utils.check_dir(filename)
Ensure the parent directory of a file path exists.
Parameters
filename : str
The file path whose parent directory should be checked/created.
Returns
- bool
Trueif the directory already existed,Falseif it was created by this call.
Notes
This helper does not create the file itself; it only ensures that the directory component of
filenameexists. Iffilenamehas no directory component (e.g.,"file.txt"), this function returnsTrueand performs no action.
- ngect.utils.get_si_prefix(value: float, select: str = 'mu', lztol: int = 0) tuple
Choose a readable SI prefix for a numeric value.
Determines the prefix that minimizes leading zeros (within a tolerance) and keeps a small number of digits before the decimal point after scaling the value.
Parameters
- valuefloat
The numeric value to scale.
- selectstr, optional
String containing the set of candidate SI prefixes to consider. Must be a subset of
"yzafpnum kMGTPEZY"(a space represents no prefix). Defaults to"mu"(micro and none).- lztolint, optional
Leading-zero tolerance used when selecting the prefix. Higher values allow more leading zeros before switching to a smaller unit. Default is
0.
Returns
- tuple
(prefix: str, scale: float)whereprefixis the chosen SI prefix symbol (e.g.,"m","u","k", or""for none) andscaleis the multiplicative factor corresponding to that prefix (e.g.,1e-3for"m",1e6for"M").
Notes
The full ordered table of supported prefixes is:
"yzafpnum kMGTPEZY"The space character denotes the base unit (no prefix).
Examples
Frequency value in Hz:
>>> get_si_prefix(10**7, select='kMGT') ('M', 1000000.0)
Flux value in Jy:
>>> get_si_prefix(1.0, select='um') ('', 1.0) >>> get_si_prefix(0.0, select='um') ('', 1.0) >>> get_si_prefix(-0.9, select='um') ('m', 0.001) >>> get_si_prefix(0.9, select='um', lztol=1) ('', 1.0) >>> get_si_prefix(1e-7, select='um') ('u', 1e-06) >>> get_si_prefix(1e3, select='um') ('', 1.0)