def linear_regress_ts(tsin, coortype='ENU', titledetails=''): """ doit être cablé ASAP linear_regress_find_coeff""" try: A, B, C, T, sA, sB, sC = tsin.to_list(coortype) fig, axes = plt.subplots(2, 2) fig.suptitle(tsin.stat + ' ' + titledetails) axes = np.reshape(axes, -1) W = linear_regress_find_coeff(tsin, coortype) for i, composante in enumerate((A, B, C)): M = np.array([T, np.ones(len(T))]) # w = scipy.linalg.lstsq(M.T,composante)[0] w = W[i] xi = np.arange(min(T), max(T), 10**5) line = w[0] * xi + w[1] axes[i].plot(conv.posix2dt(xi), line, 'r-') axes[i].plot(conv.posix2dt(T), composante, '+') axes[i].set_title(coortype[i]) axes[i].set_xlabel('Date') axes[i].set_ylabel('Displacement (m)') v = w[0] * 31556926 / 10**-3 v2 = round(v, 2) print('composante ' + coortype[i] + ' = ' + str(v) + 'mm/year') axes[i].text(0.9, 0.1, str(v2) + 'mm/year', horizontalalignment='center', verticalalignment='center', transform=axes[i].transAxes) except: pass
def export_ts_as_midas_tenu(tsin, outdir, outprefix, coordtype='ENU', export_step=True): """ export to a MIDAS .tneu compatible format outfile will be writed in /outdir/outprefixSTAT.tneu if export_step == True: export a step file as /outdir/outprefixSTAT.step """ if not hasattr(tsin[0], 'X'): print('WARN : export_ts_as_midas_tneu : no XYZ in ts') noXYZ = True else: noXYZ = False tswork = copy.deepcopy(tsin) stat = tswork.stat if coordtype == 'XYZ': mp = tswork.mean_posi() tswork.ENUcalc(mp) outpath = outdir + '/' + outprefix + tswork.stat + '.tenu' outfile = open(outpath, 'w+') E, N, U, T, sE, sN, sU = tswork.to_list('ENU') first_pt = tswork.pts[0] if noXYZ: first_pt.X = 0. first_pt.Y = 0. first_pt.Z = 0. first_pt.L = 0. first_pt.H = 0. first_pt.F = 0. e0, n0, u0, t0 = list(zip(E, N, U, T))[0] for e, n, u, t in zip(E, N, U, T): t = conv.toYearFraction(conv.posix2dt(t)) #outfile.write('{} {:.5f} {:+.6f} {:+.6f} {:+.6f} \n'.format(stat,t,n-n0,e-e0,u-u0)) outfile.write('{} {:.5f} {:+.6f} {:+.6f} {:+.6f} \n'.format( stat, t, e - e0, n - n0, u - u0)) print('INFO : timeserie exported in ' + outpath) if export_step and tswork.bool_discont: outpath_step = outdir + '/' + outprefix + tswork.stat + '.step' outfile_step = open(outpath_step, 'w+') for d in tswork.discont: d = conv.toYearFraction(d) line = tswork.stat + " " + str(d) + "\n" outfile_step.write(line) print('INFO : timeserie discont. (steps) exported in ' + outpath_step) return None
def time_win_basic(start,end,Tlisin,Datalisin,outposix=True,invert=False, out_array=False , out_boolis = False , only_boolis = False): """ In Intern, we works in POSIX only_boolis : To gain speed, no operation on Tlis & Datalisin is be done None is outputed for Tlisout , Datalisout Outputs : If out_boolis == True: Tlisout , Datalisout , boolis If out_boolis == False: Tlisout , Datalisout """ if isinstance(Tlisin[0],dt.datetime): Tlis = conv.dt2posix(Tlisin) else: Tlis = Tlisin if isinstance(start,dt.datetime): start = conv.dt2posix(start) if isinstance(end,dt.datetime): end = conv.dt2posix(end) if not isinstance(Tlis,np.ndarray) or not isinstance(Datalisin,np.ndarray): Tlis = np.array(Tlis) Datalis = np.array(Datalisin) else: Tlis = Tlis Datalis = Datalisin boolis = (start <= Tlis) * (Tlis <= end) if invert: boolis = np.logical_not(boolis) if only_boolis: Datalisout = None Tlisout = None else: Datalisout = Datalis[boolis] Tlisout = Tlis[boolis] if not outposix: Tlisout = conv.posix2dt(Tlisout) if out_array: Tlisout , Datalisout = np.array(Tlisout) , np.array(Datalisout) if out_boolis: out_tuple = (Tlisout , Datalisout , boolis) else: out_tuple = (Tlisout , Datalisout) return out_tuple
def add_offset_smart_for_GINS_kine(tsin, tslist_offset_3ple, list_windows, coortype='XYZ'): """ tslist_offset_3ple : list of len N containing (dX,dY,dZ) offsets list_windows : list of len N-1 containing dates of changes """ if type(list_windows[0]) is int: list_windows = [conv.posix2dt(e) for e in list_windows] list_windows = [dt.datetime(1980, 1, 1) ] + list_windows + [dt.datetime(2099, 1, 1)] tsout = copy.deepcopy(tsin) tsout.del_data() olddX, olddY, olddZ = 12345, 12345, 12345 for pt in tsin: for i in range(len(list_windows) - 1): if list_windows[i] <= pt.Tdt < list_windows[i + 1]: curdX, curdY, curdZ = tslist_offset_3ple[i][ 0], tslist_offset_3ple[i][1], tslist_offset_3ple[i][2] if curdX != olddX or curdY != olddY or curdZ != olddZ: olddX, olddY, olddZ = curdX, curdY, curdZ print('INFO : change of time window / offset') ptout = add_offset_point(pt, curdX, curdY, curdZ, coortype='XYZ') tsout.add_point(ptout) continue return tsout
def time_win_multi_start_end(Start_list_in, End_list_in, Tlisin, Datalisin, outposix=True, invert=False, out_array=False, out_boolis=False): """ In Intern, we works in POSIX Outputs : If out_boolis == True: Tlisout , Datalisout , boolis_opera , boolis_stk (4 values !!) If out_boolis == False: Tlisout , Datalisout """ if len(Start_list_in) != len(End_list_in): print( "ERR : time_win_multi_start_end : len(Start_list_in) != len(End_list_in) !!" ) boolis_stk = [] for start, end in zip(Start_list_in, End_list_in): _, _, boolis = time_win_basic(start, end, Tlisin, Datalisin, outposix=outposix, invert=invert, out_boolis=True, only_boolis=True) boolis_stk.append(boolis) boolis_stk = np.stack(boolis_stk) boolis_opera = np.all(boolis_stk, axis=0) Datalis = np.array(Datalisin) Tlis = np.array(Tlisin) Datalisout = Datalis[boolis_opera] Tlisout = Tlis[boolis_opera] if not outposix: Tlisout = conv.posix2dt(Tlisout) if out_array: Tlisout, Datalisout = np.array(Tlisout), np.array(Datalisout) if out_boolis: out_tuple = (Tlisout, Datalisout, boolis_opera, boolis_stk) else: out_tuple = (Tlisout, Datalisout) return out_tuple
def enddate(self): """ Method to get the last epoch of the data in the TimeSerie Returns ------- DateTime """ self.sort() return conv.posix2dt(self.pts[-1].T)
def Tset(self, T=0): if T == 0: T = dt.datetime.now() if type(T) == dt.datetime: self.Tdt = T self.T = conv.dt2posix(T) else: self.T = float(T) self.Tdt = conv.posix2dt(float(T))
def startdate(self): """ Method to get the first epoch of the data in the TimeSerie Returns ------- DateTime """ self.sort() return conv.posix2dt(self.pts[0].T)
def export_ts(ts, outdir, coordtype='ENU', outprefix='', write_header=False): """ export the timeserie write_header not well implemented !!! """ proto_str = '{:23} ' * 14 A, B, C, T, sA, sB, sC = ts.to_list(coordtype) if outprefix != '': outprefix = outprefix + '_' outfilenam = outprefix + ts.stat + "_" + ts.name + '.' + coordtype + '.ts.dat' outpath = os.path.join(outdir, outfilenam) filobj = open(outpath, 'w+') if write_header: header = "#" + proto_str.format( *(coordtype[0], "sigma" + coordtype[0], coordtype[1], "sigma" + coordtype[1], coordtype[2], "sigma" + coordtype[2], "year", "month", "day", "hour", "minute", "seconds", "year_decimal", "posix_time")) filobj.write(header + "\n") for a, b, c, t, sa, sb, sc in zip(A, B, C, T, sA, sB, sC): paramstr = [str(e) for e in [a, sa, b, sb, c, sc]] #paramstr = [e.ljust(18, '0') for e in paramstr] tt = conv.posix2dt(t) yr_dec_str = str(conv.dt2year_decimal(tt)) posix_str = str(t) paramstr_time = list(tt.strftime("%Y %m %d %H %M %S").split()) paramstr2 = paramstr + paramstr_time + [yr_dec_str, posix_str] outlin = proto_str.format(*paramstr2) + "\n" filobj.write(outlin) filobj.close() print('INFO : timeserie exported in ' + outpath) return None
def plot(self,diapt=10,alpha=0.8,fig=1,new_style=True): A, B, C , T , sA, sB, sC = self.to_list() print("plot :", self.nbobs , "obs., ", self.name) if self.typeobs == 'RPY': listtitle = ['','Roll','Pitch','Yaw'] else: listtitle = ['','','',''] namest=0 namend=10 Tdt = conv.posix2dt(T) if new_style: styleint = 410 else: styleint = 220 plt.figure(fig) plt.subplot(styleint + 1) plt.axis('equal') plt.plot(A,B,'.',label=str(self.devID)[namest:namend], markersize=diapt, alpha=alpha) plt.legend() plt.title(listtitle[0]) plt.subplot(styleint + 2) plt.plot(Tdt,A,'.',label=str(self.devID)[namest:namend], markersize=diapt, alpha=alpha) plt.legend() plt.title(listtitle[1]) plt.subplot(styleint + 3) plt.plot(Tdt,B,'.',label=str(self.devID)[namest:namend], markersize=diapt, alpha=alpha) plt.legend() plt.title(listtitle[2]) plt.subplot(styleint + 4) plt.plot(Tdt,C,'.',label=str(self.devID)[namest:namend], markersize=diapt, alpha=alpha) plt.legend() plt.title(listtitle[3])
def to_dataframe(self, coortype='XYZ'): """ Export the TimeSerie Object as DataFrame Parameters ---------- coortype : str or iterable of str. The coordinates type exported to the DataFrame. 'XYZ', 'FLH', 'ENU', 'NED' can be also an iterable like ('XYZ','FLH') The default is 'XYZ'. Returns ------- DF : DataFrame output DataFrame. """ if not utils.is_iterable(coortype): coortype = (coortype, ) ColStk = tuple() ColNameStk = [] for icoty, coty in enumerate(coortype): A, B, C, T, sA, sB, sC = self.to_list(coty) if icoty == 0: Tdt = conv.posix2dt(T) ColStk = ColStk + (Tdt, T, A, B, C, sA, sB, sC) ColNameStk = ["Tdt", "T"] + [e for e in coty ] + ["s" + e for e in coty] else: ColStk = ColStk + (A, B, C, sA, sB, sC) ColNameStk = [e for e in coty] + ["s" + e for e in coty] BIG = np.column_stack(ColStk) DF = pd.DataFrame(BIG) DF.columns = ColNameStk DF = DF.infer_objects() return DF
def velocity_trans(self, vx, vy, vz, epoc_init='auto', epoc_end='auto'): """ auto == epoc of the measures """ if epoc_init == 'auto' and epoc_end == 'auto': print('ERR : epoc_init == auto and epoc_end == auto') return None tdt = conv.posix2dt(self.T) yeardec = conv.dt2year_decimal(tdt) if epoc_init == 'auto': epoc_init = yeardec if epoc_end == "auto": epoc_end = yeardec Xb = reffram.itrf_speed_calc(self.X, self.Y, self.Z, epoc_init, vx, vy, vz, epoc_end) self.XYZset(*Xb) return None
def export_ts_as_neu(tsin, outdir, outprefix, coordtype='ENU'): """ export to a HECTOR .neu compatible format outfile will be writed in /outdir/outprefixSTAT.neu NB: The XYZ mode is quite dirty (191001) """ if not hasattr(tsin[0], 'X'): print('WARN : export_ts_as_neu : no XYZ in ts') noXYZ = True else: noXYZ = False tswork = copy.deepcopy(tsin) #if coordtype == 'XYZ': # mp = tswork.mean_posi() # tswork.ENUcalc(mp) outpath = outdir + '/' + outprefix + tswork.stat + '.neu' outfile = open(outpath, 'w+') E, N, U, T, sE, sN, sU = tswork.to_list(coordtype) first_pt = tswork.pts[0] if noXYZ: first_pt.X = 0. first_pt.Y = 0. first_pt.Z = 0. first_pt.L = 0. first_pt.H = 0. first_pt.F = 0. e0, n0, u0, t0 = list(zip(E, N, U, T))[0] # write the header outfile.write('# Site : {} \n'.format(tswork.stat)) if 'calc_center' in list(tswork.anex.keys()): outfile.write('# Analysis Centre: {} \n'.format( tswork.anex['calc_center'])) else: outfile.write('# Analysis Centre: N/A \n') outfile.write('# Solution code: GINS_PS \n') outfile.write('# Datum: ITRF2008\n') outfile.write('#\n') outfile.write('# Reference epoch: {}\n'.format( conv.toYearFraction(first_pt.Tdt))) outfile.write('# X : {}\n'.format(first_pt.X)) outfile.write('# Y : {}\n'.format(first_pt.Y)) outfile.write('# Z : {}\n'.format(first_pt.Z)) outfile.write('#\n') outfile.write('# Longitude : {}\n'.format(first_pt.L)) outfile.write('# Latitude : {}\n'.format(first_pt.F)) outfile.write('# Height : {}\n'.format(first_pt.H)) outfile.write('#\n') if coordtype == "ENU": outfile.write('# Components : ' + "NEU" + "\n") elif coordtype == "XYZ": outfile.write('# Components : ' + "YXZ" + "\n") outfile.write( '# Cartesian components are undirect to maintain consistency with NEU\n' ) outfile.write('#\n') if tswork.bool_discont: outfile.write( '# type_of_offset : from discontinuties got from a station.info\n') outfile.write('#\n') for disc in sorted(tswork.discont): outfile.write('# offset {} 7\n'.format(conv.toYearFraction(disc))) outfile.write('#\n') # write the data for e, n, u, t, se, sn, su in zip(E, N, U, T, sE, sN, sU): t = conv.toYearFraction(conv.posix2dt(t)) if coordtype == "ENU": outfile.write( '{:.5f} {:+.6f} {:+.6f} {:+.6f} {:+.6f} {:+.6f} {:+.6f}\n' .format(t, n - n0, e - e0, u - u0, se, sn, su)) elif coordtype == "XYZ": outfile.write( '{:.5f} {:+.6f} {:+.6f} {:+.6f} {:+.6f} {:+.6f} {:+.6f}\n' .format(t, n - n0, e - e0, u - u0, se, sn, su)) print('INFO : timeserie exported in ' + outpath) return None
def startdate(self): self.sort() return conv.posix2dt(self.pts[0].T)
def enddate(self): self.sort() return conv.posix2dt(self.pts[-1].T)
def compar2(tstup, coortype='ENU', seuil=3., win=[], mode='keep', Dtype='3D', namest=0, namend=10, alpha=5, diapt=5, verbose=True, print_report=True, plot=True): """ 160903 cette fonction semble discontinuée """ tsref = tstup[0] dicolist = [] for tsvar in tstup: Aref, Bref, Cref, Tref, _, _, _ = tsref.to_list() Avar, Bvar, Cvar, Tvar, _, _, _ = tsvar.to_list() Tref = np.round(Tref, 1) Tvar = np.round(Tvar, 1) Tcommon = np.intersect1d(Tref, Tvar) Tcommon_dt = conv.posix2dt(Tcommon) ind_ref = np.nonzero(np.in1d(Tref, Tcommon))[0] ind_var = np.nonzero(np.in1d(Tvar, Tcommon))[0] dA = Avar[ind_var] - Aref[ind_ref] dB = Bvar[ind_var] - Bref[ind_ref] dC = Cvar[ind_var] - Cref[ind_ref] if Dtype == '2D': dD = np.sqrt(dA**2 + dB**2) elif Dtype == '3D': dD = np.sqrt(dA**2 + dB**2 + dC**2) dicovar = dict() dicovar['name'] = tsvar.name dicovar['coortype'] = coortype dicovar['Dtype'] = Dtype dicovar['TA'] = Tcommon_dt dicovar['dA'] = np.array(dA) dicovar['dAbrut'] = np.array(dA) dicovar['TB'] = Tcommon_dt dicovar['dB'] = np.array(dB) dicovar['dBbrut'] = np.array(dB) dicovar['TC'] = Tcommon_dt dicovar['dC'] = np.array(dC) dicovar['dCbrut'] = np.array(dC) dicovar['TD'] = Tcommon_dt dicovar['dD'] = np.array(dD) dicovar['dDbrut'] = np.array(dD) dicovar['dDtype'] = np.array(Dtype) dicolist.append(dicovar) if plot: compar_plot(dicolist, namest, namend, alpha, diapt) try: suptit = ' '.join((tstup[-1].stat, str(tstup[-1].startdate()), str(tstup[-1].enddate()))) plt.suptitle(suptit) except: pass return dicolist
def compar(tstup, coortype='ENU', seuil=3., win=[], mode='keep', Dtype='2D3D', namest=0, namend=10, alpha=5, diapt=5, verbose=True, print_report=True, plot=True): """ si seuil == 0, pas de nettoyage On preconise compar en mode keep : Ainsi le Tref est strictement cantonné aux bonnes valeurs des Ti En mode del, les Ti sont extrapolé aux valeur de Tref => nan => pbs pour la mad if 2D3D /3D2D : selectionne 2D ou 3D (selon) en prioritaire pour dD mais détermine 2D et 3D et les introduit dans des clés adhoc dD2D dD3D ''' """ if len(Dtype) == 4: D2n3 = True Dtype = Dtype[0:2] else: D2n3 = False diapt = 10 alpha = 0.8 if len(tstup) <= 1: print( "ERR : compar : len(tstup) <= 1 , on ne compar pas un élément seul ;) !!!!" ) if verbose: print("") print("BILAN DE LA COMPARAISON") print("reference : " + tstup[0].name) print("coordonnées : " + coortype) print("") dicolist = [] for ivar, tsvar in enumerate(tstup): #dico de sortie dicovar = dict() if verbose: print( "===========================================================") print(tsvar.name) print("------------------------------") # tsout = TimeSeriePoint() # tsout = copy.copy(tsvar) # tsout.del_data() if tsvar.bool_interp_uptodate == False: tsvar.interp_set() A, B, C, T, sA, sB, sC = tsvar.to_list(coortype=coortype) tsref = tstup[0].interp_get(T, coortype=coortype) Aref, Bref, Cref, Tref, sA, sB, sC = tsref.to_list(coortype=coortype) # WTF IS THOSE LINES # autowin = time_gap(tstup[0],marge=1.1,mode='keep') # win = autowin # # outbool = [] # (winlis == [] or (ivar in winlis)) and if win != []: if verbose: print("Application du fenetrage") print("------------------------------") bl = time_win_T(T, win, mode=mode) if verbose: print("Nb de pts : ", len(bl)) print("Pts validés :", np.sum(bl)) print('') else: bl = np.asarray([True] * len(Tref)) dA = A[bl] - Aref[bl] dB = B[bl] - Bref[bl] dC = C[bl] - Cref[bl] Tref = Tref[bl] #print "N of diff close to 0 => Bug ..." #print 'dA' , np.sum(np.isclose(dA,0)) #print 'dB' , np.sum(np.isclose(dB,0)) #print 'dC' , np.sum(np.isclose(dC,0)) dD2D = np.sqrt(dA**2 + dB**2) dD3D = np.sqrt(dA**2 + dB**2 + dC**2) if Dtype == '2D': dD = np.sqrt(dA**2 + dB**2) elif Dtype == '3D': dD = np.sqrt(dA**2 + dB**2 + dC**2) else: print("ERR : compar : wrong Dtype") # print "Stats sans Nettoyage" # print "------------------------------" # print4compar(dA,dB,dC,dD,coortype) # nettoyage par la MAD if seuil != 0: if verbose: print("Nettoyage par la MAD") print("------------------------------") dAin = dA dA, bb = stats.outiler_mad(dA, seuil=seuil) dAout = dA TA = conv.posix2dt(np.array(Tref[bb])) dBin = dB dB, bb = stats.outiler_mad(dB, seuil=seuil) dBout = dB TB = conv.posix2dt(np.array(Tref[bb])) dCin = dC dC, bb = stats.outiler_mad(dC, seuil=seuil) dCout = dC TC = conv.posix2dt(np.array(Tref[bb])) dDin = dD dD, bb = stats.outiler_mad(dD, seuil=seuil) TD = conv.posix2dt(np.array(Tref[bb])) dDout = dD if D2n3: dD2Din = dD2D dD2D, bb = stats.outiler_mad(dD2D, seuil=seuil) TD2D = conv.posix2dt(np.array(Tref[bb])) dD2Dout = dD2D dD3Din = dD3D dD3D, bb = stats.outiler_mad(dD3D, seuil=seuil) TD3D = conv.posix2dt(np.array(Tref[bb])) dD3Dout = dD3D if verbose: print('') print("Stats après Nettoyage") print("------------------------------") if print_report: print4compar(dA, dB, dC, dD, coortype) else: Tdt = conv.posix2dt(Tref) TA, TB, TC, TD = Tdt, Tdt, Tdt, Tdt dAin, dBin, dCin, dDin = dA, dB, dC, dD # pour la legende : les indices de debut et fin du nom # fabrication du Dico de sortie dicovar['name'] = tsvar.name dicovar['coortype'] = coortype dicovar['Dtype'] = Dtype dicovar['TA'] = np.array(TA) dicovar['dA'] = np.array(dA) dicovar['dAbrut'] = np.array(dAin) dicovar['TB'] = np.array(TB) dicovar['dB'] = np.array(dB) dicovar['dBbrut'] = np.array(dBin) dicovar['TC'] = np.array(TC) dicovar['dC'] = np.array(dC) dicovar['dCbrut'] = np.array(dCin) dicovar['TD'] = np.array(TD) dicovar['dD'] = np.array(dD) dicovar['dDbrut'] = np.array(dDin) dicovar['dDtype'] = np.array(Dtype) if D2n3: dicovar['TD2D'] = np.array(TD2D) dicovar['dD2D'] = np.array(dD2D) dicovar['dD2Dbrut'] = np.array(dD2Din) dicovar['dD2Dtype'] = np.array('2D') dicovar['TD3D'] = np.array(TD3D) dicovar['dD3D'] = np.array(dD2D) dicovar['dD3Dbrut'] = np.array(dD3Din) dicovar['dD3Dtype'] = np.array('3D') dicolist.append(dicovar) if plot: compar_plot(dicolist, namest, namend, alpha, diapt) try: suptit = ' '.join((tstup[-1].stat, str(tstup[-1].startdate()), str(tstup[-1].enddate()))) plt.suptitle(suptit) except: pass return dicolist
def plot(self,coortype='ENU',diapt=2,alpha=0.8,fig=1, errbar=True,new_style=True,symbol = '.',errbar_width=1): """ fig can accept a int (id of a Figure) OR the figure Object itself """ if new_style: styleint = 310 else: styleint = 220 try: A, B, C , T , sA, sB, sC = self.to_list(coortype=coortype) except TypeError as tyer: print("ERR : unable to get coordinates") print("TRICK : check if the given coortype is in the timeserie") raise tyer if coortype == 'ENU': Atitle = 'East' Btitle = 'North' Ctitle = 'Up' ABtitle = 'East North' yylabel = 'displacement (m)' elif coortype == 'XYZ': Atitle = 'X' Btitle = 'Y' Ctitle = 'Z' yylabel = 'displacement (m)' ABtitle = 'X Y (sans signification)' elif coortype == 'FLH': Atitle = 'Phi' Btitle = 'Lambda' Ctitle = 'Haut' yylabel = 'displacement (m)' ABtitle = 'Phi Lambda (sans signification)' else: Atitle = 'A' Btitle = 'B' Ctitle = 'C' yylabel = 'displacement (??)' ABtitle = 'A & B' print("plot :", self.nbpts , "pts, ", self.stat) namest=0 namend=10 Tdt = conv.posix2dt(T) if type(fig) is int: figobj = plt.figure(fig) elif type(fig) is plt.Figure: figobj = fig figobj.suptitle(self.stat) if self.name: name4plot = self.name[namest:namend] else: name4plot = self.stat plt.subplot(styleint+1) if errbar: plt.errorbar(Tdt,A,sA, fmt=symbol,label=name4plot, markersize=diapt, alpha=alpha,ecolor='tab:cyan', elinewidth=errbar_width) else: plt.plot(Tdt,A,symbol,label=name4plot, markersize=diapt, alpha=alpha) try: plt.legend() except: pass #plt.xlabel('Date') plt.ylabel(yylabel) plt.title(Btitle) plt.title(Atitle) # ax = plt.gca() # if coortype == 'ENU': ## refstr = 'ref XYZ = ' + utils.join_improved(',',self.refENU.X , ## self.refENU.Y , ## self.refENU.Z) plt.subplot(styleint+2) if errbar: plt.errorbar(Tdt,B,sB, fmt=symbol,label=name4plot, markersize=diapt, alpha=alpha,ecolor='tab:cyan', elinewidth=errbar_width) else: plt.plot(Tdt,B,symbol,label=name4plot, markersize=diapt, alpha=alpha) try: plt.legend() except: pass #plt.xlabel('Date') plt.ylabel(yylabel) plt.title(Btitle) plt.subplot(styleint+3) if errbar: plt.errorbar(Tdt,C,sC, fmt=symbol,label=name4plot, markersize=diapt, alpha=alpha,ecolor='tab:cyan', elinewidth=errbar_width) else: plt.plot(Tdt,C,symbol,label=name4plot, markersize=diapt, alpha=alpha) try: plt.legend() except: pass plt.xlabel('Date') plt.ylabel(yylabel) plt.title(Ctitle) figobj.set_size_inches(8.27,11.69) figobj.tight_layout() plt.subplots_adjust(top=0.93) if not new_style: plt.subplot(styleint+4) plt.axis('equal') try: plt.legend() except: pass plt.plot(A,B,'.',label=name4plot, markersize=diapt, alpha=alpha) plt.xlabel(Atitle + ' ' + yylabel) plt.ylabel(Btitle + ' ' + yylabel) plt.title(ABtitle) return figobj
def read_erp(file_path_in, ac=None): """ Read IGS Analysis Center ERP files Parameters ---------- file_path_in : str Path of the file in the local machine. ac : str The analysis center that will be used. If not precised, will be the first 3 letters of the input name Returns ------- out1 : pandas table Returns a panda table format with the data extracted from the file. Note ---- Columns name ('MJD','X-P (arcsec)', 'Y-P (arcsec)', 'UT1UTC (E-7S)','LOD (E-7S/D)','S-X (E-6" arcsec)','S-Y (E-6" arcsec)', 'S-UT (E-7S)','S-LD (E-7S/D)','NR (E-6" arcsec)', 'NF (E-6" arcsec)', 'NT (E-6" arcsec)', 'X-RT (arcsec/D)','Y-RT (arcsec/D)','S-XR (E-6" arcsec/D)','S-YR (E-6" arcsec/D)', 'C-XY', 'C-XT', 'C-YT', 'DPSI', 'DEPS','S-DP','S-DE') """ caminho_arq = file_path_in #### FIND DELIVERY DATE name = os.path.basename(caminho_arq) if not ac: ac = name[:3] if len(name) == 12: dt_delivery = conv.sp3name2dt(caminho_arq) elif len(name) == 38: dt_delivery = conv.sp3name_v3_2dt(caminho_arq) else: dt_delivery = conv.posix2dt(0) le = open(caminho_arq, 'r') letudo = le.readlines() le.close() tamanho = len(letudo) #usado para saber quantas linhas tem o arquivo #para = tamanho #onde o arquivo para de ser lido numeros = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] #le = 0 #numlin = 0 #numero de linhas da matriz de epocas #numcol = 16 #numero de colunas que a matriz final deve ter ERP = [] if caminho_arq[-3:] in ('snx', 'ssc'): file = open(caminho_arq) Lines = file.readlines() XPO_stk = [] XPO_std_stk = [] YPO_stk = [] YPO_std_stk = [] LOD_stk = [] LOD_std_stk = [] MJD_stk = [] marker = False for i in range(len(Lines)): if len(Lines[i].strip()) == 0: continue else: if Lines[i].split()[0] == '+SOLUTION/ESTIMATE': marker = True if Lines[i].split()[0] == '-SOLUTION/ESTIMATE': marker = False if utils.contains_word(Lines[i], 'XPO') and marker: # Doy = (Lines[i][30:33]) # Year = (Lines[i][27:29]) # Pref_year = '20' # Year = int(Pref_year+Year) # Date = conv.doy2dt(Year,Doy) Date = conv.datestr_sinex_2_dt(Lines[i].split()[5]) XPO = float(Lines[i][47:68]) * (10**-3) XPO_std = float(Lines[i][69:80]) * (10**-3) XPO_stk.append(XPO) XPO_std_stk.append(XPO_std) MJD_stk.append(conv.dt2MJD(Date)) #MJD_stk.append(cmg.jd_to_mjd(cmg.date_to_jd(Date.year,Date.month,Date.day))) if utils.contains_word(Lines[i], 'YPO') and marker: # Doy = (Lines[i][30:33]) # Year = (Lines[i][27:29]) # Pref_year = '20' # Year = int(Pref_year+Year) # Date = conv.doy2dt(Year,Doy) Date = conv.datestr_sinex_2_dt(Lines[i].split()[5]) YPO = float(Lines[i][47:68]) * (10**-3) YPO_std = float(Lines[i][69:80]) * (10**-3) YPO_stk.append(YPO) YPO_std_stk.append(YPO_std) MJD_stk.append(conv.dt2MJD(Date)) #MJD_stk.append(cmg.jd_to_mjd(cmg.date_to_jd(Date.year,Date.month,Date.day))) if utils.contains_word(Lines[i], 'LOD') and marker: # Doy = (Lines[i][30:33]) # Year = (Lines[i][27:29]) # Pref_year = '20' # Year = int(Pref_year+Year) # Date = conv.doy2dt(Year,Doy) Date = conv.datestr_sinex_2_dt(Lines[i].split()[5]) LOD = float(Lines[i][47:68]) * (10**+4) LOD_std = float(Lines[i][69:80]) * (10**+4) LOD_stk.append(LOD) LOD_std_stk.append(LOD_std) #MJD_stk.append(cmg.jd_to_mjd(cmg.date_to_jd(Date.year,Date.month,Date.day))) MJD_stk.append(conv.dt2MJD(Date)) MJD = list(sorted(set(MJD_stk))) if len(LOD_stk) == 0: LOD_stk = ['0'] * len(MJD) LOD_std_stk = ['0'] * len(MJD) for i in range(len(MJD)): ERP_data = [ ac, MJD[i], XPO_stk[i], YPO_stk[i], 0, LOD_stk[i], XPO_std_stk[i], YPO_std_stk[i], 0, LOD_std_stk[i], 0, 0, 0, 0, 0, 0, 0, dt_delivery ] ERP.append(ERP_data) if ac in ('COD', 'cod', 'com', 'cof', 'grg', 'mit', 'sio', 'igs', 'igr'): for i in range(tamanho + 1): linhaatual = linecache.getline(caminho_arq, i) if linhaatual[0:1] in numeros: ERP_data = linhaatual.split() for j in range(len(ERP_data)): ERP_data[j] = float(ERP_data[j]) ERP_data.insert(0, ac) ERP_data[2] = ERP_data[2] * (10**-6) ERP_data[3] = ERP_data[3] * (10**-6) ERP_data[13] = ERP_data[13] * (10**-6) ERP_data[14] = ERP_data[14] * (10**-6) del ERP_data[17:] ERP_data.append(dt_delivery) ERP.append(ERP_data) linecache.clearcache() # Erp_end = pd.DataFrame(ERP, columns=['AC','MJD','X-P', 'Y-P', 'UT1UTC(UT1 -TAI)','LOD','S-X','S-Y','S-UT','S-LD','NR', 'NF', 'NT', # 'X-RT','Y-RT','S-XR','S-YR',"Delivery_date"]) # return Erp_end # if ac in ('wum', 'grg', 'esa', 'mit', 'ngs', 'sio'): for i in range(tamanho + 1): linhaatual = linecache.getline(caminho_arq, i) if linhaatual[0:1] in numeros: ERP_data = linhaatual.split() for j in range(len(ERP_data)): ERP_data[j] = float(ERP_data[j]) ERP_data.insert(0, ac) ERP_data[2] = ERP_data[2] * (10**-6) ERP_data[3] = ERP_data[3] * (10**-6) ERP_data[13] = ERP_data[13] * (10**-6) ERP_data[14] = ERP_data[14] * (10**-6) ERP_data.append(dt_delivery) ERP.append(ERP_data) linecache.clearcache() # Erp_end = pd.DataFrame(ERP, columns=['AC','MJD','X-P', 'Y-P', 'UT1UTC(UT1 -TAI)','LOD','S-X','S-Y','S-UT','S-LD','NR', 'NF', 'NT', # 'X-RT','Y-RT','S-XR','S-YR']) # return Erp_end # if ac in ('gbm', 'gfz', 'gfr', "p1_", "p1r"): for i in range(tamanho + 1): linhaatual = linecache.getline(caminho_arq, i) if linhaatual[0:1] in numeros: ERP_data = linhaatual.split() for j in range(len(ERP_data)): ERP_data[j] = float(ERP_data[j]) ERP_data.insert(0, ac) ERP_data[2] = ERP_data[2] * (10**-6) ERP_data[3] = ERP_data[3] * (10**-6) ERP_data[13] = ERP_data[13] * (10**-6) ERP_data[14] = ERP_data[14] * (10**-6) ERP_data.append(dt_delivery) ERP.append(ERP_data) linecache.clearcache() # Erp_end = pd.DataFrame(ERP, columns=['AC','MJD','X-P', 'Y-P', 'UT1UTC(UT1 -TAI)','LOD','S-X','S-Y','S-UT','S-LD','NR', 'NF', 'NT', # 'X-RT','Y-RT','S-XR','S-YR']) ##EH TBM O RATE XY POR DIA?????? # return Erp_end header = [] if ac in ('emr'): for i in range(tamanho + 1): linhaatual = linecache.getline(caminho_arq, i) if linhaatual == 'EOP SOLUTION': del ERP_data[:] header = ['EOP SOLUTION'] if linhaatual[0:1] in numeros and 'EOP SOLUTION' in header: ERP_data = linhaatual.split() for j in range(len(ERP_data)): ERP_data[j] = float(ERP_data[j]) ERP_data.insert(0, ac) ERP_data[2] = ERP_data[2] * (10**-6) ERP_data[3] = ERP_data[3] * (10**-6) ERP_data[13] = ERP_data[13] * (10**-6) ERP_data[14] = ERP_data[14] * (10**-6) del ERP_data[17:] ERP_data.append(dt_delivery) ERP.append(ERP_data) linecache.clearcache() Erp_end = pd.DataFrame(ERP, columns=[ 'AC', 'MJD', 'X-P', 'Y-P', 'UT1UTC(UT1 -TAI)', 'LOD', 'S-X', 'S-Y', 'S-UT', 'S-LD', 'NR', 'NF', 'NT', 'X-RT', 'Y-RT', 'S-XR', 'S-YR', 'Delivered_date' ]) return Erp_end
def plot(self, coortype='ENU', diapt=2, alpha=0.8, fig=1, errbar=True, new_style=True, symbol='.', errbar_width=1): """ Plot data in a TimeSerie Object Parameters ---------- coortype : str, optional The coordinates type. The default is 'ENU'. diapt : float, optional Point diamaeter. The default is 2. alpha : float, optional Alpha (transparency) of points. The default is 0.8. fig : int or Figure object, optional Figure ID where the data will be plotted can accept a int (id of a Figure) OR the figure Object itself. The default is 1. errbar : bool, optional Plot the error bars. The default is True. new_style : bool, optional Plot in a new style. The old style is only kept for legacy The default is True. symbol : str, optional symbol. The default is '.'. errbar_width : TYPE, optional coefficient for the error bar size. The default is 1. Returns ------- None. """ if new_style: styleint = 310 else: styleint = 220 try: A, B, C, T, sA, sB, sC = self.to_list(coortype=coortype) except TypeError as tyer: print("ERR : unable to get coordinates") print("TRICK : check if the given coortype is in the timeserie") raise tyer if coortype == 'ENU': Atitle = 'East' Btitle = 'North' Ctitle = 'Up' ABtitle = 'East North' yylabel = 'displacement (m)' elif coortype == 'XYZ': Atitle = 'X' Btitle = 'Y' Ctitle = 'Z' yylabel = 'displacement (m)' ABtitle = 'X Y (sans signification)' elif coortype == 'FLH': Atitle = 'Phi' Btitle = 'Lambda' Ctitle = 'Haut' yylabel = 'displacement (m)' ABtitle = 'Phi Lambda (sans signification)' else: Atitle = 'A' Btitle = 'B' Ctitle = 'C' yylabel = 'displacement (??)' ABtitle = 'A & B' print("plot :", self.nbpts, "pts, ", self.stat) namest = 0 namend = 10 Tdt = conv.posix2dt(T) if type(fig) is int: figobj = plt.figure(fig) elif type(fig) is plt.Figure: figobj = fig figobj.suptitle(self.stat) if self.name: name4plot = self.name[namest:namend] else: name4plot = self.stat plt.subplot(styleint + 1) if errbar: plt.errorbar(Tdt, A, sA, fmt=symbol, label=name4plot, markersize=diapt, alpha=alpha, ecolor='tab:cyan', elinewidth=errbar_width) else: plt.plot(Tdt, A, symbol, label=name4plot, markersize=diapt, alpha=alpha) try: plt.legend() except: pass #plt.xlabel('Date') plt.ylabel(yylabel) plt.title(Btitle) plt.title(Atitle) # ax = plt.gca() # if coortype == 'ENU': ## refstr = 'ref XYZ = ' + utils.join_improved(',',self.refENU.X , ## self.refENU.Y , ## self.refENU.Z) plt.subplot(styleint + 2) if errbar: plt.errorbar(Tdt, B, sB, fmt=symbol, label=name4plot, markersize=diapt, alpha=alpha, ecolor='tab:cyan', elinewidth=errbar_width) else: plt.plot(Tdt, B, symbol, label=name4plot, markersize=diapt, alpha=alpha) try: plt.legend() except: pass #plt.xlabel('Date') plt.ylabel(yylabel) plt.title(Btitle) plt.subplot(styleint + 3) if errbar: plt.errorbar(Tdt, C, sC, fmt=symbol, label=name4plot, markersize=diapt, alpha=alpha, ecolor='tab:cyan', elinewidth=errbar_width) else: plt.plot(Tdt, C, symbol, label=name4plot, markersize=diapt, alpha=alpha) try: plt.legend() except: pass plt.xlabel('Date') plt.ylabel(yylabel) plt.title(Ctitle) figobj.set_size_inches(8.27, 11.69) figobj.tight_layout() plt.subplots_adjust(top=0.93) if not new_style: plt.subplot(styleint + 4) plt.axis('equal') try: plt.legend() except: pass plt.plot(A, B, '.', label=name4plot, markersize=diapt, alpha=alpha) plt.xlabel(Atitle + ' ' + yylabel) plt.ylabel(Btitle + ' ' + yylabel) plt.title(ABtitle) return figobj
def to_list(self, coortype='XYZ', specific_output=None, time_as_datetime=False): """ Export the TimeSerie Object as Lists (Numpy Arrays) Parameters ---------- coortype : str, optional The coordinates type exported to the list. The default is 'XYZ'. specific_output : int, optional ask for a specific list, ranges between 0 and 6. The default is None. time_as_datetime : bool, optional if True the Time list is exported in DataFrame if False the Time list is exported in Posix time Returns ------- A,B,C,T,sA,sB,sC : lists A = X, F (latitude), E. B = Y, L (longitude), N. C = Z, H (hight), U. T = Time sA = sigma of A component sB = sigma of B component sC = sigma of C component """ if coortype == 'XYZ': A, B, C = 'X', 'Y', 'Z' sA, sB, sC = 'sX', 'sY', 'sZ' elif coortype == 'FLH': A, B, C = 'F', 'L', 'H' sA, sB, sC = 'sF', 'sL', 'sH' elif coortype == 'ENU': if self.boolENU == False: print("WARN : to_list : pas de coord. ENU pour " + self.name) return 0 A, B, C = 'E', 'N', 'U' sA, sB, sC = 'sE', 'sN', 'sU' else: print("ERR : to_list : le coortype n'existe pas") if self.nbpts == 0: print("ERR : to_list : " + self.name + " the timeserie is empty") A = np.asarray([getattr(pt, A) for pt in self.pts]) B = np.asarray([getattr(pt, B) for pt in self.pts]) C = np.asarray([getattr(pt, C) for pt in self.pts]) T = np.asarray([pt.T for pt in self.pts]) if hasattr(self.pts[0], sA): sA = np.asarray([getattr(pt, sA) for pt in self.pts]) sB = np.asarray([getattr(pt, sB) for pt in self.pts]) sC = np.asarray([getattr(pt, sC) for pt in self.pts]) else: sA = np.asarray([np.nan] * len(self.pts)) sB = np.asarray([np.nan] * len(self.pts)) sC = np.asarray([np.nan] * len(self.pts)) # il faut squeezer les vecteurs parce que des fois on se retrouve # avec des matrices if time_as_datetime: Tout = conv.posix2dt(T) else: Tout = T sq = np.squeeze outtup = (sq(A), sq(B), sq(C), sq(Tout), sq(sA), sq(sB), sq(sC)) if specific_output == None: return outtup elif type(specific_output) is int: return outtup[specific_output] else: print("INFO : this mode must be implemented ;)") print("use an int as index instead") return outtup