U
    }-_                      @   s   d dl Z d dlZd dlmZ d dlmZ d dlZd dlZ	d dl
mZ d dlZd dlZd dlmZmZmZ d dlmZ dZdd Zdd	d
Zdd Zdd Zdd Zdd Zdd ZdddZedkre  dS )    N)LightSource)Image)readfile	writefileutils)multitrack_utilitieszexample:
  save_geodmod.py S1_IW23_026_0108_0113_20171117_XXXXXXXX.he5 -s 20171128 -e 20181210 
  
  save_geodmod.py S1_IW23_026_0108_0113_20171117_XXXXXXXX.he5 -b 26.0 27.5 65.0 66.0 -s 20171128 -e 20181210 
c               	   C   sf   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d | jdddddd | S )Nz!Prepare data for Geodmod software)descriptionformatter_classepilogfile?zascending or descending files
)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-sz--startDate	startDatez@date1 of timeseires to be converted.The default is the StartDate)r   r   r   z-ez	--endDateendDatez>date2 of timeseries to be converted.The default is the EndDate)argparseArgumentParserRawTextHelpFormatterEXAMPLEadd_argumentfloat)parser r!   I/home/centos/operations/rsmas_insar/sources/MimtPy/mimtpy/save_geodmod.pycreate_parser   s    r#   c                 C   s   t  }|j| d}|S )N)args)r#   
parse_args)iargsr    inpsr!   r!   r"   cmd_line_parse-   s    r(   c                 C   s
  t |}t }td|d< |d |d< |d |d< d|d< t|d d |d< d|d	< t|d d |d
< t|d |d< t|d |d< t|d |d< t|d |d< d|d< d|d< |d |d< |d |d< d|d< d|d< d|d< d|d< tj||dd |S )zJ write rsc file for Geodmod just estract several properities from rsc filepwdZFILE_DIRFILE_LENGTHWIDTHr   XMIN   XMAXYMINYMAXX_FIRSTY_FIRSTX_STEPY_STEPdegreesX_UNITY_UNITRLOOKSALOOKSZZ_OFFSETZZ_SCALELATLON
PROJECTIONz111111-222222DATE12T)	print_msg)	r   read_roipac_rscdictosgetenvintr   r   write_roipac_rsc)r'   in_fileout_filemetaZrscr!   r!   r"   write_rsc_file3   s.    
rG   c                    sx  | d }|d }t | d | t| d }td|j tt|j}d  fdd|D }td| t	|d	 }t
|d
 }tddd}|j|dtd||d}	tj|	t|	dddddf < td|	j tj|d\}
}|j|	ddd |d | g  | g  |
jddddd td| tj|dddd t|}||jtj}|| dS )z-generate dem.jepg file based on Yunjun's codez.jpegz.rscr   z
dem.shape:i,  c                    s   g | ]}|  qS r!   r!   ).0ifig_dpir!   r"   
<listcomp>[   s     zdem_jpeg.<locals>.<listcomp>z	fig_size:i  i  i;  -   )azdegaltdegg333333?gray)	vert_exagcmapvminvmaxNzdem_shade.shape:)figsizespline16upper)interpolationoriginoffr-   )leftrightbottomtopzsave figure to file {}Tg        )transparentdpi
pad_inches)shutilcopy2r   readprintshapetuplereversednpnanminnanmaxr   shadepltget_cmapnanisnansubplotsimshowaxis	get_xaxis	set_ticks	get_yaxissubplots_adjustformatsavefigr   openresizeNEARESTsave)dem_filerE   Zrsc_filedemds_shapefig_sizedisp_mindisp_maxls	dem_shadefigaximZim_outr!   rJ   r"   dem_jpegP   s4    
"

r   c                 C   s>  t d| j}t|d }t|d }t|d }t|d }t| jd }t| jd }t| jd }	t| jd	 }
||	k rt  td
n||
krt  tdt	|| | d }t	|| | d }t	|	| | d d }t	|
| | d d }t 
|\}}t||d< t||d< tj|||d dS )z0return the row_no,sample_no and rows and samples r2   r1   r4   r3   r-      r      z/input bounding box error! Wrong latitude order!z0input bounding box error! Wrong longitude order!g      ?LENGTHr+   rE   metadataN)r   read_attributejoinr   r   r   r    print_usage	ExceptionrB   rd   strr   write)r'   datasetatrul_latul_lonlat_steplon_step	user_lat0	user_lon0	user_lat1	user_lon1rowsamplerowssamplesdatar!   r!   r"   subset_data_based_bboxx   s0    
r   c                 C   sX   t d| tjd| jddd }t|\}}tj||dk< tj	|||d dS )z	mask datazmask {} filer   z%/HDFEOS/GRIDS/timeseries/quality/mask)datasetNamer   r   N)
re   rx   r   rd   r   r   ri   ro   r   r   )r'   r   maskfiler   r   r!   r!   r"   mask_filter   s
    r   c           
      C   sn  | j }td|dd \}}dd| jd| jdg}d| | dd| jd| jgd	|g}td
| t|}t	td
|
 g dd| jd| jdg}d| | dd	|g}td
| t|}t	td
|
 g}d}	d| | dd	|	g}td
| t|}t	td
|
 g t| | t| | | jrjtd t| | t| | t| |	 dS )zprocess *.he5 filer   r-   r   geo__z.unwz../zdisplacement-z-ozsave_roipac.pyz.cortemporalCoherencesrtm.demheightzSubset data based on bboxN)r   museprate_filename_extensionr   r   r   re   seperate_str_byspacer@   systemsplitr   r   r   )
r'   Zatr_ascfilename	extensionZ
output_unwZcmd_argsZasct_strZ
output_corcompletion_statusZ
output_demr!   r!   r"   process_HDFEOS   s0    &









r   c                 C   sj   t | }t| t| tdg}t||tdg t| td ttdg| t	d d S )Nzsrtm.dem.rsczsrtm.dem.rsc1zrename *.rsc1 to *.rscr   )
r(   re   r   r   r   rG   r@   removerenamer   )r&   r'   outfiler!   r!   r"   main   s    
r   __main__)N)N) r@   r   matplotlib.pyplotpyplotrm   matplotlib.colorsr   rb   numpyri   PILr   mintpymintpy.workflowmintpy.utilsr   r   r   utmimtpy.utilsr   r   r   r#   r(   rG   r   r   r   r   r   __name__r!   r!   r!   r"   <module>   s*   
(!$
