extract_quantity_from_filepath

extract_quantity_from_filepath#

extract_quantity_from_filepath(ifile, default=None)[source]#

Extract the MAS/POT3D quantity name from a filename stem.

Searches for the first recognized quantity token anywhere in the stem. A match is accepted only when the token is not immediately preceded or followed by another ASCII letter, so partial matches inside longer words are rejected. The match is case-insensitive.

Parameters:
ifilePath

File path whose stem is examined. Only the stem (filename without extension) is inspected.

defaultstr or None, optional

Value to return when no quantity token is found. Defaults to None.

Returns:
outstr or None

Lower-case quantity name (e.g. 'br'), or default if no recognized quantity token is found in the stem.

Examples

>>> from psi_io.models import extract_quantity_from_filepath
>>> from pathlib import Path
>>> extract_quantity_from_filepath(Path('br001001.h5'))
'br'
>>> extract_quantity_from_filepath(Path('heat001.h5'))
'heat'
>>> extract_quantity_from_filepath(Path('run_br_001.h5'))
'br'
>>> extract_quantity_from_filepath(Path('unknown.h5')) is None
True
>>> extract_quantity_from_filepath(Path('unknown.h5'), default='br')
'br'