U
    a                     @   s   d dl Z d dlZd dlZd dlmZmZ d dlmZm	Z	 dZ
dd ZdddZd	d
 Zdd Zdd ZdddZedkr~e  dS )    N)readfile	writefile)	RAMP_LISTderampa  example:
   subtract_h5.py geo_20180610_20190430_ramp.h5 geo_20181125_20181207_ramp.h5 -b 34 34.6 45.5 46 --output geo_cumulative_des.h5
   subtract_h5.py geo_20180610_20190430_ramp.h5 geo_20181125_20181207_ramp.h5 --output geo_cumulative_des.h5
   subtract_h5.py geo_20180610_20190430_ramp.h5 geo_20181125_20181207_ramp.h5 --output geo_cumulative_des.h5 --ramp -s linear -m mask.h5 --ramp_file ramp.h5
c               	   C   s   t jdt jtd} | jdddd | jddd	td
ddd | jddddd | jddtdd | jdd}|jddddd |jddd td!d" |jd#d$td%d |jd&td'd( | S ))NzXSubtract the second data from the first data.And calculate ramp from the subtracted data)descriptionformatter_classepilogfile   zHDF5 file to be calculated.)nargshelpz-bz--bboxSNWE   )SNWEzBounding box of area to be geocoded.
Include the uppler left corner of the first pixel    and the lower right corner of the last pixel)desttyper   metavarr   z-oz--outputoutfilezoutput file name.)r   r   z--outdiroutdirzoutput file dir)r   r   r   zoptions for calculate ramps)titlez--ramp
store_trueFz-whether calculate the ramp of subtracted data)actiondefaultr   z-ssurface_typelinearz1type of surface/ramp to remove, linear by default)r   r   choicesr   z-mmaskz	mask dataz--ramp_filezoutput name of calculated ramp)r   r   )	argparseArgumentParserRawTextHelpFormatterEXAMPLEadd_argumentfloatstradd_argument_groupr   )parserZramp_option r)   H/home/centos/operations/rsmas_insar/sources/MimtPy/mimtpy/subtract_h5.pycreate_parser   s(    
r+   c                 C   s   t  }|j| d}|S )N)args)r+   
parse_args)iargsr(   inpsr)   r)   r*   cmd_line_parse3   s    r0   c                 C   s   t | jd }tt| jd t|d  t|d  }tt| jd t|d  t|d  }tt| jd t|d  t|d  }tt| jd t|d  t|d  }||||fS )	zchange SNWD to xyr   Y_FIRSTY_STEP   r
   X_FIRSTX_STEP   )r   read_attributer	   intnprintr   r%   )r/   atrrow_maxrow_minclom_minclom_maxr)   r)   r*   
latlontoyx9   s    ,,,,r@   c                 C   s   t | jd }t | jd d }t | jd d }| jd k	rt| \}}}}td |||||f  |||||f 8  < n||8 }| jdr|d 	dr|d |d< d|d< ||| jfS )	Nr   r3   zextract displacementz.h5	FILE_TYPE.roipac	PROCESSOR)
r   r7   r	   readr   r@   printr   endswith
startswith)r/   r;   data1data2r=   r<   r>   r?   r)   r)   r*   subtract_dataD   s    
.rK   c                 C   s<   |j }t|jd }t| |||dd }tj||dk< |S )zcalcualte ramp from data filer   )	ramp_typemetadatar3   F)r   r   rE   r   r   r9   nan)datar;   r/   rL   r   rampr)   r)   r*   calculate_rampa   s
    rQ   c                 C   sZ   t | }t|\}}}|jr@t|||}tj||j|j |d tj||j| |d d S )N)out_filerM   )r0   rK   rP   rQ   r   writer   Z	ramp_file)r.   r/   rO   r;   rR   rP   r)   r)   r*   maink   s    rT   __main__)N)N)osr    numpyr9   mintpy.utilsr   r   mintpy.objectsr   r   r#   r+   r0   r@   rK   rQ   rT   __name__r)   r)   r)   r*   <module>   s   


