GDSII格式读写器
klamath的Python项目详细描述
克拉马斯自述
klamath
是一个Python模块,用于读写GDSII文件格式。在
目标是使该库保持简单:
- 尽可能直接映射数据类型。
- 精确表示保存到文件中的内容。在
- 避免过多的拷贝/分配以提高速度。在
- 没有“自动”错误检查,除非在转换数据类型时。 如果提供了数据完整性检查,则必须 由调用方显式运行。在
- 低级功能是一流的。
- 用于调用方希望读或写的用例 个人记录。在
- 提供对写入文件的完全控制。在
- 固执己见和有限的高级功能。
- 丢弃或忽略很少遇到的数据类型。在
- 保持函数的简单性和可重用性。在
- 仅对文件格式进行反/编码,不提供修改工具 数据本身。在
- 大多数字段仍然需要显式值。在
- 不编译
- 使用
numpy
来提高速度,因为它通常是可用的/预构建的。在 - 构建这个库不需要编译器。在
- 使用
klamath
旨在为
masque,它提供了更高级别的
用于处理分层设计数据并支持多个
文件格式。在
替代品
- gdspy
- 提供处理设计数据的抽象和方法 在I/O过程之外(例如多边形剪裁)。在
需要编译(C++)从源代码编译。在 - 专注于高级API
- 纯python实现。可以很容易地更改为使用
numpy
对于速度,但受对象分配开销的限制。在 - 专注于高级API
链接
安装
要求:
- python>;=3.7(用3.8编写并测试)
- numpy公司
使用pip安装:
pip3 install klamath
或者,从git安装
^{pr2}$示例
低电平
过滤基于层读取的多边形:
importioimportklamathfromklamathimportrecordsfromklamath.recordimportRecorddefread_polygons(stream,filter_layer_tuple=(4,5)):""" Given a stream positioned at the start of a record, return the vertices of all BOUNDARY records which match the provided `filter_layer_tuple`, up to the next ENDSTR record. """polys=[]whileTrue:size,tag=Record.read_header(stream)stream.seek(size,io.SEEK_CUR)# skip to next headeriftag==records.ENDEL.tag:break# If ENDEL, we are doneiftag!=records.BOUNDARY.tag:continue# Skip until we find a BOUNDARYlayer=records.LAYER.skip_and_read(stream)[0]# skip to LAYERdtype=records.DATATYPE.read(stream)[0]if(layer,dtype)!=filter_layer_tuple:continue# Skip reading XY unless layer matchesxy=XY.read(stream).reshape(-1,2)polys.append(xy)returnpolys
高电平
编写示例GDS文件:
importklamathfromklamath.elementsimportBoundary,Text,Path,Referencestream=open('example.gds','wb')header=klamath.library.FileHeader(name=b'example',meters_per_db_unit=1e-9,# 1 nm DB unituser_units_per_db_unit=1e-3)# 1 um (1000nm) display unitheader.write(stream)elements_A=[Boundary(layer=(4,18),xy=[[0,0],[10,0],[10,20],[0,20],[0,0]],properties={1:b'prop1string',2:b'some other string'}),Text(layer=(5,5),xy=[[5,10]],string=b'center position',properties={},# Remaining args are set to default valuespresentation=0,# and will be omitted when writingangle_deg=0,invert_y=False,width=0,path_type=0,mag=1),Path(layer=(4,20),xy=[[0,0],[10,10],[0,20]],path_type=0,width=0,extension=(0,0),# ignored since path_type=0properties={}),]klamath.library.write_struct(stream,name=b'my_struct',elements=elements_A)elements_top=[Reference(struct_name=b'my_struct',xy=[[30,30]],colrow=None,# not an arrayangle_deg=0,invert_y=True,mag=1.5,properties={}),Reference(struct_name=b'my_struct',colrow=(3,2),# 3x2 array at (0, 50)xy=[[0,50],[60,50],[30,50]],# with basis vectorsangle_deg=30,# [20, 0] and [0, 30]invert_y=False,mag=1,properties={}),]klamath.library.write_struct(stream,name=b'top',elements=elements_top)klamath.records.ENDLIB.write(stream,None)stream.close()
读回文件:
importklamathstream=open('example.gds','rb')header=klamath.library.FileHeader.read(stream)structs={}struct=klamath.library.try_read_struct(stream)whilestructisnotNone:name,elements=structstructs[name]=elementsstruct=klamath.library.try_read_struct(stream)stream.close()print(structs)
按名称读回单个结构:
importklamathstream=open('example.gds','rb')header=klamath.library.FileHeader.read(stream)struct_positions=klamath.library.scan_structs(stream)stream.seek(struct_positions[b'my_struct'])elements_A=klamath.library.try_read_struct(stream)stream.close()print(elements_A)
- 项目
标签: