U
    bbF                     @   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)info        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# | 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--suffixsuffixz-suffix to be appended to file name (e.g. PS).z--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&   s0    



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||_|j|j	|j
|j|jfD ]}tj|st|q|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        
r4   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)
r4   printr)   r*   r,   r   read_templatekeysr5   r7   )r   r2   templateprefixkeyr#   r#   r$   read_template2inps^   s    
r>   Tc                 C   s   t | }|jdd t|j}| D ]\}}|ds&|||< q&t||j}|rtt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dateListr8   r   Zprint_attributesr5   )r   r;   r@   ts_objmetar=   valueunavco_metar#   r#   r$   prep_metadatax   s    




rO   c                 C   s>  i }|   D ]D}| | ||< dD ].}|| kr | |d }| | ||< q qt }z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r,d0|d1< nd2|d1< |	d3d*|d4< t|	d5d|d6< t|d7 |d8< d9|  kr2t|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	PROCESSORrB   Zprocessing_softwarer   %Y%m%d
   
first_date	last_dateLON_REF1LON_REF3LON_REF4LON_REF2LAT_REF1LAT_REF3LAT_REF4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 re   r#   rg   r#   r#   r$   rk      s     Zdata_footprintzJInput file is not geocoded, no data_footprint without X/Y_FIRST/STEP info.)r:   lowersplitrF   r   get_unavco_mission_name
ValueErrorr8   intgetdtdatetimestrptime	isoformatr.   ziputcnowupperfloatstr)Zmeta_inrJ   rL   r=   r<   key2rN   lonslatslon0lat0lon1lat1r#   r#   r$   rI      s    ""
""
rI   Fc                 C   sf  | 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}|s|d | d | d |	 d |
 d | d }n8|d | d | d |	 d |
 d | d | d }|rbt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 rdtt	|d  }|dk r,dtt	|d  }d||||f }d 
tj|d! |tj|d" }|S )#z2Get output file name of HDF-EOS5 time-series file.rQ   rR   rS   z%03drU   rm   rn   z%04dz_%04drY   z%Y-%m-%drW   r[   z+Update mode is ON, put endDate as XXXXXXXX.ZXXXXXXXX_z.he5zASubset mode is enabled, put subset range info in output filename.r|   r{   r   r   r}   r~   zN%05dg     @@zE%06dr	   zS%05dzW%06dz_%s_%s_%s_%sz{}{}{}r   rP   )r   r   r   r   r   strftimer8   r   roundabsformatr)   r*   splitext)rG   r   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   sT    28
 
 
 
 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}}{}/{})dwz of {t:<10} in size of {s})tsz with compression={c})crP   )datacompression   T)r   chunksr   )r   namer   dtypeshaper8   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}t|	D ]P}tj||| dd ||ddddf< |j|d d|d |	|| d q|  ||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 |jD ]}|j|dd&}t|||}||jd< |d'krt|jd< t|jd< d|jd< n||d(kr<t|jd< t|jd< d)|jd< nR|d*krft|jd< t|jd< d"|jd< n(|d+krt|jd< t|jd< d"|jd< qtd, t|   D ]\}}||
j|< q|
  td-| |S ).z#Write HDF5 file in HDF-EOS5 format.Fr?   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}r   float32)r   r   r   r   r   NrP   r   T)r   maxshaper   r   r   z%write data acquition by acquition ...)maxValue)datasetNamer   z{}/{} {})r   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   rE   rJ   lenh5pyFiler8   r   create_grouplengthwidthnpr   COMPRESSIONr   r   progressBarranger   readr5   closeattrs
FLOAT_ZEROarraystring_r   pbase	BOOL_ZEROr   datasetNamesiterrH   )rG   out_filer   r   r   r   r   rK   rJ   numDatefr   ZgNamer   r   dsShape
dsDataTyper   prog_barir   geom_objr=   rM   r#   r#   r$   write_hdf5_file:  s    

(&































r   c              	   C   sb   t | }t|j|\}}t|j|dd}t||j|j|jd}t	|||j|j
|j|j|jd |S )NT)r   r;   r@   )rG   r   r   r   )rG   r   r   r   r   r   r   )r4   r>   r   rO   r   r   r   r5   r7   r   r   r   r   r   )r1   r2   r;   rL   r   r#   r#   r$   main  s(    r   __main__rP   )N)N)NT)NFF)N)*r)   sysr   r   r   r   numpyr   mintpy.objectsr   r   r   mintpy.defaults.templater   mintpy.utilsr   r   rA   r   bool_r   int16ZINT_ZEROr   r   	complex64ZCPX_ZEROr   r   TEMPLATEr    r%   r4   r>   rO   rI   r   r   r   r   __name__argvr#   r#   r#   r$   <module>	   s8   






c
1 

