Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Profesergiom committed Sep 30, 2022
0 parents commit 8282ba5
Show file tree
Hide file tree
Showing 8 changed files with 1,127 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
*.pyc
*.csv
*.jpg
*.json
keys/
last/
loc/
time/
model/
inputs/
obs/
runtime/lastloc.txt
26 changes: 26 additions & 0 deletions RT.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 30 09:27:34 2022
@author: sergio.morales
"""

from main import mainnll
import sched, time
s = sched.scheduler(time.time, time.sleep)


def do_something(sc):
from datetime import datetime
fecha=datetime.now()
print(str(fecha)+ " Monitoreando eventos nuevos clasificados...")
try:
mainnll(exec_point='local',debug=False)
except:
print('algo pasó :(')
s.enter(5, 1, do_something, (sc,))



s.enter(1, 1, do_something, (s,))
s.run()
81 changes: 81 additions & 0 deletions lib/dblib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 28 15:20:35 2022
@author: sergio.morales
"""

def getLastEv(debug=False):
pathlastloc='runtime/lastloc.txt'
import pandas as pd
import ovdas_getfromdb_lib as gdb
import datetime as dt
fini = dt.datetime.strftime(dt.datetime.utcnow() - dt.timedelta(days=1), '%Y-%m-%d')
ffin = dt.datetime.strftime(dt.datetime.utcnow() + dt.timedelta(days=1), '%Y-%m-%d')
ev = pd.DataFrame(gdb.extraer_eventos(inicio=fini,final=ffin,volcan='*',ml='>0'))
ev = ev[ev.tipoevento.isin(['VT','LP','LV','TO','HB'])]
ev = ev[ev.cod!='-']
ev = ev.tail(1)
last_loc_hypo = str(ev['idevento'].iloc[0])+'-'+str(ev.fecha.iloc[0].year)
print("Último evento localizado por Hypo: "+last_loc_hypo)
import os
if debug==False:
if os.path.isfile(pathlastloc):
last_loc_NLL = open(pathlastloc).read().splitlines()[0]
with open(pathlastloc, "w") as text_file:
print(f"{last_loc_hypo}", file=text_file)
else:
print('Archivo lastloc.txt no existe, creando...')
with open(pathlastloc, "w") as text_file:
print("None", file=text_file)
last_loc_NLL = open(pathlastloc).read().splitlines()[0]
if last_loc_hypo==last_loc_NLL:
print('No hay eventos nuevos localizados')
reloc=False
else:
print('A localizar!')
reloc=True
with open(pathlastloc, "w") as text_file:
print(f"{last_loc_hypo}", file=text_file)
else:
reloc=True
return reloc, ev


def saveNLLcsv(voldf,ev,NLL,exec_point):
if exec_point == 'local':
raiz='//172.16.40.102/Monitoreo/ovv2023/csv/'

elif exec_point == 'server':
raiz='/mnt/puntocientodos/ovv2023/'
import pandas as pd
import os
volcan=voldf.nombre_db.iloc[0]
fecha=str(ev.fecha.dt.year.iloc[0])+str(ev.fecha.dt.month.iloc[0]).zfill(2)+str(ev.fecha.dt.day.iloc[0]).zfill(2)
#Guardar
file = fecha+'_'+volcan+"_NLLoc.csv"
row = (str(ev.fecha.iloc[0])+' '+str(NLL[0])+' '+str(NLL[1])+' '+str(NLL[2])+' '+str(NLL[3])+' '+str(NLL[4])+' '+str(NLL[5])
+' '+str(ev.tipoevento.iloc[0])+' '+str(ev.idevento.iloc[0])+'-'+str(ev.fecha.iloc[0].year)
)
df = pd.DataFrame([row])
raiz= raiz+str(ev.fecha.dt.year.iloc[0])+'/'+str(ev.fecha.dt.month.iloc[0]).zfill(2)+'/'+str(ev.fecha.dt.day.iloc[0]).zfill(2)+'/'
local = 'csv/'+str(ev.fecha.dt.year.iloc[0])+'/'+str(ev.fecha.dt.month.iloc[0]).zfill(2)+'/'+str(ev.fecha.dt.day.iloc[0]).zfill(2)+'/'
print(raiz)
if (os.path.exists(raiz+file)==True):
df.to_csv(raiz+file,sep=';',index=False,mode='a',header=False)
else:
if (os.path.exists(raiz)==True):
df.to_csv(raiz+file,sep=';',index=False,header=False)
else:
os.makedirs(raiz)
df.to_csv(raiz+file,sep=';',index=False,header=False)

if (os.path.exists(local+file)==True):
df.to_csv(local+file,sep=';',index=False,mode='a',header=False)
else:
if (os.path.exists(local)==True):
os.makedirs(local)
df.to_csv(local+file,sep=';',index=False,header=False)
else:
os.makedirs(local)
df.to_csv(local+file,sep=';',index=False,header=False)
153 changes: 153 additions & 0 deletions lib/maillib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 28 16:19:43 2022
@author: sergio.morales
"""

import os
import pickle
# Gmail API utils
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# for encoding/decoding messages in base64
#from base64 import urlsafe_b64decode, urlsafe_b64encode
# for dealing with attachement MIME types
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from mimetypes import guess_type as guess_mime_type
#import textwrap

# Request all access (permission to read/send/receive emails, manage the inbox, and more)
SCOPES = ['https://mail.google.com/']
our_email = '[email protected]'

def gmail_authenticate():
creds = None
# the file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first time
if os.path.exists("keys/token.pickle"):
with open("keys/token.pickle", "rb") as token:
creds = pickle.load(token)
# if there are no (valid) credentials availablle, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('keys/gmailapi.json', SCOPES)
creds = flow.run_local_server(port=0)
# save the credentials for the next run
with open("keys/token.pickle", "wb") as token:
pickle.dump(creds, token)
return build('gmail', 'v1', credentials=creds)



# Adds the attachment with the given filename to the given message
def add_attachment(message, filename):
content_type, encoding = guess_mime_type(filename)
if content_type is None or encoding is not None:
content_type = 'application/octet-stream'
main_type, sub_type = content_type.split('/', 1)
if main_type == 'text':
fp = open(filename, 'rb')
msg = MIMEText(fp.read().decode(), _subtype=sub_type)
fp.close()
elif main_type == 'image':
fp = open(filename, 'rb')
msg = MIMEImage(fp.read(), _subtype=sub_type)
fp.close()
elif main_type == 'audio':
fp = open(filename, 'rb')
msg = MIMEAudio(fp.read(), _subtype=sub_type)
fp.close()
else:
fp = open(filename, 'rb')
msg = MIMEBase(main_type, sub_type)
msg.set_payload(fp.read())
fp.close()
filename = os.path.basename(filename)
msg.add_header('Content-Disposition', 'attachment', filename=filename)
message.attach(msg)


def build_message(destination, obj, body, attachments=[]):
from base64 import urlsafe_b64encode
cuerpo = MIMEText(body,'plain')


if not attachments: # no attachments given
message = cuerpo
message['to'] = destination
message['from'] = our_email
message['subject'] = obj
else:
message = MIMEMultipart()
message['to'] = destination
message['from'] = our_email
message['subject'] = obj
message.attach(cuerpo)
for filename in attachments:
add_attachment(message, filename)
return {'raw': urlsafe_b64encode(message.as_bytes()).decode()}


def send_message(service, destination, obj, body, attachments=[]):
return service.users().messages().send(
userId="me",

body=build_message(destination, obj, body, attachments)
).execute()


def sendMail(voldf,evsel):
import numpy as np
#volcan=voldf.nombre_db.iloc[0]
idev = evsel.idevento.iloc[0]
amp= evsel.amplitud_ums.iloc[0]
frec= evsel.frecuencia.iloc[0]
tipoev = evsel.tipoevento.iloc[0]
prof = evsel.profundidad_abs.iloc[0]
lat = evsel.latitud.iloc[0]
lon = evsel.longitud.iloc[0]
ML= evsel.ml.iloc[0]
volcan_real = voldf.nombre.iloc[0]
tipo = voldf.vol_tipo.iloc[0]
#lista_debug=['[email protected]','[email protected]']
lista_real = ['[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]'
]
asunto="Sismo tipo "+tipoev+" ML="+str(ML)+" "+tipo+" "+volcan_real


cuerpo = """\nSismo localizado en el {tipov} {volcan}:
\nTipo Evento: {tipo}
Magnitud : {ml} (ML)
Amplitud: {amplitud} um/s (referencia)
Frecuencia: {frecuencia} Hz (Referencia)
Profundidad: {profundidad} km bajo nivel del mar
Latitud: {latitud}°
Longitud: {longitud}°
\nSe adjunta imagen correspondiente a localización con Hypo71 realizada en la sala de monitoreo y localización usando NLL en base a las fases P y S detectadas por los especialistas.
\nSaludos.
\n(Este correo es automático, no responder)
"""
cuerpo = cuerpo.format(tipo=tipoev,ml=ML,tipov=tipo,volcan=volcan_real,amplitud=amp,frecuencia=frec,profundidad=np.round(prof,1),
latitud=np.round(lat,2),longitud=np.round(lon,2))
for destino in lista_real:
print(destino)
send_message(gmail_authenticate(), destino, asunto,cuerpo,['figs/loc_'+str(idev)+'.jpg'])
Loading

0 comments on commit 8282ba5

Please sign in to comment.