B
    Qa+                 @   s   d dl Z 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mZmZ dZ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)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 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 timeseries.h5  --date --num           # print date list of timeseries with its number
  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 timeseries.h5                              --slice  --num
  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   t jdt j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)descriptionformatter_classepilog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--slice
disp_slicezShow slice list of the filez--numdisp_numzShow date/date12/slice numberz--showz--show-ifgramdisp_ifgramallkeptdroppedz<Show all / kept / dropped interferograms only. Default: all.)r   choicesdefaultr   )argparseArgumentParserRawTextHelpFormatterEXAMPLEadd_argumentstradd_argument_group)parserZpar_list r$   A/home/centos/operations/rsmas_insar/sources/MintPy/mintpy/info.pycreate_parser7   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_parseO   s    
r3   Tr(   c             C   s   t dd t|  D dg }d}d}xlt|  dd dD ]T\}}|d7 }||krb|d	7 }P q>y|d
}W n   Y nX |dj|||d7 }q>W |S )Nc             S   s   g | ]}t |qS r$   )len).0keyr$   r$   r%   
<listcomp>`   s    z%attributes2string.<locals>.<listcomp>r    c             S   s   | d S )Nr   r$   )xr$   r$   r%   <lambda>c       z#attributes2string.<locals>.<lambda>)r6      z  ...
utf8z  {k:<{d}}    {v}
)kdv)maxlistkeyssorteditemsdecodeformat)atrsortingr/   digits
atr_stringir6   valuer$   r$   r%   attributes2string^   s      rN   c             C   s   t | |d}t| d S )N)r/   )rN   print)rH   r/   rK   r$   r$   r%   print_attributest   s    rP   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 )Nr8   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}>
)rQ   wstr   )r/   
)
isinstanceh5pyGroup	h5_stringrG   DatasetmaxDigitr!   shapedtypedictattrsr4   rN   )nameobjrH   )r/   r$   r%   hdf5_structure2string~   s    
z3print_hdf5_structure.<locals>.hdf5_structure2stringrr   zAttributes in / level:
)r/   rU   c             S   s   g | ]}t |qS r$   )r4   )r5   rL   r$   r$   r%   r7      s    z(print_hdf5_structure.<locals>.<listcomp>r)   r<   	FILE_TYPEr   r   #   )rY   rW   Filer^   r_   r4   rN   rA   rC   r[   get
visititemscloserO   )fnamer/   rb   frH   r$   )r/   r%   print_hdf5_structurey   s    

rl   c             C   s\   t | d }td| d  td| d  tdt|  tdt| d S )Nr<   zStart Date: {}r   zEnd   Date: {}zNumber of dates  : {}zSTD of datetimes : {:.2f} years)r   date_list2vectorrO   rG   r4   npstd)dateList
datevectorr$   r$   r%   print_timseries_date_stat   s    rs   r   Fc             C   sN  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| |rJ|dk	rJxN|D ]F}	|r8|dkr ||	}
n
||	}
d|	|
}n|	}t| q W |S )zPrint time/date info of filerd   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   openrB   rD   setrO   rG   index)rj   r   r   rt   r>   rq   ra   ZdateListAllZdateListKeptr?   nummsgr$   r$   r%   print_date_list   s>    




r~   c             C   sF   t | }|rBx2tt|D ]"\}}t|r8| d| n| qW |S )zPrint slice info of file	)r   get_slice_listranger4   rO   )rj   r   rt   
slice_listrL   Z
slice_namer$   r$   r%   print_slice_list   s
    
r   c             C   s   yt | }|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 )Nrd   z
{} {:*<40}z********************zBasic File Info zfile name: 	FILE_PATHzfile type: Y_FIRSTzcoordinates : GEOzcoordinates : RADAR)r   z
{} {:*<40}zDate Stat Info )r   rv   rO   rG   rC   r~   rs   )rj   rH   r>   rq   r$   r$   r%   print_aux_info   s     

r   c          	   C   s   dd }g a t| d}|| W d Q R X |t krVd|}|dt 7 }t|t| d}|| d d  }t| W d 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)rV   rW   rZ   dsNamesappend)r`   ra   r$   r$   r%   get_hdf5_dataset  s    z'print_dataset.<locals>.get_hdf5_datasetrc   zinput dataset {} not found!z
available datasets: {}zdataset size: {}zdataset min / max: {} / {}znumber of pixels in NaN: {})r   rW   rf   rh   rG   
ValueErrorrO   r\   ro   nanminnanmaxsumisnan)rj   dsNamer   rk   r}   datar$   r$   r%   print_dataset   s    
r   c             C   s   t | }tj|jd  }|jr>t|j|j|j	dd d S |j
rl|jdkrVtdt|j|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 )Nr<   T)r   r   rt   r   z2--show-ifgram option is not applicable to --slice.)r   rt   )r   )z.h5z.he5z
{} {:*<40}z********************zHDF5 File Structure )r/   zBinary File Attributes )r3   r+   r,   splitextr   lowerr   r~   r   r   r   r   r   dsetr   r   rO   rG   rl   r/   r   rv   rP   )r1   r2   extrH   r$   r$   r%   main  s4    

r   __main__r<   )N)Tr(   )r(   )r(   )r   FF)FF)N)r+   sysr   rW   numpyro   mintpy.utilsr   r   mintpy.objectsr   r   r   r   r   r   r&   r3   rN   rP   rl   rs   r~   r   r   r   r   __name__argvr$   r$   r$   r%   <module>	   s(   $



*
1
	
*