
    Zf3T                         d Z ddlZddlZddlZddlmZ ddlmZm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZmZmZ ddl ddl dd	Z	 	 dd
Z	 	 ddZddZddZd ZddZy)z0Miscellaneous utilities - dependent on utils0/1.    N)map_coordinates)GEOMETRY_DSET_NAMESgeometryifgramStack
timeseries)
coordinate)resample)	attributeptimereadfile)*c                 v	   | st        j                         } t         j                  j                  |       } t         j                  j	                  | d      }t        j                  |      d   }|dk(  rd}nd}t         j                  j	                  | d      }g d}t        |d	      rt        |      j                  d
       t        fd|D              r%d| d}|dj	                  |      z  }t        |      dj                  v r\dj                  vrNt        d|        n?t        t        j                   t        j"                  t        j                         ||z         t        j$                  |      }	d|	j'                         v rdnd}
|	d   }t         j                  j	                  | dd|
j)                          d      }t*        d   }t        |d	      r<t-        |      j                  d
       |j                  vrPt        d| d|       t        t        j                   t        j"                  t        j                         ||z         t         j                  j	                  | d      }t/        |d|      }|
dk(  r|qt-        |      j                  d
       |d v r
t*        d!d" }n|d#v r
t*        d"d$ }nd%| d&}t1        |      |D ]!  }|j                  vst3        d| d|        nGt        t        j                   t        j"                  t        j                         |      t        d'       t         j                  j	                  | d(      }d}t        |d	      rgt        |      j                  d
       |j                  vrt        d| d|       dj                  v rdj                  vrt        d|        nd}|r|rt         j                  j5                  |      n|}|rt         j                  j5                  |      n|}|rt         j                  j5                  |      n|}|rt         j                  j5                  |      n|}|rBd)| }|d*|
 d+z  }|d,| z  }||rd-| ndz  }|d.| z  }|d/| z  }|d0z  }t        |       ||||fS )1a  Check the loaded input files, following two rules:
        1. file existence
        2. file attribute readability

    Parameters: work_dir    - str, MintPy working directory
                print_msg   - bool, print out message
    Returns:    stack_file  - str, path to the interferogram stack file
                geom_file   - str, path to the geometry file
                lookup_file - str, path to the look up table file, for radar-coord dataset only.
                ion_file    - str, path to the ionosphere stack file
    Example:    work_dir = os.path.expandvars('./FernandinaSenDT128/mintpy')
                stack_file, geom_file, lookup_file = ut.check_loaded_dataset(work_dir)[:3]
    zsmallbaselineApp.cfgzmintpy.load.processorariazP. Re-run "prep_aria.py" as printed out in "load_data" step for more information! zinputs/ifgramStack.h5)unwrapPhaserangeOffsetazimuthOffsetT)abspathF	print_msgc              3   :   K   | ]  }|j                   v  y w)N)datasetNames).0xobjs     K/home/exouser/operations/rsmas_insar/tools/MintPy/src/mintpy/utils/utils.py	<genexpr>z'check_loaded_dataset.<locals>.<genexpr>A   s     9Qq(((9s   z$required dataset is missing in file z:
z OR r   	coherencez(WARNING: "coherence" is missing in file Y_FIRSTGEORADAR	PROCESSORinputsr   z.h5r   zrequired dataset "z" is missing in file zinputs/geometry*.h5)r   r   N)iscedoris      )gammaroipac   zUnknown InSAR processor: z to locate look up table!z8Input data seems to be geocoded. Lookup file not needed.zinputs/ionStack.h5z0Loaded dataset are processed by InSAR software: z
Loaded dataset are in z coordinatesz
Interferogram Stack: z
Ionosphere    Stack: z
Geometry      File : z
Lookup Table  File : z3
--------------------------------------------------)osgetcwdpathr   joinr   read_templateis_file_existr   openall
ValueErrorr   printFileNotFoundErrorerrnoENOENTstrerrorread_attributekeys
capitalizer   r   get_lookup_fileAttributeError	Exceptionrelpath)work_dirr   r?   template_fileprocmsg_aria
stack_filednamesmsgatr
coord_type	processor	geom_filednamelookup_fileion_filer   s                   @r   check_loaded_datasetrN      s~    99;wwx(H GGLL+ABM!!-01HIDv~e h(?@J<FZ.*%5!9&998CHC6;;v&&CS/! C,,,CDTDT1T<ZLIJbkk%,,.GV^I^__ 
!
!*
-C#sxxz1wJK I Xx8J<Q<Q<S;TTW1XYI"EY-y!5!(((1%8Mi[YZZbkk%,,.GU]I]^^ '',,x)>?K!+tyQKW";'CHHuH% --,Qq111,Qq11)<UV$S)) d 0 00#&8?TU`Ta$bccd $ELL"++ell2K[YYHI ww||H&:;HEXt,(#5!(((1%8MhZXYY C,,,CDTDT1T<XJGH6@bggooj1z
6?bggooi0y	6Abggook2{6>bggooh/x A)M)*\BB(55x(
3RG(44(66c
y+x77    c                 8   t        j                  |      }t        ||      }|j                  | |      dd \  }}|
r"t	        d|  d|        t	        d| d|        d\  }}||j                  ||      dd \  }}t        |||||||||	d	
      \  }}}|||fS )
a   Read time-series of one pixel with input lat/lon
    Parameters: lat/lon     - float, latitude/longitude
                ts_file     - string, filename of time-series HDF5 file
                lookup_file - string, filename of lookup table file
                ref_lat/lon - float, latitude/longitude of reference pixel
                zero_first  - bool, shift the time-series so that it starts from zero
                win_size    - int, windows size centered at point of interest
                unit        - str, output displacement unit
                method      - str, method to calculate the output displacement and its dispersity
    Returns:    dates       - 1D np.ndarray of datetime.datetime objects, i.e. datetime.datetime(2010, 10, 20, 0, 0)
                dis         - 1D np.ndarray of float32, displacement
                dis_std     - 1D np.ndarray of float32, displacement dispersity
    )rL   r      zinput lat / lon:  / zcorresponding y / x: )NNF)ref_yref_x
zero_firstwin_sizeunitmethodr   )r   r9   r   	geo2radarr4   read_timeseries_yx)latlonts_filerL   ref_latref_lonrU   rV   rW   rX   r   rG   coordyr   rS   rT   datesdisdis_stds                       r   read_timeseries_lalore      s     
!
!'
*Cs4E??3$Qq)DAq!#c#/0%aSA3/0 LE5w81=u -Q738388B6>264:7<>E3 #wrO   c
                    t        |      }
|
j                  d       t        j                  |
j                        d   }t        j                  |      }|	rt        d|  d|        || |dz   | dz   f}t        j                  ||      d   }d}|dk7  rt        |d	z        }||z
  | |z
  ||z   dz   | |z   dz   f}t        j                  ||      d   j                  |
j                  d
      }|dk(  r/t        j                  |d      }t        j                  |d      }n6|dk(  r#t        j                  |d      }t!        |      }nt#        d|       |)|||dz   |dz   f}|t        j                  ||      d   z  }|r||d   z  }|dk(  rn6|dk(  r|dz  }|dn|dz  }n"|dk(  r|dz  }|dn|dz  }nt#        d|       |||fS )aE   Read time-series of one pixel with input y/x
    Parameters: y/x        - int, row/column number of interest
                ts_file    - string, filename of time-series HDF5 file
                ref_y/x    - int, row/column number of reference pixel
                zero_first - bool, shift the time-series so that it starts from zero
                win_size   - int, windows size centered at point of interest
                unit       - str, output displacement unit
                method     - str, method to calculate the output displacement and its dispersity
    Returns:    dates      - 1D np.ndarray of datetime.datetime objects, i.e. datetime.datetime(2010, 10, 20, 0, 0)
                dis        - 1D np.ndarray of float32, displacement
                dis_std    - 1D np.ndarray of float32, displacement dispersity
    Fr   r   zinput y / x: rR   r&   )boxNrQ   mean)axismedianzun-recognized method: mcmg      Y@mmg     @@zun-supported output unit: )r   r1   r   date_list2vectordateListnparrayr4   r   readintreshapenumDatenanmeannanstd	nanmedianmedian_abs_deviationr3   )ra   r   r]   rS   rT   rU   rV   rW   rX   r   r   rb   rg   rc   rd   bufbox_windis_winref_boxs                      r   rZ   rZ      s     W
CHHuH""3<<03EHHUOE aSA3'(a1ac
C
--S
)!
,CG1}(Q,S5!C%3q!C%'2--W5a8@@bQV**W1-Ciia0Gx,,wQ/C*73G 5fX>?? %q%'2x}}W'2155 s1v s{	t!/$w~	u!/$w5dV<==#wrO   c                 R   |j                         }|\  }}|\  }}t        |d         t        |d         ct        fdt        ||g||g      D              s)d}	|	d| z  }	|	d| z  }	|	d d d	z  }	t	        |	      t        t        j                  ||z
  ||z
              }
t        j                  |||
t
        j                  
      }t        j                  |||
t
        j                  
      }|dk(  rT| t        j                  |      j                  t              t        j                  |      j                  t              f   }ntdddd}||j                         vr&d| }	|	d|j                          z  }	t	        |	      ||j                            }t        | t        j                  ||f      |      }d}d}d|j                         v rt        |d         }t        |d         }|j                  dd      st!        |      j#                  ||g||g      d   \  }}||z   dz  }|t
        j$                  dz  |z  z  }|t
        j$                  dz  |z  t        j&                  |t
        j$                  z  dz        z  z  }n	 t)        |      }t+        |      }t        j                  ||z
  |z  ||z
  |z        }t        j.                  |       }||dk7  z  }i }||   |d <   ||   |d!<   ||   |d"<   ||   |d#<   ||d$<   |S # t,        $ r	 d}d}d}Y zw xY w)%a
  Extract 2D matrix (z) value along the line [x0,y0;x1,y1]
    Link: http://stackoverflow.com/questions/7878398/how-to-extract-an-arbitrary-line-of-values-from-a-numpy-array

    Parameters: z        : (np.ndarray) 2D data matrix
                atr      : (dict) attribute
                start_yx : (list) y,x coordinate of start point
                end_yx   : (list) y,x coordinate of end   point
                interpolation : str, sampling/interpolation method, including:
                    'nearest' - nearest neighbour
                    'linear'  - linear  spline interpolation (order of 1)
                    'cubic'   - cubic   spline interpolation (order of 3)
                    'quintic' - quintic spline interpolation (order of 5)

    Returns:    transect : (dict) containing 1D matrix:
                    'X'        - 1D np.array for X/column coordinates in float32
                    'Y'        - 1D np.array for Y/row    coordinates in float32
                    'value'    - 1D np.array for z value in float32
                    'distance' - 1D np.array for distance in float32

    Example: from mintpy.utils import readfile, utils as ut
             dem, atr = readfile.read('srtm1.dem.wgs84')
             txn = transect_yx(dem, atr, [10,15], [100,115])
    LENGTHWIDTHc              3   h   K   | ])  \  }}d |cxk  xr k  nc xr d |cxk  xr k  nc  + yw)r   N )r   ijlengthwidths      r   r   ztransect_yx.<locals>.<genexpr>%  s-     QCAaq1}u}/Q/Qs   /2z-input start/end point is out of data coveragez
start_yx: z
end_yx:z
data size: (z, )dtypenearestr&   r'   r*   )linearcubicquinticz#un-supported interpolation method: z
available methods: )orderg    TXArl   r   Y_STEPX_STEPUTM_ZONENr   g       @     f@   pixelg        YXvaluedistancedistance_unit)lowerrt   r2   zipr3   rq   hypotlinspacefloat32rintastyper:   r   vstackfloatgetr   yx2lalopicosrange_ground_resolutionazimuth_ground_resolutionKeyErrorisnan)zrG   start_yxend_yxinterpolationy0x0y1x1rF   num_ptsysxsz_lineinterpolate_name2orderinterp_orderearth_radius	dist_unity_stepx_steplat0lat1lat_c	dist_linemasktransectr   r   s                             @@r   transect_yxr     sP   0 "'')MHRHR H&CL(9MFEQ32wB;PQQ=hZ((6(##xr%22o "((2b5"R%()G	RWBJJ	7B	RWBJJ	7B
 	!2772;%%c*BGGBK,>,>s,CCD
 "

  6 ; ; ==7GC*+A+F+F+H*IJJCS/!-m.A.A.CD BIIr2h$7|L LICHHJs8}%s8}%wwz4(%c?22B8b"XFqILT4D[B&EbeeEkL00FbeeEkL0266%"%%-:K3LLLF	 ,S1F.s3F
 "r'V+r'V+-I HHVDFcMD HtHHSMtHHSMtHW$T?HZ )H_O'  	 FFI	 s   L L&%L&c                     t        |      }|j                  |d   |d   g|d   |d   g      \  \  }}\  }}	t        | |||g||	g|      }
|
S )a  Extract 2D matrix (z) value along the line [start_lalo, end_lalo]

    Parameters: z        : (np.ndarray) 2D data matrix
                atr      : (dict) attribute
                start_yx : (list) y,x coordinate of start point
                end_yx   : (list) y,x coordinate of end   point
                interpolation : str, sampling/interpolation method, including:
                    'nearest' - nearest neighbour
                    'linear'  - linear  spline interpolation (order of 1)
                    'cubic'   - cubic   spline interpolation (order of 3)
                    'quintic' - quintic spline interpolation (order of 5)

    Returns:    transect : (dict) containing 1D matrix:
                    'X'        - 1D np.array for X/column coordinates in float32
                    'Y'        - 1D np.array for Y/row    coordinates in float32
                    'value'    - 1D np.array for z value in float32
                    'distance' - 1D np.array for distance in float32

    Example: from mintpy.utils import readfile, utils as ut
             vel, atr = readfile.read('geo_velocity_msk.h5')
             txn = transect_yx(vel, atr, [30.0, 110.0], [30.2, 111.3])
    r   r&   )r   lalo2yxr   )r   rG   
start_laloend_lalor   r`   r   r   r   r   r   s              r   transect_lalor   k  sg    . sOE
1x{'C(21x{'CEHRhr21cB8b"X}EHOrO   c                 N   i }d}g |d<   t        |      D ]  \  }}|d   |d   }}d|j                         v rt        | |||      }	nt        | |||      }	|	dxx   |z  cc<   |dk(  rA|	j	                         D ]-  \  }
}t        j                  |t
        j                        ||
<   / n5|	j	                         D ]"  \  }
}t        j                  ||
   |f      ||
<   $ |d   j                  |       |d   d   } t        j                  |d   t
        j                        |d<   |S )a  Extract 2D matrix (z) value along multiple lines
    Parameters: z     : 2D np.ndarray in size of (l,w)
                atr   : dict, metadata of matrix z
                lines : list of lines with each line is defined as:
                    [[lat0, lon0], [lat1, lon1]] for geo coordinates
                    [[y0, x0], [y1, x1]] for radar coordinates
    Returns: transect : (dict) containing 1D matrix:
                    'X' - 1D np.array for X/column coordinates in float32
                    'Y' - 1D np.array for Y/row.   coordinates in float32
                    'value' - 1D np.array for z value in float32
                    'distance' - 1D np.array for distance in float32
    r   start_distancer&   r   r   r   rh   )
	enumerater:   r   r   itemsrq   rr   r   concatenateappend)r   rG   linesr   r   r   liner   r   segkeyr   s               r   transect_linesr     s>    HN!#HU# 24#AwQH

"3
H=Caj(;CJ>) 6!iik B
U "bjj AB "iik G
U "u/E FG 	!")).9!*-b1)2* "$(3C*DBJJ!WHOrO   c                    t        d|         t        j                  |       }t        d|         t        j                  | d      d   }dt        j                  |       v r)t        d|         t        j                  | d      d   }n_t        d       t        d	       t        j                  |j                  t
        j                  
      t        |d         z  }t        |      }d|j                         vrt        d       t        d       t        | |       }|j                          |j                          |j                  d   }|j!                  ||d   |d   |d   |d   f         }|j!                  ||d   |d   |d   |d   f         }t#        j$                  ||      }|d   j'                         j)                  d      st+        |d         t+        |d         }	}t        |d         }
t        |d         }t        |d         }t        |d         }|
||z  z   }|||	z  z   }t-        |||
      \  }}t-        |||      \  }}||z
  |z  }||z
  |	z  }||d<   ||d<   ||d<   ||d<   d|d<   d|d<   t
        j.                  ||dk(  <   |j)                  d      r,|t
        j0                  dz  z  }|t
        j0                  dz  z  }|||fS ) a   Prepare LOS geometry data/info in geo-coordinates.

    Parameters: geom_file - str, path of geometry file
                unit      - str, rad or deg, output angle unit
    Returns:    inc_angle - 2D np.ndarray, incidence angle in radians / degrees
                            measured from the vertical
                az_angle  - 2D np.ndarray, azimuth   angle in radians / degrees
                            measured from the north with anti-clockwise direction as positive
                atr       - dict, metadata in geo-coordinate
    z3prepare LOS geometry in geo-coordinates from file: zread incidenceAngle from file: incidenceAngle)datasetNamer   azimuthAnglezread azimuthAngle   from file: z3use the HEADING attribute as the mean heading anglez&convert heading angle to azimuth angler   HEADINGr   z2--------------------------------------------------z,geocoding the incidence / heading angles ...)lut_filesrc_filer&   r'   rQ   )src_data)res_objY_UNITdegr   r   X_FIRSTr   r   degreesX_UNITradr   )r4   r   r9   rs   get_dataset_listrq   onesshaper   r   heading2azimuth_angler:   r	   r1   preparesrc_box_listrun_resampleattrupdate_attribute4radar2geor   
startswithrt   
utm2latlonnanr   )rJ   rW   rG   	inc_angleaz_angle
head_angler   rg   r   r   NWr   r   SEr   lon0r   lon1lat_steplon_steps                         r   prepare_geo_los_geometryr     s    
?	{
KL

!
!)
,C	+I;
78i5EFqII229==/	{;<MM)HKCD67WWY__BJJ?%IBWW
(4 
"f<=I	B ""1%(()CF3q6M3q6RUVWRX=<X2Y(Z	''#a&Q-QPSTUPV:V1W'X --c7C x= ++E2CM*CG,=#i.!#i.!s8}%s8}%  Q*
dQ*
d4K6)4K5( II H H!H!H !#Ii1n uRUUT\!	BEEDL h##rO   )z./TF)NNNTr&   rl   ri   T)NNTr&   rl   ri   T)r   )r   )__doc__r6   r+   numpyrq   scipy.ndimager   mintpy.objectsr   r   r   r   mintpy.objects.coordr   mintpy.objects.resampler	   mintpy.utilsr
   r   r   r   mintpy.utils.utils0mintpy.utils.utils1rN   re   rZ   r   r   r   r   r   rO   r   <module>r     st    6  	  )  , , ; ; ! !y8z UYY]$N JNFJ@HaH<'ZM$rO   