
    IR-e                         d Z ddlZddlmZ dddddZd	 Zd
 Z G d d          Z G d de          Z	 G d de          Z
 G d d          Z G d de          ZdS )zI
This module provides functions to help with testing against iraf tasks.
    N)log	ChebyshevLegendreSpline3Spline1)g      ?g       @g      @g      @c                     t          |           }|                                }|                                 |                    d          dd         }d |D             }|S )z
    Read the records of an IRAF database file into a python list.

    Parameters
    ----------
    fname : str
           name of an IRAF database file

    Returns
    -------
        A list of records
    begin   Nc                 ,    g | ]}t          |          S  )Record.0rs     ?lib/python3.11/site-packages/astropy/modeling/tests/irafutil.py
<listcomp>zget_records.<locals>.<listcomp>   s    '''Qvayy'''    )openreadclosesplit)fnamefdtbrecsrecordss        r   get_recordsr      s[     	UA
&&((CGGIII99Wabb!D''$'''GNr   c                 t    t          |           }|                                }|                                 |S )z
    Read an IRAF database file.

    Parameters
    ----------
    fname : str
          name of an IRAF database file

    Returns
    -------
        the database file as a string
    )r   r   r   )r   r   r   s      r   get_database_stringr   #   s.     	UA
&&((CGGIIIJr   c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )r   a-  
    A base class for all records - represents an IRAF database record.

    Attributes
    ----------
    recstr: string
            the record as a string
    fields: dict
            the fields in the record
    taskname: string
            the name of the task which created the database file
    c                 x    || _         |                                 | _        |                                 | _        d S N)recstr
get_fieldsfieldsget_task_nametaskname)selfr#   s     r   __init__zRecord.__init__D   s1    oo''**,,r   c                 p    | j                             d          d D             fdD              S )N
c                 6    g | ]}|                                 S r   )stripr   entrys     r   r   z!Record.aslist.<locals>.<listcomp>K   s     666U5;;==666r   c                 `    g | ]*}t          |          d k                        |          +S )r   )lenremove)r   r/   reclists     r   r   z!Record.aslist.<locals>.<listcomp>L   s0    GGG5s5zzQ		r   )r#   r   )r(   r3   s    @r   aslistzRecord.aslistI   sJ    +##D))66g666GGGGGGGGGr   c           
         i }|                                  }t          |          }t          |          D ]
}||         }|r|d                                         r|                                }|dz   |k     r||dz            d                                         s@|                     |||t          |d                   z   dz                      ||d         <   d                    d |dd          D                       ||d         <   d                    d |dd          D                       ||d         <   
|S )Nr   r
    c              3      K   | ]}|V  d S r"   r   r   ss     r   	<genexpr>z$Record.get_fields.<locals>.<genexpr>^   s"      3I3I!A3I3I3I3I3I3Ir   c              3      K   | ]}|V  d S r"   r   r8   s     r   r:   z$Record.get_fields.<locals>.<genexpr>`   s"      /E/Ea/E/E/E/E/E/Er   )r4   r1   rangeisalphar   read_array_fieldintjoin)r(   r%   flist	numfieldsilinefields          r   r$   zRecord.get_fieldsO   sM   JJ	y!! 	 	A8D Q)) 

q59$$ Q<?2244 J+/+@+@!!a#eAh--&7!&;";<, ,uQx(( ,/883I3IuQRRy3I3I3I+I+IuQx(('*xx/E/E59/E/E/E'E'EF58$$r   c                 @    	 | j         d         S # t          $ r Y d S w xY w)Ntask)r%   KeyErrorr(   s    r   r&   zRecord.get_task_namee   s6    	;v&& 	 	 	44	s    
c                 6   d |dd          D             }d |D             }	 t          j        |          }nE# t          $ r8 t          j        d|d                                         d                     Y nw xY w|                    t           j                  S )Nc                 6    g | ]}|                                 S r   )r   r.   s     r   r   z+Record.read_array_field.<locals>.<listcomp>m   s     >>>uU[[]]>>>r   r
   c                 "    g | ]}|d d         S )N   r   r.   s     r   r   z+Record.read_array_field.<locals>.<listcomp>p   s     000UuRaRy000r   zCould not read array field r   )nparray	Exceptionr   debugr   astypefloat64)r(   	fieldlist	fieldlinexyzfarrs        r   r>   zRecord.read_array_fieldk   s    >>	!"">>>	 10i000	O8C==DD 	O 	O 	OIMIaL4F4F4H4H4KMMNNNNN	O{{2:&&&s   7 ?A98A9N)	__name__
__module____qualname____doc__r)   r4   r$   r&   r>   r   r   r   r   r   6   si         - - -
    ,  
' 
' 
' 
' 
'r   r   c                   @     e Zd ZdZ fdZd Zd Zd Zd Zd Z	 xZ
S )IdentifyRecorda  
    Represents a database record for the onedspec.identify task.

    Attributes
    ----------
    x: array
       the X values of the identified features
       this represents values on axis1 (image rows)
    y: int
       the Y values of the identified features
       (image columns)
    z: array
       the values which X maps into
    modelname: string
        the function used to fit the data
    nterms: int
        degree of the polynomial which was fit to the data
        in IRAF this is the number of coefficients, not the order
    mrange: list
        the range of the data
    coeff: array
        function (modelname) coefficients
    c                    t                                          |           | j        d                                         | _        | j        d         d d df         | _        |                                 | _        | j        d         d d df         | _        | 	                                | _
        |                                 | _        |                                 | _        |                                 | _        d S )Ncoefficientsfeaturesr   r
   )superr)   r%   flatten
_flatcoeffx	get_ydatayzget_model_name	modelname
get_ntermsnterms	get_rangemrange	get_coeffcoeffr(   r#   	__class__s     r   r)   zIdentifyRecord.__init__   s       +n5==??Z(A.!!Z(A.,,..oo''nn&&^^%%


r   c                 2    t           | j        d                  S )Nr   )iraf_models_maprc   rI   s    r   rh   zIdentifyRecord.get_model_name   s    tq122r   c                     | j         d         S )Nr
   rc   rI   s    r   rj   zIdentifyRecord.get_nterms   s    q!!r   c                 >    | j         d         }| j         d         }||gS )N   rM   ru   )r(   lowhighs      r   rl   zIdentifyRecord.get_range   s$    oa q!T{r   c                      | j         dd          S )N   ru   rI   s    r   rn   zIdentifyRecord.get_coeff   s    qrr""r   c                    | j         d         }|                    d          dz   }|                    d          }|||         }d|v r/|                    d          dz   }t          ||d                   S t          |          S )Nimage[r
   ],)r%   findr?   )r(   r}   leftrightsectionyinds         r   re   zIdentifyRecord.get_ydata   s~    G$zz#"

3U
#'>>::c??Q&DuT"W~&&&w<<r   )rX   rY   rZ   r[   r)   rh   rj   rl   rn   re   __classcell__rq   s   @r   r]   r]   x   s         0	& 	& 	& 	& 	&3 3 3" " "  
# # #	  	  	  	  	  	  	 r   r]   c                   (     e Zd ZdZ fdZd Z xZS )FitcoordsRecorda  
    Represents a database record for the longslit.fitccords task.

    Attributes
    ----------
    modelname: string
        the function used to fit the data
    xorder: int
        number of terms in x
    yorder: int
        number of terms in y
    xbounds: list
        data range in x
    ybounds: list
        data range in y
    coeff: array
        function coefficients

    c                    t                                          |           | j        d                                         | _        t
          | j        d                  | _        | j        d         | _        | j        d         | _        | j        d         | j        d         g| _	        | j        d         | j        d         g| _
        |                                 | _        d S )	Nsurfacer   r
   rw   r{            )ra   r)   r%   rb   _surfacers   ri   xorderyorderxboundsyboundsrn   ro   rp   s     r   r)   zFitcoordsRecord.__init__   s       I.6688(q)9:mA&mA&a($-*:;a($-*:;^^%%


r   c                      | j         dd          S )N   )r   rI   s    r   rn   zFitcoordsRecord.get_coeff   s    }QRR  r   )rX   rY   rZ   r[   r)   rn   r   r   s   @r   r   r      sQ         (& & & & &! ! ! ! ! ! !r   r   c                       e Zd ZdZd Zd ZdS )IDBz
    Base class for an IRAF identify database.

    Attributes
    ----------
    records: list
             a list of all `IdentifyRecord` in the database
    numrecords: int
             number of records
    c                     d |                      |          D             | _        t          | j                  | _        d S )Nc                 ,    g | ]}t          |          S r   )r]   )r   rstrs     r   r   z IDB.__init__.<locals>.<listcomp>   s     MMMt,,MMMr   )r4   r   r1   
numrecords)r(   dtbstrs     r   r)   zIDB.__init__   s8    MMV9L9LMMMdl++r   c                 2   |                     d          }	 |d                              d          }n# t          $ r |cY S w xY wt          |          dk    r?|d                             d          r$|d                                         s
|dd          S |S )Nr	   r   r+   rw   #r
   )r   rP   r1   
startswithr-   )r(   r   rlrl0s       r   r4   z
IDB.aslist   s     YYw	Q%++d##CC 	 	 	III	s88q==SV..s33=CFLLNN=abb6MIs   3 AAN)rX   rY   rZ   r[   r)   r4   r   r   r   r   r      s<        	 	, , ,    r   r   c                   (     e Zd ZdZ fdZd Z xZS )ReidentifyRecordzH
    Represents a database record for the onedspec.reidentify task.
    c                    t                                          |           t          j        d | j        D                       | _        |                                 | _        t          j        d | j        D                       | _        d S )Nc                     g | ]	}|j         
S r   )rd   r   s     r   r   z-ReidentifyRecord.__init__.<locals>.<listcomp>       555113555r   c                     g | ]	}|j         
S r   )rg   r   s     r   r   z-ReidentifyRecord.__init__.<locals>.<listcomp>   r   r   )	ra   r)   rN   rO   r   rd   re   rf   rg   )r(   databasestrrq   s     r   r)   zReidentifyRecord.__init__   st    %%%5555566!!5555566r   c                     t          j        | j        j                  }|t          j        d | j        D                       d d t           j        f         z  }|S )Nc                     g | ]	}|j         
S r   )rf   r   s     r   r   z.ReidentifyRecord.get_ydata.<locals>.<listcomp>  s    444!!#444r   )rN   onesrd   shaperO   r   newaxis)r(   rf   s     r   re   zReidentifyRecord.get_ydata  sN    GDFL!!44t|44455aaamDDr   )rX   rY   rZ   r[   r)   re   r   r   s   @r   r   r      sQ         7 7 7 7 7      r   r   )r[   numpyrN   astropy.loggerr   rs   r   r   r   r]   r   r   r   r   r   r   <module>r      s9   
          #*99UU  *  &?' ?' ?' ?' ?' ?' ?' ?'D;  ;  ;  ;  ; V ;  ;  ; | !  !  !  !  !f  !  !  !F       <    s     r   