U
    e»{j  ã                   @   s†   d Z ddlZddlmZ dddddœZd	d
„ Zdd„ ZG dd„ dƒZG dd„ deƒZ	G dd„ deƒZ
G dd„ dƒZG dd„ deƒZdS )zH
This module provides functions to help with testing against iraf tasks
é    N)ÚlogZ	ChebyshevZLegendreZSpline3ZSpline1)g      ð?g       @g      @g      @c                 C   s<   t | ƒ}| ¡ }| ¡  | d¡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                 S   s   g | ]}t |ƒ‘qS © )ÚRecord©Ú.0Úrr   r   ú^/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/modeling/tests/irafutil.pyÚ
<listcomp>   s     zget_records.<locals>.<listcomp>)ÚopenÚreadÚcloseÚsplit)ÚfnameÚfÚdtbZrecsÚrecordsr   r   r
   Úget_records   s    r   c                 C   s   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   r   r   r
   Úget_database_string#   s    r   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
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                 C   s   || _ |  ¡ | _|  ¡ | _d S ©N)ÚrecstrÚ
get_fieldsÚfieldsÚget_task_nameZtaskname©Úselfr   r   r   r
   Ú__init__E   s    
zRecord.__init__c                    s0   | j  d¡‰ dd„ ˆ D ƒ‰ ‡ fdd„ˆ D ƒ ˆ S )NÚ
c                 S   s   g | ]}|  ¡ ‘qS r   )Ústrip©r   Úentryr   r   r
   r   L   s     z!Record.aslist.<locals>.<listcomp>c                    s"   g | ]}t |ƒd krˆ  |¡‘qS )r   )ÚlenÚremover    ©Zreclistr   r
   r   M   s      )r   r   ©r   r   r$   r
   ÚaslistJ   s    zRecord.aslistc              	   C   sà   i }|   ¡ }t|ƒ}t|ƒD ]¾}|| }|r|d  ¡ r| ¡ }|d |k r²||d  d  ¡ sŒ|  |||t|d ƒ d … ¡||d < qÖd dd„ |dd … D ƒ¡||d < qÚd dd„ |dd … D ƒ¡||d < qqq|S )Nr   r   ú c                 s   s   | ]
}|V  qd S r   r   ©r   Úsr   r   r
   Ú	<genexpr>_   s     z$Record.get_fields.<locals>.<genexpr>c                 s   s   | ]
}|V  qd S r   r   r(   r   r   r
   r*   a   s     )r&   r"   ÚrangeÚisalphar   Úread_array_fieldÚintÚjoin)r   r   ÚflistZ	numfieldsÚiÚlineÚfieldr   r   r
   r   P   s     ÿ&&zRecord.get_fieldsc                 C   s(   z| j d W S  tk
r"   Y d S X d S )NÚtask)r   ÚKeyErrorr%   r   r   r
   r   f   s    zRecord.get_task_namec                 C   sp   dd„ |dd … D ƒ}dd„ |D ƒ}zt  |¡}W n0 tk
rb   t d|d  ¡ d › �¡ Y nX | t j¡S )Nc                 S   s   g | ]}|  ¡ ‘qS r   )r   r    r   r   r
   r   n   s     z+Record.read_array_field.<locals>.<listcomp>r   c                 S   s   g | ]}|d d… ‘qS )Né   r   r    r   r   r
   r   q   s     zCould not read array field r   )ÚnpÚarrayÚ	Exceptionr   Údebugr   ZastypeZfloat64)r   Z	fieldlistZ	fieldlineZxyzZfarrr   r   r
   r-   l   s    "zRecord.read_array_fieldN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r&   r   r   r-   r   r   r   r
   r   6   s   r   c                       sH   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	‡  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                    s‚   t ƒ  |¡ | jd  ¡ | _| jd d d …df | _|  ¡ | _| jd d d …df | _|  	¡ | _
|  ¡ | _|  ¡ | _|  ¡ | _d S )NZcoefficientsÚfeaturesr   r   )Úsuperr   r   ÚflattenÚ
_flatcoeffÚxÚ	get_ydataÚyÚzÚget_model_nameÚ	modelnameÚ
get_ntermsZntermsÚ	get_rangeZmrangeÚ	get_coeffÚcoeffr   ©Ú	__class__r   r
   r   “   s    



zIdentifyRecord.__init__c                 C   s   t | jd  S )Nr   )Úiraf_models_maprC   r%   r   r   r
   rH   ž   s    zIdentifyRecord.get_model_namec                 C   s
   | j d S )Nr   ©rC   r%   r   r   r
   rJ   ¡   s    zIdentifyRecord.get_ntermsc                 C   s   | j d }| j d }||gS )Né   r6   rQ   )r   ÚlowÚhighr   r   r
   rK   ¤   s    

zIdentifyRecord.get_rangec                 C   s   | j dd … S )Né   rQ   r%   r   r   r
   rL   ©   s    zIdentifyRecord.get_coeffc                 C   s`   | j d }| d¡d }| d¡}|||… }d|krT| d¡d }t||d… ƒS t|ƒS d S )NÚimageú[r   ú]ú,éÿÿÿÿ)r   Úfindr.   )r   rV   ÚleftÚrightÚsectionZyindr   r   r
   rE   ¬   s    

zIdentifyRecord.get_ydata)r;   r<   r=   r>   r   rH   rJ   rK   rL   rE   Ú__classcell__r   r   rN   r
   r?   y   s   r?   c                       s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  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                    s~   t ƒ  |¡ | jd  ¡ | _t| jd  | _| jd | _| jd | _| jd | jd g| _	| jd | jd g| _
|  ¡ | _d S )	NZsurfacer   r   rR   rU   é   é   é   )rA   r   r   rB   Ú_surfacerP   rI   ZxorderZyorderZxboundsZyboundsrL   rM   r   rN   r   r
   r   Î   s    zFitcoordsRecord.__init__c                 C   s   | j dd … S )Né   )rd   r%   r   r   r
   rL   Ø   s    zFitcoordsRecord.get_coeff)r;   r<   r=   r>   r   rL   r_   r   r   rN   r
   r`   ¸   s   
r`   c                   @   s    e Zd ZdZdd„ Zd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                 C   s&   dd„ |   |¡D ƒ| _t| jƒ| _d S )Nc                 S   s   g | ]}t |ƒ‘qS r   )r?   )r   Zrstrr   r   r
   r   ê   s     z IDB.__init__.<locals>.<listcomp>)r&   r   r"   Z
numrecords)r   Zdtbstrr   r   r
   r   é   s    zIDB.__init__c                 C   sp   |  d¡}z|d   d¡}W n tk
r4   | Y S X t|ƒdkrh|d  d¡rh|d  ¡ sh|dd … S |S d S )Nr   r   r   rR   ú#r   )r   r9   r"   Ú
startswithr   )r   r   ZrlZrl0r   r   r
   r&   í   s    

&z
IDB.aslistN)r;   r<   r=   r>   r   r&   r   r   r   r
   rf   Ü   s   rf   c                       s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )ÚReidentifyRecordzG
    Represents a database record for the onedspec.reidentify task
    c                    sJ   t ƒ  |¡ t dd„ | jD ƒ¡| _|  ¡ | _t dd„ | jD ƒ¡| _d S )Nc                 S   s   g | ]
}|j ‘qS r   )rD   r   r   r   r
   r     s     z-ReidentifyRecord.__init__.<locals>.<listcomp>c                 S   s   g | ]
}|j ‘qS r   )rG   r   r   r   r
   r     s     )	rA   r   r7   r8   r   rD   rE   rF   rG   )r   ZdatabasestrrN   r   r
   r     s    
zReidentifyRecord.__init__c                 C   s:   t  | jj¡}|t  dd„ | jD ƒ¡d d …t jf  }|S )Nc                 S   s   g | ]
}|j ‘qS r   )rF   r   r   r   r
   r   	  s     z.ReidentifyRecord.get_ydata.<locals>.<listcomp>)r7   ZonesrD   Úshaper8   r   Znewaxis)r   rF   r   r   r
   rE     s    (zReidentifyRecord.get_ydata)r;   r<   r=   r>   r   rE   r_   r   r   rN   r
   ri   û   s   ri   )r>   Únumpyr7   Zastropy.loggerr   rP   r   r   r   r?   r`   rf   ri   r   r   r   r
   Ú<module>   s   C?$