def get_unr(self, site, verbose=False): """ Get a time series from http://geodesy.unr.edu/gps_timeseries/txyz/IGS14/ in PYACS :param site: 4-letters code :param verbose: verbose mode """ # import import urllib.request from urllib.error import HTTPError, URLError import socket from pyacs.gts.Gts import Gts import numpy as np import os import pyacs.lib.astrotime as at import datetime from datetime import timedelta delta_12h = timedelta(hours=12) # url url = ("http://geodesy.unr.edu/gps_timeseries/txyz/IGS14/%s.txyz2" % site.upper()) # get data try: urllib.request.urlretrieve(url=url, filename="test.dat") except HTTPError as error: print('Data not retrieved because %s\nURL: %s', error, url) except URLError as error: if isinstance(error.reason, socket.timeout): print('socket timed out - URL %s', url) else: print('some other error happened') # creates Gts # get code code = np.genfromtxt('test.dat', usecols=0, dtype=str)[0] gts = Gts(code=code) # get data gts.data_xyz = np.genfromtxt('test.dat', usecols=(2, 3, 4, 5, 6, 7, 8, 9, 10, 11)) # decimal year dates in UNR files only have 4 digits, making the day time very approximate # we round the dates at 12:00 and prefer the date string str_date = np.genfromtxt('test.dat', usecols=(1), dtype=str) np_datetime = np.array( [datetime.datetime.strptime(x, "%y%b%d") for x in str_date]) + delta_12h #unr_dates_decyear = gts.data_xyz[:,0] #np_year = np.array(unr_dates_decyear, dtype=int) #(np_doy,_np_ut) = at.decyear2dayno( unr_dates_decyear ) #gts.data_xyz[:,0] = at.dayno2decyear( np_doy , np_year ) gts.data_xyz[:, 0] = at.datetime2decyear(np_datetime) # convert data gts.xyz2neu(corr=True, ref_xyz=None, verbose=verbose) # remove 'test.dat' os.remove('test.dat') # return return gts
def same_site(self, dc=10, in_place=True, verbose=False): ################################################################### """ Check that all gts in the current Sgts are actually the same site. If a given time series is found to be of two separate sites, then a new gts is added to the return Sgts instance. param dc: critical distance to decide to split the time series param in_place: if True modify current Sgts, False retuen a new Sgts param verbose: verbose mode return: a new Sgts instance """ # import import numpy as np from pyacs.gts.Sgts import Sgts from pyacs.gts.Gts import Gts if not in_place: new_Sgts = Sgts(read=False) # start loop on sites lcode = self.lcode() for site in lcode: if verbose: print('-- Processing ', site) my_ts = self.__dict__[site].copy() if my_ts.data_xyz is not None: data = my_ts.data_xyz[:, 1:4] ddata = np.copy(my_ts.data_xyz[:, 1:4]) else: # if no data_xyz go to next gts print( "!!! WARNING: data_xyz attribute required for method same_site and not found gts %s" % (site)) # ensure median calculation if np.mod(data.shape[0], 2) == 0: # duplicates the last date ddata = np.vstack((ddata, ddata[-1, :])) median = np.median(ddata, axis=0) dist_data = np.sqrt(np.sum((data - median)**2, axis=1)) lindex = np.where(dist_data > dc * 1.E3) # case gts needs to be split if len(lindex[0]) > 0: # create a new code new_code = my_ts.code[:3] + '_' if new_code in self.lcode(): print( "!!! ERROR: try to create a new gts with code %s and it already exists." % (new_code)) new_code = my_ts.code[:2] + '__' if verbose: print( "-- time series for site %s appears to include different sites because there are coordinates at %d dates %.1lf km from the median position" % (site, len(lindex), np.max(ddata) * 1.E-3)) print( "-- %s time series will be split into code %s and code %s" % (site, site, new_code)) # create a new gts new_gts = Gts(code=new_code, data_xyz=np.copy(my_ts.data_xyz[lindex])) new_gts.xyz2neu(corr=True) # remove the line from my_ts my_ts.data_xyz = np.delete(my_ts.data_xyz, lindex, axis=0) my_ts.xyz2neu(corr=True) # update the ouput if in_place: self.append(new_gts) else: new_Sgts.append(new_gts) if in_place: self.__dict__[site] = my_ts else: new_Sgts.append(my_ts) if in_place: return self else: return new_Sgts