import requests

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Float
from sqlalchemy.orm import sessionmaker

from apscheduler.schedulers.blocking import BlockingScheduler

import time
from xml.etree import ElementTree

PSKReporterURL = "https://retrieve.pskreporter.info/query?noactive=1&rronly=1&flowStartSeconds=-900&frange=50000000-51000000&appcontact=<EMAIL>"
RTSWProtonURL =  "https://services.swpc.noaa.gov/json/rtsw/rtsw_wind_1m.json"
RTSWMagneticURL = "https://services.swpc.noaa.gov/json/rtsw/rtsw_mag_1m.json"
GOESElectronURL = "https://services.swpc.noaa.gov/json/goes/primary/integral-electrons-6-hour.json"
GOESProtonURL = "https://services.swpc.noaa.gov/json/goes/primary/integral-protons-6-hour.json"
GOESMagenticURL = "https://services.swpc.noaa.gov/json/goes/primary/magnetometers-6-hour.json"
GOESXray = "https://services.swpc.noaa.gov/json/goes/primary/xrays-6-hour.json"
 
scheduler = BlockingScheduler() 

eng = create_engine('sqlite:///data.db')

Base = declarative_base()
 
class ReceptionReport(Base):
    __tablename__ = "ReceptionReports"
 
    Id = Column(Integer, primary_key=True)
    RecordedTime = Column(Integer) #Time recorded into the DB
    ReportedTime = Column(Integer) #Time according to PSKReporter it was recieved
    ReceiverCallsign = Column(String)
    SenderCallsign = Column(String)
    ReceiverLocator = Column(String)  
    SenderLocator = Column(String) 
    Mode = Column(String)
    Frequency = Column(Integer) #In Hz
    
class SpaceWeatherReport(Base):
    __tablename__ = "SpaceWeatherReports"
    
    Id = Column(Integer, primary_key=True)
    RecordedTime = Column(Integer) #Time recorded into the DB
    ProtonSpeed = Column(Float)
    ProtonDensity = Column(Float)
    ProtonTemperature = Column(Integer)
    InterplanetaryMagneticFieldStrength = Column(Float)
    IntegralElectrons = Column(Float)
    IntegralProtons1MeV = Column(Float)
    IntegralProtons5MeV = Column(Float)
    IntegralProtons10MeV = Column(Float)
    IntegralProtons30MeV = Column(Float)
    IntegralProtons50MeV = Column(Float)
    IntegralProtons60MeV = Column(Float)
    IntegralProtons100MeV = Column(Float)
    IntegralProtons500MeV = Column(Float)
    MagneticFieldStrength = Column(Float)
    Xrays8 = Column(Float)
    Xrays4 = Column(Float)
    

    
Session = sessionmaker(bind=eng)
ses = Session()  
Base.metadata.create_all(eng)
  

def CollectData():
    print("Collecting Data at "+ time.ctime())
    now = int(time.time())
    pskReportData = ElementTree.fromstring(requests.get(PSKReporterURL).text)
    reports = list(pskReportData)[2:]
    for x in reports:
        ses.add(ReceptionReport(RecordedTime=now,ReportedTime=int(x.get("flowStartSeconds")),ReceiverCallsign=x.get("receiverCallsign"),SenderCallsign=x.get("senderCallsign"),ReceiverLocator=x.get("receiverLocator"),SenderLocator=x.get("senderLocator"),Mode=x.get("mode"),Frequency=x.get("frequency")))
    
    rtswProtonData = requests.get(RTSWProtonURL).json()
    proton_speed = None
    proton_density = None
    proton_temperature = None
    for x in rtswProtonData:
        proton_speed = x["proton_speed"]
        proton_density = x["proton_density"]
        proton_temperature = x["proton_temperature"]
        if (not proton_speed == None) and (not proton_density == None) and (not proton_temperature == None):
            break
    
    rtswMagData = requests.get(RTSWMagneticURL).json()
    bt = rtswMagData[0]["bt"]
    temp = 1
    while bt == None:
        bt = x[temp]["bt"]
        
        
    
    electronFlux = requests.get(GOESElectronURL).json()[-1]["flux"]
    
    protonFluxData = requests.get(GOESProtonURL).json()[-32:]
    mev1 = (protonFluxData[0]["flux"] + protonFluxData[8]["flux"] + protonFluxData[16]["flux"] + protonFluxData[24]["flux"])/4
    mev10 = (protonFluxData[1]["flux"] + protonFluxData[9]["flux"] + protonFluxData[17]["flux"] + protonFluxData[25]["flux"])/4
    mev100 = (protonFluxData[2]["flux"] + protonFluxData[10]["flux"] + protonFluxData[18]["flux"] + protonFluxData[26]["flux"])/4
    mev30 = (protonFluxData[3]["flux"] + protonFluxData[11]["flux"] + protonFluxData[19]["flux"] + protonFluxData[27]["flux"])/4
    mev5 = (protonFluxData[4]["flux"] + protonFluxData[12]["flux"] + protonFluxData[20]["flux"] + protonFluxData[28]["flux"])/4
    mev50 = (protonFluxData[5]["flux"] + protonFluxData[13]["flux"] + protonFluxData[21]["flux"] + protonFluxData[29]["flux"])/4
    mev500 = (protonFluxData[6]["flux"] + protonFluxData[14]["flux"] + protonFluxData[22]["flux"] + protonFluxData[30]["flux"])/4
    mev60 =  (protonFluxData[7]["flux"] + protonFluxData[15]["flux"] + protonFluxData[23]["flux"] + protonFluxData[31]["flux"])/4
  
    GoesMagData = requests.get(GOESMagenticURL).json()[-4:]
    Hp = 0
    for x in GoesMagData:
        Hp += x["Hp"]
    Hp = Hp/4
    
    GoesXray = requests.get(GOESXray).json()[-4:]
    xray4 = (GoesXray[0]["flux"] + GoesXray[2]["flux"])/2
    xray8 = (GoesXray[1]["flux"] + GoesXray[3]["flux"])/2
    
    ses.add(SpaceWeatherReport(RecordedTime=now,ProtonSpeed=proton_speed,ProtonDensity=proton_density,ProtonTemperature=proton_temperature,InterplanetaryMagneticFieldStrength=bt,IntegralElectrons=electronFlux,IntegralProtons1MeV=mev1,IntegralProtons5MeV=mev5,IntegralProtons10MeV=mev10,IntegralProtons30MeV=mev30,IntegralProtons50MeV=mev50,IntegralProtons60MeV=mev60,IntegralProtons100MeV=mev100,IntegralProtons500MeV=mev500,MagneticFieldStrength=Hp,Xrays8=xray8,Xrays4=xray4))
    ses.commit()
    
scheduler.add_job(CollectData,'interval',minutes=15)
CollectData()

try:
    scheduler.start()
except (KeyboardInterrupt, SystemExit):
    pass

import requests

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Float
from sqlalchemy.orm import sessionmaker

from apscheduler.schedulers.blocking import BlockingScheduler

import time
from xml.etree import ElementTree

PSKReporterURL = "https://retrieve.pskreporter.info/query?noactive=1&rronly=1&flowStartSeconds=-900&frange=50000000-51000000&appcontact=<EMAIL>"
RTSWProtonURL =  "https://services.swpc.noaa.gov/json/rtsw/rtsw_wind_1m.json"
RTSWMagneticURL = "https://services.swpc.noaa.gov/json/rtsw/rtsw_mag_1m.json"
GOESElectronURL = "https://services.swpc.noaa.gov/json/goes/primary/integral-electrons-6-hour.json"
GOESProtonURL = "https://services.swpc.noaa.gov/json/goes/primary/integral-protons-6-hour.json"
GOESMagenticURL = "https://services.swpc.noaa.gov/json/goes/primary/magnetometers-6-hour.json"
GOESXray = "https://services.swpc.noaa.gov/json/goes/primary/xrays-6-hour.json"
 
scheduler = BlockingScheduler() 

eng = create_engine('sqlite:///data.db')

Base = declarative_base()
 
class ReceptionReport(Base):
    __tablename__ = "ReceptionReports"
 
    Id = Column(Integer, primary_key=True)
    RecordedTime = Column(Integer) #Time recorded into the DB
    ReportedTime = Column(Integer) #Time according to PSKReporter it was recieved
    ReceiverCallsign = Column(String)
    SenderCallsign = Column(String)
    ReceiverLocator = Column(String)  
    SenderLocator = Column(String) 
    Mode = Column(String)
    Frequency = Column(Integer) #In Hz
    
class SpaceWeatherReport(Base):
    __tablename__ = "SpaceWeatherReports"
    
    Id = Column(Integer, primary_key=True)
    RecordedTime = Column(Integer) #Time recorded into the DB
    ProtonSpeed = Column(Float)
    ProtonDensity = Column(Float)
    ProtonTemperature = Column(Integer)
    InterplanetaryMagneticFieldStrength = Column(Float)
    IntegralElectrons = Column(Float)
    IntegralProtons1MeV = Column(Float)
    IntegralProtons5MeV = Column(Float)
    IntegralProtons10MeV = Column(Float)
    IntegralProtons30MeV = Column(Float)
    IntegralProtons50MeV = Column(Float)
    IntegralProtons60MeV = Column(Float)
    IntegralProtons100MeV = Column(Float)
    IntegralProtons500MeV = Column(Float)
    MagneticFieldStrength = Column(Float)
    Xrays8 = Column(Float)
    Xrays4 = Column(Float)
    

    
Session = sessionmaker(bind=eng)
ses = Session()  
Base.metadata.create_all(eng)
  

def CollectData():
    print("Collecting Data at "+ time.ctime())
    now = int(time.time())
    pskReportData = ElementTree.fromstring(requests.get(PSKReporterURL).text)
    reports = list(pskReportData)[2:]
    for x in reports:
        ses.add(ReceptionReport(RecordedTime=now,ReportedTime=int(x.get("flowStartSeconds")),ReceiverCallsign=x.get("receiverCallsign"),SenderCallsign=x.get("senderCallsign"),ReceiverLocator=x.get("receiverLocator"),SenderLocator=x.get("senderLocator"),Mode=x.get("mode"),Frequency=x.get("frequency")))
    
    rtswProtonData = requests.get(RTSWProtonURL).json()
    proton_speed = None
    proton_density = None
    proton_temperature = None
    for x in rtswProtonData:
        proton_speed = x["proton_speed"]
        proton_density = x["proton_density"]
        proton_temperature = x["proton_temperature"]
        if (not proton_speed == None) and (not proton_density == None) and (not proton_temperature == None):
            break
    
    rtswMagData = requests.get(RTSWMagneticURL).json()
    bt = rtswMagData[0]["bt"]
    temp = 1
    while bt == None:
        bt = x[temp]["bt"]
        
        
    
    electronFlux = requests.get(GOESElectronURL).json()[-1]["flux"]
    
    protonFluxData = requests.get(GOESProtonURL).json()[-32:]
    mev1 = (protonFluxData[0]["flux"] + protonFluxData[8]["flux"] + protonFluxData[16]["flux"] + protonFluxData[24]["flux"])/4
    mev10 = (protonFluxData[1]["flux"] + protonFluxData[9]["flux"] + protonFluxData[17]["flux"] + protonFluxData[25]["flux"])/4
    mev100 = (protonFluxData[2]["flux"] + protonFluxData[10]["flux"] + protonFluxData[18]["flux"] + protonFluxData[26]["flux"])/4
    mev30 = (protonFluxData[3]["flux"] + protonFluxData[11]["flux"] + protonFluxData[19]["flux"] + protonFluxData[27]["flux"])/4
    mev5 = (protonFluxData[4]["flux"] + protonFluxData[12]["flux"] + protonFluxData[20]["flux"] + protonFluxData[28]["flux"])/4
    mev50 = (protonFluxData[5]["flux"] + protonFluxData[13]["flux"] + protonFluxData[21]["flux"] + protonFluxData[29]["flux"])/4
    mev500 = (protonFluxData[6]["flux"] + protonFluxData[14]["flux"] + protonFluxData[22]["flux"] + protonFluxData[30]["flux"])/4
    mev60 =  (protonFluxData[7]["flux"] + protonFluxData[15]["flux"] + protonFluxData[23]["flux"] + protonFluxData[31]["flux"])/4
  
    GoesMagData = requests.get(GOESMagenticURL).json()[-4:]
    Hp = 0
    for x in GoesMagData:
        Hp += x["Hp"]
    Hp = Hp/4
    
    GoesXray = requests.get(GOESXray).json()[-4:]
    xray4 = (GoesXray[0]["flux"] + GoesXray[2]["flux"])/2
    xray8 = (GoesXray[1]["flux"] + GoesXray[3]["flux"])/2
    
    ses.add(SpaceWeatherReport(RecordedTime=now,ProtonSpeed=proton_speed,ProtonDensity=proton_density,ProtonTemperature=proton_temperature,InterplanetaryMagneticFieldStrength=bt,IntegralElectrons=electronFlux,IntegralProtons1MeV=mev1,IntegralProtons5MeV=mev5,IntegralProtons10MeV=mev10,IntegralProtons30MeV=mev30,IntegralProtons50MeV=mev50,IntegralProtons60MeV=mev60,IntegralProtons100MeV=mev100,IntegralProtons500MeV=mev500,MagneticFieldStrength=Hp,Xrays8=xray8,Xrays4=xray4))
    ses.commit()
    
scheduler.add_job(CollectData,'interval',minutes=15)
CollectData()

try:
    scheduler.start()
except (KeyboardInterrupt, SystemExit):
    pass

#Time recorded into the DB #Time according to PSKReporter it was recieved #In Hz #Time recorded into the DB