o
    <Ôc$  ã                   @   s"   d dl Z d dlZG dd„ dƒZdS )é    Nc                   @   sX   e Zd 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„ Zdd„ ZdS )ÚTemplatea   Template object encapsulates a dictionary of template options.
        Given a dataset.template file, this object creates a dictionary of options keyed on the option name. This
        will allow for accessing of various template parameters in constant time (as opposed to O(n) time) and includes
        a simple, user friendly syntax.
        Use as follows:
            template = Template(file_name)                                  # create a template object
            options = template.get_options()                                # access the options dictionary
            dataset = options['dataset']                                    # access a specific option
            options = template.update_options_from_file(default_template_file)        # access a specific option
    c                 C   s   |   |¡| _dS )a   Initializes Template object with a custom template file.
            The provided template file is parsed line by line, and each option is added to the options dictionary.
            :param custom_template_file: file, the template file to be accessed
        N)Úread_optionsÚoptions)ÚselfÚcustom_template_file© r   úG/home/exouser/operations/rsmas_insar/minsar/objects/dataset_template.pyÚ__init__   s   zTemplate.__init__c                 C   s¬   d|  d¡d   d¡d i}t|ƒ5}|D ]*}d|v r?| d¡s?|  d¡}|d  ¡ }|d	  ¡   d
¡d  d¡}||t|ƒ< qW d  ƒ n1 sJw   Y  |  |¡}|S )z Read template options.

            :param template_file: file, the template file to be read and stored in a dictionary
        Údatasetú/éÿÿÿÿÚ.r   ú=©ú%ú#z = é   r   ú N)ÚsplitÚopenÚ
startswithÚrstripÚstripÚstrÚcorrect_keyvalue_quotes)r   Útemplate_filer   ÚtemplateÚlineÚpartsÚkeyÚvaluer   r   r   r      s   

€óÿ
zTemplate.read_optionsc              	   C   s2  t j |¡}|  |¡}|d }t|dƒl}t|dƒD ]^}dd„ | ¡  dd¡D ƒ}| d¡sst|ƒdkrs|d	 }t	 
|d d
d¡ d¡d	  ¡ }	|| j ¡ v rs| j| |	krs| 
|	| j| d¡}| j| ||< td ||	| j| ¡ƒ | |¡ qW d  ƒ n1 sƒw   Y  d ||¡}
t  |
¡ || _| jS )z² Update default template file with the options from custom template file read initially.

            :param default_template_file: file, the template file to be updated
        z.tmpÚwÚrc                 S   s   g | ]}|  ¡ ‘qS r   )r   )Ú.0Úir   r   r   Ú
<listcomp>B   s    z+Template.update_options.<locals>.<listcomp>r   r   r   r   Ú
Ú r   z"From custom_template {}: {} --> {}Nzmv {} {})ÚosÚpathÚabspathr   r   r   r   r   Úlenr   Úreplacer   ÚkeysÚprintÚformatÚwriteÚsystem)r   Zdefault_template_file_tmpZdefault_template_fileZdefault_optionsZtmp_fileZf_tmpr   Úcr   r    ZmvCmdr   r   r   Úupdate_options6   s(   
 õÿ
zTemplate.update_optionsc                 C   s   |   ¡ }|||< dS )z» Updates the options dictionary key with the specified value.
            :param key   : the dictionary key to update
            :param value : the value to replace the key with
        N)Úget_options)r   r   r    r   r   r   r   Úupdate_optionR   s   zTemplate.update_optionc                 C   s   | j S )z§ Provides direct access to the options dictionary.
            This should be used in lieu of directly accessing the options dictionary via Template().options
        ©r   ©r   r   r   r   r4   Z   s   zTemplate.get_optionsc                 C   s
   | j d S )zº Provides quick access to the dataset property of the options dictionary.
            Should be used to quickly access the dataset name when directories require the dataset name
        r
   r6   r7   r   r   r   Úget_dataset_name`   s   
zTemplate.get_dataset_namec                 C   s¨  dg}d}|D ]}|| j vrd}d| j v r| j d }qtdƒ‚q|sÒ| j d }d |¡}d| j  ¡ v r?| j d }|d |¡7 }d	| j  ¡ v rR| j d	 }|d
 |¡7 }d| j  ¡ v re| j d }|d |¡7 }d| j  ¡ v rx| j d }	|d |	¡7 }d| j  ¡ v r‹| j d }
|d |
¡7 }d| j v rœ| j d }|d |¡7 }d| j v r­| j d }|d |¡7 }d| j v r¾| j d }|d |¡7 }d| j v rÉ| j d }nd}|d |¡7 }|S )z‡ Generates the ssaraopt string for ssara_federated_query.py command line calls from the options
            dictionary values.
        zssaraopt.relativeOrbitFTÚssaraoptz no ssaraopt, relativeOrbit foundz --relativeOrbit={}zssaraopt.platformz --platform={}zssaraopt.collectionNamez --collectionName={}zssaraopt.beamModez --beamMode={}zssaraopt.beamSwathz --beamSwath={}zssaraopt.framez --frame={}zssaraopt.startDatez --start={}zssaraopt.endDatez	 --end={}zssaraopt.intersectsWithPointz --intersectsWith='POINT({})'zssaraopt.parallelé   z --parallel={})r   Ú	Exceptionr/   r-   )r   Zssaraopt_keysZbad_keyr   r9   ZrelativeOrbitÚplatformZcollectionNameZbeamModeZ	beamSwathÚframeZ	startDateZendDateZintersectsWithPointÚparallelr   r   r   Úgenerate_ssaraopt_stringf   sR   


û













z!Template.generate_ssaraopt_stringc                 C   sD   dD ]}||  ¡ v r|| }|  |¡}|||< t|d | ƒ q|S )zÍ quote-independent reformatting of sentinel.subswath key-value:
             1 2 ---> '1 2'
             1  ---> 1
            '1' ---> 1
            -1 0.15 -91.6 -90.9 ---> '-1 0.15 -91.6 -90.9'
        )ztopsStack.subswathztopsStack.boundingBoxz-->)r-   Úcheck_correct_quotesr.   )r   Z
options_inÚitemZ
value_origZ	value_newr   r   r   r   ¤   s   
€z Template.correct_keyvalue_quotesc                 C   sl   |dd… dkr|  d¡}t|ƒdkr|dd… }|S |}|S |  d¡}t|ƒdkr2d| d }|S |}|S )zO checks weather quotes are as required and corrects/replaces if needed
        r   r   ú'r   é   )r   r+   )r   ÚstringZnum_itemZ
out_stringr   r   r   r@   ´   s   

ø
úþzTemplate.check_correct_quotesN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   r3   r5   r4   r8   r?   r   r@   r   r   r   r   r      s    >r   )r(   Úsysr   r   r   r   r   Ú<module>   s    