feat: parser_type_7 adicionado, falta adicionar nos returns
This commit is contained in:
61
parser.py
61
parser.py
@@ -1,13 +1,26 @@
|
|||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from datetime import datetime
|
from datetime import datetime, time
|
||||||
|
|
||||||
def is_blank(l: str) -> bool:
|
def is_blank(l: str) -> bool:
|
||||||
return len(l.strip(" ")) == 0
|
return len(l.strip(" ")) == 0
|
||||||
|
|
||||||
|
def parse_flt(v:str) -> float | None:
|
||||||
|
try:
|
||||||
|
t = float(v)
|
||||||
|
return t
|
||||||
|
except ValueError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def parse_int(v:str) -> int | None:
|
||||||
|
try:
|
||||||
|
t = int(v)
|
||||||
|
return t
|
||||||
|
except ValueError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def parse():
|
def parse():
|
||||||
fp = open("test_data.txt")
|
fp = open("dados.txt")
|
||||||
data = [l for l in fp.read().split("\n")]
|
data = [l for l in fp.read().split("\n")]
|
||||||
chunks = boundaries(data)
|
chunks = boundaries(data)
|
||||||
|
|
||||||
@@ -32,14 +45,17 @@ def boundaries(data: list[str]):
|
|||||||
|
|
||||||
|
|
||||||
def parse_chunk(chunk_lines: list[str]):
|
def parse_chunk(chunk_lines: list[str]):
|
||||||
header = None
|
hIdx = None
|
||||||
for (idx, l) in enumerate(chunk_lines):
|
for (idx, l) in enumerate(chunk_lines):
|
||||||
if l[-1] == "7":
|
if l[-1] == "7":
|
||||||
header = idx
|
hIdx = idx
|
||||||
break
|
break
|
||||||
parse_header(chunk_lines[:header])
|
headersRet = parse_header(chunk_lines[:hIdx])
|
||||||
|
# TODO: implementar o parser das fases parser_type_7
|
||||||
|
|
||||||
|
return headersRet
|
||||||
|
|
||||||
|
|
||||||
from pprint import pprint
|
|
||||||
def parse_header(hLines: list[str]):
|
def parse_header(hLines: list[str]):
|
||||||
aux = defaultdict(list)
|
aux = defaultdict(list)
|
||||||
|
|
||||||
@@ -47,12 +63,8 @@ def parse_header(hLines: list[str]):
|
|||||||
match line[-1]:
|
match line[-1]:
|
||||||
case "1":
|
case "1":
|
||||||
aux[1].append(line)
|
aux[1].append(line)
|
||||||
case "2":
|
|
||||||
aux[2].append(line)
|
|
||||||
case "3":
|
case "3":
|
||||||
aux[3].append(line)
|
aux[3].append(line)
|
||||||
case "5":
|
|
||||||
aux[5].append(line)
|
|
||||||
case "6":
|
case "6":
|
||||||
aux[6].append(line)
|
aux[6].append(line)
|
||||||
case "E":
|
case "E":
|
||||||
@@ -68,14 +80,13 @@ def parse_header(hLines: list[str]):
|
|||||||
for (k,v) in aux.items():
|
for (k,v) in aux.items():
|
||||||
if len(v) != 0:
|
if len(v) != 0:
|
||||||
headerDict.update(FUNCS[k](v))
|
headerDict.update(FUNCS[k](v))
|
||||||
pprint(headerDict)
|
return headerDict
|
||||||
|
|
||||||
|
|
||||||
def parse_mag(line: str):
|
def parse_mag(line: str):
|
||||||
magnitudes = []
|
magnitudes = []
|
||||||
base = 55
|
base = 55
|
||||||
while base < 79:
|
while base < 79:
|
||||||
print(base)
|
|
||||||
m = line[base:base+4]
|
m = line[base:base+4]
|
||||||
mt = line[base+4]
|
mt = line[base+4]
|
||||||
if not is_blank(m):
|
if not is_blank(m):
|
||||||
@@ -109,26 +120,34 @@ def parse_type_1(data: list[str]):
|
|||||||
|
|
||||||
return hypo
|
return hypo
|
||||||
|
|
||||||
|
|
||||||
def parse_type_2(data: list[str]):
|
|
||||||
return {}
|
|
||||||
|
|
||||||
def parse_type_3(data: list[str]):
|
def parse_type_3(data: list[str]):
|
||||||
comments = []
|
comments = []
|
||||||
for line in data:
|
for line in data:
|
||||||
comments.append(line[:-2].strip())
|
comments.append(line[:-2].strip())
|
||||||
return {"Comments": comments}
|
return {"Comments": comments}
|
||||||
|
|
||||||
def parse_type_5(data: list[str]):
|
|
||||||
return {}
|
|
||||||
|
|
||||||
def parse_type_6(data: list[str]):
|
def parse_type_6(data: list[str]):
|
||||||
waves = []
|
waves = []
|
||||||
for l in data:
|
for l in data:
|
||||||
waves.append(l.strip().split(" ")[0])
|
waves.append(l.strip().split(" ")[0])
|
||||||
|
|
||||||
return {"Wave": waves}
|
return {"Wave": waves}
|
||||||
|
|
||||||
|
|
||||||
|
def parse_type_7(data: list[str]):
|
||||||
|
phases = []
|
||||||
|
# nordic format
|
||||||
|
for l in data:
|
||||||
|
print(l)
|
||||||
|
h = int(l[18:20])
|
||||||
|
m = int(l[20:22])
|
||||||
|
sec = int(l[23:25])
|
||||||
|
mil = int(l[26:28]) * 10**4
|
||||||
|
t = time(h,m,sec,mil)
|
||||||
|
phases.append({"Stat:":l[1:5], "Com": l[6:10], "I": l[9].strip(), "Phase": l[10:15].strip(), "Polarity": l[16].strip(), "Time": t.isoformat(), "Duration": parse_flt(l[29:33]), "Amplitude": parse_flt(l[34:40]), "Period": parse_flt(l[41:45]), "Azimuth": parse_flt(l[46:51]), "Velocity":parse_int(l[52:56]), "AIN": parse_int(l[57:60]), "AR": l[61:63], "Travel Time": parse_flt(l[63:67]), "Weigth": parse_int(l[67:70]), "Distance": float(l[71:75]), "CAZ": int(l[76:79])})
|
||||||
|
|
||||||
|
return {"Phases": phases}
|
||||||
|
|
||||||
def parse_type_e(data: list[str]):
|
def parse_type_e(data: list[str]):
|
||||||
aux = data[0]
|
aux = data[0]
|
||||||
error = {"Gap": int(aux[5:8]), "Origin": float(aux[14:20]), "Error_lat": float(aux[24:30]), "Error_long": float(aux[32:38]), "Error_depth": float(aux[38:43]), "Cov_xy": float(aux[43:55]), "Cov_xz": float(aux[55:67]), "Cov_yz": float(aux[67:79])}
|
error = {"Gap": int(aux[5:8]), "Origin": float(aux[14:20]), "Error_lat": float(aux[24:30]), "Error_long": float(aux[32:38]), "Error_depth": float(aux[38:43]), "Cov_xy": float(aux[43:55]), "Cov_xz": float(aux[55:67]), "Cov_yz": float(aux[67:79])}
|
||||||
@@ -138,12 +157,14 @@ def parse_type_e(data: list[str]):
|
|||||||
def parse_type_f(data: list[str]):
|
def parse_type_f(data: list[str]):
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
||||||
def parse_type_i(data: list[str]):
|
def parse_type_i(data: list[str]):
|
||||||
aux = data[0]
|
aux = data[0]
|
||||||
dt = datetime.strptime(aux[12:26], "%y-%m-%d %H:%M")
|
dt = datetime.strptime(aux[12:26], "%y-%m-%d %H:%M")
|
||||||
return {"Action": aux[8:11], "Action Extra": {"Date": dt.isoformat(), "OP": aux[30:35].strip(), "Status": aux[42:57].strip(), "ID":int(aux[60:74])}}
|
return {"Action": aux[8:11], "Action Extra": {"Date": dt.isoformat(), "OP": aux[30:35].strip(), "Status": aux[42:57].strip(), "ID":int(aux[60:74])}}
|
||||||
|
|
||||||
FUNCS = {1: parse_type_1, 2: parse_type_2, 3: parse_type_3, 5: parse_type_5, 6: parse_type_6, "E": parse_type_e, "F": parse_type_f, "I": parse_type_i}
|
|
||||||
|
FUNCS = {1: parse_type_1, 3: parse_type_3, 6: parse_type_6, "E": parse_type_e, "F": parse_type_f, "I": parse_type_i}
|
||||||
|
|
||||||
|
|
||||||
parse()
|
parse()
|
||||||
|
|||||||
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pytest==8.4.2
|
||||||
Reference in New Issue
Block a user