Package: pysdif
Needs Cython >= 0.15It allows to read and write any kind of SDIF file, to define newkinds of frames and matrices and to read and write metadata.The matrices read from a sdif file are exposed as numpy arrays.It exposes both a low level and a high level interface.The low level interface for reading and writing sdif files mirrors thesdif library quite transparently so that the example files andutilities using it can be directly translated with it. In particularit does not create any intermediate objects, even the data of the matricesis a numpy array mapped to the c array read from disk, so no allocation takesplace. Whereas this makes for very fast code, one has to take care to copy thedata if it will be used longer, since by the time a new matrix is read thisdata is no longer valid.to read for ex. 1TRC format:import pysdifsdif_file = pysdif.SdifFile('filename.sdif')sig1TRC = pysdif.str2signature("1TRC")while not sdif_file.eof:sdif_file.read_frame_header()if sdif_file.frame_numerical_signature) == sig1TRC:print sdif_file.timefor n in range(sdif_file.matrices_in_frame):sdif_file.read_matrix_header()if sdif_file.matrix_numerical_signature == sig1TRC:data = sdif_file.get_matrix_data(copy=True) # default is True# data is now a numpy array# here is what happens under the hood: the SDIF library# reads a whole matrix and puts it in its internal buffer# This data is memcpy(ed) to create a numpy array# If you dont intend to keep this data and there are no# chances that a new matrix or frame will be read before# you use this data for some calculation, then it is safe# to call sdif_file.get_matrix_data(copy=False)# in this case no memory is copied, the numpy array does not# own its memory and uses the internal buffer of the SDIF runtimeprint dataa more natural way:from pysdif import SdifFilesdif_file = SdifFile('filename.sdif')for frame in sdif_file:if frame.signature == "1TRC":print frame.timefor matrix in frame:if matrix.signature == "1TRC":print matrix.get_data() # data will be copied, use .get_data(copy=False) to avoid thisthe frames and the matrices resulting from the iterationare only guaranteed to be valid as long as no new frames and matrices are readto write a SdifFile:f = SdifFile('new_sdif.sdif', 'w')# these are optional#   add some metadataf.add_NVT({'name' : 'my name','date' : time.asctime(time.localtime())})# define new frame and matrix typesf.add_frame_type('1NEW', '1ABC NewMatrix, 1FQ0 New1FQ0')f.add_matrix_type('1ABC', 'Column1, Column2')# now you can begin adding framesframe = f.new_frame('1NEW', time_now)frame.add_matrix('1ABC', array([[0,     1.2],[3.5,   8.13],...]))frame.write()# say we just want to take the data from an existing# sdiffile, modify it and write it backin_sdif = SdifFile("existing-file.sdif")out_sdif = SdifFile("outfile.sdif", "w")out_sdif.clone_definitions(in_sdif)for in_frame in in_sdif:if in_frame.signature == "1NEW":new_frame = out_sdif.new_frame("1NEW", in_frame.time)# we know there is only one matrix and we dont need to keep the datain_data = in_frame.get_matrix_data(copy=False)# multiply itin_data[:,1] *= 0.5# add it to the streamnew_frame.add_matrix('1ABC', in_data)# only one matrix, so write the frame.new_frame.write()# along this operation, only the memory used to allocate the original# matrix was used, the rest of the operations is performed in place# This is only safe where only one thread has access to# the sdif entity at once and data is not kept longer than the time# read it, transform it and rewrite it.there are also many utility functions under pysdif.sdiftoolssee release notes and changes at http://github.com/gesellkammer/pysdif