U
     c%+                     @   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	 d dl
mZmZmZmZmZ dZd"ddZd#dd	Zd$ddZd%ddZd&ddZdd Zd'ddZd(ddZdd Zdd Zd)ddZed kreejd!d  dS )*    N)readfileptime)create_argument_parser)giantIfgramStackgiantTimeseriesifgramStack
timeseriesHDFEOSa}  example:
  info.py timeseries.h5
  info.py velocity.h5
  info.py ifgramStack.h5

  # Display dataset
  info.py timeseries.py --dset date
  info.py timeseries.py --dset bperp

  # Time / Date Info
  info.py ifgramStack.h5 --date                 #print date1_date2 info for all  interferograms
  info.py timeseries.h5  --num                  #print date list of timeseries with its number
  info.py ifgramStack.h5 --date --show kept     #print date1_date2 info for kept interferograms
  info.py ifgramStack.h5 --date --show dropped  #print date1_date2 info for dropped/excluded interferograms
  info.py LS-PARAMS.h5   --date > date_list.txt #print date list of timeseries and save it to txt file.
  info.py S1_IW12_128_0593_0597_20141213_20180619.h5 --date

  # save date1_date2 info of interferograms to a text file
  info.py ifgramStack.h5 --date --show kept > date12_list.txt  

  # Slice / Dataset Info
  info.py timeseries.h5                              --slice
  info.py inputs/ifgramStack.h5                      --slice
  info.py S1_IW12_128_0593_0597_20141213_20180619.h5 --slice
  info.py LS-PARAMS.h5                               --slice
c                 C   s   d}t }tdd }t||||| d}|jdtdd |jdd	d
d |jdtdd |dd}|jddd	dd |jddd	dd |jddd	dd |jddddddhdd d! |S )"zCreate command line parser.z4Display Metadata / Structure information of ANY File.)synopsisdescriptionepilog
subparsersfilezFile to check)typehelpz	--compact
store_truez8show compact info by displaying only the top 20 metadata)actionr   z--dsetzShow datasetListzlist date/slice infoz--date	disp_datez#Show date/date12 info of input file)destr   r   z--numdisp_numz"Show date/date12 info with numbersz--slice
disp_slicezShow slice list of the filez--showz--show-ifgramdisp_ifgramallkeptdroppedz<Show all / kept / dropped interferograms only. Default: all.)r   choicesdefaultr   )EXAMPLE__name__splitr   add_argumentstradd_argument_group)r   r   r   nameparserZpar_list r(   B/home/exouser/operations/rsmas_insar/sources/MintPy/mintpy/info.pycreate_parser6   s>        



 r*   c                 C   s@   t  }|j| d}tj|js*t|jd|_|jr<d|_|S )zCommand line parser.)args      )	r*   
parse_argsospathisfiler   FileNotFoundErrormax_meta_numcompact)iargsr'   inpsr(   r(   r)   cmd_line_parseQ   s    
r7   Tr,   c                 C   s   t dd t|  D dg }d}d}t|  dd dD ]V\}}|d7 }||krb|d	7 } qq<z|d
}W n   Y nX |dj|||d7 }q<|S )Nc                 S   s   g | ]}t |qS r(   len).0keyr(   r(   r)   
<listcomp>b   s     z%attributes2string.<locals>.<listcomp>r    c                 S   s   | d S )Nr   r(   )xr(   r(   r)   <lambda>e       z#attributes2string.<locals>.<lambda>)r;      z  ...
utf8z  {k:<{d}}    {v}
)kdv)maxlistkeyssorteditemsdecodeformat)atrsortingr3   digits
atr_stringir;   valuer(   r(   r)   attributes2string`   s"     
rS   c                 C   s   t | |d}t| d S )Nr3   )rS   print)rM   r3   rP   r(   r(   r)   print_attributesv   s    rV   c                    s   da  fdd}t| d}t|j}t|dkrNt d7 a t t| dd 7 a td	d
 | D a	tdt	d a	|
dddkrt	d7 a	|| |  tt  t S )Nr=   c                    sv   t |tjrtdj| d7 an*t |tjrHtdj| tt|j|j	d7 at
|j}t|dkrrtt| dd 7 ad S )NzHDF5 group   "/{n}"
)nz5HDF5 dataset "/{n:<{w}}": shape {s:<20}, dtype <{t}>
)rW   wstr   rT   
)
isinstanceh5pyGroup	h5_stringrL   DatasetmaxDigitr$   shapedtypedictattrsr9   rS   )r&   objrM   rT   r(   r)   hdf5_structure2string   s    
z3print_hdf5_structure.<locals>.hdf5_structure2stringrr   zAttributes in / level:
rT   r[   c                 S   s   g | ]}t |qS r(   r8   )r:   rQ   r(   r(   r)   r<      s     z(print_hdf5_structure.<locals>.<listcomp>r-   rA   	FILE_TYPEr   r	   #   )r_   r]   Filerd   re   r9   rS   rF   rH   ra   get
visititemscloserU   )fnamer3   rg   frM   r(   rT   r)   print_hdf5_structure{   s    

rq   c                 C   s\   t | d }td| d  td| d  tdt|  tdt| d S )NrA   zStart Date: {}r   zEnd   Date: {}r   zNumber of dates  : {}zSTD of datetimes : {:.2f} years)r   date_list2vectorrU   rL   r9   npstd)dateList
datevectorr(   r(   r)   print_timseries_date_stat   s    rw   r   Fc                 C   sH  t | d }d}|dkr(t|  }n|dkr>t|  }n|dkrTt|  }n|dkrjt|  }n|dkrt| }|j	dd	 |jdd
}|jdd
}|dkrt
|}q|dkrt
|}qtt
t|t| }ntd| |rD|dk	rD|D ]D}	|r6|dkr||	}
n
||	}
d|	|
}n|	}t| q|S )zPrint time/date info of fileri   Nr   r	   r   )r   )r   F	print_msg)
dropIfgramTr   r   z7--date option can not be applied to {} file, ignore it.z{}	{})r   read_attributer   get_date_listr	   r   r   get_date12_listr   openrG   rI   setrU   rL   index)ro   r   r   rz   rC   ru   rf   ZdateListAllZdateListKeptrD   nummsgr(   r(   r)   print_date_list   s>    




r   c                 C   s$   t | }|r |D ]}t| q|S )zPrint slice info of file)r   get_slice_listrU   )ro   rz   
slice_listZ
slice_namer(   r(   r)   print_slice_list   s
    

r   c                 C   s   zt | }|d }tddd td|d   td|d   d| krZtd	 ntd
 |dkrt| }tddd t| W n   Y nX d S )Nri   z
{} {:*<40}********************zBasic File Info zfile name: 	FILE_PATHzfile type: Y_FIRSTzcoordinates : GEOzcoordinates : RADARrx   
{} {:*<40}zDate Stat Info )r   r|   rU   rL   rH   r   rw   )ro   rM   rC   ru   r(   r(   r)   print_aux_info   s     

r   c              	   C   s   dd }g a t| d}|| W 5 Q R X |t krVd|}|dt 7 }t|t| d}|| d d  }t| W 5 Q R X td|j tdt	|t
| tdtt| d S )	Nc                 S   s   t |tjrt|  d S )N)r\   r]   r`   dsNamesappend)r&   rf   r(   r(   r)   get_hdf5_dataset  s    z'print_dataset.<locals>.get_hdf5_datasetrh   zinput dataset {} not found!z
available datasets: {}zdataset size: {}zdataset min / max: {} / {}znumber of pixels in NaN: {})r   r]   rk   rm   rL   
ValueErrorrU   rb   rs   nanminnanmaxsumisnan)ro   dsNamer   rp   r   datar(   r(   r)   print_dataset   s    
r   c                 C   s   t | }tj|jd  }|js*|jrDt|j|j	|jdd d S |j
rn|j	dkr\tdt|jdd d S |jrt|j|jd d S t|j |dkrtd	d
d t|j|jd n*td	d
d t|j}t||jd d S )NrA   T)r   r   rz   r   z2--show-ifgram option is not applicable to --slice.ry   )r   )z.h5z.he5r   r   zHDF5 File Structure rT   zBinary File Attributes )r7   r/   r0   splitextr   lowerr   r   r   r   r   r   r   dsetr   r   rU   rL   rq   r3   r   r|   rV   )r5   r6   extrM   r(   r(   r)   main  s2    

r   __main__rA   )N)N)Tr,   )r,   )r,   )r   FF)F)N)r/   sysr]   numpyrs   mintpy.utilsr   r   mintpy.utils.arg_utilsr   mintpy.objectsr   r   r   r   r	   r    r*   r7   rS   rV   rq   rw   r   r   r   r   r   r!   argvr(   r(   r(   r)   <module>	   s(   





*
1
	
(