|
| 1 | +"""Parse castep .pes files.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import TextIO, TypedDict |
| 6 | + |
| 7 | +from ..utilities.datatypes import ThreeByThreeMatrix, ThreeVector |
| 8 | +from ..utilities.filewrapper import Block |
| 9 | +from ..utilities.utility import file_or_path, to_type |
| 10 | + |
| 11 | + |
| 12 | +class PESData(TypedDict): |
| 13 | + """PES sample points.""" |
| 14 | + |
| 15 | + pos: ThreeVector |
| 16 | + e: float |
| 17 | + f_opt: float | None |
| 18 | + |
| 19 | + |
| 20 | +class PESFileInfo(TypedDict, total=False): |
| 21 | + """Full pes file information.""" |
| 22 | + |
| 23 | + probe_species: str |
| 24 | + probe_method: str |
| 25 | + cell: ThreeByThreeMatrix |
| 26 | + date_started: str |
| 27 | + data: list[PESData] |
| 28 | + units: dict[str, str] |
| 29 | + samples: dict[str, int] |
| 30 | + |
| 31 | + |
| 32 | +def _parse_header(header: Block) -> PESFileInfo: |
| 33 | + accum: PESFileInfo = {"units": {}, "samples": {}, "data": []} |
| 34 | + |
| 35 | + header = map(str.strip, header) |
| 36 | + |
| 37 | + for line in header: |
| 38 | + if line.startswith("Job:"): |
| 39 | + _, accum["probe_method"] = line.split(": ") |
| 40 | + elif line.startswith("Probe species:"): |
| 41 | + _, accum["probe_species"] = line.split(": ") |
| 42 | + elif "unit:" in line: |
| 43 | + typ_, _, unit = line.split() |
| 44 | + accum["units"][typ_.lower()] = unit |
| 45 | + elif line.startswith("Number of samples"): |
| 46 | + *_, direc, count = line.split() |
| 47 | + accum["samples"][direc.strip(":")] = int(count) |
| 48 | + elif line.startswith("Cell Vectors"): |
| 49 | + *_, unit = line.split() |
| 50 | + accum["units"]["cell"] = unit.strip("()") |
| 51 | + accum["cell"] = tuple(to_type(cell.split(), float) |
| 52 | + for cell, _ in zip(header, range(3))) |
| 53 | + elif line.startswith("Date"): |
| 54 | + _, accum["date_started"] = line.split(": ", maxsplit=1) |
| 55 | + |
| 56 | + return accum |
| 57 | + |
| 58 | + |
| 59 | +@file_or_path(mode="r") |
| 60 | +def parse_pes_file(pes_file: TextIO) -> PESFileInfo: |
| 61 | + """Parse castep .pes file. |
| 62 | +
|
| 63 | + Parameters |
| 64 | + ---------- |
| 65 | + pes_file |
| 66 | + A handle to a CASTEP .pes file. |
| 67 | +
|
| 68 | + Returns |
| 69 | + ------- |
| 70 | + PESFileInfo |
| 71 | + Parsed data. |
| 72 | + """ |
| 73 | + line = next(pes_file) |
| 74 | + block = Block.from_re(line, pes_file, "BEGIN header", "END header") |
| 75 | + |
| 76 | + accum = _parse_header(block) |
| 77 | + |
| 78 | + line = next(pes_file) |
| 79 | + block = Block.from_re(line, pes_file, " BLOCK DATA", "ENDBLOCK DATA") |
| 80 | + block.remove_bounds(1, 1) |
| 81 | + |
| 82 | + data = accum["data"] |
| 83 | + |
| 84 | + for line in block: |
| 85 | + a, b, c, e, *extra = to_type(line.split(), float) |
| 86 | + |
| 87 | + curr = {} |
| 88 | + curr["pos"] = a, b, c |
| 89 | + curr["e"] = e |
| 90 | + curr["f_opt"] = extra[0] if extra else None |
| 91 | + |
| 92 | + data.append(curr) |
| 93 | + |
| 94 | + return accum |
0 commit comments