B
    QaZ!                 @   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Zd dlm	Z	 d dl
mZ d dlmZ d dlmZmZmZmZmZ edZdZdd	 Zdd
dZdddZdd ZdddZdddZedkreejdd  dS )    N)
timeseries)split_box2sub_boxes)get_template_content)	arg_groupreadfile	writefileptimeutilsreference_datezexample:
  reference_date.py timeseries.h5 timeseries_ERA5.h5 timeseries_ERA5_demErr.h5 --template smallbaselineApp.cfg
  reference_date.py timeseries_ERA5_demErr.h5 --ref-date 20050107
c              C   s~   t jdt jtd t d} | jdddd | jdd	d
ddd | jddddd | jdddd | jdddd t| } | S )Nz$Change reference date of timeseries.
)descriptionformatter_classepilogtimeseries_file+ztimeseries file(s))nargshelpz-rz
--ref-daterefDateZminRMSzreference date or method, default: auto. e.g.
20101120
time-series HDF5 file with REF_DATE in its attributes
reference_date.txt - text file with date in YYYYMMDD format in it
minRMS             - choose date with min residual standard deviation)destdefaultr   z-tz
--templatetemplate_fileztemplate file with options)r   r   z-oz	--outfilezOutput file name.)r   z--force
store_truezForce updating the data matrix.)actionr   )argparseArgumentParserRawTextHelpFormatterTEMPLATEEXAMPLEadd_argumentr   add_memory_argument)parser r!   K/home/centos/operations/rsmas_insar/sources/MintPy/mintpy/reference_date.pycreate_parser    s    

r#   c             C   sH   t  }|j| d}t|jd }d|d  krDtd|d |S )N)argsr   r   	FILE_TYPEz&input file type: {} is not timeseries.)r#   
parse_argsr   read_attributer   lower
ValueErrorformat)iargsr    inpsatrr!   r!   r"   cmd_line_parse8   s    r.   c             C   sj   |s
t  }t| }t|}d}|| kr@|| r@|| |_d}|| krf|| rft|| |_|S )z*Update inps with options from templateFilezmintpy.reference.datezmintpy.compute.maxMemory)	r.   r   read_templateutcheck_template_auto_valuekeysr   float	maxMemory)templateFiler,   templatekeyr!   r!   r"   read_template2inpsC   s    


r8   c             C   s   | j std d S | j  rnhtj| j rrtd| j   | j dr^t| j }|d | _ qt	
| j d | _ ntd| j  d S t	| j | _ td| j  t| jd  }| j |krd| j }|d	|7 }t|| j S )
Nz(No reference date input, skip this step.zread reference date from file: z.h5REF_DATEr   z,input file {} does not exist, skip this stepzinput reference date: {}z&input reference date: {} is not found.z
All available dates:
{})r   printisdigitospathisfileendswithr   r'   r   read_date_txtr*   yyyymmddr   r   get_date_list	Exception)r,   r-   	date_listmsgr!   r!   r"   read_ref_dateU   s*    

rF         @Fc          
   C   sv  t j| } |s| }t j|}td td|  t| }|d }||ddkr|std || krxtd | S td| | t	| | |S t
| }|jd	d
 |j}|j}	|j}
|j|}tt||	 |
 d d |d  }tdd|
|	f|ddd}|| krd}nd}tj|| d x
t|D ]\}}|d |d  }|d |d  }|dkrtd|d | td| td| td tj| |dd }td |j}|t||ddddf d|d |d |d ddf8 }d||d |d |d |d f}tj|||||d q6W td| t|d}||jd< ||jd< W dQ R X |S ) a?  Change input file reference date to a different one.
    Parameters: ts_file : str, timeseries file to be changed
                ref_date : str, date in YYYYMMDD format
                outfile  : if str, save to a different file
                           if None, modify the data value in the existing input file
    z2--------------------------------------------------z"change reference date for file: {}r%   r9   Nz3input refDate is the same as the existing REF_DATE.zNothing to be done.zCopy {} to {}F)	print_msg      i   @r   yT)box	num_split	dimensionrH   zr+a)ref_file      z5
------- processing patch {} out of {} --------------zbox width:  {}zbox length: {}zreading data ...)rL   zreferencing in time ...)datadatasetNameblockmodez'update "REF_DATE" attribute value to {}	FILE_PATH) r<   r=   abspathr:   r*   r   r'   getshutilcopy2r   opennumDatelengthwidthdateListindexintnpceilr   r   layout_hdf5	enumeratereadshapetilereshapewrite_hdf5_blockh5pyFileattrs)ts_fileref_dateoutfile
max_memoryforcer-   dsNameobjnum_dater^   r_   ref_idxnum_boxbox_listrV   irL   Z	box_widthZ
box_lengthts_datadshaperU   fr!   r!   r"   change_timeseries_ref_datew   sl    
&

> 
r~   c             C   s   t | }t }|jr"t|j|}t||_|jrfx2|jD ](}t||j|j|j	|j
d td q:W tt | d\}}td|| d S )N)rp   rq   rr   rs   rR   <   z'time used: {:02.0f} mins {:02.1f} secs.)r.   timer   r8   rF   r   r   r~   rq   r4   rs   sleepdivmodr:   r*   )r+   r,   
start_timero   msr!   r!   r"   main   s     

r   __main__rR   )N)N)NrG   F)N)r<   sysr   rZ   r   rl   numpyrc   mintpy.objectsr   mintpy.objects.clusterr   mintpy.defaults.templater   mintpy.utilsr   r   r   r   r	   r0   r   r   r#   r.   r8   rF   r~   r   __name__argvr!   r!   r!   r"   <module>	   s(   

"
V
