# -*- coding: utf-8 -*-
"""
Created on Mon Jun  5 10:36:42 2023

@author: janus
"""
import xarray as xr
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import numpy as np
from matplotlib.colors import Normalize
import glob
import pickle

#[nyT31,nyT22,lat,lon,sst,SIC,era5sic]

with open('RTMkorrigeretDecember.pkl', 'rb') as f:
    RTMDecember = pickle.load(f)

with open('RTMKorrigeretJuni.pkl', 'rb') as f:
    RTMJuni = pickle.load(f)

#%% Tiepoints beregning
decnordis1 = []
decnordis2 = []
decnordvand1 = []
decnordvand2 = []
decsydis1 = []
decsydis2 = []
decsydvand1 = []
decsydvand2 = []

junnordis1 = []
junnordis2 = []
junnordvand1 = []
junnordvand2 = []
junsydis1 = []
junsydis2 = []
junsydvand1 = []
junsydvand2 = []

#[2=31ghz, 1=22ghz]

for x in range (len(RTMDecember)):
    if RTMDecember[x,4] < 278:
        if RTMDecember[x,2] > 0:
            if RTMDecember[x,5] > 0.9:
                if RTMDecember[x,1] > 100:
                    decnordis1.append(RTMDecember[x,1])
                if RTMDecember[x,0] > 100:
                    decnordis2.append(RTMDecember[x,0])
            elif RTMDecember[x,5] < 0.3:
                if RTMDecember[x,1] > 100:
                    decnordvand1.append(RTMDecember[x,1])
                if RTMDecember[x,0] > 100:
                    decnordvand2.append(RTMDecember[x,0])
        elif RTMDecember[x,2] < 0:
            if RTMDecember[x,5] > 0.9:
                if RTMDecember[x,1] > 100:
                    decsydis1.append(RTMDecember[x,1])
                if RTMDecember[x,0] > 100:
                    decsydis2.append(RTMDecember[x,0])
            elif RTMDecember[x,5] < 0.3:
                if RTMDecember[x,1] > 100:
                    decsydvand1.append(RTMDecember[x,1])
                if RTMDecember[x,0] > 100:
                    decsydvand2.append(RTMDecember[x,0])

for x in range (len(RTMJuni)):
    if RTMJuni[x,4] < 278:
        if RTMJuni[x,2] > 0:
            if RTMJuni[x,5] > 0.9:
                if RTMJuni[x,1] > 100:
                    junnordis1.append(RTMJuni[x,1])
                if RTMJuni[x,0] > 100:
                    junnordis2.append(RTMJuni[x,0])
            elif RTMJuni[x,5] < 0.3:
                if RTMJuni[x,1] > 100:
                    junnordvand1.append(RTMJuni[x,1])
                if RTMJuni[x,0] > 100:
                    junnordvand2.append(RTMJuni[x,0])
        elif RTMJuni[x,2] < 0:
            if RTMJuni[x,5] > 0.9:
                if RTMJuni[x,1] > 100:
                    junsydis1.append(RTMJuni[x,1])
                if RTMJuni[x,0] > 100:
                    junsydis2.append(RTMJuni[x,0])
            elif RTMJuni[x,5] < 0.3:
                if RTMJuni[x,1] > 100:
                    junsydvand1.append(RTMJuni[x,1])
                if RTMJuni[x,0] > 100:
                    junsydvand2.append(RTMJuni[x,0])

stddecnordvand1 = np.std(decnordvand1)
stddecnordvand2 = np.std(decnordvand2)
stddecsydvand1 = np.std(decsydvand1)
stddecsydvand2 = np.std(decsydvand2)
stddecnordis1 = np.std(decnordis1)
stddecnordis2 = np.std(decnordis2)
stddecsydis1 = np.std(decsydis1)
stddecsydis2 = np.std(decsydis2)

stdjunnordvand1 = np.std(junnordvand1)
stdjunnordvand2 = np.std(junnordvand2)
stdjunsydvand1 = np.std(junsydvand1)
stdjunsydvand2 = np.std(junsydvand2)
stdjunnordis1 = np.std(junnordis1)
stdjunnordis2 = np.std(junnordis2)
stdjunsydis1 = np.std(junsydis1)
stdjunsydis2 = np.std(junsydis2)

TPdecnordis1 = np.mean(decnordis1)
TPdecnordis2 = np.mean(decnordis2)
TPdecnordvand1 = np.mean(decnordvand1)
TPdecnordvand2 = np.mean(decnordvand2)
TPdecsydis1 = np.mean(decsydis1)
TPdecsydis2 = np.mean(decsydis2)
TPdecsydvand1 = np.mean(decsydvand1)
TPdecsydvand2 = np.mean(decsydvand2)

TPjunnordis1 = np.mean(junnordis1)
TPjunnordis2 = np.mean(junnordis2)
TPjunnordvand1 = np.mean(junnordvand1)
TPjunnordvand2 = np.mean(junnordvand2)
TPjunsydis1 = np.mean(junsydis1)
TPjunsydis2 = np.mean(junsydis2)
TPjunsydvand1 = np.mean(junsydvand1)
TPjunsydvand2 = np.mean(junsydvand2)

#%% 1chn siconc
#[nyT31,nyT22,lat,lon,sst,SIC,era5sic]
#[2=31ghz, 1=22ghz]

DecSiconc31n = []
DecSiconc31s = []
DecSiconc22n = []
DecSiconc22s = []

for x in range(len(RTMDecember)):
    lat = RTMDecember[x,2]
    lon = RTMDecember[x,3]
    T31 = RTMDecember[x,0]
    T22 = RTMDecember[x,1]

    if RTMDecember[x,4] < 278:
        if RTMDecember[x,2] > 0:
            if RTMDecember[x,0] > 100:
                sic1 = (T31-TPdecnordvand2)/(TPdecnordis2-TPdecnordvand2)
                DecSiconc31n.append([lat,lon,sic1,T31,T22])
            if RTMDecember[x,1] > 100:
                sic2 = (T31-TPdecnordvand1)/(TPdecnordis1-TPdecnordvand1)
                DecSiconc22n.append([lat,lon,sic2,T31,T22])
        elif RTMDecember[x,2] < 0:
            if RTMDecember[x,0] > 100:
                sic1 = (T31-TPdecsydvand2)/(TPdecsydis2-TPdecsydvand2)
                DecSiconc31s.append([lat,lon,sic1,T31,T22])
            if RTMDecember[x,1] > 100:
                sic2 = (T31-TPdecsydvand1)/(TPdecsydis1-TPdecsydvand1)
                DecSiconc22s.append([lat,lon,sic2,T31,T22])

JunSiconc31n = []
JunSiconc31s = []
JunSiconc22n = []
JunSiconc22s = []

for x in range(len(RTMJuni)):
    lat = RTMJuni[x,2]
    lon = RTMJuni[x,3]
    T31 = RTMJuni[x,0]
    T22 = RTMJuni[x,1]

    if RTMJuni[x,4] < 278:
        if RTMJuni[x,2] > 0:
            if RTMJuni[x,0] > 100:
                sic1 = (T31-TPjunnordvand2)/(TPjunnordis2-TPjunnordvand2)
                JunSiconc31n.append([lat,lon,sic1,T31,T22])
            if RTMJuni[x,1] > 100:
                sic2 = (T31-TPjunnordvand1)/(TPjunnordis1-TPjunnordvand1)
                JunSiconc22n.append([lat,lon,sic2,T31,T22])
        elif RTMJuni[x,2] < 0:
            if RTMJuni[x,0] > 100:
                sic1 = (T31-TPjunsydvand2)/(TPjunsydis2-TPjunsydvand2)
                JunSiconc31s.append([lat,lon,sic1,T31,T22])
            if RTMJuni[x,1] > 100:
                sic2 = (T31-TPjunsydvand1)/(TPjunsydis1-TPjunsydvand1)
                JunSiconc22s.append([lat,lon,sic2,T31,T22])

DecSiconc31n = np.array(DecSiconc31n)
DecSiconc22n = np.array(DecSiconc22n)
DecSiconc31s = np.array(DecSiconc31s)
DecSiconc22s = np.array(DecSiconc22s)
JunSiconc31n = np.array(JunSiconc31n)
JunSiconc22n = np.array(JunSiconc22n)
JunSiconc31s = np.array(JunSiconc31s)
JunSiconc22s = np.array(JunSiconc22s)

with open('RTM1chnDec31N.pkl', 'wb') as f:
    pickle.dump(DecSiconc31n, f)
with open('RTM1chnDec31S.pkl', 'wb') as f:
    pickle.dump(DecSiconc31s, f)
with open('RTM1chnDec22N.pkl', 'wb') as f:
    pickle.dump(DecSiconc22n, f)
with open('RTM1chnDec22S.pkl', 'wb') as f:
    pickle.dump(DecSiconc22s, f)

with open('RTM1chnJun31N.pkl', 'wb') as f:
    pickle.dump(JunSiconc31n, f)
with open('RTM1chnJun31S.pkl', 'wb') as f:
    pickle.dump(JunSiconc31s, f)
with open('RTM1chnJun22N.pkl', 'wb') as f:
    pickle.dump(JunSiconc22n, f)
with open('RTM1chnJun22S.pkl', 'wb') as f:
    pickle.dump(JunSiconc22s, f)

#%% Firstyear og Multiyear tiepoints

filterDecSiconc31n = DecSiconc31n[DecSiconc31n[:,2]>0.9]
filterDecSiconc31s = DecSiconc31s[DecSiconc31s[:,2]>0.9]
filterDecSiconc22n = DecSiconc22n[DecSiconc22n[:,2]>0.9]
filterDecSiconc22s = DecSiconc22s[DecSiconc22s[:,2]>0.9]
filterJunSiconc31n = JunSiconc31n[JunSiconc31n[:,2]>0.9]
filterJunSiconc31s = JunSiconc31s[JunSiconc31s[:,2]>0.9]
filterJunSiconc22n = JunSiconc22n[JunSiconc22n[:,2]>0.9]
filterJunSiconc22s = JunSiconc22s[JunSiconc22s[:,2]>0.9]

sortDecSiconc31n = filterDecSiconc31n[np.argsort(filterDecSiconc31n[:,2])]
sortDecSiconc31s = filterDecSiconc31s[np.argsort(filterDecSiconc31s[:,2])]
sortDecSiconc22n = filterDecSiconc22n[np.argsort(filterDecSiconc22n[:,2])]
sortDecSiconc22s = filterDecSiconc22s[np.argsort(filterDecSiconc22s[:,2])]
sortJunSiconc31n = filterJunSiconc31n[np.argsort(filterJunSiconc31n[:,2])]
sortJunSiconc31s = filterJunSiconc31s[np.argsort(filterJunSiconc31s[:,2])]
sortJunSiconc22n = filterJunSiconc22n[np.argsort(filterJunSiconc22n[:,2])]
sortJunSiconc22s = filterJunSiconc22s[np.argsort(filterJunSiconc22s[:,2])]

antalraekkerDec31n=int(len(sortDecSiconc31n)*0.2)
stdmyDec31n = np.std(sortDecSiconc31n[:antalraekkerDec31n,3])
myDec31n=np.mean(sortDecSiconc31n[:antalraekkerDec31n,3])
stdfyDec31n = np.std(sortDecSiconc31n[-antalraekkerDec31n:,3])
fyDec31n=np.mean(sortDecSiconc31n[-antalraekkerDec31n:,3])

antalraekkerDec31s=int(len(sortDecSiconc31s)*0.2)
stdmyDec31s = np.std(sortDecSiconc31s[:antalraekkerDec31s,3])
myDec31s=np.mean(sortDecSiconc31s[:antalraekkerDec31s,3])
stdfyDec31s = np.std(sortDecSiconc31s[-antalraekkerDec31s:,3])
fyDec31s=np.mean(sortDecSiconc31s[-antalraekkerDec31s:,3])

antalraekkerDec22n=int(len(sortDecSiconc22n)*0.2)
stdmyDec22n = np.std(sortDecSiconc22n[:antalraekkerDec22n,4])
myDec22n=np.mean(sortDecSiconc22n[:antalraekkerDec22n,4])
stdfyDec22n = np.std(sortDecSiconc22n[-antalraekkerDec22n:,4])
fyDec22n=np.mean(sortDecSiconc22n[-antalraekkerDec22n:,4])

antalraekkerDec22s=int(len(sortDecSiconc22s)*0.2)
stdmyDec22s = np.std(sortDecSiconc22s[:antalraekkerDec22s,4])
myDec22s=np.mean(sortDecSiconc22s[:antalraekkerDec22s,4])
stdfyDec22s = np.std(sortDecSiconc22s[-antalraekkerDec22s:,4])
fyDec22s=np.mean(sortDecSiconc22s[-antalraekkerDec22s:,4])

antalraekkerJun31n=int(len(sortJunSiconc31n)*0.2)
stdmyJun31n = np.std(sortJunSiconc31n[:antalraekkerJun31n,3])
myJun31n=np.mean(sortJunSiconc31n[:antalraekkerJun31n,3])
stdfyJun31n = np.std(sortJunSiconc31n[-antalraekkerJun31n:,3])
fyJun31n=np.mean(sortJunSiconc31n[-antalraekkerJun31n:,3])

antalraekkerJun31s=int(len(sortJunSiconc31s)*0.2)
stdmyJun31s = np.std(sortJunSiconc31s[:antalraekkerJun31s,3])
myJun31s=np.mean(sortJunSiconc31s[:antalraekkerJun31s,3])
stdfyJun31s = np.std(sortJunSiconc31s[-antalraekkerJun31s:,3])
fyJun31s=np.mean(sortJunSiconc31s[-antalraekkerJun31s:,3])

antalraekkerJun22n=int(len(sortJunSiconc22n)*0.2)
stdmyJun22n = np.std(sortJunSiconc22n[:antalraekkerJun22n,4])
myJun22n=np.mean(sortJunSiconc22n[:antalraekkerJun22n,4])
stdfyJun22n = np.std(sortJunSiconc22n[-antalraekkerJun22n:,4])
fyJun22n=np.mean(sortJunSiconc22n[-antalraekkerJun22n:,4])

antalraekkerJun22s=int(len(sortJunSiconc22s)*0.2)
stdmyJun22s = np.std(sortJunSiconc22s[:antalraekkerJun22s,4])
myJun22s=np.mean(sortJunSiconc22s[:antalraekkerJun22s,4])
stdfyJun22s = np.std(sortJunSiconc22s[-antalraekkerJun22s:,4])
fyJun22s=np.mean(sortJunSiconc22s[-antalraekkerJun22s:,4])

plt.scatter(fyDec31n,fyDec22n,color='red')
plt.scatter(myDec31n,myDec22n,color='black')
plt.scatter(TPdecnordvand2,TPdecnordvand1,color='blue')
plt.xlabel('TB 31.4 GHz [K]')
plt.ylabel('TB 22.235 GHz [K]')
plt.title("December 72 Tiepoints Nord")
plt.xlim([140, 260])
plt.ylim([140, 260])
plt.show(1)

plt.scatter(fyJun31n,fyJun22n,color='red')
plt.scatter(myJun31n,myJun22n,color='black')
plt.scatter(TPjunnordvand2,TPjunnordvand1,color='blue')
plt.xlabel('TB 31.4 GHz [K]')
plt.ylabel('TB 22.235 GHz [K]')
plt.title("Juni 73 Tiepoints Nord")
plt.xlim([140, 260])
plt.ylim([140, 260])
plt.show(2)

plt.scatter(fyDec31s,fyDec22s,color='red')
plt.scatter(myDec31s,myDec22s,color='black')
plt.scatter(TPdecsydvand2,TPdecsydvand1,color='blue')
plt.xlabel('TB 31.4 GHz [K]')
plt.ylabel('TB 22.235 GHz [K]')
plt.title("December 72 Tiepoints Syd")
plt.xlim([140, 260])
plt.ylim([140, 260])
plt.show(3)

plt.scatter(fyJun31s,fyJun22s,color='red')
plt.scatter(myJun31s,myJun22s,color='black')
plt.scatter(TPjunsydvand2,TPjunsydvand1,color='blue')
plt.xlabel('TB 31.4 GHz [K]')
plt.ylabel('TB 22.235 GHz [K]')
plt.title("Juni 73 Tiepoints Syd")
plt.xlim([140, 260])
plt.ylim([140, 260])
plt.show(4)

#%%FCOMISO
#Format for RTMDecember og RTMJuni [nyT31,nyT22,lat,lon,sst,SIC,era5sic]

RTMfcomisoDec = []
RTMfcomisoJun = []

for x in range(len(RTMDecember)):
    lat = RTMDecember[x,2]
    lon = RTMDecember[x,3]
    T31 = RTMDecember[x,0]
    T22 = RTMDecember[x,1]
    sst = RTMDecember[x,4]
    if T31 > 100 and T22 > 100 and sst < 278:
        if lat > 0:
            af = (fyDec31n - myDec31n)/(fyDec22n - myDec22n)
            bf = (myDec31n - af*myDec22n)
            qf = (T31 - TPdecnordvand2)/(T22 - TPdecnordvand1)
            wf = (TPdecnordvand2 - qf*TPdecnordvand1)
            ti18vf = (bf - wf)/(qf - af)
            siconccalc = (T22 - TPdecnordvand1)/(ti18vf - TPdecnordvand1)
            RTMfcomisoDec.append([lat,lon,siconccalc,T31,T22])
        if lat < 0:
            af = (fyDec31s - myDec31s)/(fyDec22s - myDec22s)
            bf = (myDec31s - af*myDec22s)
            qf = (T31 - TPdecsydvand2)/(T22 - TPdecsydvand1)
            wf = (TPdecsydvand2 - qf*TPdecsydvand1)
            ti18vf = (bf - wf)/(qf - af)
            siconccalc = (T22 - TPdecsydvand1)/(ti18vf - TPdecsydvand1)
            RTMfcomisoDec.append([lat,lon,siconccalc,T31,T22])

for x in range(len(RTMJuni)):
    lat = RTMJuni[x,2]
    lon = RTMJuni[x,3]
    T31 = RTMJuni[x,0]
    T22 = RTMJuni[x,1]
    sst = RTMDecember[x,4]
    if T31 > 100 and T22 > 100 and sst < 278:
        if lat > 0:
            af = (fyJun31n - myJun31n)/(fyJun22n - myJun22n)
            bf = (myJun31n - af*myJun22n)
            qf = (T31 - TPjunnordvand2)/(T22 - TPjunnordvand1)
            wf = (TPjunnordvand2 - qf*TPjunnordvand1)
            ti18vf = (bf - wf)/(qf - af)
            siconccalc = (T22 - TPjunnordvand1)/(ti18vf - TPjunnordvand1)
            RTMfcomisoJun.append([lat,lon,siconccalc,T31,T22])
        if lat < 0:
            af = (fyJun31s - myJun31s)/(fyJun22s - myJun22s)
            bf = (myJun31s - af*myJun22s)
            qf = (T31 - TPjunsydvand2)/(T22 - TPjunsydvand1)
            wf = (TPjunsydvand2 - qf*TPjunsydvand1)
            ti18vf = (bf - wf)/(qf - af)
            siconccalc = (T22 - TPjunsydvand1)/(ti18vf - TPjunsydvand1)
            RTMfcomisoJun.append([lat,lon,siconccalc,T31,T22])

RTMfcomisoDec = np.array(RTMfcomisoDec)
RTMfcomisoJun = np.array(RTMfcomisoJun)

with open('RTMfcomisoDec.pkl', 'wb') as f:
    pickle.dump(RTMfcomisoDec, f)
with open('RTMfcomisoJun.pkl', 'wb') as f:
    pickle.dump(RTMfcomisoJun, f)

#%% Usikkerhed singlechannel siconc

# stddecnordvand1
# stddecnordvand2
# stddecsydvand1
# stddecsydvand2
# stdjunnordvand1
# stdjunnordvand2
# stdjunsydvand1
# stdjunsydvand2

#TPdecnordis1
#TPdecnordis2
#TPdecnordvand1
#TPdecnordvand2
#TPdecsydis1
#TPdecsydis2
#TPdecsydvand1
#TPdecsydvand2

#TPjunnordis1
#TPjunnordis2
#TPjunnordvand1
#TPjunnordvand2
#TPjunsydis1
#TPjunsydis2
#TPjunsydvand1
#TPjunsydvand2

#DecSiconc31n
#DecSiconc22n
#DecSiconc31s
#DecSiconc22s
#JunSiconc31n
#JunSiconc22n
#JunSiconc31s
#JunSiconc22s

decTB31n = []
decTB22n = []
decTB31s = []
decTB22s = []

for x in range(len(RTMDecember)):
    if RTMDecember[x,2] > 0:
        decTB31n.append(RTMDecember[x,0])
        decTB22n.append(RTMDecember[x,1])
    if RTMDecember[x,2] < 0:
        decTB31s.append(RTMDecember[x,0])
        decTB22s.append(RTMDecember[x,1])

decstd31n = np.nanstd(decTB31n)
decstd22n = np.nanstd(decTB22n)
decstd31s = np.nanstd(decTB31s)
decstd22s = np.nanstd(decTB22s)

eps = 0.8

c = []
start_value = 0
end_value = 100
step_size = 0.01

current_value = start_value
while current_value <= end_value:
    c.append(current_value)
    current_value += step_size

DCdecn22 = []
DCdecn31 = []
DCdecs22 = []
DCdecs31 = []

for x in range(len(c)):
    C = c[x]
    diffTB = 1/((TPdecnordis1)-(TPdecnordvand1))
    diffTBw = (-(1-C))/(TPdecnordis1-TPdecnordvand1)
    diffeps = -(C*TPdecnordis1/eps)/(TPdecnordis1-TPdecnordvand1)
    diffTBi = -(C*eps)/(TPdecnordis1-TPdecnordvand1)

    dC = ((diffTB*decstd22n)**2+(diffTBw*stddecnordvand1)**2+((diffTBi)*stddecnordis1)**2)**(1/2)
    DCdecn22.append([C,dC])

DCdecn22 = np.array(DCdecn22)

for x in range(len(c)):
    C = c[x]
    diffTB = 1/((TPdecnordis2)-(TPdecnordvand2))
    diffTBw = (-(1-C))/(TPdecnordis2-TPdecnordvand2)
    diffeps = -(C*TPdecnordis2)/(TPdecnordis2-TPdecnordvand2)
    diffTBi = -(C*eps)/(TPdecnordis2-TPdecnordvand2)

    dC = ((diffTB*decstd31n)**2+(diffTBw*stddecnordvand2)**2+((diffTBi)*stddecnordis2)**2)**(1/2)
    DCdecn31.append([C,dC])

DCdecn31 = np.array(DCdecn31)

for x in range(len(c)):
    C = c[x]
    diffTB = 1/((TPdecsydis1)-(TPdecsydvand1))
    diffTBw = (-(1-C))/(TPdecsydis1-TPdecsydvand1)
    diffeps = -(C*TPdecsydis1)/(TPdecsydis1-TPdecsydvand1)
    diffTBi = -(C*eps)/(TPdecsydis1-TPdecsydvand1)

    dC = ((diffTB*decstd22s)**2+(diffTBw*stddecsydvand1)**2+((diffTBi)*stddecsydis1)**2)**(1/2)
    DCdecs22.append([C,dC])

DCdecs22 = np.array(DCdecs22)

for x in range(len(c)):
    C = c[x]
    diffTB = 1/((TPdecsydis2)-(TPdecsydvand2))
    diffTBw = (-(1-C))/(TPdecsydis2-TPdecsydvand2)
    diffeps = -(C*TPdecsydis2)/(TPdecsydis2-TPdecsydvand2)
    diffTBi = -(C*eps)/(TPdecsydis2-TPdecsydvand2)

    dC = ((diffTB*decstd31s)**2+(diffTBw*stddecsydvand2)**2+((diffTBi)*stddecsydis2)**2)**(1/2)
    DCdecs31.append([C,dC])

DCdecs31 = np.array(DCdecs31)

sizes=np.ones(len(DCdecn22))*0.1

plt.plot(DCdecn22[:,0],DCdecn22[:,1],color='blue')
plt.plot(DCdecn31[:,0],DCdecn31[:,1],color='green')
plt.plot(DCdecs22[:,0],DCdecs22[:,1],color='blue',linestyle='--')
plt.plot(DCdecs31[:,0],DCdecs31[:,1],color='green',linestyle='--')
plt.title("Usikkerhed for december")
plt.xlabel('SIC')
plt.ylabel('Usikkerhed i iskoncentration')
plt.legend(["Nordlige data 22GHz","Nordlige data 31GHz","Sydlige data 22GHz","Sydlige data 31GHz"])
plt.show(1)

junTB31n = []
junTB22n = []
junTB31s = []
junTB22s = []

for x in range(len(RTMJuni)):
    if RTMJuni[x,2] > 0:
        junTB31n.append(RTMJuni[x,0])
        junTB22n.append(RTMJuni[x,1])
    if RTMJuni[x,2] < 0:
        junTB31s.append(RTMJuni[x,0])
        junTB22s.append(RTMJuni[x,1])

junstd31n = np.nanstd(junTB31n)
junstd22n = np.nanstd(junTB22n)
junstd31s = np.nanstd(junTB31s)
junstd22s = np.nanstd(junTB22s)

eps = 0.8

c = []
start_value = 0
end_value = 100
step_size = 0.01

current_value = start_value
while current_value <= end_value:
    c.append(current_value)
    current_value += step_size

DCjunn22 = []
DCjunn31 = []
DCjuns22 = []
DCjuns31 = []

for x in range(len(c)):
    C = c[x]
    diffTB = 1/((TPjunnordis1)-(TPjunnordvand1))
    diffTBw = (-(1-C))/(TPjunnordis1-TPjunnordvand1)
    diffeps = -(C*TPjunnordis1)/(TPjunnordis1-TPjunnordvand1)
    diffTBi = -(C*eps)/(TPjunnordis1-TPjunnordvand1)

    dC = ((diffTB*junstd22n)**2+(diffTBw*stdjunnordvand1)**2+((diffTBi)*stdjunnordis1)**2)**(1/2)
    DCjunn22.append([C,dC])

DCjunn22 = np.array(DCjunn22)

for x in range(len(c)):
    C = c[x]
    diffTB = 1/((TPjunnordis2)-(TPjunnordvand2))
    diffTBw = (-(1-C))/(TPjunnordis2-TPjunnordvand2)
    diffeps = -(C*TPjunnordis2)/(TPjunnordis2-TPjunnordvand2)
    diffTBi = -(C*eps)/(TPjunnordis2-TPjunnordvand2)

    dC = ((diffTB*junstd31n)**2+(diffTBw*stdjunnordvand2)**2+((diffTBi)*stdjunnordis2)**2)**(1/2)
    DCjunn31.append([C,dC])

DCjunn31 = np.array(DCjunn31)

for x in range(len(c)):
    C = c[x]
    diffTB = 1/((TPjunsydis1)-(TPjunsydvand1))
    diffTBw = (-(1-C))/(TPjunsydis1-TPjunsydvand1)
    diffeps = -(C*TPjunsydis1)/(TPjunsydis1-TPjunsydvand1)
    diffTBi = -(C*eps)/(TPjunsydis1-TPjunsydvand1)

    dC = ((diffTB*junstd22s)**2+(diffTBw*stdjunsydvand1)**2+((diffTBi)*stdjunsydis1)**2)**(1/2)
    DCjuns22.append([C,dC])

DCjuns22 = np.array(DCjuns22)

for x in range(len(c)):
    C = c[x]
    diffTB = 1/((TPjunsydis2)-(TPjunsydvand2))
    diffTBw = (-(1-C))/(TPjunsydis2-TPjunsydvand2)
    diffeps = -(C*TPjunsydis2)/(TPjunsydis2-TPjunsydvand2)
    diffTBi = -(C*eps)/(TPjunsydis2-TPjunsydvand2)

    dC = ((diffTB*junstd31s)**2+(diffTBw*stdjunsydvand2)**2+((diffTBi)*stdjunsydis2)**2)**(1/2)
    DCjuns31.append([C,dC])

DCjuns31 = np.array(DCjuns31)

sizes=np.ones(len(DCjunn22))*0.1

plt.plot(DCjunn22[:,0],DCjunn22[:,1],color='blue')
plt.plot(DCjunn31[:,0],DCjunn31[:,1],color='green')
plt.plot(DCjuns22[:,0],DCjuns22[:,1],color='blue',linestyle='--')
plt.plot(DCjuns31[:,0],DCjuns31[:,1],color='green',linestyle='--')
plt.title("Usikkerhed for juni")
plt.xlabel('SIC')
plt.ylabel('Usikkerhed i iskoncentration')
plt.legend(["Nordlige data 22GHz","Nordlige data 31GHz","Sydlige data 22GHz","Sydlige data 31GHz"])
plt.show(2)

#%% Standardafvigelser på tiepoints med 2kanals usikkerhed

#lat,lon,siconccalc,T31,T22

with open('RTMfcomisoDec.pkl', 'rb') as f:
    RTMDecember = pickle.load(f)

with open('RTMfcomisoJun.pkl', 'rb') as f:
    RTMJuni = pickle.load(f)

Decemberisnord = []
Decembervandnord = []
Decemberissyd = []
Decembervandsyd = []

for x in range(len(RTMDecember)):
    sic = RTMDecember[x,2]
    lat = RTMDecember[x,0]
    if sic > 1:
        sic = 1
    if sic < 0:
        sic = 0
    if lat>0:
        if sic > 0.9:
            Decemberisnord.append(sic)
        if sic < 0.3:
            Decembervandnord.append(sic)
    if lat<0:
        if sic > 0.9:
            Decemberissyd.append(sic)
        if sic < 0.3:
            Decembervandsyd.append(sic)

stddecisnord = np.std(Decemberisnord)
stddecvandnord = np.std(Decembervandnord)
stddecissyd = np.std(Decemberissyd)
stddecvandsyd = np.std(Decembervandsyd)

Juniisnord = []
Junivandnord = []
Juniissyd = []
Junivandsyd = []

for x in range(len(RTMJuni)):
    sic = RTMJuni[x,2]
    lat = RTMJuni[x,0]
    if sic > 1:
        sic = 1
    if sic < 0:
        sic = 0
    if lat>0:
        if sic > 0.9:
            Juniisnord.append(sic)
        if sic < 0.3:
            Junivandnord.append(sic)
    if lat<0:
        if sic > 0.9:
            Juniissyd.append(sic)
        if sic < 0.3:
            Junivandsyd.append(sic)

stdjunisnord = np.std(Juniisnord)
stdjunvandnord = np.std(Junivandnord)
stdjunissyd = np.std(Juniissyd)
stdjunvandsyd = np.std(Junivandsyd)

#%%
c = []
start_value = 0
end_value = 100
step_size = 0.01

current_value = start_value
while current_value <= end_value:
    c.append(current_value)
    current_value += step_size

eice = stddecisnord
ew = stddecvandnord

Usikkerheddecnord = []

for x in range(len(c)):
    C = c[x]
    usik = np.sqrt((1-C)**2*ew**2+C**2*eice**2)
    Usikkerheddecnord.append([C,usik])

Usikkerheddecnord = np.array(Usikkerheddecnord)

eice = stddecissyd
ew = stddecvandsyd

Usikkerheddecsyd = []

for x in range(len(c)):
    C = c[x]
    usik = np.sqrt((1-C)**2*ew**2+C**2*eice**2)
    Usikkerheddecsyd.append([C,usik])

Usikkerheddecsyd = np.array(Usikkerheddecsyd)

eice = stdjunisnord
ew = stdjunvandnord

Usikkerhedjunnord = []

for x in range(len(c)):
    C = c[x]
    usik = np.sqrt((1-C)**2*ew**2+C**2*eice**2)
    Usikkerhedjunnord.append([C,usik])

Usikkerhedjunnord = np.array(Usikkerhedjunnord)

eice = stdjunissyd
ew = stdjunvandsyd

Usikkerhedjunsyd = []

for x in range(len(c)):
    C = c[x]
    usik = np.sqrt((1-C)**2*ew**2+C**2*eice**2)
    Usikkerhedjunsyd.append([C,usik])

Usikkerhedjunsyd = np.array(Usikkerhedjunsyd)

plt.plot(Usikkerheddecnord[:,0],Usikkerheddecnord[:,1],color='green')
plt.plot(Usikkerheddecsyd[:,0],Usikkerheddecsyd[:,1],color='green',linestyle='--')
plt.plot(Usikkerhedjunnord[:,0],Usikkerhedjunnord[:,1],color='blue')
plt.plot(Usikkerhedjunsyd[:,0],Usikkerhedjunsyd[:,1],color='blue',linestyle='--')
plt.title("Usikkerhed for 2-kanals iskoncentration")
plt.xlabel('Iskoncentration')
plt.ylabel('Usikkerhed i iskoncentration')
plt.legend(["December Nord","December Syd","Juni Nord","Juni Syd"])
plt.show()


from collections import OrderedDict
from .process_pointer import ProcessPointer
from corenet.node.updatable_node import UpdatableNode

from logging import getLogger, DEBUG

logger = getLogger(__name__)

class NetworkManager:
    def __init__(self):
        self.process_list = OrderedDict()
        self.pointer = ProcessPointer(self.process_list)
        self.variables = {}

    @property
    def network(self):
        return self.process_list

    def get_node(self, name):
        if name not in self.process_list:
            raise KeyError()
        return self.process_list[name]

    def add_node(self, node):
        assert node.name not in self.process_list,\
            'Duplicating label name: {}({})<{}>'.format(node.name, node, str(
                {key: str(node) for key, node in self.network.items()}))
        self.process_list[node.name] = node

    def build_network(self):
        self.pointer.sync()

    def validate_network(self):
        unreached = []
        for node in self.pointer.forward():
            unreached.append(str(node))
        return list(reversed(unreached))

    def update(self):
        updatables = []
        for node in self.pointer.forward(0):
            if not issubclass(type(node), UpdatableNode):
                continue

            if node.update_variable is None:
                node.update_before()
                continue

            if node.update_variable not in self.variables:
                unreached = self.validate_network()
                raise ValueError(
                    'Unreached loss computation.\nFollowing list is unreached nodes: \n' +
                    '\n'.join([str(n) for n in unreached])
                )
            updatables.append(node.update(self.variables))

        for _ in range(3):
            for updatable in updatables:
                next(updatable)

    def invoke(self, node):
        in_values = []
        for var in node.input:
            if var not in self.variables:
                msg = 'Unknown variable: {} at {}'.format(var, str(node))
                raise KeyError(msg)
            value = self.variables[var]
            if logger.isEnabledFor(DEBUG):
                if hasattr(value, 'shape'):
                    logger.debug('Arguments: {}, {}'.format(var, value.shape))
                else:
                    logger.debug('Arguments: {}, {}'.format(var, value))
            in_values.append(value)

        out = node(*in_values)
        if not isinstance(out, (list, tuple)):
            out = [out]

        return out

    def is_runtime(self, mode, node):
        value = node.attrs.get(mode, True)
        return value if isinstance(value, bool) else False

    def update_variables(self, node, values):
        """Update storing variables.

        Args:
            node (NetworkNode): A processed node.
            values (dict): Output variable name and value.
        """

        assert isinstance(values, (tuple, list)), \
            'Output value is not iterable; Node model: {}, Values:{}'.format(
                node.model, values)
        output = {out: value for out, value in zip(node.output, values)}
        self.variables.update(**output)

    def __call__(self, mode='train', **inputs):
        assert len(self.network) > 0, "Network node is empty."
        self.build_network()

        self.variables = {}
        self.variables.update(inputs)

        # assert all((name in inputs for name in self.input_list)) or mode == 'test', \
        #    'Input requirement is not satisfied. (Inputs: {}, Input requirement: {}])'.format(list(inputs.keys()), self.input_list)

        for node in self.pointer.forward(0):
            if not self.is_runtime(mode, node):
                continue
            try:
                output = self.invoke(node)
            except Exception:
                print('Uncaught exception occured: {}, {}'.format(
                    node.name, node.model))
                raise
            self.update_variables(node, output)


from datetime import datetime, timezone
import config

from job_queue import JobQueue

queue = JobQueue(queue_name=config.queue_name, host=config.redis_host)

message = {
    'timestamp': datetime.now(timezone.utc).isoformat(),
    'task': 'run'
}

queue.publish(message)
print('Published')


from datetime import datetime
import re
import os

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from sqlalchemy.orm import sessionmaker
from sqlalchemy import select, desc
import pandas as pd

from src.bot.database import DB, Users, Images, Dishes, \
    DatesMenu, Sprints, Orders, OrderDishes, \
    ExtraWishes, MetaInfo, SpecialPermissions, ENGINE

FOLDER = 'МАМА ДОМА. Рабочая папка. Проект.'

MIMETYPES = {
        # Drive Document files as MS dox
        'application/vnd.google-apps.document': 'text/plain',
        # Drive Sheets files as MS Excel files.
        'application/vnd.google-apps.spreadsheet': 'text/csv'
  }

gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)

def get_folder_id(folder_name, parent_folder="root"):
    if parent_folder != "root":
        parent_folder_id = get_folder_id(parent_folder)
    else:
        parent_folder_id = parent_folder

    file_list = drive.ListFile({
        'q': f"'{parent_folder_id}' in parents and trashed=false"
    }).GetList()

    for file in file_list:
        if file['title'] == folder_name:
            return file['id']

MAMA_FOLDER_ID = get_folder_id(FOLDER)

def get_data_from_google_drive():

    '''Getting data from Google Drive and adding it to database'''

    menu_folder = 'Меню'
    menu_folder_id = get_folder_id(menu_folder, FOLDER)

    def get_sheet_content(file):
        title = file['title']
        temp_file = 'dish.csv'
        download_mimetype = MIMETYPES[file['mimeType']]
        file.GetContentFile(filename=temp_file, mimetype=download_mimetype)
        if title != 'Меню на неделю':
            result = pd.read_csv(temp_file, names=columns, skiprows=1)
            num = re.search(r'\d', title)[0]
            category = pd.Series([num] * result.shape[0])
            result['category'] = category
        else:
            col = ['month', 'week_day', 'date', 'Dish_1',
                   'Dish_2', 'Dish_3', 'Dish_4', 'Dish_5']
            result = pd.read_csv(temp_file, names=col, skiprows=1)
        os.remove(temp_file)

        return result

    menu_sheets = ('1. Основное блюдо (список)',
                   '2. Гарнир (список)',
                   '3. Суп (список)',
                   '4. Салат (список)',
                   '5. Десерт (список)')

    columns = ['category', 'name', 'price', 'description',
               'calories', 'PFC', 'photo_name', 'amount']
    all_dishes = pd.DataFrame(columns=columns)

    lister = drive.ListFile({
        'q': f"'{menu_folder_id}' in parents and trashed=false"
    }).GetList()
    for file in lister:
        if file['title'] in menu_sheets:
            all_dishes = pd.concat([all_dishes, get_sheet_content(file)],
                                  ignore_index=True
                                  )

        elif file['title'] == 'Меню на неделю':
            week_menu = get_sheet_content(file)

        elif file['title'] == 'Меню фотографии':
            pass
            # lister = drive.ListFile({
            # 'q': f"'{file['id']}' in parents and trashed=false"
            # }).GetList()
            # photos = []
            # i = 0
            # for file in lister:
            #     name = re.sub(r'\.\w+', '', file['title'])
            #     title = re.sub(r'.+\.', f'{i}.', file['title'])
            #     local_filename = LOCAL_IMAGES_FOLDER + title
            #     file.GetContentFile(filename=local_filename)
            #     photos.append(name)
            #     add_img_to_DB(name, local_filename)
            #     os.remove(local_filename)
            #     i += 1

    return (all_dishes, week_menu)

def add_data_to_db(all_dishes, week_menu):

    Session = sessionmaker(bind=ENGINE)
    session = Session()

    # Adding dates to database

    dates = list(week_menu['date'])
    dates.sort()
    all_dates = ', '.join(dates)
    sprint = Sprints(first_date=datetime.strptime(dates[0], '%Y-%m-%d'),
                     last_date=datetime.strptime(dates[-1], '%Y-%m-%d'),
                     all_dates=all_dates
                     )

    session.merge(sprint)

    # Adding dishes list to database

    # all_dishes.loc[(~all_dishes['photo_name'].isin(photos)), 'photo_name'] = 'default_photo' TODO

    menu = all_dishes.apply(
        lambda x: Dishes(description=x['description'],
                         name=x['name'],
                         photo_name=x['photo_name'],
                         calories=x['calories'],
                         price=x['price'],
                         amount=x['amount'],
                         category=x['category'],
                         PFC=x['PFC']), axis=1)
    menu = list(menu)

    for item in menu:
        not_unique = session.scalars(
                     select(Dishes).where(Dishes.name == item.name)
                     ).all()
        for i in not_unique:
            session.delete(i)
            session.commit()
        session.merge(item)

    session.commit()

    # Adding week's menu to database

    dishes = session.query(Dishes).all()
    name_id = {i.name: i.idDishes for i in dishes}

    category_columns = list(week_menu.columns[3:])
    week_menu_dict = {i: week_menu[list(week_menu.columns[0:3]) + [j]]
                      for (i, j) in zip(range(1, 6), category_columns)}
    columns = list(week_menu.columns[0:3]) + ['Dish', 'category']
    dates_menu = pd.DataFrame(columns=columns)
    for i, val in week_menu_dict.items():
        val_1 = val.copy()
        val_1.rename(columns={f'Dish_{i}': 'Dish'}, inplace=True)
        val_1.insert(4, 'category', [i] * week_menu.shape[0], False)
        dates_menu = pd.concat([dates_menu, val_1])
    dates_menu['date'] = pd.to_datetime(dates_menu['date'])
    dates_menu['category'] = dates_menu['category'].astype(str)

    id_list = [name_id[i] for i in dates_menu['Dish']]
    dates_menu.rename(columns={f'Dish': 'id_dish'}, inplace=True)
    dates_menu['id_dish'] = id_list
    dates_menu_list = [DatesMenu(date=i.date, id_dish=i.id_dish,
                                 category=i.category) for i in
                       dates_menu.itertuples()]

    for item in dates_menu_list:
        not_unique = session.scalars(
                        select(DatesMenu).where(DatesMenu.date == item.date)
                            .where(
                                DatesMenu.category == item.category
                            )
                     ).all()
        for i in not_unique:
            session.delete(i)
            session.commit()
        session.merge(item)

    session.commit()
    session.close()

def download_menu():
    all_dishes, week_menu = get_data_from_google_drive()
    add_data_to_db(all_dishes, week_menu)

def upload(user_info, file_name):
        data_list = []
        for item in user_info:
            user_info_dict = item.__dict__
            user_info_dict.pop('_sa_instance_state', None)
            data_list.append(user_info_dict)

        lister = drive.ListFile({'q': f"'{MAMA_FOLDER_ID}'"
                                      f"in parents and "
                                      f"trashed=false"}).GetList()
        for file in lister:
            if file['title'] == file_name:
                file.Trash()
                break

        result = pd.DataFrame(data_list)

        my_file = drive.CreateFile({'title': file_name, 'mimeType':
            'text/csv', 'parents': [{'id': MAMA_FOLDER_ID}]})
        my_file.SetContentString(result.to_csv())
        my_file.Upload({'convert': True})

def upload_clients():
    Session = sessionmaker(bind=ENGINE)
    session = Session()

    user_info = session.scalars(select(Users)).all()

    session.close()

    if user_info:
        upload(user_info, 'База клиентов')
    else:
        return None

def upload_orders():
    Session = sessionmaker(bind=ENGINE)
    session = Session()

    last_sprint = session.query(Sprints).order_by(desc(
        Sprints.first_date)).first().first_date
    user_info = (
        session.query(Users.username_tg, Orders.Comment, Orders.order_date,)
        .join(Employee.department)
        .join(Address, Employee.id == Address.employee_id)
        .filter(Department.name == 'HR')
        .scalars()
        .all()
    )

    session.close()

    if user_info:
        upload(user_info, 'Актуальные заказы')
    else:
        return None

if __name__ == '__main__':
    upload()


"""IG: few.pz"""

def main():
    """ Main function """
    amount = int(input())
    int(input())
    value = 0
    value_with_power_2 = 0
    list_score = []
    for _ in range(amount):
        score_student = int(input())
        value += score_student
        value_with_power_2 += (score_student**2)
        list_score.append(score_student)
    standard_deviation = (
        ((amount*(value_with_power_2))-(value**2))/(amount*(amount-1)))**0.5
    sum_x = value/amount
    for i in list_score:
        sum_z = (int(i)-sum_x) / standard_deviation
        print('%.2f' % (50 + 10*(sum_z)))

main()


import sys, os, cv2, tqdm, pathlib
from fp import pipe, cmap, cfilter, flatten
import itertools
import utils

utils.help_option('''
img_cutter
  copy_tree structure of 'src_path' to 'dst_path'
  then save square crops of images in 'src_path'

[synopsys]
  python img_cutter.py crop_size src_path dst_path
ex)
  python img_cutter.py 256 ./src ./dst
''')

def hw2start_yxs(origin_yx, img_hw, piece_hw):
    org_y,org_x = origin_yx
    img_h,img_w = img_hw
    piece_h,piece_w = piece_hw
    for y in range(org_y, img_h, piece_h):
        for x in range(org_x, img_w, piece_w):
            yield (y,x)

def hw2not_excess_start_yxs(origin_yx, img_hw, piece_hw):
    img_h,img_w = img_hw
    piece_h,piece_w = piece_hw
    def not_excess(yx):
        y,x = yx
        return (y + piece_h < img_h) and (x + piece_w < img_w)
    return filter(not_excess,
                  hw2start_yxs(origin_yx, img_hw, piece_hw))

def safe_path2img(path):
    ret = cv2.imread(path)
    print(path,type(ret))
    return ret

def gen_name_piece_pairs(img, piece_size):
    img_hw = img.shape[:2]
    h = w = piece_size
    for y,x in hw2not_excess_start_yxs( (0,0), img_hw, (h,w) ):
        yield 'piece_%d_%d.png' % (y,x), img[y:y+h,x:x+w]
    for y,x in hw2not_excess_start_yxs( (h//2,w//2), img_hw, (h,w) ):
        yield 'piece_%d_%d.png' % (y,x), img[y:y+h,x:x+w]

def path_img2path_pieces(path_img, piece_size, imgs_dir, pieces_dir):
    global imgno
    path, img = path_img
    ext_removed = os.path.splitext(path)[0]

    parts = pathlib.Path(ext_removed).parts
    old_parent, img_name = parts[0], parts[-1]
    new_path = utils.make_dstpath(ext_removed, old_parent, pieces_dir)

    img_hw = img.shape[:2]
    h = w = piece_size
    for r_y,r_x in sorted(itertools.product([0, 0.5], [0, 0.5])):
        org_yx = int(h*r_y),int(w*r_x)
        for y,x in hw2not_excess_start_yxs( org_yx, img_hw, (h,w) ):
            #print(y,x)
            #cv2.imshow('img',img[y:y+h, x:x+w]); cv2.waitKey(0)
            tail = '_%d_%d.png' % (y,x)
            yield new_path+tail, img[y:y+h, x:x+w]

    '''
    parts = pathlib.Path(ext_removed).parts
    old_parent, img_name = parts[0], parts[-1]
    new_path = utils.make_dstpath(ext_removed, old_parent, pieces_dir)

    img_hw = img.shape[:2]
    h = w = piece_size
    for r_y,r_x in sorted(itertools.product([0, 0.5], [0, 0.5])):
        org_yx = int(h*r_y),int(w*r_x)
        for y,x in hw2not_excess_start_yxs( org_yx, img_hw, (h,w) ):
            #print(y,x)
            #cv2.imshow('img',img[y:y+h, x:x+w]); cv2.waitKey(0)
            yield '%s_%d_%d.png' % (img_name,y,x), img[y:y+h, x:x+w]
    '''

if __name__ == '__main__':
    def path2path_img(path):
        return (path, cv2.imread(path))
    crop_size = int(sys.argv[1])
    imgs_dir = sys.argv[2]
    pieces_dir = sys.argv[3]

    utils.safe_copytree(imgs_dir,pieces_dir,
                        ['*.jpg', '*.jpeg', '*.png'])

    timer = utils.ElapsedTimer('Total Cutting')
    #-------------------------------------------------------------
    pieces \
    = pipe(utils.file_paths,
           cmap(path2path_img),
           cfilter(lambda path_img:path_img[1] is not None),
           cmap(lambda pair: path_img2path_pieces(pair,crop_size,imgs_dir,pieces_dir)),
           flatten)(imgs_dir)

    for path,img in pieces:
        #print(path)
        #print(path)
        #cv2.imwrite(path, img)
        cv2.imwrite(path, img)
        pass
    #-------------------------------------------------------------
    timer.elapsed_time()

import unittest
class hw2start_yxsTest(unittest.TestCase):
    def test_mod0cases(self):
        self.assertEqual(list(hw2start_yxs((0,0),(100,60),(20,20))),
                         [( 0,0),( 0,20),( 0,40),
                          (20,0),(20,20),(20,40),
                          (40,0),(40,20),(40,40),
                          (60,0),(60,20),(60,40),
                          (80,0),(80,20),(80,40),])
    def test_not_mod0cases(self):
        self.assertEqual(list(hw2start_yxs((0,0),(90,49),(20,20))),
                         [( 0,0),( 0,20),( 0,40),
                          (20,0),(20,20),(20,40),
                          (40,0),(40,20),(40,40),
                          (60,0),(60,20),(60,40),
                          (80,0),(80,20),(80,40),])
    def test_not_excess(self):
        self.assertEqual(list(hw2not_excess_start_yxs(( 0, 0),
                                                      (91,49),
                                                      (20,20))),
                         [( 0,0),( 0,20),
                          (20,0),(20,20),
                          (40,0),(40,20),
                          (60,0),(60,20),])
    def test_origin_specified(self):
        self.assertEqual(list(hw2start_yxs((40,20),
                                           (90,49),
                                           (20,20))),
                         [(40,20),(40,40),
                          (60,20),(60,40),
                          (80,20),(80,40),])

        self.assertEqual(list(hw2start_yxs((30,10),
                                           (90,49),
                                           (20,20))),
                         [(30,10),(30,30),
                          (50,10),(50,30),
                          (70,10),(70,30)])
        self.assertEqual(list(hw2not_excess_start_yxs((40,20),
                                                      (91,49),
                                                      (20,20))),
                         [(40,20),(60,20)])


from PyQt5.QtWidgets import QVBoxLayout, QPushButton

class Tools(QVBoxLayout):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.button1 = QPushButton('Open \nImage')
        self.button2 = QPushButton('Open \nFolder')
        self.button3 = QPushButton('Save')
        self.button4 = QPushButton('Undo')

        # Set the size of the buttons
        self.button1.setFixedSize(80, 80)
        self.button2.setFixedSize(80, 80)
        self.button3.setFixedSize(80, 80)
        self.button4.setFixedSize(80, 80)

        # buttonLayout = QVBoxLayout()
        self.addWidget(self.button1)
        self.addWidget(self.button2)
        self.addWidget(self.button3)
        self.addWidget(self.button4)
        self.addStretch()
        self.setSpacing(0)

        # self.setAlignment(Qt.AlignmentFlag.AlignHCenter)


import argparse
import json
import brokjson
from pathlib import Path

# Convert GeoJSON to BrokJSON (single file)
def toBrok(infile, outfile=None):
    # Specifie outfile if not given and create directory
    if not outfile:
        if args.overwrite == 1:
            outfile = infile
        else:
            outfile = Path(infile.parents[0] / 'brokjson' / infile.name)

    outfile.parents[0].mkdir(exist_ok=True)

    # Read geojson object from file and create brokjson object
    geo = json.loads(open(infile, 'r').read())
    brok = brokjson.geo2brok(geo)

    # Write file
    with open(outfile, 'w') as file:
        file.write(json.dumps(brok))

# Convert BrokJSON to GeoJSON (single file)
def toGeo(infile, outfile=None):
    # Specifie outfile if not given and create directory
    if not outfile:
        if args.overwrite == 1:
            outfile = infile
        else:
            outfile = Path(infile.parents[0] / 'geojson' / infile.name)

    outfile.parents[0].mkdir(exist_ok=True)

    # Read brokjson object from file and create geojson object
    brok = json.loads(open(infile, 'r').read())
    geo = brokjson.brok2geo(brok)

    # Write file
    with open(outfile, 'w') as file:
        file.write(json.dumps(geo))

# Convert GeoJSON to BrokJSON (directory)
def toBrokMass(indir, outdir=None):
    # Specifie output directory if not given and create it
    if not outdir:
        if args.overwrite == 1:
            outdir = indir
        else:
            outdir = Path(indir / 'brokjson')

    outdir.mkdir(parents=True, exist_ok=True)

    # Load all json files
    infiles = indir.glob('*.json')
    for file in infiles:
        # Read geojson object from file and create brokjson object
        geo = json.loads(open(file, 'r').read())
        brok = brokjson.geo2brok(geo)

        # Write file
        with open((outdir / file.name), 'w') as writefile:
            writefile.write(json.dumps(brok))

# Convert BrokJSON to GeoJSON (directory)
def toGeoMass(indir, outdir=None):
    # Specifie output directory if not given and create it
    if not outdir:
        if args.overwrite == 1:
            outdir = indir
        else:
            outdir = Path(indir / 'geojson')

    outdir.mkdir(parents=True, exist_ok=True)

    # Load all json files
    infiles = indir.glob('*.json')
    for file in infiles:
        # Read brokjson object from file and create geojson object
        brok = json.loads(open(file, 'r').read())
        geo = brokjson.brok2geo(brok)

        # Write file
        with open((outdir / file.name), 'w') as writefile:
            writefile.write(json.dumps(geo))

if __name__ == '__main__':
    # Argument parser
    parser = argparse.ArgumentParser(description='Convert GeoJSON files to BrokJSON files and vice versa from command line')
    parser.add_argument('function', choices=['geo2brok', 'brok2geo'], help='Direction of conversion: geo2brok = GeoJSON to BrokJSON, brok2geo = BrokJSON to GeoJSON')
    parser.add_argument('-o', '--overwrite', dest='overwrite', action='store_const', const=1, default=0, help='Overwrite already existing files, even if no export directory is specified')
    parser.add_argument('inpath', metavar='I', type=str, nargs=1, help='GeoJSON or BrokJSON file or directory to convert')
    parser.add_argument('outpath', metavar='O', type=str, nargs='*', help='Converted file or output directory')
    args = parser.parse_args()

    # File path
    inpath = Path(args.inpath[0])
    outpath = Path()
    try:
	    outpath = Path(args.outpath[0])
    except IndexError:
	    outpath = None

    # Select function
    if args.function == 'geo2brok':
        if inpath.is_dir():
            toBrokMass(inpath, outpath)
        else:
            toBrok(inpath, outpath)

    if args.function == 'brok2geo':
        if inpath.is_dir():
            toGeoMass(inpath, outpath)
        else:
            toGeo(inpath, outpath)


import boto3
from boto3.session import Session

client = boto3.client('sts')

response = client.assume_role(RoleArn=arn, RoleSessionName=session_name)
session = Session(aws_access_key_id=response['Credentials']['AccessKeyId'],
                      aws_secret_access_key=response['Credentials']['SecretAccessKey'],
                      aws_session_token=response['Credentials']['SessionToken'])
s3_client = session.client('sts')


import socket
import  threading

#for local host
Host='127.0.0.1'
# the port_no is optional you can choose any port_no but be free!
Port_no=45687
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind((Host,Port_no))
#here we put the server on listening mode for listening the connection.
server.listen()
# if new client is joining the put into the client list and the name list used for the name of the client.
clients=[]
name=[]
# this function is broadcast function this function send the messege to all the client which is connected to this server.
def broadcast(msg):
    for client in clients:
        client.send(msg)
# here function is broadcast the messege to all the client that is connected to the server.
def handle_client(client):
    while 1:
        try:
            message=client.recv(1024) #1024 is file size it can be change by your need.
            broadcast(msg)
        except:
            index=clints.index(client)# in this block remove the client from the serverif any error occur and send there name of client which can be remove.
            clients.remove(client)
            client.close()
            name_=name[index]
            name.remove(name_)
            broadcast(f'{name_}is left'.encode('ascii'))
            break
# this is the main method recieve method to combine and the messege all all client.
def receive():
    while 1:
        #here the client and address of the client.
        client,address=server.accept()
        print(f'connected with this address{str(address)}')
        # here ask the name of the client.
        client.send('what is your name'.encode('ascii'))
        name_=client,recv(1024).decode('ascii')
        name.append(name_)
        clients.append(client)
        # send the message to the all chat this name has join the chat.
        print(f'Name of the client is {name_}.')
        broadcast(f'{name_}join the chat.'.encode('ascii'))
        client.send('you are join chat Room'.encode('ascii'))

        thread=threading.Thread(target=handle_client,args=(client,))
        thread.start()
print("server is listening....")
receive()


from .connection import cur
from utils.static_messages import FETCH_QUERY_FAILED, RECORD_INSERTED_SUCCESSFULLY, UNABLE_TO_INSERT_RECORD

def fetch_record(table, identifier_type, identifier_value):
    try:
        SQL = f'SELECT {identifier_type} FROM {table} where {identifier_type} = {identifier_value}'
        cur.execute(SQL)
        res = cur.fetchone()
        return {'err': False, 'msg': res[0] if res else None}
    except Exception:
        return {'err': True, 'msg': FETCH_QUERY_FAILED}

def insert_to_patients_table(value: int):
    try:
        SQL = 'INSERT INTO patients_details(patient_id) VALUES(%s)'
        data = value
        cur.execute(SQL, (data,))
        return {'err': False, 'msg': RECORD_INSERTED_SUCCESSFULLY}
    except Exception:
        return {'err': True, 'msg': UNABLE_TO_INSERT_RECORD}


# -*- codeing -*-
# @Time : 2021/11/27 15:37
# @Author : Torres-圣君
# @File : timing.py
# @Sofaware : PyCharm
import datetime
# import os
import subprocess
import time
import tkinter as tk
from tkinter import messagebox

class TimingTime:
    def __init__(self):
        self.Timing_custom_flag = True
        self.count = 1
        self.year = int(time.strftime('%Y'))    # 获取：年
        self.month = int(time.strftime('%m'))   # 获取：月
        self.day = int(time.strftime('%d'))     # 获取：日
        self.hour = int(time.strftime('%H'))    # 获取：时
        self.min = int(time.strftime('%M'))     # 获取：分
        self.second = int(time.strftime('%S'))  # 获取：秒

        self.now_s = int(time.time())
        # self.set_year = tk.IntVar()     # 定义年
        # self.set_month = tk.IntVar()    # 定义月
        # self.set_day = tk.IntVar()      # 定义日
        self.set_hour = tk.IntVar()     # 定义时
        self.set_min = tk.IntVar()      # 定义分
        self.set_second = tk.IntVar()   # 定义秒

        self.new_month = self.month     # 用于计算新的月份
        self.new_day = self.day         # 用于计算新的天数
        pass

    def timingcustom(self):
        if self.Timing_custom_flag:
            self.Timing_custom_flag = False
            # 创建子窗口，主窗口一旦关闭，子窗口连同一起关闭
            self.ttwin = tk.Toplevel()
            self.ttwin.title("设置定时关机-24小时制")
            self.ttwin.resizable(0, 0)
            sw = self.ttwin.winfo_screenwidth()
            sh = self.ttwin.winfo_screenheight()
            ww = 300
            wh = 100
            x = (sw - ww) / 2
            y = (sh - wh - 50) / 2
            self.ttwin.geometry("%dx%d+%d+%d" % (ww, wh, x, y))

            # 设置小时定时
            hour_text = tk.Label(self.ttwin, text="时：")
            hour_text.place(x=30, y=10)
            set_hour = tk.Entry(self.ttwin, textvariable=self.set_hour, width=10)
            set_hour.place(x=80, y=10)
            # 设置分钟定时
            min_text = tk.Label(self.ttwin, text="分：")
            min_text.place(x=30, y=40)
            set_min = tk.Entry(self.ttwin, textvariable=self.set_min, width=10)
            set_min.place(x=80, y=40)
            # 设置秒定时
            second_text = tk.Label(self.ttwin, text="秒：")
            second_text.place(x=30, y=70)
            set_second = tk.Entry(self.ttwin, textvariable=self.set_second, width=10)
            set_second.place(x=80, y=70)
            # 固定追加的默认值
            if self.count:
                set_hour.insert(0, "0")
                set_min.insert(0, "0")
                set_second.insert(0, "0")
                self.count -= 1
            # 设置确定按钮
            ok_button = tk.Button(self.ttwin, text="确定", command=self.timing_custom)
            ok_button.place(x=220, y=10)
            # 设置取消按钮
            quit_button = tk.Button(self.ttwin, text="取消", command=lambda: (self.ttwin.destroy(), self.exit_ttwin()))
            quit_button.place(x=220, y=60)
            # 设置点'X'后执行的内容
            self.ttwin.protocol('WM_DELETE_WINDOW', lambda: (self.ttwin.destroy(), self.exit_ttwin()))
            self.ttwin.mainloop()

            self.ttwin.mainloop()
            self.Timing_custom_flag = True

    def exit_ttwin(self):
        # 点击'取消'或'X'后，将规则重回为True
        self.Timing_custom_flag = True

    def timing_custom(self):
        # 判断用户输入的值是否合法
        try:
            if 0 <= int(self.set_hour.get()) < 24 and \
                    0 <= int(self.set_min.get()) < 60 and \
                    0 <= int(self.set_second.get()) < 60:
                use_time = "%02d:%02d:%02d" % (self.set_hour.get(), self.set_min.get(), self.set_second.get())
                # 调用get_use_time_s函数，获取定时操作
                self.get_use_time_s(use_time)
            else:
                messagebox.showerror(title='设置异常', message='您的输入超出范围，请设置正确的值！')
        except Exception as re:
            messagebox.showerror(title='设置异常', message='您的输入有误，请输入正整数！')
            print(re)

    def count_time_00(self):
        self.get_use_time_s("00:00:00")

    def count_time_06(self):
        self.get_use_time_s("06:00:00")

    def count_time_12(self):
        self.get_use_time_s("12:00:00")

    def count_time_18(self):
        self.get_use_time_s("18:00:00")

    def get_use_time_s(self, use_time):
        set_use_time = use_time.split(":")
        # 判断传入的时间是否已经过去。没有过去，则定时为今日
        if self.hour < int(set_use_time[0]) or \
                (self.hour == int(set_use_time[0]) and self.min < int(set_use_time[1])) or \
                (self.hour == int(set_use_time[0]) and self.min == int(set_use_time[1]) and self.second < int(set_use_time[2])):
            # 获取对话框停留的秒数
            msg_time_start = int(time.time())
            res = messagebox.askokcancel('关机提醒', message=f'确定要设置在{use_time}关机吗？')
            msg_time_end = int(time.time())
            msg_time = msg_time_end - msg_time_start
            # 如果点确定则执行
            if res:
                # 获取当前时间
                d1 = datetime.datetime.strptime(
                    f'{self.year}-{self.month}-{self.day} {self.hour}:{self.min}:{self.second}', '%Y-%m-%d %H:%M:%S')
                # 获取设置的时间
                d2 = datetime.datetime.strptime(f'{self.year}-{self.month}-{self.day} {use_time}', '%Y-%m-%d %H:%M:%S')
                # 获取现在到指定时间需要的时间
                delta = d2 - d1
                delta_time = str(delta).split(":")
                # 获取现在到指定时间的秒数
                set_timing = int(delta_time[0]) * 60 + int(delta_time[1]) * 60 + int(delta_time[2]) - msg_time - 1
                # os.system(f"Windows定时关机 -s -t {set_timing}")
                subprocess.run(f"shutdown -s -t {set_timing}",
                               shell=True,
                               stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)
                messagebox.showinfo(title='关机提醒', message=f'该计算机将于{use_time}关机！')
        # 如果过去，则询问是否定为明天
        else:
            # 获取对话框停留的秒数
            msg_time_start = int(time.time())
            res = messagebox.askokcancel("关机提醒", message="你设置的关机时间已过去，是否设置为明天的定时？")
            msg_time_end = int(time.time())
            msg_time = msg_time_end - msg_time_start
            # 如果点确定则执行
            if res:
                # 如果设置的时间以过，则天数+1
                if (self.month != 1 or self.month != 2 or self.month != 3 or self.month != 5 or
                        self.month != 7 or self.month != 8 or self.month != 10 or self.month != 12) and \
                        self.day == 30:     # 如果月数不是1,2,3,5,7,8,10,12月，并且是第30天时，月份+1，天数归1
                    self.new_month = self.month + 1
                    self.new_day = 1
                elif self.day == 31:        # 如果是第31天时，月份+1，天数归1
                    self.new_month = self.month + 1
                    self.new_day = 1
                else:       # 否则，也就是不是每月的最后一天，则天数+1
                    self.new_day = self.day + 1
                # 获取当前时间
                d1 = datetime.datetime.strptime(
                    f'{self.year}-{self.month}-{self.day} {self.hour}:{self.min}:{self.second}', '%Y-%m-%d %H:%M:%S')
                # 获取设置的时间
                d2 = datetime.datetime.strptime(f'{self.year}-{self.new_month}-{self.new_day} {use_time}', '%Y-%m-%d %H:%M:%S')
                # 获取现在到指定时间需要的时间
                delta = d2 - d1
                delta_time = str(delta).split(":")
                # 获取现在到指定时间的秒数
                set_timing = int(delta_time[0]) * 60 * 60 + int(delta_time[1]) * 60 + int(delta_time[2]) - msg_time - 1
                # os.system(f"Windows定时关机 -s -t {set_timing}")
                subprocess.run(f"shutdown -s -t {set_timing}",
                               shell=True,
                               stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)
                messagebox.showinfo(title='关机提醒', message=f'该计算机将于{self.new_month}月{self.new_day}日 {use_time}关机！')
                print(set_timing)


#without using class
"""stack"""
s = []
ans = input("Enter y to continue: ")
ch = 0
while(ans == "y"):
    print("1.Push: ")
    print("2.Pop: ")
    print("3.Display: ")
    ch = int(input("Enter your choice: "))
    if(ch == 1):
        a = int(input("Enter any number: "))
        s.append(a)
        print(s)
    elif(ch == 2):
        if(s == []):
            print("STACK IS EMPTY")
        else:
            print("Deleted element is: ",s.pop())

    elif(ch == 3):
        L = len(s)
        for i in range(L - 1, -1, -1):
            print(s[i])

    else:
        print("WRONG INPUT")
    ans = input("Enter y to continue: ")


import mmcv
import torch
import torch.nn as nn
import torch.nn.functional as F
from ..builder import LOSSES
from .utils import weighted_loss

@LOSSES.register_module()
class GazeArccosLoss(nn.Module):
    """Gaze Arccos loss.

    Args:
        beta (float, optional): The threshold in the piecewise function.
            Defaults to 1.0.
        reduction (str, optional): The method to reduce the loss.
            Options are "none", "mean" and "sum". Defaults to "mean".
        loss_weight (float, optional): The weight of loss.
    """

    def __init__(self, beta=1.0, reduction='mean', loss_weight=1.0):
        super(GazeArccosLoss, self).__init__()
        self.beta = beta
        self.reduction = reduction
        self.loss_weight = loss_weight

    def forward(self,
                pred,
                target,
                weight=None,
                avg_factor=None,
                reduction_override=None,
                **kwargs):
        """Forward function.

        Args:
            pred (torch.Tensor): The prediction.
            target (torch.Tensor): The learning target of the prediction.
            weight (torch.Tensor, optional): The weight of loss for each
                prediction. Defaults to None.
            avg_factor (int, optional): Average factor that is used to average
                the loss. Defaults to None.
            reduction_override (str, optional): The reduction method used to
                override the original reduction method of the loss.
                Defaults to None.
        """
        assert reduction_override in (None, 'none', 'mean', 'sum')
        reduction = (
            reduction_override if reduction_override else self.reduction)
        gaze_dim = target.size(1)
        if gaze_dim != 3:
            pred = self.yaw_pitch_to_vector(pred)
            target = self.yaw_pitch_to_vector(target)
        sim = F.cosine_similarity(pred, target, dim=-1, eps=1e-6)
        sim = F.hardtanh(sim, -1.0 + 1e-6, 1.0 - 1e-6)

        # dot_product = torch.sum(pred * target, dim=-1)
        # norm_pred = torch.sqrt(torch.sum(pred ** 2, dim=-1))
        # norm_target = torch.sqrt(torch.sum(target ** 2, dim=-1))
        # cos_sim = dot_product / (norm_pred * norm_target)

        loss_angle = torch.acos(sim)

        return self.loss_weight*loss_angle.mean()

    def yaw_pitch_to_vector(self, x):
        x = torch.reshape(x, (-1, 2))
        output = torch.zeros((x.shape[0], 3))
        output[:,2] = - torch.cos(x[:,1]) * torch.cos(x[:,0])
        output[:,0] = torch.cos(x[:,1]) * torch.sin(x[:,0])
        output[:,1] = torch.sin(x[:,1])
        return output

    def vector_to_yaw_pitch(self, x):
        x = torch.reshape(x, (-1, 3))
        x = x / torch.norm(x, dim=1).reshape(-1, 1)
        output = torch.zeros((x.shape[0], 2))
        output[:,0] = torch.atan2(x[:,0], - x[:,2])
        output[:,1] = torch.asin(x[:,1])
        return output


# Uses python3
import sys

def get_optimal_value(capacity, inf):
    value = 0.
    # write your code here
    for o in inf:
        if capacity <= 1e-5:
            return value
        if o[2] < capacity - 1e-5:
            capacity -= o[2]
            value += o[1]
        else:
            value += o[1] * capacity / o[2]
            capacity = 0
    return value

if __name__ == "__main__":
    data = list(map(int, sys.stdin.read().split()))
    n, capacity = data[0:2]
    values = data[2:(2 * n + 2):2]
    weights = data[3:(2 * n + 2):2]
    ratio = [v/w for (v,w) in zip(values, weights)]
    inf = [[r,v,w] for (r,v,w) in zip (ratio, values, weights)]
    inf.sort(key = lambda x: x[0], reverse = True)
    # print (inf)
    opt_value = get_optimal_value(capacity, inf)
    print("{:.10f}".format(opt_value))


from . import markers

from ..ctx import context

from .fixtures.parameters import Parametrization
from ..exceptions import UnknownFixtures

def exclude(name, values):
    """
    Excludes a specific parametrization of a test from running

    :param name: can receive either a name of a parameter for this test, or a name of a fixture parameter
    :param values: must be a list of values to exclude for the given parameter
    """
    return markers.exclude_marker((name, values))

def is_excluded(test):
    test_func = test.get_test_function()
    exclusions = markers.exclude_marker.get_value(test_func, default=None)
    if not exclusions:
        return False
    exclusions = dict(exclusions)
    for parameter_name, values in exclusions.items():
        param = context.session.fixture_store.resolve_name(parameter_name, start_point=test_func)
        if not isinstance(param, Parametrization):
            raise UnknownFixtures('{!r} is not a parameter, and therefore cannot be the base for value exclusions'.format(parameter_name))
        param_index = test.__slash__.variation.param_value_indices[param.info.id] #pylint: disable=no-member
        value = param.transform(param.values[param_index]) #pylint: disable=no-member
        if value in values:
            return True
    return False


from collections import deque

def best_list_pureness(numbers, k):
    numbers_queue = deque(numbers)
    best_result = 0
    best_index = 0

    for index in range(k + 1):
        current_index_sum = sum([n * i for i, n in enumerate(numbers_queue)])
        if current_index_sum > best_result:
            best_index = index
            best_result = current_index_sum
        numbers_queue.appendleft(numbers_queue.pop())

    return f'Best pureness {best_result} after {best_index} rotations'

test = ([4, 3, 2, 6], 4)
result = best_list_pureness(*test)
print(result)
test = ([7, 9, 2, 5, 3, 4], 3)
result = best_list_pureness(*test)
print(result)
test = ([1, 2, 3, 4, 5], 10)
result = best_list_pureness(*test)
print(result)


# -*- coding: utf-8 -*-

from odoo import _, api, exceptions, fields, models
from odoo.exceptions import UserError

class TSCAccountMove(models.Model):
    _inherit = 'account.move'

    tsc_check_user_group = fields.Boolean(
        string="Revisa el grupo del usuario activo",
        compute="_compute_tsc_check_user_group",
        store=False
    )

    @api.depends('state')
    def _compute_tsc_check_user_group(self):
        self.tsc_check_user_group = False
        if self.move_type == 'out_refund':
            res_user = self.env['res.users'].search([('id', '=', self._uid)])

            if res_user.has_group('tsc_restrict_credit_note_confirmation.tsc_confirm_customer_credit_note_group'):
                if ((self.state != 'draft') or (self.auto_post == True) or (self.move_type == 'entry') or (self.display_inactive_currency_warning == True)):
                    self.tsc_check_user_group = True
                else:
                    self.tsc_check_user_group = False
            else:
                self.tsc_check_user_group = True
        else:
            if ((self.state != 'draft') or (self.auto_post == True) or (self.move_type == 'entry') or (self.display_inactive_currency_warning == True)):
                self.tsc_check_user_group = True

class TSCAccountMoveReversal(models.TransientModel):
    _inherit = 'account.move.reversal'

    @api.model
    def _tsc_get_selection(self):
        res_user = self.env['res.users'].search([('id', '=', self._uid)])

        if res_user.has_group('tsc_restrict_credit_note_confirmation.tsc_confirm_customer_credit_note_group'):
            if self._context.get('lang', 'en_EN') == 'es_ES':
                options = [
                    ('refund', 'Reembolso parcial'),
                    ('cancel', 'Reembolso completo'),
                    ('modify', 'Reembolso completo y nuevo borrador de factura')
                ]
            else:
                options = [
                    ('refund', 'Partial Refund'),
                    ('cancel', 'Full Refund'),
                    ('modify', 'Full refund and new draft invoice')
                ]
        else:
            if self._context.get('lang', 'en_EN') == 'es_ES':
                options = [
                    ('refund', 'Reembolso parcial'),
                ]
            else:
                options = [
                    ('refund', 'Partial Refund'),
                ]

        return options

    refund_method = fields.Selection(
        selection=_tsc_get_selection,
        string='Credit Method',
        required=True,
        help='Choose how you want to credit this invoice. You cannot "modify" nor "cancel" if the invoice is already reconciled.',
        translate=True
    )


from demosaicking.utils.image_utils import tsplit, tstack, norm_img
from demosaicking.utils.bayer_mosaicking import masks_CFA_Bayer
from demosaicking.utils.convolution_utils import to_convolve_mat
from scipy.ndimage.filters import convolve
from gradients import *

def postprocessing_step(RGB_d, eps=0.01):
    R_d, G_d, B_d = tsplit(RGB_d)
    img_shape = RGB_d.shape[:2]
    r_mask, g_mask, b_mask = masks_CFA_Bayer(img_shape)
    post_diff_gr = G_d - R_d
    post_diff_gb = G_d - B_d

    post_h5 = np.array([-5, 15, 44, 15, -5]) / 64.

    post_P_mat = to_convolve_mat(np.eye(5) * post_h5)
    post_Q_mat = to_convolve_mat(np.flip(np.eye(5), 1) * post_h5)
    h_mat = np.vstack([np.zeros((2, 5)), post_h5.reshape((1, 5)), np.zeros((2, 5))])
    post_H_mat = to_convolve_mat(h_mat)
    post_V_mat = to_convolve_mat(np.transpose(h_mat))

    def process_color(post_diff_color):
        D_p = convolve(post_diff_color, post_P_mat)
        D_q = convolve(post_diff_color, post_Q_mat)
        D_h = convolve(post_diff_color, post_H_mat)
        D_v = convolve(post_diff_color, post_V_mat)

        lambda_p = gradient_p(post_diff_color)
        lambda_q = gradient_q(post_diff_color)
        lambda_h = gradient_h(post_diff_color)
        lambda_v = gradient_v(post_diff_color)

        lambdas = [lambda_p, lambda_q, lambda_h, lambda_v]
        etas = np.asarray(map(lambda x: 1. / (x + eps), lambdas))

        etas_sum = np.apply_along_axis(np.sum, 0, etas)

        post_est_sum = np.apply_along_axis(np.sum, 0, etas * np.asarray([D_p, D_q, D_h, D_v]))
        post_diff_p = post_est_sum / etas_sum

        post_diff_p[:2, :] = post_diff_color[:2, :]
        post_diff_p[-2:, :] = post_diff_color[-2:, :]
        post_diff_p[:, :2] = post_diff_color[:, :2]
        post_diff_p[:, -2:] = post_diff_color[:, -2:]

        return post_diff_p

    post_diff_gr_p = process_color(post_diff_gr)
    post_diff_gb_p = process_color(post_diff_gb)

    rb_mask = np.asarray(1 - g_mask, dtype=bool)
    rg_mask = np.asarray(1 - b_mask, dtype=bool)
    bg_mask = np.asarray(1 - r_mask, dtype=bool)

    G_refined = (0.5 * (R_d + post_diff_gr_p) + 0.5 * (B_d + post_diff_gb_p)) * rb_mask + G_d * g_mask
    assert (np.sum(np.isnan(G_refined)) == 0)

    post_diff_rg = R_d - G_refined
    post_diff_bg = B_d - G_refined

    post_diff_rg_p = process_color(post_diff_rg)
    post_diff_bg_p = process_color(post_diff_bg)

    R_refined = (G_refined + post_diff_rg_p) * bg_mask + R_d * r_mask
    B_refined = (G_refined + post_diff_bg_p) * rg_mask + B_d * b_mask

    assert (np.sum(np.isnan(R_refined)) == 0)
    assert (np.sum(np.isnan(B_refined)) == 0)

    return norm_img(tstack([R_refined, G_refined, B_refined]))


from typing import List

class Solution:
    def isMonotonic(self, nums: List[int]) -> bool:
        n = len(nums)
        increasing, decreasing = False, False
        for i in range(n - 1):
            if nums[i] < nums[i + 1]:
                increasing = True
            elif nums[i] > nums[i + 1]:
                decreasing = True
            if increasing and decreasing:
                return False
        return True


from math import *

def ngto(n):
    for i in range(2,isqrt(n)+1):
        if(n%i==0): return 0
    return n>1

def solve(n):
    dau=int(n[:3])
    cuoi=int(n[-3:])
    if(ngto(dau)+ngto(cuoi)==2):
        print('YES')
    else:
        print('NO')

t=int(input())
for _ in range(t):
    s=input()
    solve(s)


#!/usr/bin/python3

import os
import sys
import re
import time
import random
import argparse
import requests
import tldextract
from threading import Thread
from queue import Queue
from urllib.parse import urlparse
from colored import fg, bg, attr

MAX_EXCEPTION = 3
MAX_VULNERABLE = 3

# disable "InsecureRequestWarning: Unverified HTTPS request is being made."
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

def banner():
	print("""
                       ___ ___  _ __ ___       _ __  _   _
                      / __/ _ \| '__/ __|     | '_ \| | | |
                     | (_| (_) | |  \__ \  _  | |_) | |_| |
                      \___\___/|_|  |___/ (_) | .__/ \__, |
                                              |_|    |___/

                                by @gwendallecoguic

""")
	pass

banner()

def generatePayloads( url ):
    t_url_parse = urlparse( url )
    t_host_parse = tldextract.extract( t_url_parse.netloc )
    domain = t_host_parse.domain + '.' + t_host_parse.suffix

    t_url_payloads = []
    for payload in t_payloads:
        payload = payload.replace( 'www.domain.com', t_url_parse.netloc ).replace( 'domain.com', domain )
        t_url_payloads.append( payload )

    # print( t_url_payloads )
    return t_url_payloads

def testURL( url ):
    time.sleep( 0.01 )

    if _verbose <= 1:
        sys.stdout.write( 'progress: %d/%d\r' %  (t_multiproc['n_current'],t_multiproc['n_total']) )
        t_multiproc['n_current'] = t_multiproc['n_current'] + 1

    t_url_payloads = generatePayloads( url )
    for payload in t_url_payloads:
        testPayload( url, payload )

def testPayload( url, payload ):
    t_urlparse = urlparse(url)
    u = t_urlparse.scheme + '_' + t_urlparse.netloc

    if not u in t_exceptions:
        t_exceptions[u] = 0
    if t_exceptions[u] >= MAX_EXCEPTION:
        if _verbose >= 3:
            print("skip too many exceptions %s" % t_urlparse.netloc)
        return

    if not u in t_vulnerable:
        t_vulnerable[u] = 0
    if t_vulnerable[u] >= MAX_VULNERABLE:
        if _verbose >= 3:
            print("skip already vulnerable %s" % t_urlparse.netloc)
        return

    headers = { "Origin":payload, "Referer":payload }
    headers.update( t_custom_headers )

    try:
        r = requests.head( url, headers=headers, timeout=5, verify=False )
    except Exception as e:
        t_exceptions[u] = t_exceptions[u] + 1
        if _verbose >= 3:
            sys.stdout.write( "%s[-] error occurred: %s%s\n" % (fg('red'),e,attr(0)) )
        return

    if 'Content-Type' in r.headers:
        content_type = r.headers['Content-Type']
    else:
        content_type = '-'

    vuln = '-'
    if 'Access-Control-Allow-Credentials' in r.headers and r.headers['Access-Control-Allow-Credentials'] == 'true':
            if 'Access-Control-Allow-Origin' in r.headers:
                if r.headers['Access-Control-Allow-Origin'] == 'null' or 'evil' in r.headers['Access-Control-Allow-Origin']:
                    vuln = 'VULNERABLE'

    if vuln == 'VULNERABLE':
        t_vulnerable[u] = t_vulnerable[u] + 1

    output = '%sC=%d\t\tT=%s\t\tV=%s\t\tP=%s\n' %  (url.ljust(t_multiproc['u_max_length']),r.status_code,content_type,vuln,payload)

    fp = open( t_multiproc['f_output'], 'a+' )
    fp.write( output )
    fp.close()

    if _verbose >= 2 or (_verbose >= 1 and vuln == 'VULNERABLE'):
        if vuln == 'VULNERABLE':
            sys.stdout.write( '%s%s%s' % (fg('light_red'),output,attr(0)) )
        else:
            sys.stdout.write( output )
    if _verbose >= 3:
        sys.stdout.write( '%s%s%s\n' % (fg('dark_gray'),headers,attr(0)) )

parser = argparse.ArgumentParser()
parser.add_argument( "-d","--header",help="custom headers, example: cookie1=value1;cookie2=value2...", action="append" )
parser.add_argument( "-a","--path",help="set paths list" )
parser.add_argument( "-p","--payloads",help="set payloads list" )
parser.add_argument( "-o","--hosts",help="set host list (required or -u)" )
# parser.add_argument( "-r","--redirect",help="follow redirection" )
parser.add_argument( "-s","--scheme",help="scheme to use, default=http,https" )
parser.add_argument( "-t","--threads",help="threads, default 10" )
parser.add_argument( "-u","--urls",help="set url list (required or -o)" )
parser.add_argument( "-v","--verbose",help="display output, 0=nothing, 1=only vulnerable, 2=all requests, 3=full debug, default: 1" )
parser.parse_args()
args = parser.parse_args()

if args.scheme:
    t_scheme = args.scheme.split(',')
else:
    t_scheme = ['http','https']

t_custom_headers = {}
if args.header:
    for header in args.header:
        if ':' in header:
            tmp = header.split(':')
            t_custom_headers[ tmp[0].strip() ] = tmp[1].strip()

t_hosts = []
if args.hosts:
    if os.path.isfile(args.hosts):
        fp = open( args.hosts, 'r' )
        t_hosts = fp.read().strip().split("\n")
        fp.close()
    else:
        t_hosts.append( args.hosts )
n_hosts = len(t_hosts)
sys.stdout.write( '%s[+] %d hosts found: %s%s\n' % (fg('green'),n_hosts,args.hosts,attr(0)) )

t_urls = []
if args.urls:
    if os.path.isfile(args.urls):
        fp = open( args.urls, 'r' )
        t_urls = fp.read().strip().split("\n")
        fp.close()
    else:
        t_urls.append( args.urls )
n_urls = len(t_urls)
sys.stdout.write( '%s[+] %d urls found: %s%s\n' % (fg('green'),n_urls,args.urls,attr(0)) )

if n_hosts == 0 and n_urls == 0:
    parser.error( 'hosts/urls list missing' )

t_path = [ '' ]
if args.path:
    if os.path.isfile(args.path):
        fp = open( args.path, 'r' )
        t_path = fp.read().strip().split("\n")
        fp.close()
    else:
        t_path.append( args.path )
n_path = len(t_path)
sys.stdout.write( '%s[+] %d path found: %s%s\n' % (fg('green'),n_path,args.path,attr(0)) )

if args.payloads:
    t_payloads = []
    if os.path.isfile(args.payloads):
        fp = open( args.payloads, 'r' )
        t_payloads = fp.read().strip().split("\n")
        fp.close()
    else:
        t_payloads.append( args.payloads )
    n_payloads = len(t_payloads)
    sys.stdout.write( '%s[+] %d payloads found: %s%s\n' % (fg('green'),n_payloads,args.payloads,attr(0)) )
else:
    n_payloads = 0

if args.verbose:
    _verbose = int(args.verbose)
else:
    _verbose = 1

if args.threads:
    _threads = int(args.threads)
else:
    _threads = 10

if not n_payloads:
    t_payloads = [
        'https://www.evildomain.com',
        'https://evil.www.domain.com',
        'https://evil.domain.com',
        'https://www.domain.com.evil.com',
        'https://www.domain.comevil',
        'https://www.evil.com',
        'https://domain.com%60.evil.com',
        'null',
        # source: https://www.bedefended.com/papers/cors-security-guide
        'https://www.domain.com".evil.com',
        'https://www.domain.com\'.evil.com',
        'https://www.domain.com!.evil.com',
        'https://www.domain.com$.evil.com',
        'https://www.domain.com%0b.evil.com',
        'https://www.domain.com%60.evil.com',
        'https://www.domain.com&.evil.com',
        'https://www.domain.com(.evil.com',
        'https://www.domain.com).evil.com',
        'https://www.domain.com*.evil.com',
        'https://www.domain.com,.evil.com',
        'https://www.domain.com;.evil.com',
        'https://www.domain.com=.evil.com',
        'https://www.domain.com+.evil.com',
        'https://www.domain.com^.evil.com',
        'https://www.domain.com`.evil.com',
        'https://www.domain.com{.evil.com',
        'https://www.domain.com}.evil.com',
        'https://www.domain.com|.evil.com',
        'https://www.domain.com[.evil.com',
        'https://www.domain.com].evil.com',
        'https://www.domain.com~.evil.com',
        'https://www.domain.com@.evil.com',
        'https://www.domain.com-.evil.com',
        'https://www.domain.com_.evil.com',
    ]

t_totest = []
u_max_length = 0
d_output =  os.getcwd()+'/cors'
f_output = d_output + '/' + 'output'
if not os.path.isdir(d_output):
    try:
        os.makedirs( d_output )
    except Exception as e:
        sys.stdout.write( "%s[-] error occurred: %s%s\n" % (fg('red'),e,attr(0)) )
        exit()

sys.stdout.write( '%s[+] options are -> threads:%d, verbose:%d%s\n' % (fg('green'),_threads,_verbose,attr(0)) )
sys.stdout.write( '[+] computing host and payload list...\n' )

for scheme in t_scheme:
    for host in t_hosts:
        for path in t_path:
            u = scheme + '://' + host.strip() + path
            t_totest.append( u )
            l = len(u)
            if l > u_max_length:
                u_max_length = l

for url in t_urls:
    for path in t_path:
        u = url.strip() + path
        t_totest.append( u )
        l = len(u)
        if l > u_max_length:
            u_max_length = l

n_totest = len(t_totest)
sys.stdout.write( '%s[+] %d urls created.%s\n' % (fg('green'),n_totest,attr(0)) )
sys.stdout.write( '[+] testing...\n' )

random.shuffle(t_totest)
# print("\n".join(t_totest))
# exit()

t_exceptions = {}
t_vulnerable = {}
t_multiproc = {
    'n_current': 0,
    'n_total': n_totest,
    'u_max_length': u_max_length+5,
    'd_output': d_output,
    'f_output': f_output,
}

def doWork():
    while True:
        url = q.get()
        testURL( url )
        q.task_done()

q = Queue( _threads*2 )

for i in range(_threads):
    t = Thread( target=doWork )
    t.daemon = True
    t.start()

try:
    for url in t_totest:
        q.put( url )
    q.join()
except KeyboardInterrupt:
    sys.exit(1)


import requests, json, sys, pprint

def pretty(d, indent=0):
   for key, value in d.items():
      print('\t' * indent + str(key))
      if isinstance(value, dict):
         pretty(value, indent)
      else:
         print('\t' * (indent) + str(value))
      print('\t')

def client_cli():
    choice = 1
    print("Welcome to Covid-19 Data Provider\n")
    while(choice != 0):

        choice = int(input(
                           "Press 0 - to exit the application\n"+
                           "Press 1 - to view total confirmed, recovered and death cases\n"+
                           "Press 2 - to view confirmed, recovered and death cases per country\n"+
                           "Press 3 - to view only confirmed cases per country\n"))
        if(choice == 1):
            total_covid_amount()
        elif(choice == 2):
            total_per_country()
        elif(choice == 3):
            country_amount()
        else:
            print("Good Bye!")
            break

def total_covid_amount():
    url = "https://covid-19-data.p.rapidapi.com/totals"
    querystring = {"format":"undefined"}
    headers = {
    'x-rapidapi-host': "covid-19-data.p.rapidapi.com",
    'x-rapidapi-key': "****privateAPI_KEY"
    }
    response = requests.request("GET", url, headers=headers, params=querystring)
    response_str = response.text
    resp_dictionary = json.loads(response_str)[0]
    pretty(resp_dictionary)

def total_per_country():
    country_name = input("Please, input a country name: ")
    url = "https://covid-19-data.p.rapidapi.com/country"
    querystring = {"format":"undefined","name": country_name}
    headers = {
    'x-rapidapi-host': "covid-19-data.p.rapidapi.com",
    'x-rapidapi-key': "****privateAPI_KEY"
    }

    response = requests.request("GET", url, headers=headers, params=querystring)
    response_str = response.text
    resp_dictionary = json.loads(response_str)[0]

    infected = resp_dictionary['confirmed']
    recovered = resp_dictionary['recovered']
    deaths = resp_dictionary['deaths']

    dict_return  = {}

    dict_return['confirmed'] = infected
    dict_return['recovered'] = recovered
    dict_return['deaths'] = deaths

    pretty(dict_return)

def country_amount():
    """The input Countries list """
    countries_list=[]
    length = int(input("How many countries you want to get data about?: "))
    for i in range(length):
        con_name = str(input())
        countries_list.append(con_name)

    """Output is a dict with a country name and confirmed cases"""
    dictionary_to_return = {}
    for country in countries_list:
        url = "https://covid-19-data.p.rapidapi.com/country"
        querystring = {"format":"undefined","name": country}
        headers = {
            'x-rapidapi-host': "covid-19-data.p.rapidapi.com",
            'x-rapidapi-key': "****privateAPI_KEY"
            }
        response = requests.request("GET", url, headers=headers, params=querystring)
        response_str = response.text
        resp_dictionary = json.loads(response_str)[0]
        infected_amount = resp_dictionary['confirmed']
        dictionary_to_return[country] = infected_amount
    pretty(dictionary_to_return)

client_cli()


a=int(input())
s=list(map(int,input().split()))
d=[]
for i in range(len(s)-1):
    m=s[i+1:len(s)]
    m.sort(reverse=True)
    d.append(m[0])
d.append(0)
print(*d)


# BST Sequences:A binary search tree was created by traversing through an array
# from left to right and inserting each element. Given a binary search tree with
#     distinct elements, print all possible arrays that could have led to this tree.
# 2 1 3
# 2 3 1
# a bit wrong with this problem, all permutations can create this bst, but the problem is only focusing on
# the  traversel that root printed out first
# this sequences is such that by scan and creat the tree, only insert on the leaf nodes of the tree

class Node(object):
    def __init__(self,data):
        self.data=data
        self.nextnode = None

def bst_seq(t, res=[]):
    """
    res should not be [], just when call the function, let res=[] bst_seq(tree,[]), then OK
    """

    if not t:
        return  res
    else:
        l = bst_seq(t.left)
        r = bst_seq(t.right)
        for x in l:
            for y in r:
                # keep seperate relative order of x and y, then weave x and y, then add prefix
                for z in weaving(x, y):
                    # z  is one weaving of the array x and y
                    res.append([t.data]+z)
        return res

def weaving(x,y, temp=[]):
    """
    weaving two array
    """
    if not x:
        temp.append(y)
        return temp
    elif not y:
        temp.append(x)
        return temp
    else:
        for t in weaving(x[1:],y,temp):
            temp.append([x[0]+ t])

        for p in weaving(x, y[1:],temp):
            temp.append([y[0]+p])
        return temp


# Task 4.4
import sqlite3
import flask

### Database ##########################################
STATEMENT = f'''
SELECT OfficeID, SalesPersonName, MAX(TotalAmt) FROM
(SELECT SalesPersonName, SUM(Amount) AS TotalAmt, OFFICE.OfficeID
FROM SALESPERSON INNER JOIN SALE ON SALESPERSON.SalesPersonID == SALE.SalesPersonID
INNER JOIN OFFICE ON OFFICE.OfficeID==SALESPERSON.OfficeID
WHERE SaleDate like ? || '/' || ? || '/%' GROUP BY SalesPersonName) AS SALEAMOUNT GROUP BY OfficeID
ORDER BY OfficeID ASC
'''

def getTopPerforming(month, year):
    conn = sqlite3.connect('../computercompany.db')
    data = []
    dateFormatting = (f"{year:04}", f"{month:02}")
    #return dateFormatting
    cursor = conn.execute(STATEMENT, dateFormatting)
    for record in cursor:
        data.append(record)
    conn.close()
    return data

### Web app ###########################################
app = flask.Flask(__name__)

@app.route('/')
def main():
    return flask.render_template('form.html')

@app.route('/top_performers', methods=['POST'])
def topPerformers():
    # Processing form data
    month = int(flask.request.form['month'])
    year = int(flask.request.form['year'])
    # Templating
    monthNames = ['January', 'Feburary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
    monthName = monthNames[month - 1]
    title = f"Top Performers in {monthName} {year}"
    headers = ['Office ID', 'Salesperson', 'Total Amount S$']
    data = getTopPerforming(month, year)
    return flask.render_template('table.html',
                                 title=title,
                                 headers=headers, data=data)

app.run('0.0.0.0', 5000, debug=True)

School Exercises/2. NYJC A Level Computing 2019-2020/Other School Papers/2020 Prelim/JPJC P2/TASK4_4/TASK4_4.py


from copy import copy
import logging
import logging.config
import re
import sys

from . import exceptions
from . import model

def urljoin(*parts):
    if len(parts) == 1:
        return parts[0]
    first = parts[0]
    last = parts[-1]
    middle = parts[1:-1]

    first = first.rstrip('/')
    middle = list(map(lambda s: s.strip('/'), middle))
    last = last.lstrip('/')

    return '/'.join([first] + middle + [last])

class LogFormatter(logging.Formatter):
    level_colors = {
        logging.DEBUG: lambda level_name:
        f'{Bcolors.BOLD}{Bcolors.BLUE}{level_name}{Bcolors.RESET_ALL}',
        logging.INFO: lambda level_name:
        f'{Bcolors.BOLD}{Bcolors.GREEN}{level_name}{Bcolors.RESET_ALL}',
        logging.WARNING: lambda level_name:
        f'{Bcolors.BOLD}{Bcolors.YELLOW}{level_name}{Bcolors.RESET_ALL}',
        logging.ERROR: lambda level_name:
        f'{Bcolors.BOLD}{Bcolors.RED}{level_name}{Bcolors.RESET_ALL}',
    }

    def color_level_name(self, level_name, level_number):
        def default(level_name):
            return str(level_name)

        func = self.level_colors.get(level_number, default)
        return func(level_name)

    def formatMessage(self, record):
        record_copy = copy(record)
        levelname = record_copy.levelname
        if sys.stdout.isatty():
            levelname = self.color_level_name(levelname, record_copy.levelno)
            if "color_message" in record_copy.__dict__:
                record_copy.msg = record_copy.__dict__["color_message"]
                record_copy.__dict__["message"] = record_copy.getMessage()
        record_copy.__dict__["levelprefix"] = levelname
        return super().formatMessage(record_copy)

class Bcolors:
    RESET_ALL = '\033[0m'
    BOLD = '\033[1m'
    RED = '\033[31m'
    GREEN = '\033[32m'
    YELLOW = '\033[33m'
    BLUE = '\033[34m'

def configure_default_logging(
    stdout_level=None,
    force=True,
    print_thread_id=False,
):
    if not stdout_level:
        stdout_level = logging.INFO

    # make sure to have a clean root logger (in case setup is called multiple times)
    if force:
        for h in logging.root.handlers:
            logging.root.removeHandler(h)
            h.close()

    sh = logging.StreamHandler(stream=sys.stdout)
    sh.setLevel(stdout_level)
    sh.setFormatter(LogFormatter(fmt=default_fmt_string(print_thread_id=print_thread_id)))
    logging.root.addHandler(hdlr=sh)
    logging.root.setLevel(level=stdout_level)

def default_fmt_string(print_thread_id: bool=False):
    ptid = print_thread_id
    return f'%(asctime)s [%(levelprefix)s] {"TID:%(thread)d " if ptid else ""}%(name)s: %(message)s'

def strip_time_str_to_int(
    timeout_str: str,
    ms=False,
    s=False,
    m=False,
    h=False,
    d=False,
) -> int:
    timeunits = {
        'ms': ms,
        's': s,
        'm': m,
        'h': h,
        'd': d,
    }
    for k, v in timeunits.items():
        if v:
            timeout_str = timeout_str.replace(k, '')
    return int(timeout_str)

def parse_time_str_to_timedetail(
    time_str :str,
) -> model.TimeDetail:
    regex = re.compile('([0-9]+)(ms|s|m|h|d)')
    res = regex.match(time_str)
    if not res:
        raise exceptions.OpenmonitorConfigError('Unable to parse config', time_str=time_str)
    return model.TimeDetail(
        value=int(res[1]),
        unit=model.TimeUnit(res[2]),
    )


import os
import numpy
from pickle import load, dump
from tensorflow.keras import backend
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.layers import Input, Dense, Flatten, Dropout, Embedding, Conv1D, MaxPooling1D, concatenate

import tensorflow as tf
from tensorflow.python.util import deprecation

deprecation._PRINT_DECPRECATION_WARNINGS = False
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)

CHAR_ENCODE = {
    ' ' : 1,
    'E' : 2,
    'T' : 3,
    'A' : 4,
    'O' : 5,
    'I' : 6,
    'N' : 7,
    'S' : 8,
    'R' : 9,
    'H' : 10,
    'D' : 11,
    'L' : 12,
    'U' : 13,
    'C' : 14,
    'M' : 15,
    'F' : 16,
    'Y' : 17,
    'W' : 18,
    'G' : 19,
    'P' : 20,
    'B' : 21,
    'V' : 22,
    'K' : 23,
    'X' : 24,
    'Q' : 25,
    'J' : 26,
    'Z' : 27
}

class NeuralNetwork():
    def __init__(self, ext):
        self.ext = ext
        self.model = self.load_model()
        self.tokenizer = self.load_tokenizer()
        self.embed_len = 27
        self.max_msg_len = 256
        self.ready()

    def ready(self):
        if self.model == None: # or self.tokenizer == None:
            print('Files missing, please rebuild.')
            return False
        return True

    def load_tokenizer(self):
        if os.path.isfile('./cache/t_' + self.ext + '.pkl'):
            return load(open('./cache/t_' + self.ext + '.pkl', 'rb'))
        return None

    def load_model(self):
        if os.path.isfile('./cache/m_' + self.ext + '.h5'):
            return load_model('./cache/m_' + self.ext + '.h5')
        return None

    # def rebuild_tokenizer(self, lines):
    #     self.tokenizer = Tokenizer(num_words=27)
    #     self.tokenizer.fit_on_texts(lines)
    #     tokens = len(self.tokenizer.word_index) + 1
    #     self.embed_len = tokens
    #     dump(self.tokenizer, open('./cache/t_' + self.ext + '.pkl', 'wb'))

    def rebuild_model(self):
        self.model = self.define_model()

    def vectorize(self, lines):
        # print(lines[0])
        # encoded1 = self.tokenizer.texts_to_sequences(lines)
        encoded2 = [[CHAR_ENCODE[c] for c in l] for l in lines]
        # charset = set()
        # for ll, el in zip(lines, encoded):
        #     for lc, ec in zip(ll, el):
        #         charset.add((lc, ec,))
        # print('LENGTH:', len(charset))
        # for chars in charset:
        #     print(chars)
        # input()
        # padded1 = pad_sequences(encoded1, maxlen=self.max_msg_len, padding='post')
        padded2 = pad_sequences(encoded2, maxlen=self.max_msg_len, padding='post')
        # print(padded[0])
        # print(padded1)
        # input(padded2)
        return padded2

    def train(self, lines, labels, batch_size):
        X, Y = self.inputs(lines, labels)
        self.model.fit(X, Y, epochs=1, batch_size=batch_size)

    def test(self, lines, labels):
        X, Y = self.inputs(lines, labels)
        loss, acc = self.model.evaluate(X, Y, verbose=0)

    def save(self):
        self.model.save('./cache/m_' + self.ext + '.h5')

    def run_model(self, lines):
        if not self.ready():
            return None
        X, Y = self.inputs(lines, [])
        preds = self.model.predict(X)
        return preds

class CNN(NeuralNetwork):

    def __init__(self, ext):
        super().__init__(ext)

    def inputs(self, lines, labels):
        X = self.vectorize(lines)
        # print(X)
        # input(labels)
        return [X, X, X, X, X], labels

    def define_model(self):
        # Modification of the model shown here:
        # https://machinelearningmastery.com/develop-n-gram-multichannel-convolutional-neural-network-sentiment-analysis/

        # channel 1
        inputs1 = Input(shape=(self.max_msg_len,))
        embedding1 = Embedding(input_dim=self.embed_len, output_dim=self.max_msg_len, input_length=self.max_msg_len)(inputs1)
        conv1 = Conv1D(filters=32, kernel_size=2, activation='relu')(embedding1)
        drop1 = Dropout(0.5)(conv1)
        pool1 = MaxPooling1D(pool_size=2)(drop1)
        flat1 = Flatten()(pool1)
        # channel 2
        inputs2 = Input(shape=(self.max_msg_len,))
        embedding2 = Embedding(input_dim=self.embed_len, output_dim=self.max_msg_len, input_length=self.max_msg_len)(inputs2)
        conv2 = Conv1D(filters=32, kernel_size=3, activation='relu')(embedding2)
        drop2 = Dropout(0.5)(conv2)
        pool2 = MaxPooling1D(pool_size=2)(drop2)
        flat2 = Flatten()(pool2)
        # channel 3
        inputs3 = Input(shape=(self.max_msg_len,))
        embedding3 = Embedding(input_dim=self.embed_len, output_dim=self.max_msg_len, input_length=self.max_msg_len)(inputs3)
        conv3 = Conv1D(filters=32, kernel_size=4, activation='relu')(embedding3)
        drop3 = Dropout(0.5)(conv3)
        pool3 = MaxPooling1D(pool_size=2)(drop3)
        flat3 = Flatten()(pool3)
        # channel 4
        inputs4 = Input(shape=(self.max_msg_len,))
        embedding4 = Embedding(input_dim=self.embed_len, output_dim=self.max_msg_len, input_length=self.max_msg_len)(inputs4)
        conv4 = Conv1D(filters=32, kernel_size=5, activation='relu')(embedding4)
        drop4 = Dropout(0.5)(conv4)
        pool4 = MaxPooling1D(pool_size=2)(drop4)
        flat4 = Flatten()(pool4)
        # channel 5
        inputs5 = Input(shape=(self.max_msg_len,))
        embedding5 = Embedding(input_dim=self.embed_len, output_dim=self.max_msg_len, input_length=self.max_msg_len)(inputs5)
        conv5 = Conv1D(filters=32, kernel_size=6, activation='relu')(embedding5)
        drop5 = Dropout(0.5)(conv5)
        pool5 = MaxPooling1D(pool_size=2)(drop5)
        flat5 = Flatten()(pool5)
        # merge
        merged = concatenate([flat1, flat2, flat3, flat4, flat5])
        # interpretation
        dense1 = Dense(10, activation='relu')(merged)
        outputs = Dense(1)(dense1)
        model = Model(inputs=[inputs1, inputs2, inputs3, inputs4, inputs5], outputs=outputs)
        # compile
        model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
        # summarize
        # print(model.summary())
        # plot_model(model, show_shapes=True, to_file='./multichannel.png')
        return model


import json
import os
import requests

if __name__ == '__main__':
    print('PREPARING...')
    urls = [x.strip() for x in os.environ['ES_DATAPACKAGE'].split('\n') if x.strip()]
    total = []
    for url in urls:
        print('GET', url)
        total.append(requests.get(url, timeout=60).json())
    json.dump(total, open('datapackages.json', 'w'))