
    (ph*                     d    S r SSKrSSKr/ SQr " S S\\5      r " S S\\5      r " S S	5      r	g)
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)FortranFileFortranEOFErrorFortranFormattingErrorc                       \ rS rSrSrSrg)r      zIndicates 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____firstlineno____doc____static_attributes__r       D/var/www/html/venv/lib/python3.13/site-packages/scipy/io/_fortran.pyr   r      s     	r   r   c                       \ rS rSrSrSrg)r      z`Indicates that the file ended mid-record.

Descends from TypeError for backward compatibility.

r   Nr	   r   r   r   r   r      s    
 	r   r   c                   v    \ rS rSrSrS\R                  4S jrSS jrS r	S r
SS jrSS	 jrS
 rS rS rSrg)r   !   a	  
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                 8   Uc  [        S5      e[        R                  " U5      nUR                  S:w  a  [        R
                  " SSS9  US;  d  [        U5      S:w  a  [        S5      e[        US	5      (       a  Xl        O[        X S
35      U l        X0l
        g )NzMust specify dtypeuz$Given a dtype which is not unsigned.   )
stacklevelrw   zmode must be either r or wseekb)
ValueErrornpdtypekindwarningswarnlenhasattr_fpopen_header_dtype)selffilenamemodeheader_dtypes       r   __init__FortranFile.__init__m   s    122xx-#MM@QOts4yA~9::8V$$HHaj1DH)r   c                     U R                   R                  nU R                  R                  U5      nU(       d  U(       a  [	        S5      e[        U5      U:  a  [        S5      e[        [        R                  " X0R                   SS9S   5      $ )Nz%End of file occurred at end of recordz,End of file in the middle of the record sizer   r!   countr   )
r)   itemsizer'   readr   r%   r   intr    
frombuffer)r*   eof_oknr   s       r   
_read_sizeFortranFile._read_size   st    ''HHMM!v!"IJJVaZ(>@ @2==*<*<AFqIJJr   c                 @   [        S U 5       5      n[        S U 5       5      n[        R                  " U/U R                  S9nUR                  U R                  5        U H  nUR                  U R                  5        M      UR                  U R                  5        g)a  
Write a record (including sizes) to the file.

Parameters
----------
*items : array_like
    The data arrays to write.

Notes
-----
Writes data items to a file::

    write_record(a.T, b.T, c.T, ...)

    write(1) a, b, c, ...

Note that data in multidimensional arrays is written in
row-major order --- to make them read correctly by Fortran
programs, you need to transpose the arrays yourself when
writing them.

c              3   N   #    U  H  n[         R                  " U5      v   M     g 7fN)r    asarray.0items     r   	<genexpr>+FortranFile.write_record.<locals>.<genexpr>   s     954bjj&&5   #%c              3   8   #    U  H  oR                   v   M     g 7fr=   )nbytesr?   s     r   rB   rC      s     7   )r!   N)tuplesumr    arrayr)   tofiler'   )r*   items
total_sizenbrA   s        r   write_recordFortranFile.write_record   sv    . 9599777
XXzl$*<*<=
		$((DKK! 
		$((r   c                 L   UR                  SS5      nU(       a%  [        S[        UR                  5       5       35      eUb  X4-   nOU(       d  [        S5      eU R	                  SS9n[        S U 5       5      n[        S U 5       5      n[        XE5      u  pgUS	:w  a  [        S
U SU S35      e[        U5      S:w  a  XE:w  a  [        S
U SU S35      e/ nU H  n[        R                  " U R                  X6S9n	[        U	5      U:w  a  [        S5      eUR                  S:w  a*  US:X  a$  U	R                  SUR                  -   :X  d   eU	S	   n	UR                  U	5        M     U R	                  5       n
XJ:w  a  [        S5      e[        U5      S:X  a  US	   $ [        U5      $ )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('<f4', '<i4')
    a = record[0]  # first number
    b = record[1]  # second number

and if any of the variables are arrays, the shape can be specified as
the third item in the relevant dtype::

    double precision :: a
    integer :: b(3,4)
    write(1) a, b

    record = f.read_record('<f4', np.dtype(('<i4', (4, 3))))
    a = record[0]
    b = record[1].T

NumPy also supports a short syntax for this kind of type::

    record = f.read_record('<f4', '(3,3)<i4')

See Also
--------
read_reals
read_ints

r!   NzUnknown keyword arguments zMust specify at least one dtypeT)r7   c              3   N   #    U  H  n[         R                  " U5      v   M     g 7fr=   )r    r!   r@   r!   s     r   rB   *FortranFile.read_record.<locals>.<genexpr>   s     ;F5rxxFrD   c              3   8   #    U  H  oR                   v   M     g 7fr=   )r3   rS   s     r   rB   rT      s     <VEVrG   r   zSize obtained (z)) is not a multiple of the dtypes given (z).r   z)) does not match with the expected size (z) of multi-item recordr1   z%End of file in the middle of a recordr   )r   zPSizes do not agree in the header and footer for this record - check header dtype)popr   rH   keysr9   rI   divmodr%   r    fromfiler'   r   shapeappend)r*   dtypeskwargsr!   
first_size
block_size
num_blocks	remainderdatar   second_sizes              r   read_recordFortranFile.read_record   s   T 

7D)9%:N9OPQQh&F>??__D_1
;F;;<V<<
 &z >
>zl ;..8\= > > v;!
 8 zl ;//9l:PR S S EDHHEDA1v#,;= ={{b ?77dU[[&8888!AKKN  oo'$ @ A A v;!7N;r   c                 $    U R                  U5      $ )a9  
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

rd   r*   r!   s     r   	read_intsFortranFile.read_ints'      * &&r   c                 $    U R                  U5      $ )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

rg   rh   s     r   
read_realsFortranFile.read_reals>  rk   r   c                 8    U R                   R                  5         g)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'   closer*   s    r   rp   FortranFile.closeU  s     	r   c                     U $ r=   r   rq   s    r   	__enter__FortranFile.__enter__^  s    r   c                 $    U R                  5         g r=   )rp   )r*   typevaluetbs       r   __exit__FortranFile.__exit__a  s    

r   )r'   r)   N)F)i4)f8)r
   r   r   r   r   r    uint32r.   r9   rO   rd   ri   rm   rp   rt   rz   r   r   r   r   r   r   !   sE    JV '*		 *$KB{z'.'.r   r   )
r   r#   numpyr    __all__	TypeErrorOSErrorr   r   r   r   r   r   <module>r      s?     
F	i 		Y 	A Ar   