Source code for Muscat.IO.CGNSReader

# -*- coding: utf-8 -*-
#
# This file is subject to the terms and conditions defined in
# file 'LICENSE.txt', which is part of this source code package.
#

"""CGNS file reader
"""
from typing import Optional, Union
import os

import numpy as np

from Muscat.Types import MuscatFloat, MuscatIndex
from Muscat.Bridges.CGNSBridge import CGNSToMesh
from Muscat.Containers.Mesh import Mesh


[docs]def ReadCGNS(fileName, time: Optional[MuscatFloat] = None, baseNumberOrName: Union[MuscatIndex, str] = 0, zoneNumberOrName: Union[MuscatIndex, str] = 0) -> Mesh: """Function API for reading a CGNS file Parameters ---------- fileName : str name of the file to be read time : float, optional not coded yet, by default None baseNumberOrName : int or str, optional name of the base to use, by default 0 (first) zoneNumberOrName : int or str, optional name of the zone to be read, by default 0 (first) Returns ------- Mesh output unstructured mesh object containing reading result """ reader = CGNSReader() reader.SetFileName(fileName) reader.baseNumberOrName: Union[MuscatIndex, str] = baseNumberOrName reader.zoneNumberOrName: Union[MuscatIndex, str] = zoneNumberOrName reader.SetTimeToRead(time) res = reader.Read(fileName=fileName) return res
[docs]class CGNSReader: """CGNS Reader class""" def __init__(self) -> None: super().__init__() self.fileName: str = None self.fieldName = None self.baseNumberOrName = None self.zoneNumberOrName = None self.timeToRead: MuscatFloat = -1.0 self.encoding = None self.canHandleTemporal = False
[docs] def SetFileName(self, fileName: str) -> None: """Function to set fileName to read Parameters ---------- fileName : str name of the file to be read """ self.fileName = fileName if fileName is None: self.__path = None else: self.filePath = os.path.abspath(os.path.dirname(fileName)) + os.sep
[docs] def SetTimeToRead(self, time: Optional[MuscatFloat] = None) -> None: """Function to set time value to read Parameters ---------- time : float, optional not coded yet, by default None """ if time is None: self.timeToRead = 0.0 else: # pragma: no cover raise NotImplementedError("not coded yet") self.timeToRead = time
[docs] def Read(self, fileName: Optional[str] = None, time: Optional[MuscatFloat] = None) -> Mesh: """Function that performs the reading of a CGNS result file Parameters ---------- fileName : str name of the file to be read time : float, optional not coded yet, by default None baseNumberOrName : int or str, optional name of the base to use, by default 0 (first) zoneNumberOrName : int or str, optional name of the zone to be read, by default 0 (first) Returns ------- Mesh output unstructured mesh object containing reading result """ if fileName is not None: self.SetFileName(fileName) self.SetTimeToRead(time) from h5py import File h5file = File(self.fileName, "r") def ConvertData(node): res = [node.attrs["name"].decode("utf-8"), None, [], node.attrs["label"].decode("utf-8")] if " data" in node: dataitem = node[" data"] res[1] = np.copy(dataitem[()].transpose(), order="F") if node.attrs["type"] == b"C1": res[1] = np.vectorize(chr)(res[1]).astype(np.dtype("c")) names = [x for x in node.keys() if x[0] != " "] for name in names: child = ConvertData(node[name]) if child is not None: res[2].append(child) return res node = ConvertData(h5file) node[0] = "CGNSTree" node[3] = "CGNSTree_t" self.CGNSTree = node res = CGNSToMesh(node) res.PrepareForOutput() return res
from Muscat.IO.IOFactory import RegisterReaderClass RegisterReaderClass(".cgns", CGNSReader)
[docs]def CheckIntegrity(GUI: bool = False): try: from h5py import File except: # pragma: no cover return "skip! h5py module not available" import Muscat.IO.CGNSWriter as CW CW.CheckIntegrity() from Muscat.Helpers.IO.TemporaryDirectory import TemporaryDirectory tempdir = TemporaryDirectory.GetTempPath() mesh = ReadCGNS(fileName=tempdir + os.sep + "toto.cgns") print("Read mesh from cgns:", mesh) reader = CGNSReader() reader.SetFileName(None) from Muscat.TestData import GetTestDataPath print("Read mesh from cgns:", ReadCGNS(GetTestDataPath() + "CGNSExample/2TriMesh.cgns")) return "ok"
if __name__ == "__main__": print(CheckIntegrity(True)) # pragma: no cover