o
     [dj2                     @   s   d dl Z 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
mZ d dl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r[e  dS dS )    N)gdalosr)readfile	writefileptime)view)multitrack_utilitiesa  example:
    HDFEOS_to_geotif.py $SCRATCHDIR/BogdSenDT106/mintpy/S1_IW123_106_0441_0447_20161013_20191215.he5 mask
    HDFEOS_to_geotif.py $SCRATCHDIR/BogdSenDT106/mintpy/S1_IW123_106_0441_0447_20161013_20191215.he5 temporalCoherence --outdir $SCRATCHDIR/BogdSenDT106/
    HDFEOS_to_geotif.py $SCRATCHDIR/BogdSenDT106/mintpy/S1_IW123_106_0441_0447_20161013_20191215.he5 displacement --date 20191215 --mask
    
    HDFEOS_to_geotif.py $SCRATCHDIR/BogdSenDT106/mintpy/S1_IW123_106_0441_0447_20161013_20191215.he5 velocity --outdir $SCRATCHDIR/BalochistanSenDT151/test
     
    HDFEOS_to_geotif.py $SCRATCHDIR/BogdSenDT106/mintpy/S1_IW123_106_0441_0447_20161013_20191215.he5 displacement --date 20161013_20191215 --mask
    
    HDFEOS_to_geotif.py $SCRATCHDIR/BogdSenDT106/mintpy/S1_IW123_106_0441_0447_20161013_20191215.he5 displacement --date 20161013_20191215 --mask --bbox 45 46 101 102
    
c                  C   s   t jdt jtd} | jdddd | jdddd | jd	d
ddd | jddddd | jddtdddd | jdtdddd | S )NzCGenerate *.h5 and *.tiff file with WGS projection based HDFEOS file)descriptionformatter_classepiloginput_HDFEOS   zdirectory stored *.he5 files. 
)nargshelp	attributezchosen attribute. 
z--datedatez$date1 or date1_2 for displacement. 
)destr   r   z--mask
store_trueFzwhether mask outputfile. 
)actiondefaultr   z--bboxSNWE   )SNWEzBounding box of area to be geocoded.
Include the uppler left corner of the first pixeland the lower right corner of the last pixel)r   typer   metavarr   z--outdir?z./zoutput directory)r   r   r   r   )argparseArgumentParserRawTextHelpFormatterEXAMPLEadd_argumentfloatstr)parser r'   M/home/exouser/operations/rsmas_insar/tools/MimtPy/mimtpy/HDFEOS_to_geotiff.pycreate_parser   s   r)   c                 C   s   t  }|j| d}|S )N)args)r)   
parse_args)iargsr&   inpsr'   r'   r(   cmd_line_parse5   s   r.   c                 C   sd   d | j}g d}g d}g d}||v rd}||v rd}||v r0d}|dks,|d	kr0td
|S )z/find the dataset based on the given attribution )azimuthAngleheightincidenceAnglelatitude	longitude
shadowMaskslantRangeDistance)bperpr   displacementvelocity)masktemporalCoherenceZavgSpatialCoherencez"/HDFEOS/GRIDS/timeseries/geometry/z!/HDFEOS/GRIDS/timeseries/quality/z%/HDFEOS/GRIDS/timeseries/observation/r7   r   z%ERROR! This attributaion is 1D array!)joinr   	Exception)r-   attrZgeometry_attrZobservation_attrZquality_attrdatasetr'   r'   r(   located_dataset_based_attribut;   s   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 rIt  td
||	krUt  tdt	|| | d }
t	|| | d }t	|| | d d }t	|	| | d d }|
|||fS )z0return the row_no,sample_no and rows and samplesr/   Y_FIRSTX_FIRSTY_STEPX_STEPr      r      z/input bounding box error! Wrong latitude order!z0input bounding box error! Wrong longitude order!g      ?)
r   read_attributer<   r   r$   r   r&   print_usager=   int)r-   atrul_latul_lonlat_steplon_stepZ	user_lat0Z	user_lon0Z	user_lat1Z	user_lon1rowsamplerowssamplesr'   r'   r(   extract_data_based_bboxO   s(   rS   c                 C   s  t d| j}d| j}|dks|dkr| jdkr&|d }|d }n$dd| jv r=td| jd\}}n|d }td| j}|dkrt 	d| j}d}t
|d||d	 d	 }	t
|d||d	 d
 }
t jd| j|
dd	 }|t jd| j|	dd	 8 }d|||}| jrtd t jd| jddd	 }tj||d	k< |d | d }d|d< tj|||d n|dkrt jd| jddd	 }d}t jd| j|| dd	 }| jrtd tj|dd|d	kf< td| jd}||d  }||d  }d}|d | }t }||d< ||d< ||d< d|d< tj|||d d| d | d }|dd	 }t| tjdrgttd|d|d|ddd |g
}nttd|d|d|d |g}|d
krtd!t j|dd\}}|}| jrtd tj|| < tj|||d td" nAt jd| j|| dd	 }|d | d }|}||d< | jrtd t jd| jddd	 }tj||d	k< tj|||d |||fS )#z:extract data from HDFEOS file based on the given attributer/   r8   r9   N
START_DATEEND_DATE_z{}-{}r   r   )datasetNamez{}_{}_{}z	mask filez%/HDFEOS/GRIDS/timeseries/quality/mask/z.h5z.unw	FILE_TYPE)out_filemetadatarr7   r   ztimeseries_ERA_demErr.h5
timeseriesZ	velocity_.z../exclude_date.txtztimeseries2velocity.pyz--start-datez
--end-datez	--excludez--outputzerror when generate velocity!z../)r   rG   r<   r   r   r   r   yyyymmddsplitget_slice_listr   Zsearch_dataset_inputformatreadr:   printnpnanr   writeh5pyFiledictoschdirpathexistssystemr   Zseparate_string_by_spacer=   )r-   r?   outdirrJ   r>   date1date2
slice_listdnameZslice_name1Zslice_name2data	data_nameZmaskfileoutfileZdata_timeseriesZ
bperp_dateZ
data_bperpZ	data_dateZts_data_nameZdsDictZ
output_velZcompletion_statusZdata_velvel_atrr'   r'   r(   extract_datai   s   



& 


ry   c                 C   s"  t |d }t |d }t|d }t|d }t |d }t |d }	|}
|||d   }|}||	|d   }|}|}|	}t|}|d | d	 }td
| ||d|
d| g}td|||dtj}|	| t
 }|d ||  |d|  |  td| dS )geocode steprA   rB   WIDTHLENGTHrC   rD   r   rX   .tiffz#Start calculating geotiff data {}.
r   GTiff  z'Finishing calculating geotiff data {}.
N)r$   rI   absrd   rb   r   GetDriverByNameCreateGDT_Float32SetGeoTransformr   SpatialReferenceImportFromEPSGSetProjectionExportToWktGetRasterBand
WriteArray
FlushCache)ru   rv   rJ   rp   rK   rL   rR   rQ   rM   rN   ymaxyminxminxmaxnrowsncolsxresyres
output_tifgeotransformrastersrsr'   r'   r(   geocode   s2   

r   c                 C   s   t |d }t |d }t |jd }t |jd }| j\}	}
|}t|}|d | d }||d|d| g}td||
|	dtj}|| t	
 }|d	 ||  |d|  |  d
S )rz   rC   rD   r   rE   rX   r}   r   r~   r   N)r$   r   shaper   r   r   r   r   r   r   r   r   r   r   r   r   r   )ru   rv   r-   rJ   rp   rM   rN   r   r   r   r   r   r   r   r   r   r   r'   r'   r(   geocode_bbox   s    


r   c                 C   s0   t | }t|}|jd }t|||\}}}d S )Nr   )r.   r@   rp   ry   )Ziagrsr-   r?   rp   ru   rv   rJ   r'   r'   r(   main  s   
r   __main__)N)rk   r   rh   numpyre   osgeor   r   mintpy.utilsr   r   r   mintpyr   Zmimtpy.utilsr   r"   r)   r.   r@   rS   ry   r   r   r   __name__r'   r'   r'   r(   <module>   s(   
a#

