a
    LYi                     @   s>   d dl Z d dlmZ d dlmZ d dlmZ G dd dZdS )    N)CONFIG)DataType)outputc                   @   sN   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dddZ
dS )Importerc                 C   s(   d| _ d| _d| _td d d | _d S )Nr   satbotsimports
batch_size)batches_remainingtotal_batchespoints_countr   r   )self r   N/var/www/staging/api/app_home/main/services/satbots/scripts/importer/shared.py__init__   s    zImporter.__init__c              	   C   s   || j  }|d | j  }|| j  }|d | j  }i }	dD ]<}
z$||
 ||||f | |	|
< W q8 tyr   Y q80 q8i }tD ]n}td d d |j}|r~|d r~t||d  dd||||f dd|d	 |d f d	d||< q~|	|fS )
h
        :param batch_indices: This represents the indices of the points relative to the batch.
           )	LongitudeLatitudeVelocityRMSE	uwerrfracPS_maskAverage_Cohquality_delta_3monthquality_delta_6monthvelocity_delta_3monthvelocity_delta_6monthr   r   displacementsenabledmappingNr   )r   KeyErrorr   r   getnamenumpyswapaxes)r   vel_filebatch_indicesbatch_number_xbatch_number_ymin_index_0max_index_0min_index_1max_index_1data	attributedisps	disp_type	disp_infor   r   r   hdf5_vel_batch   s     

$HzImporter.hdf5_vel_batchc                 C   sn   || j  }|d | j  }|| j  }|d | j  }i }	dD ]0}
|
| v r8||
 ||||f | |	|
< q8|	S )r   r   )	DEM_ErrorHeight)r   keys)r   	data_filer&   r'   r(   r)   r*   r+   r,   r-   r.   r   r   r   hdf5_data_batch'   s    

"zImporter.hdf5_data_batchc                 C   s@   dd }|d||||}|d||||}t t t ||S )Nc                 S   s0   ||  || |d | || |d | f S )Nr   r   )datasetr%   r'   r(   r   r   r   r   get_data_for_batch9   s    z0Importer.get_indices.<locals>.get_data_for_batchr   ZReliable_Pixels)r#   wherelogical_andisfinite)r   r%   r'   r(   r   r9   ZvelsZreliable_ixr   r   r   get_indices8   s    zImporter.get_indicesc                 C   sd   g }|d j }t|d D ]$}t|d D ]}|||g q*q| |ddt|}|t|d fS )Nr   r   r   )shaperangeappendr=   len)r   r%   Zpossible_pointsr>   ijindicesr   r   r   possible_indicesA   s    
zImporter.possible_indicesc                 C   s   |d j }tt|d | j }tt|d | j }g }t|D ] }t|D ]}|||f qRqFt|| _t|| _	t
|dS )Nr   r   r   d   )r>   intr#   ceilr   r?   r@   rA   r	   r
   array_split)r   r%   Zh5file_shapeZbatch_count_xZbatch_count_ybatch_countsrB   rC   r   r   r   rJ   L   s    


zImporter.batch_countsc                 C   sn   t |d }i }i }|D ]J}t |t| dk|t|d  dk @ }|||< ||  ||< q||fS )NPDate0101r   )r#   arrayr:   rG   tolist)r   r%   Zupdate_years
hdf5_datesdatesyear_indicesyearZthis_year_indicesr   r   r   rQ   \   s    .zImporter.year_indicesr   r    c                 C   s<   || | d }|r8t || d| j d| t|d d S )NrF   z |  )progress)r   r   round)r   satbotr	   r
   suffixZbatch_progressr   r   r   update_job_progressi   s    zImporter.update_job_progressN)r   r   rS   )__name__
__module____qualname__r   r2   r7   r=   rE   rJ   rQ   rY   r   r   r   r   r      s   	r   )r#   setupr   Zmain.models.insar.point_datar   $main.services.satbots.scripts.sharedr   r   r   r   r   r   <module>   s   