B
    QaE                 @   s   d dl Z 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
mZ d dlmZ d dlmZmZ d dlmZ ed Zed ZedZedZdZed ZZd	Zd
d Zd!ddZ d"ddZ!d#ddZ"dd Z#d$ddZ$defddZ%dd Z&d%ddZ'e(dkre'ej)d d  dS )&    N)
timeseriesgeometrysensor)get_template_content)ptimereadfile)infog        lzfhdfeos5zexample:
  save_hdfeos5.py geo/geo_timeseries_ERA5_ramp_demErr.h5
  save_hdfeos5.py timeseries_ERA5_ramp_demErr.h5 --tc temporalCoherence.h5 --asc avgSpatialCoh.h5 -m maskTempCoh.h5 -g inputs/geometryGeo.h5
c              C   s   t jdt jtd t d} | j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 | jddddd | jdddd  | jd!dd"d  | S )#NzConvert MintPy timeseries product into HDF-EOS5 format
  https://earthdata.nasa.gov/esdis/eso/standards-and-references/hdf-eos5
  https://mintpy.readthedocs.io/en/latest/hdfeos5/
)descriptionformatter_classepilogts_fileztimeseries.h5zTimeseries file)defaulthelpz-tz
--templatetemplate_filez>Template file for 1) arguments/options and 2) missing metadata)destr   z--tcz
--temp-coh	tcoh_filez5Coherence/correlation file, i.e. temporalCoherence.h5z--ascz--avg-spatial-coh	scoh_filez5Average spatial coherence file, i.e. avgSpatialCoh.h5z-mz--mask	mask_filez	Mask filez-gz
--geometry	geom_filezgeometry filez--update
store_truezREnable update mode, a.k.a. put XXXXXXXX as endDate in filename if endDate < 1 year)actionr   z--subsetzDEnable subset mode, a.k.a. put suffix _N31700_N32100_E130500_E131100)argparseArgumentParserRawDescriptionHelpFormatterTEMPALTEEXAMPLEadd_argument)parser r!   I/home/centos/operations/rsmas_insar/sources/MintPy/mintpy/save_hdfeos5.pycreate_parser&   s$    


r#   c       	      C   s  t  }|j| d}tj|j}tj|jdrntj|d}tj|d}tj|d}tj|d}n8tj|d}tj|d}tj|d	}tj|d
}|j	s||_	|j
s||_
|js||_|js||_x4|j|j	|j
|j|jgD ]}tj|st|qW |S )N)argsgeo_zgeo_temporalCoherence.h5zgeo_avgSpatialCoh.h5zgeo_maskTempCoh.h5zgeo_geometryRadar.h5ztemporalCoherence.h5zavgSpatialCoh.h5zmaskTempCoh.h5zinputs/geometryGeo.h5)r#   
parse_argsospathdirnamer   basename
startswithjoinr   r   r   r   isfileFileNotFoundError)	iargsr    inpsZts_dirr   r   r   r   fnamer!   r!   r"   cmd_line_parse?   s0        r2   c             C   s   |s
t  }| s|dfS tdtj|   t| }d}|d }|| kr^|| dkr^d|_|d }|| kr|| dkrd|_	||fS )z/Read input template options into Namespace inpsNz!read options from template file: zmintpy.save.hdfEos5.updateyesTsubset)
r2   printr'   r(   r*   r   read_templatekeysr3   r5   )r   r0   templateprefixkeyr!   r!   r"   read_template2inps]   s    
r<   Tc             C   s   t | }|jdd t|j}x&| D ]\}}|ds(|||< q(W t||j}|rxtd td t	
| td || d|d< |S )z#Prepare metadata for HDF-EOS5 file.F)	print_msg)mintpyiscez## UNAVCO Metadata:z)-----------------------------------------HDFEOS	FILE_TYPE)r   opendictmetadataitemsr+   metadata_mintpy2unavcodateListr6   r   Zprint_attributesr3   )r   r9   r=   ts_objmetar;   valueunavco_metar!   r!   r"   prep_metadataw   s    



rL   c             C   sF  i }xT|   D ]H}| | ||< x6dD ].}|| kr$| |d }| | ||< q$W qW t }yt||d< W n tk
r   td Y nX |d |d< t|	dd|d< t|d |d< d	|d
< |	dd|d< t
j|d d dd |d< t
j|d d dd |d< |d |d |d |d |d g}|d |d |d |d |d g}dddd t||D  d  |d!< t
j  dd |d"< t|	d#d|d#< t|	d$|d# |d$< |	d%d&|d%< d'|d(< |	d)d*|d)< |	d+d*|d+< |	d,d*d  |d-< |d. d/kr4d0|d1< nd2|d1< |	d3d*|d4< t|	d5d|d6< t|d7 |d8< d9|  kr:t|d9 }	t|d: }
|	t|d; t|d<   }|
t|d= t|d>   }t|	t|t|t|	t|	g}t|
t|
t|t|t|
g}ddd?d t||D  d  |d@< ntdA |S )Bz7Convert metadata from mintpy format into unavco format.)zunavco.zhdfeos5.   missionz#Missing required attribute: mission	beam_mode
beam_swath0relative_orbitZLOS_TIMESERIESZprocessing_type	PROCESSORr?   Zprocessing_softwarer   z%Y%m%d
   
first_date	last_dateZLON_REF1ZLON_REF3ZLON_REF4ZLON_REF2ZLAT_REF1ZLAT_REF3ZLAT_REF4ZLAT_REF2z	POLYGON((,c             S   s   g | ]\}}|d  | qS ) r!   ).0lonlatr!   r!   r"   
<listcomp>   s    z*metadata_mintpy2unavco.<locals>.<listcomp>z))Zscene_footprinthistoryfirst_frame
last_frameZatmos_correct_methodNoneZMintPyZpost_processing_methodZprocessing_demUnknownZunwrap_methodORBIT_DIRECTIONZflight_directionANTENNA_SIDEz-1RZlook_directionLPOLARIZATIONpolarizationPRFprf
WAVELENGTH
wavelengthX_FIRSTY_FIRSTX_STEPWIDTHY_STEPLENGTHc             S   s   g | ]\}}|d  | qS )rY   r!   )rZ   r[   r\   r!   r!   r"   r]      s    Zdata_footprintzJInput file is not geocoded, no data_footprint without X/Y_FIRST/STEP info.)r8   lowersplitrC   r   get_unavco_mission_name
ValueErrorr6   intgetdtdatetimestrptime	isoformatr,   ziputcnowupperfloatstr)Zmeta_inrG   rI   r;   r:   key2rK   lonslatslon0lat0lon1lat1r!   r!   r"   rF      sl    
""

&
""(rF   Fc             C   s(  | d }| d }| d r(|t | d 7 }dt| d  }| d }| d }dt| }||krh|d	| 7 }tj| d
 dd}	tj| d dd}
|rtd d}
|d | d | d | d |	 d |
 d }|r$td t| d }t| d }|t| d t| d   }|t| d t| d   }dt|d  }dt|d  }dt|d  }dt|d  }|dk rdtt	|d  }|dk rdtt	|d  }|dk rdtt	|d  }|dk rdtt	|d  }d||||f }d 
tj|d! |tj|d" }|S )#z2Get output file name of HDF-EOS5 time-series file.rN   rO   rP   z%03drR   r_   r`   z%04dz_%04drU   z%Y-%m-%dz%Y%m%drW   z+Update mode is ON, put endDate as XXXXXXXX.ZXXXXXXXX_z.he5zASubset mode is enabled, put subset range info in output filename.rn   rm   rq   rr   ro   rp   zN%05dg     @@zE%06dg        zS%05dzW%06dz_%s_%s_%s_%sz{}{}{}r   rM   )r   rw   ry   rz   r{   strftimer6   r   roundabsformatr'   r(   splitext)rD   update_modesubset_modeSATSWZRELORBZframe1Zframe2FRAMEZDATE1ZDATE2outNamer   r   r   r   Zlat0StrZlat1StrZlon0StrZlon1StrZSUBr!   r!   r"   get_output_filename   sN    0
 
 
 
 r   7   c             C   s   dj d | j||d}|dj t|j|jd7 }|dj |d7 }t| |jdkrf| j|||d	}n|jd
kr| j||d|d}|S )z*Create HDF5 dataset and print out message.zcreate dataset {d:<{w}}z{}/{})dwz of {t:<10} in size of {s})tsz with compression={c})crM   )datacompression   T)r   chunksr   )r   namer   dtypeshaper6   ndimcreate_dataset)groupdsNamer   	max_digitr   msgdsetr!   r!   r"   create_hdf5_dataset#  s    


r   c          	   C   s  t |}|jdd |j}t|}	t|d}
td| d}d}td| |
|}d}|	|j	|j
f}tj}td	jd
|||d|td |j||d|d |d f|dtd}td tj|	d}x\t|	D ]P}tj||| dd ||ddddf< |j|d d|d |	|| d qW |  ||jd< t|jd< t|jd< d|jd< d}tj|tjd}t|||}d}tj|jtjd}t|||}d }td| |
|}d!}t|d }t|||}||jd< t|jd< t|jd< d"|jd< d#}t|d }t|||}||jd< t|jd< t|jd< d"|jd< d$}tj|d$dd }t|||}||jd< t|jd< t|jd< d"|jd< d%}td| |
|}t|}|jdd x|jD ]}|j|dd&}t|||}||jd< |d'krt|jd< t|jd< d|jd< n||d(krBt|jd< t|jd< d)|jd< nR|d*krlt|jd< t|jd< d"|jd< n(|d+krt|jd< t|jd< d"|jd< qW td, x$t|   D ]\}}||
j|< qW |
  td-| |S ).z#Write HDF5 file in HDF-EOS5 format.F)r=   r   z create HDF5 file: {} with w moder   z#HDFEOS/GRIDS/timeseries/observationzcreate group   /{}displacementzGcreate dataset /{d:<{w}} of {t:<10} in size of {s} with compression={c}z{}/{}float32)r   r   r   r   r   NrM   r   T)r   maxshaper   r   r   z%write data acquition by acquition ...)maxValue)datasetNamer   z{}/{} {})suffixZTitleZMissingValue
_FillValuemetersZUnitsdate)r   bperpzHDFEOS/GRIDS/timeseries/qualitytemporalCoherence1ZavgSpatialCoherencemaskz HDFEOS/GRIDS/timeseries/geometry)r   r=   )heightslantRangeDistancer   )incidenceAngleazimuthAnglelatitude	longitudedegrees)
rangeCoordazimuthCoord)	waterMask
shadowMaskzwrite metadata to root levelzfinished writing to {})!r   rB   rG   lenh5pyFiler6   r   create_grouplengthwidthnpr   COMPRESSIONr   r   progressBarranger   readr3   closeattrs
FLOAT_ZEROarraystring_r   pbase	BOOL_ZEROr   datasetNamesiterrE   )rD   out_filer   r   r   r   r   rH   rG   numDatefr   ZgNamer   r   dsShape
dsDataTyper   prog_barir   geom_objr;   rJ   r!   r!   r"   write_hdf5_file6  s    


((






























r   c          	   C   s^   t | }t|j|\}}t|j|dd}t||j|jd}t|||j|j	|j
|j|jd |S )NT)r   r9   r=   )rD   r   r   )rD   r   r   r   r   r   r   )r2   r<   r   rL   r   r   r3   r5   r   r   r   r   r   )r/   r0   r9   rI   r   r!   r!   r"   main  s     

r   __main__rM   )N)N)NT)FF)N)*r'   sysr   rz   ry   r   numpyr   mintpy.objectsr   r   r   mintpy.defaults.templater   mintpy.utilsr   r   r>   r   bool_r   int16ZINT_ZEROr   r   	complex64ZCPX_ZEROr   r   TEMPLATEr   r#   r2   r<   rL   rF   r   r   r   r   __name__argvr!   r!   r!   r"   <module>	   s8   






c
. 

