
    Zf޺                        d 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	Z
ddlmZ ddlmZmZmZmZ ddddd	Z eej+                               Z	 	 d$d
Zd%defdZdefdZdefdZdefdZdefdZ	 	 d&dZd ZdefdZ d Z!d Z"d Z#d'dZ$ G d d      Z% G d de%      Z& G d de%      Z' G d  d!e%      Z( G d" d#e%      Z)y)(z1Class / utilities for GNSS download / operations.    N)urlopenurlretrieve)
coordinate)ptimereadfile	time_funcutils1z7http://geodesy.unr.edu/NGLStationPages/DataHoldings.txtzShttp://garner.ucsd.edu/pub/measuresESESES_products/Velocities/ESESES_Velocities.txtz5https://sideshow.jpl.nasa.gov/post/tables/table2.html)UNRESESESJPL-SIDESHOWGENERICc           	      ^   |rt         nd }|6|dk(  rt        d      t        j                  j	                  t
        |         }t        j                  j                  |      st        |||       |dk(  rt        |      }n2|dk(  rt        |      }n!|dk(  rt        |      }n|dk(  rt        |      }t        j                  d   D 	cg c]  }	|	j                          c}	      |d<   |d	   |d	   d
kD  xx   dz  cc<    |dt        |d         dddj!                  |j#                                       |d   | d   k\  |d   | d   k  z  |d	   | d   k\  z  |d	   | d   k  z  }
 |d|  dt        j$                  |
       d       |r[d|j#                         v rIt'        j(                  |g      d   d   }|
|d   |k\  z  }
 |d| dt        j$                  |
       d       |r[d|j#                         v rIt'        j(                  |g      d   d   }|
|d   |k  z  }
 |d| dt        j$                  |
       d       |?d|j#                         v r-|
|d   |k\  z  }
 |d| dt        j$                  |
       d        ||d   |
          |d   |
   |d   |
   |d	   |
   fS c c}	w )a  Search available GNSS sites within the geo bounding box for a given GNSS source.

    Parameters: SNWE             - tuple of 4 float, indicating (South, North, West, East) in degrees
                source           - str, program or institution that processed the GNSS data
                start_date       - str, date in YYYYMMDD format
                end_date         - str, date in YYYYMMDD format
                site_list_file   - str, site list file name
                min_num_solution - int, minimum number of solutions available
    Returns:    site_names       - 1D np.ndarray in string, GNSS station names
                site_lats        - 1D np.ndarray in float32, latitude
                site_lons        - 1D np.ndarray in float32, longitude
    c                       y N argskwargss     L/home/exouser/operations/rsmas_insar/tools/MintPy/src/mintpy/objects/gnss.py<lambda>zsearch_gnss.<locals>.<lambda>1           r   z9Site list file must be specified for GENERIC GNSS source!)source	print_msgr
   r   r   sitelon   h  zload dz GNSS sites with fields:  latr            zkeep sites within SNWE of z: []end_datezkeep sites with end_date >= 
start_datezkeep sites with start_date <= num_solutionz"keep sites with # of solutions >= )print
ValueErrorospathbasenameGNSS_SITE_LIST_URLSisfiledload_site_listread_UNR_site_listread_ESESES_site_listread_JPL_SIDESHOW_site_listread_GENERIC_site_listnparrayupperlenjoinkeyssumr   date_list2vector)SNWEr'   r&   r   site_list_filemin_num_solutionr   vprintsitesr   idxstart_dtend_dts                r   search_gnssrE   #   s     U%AF YXYYWW--.A&.IJN 77>>.)vK ">2	8	%n5	>	!+N;	9	&~6 HHuV}EtdjjlEFE&M	%Lu#$+$
U3uV}%a((A#((5::<BXAYZ[ %LDG#eQ(?@%LDG#%(-eQ(?AC
'vSQ?@ jEJJL0)):,7:1=uZ H,,-j\RVVC[MKLLEJJL0''
3A6q9u\"f,,/zRVVC[MKL #%**,(Fu^$(88834D3ESPSUVWX 5==uU|C0%,s2CCC7 Fs   
J*returnc           	         |t         v sJ |ddt          d       t        |   }| t        j                  j	                  |      } t        j                  j                  |       s%|rt        d|dd|dd| d       t        ||        | S )z;Download single file with list of GNSS site locations.
    sz# GNSS is NOT supported! Use one of .zdownloading site list from z:  to )GNSS_SOURCESr.   r+   r,   r-   r/   r)   r   )out_filer   r   site_list_urls       r   r0   r0   g   s     \!bfQZ/RS_R``a#bb (/M 77##M2 77>>(#/qzM!;LDQYZ[P\]^M8,Or   r>   c           	         t        j                  | t        dd      }|dddf   |dddf   j                  t         j                        |dddf   j                  t         j                        |dddf   |dddf   |ddd	f   j                  t         j
                        d
}t        j                  |d   D cg c]"  }t        j                  j                  |d      $ c}      |d<   t        j                  |d   D cg c]"  }t        j                  j                  |d      $ c}      |d<   |S c c}w c c}w )z;Return names and lon/lat values for UNR GNSS stations.
    r"   )r   r"   r#   r$                  	   
   )dtypeskiprowsusecolsNr   r#   rR   rS   rU   )r   r!   r   r'   r&   r(   r'   z%Y-%m-%dr&   )
r5   loadtxtstrastypefloat32int16r6   dtdatetimestrptime)r>   fcrA   xs       r   r1   r1   }   s    
N#C[	\BAaCAaC

3AaC

3AaCAaCAbD2E ((QVWcQd#eABKK$8$8J$G#efE,((QVWaQb#cABKK$8$8J$G#cdE*L $f#cs   >'D8'D=c                     t        j                  | dt              }|dddf   |dddf   j                  t         j                        |dddf   j                  t         j                        d}|S )z;Return names and lon/lat values for JPL GNSS stations.
       )rW   rV   Nr   r"   r#   r   r   r!   r5   rY   rZ   r[   r\   r>   ra   rA   s      r   r2   r2      s_     
NRs	;BAaCAaC

+AaC

+E
 Lr   c                     t        j                  | ddt              }|ddddf   |ddddf   j                  t         j                        |ddddf   j                  t         j                        d}|S )	zDReturn names and lon/lat values for JPL-SIDESHOW GNSS stations.
    <rT   )commentsrW   rV   Nr#   r   r$   )r   r!   r   rf   rg   s      r   r3   r3      sq     
NS1C	HBCaCFCaCF""2::.CaCF""2::.E
 Lr   c                     t        j                  | t              }|dddf   |dddf   j                  t         j                        |dddf   j                  t         j                        d}|S )a   Return names and lon/lat values for GNSS stations processed by an
    otherwise-unsupported source.

    The user must format the station position data in a file named
    GenericList.txt The file should have three, nine, or eleven space-
    separated columns:

    SITE lat lon [vel_e vel_n vel_u err_e err_n err_u] [start_date end_date]

    where site is the four-digit, alphanumeric (uppercase) site code; and
    lat/lon are in decimal degrees. If included, vel should be in units of
    m/yr; and dates should be in format YYYYMMDD.
    rV   Nr   r"   r#   re   rf   rg   s      r   r4   r4      s]     
N#	.BAaCAaC

+AaC

+E Lr   c           
         |	rt         nd }t        |      }|dv s
J d|        |j                         dk(  rdnd}t        j                  j                  | d         }t        j                  j                  |d|d	      }||d
k(  r|dndz  }|d|j                          dz  }g d}dgdgt        |      dz
  z  z   } |d|d	       d}t        j                  j                  |      r%t        j                  ||dd      }|j                  }|
st        j                  j                  |      r||k\  r |d|d	       t        j                  ||dd      }|||      }||k7  ru||d      }t        j                  |t              }t        j                  |t              t        j                  z  }t!        |      D ]  \  }}||v s|||k(     d   ||<    |S g } |d       t#        j$                  ddg|d      }|r+|} |dt        j                  j'                  |              n
| } |d        |d!k(  r
t)               nd"}t+        j,                  ||	#      }t!        |      D ]  \  }}|j/                  |dz   |dz    d$| d%|d	&        t1        |      ||'      }|j3                  ||||||(      \  }} t        j4                  |      rt        j                  n
| d)   | d   z
  }!|j7                  |j8                  |j:                  |j<                  |!|g        |j?                           |d*|d	       tA        |d+      5 }tC        jD                  |      }"|"jG                  |       |"jI                  |       d"d"d"       t        j                  |D #cg c]  }#|#|   	 c}#      }|S # 1 sw Y   1xY wc c}#w ),a  Get the GNSS LOS observations given the query info.

    Parameters: meta       - dict, dictionary of metadata of the InSAR file
                obs_type   - str, GNSS observation data type, displacement or velocity.
                site_names - list of str, GNSS sites, output of search_gnss()
                start_date - str, date in YYYYMMDD format
                end_date   - str, date in YYYYMMDD format
                source     - str, program or institution that processed the GNSS data
                gnss_comp  - str, flag of projecting 2/3D GNSS into LOS
                             e.g. enu2los, hz2los, up2los
                horz_az_angle - float, azimuth angle of the horizontal motion in degree
                             measured from the north with anti-clockwise as positive
                model      - dict, time function model, e.g. {'polynomial': 1, 'periodic': [1.0, 0.5]}
                print_msg  - bool, print verbose info
                redo       - bool, ignore existing CSV file and re-calculate
    Returns:    site_obs   - 1D np.ndarray(), GNSS LOS velocity or displacement in m or m/yr
    Examples:   from mintpy.objects import gnss
                from mintpy.utils import readfile, utils as ut
                meta = readfile.read_attribute('geo/geo_velocity.h5')
                SNWE = ut.four_corners(meta)
                site_names = gnss.search_gnss(SNWE, start_date='20150101', end_date='20190619')[0]
                vel = gnss.get_los_obs(meta, 'velocity',     site_names, start_date='20150101', end_date='20190619')
                dis = gnss.get_los_obs(meta, 'displacement', site_names, start_date='20150101', end_date='20190619')
    c                       y r   r   r   s     r   r   zget_los_obs.<locals>.<lambda>   r   r   )displacementvelocityzun-supported obs_type: ro   r$   rO   	FILE_PATHgnss_rH   horzz.0f _z.csv)SiteLonLatDisplacementVelocityU10f8r"   z$default GNSS observation file name: r   ,T)rV   	delimiternamesz"read GNSS observations from file: rl   z calculating GNSS observation ...incidenceAngleazimuthAnglegeo)work_dircoordz)use incidence / azimuth angle from file: z+use incidence / azimuth angle from metadatar   N)maxValuer   /r    )suffix)
url_prefix)r'   r&   	gnss_comphorz_az_anglemodelz!write GNSS observations to file: w)%r)   r8   lowerr+   r,   dirnamer9   r7   r/   r5   
genfromtxtsizer6   floatzerosnan	enumerateutget_geometry_filer-   get_ESESES_url_prefixr   progressBarupdateget_gnss_classget_los_velocityisnanappendr   site_lonsite_latcloseopencsvwriterwriterow	writerows)$metaobs_type
site_namesr'   r&   r   r   r   r   r   redor@   num_siteobs_indfile_dircsv_file	col_names	col_typesnum_rowra   site_obs
temp_namestemp_obsi	site_name	data_list	geom_filegeom_objr   prog_bargnss_objveldis_tsdisfcwrb   s$                                       r   get_los_obsr      s   4  U%AF:H 33Y7Nxj5YY>>#~5a1G wwtK01Hww||Hi]&;<H)v*==%2EH!FLLN#4((HBI4&C	NQ$677I
1(1>? G	ww~~h]]894P''BGGNN8,H1D3HQ<@A]]894Pi() hIaL)Jxx6Hxx6?H )* 5 G9
*"*:+B"CA"FHQKGn Oc 	12 ((*:>)J2:%I	 H>rww?O?OPY?Z>[\]H@A 17(0B*,
 $$h)L%j1 	^LAyOOAaC1Q3%q
!Ia=(IOJ .~f-iJOH"33%!#+ 4 KC HHSM"&&vbzF1I/EC hmmX->->@Q@QSVX[\]%	^& 	 	28A,?@(C  	%B**R.CLL#MM)$	% 88;AQwZ;<O	% 	% <s   8N;&O;Oc           
         t        j                  t        t        t	        |       t	        |      z                    }t        j
                  |j                  t              }	t        |      D ]u  \  }
}t        j                  | |k(        d   d   }t        j                  ||k(        d   d   }||   ||   z
  dz  ||   ||   z
  dz  z   ||   ||   z
  dz  z   dz  }||	|
<   w |	|	d   z  }	t        j                  |	t              }	||	fS )a  Calculate the baseline change between two GNSS displacement time-series.

    Parameters: dates1/2   - 1D np.ndarray, datetime.datetime object
                pos_x/y/z1 - 1D np.ndarray in float32, displacement in meters of the 1st site
                pos_x/y/z2 - 1D np.ndarray in float32, displacement in meters of the 2nd site
    Returns:    dates      - 1D np.ndarray in dt.datetime object for the common dates
                bases      - 1D np.ndarray in float32, baseline in meters for the common dates
    rl   r   r#         ?)
r5   r6   sortedlistsetr   shaper   r   where)dates1pos_x1pos_y1pos_z1dates2pos_x2pos_y2pos_z2datesbasesr   dateidx1idx2baseis                  r   get_baseline_changer   =  s    HHVDVs6{!:;<=EHHU[[.EU# 4xx$'*1-xx$'*1-,-!3,-!34,-!348;< a 
U1XEHHU%(E%<r   r   c                 z    | dk(  rt         S | dk(  rt        S | dk(  rt        S | dk(  rt        S t	        d| dd      )zHReturn the appropriate GNSS child class based on processing source.
    r
   r   r   r   zGNSS source rH   z is NOT supported!)GNSS_UNRGNSS_ESESESGNSS_JPL_SIDESHOWGNSS_GENERICr*   )r   s    r   r   r   X  sQ     	8		>	!  	9	<qz1CDEEr   c                     t        d       d} | dz  } t        j                  j                         }d}d}||k  rV|t        j                  |      z
  j                  d      }| j                  |      }	 t        |       t        | d       	 |S S #  ||d	z
  k(  rt        d
|  d      |d	z  }t        | d       Y xY w)zlGet the url prefix for the ESESES source, which updates regularly.
    [Poor design of ESESES website].
    z#searching for ESESES url_prefix ...z=http://garner.ucsd.edu/pub/measuresESESES_products/Timeseriesz3/CurrentUntarred/Clean_TrendNeuTimeSeries_comb_{:s}   r   days%Y%m%dz [YES!]r"   zThe ESESES repository z CANNOT be found!z [no])	r)   r^   r   today	timedeltastrftimeformatr   FileNotFoundError)url_fmtr   max_daynum_dayday_strr   s         r   r   r   g  s     

/0MGDDG GGMMOEGG
G
2<<W55??I^^G,
	JZL() :	'A+%'*@	IZ([\\1E*+s   4B ,C c           
         dd l }|j                  | d      }t        j                  |dt        d      }|d d df   j                  t              }|d d df   j                  t              }|d d df   j                  t              }t        |||      D 	cg c]  \  }}}	t        j                  |||		        }
}}}	|d d d
f   j                  t              j                         }|d d df   j                  t              j                         }|d d df   j                  t              j                         }|
|||fS c c}	}}w )Nr   cp1252)encoding   )*z-DATA)rW   rV   rj   r"   r#   )yearmonthdayrO   rP   rQ   )codecsr   r5   rY   rZ   r[   intzipr^   r_   r   tolist)fnamer   fcpra   ysmsdsymr   r   XYZs                 r   read_pos_filer     s   
++e+
1C	C"C-	HB	AaC	B	AaC	B	AaC	B>A"b"oNNUQqR[[aqa0NEN
1Q3u$$&A
1Q3u$$&A
1Q3u$$&A!Q> Os   #Ec                 $   t        j                   t        j                  j                  | |dd            }|D cg c]3  }t        j                  j	                  |      j                  d      d   5 }}t        j                  |      }|S c c}w )NrH   z.*.posrI   r"   )globr+   r,   r9   r-   splitr   yy2yyyy)gnss_dirr   fnamesr   yearss        r   get_pos_yearsr    ss    YYrww||Ha.?@AF8>?1RWWa &&s+A.?E?MM% EL @s   8Bc           	         t        |dd       }t        |dd       }||z
  dz   }g g g g f\  }}}	}
t        |      D ]_  }t        ||z         }t        j                  j                  | |dd|dd  dd      }t        |      \  }}}}||z  }||z  }|	|z  }	|
|z  }
a t        j                  |      }t        j                  |      }t        j                  |	      }	t        j                  |
      }
t        j                  j                  |d	      }t        j                  j                  |d	      }t        j                  |j                  t        j                  
      }d|||k  <   d|||kD  <   ||   ||   |	|   |
|   fS )Nr   rO   r"   r   rI   r#   rH   z.posr   rl   F)r   rangerZ   r+   r,   r9   r   r5   r6   r^   r_   r`   onesr   bool_)r  r   r'   r&   year0year1num_yearr   r   r   r   r   yearir   datesiXiYiZidate0date1flags                        r   read_GSI_F3r    s|   
1Q E1Eu}q HR^NE1a8_ EAIX$q59Q-t'DE*51B	R	R	R HHUOE
A
A
AKK  X6EKK  84E77177"((+DDD;$4!D'11r   c                       e Zd ZdZddefdZddZddZddZddZ	d	ed
efdZ
ddZ	 	 ddedefdZddZ	 	 ddZ	 	 ddZy)GNSSaN  GNSS parent class for time-series of daily solution.

    The GNSS class is solely meant to be a parent class. Child classes, defined
    below, support functions for downloading and parsing GNSS position based on
    the processing source (e.g., UNR, etc.). Use the `get_gnss_class`
    method to determine appropriate child class.

    The parent class/object will assign the following attributes:
        source        - str, GNSS solution source
        version       - str, GNSS solution version
        url_prefix    - str, GNSS data file url prefix

    The chile class/object will assign the following attributes:
        file          - str, path of the local data file
        url           - str, path of the remote data file
        site          - str, four-digit site code
        site_lat/lon  - float, site latitude/longitude in degree
        dates         - 1D np.ndarray, dt.datetime object
        date_list     - list(str), dates in YYYYMMDD format
        dis_e/n/u     - 1D np.ndarray, displacement in meters
        std_e,n,u     - 1D np.ndarray, displacement STD in meters
    Nr   c                    || _         || _        || _        || _        d | _        d | _        |r|n/t        j                  j                  d|j                                | _
        t        j                  j                  | j                        s5t        d| j                         t        j                  | j                         d | _        d | _        d | _        d | _        d | _        d | _        d | _        d | _        y )NzGNSS-zcreate directory:)r   r   versionr   urlfiler+   r,   abspathr7   data_direxistsr)   mkdirr   	date_listdis_edis_ndis_ustd_estd_nstd_u)selfr   r   r  r   r   s         r   __init__zGNSS.__init__  s    	$ 	$,"''//E&,,.IYBZ2[ ww~~dmm,%t}}5HHT]]# 






r   c                     t         j                  j                  | j                        s| j	                  |       | j                          | j                  |       y)z^Read the lat/lon and displacement data of the station.
        Download if necessary.
        r   N)r+   r,   r/   r  
dload_siteget_site_lat_lonread_displacement)r*  r  r   s      r   r   z	GNSS.open  sE    
 ww~~dii(OOiO0 	3r   c           	         |rt         nd }| j                  r|s)t        j                  j	                  | j
                        s{ |d| j                  dd| j                   d| j
                  d       |}|dkD  rC	 t        | j                  | j
                          |d| j                          	 | j
                  S | j
                  S #   |d| j                   d	||z
          |d
z  }Y xxY w):  Download GNSS site data file.

        Parameters: overwrite   - bool, overwrite existing data file
                    total_tries - int, number of tries to download if failed
                    print_msg   - bool, verbose print out msg
        Returns:    self.file   - str, path to the local data file
        c                       y r   r   r   s     r   r   z!GNSS.dload_site.<locals>.<lambda>  r   r   zdownloading site rH   z from rJ   r   zsuccessfully downloaded: zerror downloading z on trial no. r"   )	r)   r  r+   r,   r/   r  r   r   r   )r*  	overwritetotal_triesr   r@   remain_triess         r   r.  zGNSS.dload_site  s     $)E88			)B&tyym6$++d499UV-XY 'L"$))46txxjAB yytyy/zT`H`Gabc A%Ls   <5C
 
"C.c                     t        d      )z~Get the GNSS site latitude & longitude into:
        Returns: site_lat/lon - float, site latitude/longitude in degree
        zAget_site_lat_lon() is NOT implemented. Override with child class.NotImplementedError)r*  r   s     r   r/  zGNSS.get_site_lat_lon  s     ""effr   c                     t        d      )a]  Get the GNSS time/displacement(Std) into:
        Returns: dates      - 1D np.ndarray in datetime.datetime object
                 date_list  - list(str), date in YYYYMMDD format
                 dis_e/n/u  - 1D np.ndarray in float32, displacement in meters
                 std_e/n/u  - 1D np.ndarray in float32, displacement STD in meters
        zBread_displacement() is NOT implemented. Override with child class.r8  )r*  r'   r&   r   displays        r   r0  zGNSS.read_displacement%  s     ""fggr   r'   r&   c                    t        j                  t        | j                        t              }|r.t        j                  |g      d   d   }d|| j                  |k  <   |r.t        j                  |g      d   d   }d|| j                  |kD  <   | j                  |   | _        | j                  |   | _        | j                  |   | _        | j                  |   | _	        | j                  |   | _
        | j                  |   | _        | j                  |   | _        | j                  D cg c]  }|j                  d       c}| _        yc c}w )zsCrop the time-series given the start/end_date in format YYYYMMDD,
        and create date_list from dates.
        rl   r   r   N)r5   r  r8   r   boolr   r<   r$  r%  r&  r'  r(  r)  r   r#  )r*  r'   r&   r  t0t1rb   s          r   _crop_to_date_rangezGNSS._crop_to_date_range/  s    wws4::d3''5a8;B$%Db!''
3A6q9B$%Db!ZZ%
ZZ%
ZZ%
ZZ%
ZZ%
ZZ%
ZZ%
 9=

C1!**X.CCs   )E
c                    ddl m} | j                  | j                          |j	                  ddd      \  }}t        |dz  |      } |d   j                  | j                  | j                  fi |  |d   j                  | j                  | j                  fi |  |d   j                  | j                  | j                  fi | |rt        d	|
      } |d   j                  | j                  | j                  fd| j                  i|  |d   j                  | j                  | j                  fd| j                  i|  |d   j                  | j                  | j                  fd| j                  i| t        g d      D ]  \  }}	||   j                  |	 d        |j!                  | j"                  dd| j$                  dd       |j'                          |j)                          ||fS )z+Plot the displacement time-series.
        r   Nr$   r"   T)nrowsncolssharexr#   )rH   cnone)	linestylecoloryerr)EastNorthUpz [m]rH   z ())matplotlib.pyplotpyplotr$  r   subplotsdictscatterr   r%  r&  errorbarr'  r(  r)  r   
set_ylabelsuptitler   r   tight_layoutshow)
r*  marker_sizemarker_colorplot_error_barpltfigaxr   r   labels
             r   plotz	GNSS.plotH  s    	(::IIK ,,Qa,=R Q,71djj$**771djj$**771djj$**77 F,?FBqENN4::tzzM

MfMBqENN4::tzzM

MfMBqENN4::tzzM

MfM ""9: 	-HAuqEwd^,	-		!}Bt{{1oQ78
Bwr   	inc_angleaz_anglec                    | j                   | j                          t        j                  |||j	                         |      }| j                   |d   z  | j
                  |d   z  z   | j                  |d   z  z   | _        | j                  |d   z  dz  | j                  |d   z  dz  z   | j                  |d   z  dz  z   dz  | _        | j                  | j                  fS )a  Convert displacement in ENU to LOS direction.

        Parameters: inc_angle     - float, LOS incidence angle in degree
                    az_angle      - float, LOS aziuth    angle in degree from the north,
                                    defined as positive in clock-wise direction
                    gnss_comp     - str, GNSS components used to convert to LOS direction
                    horz_az_angle - float, fault azimuth angle used to convert horizontal to fault-parallel
                                    measured from the north with anti-clockwise as positive
        Returns:    dis_los       - 1D np.ndarray for displacement in LOS direction
                    std_los       - 1D np.ndarray for displacement standard deviation in LOS direction
        )los_inc_anglelos_az_anglecompr   r   r"   r#   r   )r$  r   r   %get_unit_vector4component_of_interestr   r%  r&  dis_losr'  r(  r)  std_los)r*  r`  ra  r   r   unit_vecs         r   displacement_enu2loszGNSS.displacement_enu2losk  s     ::IIK ;;#!"'	
 **x{2**x{23**x{23
 !JJ!4q8 JJ!4q89 JJ!4q89>AB ||T\\))r   c                    | j                         \  }}t        |t              rt        j                  |      }t        ||      }|j                  |||      dd \  }}t        d|      }t        t        |d         dz
  |      }t        d|      }t        t        |d         dz
  |      }t        |||dz   |dz   f|      }	t        j                  |fd	d
i|	d   d   }
t        j                  |fd	di|	d   d   }|
|fS t        |t              r=t        j                  |d|      }
t        j                  t        |d               }|
|fS t!        d|       )zNGet the Line-of-Sight geometry info in incidence and azimuth angle in degrees.)lookup_filer-  r   r#   LENGTHr"   WIDTH)boxr   datasetNamer   )r   r   r   )	dimensionr   HEADINGz(input geom_obj is neither str nor dict: )r/  
isinstancerZ   r   read_attributer   	geo2radarmaxminr   rQ  readr   incidence_angleheading2azimuth_angler   r*   )r*  r   r   r!   r   atrr   r   rb   r   r`  ra  s               r   get_los_geometryzGNSS.get_los_geometry  ss   ((*S h$))(3Cs9E??3y?A!AFDAqAq	ACH$6q$8! <AAq	ACG$5a$7 ;Aq1Q3qsmyAF hW<LWPVWXYZ[^_I hWNWPVWXYZ[^_H ("" $'**8qIVI00x	7J1KLH
 ("" GzRSSr   c           
      f   | j                         }| j                  |||      d   }	| j                  |      \  }
}| j                  |
|||      \  }}|r t	        | j
                        || j                        }|j                         }|j                  |||       |j                  |      \  }
}|j                  |
|||       t        j                  t        t        t        | j                        t        |j                        z                    }	t        j                  |	j                  t        j                        }t        j                  |	j                  t        j                        }t!        |	      D ]  \  }}t        j"                  | j                  |k(        d   d   }t        j"                  |j                  |k(        d   d   }| j$                  |   |j$                  |   z
  ||<   | j&                  |   dz  |j&                  |   dz  z   dz  ||<    nd}|	||||fS )	a  Get GNSS displacement in LOS direction.

        Parameters: geom_obj      - dict / str, metadata of InSAR file, or geometry file path
                    start_date    - str, dates in YYYYMMDD format
                    end_date      - str, dates in YYYYMMDD format
                    ref_site      - str, reference GNSS site
                    gnss_comp     - str, GNSS components used to convert to LOS direction
                    horz_az_angle - float, fault azimuth angle used to convert horizontal
                                    to fault-parallel
        Returns:    dates         - 1D np.ndarray of datetime.datetime object
                    dis/std       - 1D np.ndarray of displacement / uncertainty in meters
                    site_lalo     - tuple of 2 float, lat/lon of GNSS site
                    ref_site_lalo - tuple of 2 float, lat/lon of reference GNSS site
        r-  r   )r   r   )r   r   rl   r#   r   N)r/  r0  r|  rj  r   r   r   r5   r6   r   r   r   r   r   r   r\   r   r   rg  rh  )r*  r   r'   r&   ref_siter   r   r   	site_lalor   r`  ra  r   stdref_objref_site_lalor   date_ir   r   s                       r   get_los_displacementzGNSS.get_los_displacement  s   " ))+	&&z8y&QRST"33H=	8,,x' - 
S 1nT[[1x$--XG#446M%%j(i%P")":":8"DIx((8#+ )  HHVDTZZ3w}};M)M$NOPE((5;;bjj9C((5;;bjj9C&u- Q	6xx

f 45a8;xx 78;A>d+good.CCA,,t,a/'//$2G2JJSPA	Q !Mc3	=88r   c	                    | j                  ||||||      dd \  }	}
d}t        |	      dk  rd}nR|rP|rNt        j                  |g      d   d   }t        j                  |g      d   d   }|	d   |	d   z
  ||z
  dz  k  rd}|r|	D cg c]"  }t        j
                  j                  |d	      $ }}t        j                  ||
      }t        j                  t        j                  j                  |      |
      d   | _        n0t        j                  | _        |rt        d| j                   d       | j                  |
fS c c}w )a  Convert the three-component displacement data into LOS velocity.

        Parameters: geom_obj      - dict / str, metadata of InSAR file, or
                                    geometry file path
                    start_date    - str, YYYYMMDD format
                    end_date      - str, YYYYMMDD format
                    ref_site      - str, reference GNSS site
                    gnss_comp     - str, GNSS components used to convert to LOS direction
                    horz_az_angle - float, fault azimuth angle used to convert horizontal
                                    to fault-parallel
                    model         - dict, time function model, e.g.
                                      {'polynomial': 1, 'periodic': [1.0, 0.5]}
        Returns:    dates         - 1D np.ndarray, datetime.datetime object
                    dis           - 1D np.ndarray, displacement in meters
        )r'   r&   r~  r   r   Nr#   TFr   r   rO   r   )r   r"   z&
Velocity calculation failed for site rH   )r  r8   r   r<   r^   r_   r   r   get_design_matrix4time_funcr5   dotlinalgpinvrp   r   r)   r   )r*  r   r'   r&   r~  r   r   r   r   r   r   dis2velr>  r?  r   r#  As                    r   r   zGNSS.get_los_velocity  sG   $ ..!' / 
 1
s u:?GH''5a8;B''
3A6q9BRy58#R1}4DIJq--a:JIJ55iuMAFF299>>!#4c:1=DMFFDM?		!}MN}}c!! Ks   	'E)NIGS14r
   N)NTFrP   TFNNTF)r#   kT)enu2los     V)NNNr  r  F)NNNr  r  NT)__name__
__module____qualname____doc__rZ   r+  r   r.  r/  r0  r@  r_  r   rj  r|  r  r   r   r   r   r  r    s    .S 8
46ghDc DS D2 F OX+/"*U "*U "*J#8 W[PU29j SWX\1"r   r  c                   P     e Zd ZdZddef fdZd	 fd	Zd
deeffdZddZ	 xZ
S )r   zGNSS child class for daily solutions processed by Nevada Geodetic Lab
    at University of Nevada, Reno (UNR).

    Website: http://geodesy.unr.edu/NGLStationPages/GlobalStationList
    r   c                 L   t         |   |||d|       |dk(  rBt        j                  j	                  | j
                  | j                  dd|dd      | _        nR|dk(  r>t        j                  j	                  | j
                  | j                  dd      | _        nt        d| d	      | j                  sd
| j                   | _	        t        j                  j	                  | j                  t        j                  j                  | j                              | _        y )Nr
   r   r   r  r   r   IGS08rH   rI   z.tenv3r  zUn-supported GNSS versoin: !z,http://geodesy.unr.edu/gps_timeseries/tenv3/)superr+  r+   r,   r9   r   r   r  r*   r   r  r-   r  r*  r   r   r  r   	__class__s        r   r+  zGNSS_UNR.__init__  s    ! 	 	
 gT]]tyym1WQKv4VWDIT]]tyym64JKDI:7)1EFF
  LT\\N[DO77<<1A1A$))1LMr   c                 $   t         |   ||       t        j                  j	                  | j
                  d| j                   d      }t        j                  j                  t        j                  j                  |            s4t        j                  t        j                  j                  |      d       ddd| j                     }t        j                  j	                  || j                   d      }t        ||       | j                  S )	r2  r4  r   zpic/z.pngT)exist_okz/http://geodesy.unr.edu/tsplots/IGS08/TimeSeriesz5http://geodesy.unr.edu/tsplots/IGS14/IGS14/TimeSeries)r  r  )r  r.  r+   r,   r9   r   r   r!  r   makedirsr  r   r  )r*  r4  r5  r   	plot_filer   plot_file_urlr  s          r   r.  zGNSS_UNR.dload_site4  s     	Y)D
 GGLL$tyyk0FG	 ww~~bggooi89KK	2TB HM
 ,,
 ZDII;d1CD 	M9-yyr   rF   c                 H   t         j                  j                  | j                        s| j	                  |       t        j                  | j                  t        dd      }|dddf   j                  t              \  | _
        | _        | j                  | j                  fS )zGet station lat/lon from the displacement file.

        Modifies:   self.lat/lon - float
        Returns:    self.lat/lon - float
        r-  r"   rU   )rV   rW   max_rowsr   r      )r+   r,   r/   r  r.  r5   rY   bytesr[   r   r   r   )r*  r   datas      r   r/  zGNSS_UNR.get_site_lat_lonU  sv     ww~~dii(OOiO0zz$))51rJ'+Ar"uH~'<'<U'C$t}}}dmm++r   c           	         |rt         nd }t        j                  j                  | j                        s| j                  |        |d       	 t        j                  | j                  t        d      j                  t              }t        j                  |d
d
df   D cg c]"  }t        j                  j                  |d      $ c}      | _        |d
d
df   j                  t        j                         j"                  \  | _        | _        | _        | _        | _        | _        | j1                  ||       |r| j3                          | j                  | j$                  | j&                  | j(                  | j*                  | j,                  | j.                  fS #  d}|dz  }t        |       | j                  d|	       t        j                  | j                  t        d      j                  t              }Y xxY wc c}w )a  Read GNSS displacement time-series (defined by start/end_date)
        Parameters: start_date - str, start date in YYYYMMDD format
                    end_date   - str, end_date   in YYYYMMDD format
        Returns:    dates      - 1D np.ndarray in datetime.datetime object
                    dis_e/n/u  - 1D np.ndarray in float32, displacement in meters
                    std_e/n/u  - 1D np.ndarray in float32, displacement STD in meters
        c                       y r   r   r   s     r   r   z,GNSS_UNR.read_displacement.<locals>.<lambda>l  r   r   r-  >reading time and displacement in east/north/vertical directionr"   )rV   rW   zXError occurred while reading, probably due to interuptions during previous downloading. z Remove the file and re-download.Tr  Nz%y%b%d)rS   rU               )r)   r+   r,   r/   r  r.  r5   rY   r  r[   rZ   r6   r^   r_   r`   r   r\   Tr$  r%  r&  r'  r(  r)  r@  r_  )	r*  r'   r&   r   r;  r@   ra   msgrb   s	            r   r0  zGNSS_UNR.read_displacementd  s    $)E ww~~dii(OOiO0 	OP	LDIIUQ?FFsKB XX"QPQT(SQr{{33Ax@ST
 !//077

CEE						 	  X6 IIK



DJJ



DJJ

4 	4-	LlC55C#JOOdiO@DIIUQ?FFsKBSs   9F #'HA HNr  Nr  r  r  r  r  r  r  rZ   r+  r.  r   r/  r0  __classcell__r  s   @r   r   r     s1    
NS N2B,E5> ,*4r   r   c                   P     e Zd ZdZddef fdZd	 fd	Zd
deeffdZddZ	 xZ
S )r   a%  GNSS child class for daily solutions processed for the Enhanced Solid
    Earth Science ESDR System (ESESES) project by JPL and SOPAC.

    Website: https://cddis.nasa.gov/Data_and_Derived_Products/GNSS/ESESES_products.html
             http://garner.ucsd.edu/pub/measuresESESES_products/
    r   c                     t         |   |||d|       t        j                  j	                  | j
                  | j                  j                         dd      | _        y )Nr   r  rH   zCleanTrend.neu.Z)	r  r+  r+   r,   r9   r   r   r   r  r  s        r   r+  zGNSS_ESESES.__init__  sV    ! 	 	
 GGLL499??3DQ2GGW0XY	r   c                 (   ddl m} | j                  st               | _        t        j
                  j                  | j                  t        j
                  j                  | j                              | _	        t        | -  ||        || j                  d      5 }|j                  | j                         ddd       | j                  j                  d      | _        |rt        d| j                  d       | j                  S # 1 sw Y   PxY w)	z!Download GNSS data file.
        r   )ZipFiler  rN.Zz... extracted to rH   )zipfiler  r   r   r+   r,   r9   r-   r  r  r  r.  
extractallr   stripr)   )r*  r4  r5  r   r  fzr  s         r   r.  zGNSS_ESESES.dload_site  s     	$ 35DO77<<1A1A$))1LM 	Y)D TYY$ 	)MM$--(	)IIOOD)	%dii]34yy	) 	)s   DDrF   c                 .   t         j                  j                  | j                        s| j	                  |       | j                  j                  d      r| j                  dd | _        t        | j                        5 }|j                         }|D cg c]  }|j                  d      s| c}d   j                  d      }t        |j                         d         | _        |D cg c]  }|j                  d	      s| c}d   j                  d      }t        |j                         d         | _        | xj                  | j                  d
k  rdndz  c_        ddd       | j                  | j                  fS c c}w c c}w # 1 sw Y   +xY w)Get station lat/lon based on processing source.
        Retrieve data from the displacement file.

        Modifies:   self.lat/lon - float
        Returns:    self.lat/lon - float
        r-  r  Nz
# Latituder   
r   z# East Longituder   r   )r+   r,   r/   r  r.  endswithr   	readlines
startswithr  r   r  r   r   )r*  r   flinesrb   lat_linelon_lines          r   r/  zGNSS_ESESES.get_site_lat_lon  sE    ww~~dii(OOiO0 99d#		#2DI$))_ 	@KKME $)GaALL,FGJPPQUVH!(.."22"67DM $)MaALL9K,LMaPVVW[\H!(.."22"67DMMM$--3"6QC?M	@ }}dmm++ H N	@ 	@s7    FF,F0:F*FFAF
FFc                    |rt         nd }t        j                  j                  | j                        s| j                  |       | j                  j                  d      r| j                  dd | _         |d       t        j                  | j                  t        t        dd            	      }|j                  d   }t        |      D cg c]L  }t        j                  t        ||d
f         d
d
      t        j                  t        ||df               z   N }	}t        j                   |	      | _        |ddddf   j%                  t        j&                        j(                  dz  \  | _        | _        | _        | _        | _        | _        | j7                  ||       |r| j9                          | j"                  | j,                  | j*                  | j.                  | j2                  | j0                  | j4                  fS c c}w )  Read GNSS displacement time-series (defined by start/end_date).

        Parameters: start/end_date - str, date in YYYYMMDD format
        Returns:    dates          - 1D np.ndarray of datetime.datetime object
                    dis_e/n/u      - 1D np.ndarray of displacement in meters in float32
                    std_e/n/u      - 1D np.ndarray of displacement STD in meters in float32
        c                       y r   r   r   s     r   r   z/GNSS_ESESES.read_displacement.<locals>.<lambda>  r   r   r-  r  Nr  r  r   rT   )rX   r"   r#   r   r$   i  )r)   r+   r,   r/   r  r.  r  r5   rY   tupler
  r   r^   r_   r   r   r6   r   r[   r\   r  r%  r$  r&  r(  r'  r)  r@  r_  )
r*  r'   r&   r   r;  r@   ra   r(   r   r   s
             r   r0  zGNSS_ESESES.read_displacement  s    $)E ww~~dii(OOiO0 99d#		#2DI 	OPZZ		5q+<=xx{  -/ SAqD]Aq1BLLc"QPQT(m4TT / /XXe_
 !QqS&z((466=						 	  X6 IIK



DJJ



DJJ

4 	4'/s   AHr  r  r  r  r  r  s   @r   r   r     s0    
ZS 
Z".,E5> ,>.4r   r   c                   D     e Zd ZdZddef fdZddeeffdZd	dZ xZ	S )
r   zoGNSS class for daily solutions processed by JPL-SIDESHOW.

    Website: https://sideshow.jpl.nasa.gov/pub/
    r   c                 z   t         |   |||d|       t        j                  j	                  | j
                  | j                  dd      | _        | j                  sd| _        t        j                  j	                  | j                  t        j                  j                  | j                              | _
        y )Nr   r  rH   z.serieszJhttps://sideshow.jpl.nasa.gov/pub/JPL_GPS_Timeseries/repro2018a/post/point)r  r+  r+   r,   r9   r   r   r  r   r-   r  r  s        r   r+  zGNSS_JPL_SIDESHOW.__init__  s    !! 	 	
 GGLL499Q-w0GH	 jDO77<<1A1A$))1LMr   rF   c                    t         j                  j                  t        d         }t	        |      5 }|D ]4  }|dd | j
                  k(  s|dd dk(  s|j                         dd \  }}6 	 ddd       t              | _        t              | _	        |dk(  r't        d	| j                  d
d| j                  d
       | j                  | j                  fS # 1 sw Y   mxY w)r  r   NrO   rP   rS   POSr#   T	r  z, )r+   r,   r-   r.   r   r   r  r   r   r   r)   )r*  r   r>   	site_listliner   r   s          r   r/  z"GNSS_JPL_SIDESHOW.get_site_lat_lon'  s     ))*=n*MN .! 	;Y! ;!H		)Qq	U0B)-a):&Hh;	; hhBt}}Q'r$--):;<}}dmm++	; 	;s   CCCC"c                 N   t         j                  j                  | j                        s| j	                  |       |dk(  rt        d       t        j                  | j                        }|j                  d   }t        j                  t        |      D cg c].  }t        j                  ||ddf   j                  t               0 c}      | _        |ddddf   j                  t        j                         j"                  \  | _        | _        | _        | _        | _        | _        | j1                  ||       |dk(  r| j3                          | j                  | j$                  | j&                  | j(                  | j*                  | j,                  | j.                  fS c c}w )	a  Read GNSS displacement time-series (defined by start/end_date)
        Parameters: start/end_date - str, date in YYYYMMDD format
        Returns:    dates          - 1D np.ndarray of datetime.datetime object
                    dis_e/n/u      - 1D np.ndarray of displacement in meters in float32
                    std_e/n/u      - 1D np.ndarray of displacement STD in meters in float32
        r-  Tr  r   iNr"   rR   )r+   r,   r/   r  r.  r)   r5   rY   r   r6   r
  r^   r_   r[   r   r   r\   r  r$  r%  r&  r'  r(  r)  r@  r_  )r*  r'   r&   r   r;  r  n_datar   s           r   r0  z#GNSS_JPL_SIDESHOW.read_displacementA  sC    ww~~dii(OOiO0 RS zz$))$A XXeTZm\r{{D23K,>,>s,CD\]
 AqsF|**2::688						 	  X6 d?IIK



DJJ



DJJ

4 	4# ]s   3F"r  r  r  )
r  r  r  r  rZ   r+  r   r/  r0  r  r  s   @r   r   r     s+    NS N$,E5> ,4'4r   r   c                   D     e Zd ZdZddef fdZddeeffdZd	dZ xZS )
r   a  GNSS class for daily solutions of an otherwise-unsupported source.

    Required local files:
    1. GenericList.txt: site list file, with the following 3, 9 or 11
        space-separated columns:

    SITE lat lon [vel_e vel_n vel_u err_e err_n err_u] [start_date end_date]

    where site is the four-digit, alphanumeric (uppercase) site code; and
    lat/lon are in decimal degrees. If included, vel should be in units of
    m/yr; and dates should be in format YYYYMMDD.

    2. <sitename>.txt: data file for each site, with 7 space-separated columns:

    date dis_e dis_n dis_u std_e std_n std_u

    where date is in the format <YYYYMMDD> or <YYYYMMDD>T<HH:MM:SS> or <YYYYMMDD>:<HHMMSS>,
    displacement values are in meters.
    r   c                     t         |   |||d|       t        j                  j	                  | j
                  | j                  dd      | _        d| _        d| _	        y )Nr   r  rH   z.txtrt   )
r  r+  r+   r,   r9   r   r   r  r   r  r  s        r   r+  zGNSS_GENERIC.__init__  s]    ! 	 	
 GGLL499Q-t0DE	 r   rF   c                     t        d      }|d   j                         j                  | j                        }|d   |   |d   |   fS )z8Get station lat/lon based on processing source.
        zGenericList.txtr   r!   r   )r4   r   indexr   )r*  r   rA   inds       r   r/  zGNSS_GENERIC.get_site_lat_lon  sM     ''89Fm""$**4995U|C %,s"333r   c                    t         j                  j                  | j                        s| j	                  |       |dk(  rt        d       t        j                  | j                        }t        j                  t        j                  |dddf               | _        |ddt        t        dd            f   j                  t        j                        j                   \  | _        | _        | _        | _        | _        | _        | j/                  ||       |r| j1                          | j                  | j"                  | j$                  | j&                  | j(                  | j*                  | j,                  fS )r  r-  Tr  Nr   r"   rR   )r+   r,   r/   r  r.  r)   r5   rY   r6   r   r<   r   r  r
  r[   r\   r  r$  r%  r&  r'  r(  r)  r@  r_  )r*  r'   r&   r   r;  ra   s         r   r0  zGNSS_GENERIC.read_displacement  s    ww~~dii(OOiO0 RSZZ		" XXe44R1X>?
 !U51:../66rzzBDD						 	  X6 IIK



DJJ



DJJ

4 	4r   r  r  r  )	r  r  r  r  rZ   r+  r/  r0  r  r  s   @r   r   r   k  s)    &S "4C: 4%4r   r   )NNr
   N2   T)Nr
   T)r
   r  r  NTF)NN)*r  r   r_   r^   r  r+   urllib.requestr   r   numpyr5   mintpy.objects.coordr   mintpy.utilsr   r   r   r	   r   r.   r   r:   rK   rE   rZ   r0   r1   r2   r3   r4   r   r   r   r   r   r  r  r  r   r   r   r   r   r   r   <module>r     s!   7    	 /  + A A OjL	  ',,./ TX/3ADHC ,c (	 		s 	# 6 [dEJvr6F# F L"2<O" O"d
y4t y4x|4$ |4~X4 X4vR44 R4r   