from urllib.request import urlopen
import csv
import numpy as np
import base64
import json
import datetime
from datetime import date
import psycopg2
from NEWAFireInfo.settings import DATABASES

def checkdates():
    dbstring = DATABASES['default']
    con = psycopg2.connect(
        host=dbstring['HOST'],
        port=dbstring['PORT'],
        database=dbstring['NAME'],
        user=dbstring['USER'],
        password=dbstring['PASSWORD'])
    cur = con.cursor()

    checked_yesterday = False
    check_yesterday=''

    today = date.today()
    today = str(today)
    today = today.split('-')
    today = today[0] + today[1] + today[2]
    yesterday = str(int(today) - 1)

    if yesterday[6:] == '00':
        if yesterday[4:6] == '01':
            yesterday = str(int(yesterday[:4])-1) + '1231'
        elif yesterday[4:6] == '02':
            yesterday = str(int(yesterday[:4])) + '0131'
        elif yesterday[4:6] == '03':
            if int(yesterday[:4]) % 4 == 0:
                yesterday = str(int(yesterday[:4])) + '0229'
            else:
                yesterday = str(int(yesterday[:4])) + '0228'
        elif yesterday[4:6] == '04':
            yesterday = str(int(yesterday[:4])) + '0331'
        elif yesterday[4:6] == '05':
            yesterday = str(int(yesterday[:4])) + '0430'
        elif yesterday[4:6] == '06':
            yesterday = str(int(yesterday[:4])) + '0531'
        elif yesterday[4:6] == '07':
            yesterday = str(int(yesterday[:4])) + '0630'
        elif yesterday[4:6] == '08':
            yesterday = str(int(yesterday[:4])) + '0731'
        elif yesterday[4:6] == '09':
            yesterday = str(int(yesterday[:4])) + '0831'
        elif yesterday[4:6] == '10':
            yesterday = str(int(yesterday[:4])) + '0930'
        elif yesterday[4:6] == '11':
            yesterday = str(int(yesterday[:4])) + '1031'
        elif yesterday[4:6] == '12':
            yesterday = str(int(yesterday[:4])) + '1130'
    cur.execute("SELECT date FROM firedata WHERE date='" + yesterday + "' ORDER BY date")
    answer = cur.fetchone()
    try:
        check_yesterday = answer[0]
        if check_yesterday == yesterday:
            checked_yesterday = True
    except:
        pass
    if checked_yesterday == True:
        con.close()
        return True
    else:
        con.close()
        return False


def fetch():

    link = "http://pnwwildfireplanning.pythonanywhere.com/nfdrs/wanec/kaniksu"
    #link = "http://pnwwildfireplanning.pythonanywhere.com/nfdrs/wanec/upperbasin"
 
    f = urlopen(link)
    myfile = f.read()
    s=myfile.decode()
    ts=s.find('const docs_json')+19
    tf = s.find('const render_items')-19
    s=s[ts:tf]
    t=s
    test1=0
    test2=0
    temphumid=[]
    i=0
    j=0
    while test1>=0 and test2!=-1:
        test1 = t.find('{"attributes"')
        test2 = t.find(',{"attributes"')
        stest = t[test1:test2]
        if stest.find('"attributes":{"data"') != -1:
            if stest.find('"shape":[7299]') == -1:
               if stest.find('"shape":[365]') == -1:
                    testjson = json.loads(stest)
                    if 'kbdi' in testjson['attributes']['data']:
                        tempnfdr_dt = base64.b64decode(testjson['attributes']['data']['nfdr_dt']['__ndarray__'])
                        nfdr_dt = np.copy(np.frombuffer(tempnfdr_dt))
                        ytd1 = testjson['attributes']['data']
                       # print(datetime.datetime.fromtimestamp((nfdr_dt[0]/1000)+21600).strftime("%Y-%m-%d %H"))
                    elif 'BI_class' in testjson['attributes']['data']:
                        currenttime = int(datetime.datetime.now().timestamp())
                        tempnfdr_dt = base64.b64decode(testjson['attributes']['data']['nfdr_dt']['__ndarray__'])
                        nfdr_dt = np.copy(np.frombuffer(tempnfdr_dt))
                        timetest = int(datetime.datetime.fromtimestamp((nfdr_dt[3]/1000)+25200).timestamp())
                        if timetest > currenttime:
                            forecast = testjson['attributes']['data']
                        elif timetest < currenttime:
                            if j == 0:
                                ytdtest1 = nfdr_dt
                                j += 1
                            elif j == 1:
                                ytdtest2 = nfdr_dt
                                if int(datetime.datetime.fromtimestamp((ytdtest1[0]/1000)+25200).timestamp()) < int(datetime.datetime.fromtimestamp((ytdtest2[0]/1000)+21600).timestamp()):
                                    ytd2 = testjson['attributes']['data']
                                else:
                                    ytd2 = testjson['attributes']['data']
                    if 'y' in testjson['attributes']['data']:
                        temptemphumid = base64.b64decode(testjson['attributes']['data']['y']['__ndarray__'])
                        if i == 0:
                            temphumid0 = temptemphumid
                        if i == 1:
                            temphumid1 = temptemphumid
                        if i == 2:
                            temphumid2 = temptemphumid
                        i +=1
                        temptemphumidtime = base64.b64decode(testjson['attributes']['data']['x']['__ndarray__'])
                        temphumidtime = np.copy(np.frombuffer(temptemphumidtime))
        t=t[test2+1:]

    
    if temphumid0 == temphumid1:
        humid = np.copy(np.frombuffer(temphumid0))
        temp = np.copy(np.frombuffer(temphumid2))
    elif temphumid0 == temphumid2:
        humid = np.copy(np.frombuffer(temphumid0))
        temp = np.copy(np.frombuffer(temphumid1))
    else:
        temp = np.copy(np.frombuffer(temphumid0))
        humid = np.copy(np.frombuffer(temphumid1))
    index = []
    i=0
    while i < len(temphumidtime):
        if int(datetime.datetime.fromtimestamp((temphumidtime[i]/1000)+25200).strftime("%H")) == 0: 
            index = index + [i]
        i += 1
    i=1
    j=0
    tmax = []
    rhmax = []
    rhmin = []
    temphumiddate = []
    while i < len(index):
        ttmax = temp[j]
        trhmax = humid[j]
        trhmin = humid[j]
        while j < index[i]:
            if temp[j] > ttmax:
                ttmax = temp[j]
            if humid[j] > trhmax:
                trhmax = humid[j]
            if humid[j] < trhmin:
                trhmin = humid[j]
            j += 1
        tmax = tmax + [ttmax]
        rhmax = rhmax + [trhmax]
        rhmin = rhmin + [trhmin]
        temphumiddate = temphumiddate + [datetime.datetime.fromtimestamp((temphumidtime[index[i-1]]/1000)+25200).strftime("%Y%m%d")]
        i += 1


    sta_nm = ytd1['sta_nm']
    nfdr_dt = np.copy(np.frombuffer(base64.b64decode(ytd1['nfdr_dt']['__ndarray__'])))
    nfdr_tm = ytd1['nfdr_tm']
    bi = np.copy(np.frombuffer(base64.b64decode(ytd1['bi']['__ndarray__'])))
    bi_class = ytd2['BI_class']
    bip = np.copy(np.frombuffer(base64.b64decode(ytd2['BIp']['__ndarray__'])))
    bip_100 = np.copy(np.frombuffer(base64.b64decode(ytd2['BIp_100']['__ndarray__'])))
    erc = np.copy(np.frombuffer(base64.b64decode(ytd1['ec']['__ndarray__'])))
    erc_class = ytd2['ERC_class']
    ercp = np.copy(np.frombuffer(base64.b64decode(ytd2['ERCp']['__ndarray__'])))
    ercp_100 = np.copy(np.frombuffer(base64.b64decode(ytd2['ERCp_100']['__ndarray__'])))
    hr_1 = np.copy(np.frombuffer(base64.b64decode(ytd1['one_hr']['__ndarray__'])))
    hr_10 = np.copy(np.frombuffer(base64.b64decode(ytd1['ten_hr']['__ndarray__'])))
    hr_100 = np.copy(np.frombuffer(base64.b64decode(ytd1['hu_hr']['__ndarray__'])))
    hr_1000 = np.copy(np.frombuffer(base64.b64decode(ytd1['th_hr']['__ndarray__'])))
    hrb = np.copy(np.frombuffer(base64.b64decode(ytd1['hrb']['__ndarray__'])))
    hrb_gsi = ytd1['herb_gsi'] 
    ic = np.copy(np.frombuffer(base64.b64decode(ytd1['ic']['__ndarray__'])))
    ifpl = np.copy(np.frombuffer(base64.b64decode(ytd2['IFPL']['__ndarray__'])))
    ifpl_class = model = ytd2['IFPL_class']
    kbdi = ytd1['kbdi']
    model = ytd1['msgc']
    nfdr_type = ytd1['nfdr_type']
    sc = np.copy(np.frombuffer(base64.b64decode(ytd1['sc']['__ndarray__'])))
    sfdi = np.copy(np.frombuffer(base64.b64decode(ytd2['SFDI']['__ndarray__'])))
    sfdi_class = ytd2['SFDI_class']
    sfdip = np.copy(np.frombuffer(base64.b64decode(ytd2['nfdr_dt']['__ndarray__'])))
    sta_id = ytd1['sta_id']
    sta_lat = ytd1['latitude']
    sta_lon = ytd1['longitude']
    wdy = np.copy(np.frombuffer(base64.b64decode(ytd1['wdy']['__ndarray__'])))
    wdy_gsi = ytd1['woody_gsi'] 


    dbstring = DATABASES['default']
    con = psycopg2.connect(
        host=dbstring['HOST'],
        port=dbstring['PORT'],
        database=dbstring['NAME'],
        user=dbstring['USER'],
        password=dbstring['PASSWORD'])
    cur = con.cursor()
    i=0
  

    while i < len(nfdr_dt):
        nfdr_date = datetime.datetime.fromtimestamp((nfdr_dt[i]/1000)+25200).strftime("%Y%m%d")
        cur.execute("SELECT date FROM firedata WHERE date='" + nfdr_date + "' ORDER BY date")
        answer = cur.fetchone()
        if answer is None:
            answer = ['']
        if answer[0] == nfdr_date:
            i = len(nfdr_dt) + 1
        else:
            if i <= len(temphumiddate):
                j=0
                while j < len(temphumiddate):                
                    if temphumiddate[j] == nfdr_date:
                        cur.execute("INSERT INTO firedata (station, date, time, model, hr1, hr10, hr100, hr1000, hrb, wdy, ic, sc, ec, bi, type, temp, rh, wdir, wspd, tmax, tmin, rhmax, rhmin, pdur, pamt, kbdi) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",(sta_nm[i],nfdr_date,int(nfdr_tm[i]),model[i],int(round(hr_1[i]+.01)),int(round(hr_10[i]+.01)),int(round(hr_100[i]+.01)),int(round(hr_1000[i]+.01)),int(round(hrb[i]+.01)),int(round(wdy[i]+.01)),int(round(ic[i]+.01)),int(round(sc[i]+.01)),int(round(erc[i]+.01)),int(round(bi[i]+.01)),nfdr_type[i],int(0),int(0),int(0),int(0),int(tmax[j]),int(0),int(rhmax[j]),int(rhmin[j]),int(0),float(0.0),int(kbdi[i])))
                        con.commit()
                    j+=1
                i+=1
            else:
                #cur.execute("INSERT INTO firedata (station, date, time, model, hr1, hr10, hr100, hr1000, hrb, wdy, ic, sc, ec, bi, type, temp, rh, wdir, wspd, tmax, tmin, rhmax, rhmin, pdur, pamt) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",(sta_nm[i],nfdr_date,int(nfdr_tm[i]),model[i],int(hr_1[i]),int(hr_10[i]),int(hr_100[i]),int(hr_1000[i]),int(hrb[i]),int(wdy[i]),int(ic[i]),int(sc[i]),int(erc[i]),int(bi[i]),nfdr_type[i],int(0),int(0),int(0),int(0),int(tmax[i]),int(0),int(rhmax[i]),int(rhmin[i]),int(0),float(0.0)))

                cur.execute("INSERT INTO firedata (station, date, time, model, hr1, hr10, hr100, hr1000, hrb, wdy, ic, sc, ec, bi, type, temp, rh, wdir, wspd, tmax, tmin, rhmax, rhmin, pdur, pamt, kbdi) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",(sta_nm[i],nfdr_date,int(nfdr_tm[i]),model[i],int(round(hr_1[i]+.01)),int(round(hr_10[i]+.01)),int(round(hr_100[i]+.01)),int(round(hr_1000[i]+.01)),int(round(hrb[i]+.01)),int(round(wdy[i]+.01)),int(round(ic[i]+.01)),int(round(sc[i]+.01)),int(round(erc[i]+.01)),int(round(bi[i]+.01)),nfdr_type[i],int(0),int(0),int(0),int(0),int(0),int(0),int(0),int(0),int(0),float(0.0),int(kbdi[i])))
                con.commit()
                i+=1
    con.close()


def main():
    fetch()
