o
    d                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ e ZdZddd	Zd
d ZdddZdd Zdd Zedkrbe  dS dS )    N)Template)RsmasLoggerloglevel)message_rsmas)process_utilities)PathFind)
JOB_SUBMITc           
      C   s  t j| dd}| d ur| }ntjdd  }t|jtj	t
d d|  |jd }t|d}|j|jd  d urD|j|jd  |_d|jd	 v rUtj|jd
|_nd|jd v rftj|jd|_n	tj|jd|_|jrd}|jtjd dd }t|}d|v r|d tjt
g| }|||| td tj|jst|j t|j t|j|j|j|}	d S )Ngenerate_download_command)script    z/ssara_rsmas.log)	file_namezStack.slcDirZCOSMOzssaraopt.platformRAW_dataTSXzssaraopt.collectionNameSLC_ORIGSLCZdownload_ssara_rsmas.r   z--submit)putilscmd_line_parsesysargvr   logwork_dirospathbasename__file__joinr   templateprefixdownload_dirsubmit_flagcustom_template_filesplitsepr   removeabspathsubmit_scriptexitisdirmakedirschdir	run_ssara	delta_lat)
iargsinpsinput_argumentsZlogfile_nameloggerjob_file_namejob_namejob_objcommandZ	succesful r7   H/home/exouser/operations/rsmas_insar/minsar/generate_download_command.pymain   s8   $



r9   c           
      C   s   t dg|dt|  dg }|dd}|dt|d  }g }|D ]}||dd d	d  q'|D ](}	tjt	t
 d	 t	|	 sc|tjd
 t| j| jt||d   dS q;|tjd dS )a   Checks if all of the ssara files to be dwonloaded actually exist.

        Checks if the files to be downloaded actually exist or not on the system as a means of validating
        whether or not the wrapper completed succesfully.

        Parameters: run_number: int, the current iteration the wrapper is on (maxiumum 10 before quitting)
                    args: [string], ssara_federated_query.py options to run with

        Returns: none

    zssara_federated_query-cj.pyr   z--printzutf-8
   ,r   /z=The file, %s, didn't download correctly. Running ssara again.NzEverything is there!)
subprocesscheck_outputlendecoder$   appendr   r   isfilestrgetcwdr   r   ZWARNINGr-   r!   r#   r.   INFO)
r0   
run_numberargsr2   Zssara_outputZssara_output_arrayZssara_output_filteredZfiles_to_checkentryfr7   r7   r8   check_downloadsC   s   "  rK   r   c           	      C   s   t |}|jt|j | }|d}t| |	 |}dg| dg }t
dd}|d|d  W d   dS 1 sDw   Y  dS )a	   Runs ssara_federated_query-cj.py and checks for download issues.
        Runs ssara_federated_query-cj.py and checks continuously for whether the data download has hung without
        comleting or exited with an error code. If either of the above occur, the function is run again, for a
        maxiumum of 10 times.
        Parameters: run_number: int, the current iteration the wrapper is on (maxiumum 10 before quitting)
        Returns: status_cod: int, the status of the donwload (0 for failed, 1 for success)
    r   zssara_federated_query.bashz--maxResults=20000z../ssara_command.txtwr:   N)r   ZoptionsupdatepathObjZcorrect_for_ssara_date_formatZgenerate_ssaraopt_stringr$   add_polygon_to_ssaraoptZget_optionscopyopenwriter   )	r!   r   r.   r2   rG   dataset_templatessaraoptZ
ssara_callrJ   r7   r7   r8   r-   `   s   

r-   c                 C   s  d| vrt d d}n| d }| |d  }d| }|d}|d dd|d< |d	 dd|d	< |d
 dd|d
< |d dd|d< |d }t|d | }t|d	 | }t|d
 | }	t|d | }
d|	||	||
||
||	|
}|d
| dd |D }|S )Qcalculates intersectsWith polygon from bbox and replace frame in ssaraopt if giveacquisition_mode`WARNING: "acquisition_mode" is not given --> default: tops   (available options: tops, stripmap)topsStack.boundingBoxr   r   ' r         g?zg--intersectsWith='Polygon(({:.2f} {:.2f}, {:.2f} {:.2f}, {:.2f} {:.2f}, {:.2f} {:.2f}, {:.2f} {:.2f}))'c                 S       g | ]}|d d dks|qS r      z--framer7   .0xr7   r7   r8   
<listcomp>        z+add_polygon_to_ssaraopt.<locals>.<listcomp>)printr   r$   replacefloatformatinsert)rS   rT   r.   r    string	bbox_listZ	delta_lonZmin_latZmax_latZmin_lonZmax_lonZpolygonr7   r7   r8   rO   y   s0   
rO   c                 C   s   d| vrt d d}n| d }| d d}| |d  d}|d dd	|d< |d
 dd	|d
< dt|d t|d
 }|d| dd |D }|S )rU   rV   rW   rX   zssaraopt.intersectsWithPointr   rY   r   rZ   r[   r   z'--intersectsWith='Point({:.2f} {:.2f})'r\   c                 S   r^   r_   r7   ra   r7   r7   r8   rd      re   z)add_point_to_ssaraopt.<locals>.<listcomp>)rf   r$   rg   ri   rh   rj   )rS   rT   r    Zpointrl   r7   r7   r8   add_point_to_ssaraopt   s   rm   __main__)N)r   )r   r   timer>   datetimeargparseZminsar.objects.dataset_templater   Zminsar.objects.rsmas_loggingr   r   minsar.objectsr   minsar.utilsr   r   Zminsar.objects.auto_defaultsr   minsar.job_submissionr   rN   r0   r9   rK   r-   rO   rm   __name__r7   r7   r7   r8   <module>   s,   
/
#
