# -*- 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"""fromtypingimportOptional,UnionimportosimportnumpyasnpfromMuscat.TypesimportMuscatFloat,MuscatIndexfromMuscat.Bridges.CGNSBridgeimportCGNSToMeshfromMuscat.Containers.MeshimportMesh
[docs]defReadCGNS(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]=baseNumberOrNamereader.zoneNumberOrName:Union[MuscatIndex,str]=zoneNumberOrNamereader.SetTimeToRead(time)res=reader.Read(fileName=fileName)returnres
[docs]defSetFileName(self,fileName:str)->None:"""Function to set fileName to read Parameters ---------- fileName : str name of the file to be read """self.fileName=fileNameiffileNameisNone:self.__path=Noneelse:self.filePath=os.path.abspath(os.path.dirname(fileName))+os.sep
[docs]defSetTimeToRead(self,time:Optional[MuscatFloat]=None)->None:"""Function to set time value to read Parameters ---------- time : float, optional not coded yet, by default None """iftimeisNone:self.timeToRead=0.0else:# pragma: no coverraiseNotImplementedError("not coded yet")self.timeToRead=time
[docs]defRead(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 """iffileNameisnotNone:self.SetFileName(fileName)self.SetTimeToRead(time)fromh5pyimportFileh5file=File(self.fileName,"r")defConvertData(node):res=[node.attrs["name"].decode("utf-8"),None,[],node.attrs["label"].decode("utf-8")]if" data"innode:dataitem=node[" data"]res[1]=np.copy(dataitem[()].transpose(),order="F")ifnode.attrs["type"]==b"C1":res[1]=np.vectorize(chr)(res[1]).astype(np.dtype("c"))names=[xforxinnode.keys()ifx[0]!=" "]fornameinnames:child=ConvertData(node[name])ifchildisnotNone:res[2].append(child)returnresnode=ConvertData(h5file)node[0]="CGNSTree"node[3]="CGNSTree_t"self.CGNSTree=noderes=CGNSToMesh(node)res.PrepareForOutput()returnres
[docs]defCheckIntegrity(GUI:bool=False):try:fromh5pyimportFileexcept:# pragma: no coverreturn"skip! h5py module not available"importMuscat.IO.CGNSWriterasCWCW.CheckIntegrity()fromMuscat.Helpers.IO.TemporaryDirectoryimportTemporaryDirectorytempdir=TemporaryDirectory.GetTempPath()mesh=ReadCGNS(fileName=tempdir+os.sep+"toto.cgns")print("Read mesh from cgns:",mesh)reader=CGNSReader()reader.SetFileName(None)fromMuscat.TestDataimportGetTestDataPathprint("Read mesh from cgns:",ReadCGNS(GetTestDataPath()+"CGNSExample/2TriMesh.cgns"))return"ok"
if__name__=="__main__":print(CheckIntegrity(True))# pragma: no cover