def Import(self, FileName, Header=[], Delimiter=',', SkipLine=0, Comment='#'): tab = AT.AsciiTable() tab.Import(FileName, header=Header, delimiter=Delimiter, skipline=SkipLine, comment=Comment, dtype='s') for I,D in enumerate(tab.data): if 'Id' in D.keys(): I = D['Id'] else: I += 1 L = CU.LocationInit() M = CU.MagnitudeInit() for K in tab.header: if K in L: L[K] = D[K] if K in M: M[K] = D[K] if 'Log' in D.keys(): O = D['Log'] else: O = '' self.AddEvent(I, L, M, O)
def MagConvert(Db, MagAgency, MagOld, MagNew, ConvFun, Coeff=None, Owrite=True): if type(MagAgency) != list: MagAgency = [MagAgency] if type(MagOld) != list: MagOld = [MagOld] if Owrite: DbC = Db else: DbC = Db.Copy() Cnt = 0 for E in DbC.Events: for A in E['Magnitude']: for Agn in MagAgency: if A['MagCode'] == Agn or Agn == '*': for Mag in MagOld: if A['MagType'] == Mag or Mag == '*': MS = A['MagSize'] ME = A['MagError'] if not MS: MS = None if not ME: ME = 0 if Coeff: ms, me = ConvFun(MS, ME, Coeff) else: ms, me = ConvFun(MS, ME) # Rounding if ms != None: ms = float(format(ms, '.2f')) if me != None: me = float(format(me, '.2f')) if ms != None: E['Log'] += 'MAGCONV({0}:{1});'.format( A['MagCode'], A['MagType']) A['MagSize'] = CU.CastValue('MagSize', ms) A['MagError'] = CU.CastValue('MagError', me) A['MagType'] = CU.CastValue('MagType', MagNew) Cnt += 1 print("Converting {0} to {1}: {2} events found".format( MagOld, MagNew, Cnt)) if not Owrite: return DbC
def AddEvent(self, Id, Location=[], Magnitude=[], Log='', Append=False): Event = {} Event['Id'] = CU.CastValue('Id', Id) Event['Log'] = Log Event['Location'] = [] Event['Magnitude'] = [] if Location: if type(Location) is dict: Location = [Location] for L in Location: Event['Location'].append(CU.LocationInit()) for K in L.keys(): Event['Location'][-1][K] = CU.CastValue(K, L[K]) if Magnitude: if type(Magnitude) is dict: Magnitude = [Magnitude] for M in Magnitude: Event['Magnitude'].append(CU.MagnitudeInit()) for K in M.keys(): Event['Magnitude'][-1][K] = CU.CastValue(K, M[K]) if not Append: self.Events.append(Event) else: I = self.GetIndex(Id) if I != []: self.Events[I]['Location'] += Event['Location'] self.Events[I]['Magnitude'] += Event['Magnitude'] else: print('Warning: Not a valid Id')
def Export(self, FileName): tab = AT.AsciiTable() tab.header = [ 'Id', 'Year', 'Month', 'Day', 'Hour', 'Minute', 'Second', 'Latitude', 'Longitude', 'Depth', 'SecError', 'LatError', 'LonError', 'DepError', 'LocCode', 'MagSize', 'MagError', 'MagType', 'MagCode', 'Log' ] DbC = self.Copy() for E in DbC.Events: Data = [E['Id']] if not E['Location']: E['Location'] = [CU.LocationInit()] if not E['Magnitude']: E['Magnitude'] = [CU.MagnitudeInit()] for Key in tab.header[1:-1]: Grp = CU.KeyGroup(Key) Data.append(E[Grp][0][Key]) Data.append(E['Log']) tab.AddElement(Data) tab.Export(FileName)
def SetField(self, Key, Value, Match=[]): Group = CU.KeyGroup(Key) for E in self.Events: for P in E[Group]: if Match and (P[Match[0]] == Match[1]): P[Key] = CU.CastValue(Key, Value) if not Match: P[Key] = CU.CastValue(Key, Value)
def ImportIRIS(self, file_name, magcheck=True): # Defining the headers/estruture Header = [ 'EventID', 'Time', 'Latitude', 'Longitude', 'Depth/km', 'Author', 'Catalog', 'Contributor', 'ContributorID', 'MagType', 'Magnitude', 'MagAuthor', 'EventLocationName' ] # Open/importing IRIS file tab = AT.AsciiTable() tab.Import(file_name, header=Header, delimiter='|', skipline=1, dtype='s') if tab.data: for D in tab.data: I = D['EventID'] L = CU.LocationInit() M = CU.MagnitudeInit() date = D['Time'].strip() L['Year'] = int(date[0:4]) L['Month'] = int(date[5:7]) L['Day'] = int(date[8:10]) L['Hour'] = int(date[11:13]) L['Minute'] = int(date[14:16]) L['Second'] = float(date[17:22]) L['Latitude'] = float(D['Latitude']) L['Longitude'] = float(D['Longitude']) if D['Depth/km']: L['Depth'] = float(D['Depth/km']) L['SecError'] = None L['LatError'] = None L['LonError'] = None L['DepError'] = None L['LocCode'] = D['Catalog'] L['Prime'] = True M['MagSize'] = float(D['Magnitude']) M['MagError'] = None if D['MagType']: M['MagType'] = D['MagType'] else: M['MagType'] = 'Unknown' M['MagCode'] = D['MagAuthor'] O = '' self.AddEvent(I, L, M, O)
def ImportIscGem(self, file_name): Header = [ 'date', 'lat', 'lon', 'smajax', 'sminax', 'strike', 'q', 'depth', 'depthunc', 'q', 'mw', 'mwunc', 'q ', 's', 'mo', 'fac', 'mo_auth', 'mpp', 'mpr', 'mrr', 'mrt', 'mtp', 'mtt', 'str1', 'dip1', 'rake1', 'str2', 'dip2', 'rake2', 'type', 'eventid' ] # Open/importing ISC_GEM_EXT csv file tab = AT.AsciiTable() tab.Import(file_name, header=Header, delimiter=',', comment='#', dtype='s') if tab.data: for D in tab.data: I = D['eventid'] L = CU.LocationInit() M = CU.MagnitudeInit() date = D['date'].strip() L['Year'] = int(date[0:4]) L['Month'] = int(date[5:7]) L['Day'] = int(date[8:10]) L['Hour'] = int(date[11:13]) L['Minute'] = int(date[14:16]) L['Second'] = float(date[17:22]) L['Latitude'] = float(D['lat']) L['Longitude'] = float(D['lon']) L['Depth'] = float(D['depth']) L['SecError'] = None L['LatError'] = None L['LonError'] = None L['DepError'] = None L['LocCode'] = 'ISC-GEM' L['Prime'] = True M['MagSize'] = float(D['mw']) M['MagError'] = float(D['mwunc']) M['MagType'] = 'Mw' M['MagCode'] = 'ISC-GEM' O = '' self.AddEvent(I, L, M, O)
def Sort(self, Key='Time', Owrite=True): if Key == 'Time': Value = [] for E in self.Events: L = E['Location'][0] S = CU.DateToSec(L['Year'], L['Month'], L['Day'], L['Hour'], L['Minute'], L['Second']) Value.append(S) Rev = False if Key == 'Magnitude': Value = [] for E in self.Events: M = E['Magnitude'][0]['MagSize'] Value.append(M) Rev = True # Get indexes of the sorted list Ind = sorted(range(len(Value)), key=lambda k: Value[k], reverse=Rev) Events = [] for I in Ind: Events.append(self.Events[I]) if Owrite: self.Events = cp.deepcopy(Events) else: Out = Database() Out.Header = cp.deepcopy(self.Header) Out.Events = cp.deepcopy(Events) return Out
def Print(self, I): if CU.IsType(I, 's'): I = self.GetIndex(I) if I != []: E = self.Events[I] print('Event Id: {0}'.format(E['Id'])) print('Location:') for n, L in enumerate(E['Location']): print('[{0}] -'.format(n), end='') print('Year: {0}'.format(L['Year']), end='') print('Month: {0}'.format(L['Month']), end='') print('Day: {0}'.format(L['Day']), end='') print('Hour: {0}'.format(L['Hour']), end='') print('Minute: {0}'.format(L['Minute']), end='') print('Second: {0}'.format(L['Second']), end='') print('Latitude: {0}'.format(L['Latitude']), end='') print('Longitude: {0}'.format(L['Longitude']), end='') print('Depth: {0}'.format(L['Depth']), end='') print('Agency: {0}'.format(L['LocCode']), end='') print('Prime: {0}'.format(L['Prime'])) print('Magnitude:') for m, M in enumerate(E['Magnitude']): print('[{0}] -'.format(m), end='') print('Type: {0}'.format(M['MagType']), end='') print('Size: {0}'.format(M['MagSize']), end='') print('Error: {0}'.format(M['MagError']), end='') print('Agency: {0}'.format(M['MagCode'])) print('Log:') print('{0}'.format(E['Log'])) else: print('Warning: Event not found')
def ImportIscGemExt(self, file_name): # Defining the headers/estruture Header = [ 'Id', 'LocCode', 'MagCode', '', 'Year', 'Month', 'Day', 'Hour', 'Minute', 'Second', 'SecError', 'Longitude', 'Latitude', '', '', '', 'Depth', 'DepError', 'MagSize', 'MagError' ] def _GetMagCode(MagCode): try: MC = MagCode.split('|')[1] except: MC = MagCode return MC # Open/importing ISC_GEM_EXT csv file tab = AT.AsciiTable() tab.Import(file_name, header=Header, delimiter=',', skipline=1, comment='#', dtype='s') if tab.data: for I, D in enumerate(tab.data): if 'Id' in D.keys(): I = D['Id'] else: I += 1 # Changing the Magcode [Identifier] D['MagCode'] = _GetMagCode(D['MagCode']) L = CU.LocationInit() M = CU.MagnitudeInit() for K in tab.header: if K in L: L[K] = D[K] if K in M: M[K] = D[K] if 'Log' in D.keys(): O = D['Log'] else: O = '' self.AddEvent(I, L, M, O)
def AreaSelect(Db, XY, File=None, Owrite=False, Any=False, Buffer=[], Unwrap=False, Reverse=False): if isinstance(XY, CU.Polygon): P = cp.deepcopy(XY) else: P = CU.Polygon() if File: P.Import(XY, Type=File) else: P.Load(XY) if Unwrap: P.Unwrap() if Buffer: P.AddBuffer(Buffer) DbC = Cat.Database() DbC.Header = cp.deepcopy(Db.Header) for E in Db.Events: Event = {} Event['Location'] = [] for L in E['Location']: x = L['Longitude'] y = L['Latitude'] if Unwrap: x = x if x >= 0. else x + 360. if Reverse: if not P.IsInside(x, y): Event['Location'].append(L) else: if P.IsInside(x, y): Event['Location'].append(L) if Event['Location']: Event['Id'] = E['Id'] Event['Log'] = E['Log'] Event['Magnitude'] = E['Magnitude'] if Any: Event['Location'] = E['Location'] DbC.Events.append(cp.deepcopy(Event)) if Owrite: Db.Events = DbC.Events else: return DbC
def ImportNEIC(self, file_name, magcheck=True): # Open/importing USGS-NEIC file tab = AT.AsciiTable() tab.Import(file_name, delimiter=',', skipline=0, dtype='s') if tab.data: for I, D in enumerate(tab.data): L = CU.LocationInit() M = CU.MagnitudeInit() date = D['time'].strip() L['Year'] = int(date[0:4]) L['Month'] = int(date[5:7]) L['Day'] = int(date[8:10]) L['Hour'] = int(date[11:13]) L['Minute'] = int(date[14:16]) L['Second'] = float(date[17:22]) L['Latitude'] = float(D['latitude']) L['Longitude'] = float(D['longitude']) if D['depth']: L['Depth'] = float(D['depth']) L['SecError'] = None L['LatError'] = None L['LonError'] = None if D['depthError']: L['DepError'] = float(D['depthError']) L['LocCode'] = D['locationSource'] L['Prime'] = True M['MagSize'] = float(D['mag']) if D['magError']: M['MagError'] = float(D['magError']) M['MagType'] = D['magType'] M['MagCode'] = D['magSource'] O = '' self.AddEvent(I, L, M, O)
def DelEvent(self, I): if CU.IsType(I, 's'): I = self.GetIndex(I) else: I = int(I) if I != []: del self.Events[I] else: print('Warning: Event not found')
def Extract(self, Key=[], All=False): Group = CU.KeyGroup(Key) if Key == 'Id' or Key == 'Log': Values = [E[Key] for E in self.Events] else: if not All: Values = [E[Group][0][Key] for E in self.Events if E[Group]] else: Values = [] for E in self.Events: for I in E[Group]: Values.append(I[Key]) return Values
def KeyStat(self, Key, Verbose=False): ItemList = [] Group = CU.KeyGroup(Key) for E in self.Events: for P in E[Group]: Item = P[Key] ItemList.append(Item) ItemDict = {i:ItemList.count(i) for i in set(ItemList)} ItemList = sorted(ItemDict, key=ItemDict.get, reverse=True) if Verbose: print(Key, ': Occurrence') print('----------------------') for w in ItemList: print(w, ':', ItemDict[w]) return ItemList, ItemDict
def DeltaLen(C0, C1): Dis = CU.WgsDistance(C0[1], C0[0], C1[1], C1[0]) return Dis
def GetDate(Event): L = Event['Location'][0] S = CU.DateToSec(L['Year'], L['Month'], L['Day'], L['Hour'], L['Minute'], L['Second']) return S
#----------------------------------------------------------------------------------------- # Import catalogue Db1 = Cat.Database() Db1.Load('data/isc-rev-africa-select.bin') #----------------------------------------------------------------------------------------- # Filtering by polygon # As WKT Format or List CASE = 1 if CASE == 1: p = 'POLYGON((20. -20.,40. -20.,40. 0.,20. 0.))' Db2 = Exp.AreaSelect(Db1, p) P = Ct.Polygon() P.Load(p) if CASE == 2: p = [(20., -20.), (40., -20.), (40., 0.), (20., 0.)] Db2 = Exp.AreaSelect(Db1, p) P = Ct.Polygon() P.Load(p) if CASE == 3: p = 'data/area.xy' Db2 = Exp.AreaSelect(Db1, p, File='xy') P = Ct.Polygon() P.Import(p) if CASE == 4:
def SmoothMFD(Db, a, XY, Window=GaussWin, Par=50., Delta=0.1, SphereGrid=False, Box=[], Buffer=[], Grid=[], Threshold=-100, Unwrap=False, ZeroRates=False): if isinstance(XY, CU.Polygon): P = cp.deepcopy(XY) else: P = CU.Polygon() P.Load(XY) if Par <= 0: Par = np.inf # Catalogue selection DbS = Sel.AreaSelect(Db, P, Owrite=0, Buffer=Buffer, Unwrap=Unwrap) x, y, z = Exp.GetHypocenter(DbS) # Unwrapping coordinates if Unwrap: x = [i if i > 0. else i + 360. for i in x] P.Unwrap() # Creating the mesh grid if Grid: XY = [G for G in Grid if P.IsInside(G[0], G[1])] else: if SphereGrid: XY = P.SphereGrid(Delta=Delta, Unwrap=Unwrap) else: XY = P.CartGrid(Dx=Delta, Dy=Delta, Bounds=Box) Win = [] for xyP in XY: Win.append(0) for xyE in zip(x, y): Dis = CU.WgsDistance(xyP[1], xyP[0], xyE[1], xyE[0]) Win[-1] += Window(Dis, Par) # Using homogenous zone if no events are found #if all(i == 0 for i in Win): # Win = [1. for i in Win] # Scaling and normalising the rates Norm = np.sum(Win) A = [] X = [] Y = [] for I, W in enumerate(Win): aT = -np.inf if Norm > 0. and W > 0.: aT = a + np.log10(W / Norm) if aT < Threshold: # Filter below threshold aT = -np.inf if ZeroRates: A.append(aT) X.append(XY[I][0]) Y.append(XY[I][1]) else: if aT > -np.inf: A.append(aT) X.append(XY[I][0]) Y.append(XY[I][1]) if Unwrap: # Wrap back longitudes X = [x if x < 180. else x - 360. for x in X] return X, Y, A
def Filter(self,Key, Value, Opr='=', Best=False, All=False, Owrite=True): def Search(Event, Value, Str0, Str1, Opr, Best): NewE = {} NewE[Str1] = [] NewE[Str0] = [] Klist = [] """ for V in Value: for E in Event[Str0]: if (Opr == '=') and (E[Key] == V): NewE[Str0].append(E) if (Opr == '!=') and (E[Key] != V): NewE[Str0].append(E) if (Opr == '>') and (E[Key] > V): NewE[Str0].append(E) if (Opr == '<') and (E[Key] < V): NewE[Str0].append(E) if (Opr == '>=') and (E[Key] >= V): NewE[Str0].append(E) if (Opr == '<=') and (E[Key] <= V): NewE[Str0].append(E) """ for E in Event[Str0]: if (Opr == '=') and any([V==E[Key] for V in Value]): NewE[Str0].append(E) if (Opr == '!=') and not any([V==E[Key] for V in Value]): NewE[Str0].append(E) if (Opr == '>') and any([V<E[Key] for V in Value]): NewE[Str0].append(E) if (Opr == '<') and any([V>E[Key] for V in Value]): NewE[Str0].append(E) if (Opr == '>=') and any([V<=E[Key] for V in Value]): NewE[Str0].append(E) if (Opr == '<=') and any([V>=E[Key] for V in Value]): NewE[Str0].append(E) Klist = [k[Key] for k in NewE[Str0]] if NewE[Str0]: NewE['Id'] = Event['Id'] NewE['Log'] = Event['Log'] NewE[Str1] = Event[Str1] if Best: NewE[Str0] = [NewE[Str0][0]] return NewE, Klist Out = Database() Out.Header = cp.deepcopy(self.Header) if not CU.IsType(Value, 'l'): Value = [Value] Group = CU.KeyGroup(Key) for E in self.Events: if Group == 'Location': E, Klist = Search(E, Value, 'Location', 'Magnitude', Opr, Best) if Group == 'Magnitude': E, Klist = Search(E, Value, 'Magnitude', 'Location', Opr, Best) if E['Location'] or E['Magnitude']: if All: if sorted(Value) == sorted(set(Klist)): Out.Events.append(cp.deepcopy(E)) else: Out.Events.append(cp.deepcopy(E)) if Owrite: self.Events = Out.Events else: return Out
def DeltaDep(Z0, Z1): if not CU.IsEmpty(Z0) and not CU.IsEmpty(Z1): DD = ma.fabs(Z1 - Z0) else: DD = 0. return DD
def DeltaMag(M0, M1): if not CU.IsEmpty(M0) and not CU.IsEmpty(M1): DM = ma.fabs(M1 - M0) else: DM = 0. return DM
def TimeSelect(Db, Date0, Date1, Owrite=False): Def0 = [1900, 1, 1, 0, 0, 0] Def1 = [2000, 12, 31, 23, 59, 59] if type(Date0) != list: Date0 = [Date0] if type(Date1) != list: Date1 = [Date1] for N in range(len(Date0), 6): Date0.append(Def0[N]) for N in range(len(Date1), 6): Date1.append(Def1[N]) Sec0 = CU.DateToSec(Date0[0], Date0[1], Date0[2], Date0[3], Date0[4], Date0[5]) Sec1 = CU.DateToSec(Date1[0], Date1[1], Date1[2], Date1[3], Date1[4], Date1[5]) """ # OLD VERSION (COMPARE ONLY FIRST LOCATION) def GetSec(Event): L = Event['Location'][0] S = CU.DateToSec(L['Year'], L['Month'], L['Day'], L['Hour'], L['Minute'], L['Second']) return S Sec = [] for E in Db.Events: Sec.append(GetSec(E)) Ind = [I for I,S in enumerate(Sec) if S>=Sec0 and S<=Sec1] DbN = Cat.Database() DbN.Header = cp.deepcopy(Db.Header) for I in Ind: DbN.Events.append(cp.deepcopy(Db.Events[I])) """ DbN = Cat.Database() DbN.Header = cp.deepcopy(Db.Header) for E in Db.Events: Event = {} Event['Location'] = [] for L in E['Location']: S = CU.DateToSec(L['Year'], L['Month'], L['Day'], L['Hour'], L['Minute'], L['Second']) if S >= Sec0 and S <= Sec1: Event['Location'].append(L) if Event['Location']: Event['Id'] = E['Id'] Event['Log'] = E['Log'] Event['Magnitude'] = E['Magnitude'] DbN.Events.append(cp.deepcopy(Event)) if Owrite: Db.Events = DbN.Events else: return DbN