L i*ddZddlZddlZgdZGddeeZGddeeZGdd Z y) z Module to read / write Fortran unformatted sequential files. This is in the spirit of code written by Neil Martinsen-Burrell and Joe Zuntz. N) FortranFileFortranEOFErrorFortranFormattingErrorceZdZdZy)rzIndicates that the file ended properly. This error descends from TypeError because the code used to raise TypeError (and this was the only way to know that the file had ended) so users might have ``except TypeError:``. N__name__ __module__ __qualname____doc__W/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/io/_fortran.pyrr s  r rceZdZdZy)rzhIndicates that the file ended mid-record. Descends from TypeError for backward compatibility. Nrr r rrrs   r rcfeZdZdZdej fdZd dZdZdZ ddZ ddZ d Z d Z d Zy )ra A file object for unformatted sequential files from Fortran code. Parameters ---------- filename : file or str Open file object or filename. mode : {'r', 'w'}, optional Read-write mode, default is 'r'. header_dtype : dtype, optional Data type of the header. Size and endianness must match the input/output file. Notes ----- These files are broken up into records of unspecified types. The size of each record is given at the start (although the size of this header is not standard) and the data is written onto disk without any formatting. Fortran compilers supporting the BACKSPACE statement will write a second copy of the size to facilitate backwards seeking. This class only supports files written with both sizes for the record. It also does not support the subrecords used in Intel and gfortran compilers for records which are greater than 2GB with a 4-byte header. An example of an unformatted sequential file in Fortran would be written as:: OPEN(1, FILE=myfilename, FORM='unformatted') WRITE(1) myvariable Since this is a non-standard file format, whose contents depend on the compiler and the endianness of the machine, caution is advised. Files from gfortran 4.8.0 and gfortran 4.1.2 on x86_64 are known to work. Consider using Fortran direct-access files or files from the newer Stream I/O, which can be easily read by `numpy.fromfile`. Examples -------- To create an unformatted sequential Fortran file: >>> from scipy.io import FortranFile >>> import numpy as np >>> f = FortranFile('test.unf', 'w') >>> f.write_record(np.array([1,2,3,4,5], dtype=np.int32)) >>> f.write_record(np.linspace(0,1,20).reshape((5,4)).T) >>> f.close() To read this file: >>> f = FortranFile('test.unf', 'r') >>> print(f.read_ints(np.int32)) [1 2 3 4 5] >>> print(f.read_reals(float).reshape((5,4), order="F")) [[0. 0.05263158 0.10526316 0.15789474] [0.21052632 0.26315789 0.31578947 0.36842105] [0.42105263 0.47368421 0.52631579 0.57894737] [0.63157895 0.68421053 0.73684211 0.78947368] [0.84210526 0.89473684 0.94736842 1. ]] >>> f.close() Or, in Fortran:: integer :: a(5), i double precision :: b(5,4) open(1, file='test.unf', form='unformatted') read(1) a read(1) b close(1) write(*,*) a do i = 1, 5 write(*,*) b(i,:) end do rc:| tdtj|}|jdk7rt j dd|dvst |dk7r tdt|d r||_||_ yt||d |_||_ y) NzMust specify dtypeuz$Given a dtype which is not unsigned.) stacklevelrwzmode must be either r or wseekb) ValueErrornpdtypekindwarningswarnlenhasattr_fpopen _header_dtype)selffilenamemode header_dtypes r__init__zFortranFile.__init__ms  12 2xx -    # MM@Q O t s4yA~9: : 8V $DH*Haj1DH)r c|jj}|jj|}|s |r t dt ||kr t dttj||jddS)Nz%End of file occurred at end of recordz,End of file in the middle of the record sizerrcountr) r$itemsizer"readrr rintr frombuffer)r%eof_oknrs r _read_sizezFortranFile._read_sizesv    ' ' HHMM! v!"IJ J VaZ(>@ @2==$*<*z+FortranFile.write_record..s94bjj&9!c34K|]}|jywr6)nbytesr8s rr;z+FortranFile.write_record..s77)rN)tuplesumrarrayr$tofiler")r%items total_sizenbr:s r write_recordzFortranFile.write_records{.9599777 XXzl$*<*< = $(( "D KK ! " $((r c6|jdd}|r%tdt|j|||fz}n |s td|j d}td|D}t d|D}t ||\}}|d k7rtd |d |d t|d k7r||k7rtd |d|dg}|D]}tj|j||} t| |k7r td|jdk7r(|d k(r#| jd|jzk(sJ| d } |j| |j } || k7r tdt|d k(r|d St|S)a, Reads a record of a given type from the file. Parameters ---------- *dtypes : dtypes, optional Data type(s) specifying the size and endianness of the data. Returns ------- data : ndarray A 1-D array object. Raises ------ FortranEOFError To signal that no further records are available FortranFormattingError To signal that the end of the file was encountered part-way through a record Notes ----- If the record contains a multidimensional array, you can specify the size in the dtype. For example:: INTEGER var(5,4) can be read with:: read_record('(4,5)i4').T Note that this function does **not** assume the file data is in Fortran column major order, so you need to (i) swap the order of dimensions when reading and (ii) transpose the resulting array. Alternatively, you can read the data as a 1-D array and handle the ordering yourself. For example:: read_record('i4').reshape(5, 4, order='F') For records that contain several variables or mixed types (as opposed to single scalar or array types), give them as separate arguments:: double precision :: a integer :: b write(1) a, b record = f.read_record('.s;5rxx;r<c34K|]}|jywr6)r-rJs rr;z*FortranFile.read_record..s? ?__D_1 ;F;; I >zl;..8\=> > v;!  j 8zl;//9l:PRS S E DHHEDA1v#,;=={{b ?77dU[[&8888!A KKN oo'  $@A A v;! 7N; r c$|j|S)a Reads a record of a given type from the file, defaulting to an integer type (``INTEGER*4`` in Fortran). Parameters ---------- dtype : dtype, optional Data type specifying the size and endianness of the data. Returns ------- data : ndarray A 1-D array object. See Also -------- read_reals read_record rZr%rs r read_intszFortranFile.read_ints'*&&r c$|j|S)a Reads a record of a given type from the file, defaulting to a floating point number (``real*8`` in Fortran). Parameters ---------- dtype : dtype, optional Data type specifying the size and endianness of the data. Returns ------- data : ndarray A 1-D array object. See Also -------- read_ints read_record r\r]s r read_realszFortranFile.read_reals>r_r c8|jjy)z Closes the file. It is unsupported to call any other methods off this object after closing it. Note that this class supports the 'with' statement in modern versions of Python, to call this automatically N)r"closer%s rrczFortranFile.closeUs r c|Sr6r rds r __enter__zFortranFile.__enter__^s r c$|jyr6)rc)r%typevaluetbs r__exit__zFortranFile.__exit__as  r N)F)i4)f8)rr r r ruint32r)r3rGrZr^rarcrfrkr r rrr!sEJV'* *$KB{z'.'.r r) r rnumpyr__all__ TypeErrorOSErrorrrrr r rrss?  F i  Y AAr