# input - processed text from nodejs
# output - statistics of different sorts

# part1-start = apply rules on slack messages

# please use python3 to run
import sys
import operator
import itertools as it
import math
from textblob import TextBlob as tb

import re

# import nltk
# import nltk.compat

import spacy
nlp = spacy.load('en')

dic_lsp = {}	#0:"why can this not", 1: "can this not ?"}
dic_mypos = {}
matched_rule = {}	#store the position of rules that matched
dic_revert = {}
ruleNegStart = 26
dic_rule_freq = {}

# global for similarities
dic_tfidf_word = {}
dic_sen_similar = {}
dic_tok_similar = {}
dic_com_similar = {}
dic_sen_tfidf = {}

# create all dict that has specific tokens

# question words
QW = {
	"could": 1,
	"can": 1,
	"would": 1,
	"will": 1,
	"do": 1,
	"does": 1,
	"did": 1,
	"wonder": 1,
	"were": 1
}

# wh and h questions words

WHQ = {
	"what": 1,
	"where": 1,
	"how": 1,
	"why": 1,
	"which": 1,
	"whom": 1,
	"whose": 1,
	"wherever": 1,
	"when": 1,
	"who": 1,
	"whether": 1
}

# after words
AW = {
	"that": 1,
	"i": 1,
	"you": 1,
	"it": 1,
	"this": 1,
	"there": 1,
	"they": 1,
	"are": 1,
	"anyone": 1,
	"one": 1,
	"any": 1,
	"anybody": 1,
	"doing": 1,
	"anything": 1,
	"we": 1,
	"someone": 1
}

# problem words
PW = {
	"problem": 1,
	"problems": 1,
	"error": 1,
	"errors": 1,
	"help": 1,
	"issue": 1,
	"issues": 1,
	"question": 1,
	"questions": 1,
}

# past myself words
PMW = {
	"am": 1
}

# opposite words
OW = {
	"cant": 1,
	"not": 1
}

# micellious words
MW = {
	"getting": 1,
	"having": 1,
	"outputting": 1,
}

# intermediate words
IW = {
	"is": 1,
	"are": 1,
	"so": 1,
	"you": 1,
	"but": 1,
	"any": 1,
	"if": 1,
	"have": 1,
	"had": 1,
	"has": 1
}

IW.update(WHQ)
IW.update(PW)

PUNC = {
"!": 1,
".": 1,
# ",": 1
}

MYPOS = {
	"QW": QW,
	"IW": IW,
	"MW": MW,
	"OW": OW,
	"AW": AW,
	"WHQ": WHQ,
	"PW": PW,
	"PMW": PMW,
	"PUNC" : PUNC
}

rule = {
	1: [["?"]],
	2: [["QW"], ["AW"]],	#<SCORE = 5/6>
	# 3: [["IW"],["PUNC"]],	#3 and 4 quite similar here!
	# 4: [["WHQ"],["PUNC"]],
	5: [["WHQ"],["QW"]],		#<SCORE = 3/6> but gets qs when no other indicators are present, very good
	6: [["WHQ"],["VBD"]],		#<SCORE = 4/6>
	7: [["PW"],["PMW"]],
	8: [["PW"],["have"]],
	9: [["have"], ["PW"]],
	10: [["have"], ["DT"], ["PW"]],
	11: [["any"], ["idea"]],
	12: [["is"], ["NN"]],
	13: [["is"],["there","this","that"]],
	14: [["are"], ["there","you","those","these"]],	#<SCORE = 1/2>
	15: [["are"], ["NN"]],
	16: [["were"], ["there","you","those","these"]],
	# 16: [["are"],["VB","VBD","VBG","VBN","VBP","VBZ"]],
	17: [["but"], ["QW"], ["AW"]],
	18: [["but"], ["WHQ"], ["QW"]],
	# 18: [["but"], ["IW"]],
	19: [["why"], ["OW"]],
	20: [["why"], ["IW"]],
	21: [["WHQ"], ["to"]],		#<SCORE = 1/5>
	22: [["whether"], ["AW"]],	#<SCORE = 1/1>
	23: [["wonder"], ["IW"]],
	24: [["PW"], ["is"]],
	25: [["PW"], ["are"]],

	# '-' at the start of token specifies that the latter POS cant be in token
	26: [["-", "VBZ"],["WHQ"], ["QW"], ["AW"]],

	# 25: [EOS + is]
	# 26: [EOS + are]
	#27: [SOS + is + AW]
	#[but not (VBZ) + (WH5+H) + (after words) + (question words)]
	# 9:["WHQ", ["at","is","might"]]
	#but no 'is' afterwards in that sentence, or unitl and or any sentence break is seen]
}

def reinit_matched_rule():
	matched_rule.clear()
	for i in rule:
		matched_rule[i] = 0

def clear_invalid_rule():
	for i in rule:
		if matched_rule[i] != -99:
			matched_rule[i] = 0
		elif matched_rule[i] == -99:
			matched_rule[i] = -100

def reinit_dic_revert():
	dic_revert.clear()
	for i in rule:
		dic_revert[i] = True

def init():
	reinit_matched_rule()
	reinit_dic_revert()
	dic_lsp.clear()
	dic_mypos.clear()

c = 4	#stores the number of words to be taken when doing LSP

# processes strings
def processStr(str):
	doc = nlp(str)
	i=0
	docLen = len(doc)
	if docLen >= c:
		for token in doc:
			if(i+c-1 >= docLen):
				break

			# add 1st token
			key = [token]
			# string = token.text
			for j in range(1,c):
				key.append(token.nbor(j))

			dic_lsp[i] = key
			key = []
			i+=1
	else:
		key=[]
		for token in doc:
				key.append(token)
		dic_lsp[i] = key

	# print(dic_lsp[0])

# called if a token doesn't match a pos and follows certain rules, with no neg rule
def check_isValid_rule(ruleNo,indexAt):
	if len(rule[ruleNo]) > (len(dic_mypos)-indexAt-1):
		matched_rule[ruleNo] = -1
	elif(dic_revert[ruleNo] != False):
		matched_rule[ruleNo] = 0

# called when token matches pos and is used for after processes
def process_validPos(ruleNo,indexAt):
	# print(tokenPos)
	dic_revert[ruleNo] = False
	# print("bbbb")
	if ruleNo >= ruleNegStart:
		rule_len_left = len(rule[ruleNo])-matched_rule[ruleNo]-1
	else:
		# we dont do matched_rule[ruleNo]-1 here as our rule for ruleNo >=24 starts from 1 and not zero
		# this is in the case where neg array is 0th element
		rule_len_left = len(rule[ruleNo])-1-matched_rule[ruleNo]

	if  rule_len_left == 0:
		matched_rule[ruleNo] = -99
		# print("found",ruleNo)
	elif ruleNo >= ruleNegStart and (rule_len_left > c-indexAt):
		matched_rule[ruleNo] = -1
	elif rule_len_left > (c-indexAt-1):
		matched_rule[ruleNo] = -1
	else:
		matched_rule [ruleNo] += 1

# finds neg index, assuming '-' its always in 0th position in the returned array
def find_negIndex(ruleNo):
	negIndex = -1
	count = 0;
	# find the index with '-', assume theres only 1 in a rule
	for arr in rule[ruleNo]:
		if arr[0] == "-":
			negIndex = count
			break
		count+=1
	# sanity check
	if negIndex == -1:
		print(ruleNo, "is has no '-' in it")

	return negIndex

# called when token doesn't match pos and has neg rule
def check_isValid_ruleNeg(ruleNo,indexAt, tokenPos):
	negIndex = find_negIndex(ruleNo)

	# -1 with ruleNo because it has 1 arr that is neg
	if len(rule[ruleNo])-1 > (len(dic_mypos)-indexAt):
		matched_rule[ruleNo] = -1
		# print("check_isValid_ruleNeg: rule len is greater", tokenPos)

	if(dic_revert[ruleNo] != False):
		l = len(rule[ruleNo][indexAt])
		# check if neg arr pos are present in token
		for each_myPos in rule[ruleNo][negIndex]:
			if matched_rule[ruleNo] >= 0 and each_myPos != "-":
				if each_myPos == rule[ruleNo][0][l-1]:
					lastMyPos = True
				else:
					lastMyPos = False

				lowpos = ""
				if each_myPos.islower():
					lowpos = dic_lsp[word_index][indexAt].orth_

				# if this is valid, then the neg tokens are present. hence we check if the rule can be valid. else ruleNo = -1
				if (each_myPos == lowpos or each_myPos == tokenPos or (tokenPos == "?" and matched_rule[1] == 0)) and dic_revert[ruleNo] != False:
					if len(rule[ruleNo])-1 > (len(dic_mypos)-indexAt-1):
						matched_rule[ruleNo] = -1
						dic_revert[ruleNo] = False
						return True
					else:
						matched_rule[ruleNo] = 0
						dic_revert[ruleNo] = False
						return True
				elif  lastMyPos and dic_revert[ruleNo] == True:
					return False
				else:
					return

# called after a neg rule has cleared that neg tokens are not present
def check_afterNeg(ruleNo, indexAt, tokenPos_list):
	if errDisplay:
		print("New List after Neg----",tokenPos_list)

	lastPos = False
	# reinit_dic_revert()
	tokenPos_len = len(tokenPos_list)
	for tokenPos in tokenPos_list:
		if tokenPos == tokenPos_list[tokenPos_len-1]:
			last_tokenPos = True
			if errDisplay > 1:
				print("last tokenpos: ", tokenPos)
		else:
			last_tokenPos = False

		# for each rule in rule dic, where matched_rule[ruleNo] != -1, go to a rule and...
		if errDisplay:
			print(tokenPos_list)
			print(tokenPos)
			print('before')
			for i in dic_revert:
				print(i ,dic_revert[i])

		if matched_rule[ruleNo] >= 0:
			for each_myPos in rule[ruleNo][matched_rule[ruleNo]]:
				if matched_rule[ruleNo] >= 0:
					l = len(rule[ruleNo][matched_rule[ruleNo]])
					if each_myPos == rule[ruleNo][matched_rule[ruleNo]][l-1]:
						lastMyPos = True
					else:
						lastMyPos = False

					lowpos = ""
					if each_myPos.islower():
						lowpos = dic_lsp[word_index][indexAt].orth_

					if (each_myPos == lowpos or each_myPos == tokenPos or (tokenPos == "?" and matched_rule[1] == 0)) and dic_revert[ruleNo] != False:
						process_validPos(ruleNo,indexAt)
					elif last_tokenPos and lastMyPos and dic_revert[ruleNo] != False:
						check_isValid_rule(ruleNo,indexAt)

# list of MYPOS, [previous rule if any in a form of a list], index number that is being checked
errDisplay = 0
def checkRule(tokenPos_list, indexAt, word_index, SOS, EOS):
    # no rule to exclude. so check all rules, otherwise not check that rule
    # for each MYPOS in mypos_list take a token and match it with a rule
    # for WHQ
    if errDisplay:
    	print("New List----",tokenPos_list)

    # pre-processing
    if SOS:
    	lowpos = dic_lsp[word_index][0].orth_
    	# print("SOS", SOS, "lowpos",lowpos)
    	if lowpos == "is":
    		matched_rule[28] = -99
    	elif lowpos == "are":
    		matched_rule[29] = -99

    reinit_dic_revert()
    tokenPos_len = len(tokenPos_list)
    isNegPresent = False
    last_tokenPos = False
    for tokenPos in tokenPos_list:
    	if tokenPos == tokenPos_list[tokenPos_len-1]:
    		last_tokenPos = True
    		if errDisplay:
	    		print("last tokenpos: ", tokenPos)
    	else:
    		last_tokenPos = False

    	# for each rule in rule dic, where matched_rule[ruleNo] != -1, go to a rule and...
    	if errDisplay:
	    	print(tokenPos_list)
	    	print(tokenPos)
	    	print('before')
	    	for i in dic_revert:
	    		print(i ,dic_revert[i])

    	for ruleNo in rule:
    		# ...check if the rule at x, with position matched_rule[x] is eqaul to mypos
    		# if true, increment the matched_rule[x]
    		if matched_rule[ruleNo] >= 0:
    			if ruleNo >= ruleNegStart: #for neg rules only
    				#find the neg index...(this part may be ommited if negs are always 1st pos array)
    				negIndex = find_negIndex(ruleNo)
    				# print(negIndex)
    				if negIndex == matched_rule[ruleNo]:
    					if not isNegPresent:
    						isNegPresent = check_isValid_ruleNeg(ruleNo,indexAt, tokenPos)
	    					if not isNegPresent and last_tokenPos:
	    						matched_rule[ruleNo]+=1
	    						# print(matched_rule[ruleNo])
	    						# check the given token pos for matches with negindex+1 as negs are not found
	    						check_afterNeg(ruleNo, indexAt, tokenPos_list)
    				else:
    					for each_myPos in rule[ruleNo][matched_rule[ruleNo]]:
			    			if matched_rule[ruleNo] >= 0:
			    				l = len(rule[ruleNo][matched_rule[ruleNo]])
			    				if each_myPos == rule[ruleNo][matched_rule[ruleNo]][l-1]:
			    					lastMyPos = True
			    				else:
			    					lastMyPos = False

				    			lowpos = ""
				    			if each_myPos.islower():
				    				lowpos = dic_lsp[word_index][indexAt].orth_

				    			if (each_myPos == lowpos or each_myPos == tokenPos or (tokenPos == "?" and matched_rule[1] == 0)) and dic_revert[ruleNo] != False:
				    			# check if rule is completely valid, or partially valid or can't be valid in future
				    				process_validPos(ruleNo,indexAt)

				    			# if false, check if the length of rule will hold if started from next dic_mypos.
					            # if rule len is more than tokens we have in dic_mypos, then we set -1
				    			elif last_tokenPos and lastMyPos and dic_revert[ruleNo] != False:	#we are using lastMyPos because sometimes, the 1st pos out of may works, but later ones doesnt..
				    			# elif last_tokenPos and dic_revert[ruleNo] != False:				#..so thats why we ensure that last one and it still doesnt match, then we do the elif
				    				# we will loop over the token_list again. so restart process..
				    				#.. in order to check if current token list has neg words
				    				# print(matched_rule[ruleNo], ruleNo)
				    				matched_rule[ruleNo] = 0
				    				last_tokenPos = 0
				    				isNegPresent = 0
					    			# check if neg tokens present in current tokenList
					    			for tPos in tokenPos_list:
					    				if tPos == tokenPos_list[tokenPos_len-1]:
								    		last_tokenPos = True
    									else:
    										last_tokenPos = False

    									if not isNegPresent:
				    						isNegPresent = check_isValid_ruleNeg(ruleNo,indexAt, tPos)
					    					if not isNegPresent and last_tokenPos:
					    						matched_rule[ruleNo]+=1
					    						check_afterNeg(ruleNo, indexAt, tokenPos_list)
					    						# makesure u get 2 here!
				    				# print("after all", matched_rule[ruleNo])

    			else:
		    		for each_myPos in rule[ruleNo][matched_rule[ruleNo]]:
		    			if matched_rule[ruleNo] >= 0:
		    				l = len(rule[ruleNo][matched_rule[ruleNo]])
		    				if each_myPos == rule[ruleNo][matched_rule[ruleNo]][l-1]:
		    					lastMyPos = True
		    				else:
		    					lastMyPos = False

			    			lowpos = ""
			    			if each_myPos.islower():
			    				lowpos = dic_lsp[word_index][indexAt].orth_

			    			if (each_myPos == lowpos or each_myPos == tokenPos or (tokenPos == "?" and matched_rule[1] == 0)) and dic_revert[ruleNo] != False:
			    			# check if rule is completely valid, or partially valid or can't be valid in future
			    				process_validPos(ruleNo,indexAt)

			    			# if false, check if the length of rule will hold if started from next dic_mypos.
				            # if rule len is more than tokens we have in dic_mypos, then we set -1
			    			elif last_tokenPos and lastMyPos and dic_revert[ruleNo] != False:	#we are using lastMyPos because sometimes, the 1st pos out of may works, but later ones doesnt..
			    			# elif last_tokenPos and dic_revert[ruleNo] != False:				#..so thats why we ensure that last one and it still doesnt match, then we do the elif
				    			check_isValid_rule(ruleNo,indexAt)
			    				# if has '-' in rule
			    				# if ruleNo >= 24:
	    						# 	check_isValid_ruleNeg(ruleNo,indexAt,tokenPos)
			    				# else:
			    					# not have '-' in rule

    	# # post processing
    	# if EOS and last_tokenPos:
    	# 	# c-2 as c-1 gives punc, c-2 gives one before punc
    	# 	# lowpos = dic_lsp[word_index][c-1].orth_
    	# 	# if lowpos == "?":
    	# 	# 	matched_rule[1] = -99
    	# 	# print(indexAt)
    	# 	# print(dic_lsp[0])
    	# 	lowpos = dic_lsp[word_index][len(dic_lsp[word_index])-2].orth_
    	# 	# print("EOS", EOS, "lowpos",lowpos)
    	# 	if lowpos == "is":
    	# 		matched_rule[26] = -99
    	# 	elif lowpos == "are":
    	# 		matched_rule[27] = -99

    	if errDisplay:
	    	print('after')
	    	for i in dic_revert:
	    		print(i ,dic_revert[i])

	    	for matched_index in matched_rule:
	    		print(matched_index ,matched_rule[matched_index])

# fill the global dic_mypos with MYPOS and POS and
def filldic_mypos(word_index):
	dic_mypos.clear()
	index_words=0;
	for word in dic_lsp[word_index]:
	# for "why", find its MYPOS, tag
		if not word.like_url:
			dic_mypos[index_words] = [word.tag_]

			# if word is ?, give it ? tag
			if word.orth_ == "?":
				d = dic_mypos[index_words]
				d.append("?")
				dic_mypos[index_words] = d

			for i in MYPOS:
			# take every MYPOS and check their tokens
				for j in MYPOS[i]:
				# if word was found in one of MYPOS
					if (word.orth_).lower() == j:
						d = dic_mypos[index_words]
						d.append(i)
						dic_mypos[index_words] = d
						break

		index_words+=1

s = {
	# 0: "is this way right is.",
}

def getStringsReady(fileIn):
	fileIn = open(fileIn)
	sen = ""
	key = 0
	tsFlag = 0	#if pflag is used to determine if the word is a ts (slack time)
	backupText = "" 	#this stores text for when we look ahead
	ts = ""
	with fileIn as f:
	  	while True:
		    c = f.read(1)
		    if not c:
		      # print( "End of file")
		      break
		    elif c == "p" or tsFlag:
		    	if tsFlag and c == "\t":
		    		tsFlag = 0
		    		ts = backupText
		    		backupText = ""

		    	elif tsFlag and c != "\t" and (c < "0" or c > "9"):
		    		backupText += c
		    		tsFlag = 0
		    		sen += backupText
		    		backupText = ""

		    	else:
		    		tsFlag = 1
		    		backupText += c

		    elif c == "?" or c == "!" or c == ".":
		    	sen+=c
		    	# sen = ts + sen
		    	# print(ts)
		    	if sen[0] == " ":
		    		sen = sen[1:]
		    	s[key] = ts +"\t"+ sen
		    	sen = ""
		    	key+=1
		    	# print ("Read a character:", c)
		    else:
		    	sen+=c

def part1_lable_staments():
	fileIn = sys.argv[1]
	fileoutRead = fileIn.replace("formatted","outRead")
	fileoutTech = fileIn.replace("formatted","outTech")
	fileoutTest = fileIn.replace("formatted","outTest")
	EOS = 0
	SOS = 0
	getStringsReady(fileIn)
	with open(fileoutRead,'w') as oread, open(fileoutTech,'w') as otech, open(fileoutTest,'w') as otest:
		for phrase in s:
			# string = s[phrase].split("\t",1)[0]
			# string[0] = ts
			# string[1] = actual line
			otech.write("\n"+s[phrase]+"\t")
			oread.write("\n"+s[phrase]+"\t")
			otest.write("\n"+s[phrase]+"\t")
			isquestion = 0
			init()
			# 1) get dic(words)
			processStr(s[phrase])
			# 2) create dic_mypos for len of dic_mypos[i]
			# in this case we will do it for dic_mypos[0] only
			used = []
			for lsp_index in dic_lsp:
				filldic_mypos(lsp_index)

				# 3) get the pattern and add it to respect dic_lsp element.
				for mypos in dic_mypos:
					# check for SOS
					if lsp_index == 0:
						SOS = 1
					else:
						SOS = 0
					# check for EOS
					if len(dic_lsp)-1 == lsp_index:
						EOS = 1
					else:
						EOS = 0
					checkRule(dic_mypos[mypos], mypos, lsp_index, SOS, EOS)
				# get rule number out from matched rule and put append to dic_lsp[lsp_index

				for matched_index in matched_rule:
					if matched_rule[matched_index] == -99 and matched_rule[matched_index] != -100 :
						dic_lsp[lsp_index].append(matched_index)
						if not matched_index in used:
							otech.write(str(matched_index)+",")
							used.append(matched_index)

						if not isquestion:
							isquestion = 1
							oread.write("T")

				# end 4 MYPOS rule check in 1 LSP
				# re initialise matched_rule dictionary
				reinit_matched_rule()
				if errDisplay:
					print("Valid rule cleared...")

				if errDisplay:
					for x in dic_mypos:
						print(x, dic_mypos[x])
						# print("Rule: ",s[phrase], "New LSP seq--->", dic_lsp[lsp_index])
						# print("New LSP seq--->", dic_lsp[lsp_index])
						# o.write(dic_lsp[lsp_index])
						# print(dic_lsp[lsp_index])

			# end 1 sentence check
			if not isquestion:
					oread.write("F")

	otest.close()
	oread.close()
	otech.close()

# part1-end

# part2-start = find total number of messages, questions and non questions in them

dic_fullSen = {}
dic_senTok = {}

def removePunc(line,forQuestionFile):

	line = line.lower()
	line = line.replace('.','')
	line = line.replace(',','')
	line = line.replace('‘','')
	line = line.replace('"','')
	line = line.replace('’','')
	line = line.replace('”','')
	line = line.replace('?','')
	line = line.replace('!','')
	line = line.replace('“','')
	line = line.replace(')','')
	line = line.replace('(','')
	line = line.replace('{','')
	line = line.replace('}','')
	line = line.replace(':','')
	line = line.replace('_','')
	if not forQuestionFile:
		line = line.replace('+','')
		line = line.replace('-','')
		line = line.replace('/','')
		line = line.replace('%','')
		line = line.replace('*','')
		line = line.replace('$','')
		line = line.replace('#','')
		line = line.replace('@','')
		line = line.replace('=','')

	return line

def extract_questions_only(line):
	if len(line) >= 1:
		isQuestion = line[-1]
		line = line[:-1]
		# print(isQuestion)
		line = line.split("\t",1)[1]
		# print(line)
		# if len(line) > 1:
		# 	line = line[1]
		# else:
		# 	line = line[0]
		return isQuestion, line
	else:
		return "F",""

def part2_stat_total_lable(stat_out):
	fileIn = sys.argv[1]
	fileIn = fileIn.replace("formatted","outRead")
	fileIn = open(fileIn)
	lines = [i.rstrip('\n') for i in fileIn]

	isQuestion = "F"
	question = ""
	count_msg = 0
	count_q = 0
	count_nq = 0

	for line in lines:
		count_msg += 1
		isQuestion, question = extract_questions_only(line)
		if isQuestion == "T":
			count_q += 1
		else:
			count_nq +=1
	stat_out.write("Total messages read =	" + str(count_msg) + "\n")
	stat_out.write("Total questions read =	" + str(count_q) + "\n")
	stat_out.write("Total statements read =	" + str(count_nq) + "\n\n\n\n")

	fileIn.close()

# part2-end

# part3-start = find highest occuring rules,

def part3_rule_frequency(stat_out):
	fileIn = sys.argv[1]
	fileIn = fileIn.replace("formatted","outTech")
	fileIn = open(fileIn)
	lines = [i.rstrip('\n') for i in fileIn]
	extracted_num = ""

	for key in rule:
		dic_rule_freq[key] = 0

	# for SOS and EOS
	dic_rule_freq[28] = 0
	dic_rule_freq[29] = 0

	for line in lines:
		extract = ""
		for c in reversed(line):
			if c != "\t":
				extract += c
			else:
				break
		# stat_out.write(extract+"\n")

		rule_no = ""
		for c in reversed(extract):
			# print(c, sep=' ', end='', flush=True)
			if c != "," :
				rule_no += c

			else:
				if len(rule_no) != 0:
					dic_rule_freq[int(rule_no)] += 1
					rule_no = ""
		# print()

	stat_out.write("Rule Frequency-\n")
	for key in dic_rule_freq:
		stat_out.write(str(key) + ":\t" + str(dic_rule_freq[key]) + "\n")

	stat_out.write("\n")
	fileIn.close()

# part3-end

#part4-start create questions file so server can read it and store in DB

def part4_createQuestionFile():
	fileIn = open(sys.argv[1].replace("formatted","outRead"))
	fileOut = sys.argv[1].replace("formatted","question")

	lines = [i.rstrip('\n') for i in fileIn]
	with open(fileOut,'w') as o:
		for line in lines:
			if len(line)>0:
				if line[-1] == "T":
					line = removePunc(line[:-1],1)
					if(len(line.split()) > 2):
						o.write(line+"\n")

	o.close()
	fileIn.close()

# part4-end

# part5 -starts

def part5_createTFIDF_questionFile():
	fileIn = open(sys.argv[1].replace("formatted","outRead"))
	fileOut = sys.argv[1].replace("formatted","tfidf-question")
	lines = [i.rstrip('\n') for i in fileIn]

	with open(fileOut,'a') as o:
		for line in lines:
			if len(line)>0:
				if line[-1] == "T":
					line = removePunc(line[:-1],1)
					o.write(line+"\n")

	o.close()
	fileIn.close()
	return
#part5-ends

# part6-start = get tfidf scores

def tf(word, blob):

	return blob.words.count(word) / len(blob.words)

# how many of word are there in diff docs
def word_in_total_doc(word, bloblist):
	count = 0
	for blob in bloblist:
		for i in blob.words:
			if word == i:
				count+=1
	return count

def idf(word, bloblist):
	totalLen = 0
	for blob in bloblist:
		totalLen +=len(blob.split())
	return len(bloblist) / (1+word_in_total_doc(word, bloblist))

def tfidf(word, blob, bloblist):

    return (tf(word, blob) * idf(word, bloblist))*100

def write_top_similar(arr, topn, name, dic, stat_out):
	count = 1
	# print(dic)
	for i in arr:
		key = i[0]
		stat_out.write(str(count) + ":\t" + str(i[1]) + "\n")
		key = i[0].split()
		key1 = int(key[0])
		key2 = int(key[1])
		# print(key1,key2)
		# print(dic_fullSen[int(key1)])
		# print(dic_fullSen[int(key2)])
		if name == "Token":
			stat_out.write(name + str(count) + ":\t" + str(dic[key1]) + "\n")
			stat_out.write(name + str(count) + ":\t" + str(dic[key2]) + "\n\n")
		else:
			stat_out.write(name + str(count) + ":\t" + dic[key1] + "\n")
			stat_out.write(name + str(count) + ":\t" + dic[key2] + "\n\n")

		count += 1
		if count > topn:
			return

# just get the tfidf scores and save in a file
def part6_getTFIDFscores():
	fileIn = sys.argv[1]
	fileIn = fileIn.replace("formatted","tfidf-question")
	fileIn = open(fileIn)
	bloblist = []
	lines = [i.rstrip('\n') for i in fileIn]
	for line in lines:
		line = line.split("\t",1)
		if len(line) >= 2:
			bloblist.append(tb(line[1]))

	for i,blob in enumerate(bloblist):
	    for word in blob.words:
	    	word = word.lower()
	    	if not word in dic_tfidf_word:
	    		score = tfidf(word, blob, bloblist)
	    		# print(score,word)
	    		dic_tfidf_word[word] = score

	fileout = sys.argv[1].replace("formatted","tfidf")
	with open(fileout,'w') as o:
		for word in dic_tfidf_word:
			o.write(word + "\t" + str(dic_tfidf_word[word]) + "\n")
	o.close()

	# end tfidf shit

#part6-end

def main():
	# create output file where all stats will be written
	stat_out_name = sys.argv[1]
	stat_out_name = stat_out_name.replace("formatted","stat")

	# do part 1
	part1_lable_staments()

	with open(stat_out_name,'w') as stat_out:
		# do part 2
		part2_stat_total_lable(stat_out)

		# do part 3
		part3_rule_frequency(stat_out)

		# clear memory
		dic_lsp.clear()
		dic_mypos.clear()
		matched_rule.clear()
		dic_revert.clear()
		dic_rule_freq.clear()

		#do part 4
		part4_createQuestionFile()

		# do part 5
		# this file will store all history in a suitable format. so every time new history comes in, it will
		# get appened. and tfidf scores will be recalculated using this entire file.
		part5_createTFIDF_questionFile()

	# do part 6
	# print("going to into tfidf")
	part6_getTFIDFscores()

main()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>
#
# Author: Francois Boulogne <fboulogne at sciunto dot org>, 2012

import re
from bibtexparser.bparser import BibTexParser
from bibtexparser import customization

from libcitebib.utils import uniq

def _customizations_latex(record):
    """
    This function curstumizes record for bibtex.
    See bibtexparser lib for more info.
    """
    record = customization.page_double_hyphen(record)
    record = customization.homogenize_latex_encoding(record)
    record = customization.author(record)
    return record

def _customizations_unicode(record):
    """
    This function curstumizes record for raw style.
    See bibtexparser lib for more info.
    """
    record = customization.page_double_hyphen(record)
    record = customization.convert_to_unicode(record)
    record = customization.author(record)
    return record

def _get_citations(texfilename):
    """
    Get all citations in a tex file.
    Citations may appear several times.

    :param texfilename: tex file path
    :returns: a list of citations
    """

    with open(texfilename, 'r') as tex:
        #it seems that extra spaces in cite with multiple refs
        #works with bibtex, but not recommanded as said on wikipedia

        #Catch citations
        cite = re.compile('cite(|t|p|t\*|p\*|alt|alp|alt\*|alp\*)({|\[.+?\]{)((\w|-|,|\s)+)}')  # Can contain spaces?
        allcite = []

        for line in tex:
            #handle commented lines or parts
            line = line.split('%')[0]

            #we get citations
            results = cite.findall(line)
            if results:
                # loop on all results for the line
                for el in results:
                    #sometimes, cite contains several citations
                    foo = re.sub("\s", "", str(el[2]))  # remove extra spaces
                    foo = re.split(",", foo)
                    allcite.extend(foo)
    return allcite

def get_citations(texfilenames):
    """
    Get all citations in tex files
    Citations appear only once.

    :param texfilenames: tex files path
    :returns: a list of citations
    """
    allcite = []
    for texfilename in texfilenames:
        allcite.extend(_get_citations(texfilename))
    #uniqify the list
    #A set could not be used since the order might have a sense
    allcite = uniq(allcite)
    return allcite

def get_bibtex_entries(filename, unicode=True):
    """
    Parse a bibtex file and return the content

    :param filename: bibtex filepath
    :param unicode: If True, unicode style, if False, latex style
    :returns: a dictionnary; key=ID, content=entry
    """
    if unicode:
        _customizations = _customizations_unicode
    else:
        _customizations = _customizations_latex

    with open(filename, 'r') as bibfile:
        biblio = BibTexParser(bibfile.read(), customization=_customizations)
    entries = biblio.get_entry_list()

    return biblio.get_entry_dict()


# -*- coding: utf-8 -*-
"""
FileName:   request
Author:     Tao Hao
@contact:   taohaohust@outlook.com
Created time:   2019/5/29

Description:

Changelog:
"""
from httptools import parse_url
import json
from httpserver.exceptions import InvalidUsage
from urllib.parse import parse_qs, urlunparse

try:
    from ujson import loads as json_loads
except ImportError:
    json_loads = json.loads

DEFAULT_HTTP_CONTENT_TYPE = "application/octet-stream"

class RequestParameters(dict):

    def get(self, name, default=None):
        return super(RequestParameters, self).get(name, [default])[0]

    def getlist(self, name, default=None):
        return super(RequestParameters, self).get(name, default)

class Request(object):

    def __init__(self, url_bytes, headers, version, method, connection):
        self._ip = ""
        self._port = 0
        self._remote_addr = None
        self.raw_url = url_bytes
        # _parse_url has following properties
        # - schema: bytes
        # - host: bytes
        # - port: int
        # - path: bytes
        # - query: bytes
        # - fragment: bytes
        # - userinfo: bytes
        self._parsed_url = parse_url(url_bytes)

        self.headers = headers
        self.version = version
        self.method = method
        self.connection = connection
        self.set_address_info()

        self.body = []
        self.parsed_json = None
        self.parsed_form = None
        self.parsed_args = None

    def __repr__(self):
        if self.method is None or not self.path:
            return "<{0}>".format(self.__class__.__name__)

        return "<{0}: {1} {2}>".format(
            self.__class__.__name__, self.method, self.path)

    def set_address_info(self):
        self._ip, self._port = self.connection.get_address()

    @property
    def json(self):
        if self.parsed_json is None:
            self.load_json()

        return self.parsed_json

    def load_json(self, loads=json_loads):
        try:
            self.parsed_json = loads(self.body)
        except Exception:
            if not self.body:
                return None

            raise InvalidUsage("load body json error")

        return self.parsed_json

    @property
    def args(self):
        if self.parsed_args is None:
            if self.query_string:
                self.parsed_args = RequestParameters(
                    parse_qs(self.query_string, keep_blank_values=True)
                )
            else:
                self.parsed_args = RequestParameters()

        return self.parsed_args

    @property
    def query_string(self):
        if self._parsed_url.query:
            return self._parsed_url.query.decode("utf-8")
        else:
            return ""

    @property
    def path(self):
        return self._parsed_url.path.decode("utf-8")

    @property
    def host(self):
        return self.headers.get("Host", "")

    @property
    def content_type(self):
        return self.headers.get("Content-Type", DEFAULT_HTTP_CONTENT_TYPE)

    @property
    def scheme(self):
        return "http"

    @property
    def ip(self):
        return self._ip

    @property
    def port(self):
        return self._port

    @property
    def remote_addr(self):
        if getattr(self, "_remote_addr", None) is None:
            forwarded_for = self.headers.get("X-Forwarded-For", "").split(",")
            remote_addrs = [
                addr for addr in [addr.stip() for addr in forwarded_for] if addr
            ]
            if len(remote_addrs):
                self._remote_addr = remote_addrs[0]
            else:
                self._remote_addr = ""

        return self._remote_addr

    @property
    def url(self):
        return urlunparse(
            (self.scheme, self.host, self.path, None, self.query_string, None)
        )


# word = input()
# word_len = len(word)
# reversed_word = ""
# for index in range(word_len -1, -1, -1):
#     reversed_word += word[index]
# print(reversed_word)

#######################

# word = input()
# for index in range(len(word)-1,-1,-1):
#     print(word[index], end="")
#Komentar end prosto ne pozvolqva na kursora da sleze na noviq red a ostava na syshtiq ,koeto dolepq edno do drugo simvolite , vmesto da gi svalq na nov red

######################

# word = input()
# print(word[::-1])

######################

word = input()
reversed_word = ""
for index in range(len(word)-1,-1,-1):
    reversed_word += word[index]
print(reversed_word)


#from __future__ import print_function
import numpy as np
from sklearn import neighbors
from sklearn.metrics import accuracy_score
from distance import *
from scalingdata import *
#import data  from data.txt

A = np.genfromtxt('data.txt', delimiter=',')

trainX = A[:60,0:5]
p = np.array(np.zeros(60))
for i in range (0,60):
    p[i] = np.sqrt((np.sum(A[i]*A[i])))

trainY = A[:60,5]
t = np.mean(trainX,axis = 0 )
st = np.std(trainX,axis = 0)

#trainX = scaling2(trainX,t,st)
trainX=scaling(trainX,p)
#print(trainX)
#print(trainY)
testX = A[60:80,0:5]
#testX = scaling2(testX,t,st)
testX = scaling(testX,p)
print(testX)
testY = A[60:80,5]
k = 5
p = 2
build = computing_knn(trainX,trainY,testX,k,p)

# output f1_score ,  accuracy ,presicion i

ouput(build,testY,k)

#print ( build )


speechList = {
    "npc1": {
        "0":
            "As-tu déjà rencontré Mr. Chomel ? Tu vas voir, c'est un vrai passionné !"
        ,
        "00":
            "Comme il le dit si bien: 'Si une école ne change pas la vie des gens, elle ne sert à rien !'"
    },
    "npc2": {
        "0":
            "Bonjour, et bienvenue à Hétic !"
    ,
        "00":
            "Tu as l'air épuisé, prends-donc ce café !"
    },
    "npc3": {
        "0":
            "Salut ! Je crois avoir perdu mes lunettes et je ne peux pas travailler sans elles."
        ,
        "00": {
            "question": "Peux-tu m'aider à les retrouver ?",
            "answer1": "Oui, bien sûr !",
            "answer2": "Pas le temps ..."
        },
        "001":
            "Merci, c'est gentil de m'aider !"
        ,
        "0010":
            "Il serait temps d'aller les chercher..."
        ,
        "0015":
            "Merci beaucoup, j'y vois plus clair !"
        ,
        "002":
            "Tant pis, je me débrouillerai."
        ,
        "0025":
            "Ah tu as quand même décidé de m'aider ! C'est gentil, merci."
        ,
    },
    "npc4": {
        "0":
            "Je suis en train de jouer à 'HETIC LIFE', tu connais ce jeu ?"
        ,
        "00":
            "Il a été développé par 2 Héticiens: Robin et Valentin ! C'est dingue, non ?"
    },
    "npc5": {
        "0":
            "Quelle douce musique..."
    },
    "npc6": {
        "0":
            "Moi aussi je veux jouer à Hetic Life... Malheureusement, je n'ai pas de jeton... "
        ,
        "00": {
            "question": "En aurais tu pour moi ?",
            "answer1": "Oui, bouge pas.",
            "answer2": "J'en n'ai pas, désolé."
        },
        "001":
            "Super, je t'attends ici alors."
        ,
        "002":
            "Tant pis ça sera pour une autre fois, merci quand même."
        ,
        "0020":
            "Encore toi ! T'as l'air de bien aimer cet endroit toi aussi."
        ,
        "0015":
            "Merci beaucoup ! T'es le meilleur, je vais enfin pouvoir tester ce jeu."
        ,
        "00150":
            "C'était donc vrai, ce jeu est juste génial !"
        ,
    },

}


def surfrf(udir,month,z0WIN,z0SPR,z0SUM,z0FAL):

    if month == 12 or month == 1 or month == 2:

        for i in range(len(z0WIN)):
            if i == len(z0WIN)-1:
                z0 = z0WIN[i][1]
                return z0
            elif udir >= z0WIN[i][0] and udir < z0WIN[i+1][0]:
                z0 = z0WIN[i][1]
                return z0

    elif month == 3 or month == 4 or month == 5:

        for i in range(len(z0SPR)):
            if i == len(z0SPR)-1:
                z0 = z0SPR[i][1]
                return z0
            elif udir >= z0SPR[i][0] and udir < z0SPR[i+1][0]:
                z0 = z0SPR[i][1]
                return z0

    elif month == 6 or month == 7 or month == 8:

        for i in range(len(z0SUM)):
            if i == len(z0SUM)-1:
                z0 = z0SUM[i][1]
                return z0
            elif udir >= z0SUM[i][0] and udir < z0SUM[i+1][0]:
                z0 = z0SUM[i][1]
                return z0

    elif month == 9 or month == 10 or month == 11:

        for i in range(len(z0FAL)):
            if i == len(z0FAL)-1:
                z0 = z0FAL[i][1]
                return z0
            elif udir >= z0FAL[i][0] and udir < z0FAL[i+1][0]:
                z0 = z0FAL[i][1]
                return z0

    else:
        z0 = -999
        return z0


from tkinter import *
from tkinter import messagebox

class GuiTesting:

    def __init__(self, master):
        self.master = master
        master.title('Testing Window')

        self.tLabel = Label(master, text='First TextBox')
        self.tLabel.grid(row=0, column=0, columnspan=1)
        self.text = Text(master, height=1)
        self.text.grid(row=0, column=1, columnspan=2)

        messagebox.askyesno('Would you like to continue on your last playlist')

        self.greeter = Button(master, text='Add', command=self.greet)
        self.greeter.grid(row=5, column=0, columnspan=2)

    def greet(self):
        print('you pressed a button')

#outside of class def
root = Tk()
gui = GuiTesting(root)
root.mainloop()


import wda
import time
from appium import webdriver
import threading

c = wda.Client()

# class Client(wda.Client):
#     def home_duration(self):
#         return self.http.post('/wda/homescreen')

while True:
    print(c.status())
    s = c.session('jp.co.hit-point.tabikaeru')
    print(c.healthcheck())
    time.sleep(3)
    s.swipe_right()
    s.swipe_right()
    s.tap(286.4, 464.6)
    # t1 = threading.Thread(target=call_siri)
    # t2 = threading.Thread(target=s.tap, args=(219.3, 467.6))
    # t2.start()
    # t1.start()
    break
    # try:
    #     # time.sleep(0.2)
    #     print("Home")
    # except wda.WDAError:
    #     print("Home")


import requests
import time
from selenium import webdriver
from bs4 import BeautifulSoup

class DouyuSpider(object):
    def __init__(self):
        self.base_url = "https://www.douyu.com/directory/all"
        self.driver = webdriver.Chrome()
        self.count = 0

    def parse_data(self, data):
        # 转换类型
        soup = BeautifulSoup(data, 'lxml')
        data_list = []
        # 房间名字
        room_name_list = soup.select('.DyListCover-content .DyListCover-intro')
        # 昵称
        room_nick_list = soup.select('.DyListCover-content .DyListCover-user')
        # 人气
        room_hot_list = soup.select('.DyListCover-content .DyListCover-hot')
        # 获取房间类型
        room_type_list = soup.select('.DyListCover-content .DyListCover-zone')

        for room_name, room_type, room_nick, room_hot in zip(room_name_list, room_type_list, room_nick_list,
                                                             room_hot_list):
            item = {}

            item['room_name'] = room_name.get_text().strip()
            item['room_type'] = room_type.get_text().strip()
            item['room_nick'] = room_nick.get_text().strip()
            item['room_hot'] = room_hot.get_text().strip()
            data_list.append(item)
            self.count += 1

        return data_list

    def loop_get_data(self):
        # 获取数据
        data = self.driver.page_source
        # print(data)
        data_list = []

        while True:
            if data.find('dy-Pagination-disabled dy-Pagination-next') != -1:
                break
            self.driver.find_element_by_class_name(' dy-Pagination-next').click()
            # 解析数据
            data_list.append(self.parse_data(data))

        return data_list

    def run(self):
        self.driver.get(self.base_url)

        data_list = self.loop_get_data()

        # time.sleep(3)
        # 关闭浏览器
        self.driver.quit()

        print(data_list)

        print('总共的房间:{}'.format(self.count))

if __name__ == '__main__':
    DouyuSpider().run()


def printMST(self, parent):
    print ("Edge \tWeight")
    for i in range(1,self.V):
        print (parent[i],"-",i,"\t",self.graph[i][ parent[i] ])
r = [[0, 1, 0], [4, 7, 0], [5, 7, 0], [3, 4, 1], [3, 8, 1], [0, 7, 2], [1, 9, 2], [5, 6, 3], [0, 2, 4]]

def minKey(self, key, mstSet):
    min = sys.maxsize
    for v in range(self.V):
        if key[v] < min and mstSet[v] == False:
            min = key[v]
            min_index = v

    return min_index

def prims_algorithm(self):
    return r
    key = [sys.maxint] * self.V
    parent = [None] * self.V
    key[0] = 0
    mstSet = [False] * self.V

    parent[0] = -1

    for cout in range(self.V):
        u = self.minKey(key, mstSet)

        mstSet[u] = True

    for v in range(self.V):
        if self.graph[u][v] > 0 and mstSet[v] == False and key[v] > self.graph[u][v]:
            key[v] = self.graph[u][v]
            parent[v] = u


"""nameItBackend URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.9/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from nameIt import views

urlpatterns = [
	url(r'^$', views.RootList.as_view()),
    url(r'^title/$', views.TitleList.as_view(),name='titleList'),
    url(r'^title/(?P<pk>[0-9]+)/$', views.TitleDetail.as_view(),name='titleDetail'),
    url(r'^first/$', views.FirstList.as_view(),name='firstList'),
    url(r'^first/(?P<pk>[0-9]+)/$', views.FirstDetail.as_view(),name='firstDetail'),
    url(r'^last/$', views.LastList.as_view(),name='lastList'),
    url(r'^last/(?P<pk>[0-9]+)/$', views.LastDetail.as_view(),name='lastDetail'),
    url(r'^random/$', views.RandomDetail.as_view(),name='random'),
]


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

# Класс для работы с контактами
import re

from model import Contact
from func import commonFunctions

common = commonFunctions.Common()

class ContactsHelper:

    def __init__(self, app):
        self.app = app

    contacts_cache = None

    # Выбор контакта по индексу сверхy вниз
    def select_contact_by_index(self, index):
        self.app.wd.find_elements_by_name("selected[]")[index].click()

    # Выбор контакта по индексу сверхy вниз для редактирования
    def select_contact_for_edit_by_index(self, index):
        row = self.app.wd.find_elements_by_name("entry")[index]
        cell = row.find_elements_by_tag_name("td")[7]
        cell.find_element_by_tag_name("a").click()

    # Выбор контакта по индексу сверхy вниз для просмотра
    def select_contact_for_view_by_index(self, index):
        row = self.app.wd.find_elements_by_name("entry")[index]
        cell = row.find_elements_by_tag_name("td")[6]
        cell.find_element_by_tag_name("a").click()

    # Заполнение формы редактирования
    def fill_contact_params(self, contacts):
        if contacts.firstname is not None:
            self.app.wd.find_element_by_name("firstname").click()
            self.app.wd.find_element_by_name("firstname").clear()
            self.app.wd.find_element_by_name("firstname").send_keys(contacts.firstname)
        if contacts.middlename is not None:
            self.app.wd.find_element_by_name("middlename").click()
            self.app.wd.find_element_by_name("middlename").clear()
            self.app.wd.find_element_by_name("middlename").send_keys(contacts.middlename)
        if contacts.lastname is not None:
            self.app.wd.find_element_by_name("lastname").click()
            self.app.wd.find_element_by_name("lastname").clear()
            self.app.wd.find_element_by_name("lastname").send_keys(contacts.lastname)
        if contacts.nickname is not None:
            self.app.wd.find_element_by_name("nickname").click()
            self.app.wd.find_element_by_name("nickname").clear()
            self.app.wd.find_element_by_name("nickname").send_keys(contacts.nickname)
        if contacts.bday != "" and contacts.bday is not None:
            if not self.app.wd.find_element_by_xpath(
                                    "//select[@name='bday']//option[text()='" + contacts.bday + "']").is_selected():
                self.app.wd.find_element_by_xpath(
                    "//select[@name='bday']//option[text()='" + contacts.bday + "']").click()
        if contacts.bmonth != "" and contacts.bmonth is not None:
            if not self.app.wd.find_element_by_xpath(
                                    "//select[@name='bmonth']//option[@value='" + contacts.bmonth + "']").is_selected():
                self.app.wd.find_element_by_xpath(
                    "//select[@name='bmonth']//option[@value='" + contacts.bmonth + "']").click()
        if contacts.byear is not None:
            self.app.wd.find_element_by_name("byear").click()
            self.app.wd.find_element_by_name("byear").clear()
            self.app.wd.find_element_by_name("byear").send_keys(contacts.byear)
        if contacts.aday != "" and contacts.aday is not None:
            if not self.app.wd.find_element_by_xpath(
                                    "//select[@name='aday']//option[text()='" + contacts.aday + "']").is_selected():
                self.app.wd.find_element_by_xpath(
                    "//select[@name='aday']//option[text()='" + contacts.aday + "']").click()
        if contacts.amonth != "" and contacts.amonth is not None:
            if not self.app.wd.find_element_by_xpath(
                                    "//select[@name='amonth']//option[@value='" + contacts.amonth + "']").is_selected():
                self.app.wd.find_element_by_xpath(
                    "//select[@name='amonth']//option[@value='" + contacts.amonth + "']").click()
        if contacts.ayear is not None:
            self.app.wd.find_element_by_name("ayear").click()
            self.app.wd.find_element_by_name("ayear").clear()
            self.app.wd.find_element_by_name("ayear").send_keys(contacts.ayear)
        if contacts.photo != "" and contacts.photo is not None:
            self.app.wd.find_element_by_xpath("//input[@type='file' and @name='photo']").send_keys(contacts.photo)
        if contacts.title is not None:
            self.app.wd.find_element_by_name("title").click()
            self.app.wd.find_element_by_name("title").clear()
            self.app.wd.find_element_by_name("title").send_keys(contacts.title)
        if contacts.company is not None:
            self.app.wd.find_element_by_name("company").click()
            self.app.wd.find_element_by_name("company").clear()
            self.app.wd.find_element_by_name("company").send_keys(contacts.company)
        if contacts.address is not None:
            self.app.wd.find_element_by_name("address").click()
            self.app.wd.find_element_by_name("address").clear()
            self.app.wd.find_element_by_name("address").send_keys(contacts.address)
        if contacts.home != "" and  contacts.home is not None:
            self.app.wd.find_element_by_name("home").click()
            self.app.wd.find_element_by_name("home").clear()
            self.app.wd.find_element_by_name("home").send_keys(contacts.home)
        if contacts.email != "" and  contacts.email is not None:
            self.app.wd.find_element_by_name("email").click()
            self.app.wd.find_element_by_name("email").clear()
            self.app.wd.find_element_by_name("email").send_keys(contacts.email)
        if contacts.mobile != "" and contacts.mobile is not None:
            self.app.wd.find_element_by_name("mobile").click()
            self.app.wd.find_element_by_name("mobile").clear()
            self.app.wd.find_element_by_name("mobile").send_keys(contacts.mobile)
        if contacts.work != "" and contacts.work is not None:
            self.app.wd.find_element_by_name("work").click()
            self.app.wd.find_element_by_name("work").clear()
            self.app.wd.find_element_by_name("work").send_keys(contacts.work)
        if contacts.fax is not None:
            self.app.wd.find_element_by_name("fax").click()
            self.app.wd.find_element_by_name("fax").clear()
            self.app.wd.find_element_by_name("fax").send_keys(contacts.fax)
        if contacts.email2 is not None:
            self.app.wd.find_element_by_name("email2").click()
            self.app.wd.find_element_by_name("email2").clear()
            self.app.wd.find_element_by_name("email2").send_keys(contacts.email2)
        if contacts.email3 is not None:
            self.app.wd.find_element_by_name("email3").click()
            self.app.wd.find_element_by_name("email3").clear()
            self.app.wd.find_element_by_name("email3").send_keys(contacts.email3)
        if contacts.homepage is not None:
            self.app.wd.find_element_by_name("homepage").click()
            self.app.wd.find_element_by_name("homepage").clear()
            self.app.wd.find_element_by_name("homepage").send_keys(contacts.homepage)
        if contacts.address2 is not None:
            self.app.wd.find_element_by_name("address2").click()
            self.app.wd.find_element_by_name("address2").clear()
            self.app.wd.find_element_by_name("address2").send_keys(contacts.address2)
        if contacts.phone2 != "" and contacts.phone2 is not None:
            self.app.wd.find_element_by_name("phone2").click()
            self.app.wd.find_element_by_name("phone2").clear()
            self.app.wd.find_element_by_name("phone2").send_keys(contacts.phone2)
        if contacts.notes is not None:
            self.app.wd.find_element_by_name("notes").click()
            self.app.wd.find_element_by_name("notes").clear()
            self.app.wd.find_element_by_name("notes").send_keys(contacts.notes)
        if contacts.group != "" and contacts.group is not None:
            if not self.app.wd.find_element_by_xpath(
                                    "//select[@name='new_group']//option[text() = '" + contacts.group + "']").is_selected():
                self.app.wd.find_element_by_xpath(
                    "//select[@name='new_group']//option[text() ='" + contacts.group + "']").click()

    # Добавление контакта в адресную книгу
    def addContact(self, contacts):
        self.app.wd.find_element_by_link_text("add new").click()
     #   if contacts.address is not None:
     #       self.app.wd.find_element_by_name("address").click()
     #       self.app.wd.find_element_by_name("address").clear()
     #       self.app.wd.find_element_by_name("address").send_keys(contacts.address)
     #   self.app.wd.find_element_by_xpath("//input[@type='submit' and @name='quickadd' and @value='Next']").click()
        self.fill_contact_params(contacts)
        self.app.wd.find_element_by_xpath("//input[@type='submit' and @name='submit' and @value='Enter']").click()
        self.contacts_cache = None

    # Удаление контактa по номеру в списке сверху вниз
    def delete_contact_by_index(self, index):
        self.select_contact_by_index(index)
        self.app.wd.find_element_by_xpath("//input[@type='button' and @value='Delete']").click()
        self.app.wd.switch_to_alert().accept()
        self.contacts_cache = None

    # Удаление первого сверху контакта в списке
    def delete_first_contact(self):
        self.delete_contact_by_index(0)

    # Редактирование контактa по номеру в списке сверху вниз
    def edit_contact_by_index(self, index, contacts):
      #  self.select_contact_by_index(index)
      #  self.app.wd.find_element_by_xpath("//img[@title ='Edit']").click()
        self.select_contact_for_edit_by_index(index)
        if contacts.address is not None:
            self.app.wd.find_element_by_name("address").click()
            self.app.wd.find_element_by_name("address").clear()
            self.app.wd.find_element_by_name("address").send_keys(contacts.address)
        self.fill_contact_params(contacts)
        self.app.wd.find_element_by_xpath("//input[@type='submit' and @value='Update']").click()
        self.contacts_cache = None

    # Редактирование первого сверху контакта в списке
    def edit_first_contact(self, contacts):
        self.edit_contact_by_index(0, contacts)

    # Удаление фото у первого сверху контакта в списке
    def delete_first_contact_photo(self):
        self.app.wd.find_element_by_name("selected[]").click()
        self.app.wd.find_element_by_xpath("//img[@title ='Edit']").click()
        self.app.wd.find_element_by_xpath("//input[@name='delete_photo']").click()
        self.app.wd.find_element_by_xpath("//input[@type='submit' and @value='Update']").click()

    # Редактирование первого сверху контакта в списке со страницы просмотра
    def modify_first_contact(self, contacts):
        self.app.wd.find_element_by_name("selected[]").click()
        self.app.wd.find_element_by_xpath("//img[@title ='Details']").click()
        self.app.wd.find_element_by_xpath("//input[@type='submit' and @value='Modify']").click()
        if contacts.address is not None:
            self.app.wd.find_element_by_name("address").click()
            self.app.wd.find_element_by_name("address").clear()
            self.app.wd.find_element_by_name("address").send_keys(contacts.address)
        self.fill_contact_params(contacts)
        self.app.wd.find_element_by_xpath("//input[@type='submit' and @value='Update']").click()
        self.contacts_cache = None

    # Проверка существования контактов в принципе
    def is_contact_exist(self):
          if self.app.wd.find_elements_by_name("selected[]"):
             return True
          else:
             return False

    #Список контактов
    def get_contacts_list(self):
        if self.contacts_cache is None:
            self.contacts_cache  = []
            for element in self.app.wd.find_elements_by_xpath("//tr[@name='entry']"):
             #   value = element.find_element_by_xpath("//input[@type='checkbox']").get_attribute("value")
             #   address  =  element.find_element_by_xpath("//td[4]").text
                cells = element.find_elements_by_tag_name("td")
                value = cells[0].find_element_by_tag_name("input").get_attribute("value")
                firstname = cells[2].text
                lastname = cells[1].text
                address = cells[3].text
                allmails = cells[4].text.splitlines()
                allphones = cells[5].text.splitlines()
                try:
                  home = allphones[0]
                except IndexError:
                    home = ""
                try:
                  mobile = allphones[1]
                except IndexError:
                    mobile = ""
                try:
                  work = allphones[2]
                except IndexError:
                    work = ""
                try:
                  phone2 = allphones[3]
                except IndexError:
                    phone2  = ""
                try:
                  email = allmails[0]
                except IndexError:
                    email = ""
                try:
                  email2 = allmails[1]
                except IndexError:
                    email2 = ""
                try:
                  email3 = allmails[2]
                except IndexError:
                    email3 = ""
                self.contacts_cache.append(Contact(id = value, firstname = firstname, lastname = lastname, address = address,
                                                   home = home, mobile = mobile, work = work, phone2 = phone2,
                                                   email = email, email2 = email2, email3 = email3))
        return list(self.contacts_cache)

    #Список контактов (склейка)
    def get_contacts_list_merged(self):
        if self.contacts_cache is None:
            self.contacts_cache  = []
            for element in self.app.wd.find_elements_by_xpath("//tr[@name='entry']"):
                cells = element.find_elements_by_tag_name("td")
                value = cells[0].find_element_by_tag_name("input").get_attribute("value")
                firstname = cells[2].text
                lastname = cells[1].text
                address = cells[3].text
                allmails = cells[4].text
                allphones = cells[5].text
                self.contacts_cache.append(Contact(id = value, address = address, firstname = firstname, lastname = lastname,
                                                   all_phones_from_home_page = allphones, all_emails_from_home_page = allmails))
        return list(self.contacts_cache)

    # Подсчёт кoличества контактов
    def count(self):
       return len(self.app.wd.find_elements_by_xpath("//tr[@name='entry']"))

    # Получение данных контакта из диалога редактирования (нарезка)
    def get_contact_info_from_edit_page(self, index):
       self.select_contact_for_edit_by_index(index)

       id = self.app.wd.find_element_by_xpath("//input[@name='id']").get_attribute("value")
       firstname = self.app.wd.find_element_by_xpath("//input[@name='firstname']").get_attribute("value")
       lastname = self.app.wd.find_element_by_xpath("//input[@name='lastname']").get_attribute("value")
       middlename = self.app.wd.find_element_by_xpath("//input[@name='middlename']").get_attribute("value")
     # address = self.app.wd.find_element_by_xpath("//input[@name='address']").get_attribute("value")
       address = self.app.wd.find_element_by_xpath("//textarea[@name='address']").get_attribute("value")
       home = self.app.wd.find_element_by_xpath("//input[@name='home']").get_attribute("value")
       work = self.app.wd.find_element_by_xpath("//input[@name='work']").get_attribute("value")
       phone2 = self.app.wd.find_element_by_xpath("//input[@name='phone2']").get_attribute("value")
       mobile = self.app.wd.find_element_by_xpath("//input[@name='mobile']").get_attribute("value")
       email = self.app.wd.find_element_by_xpath("//input[@name='email']").get_attribute("value")
       email2 = self.app.wd.find_element_by_xpath("//input[@name='email2']").get_attribute("value")
       email3 = self.app.wd.find_element_by_xpath("//input[@name='email3']").get_attribute("value")

       return Contact(id = id, firstname = firstname, lastname = lastname, middlename = middlename, home = home, address = address,
                      mobile = mobile, work = work, phone2 = phone2, email = email, email2 = email2, email3 = email3)

    # Получение данных контакта из диалога редактирования (нарезка)
    def get_contact_info_from_view_page(self, index):
       self.select_contact_for_view_by_index(index)

       text = self.app.wd.find_element_by_xpath("//div[@id='content']").text
       home = re.search("H: (.*)", text).group(1)
       work = re.search("W: (.*)", text).group(1)
       mobile = re.search("M: (.*)", text).group(1)
       phone2 = re.search("P: (.*)", text).group(1)

       return Contact(home = home, mobile = mobile, work = work, phone2 = phone2)

    # Поиск контакта по адресу
    def get_contact_index_by_address(self, address):
       index = -1
       i = -1

       for element in self.app.wd.find_elements_by_xpath("//tr[@name='entry']"):
           i = i + 1
           cells = element.find_elements_by_tag_name("td")
           if address == cells[3].text:
               index = i
               break
       return int(index)

    # Вспомогательный метод для обратной проверки телефонов
    def merge_phones_like_on_home_page(self, contact):

       res = "\n".join(
               map(
                   lambda x: common.clear(x, "[() -]"),[contact.home, contact.mobile, contact.work, contact.phone2]))
       return filter(
               lambda x: x !="", filter(
                       lambda x: x is not None, res))

    # Вспомогательный метод для обратной проверки email
    def merge_emails_like_on_home_page(self, contact):

        res = "\n".join([contact.email, contact.email2, contact.email3])
        return filter(
                lambda x: x !="", filter(
                        lambda x: x is not None, res))

    # Принудительная очистка кеша
    def clear_cache(self):
        self.contacts_cache = None

    # Редактирование контактa по номеру в списке сверху вниз
    def remove_contact_from_group(self, group, contact):
        self.app.wd.find_element_by_xpath(
                                    "//select[@name='group']//option[text()='" + group.name + "']").click()
        index = self.get_contact_index_by_address(contact.address)
        self.select_contact_by_index(index)
        self.app.wd.find_element_by_xpath("//input[@type='submit' and @name= 'remove']").click()

    # показать все контакты
    def show_all_contacts(self):
        self.app.wd.find_element_by_xpath(
                                    "//select[@name='group']//option[text()='[all]']").click()


from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication
from PyQt5.QtGui import QIcon

class Example(QMainWindow):
    def __init__(self):
        super().__init__()

        exit_action = QAction(QIcon('exit.png'), "&Exit", self)
        # exit_action = QAction('&Exit', self)
        exit_action.setShortcut('Ctrl+Q')
        exit_action.setStatusTip('Exit application')

        exit_action.triggered.connect(qApp.quit)

        self.statusBar()

        menubar = self.menuBar()
        fielmenu = menubar.addMenu('&File')
        fielmenu.addAction(exit_action)

        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Menubar')
        self.show()

if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


from flask import Flask, request
from flask_cors import CORS
from lang_query import query_db, query_file

app = Flask(__name__)
CORS(app)

@app.route("/langchain-db", methods=['POST'])
def call_langchaindb():
    query = request.json.get('query')
    result = query_db(query)
    return {'result': result}

@app.route("/langchain-file", methods=['POST'])
def call_langchainfile():
    query = request.json.get('query')
    result = query_file(query)
    return {'result': result}


import math
import mooda as md
from os import listdir
from os.path import isfile, join
import pandas as pd
import datetime

from service.email_service import send_to_admin
from service.ingestion_service import (post_metadata, post_vocabulary,
                                       post_data_index)
from service.fig_service import (delete_scatter, delete_platform_pie,
                                 delete_platform_availability,
                                 delete_parameter_availability, delete_map,
                                 delete_parameter_pie)

from config import (auto_upload_folder, data_index_m, data_index_m_max,
                    data_index_m_min, data_index_15d,
                    data_index_15d_max, data_index_15d_min,
                    data_index_10d, data_index_10d_max, data_index_10d_min,
                    data_index_6d, data_index_6d_max,
                    data_index_6d_min, data_index_5d, data_index_5d_max,
                    data_index_5d_min, data_index_4d, data_index_4d_max,
                    data_index_4d_min, data_index_3d, data_index_3d_max,
                    data_index_3d_min, data_index_2d, data_index_2d_max,
                    data_index_2d_min, data_index_d, data_index_d_max,
                    data_index_d_min, data_index_12h, data_index_12h_max,
                    data_index_12h_min, data_index_8h, data_index_8h_max,
                    data_index_8h_min, data_index_6h, data_index_6h_max,
                    data_index_6h_min, data_index_3h, data_index_3h_max,
                    data_index_3h_min, data_index_2h, data_index_2h_max,
                    data_index_2h_min, data_index_h, data_index_h_max,
                    data_index_h_min, data_index_r)

ingestion_raw = False
ingestion_d = False
ingestion_3d = False
ingestion_16d = False

ingestion_m = True
ingestion_m_max = True
ingestion_m_min = True
# ingestion_m_std = True

ingestion_15d = True
ingestion_15d_max = True
ingestion_15d_min = True
# ingestion_15d_std = True

ingestion_10d = True
ingestion_10d_max = True
ingestion_10d_min = True
# ingestion_10d_std = False

ingestion_6d = True
ingestion_6d_max = True
ingestion_6d_min = True
# ingestion_6d_std = False

ingestion_5d = True
ingestion_5d_max = True
ingestion_5d_min = True
# ingestion_5d_std = False

ingestion_4d = True
ingestion_4d_max = True
ingestion_4d_min = True
# ingestion_4d_std = False

ingestion_3d = True
ingestion_3d_max = True
ingestion_3d_min = True
# ingestion_3d_std = False

ingestion_2d = True
ingestion_2d_max = True
ingestion_2d_min = True
# ingestion_2d_std = False

ingestion_d = True
ingestion_d_max = True
ingestion_d_min = True
# ingestion_d_std = False

ingestion_12h = True
ingestion_12h_max = True
ingestion_12h_min = True

ingestion_8h = True
ingestion_8h_max = True
ingestion_8h_min = True

ingestion_6h = True
ingestion_6h_max = True
ingestion_6h_min = True

ingestion_3h = True
ingestion_3h_max = True
ingestion_3h_min = True

ingestion_2h = True
ingestion_2h_max = True
ingestion_2h_min = True

ingestion_h = True
ingestion_h_max = True
ingestion_h_min = True

ingestion_r = True

def resample_wf(wf_in, rule, method='mean'):
    """
    Convenience method for frequency conversion and sampling of time series of the WaterFrame
    object. Warning: if WaterFrame.data contains MultiIndex, those indexes will disappear, obtaining a single 'TIME'
    index.

    Parameters
    ----------
        rule: str
            The offset string or object representing target conversion.
            You can find all of the resample options here:
            http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases
        method: "mean", "max", "min", optional, (method = "mean")
            Save the new value with the mean(), max() or min() function.
        inplace: bool
            If True, resample in place and return 'True', If False, return a new WaterFrame.

    Returns
    -------
        new_wf: WaterFrame
    """
    if isinstance(wf_in.data.index,
        pd.MultiIndex) and 'DEPTH' in wf_in.data.index.names:

        # pd.Grouper allows you to specify a "groupby instruction for a target
        # object".
        # Ref: https://stackoverflow.com/questions/15799162/resampling-within-a-pandas-multiindex
        level_values = wf_in.data.index.get_level_values

        if method == "mean":
            data = (
                wf_in.data.groupby(
                    [level_values(0)] +  # 0 is 'DEPTH'
                    [pd.Grouper(freq=rule, level='TIME')]).mean())
        elif method == "max":
            data = (
                wf_in.data.groupby(
                    [level_values(0)] +  # 0 is 'DEPTH'
                    [pd.Grouper(freq=rule, level='TIME')]).max())
        elif method == "min":
            data = (
                wf_in.data.groupby(
                    [level_values(0)] +  # 0 is 'DEPTH'
                    [pd.Grouper(freq=rule, level='TIME')]).min())
        elif method == "std":
            data = (
                wf_in.data.groupby(
                    [level_values(0)] +  # 0 is 'DEPTH'
                    [pd.Grouper(freq=rule, level='TIME')]).std(axis=1))
    else:
        # Legacy
        if method == "mean":
            data = wf_in.data.resample(rule, level='TIME').mean()
        elif method == "max":
            data = wf_in.data.resample(rule, level='TIME').max()
        elif method == "min":
            data = wf_in.data.resample(rule, level='TIME').min()
        elif method == "std":
            data = wf_in.data.resample(rule, level='TIME').std()

    # Change "_QC" values to 0
    for key in wf_in.data.keys():
        if "_QC" in key:
            data[key] = 0

    new_wf = wf_in.copy()
    new_wf.data = data
    return new_wf

def ingestion_wf(wf_in, index_name):
    data = wf_in.data
    metadata = wf_in.metadata

    for param in wf_in.parameters:

        print(metadata['platform_code'], param)
        # send_to_admin(f'START - Ingestion nc EmodNet {one_file} - {param}',
        #             f'Start ingestion nc EmodNet {one_file} - {param}')

        data_param = data[[param, f'{param}_QC', 'TIME_QC', 'DEPTH_QC']]

        for index, row in data_param.iterrows():
            if math.isnan(row[param]):
                continue
            ingestion_data = {
                'platform_code': metadata['platform_code'],
                'parameter': param,
                # 'time': str(index[1]), df['column_name'].dt.strftime('%Y-%m-%d')
                'time': index[1].strftime("%Y-%m-%dT%H:%M:%SZ"),
                'time_qc': str(row['TIME_QC']),
                'lat': metadata['last_latitude_observation'],
                'lat_qc': '0',
                'lon': metadata['last_longitude_observation'],
                'lon_qc': '0',
                'depth': index[0],
                'depth_qc': str(row['DEPTH_QC']),
                'value': row[param],
                'qc': row[f'{param}_QC']}

            response, status_code = post_data_index(ingestion_data, index_name)
            if status_code != 201:
                send_to_admin(
                    f'ERROR {status_code} - Ingestion nc EmodNet {one_file} - {param}',
                    f'{response.get("message")}')

        delete_parameter_availability(param)

onlyfiles = [
    f for f in listdir(auto_upload_folder) if isfile(join(auto_upload_folder, f))]

# Read ingestion log
lines = []
try:
    with open("ingestion_log.txt") as file:
        lines = file.readlines()
        stripped_lines = [j.strip() for j in lines]
        lines = stripped_lines
except FileNotFoundError:
    pass

for one_file in onlyfiles:

    print(one_file)

    if f'{one_file} R' in lines:
        # This file is already ingested
        continue

    file_path = auto_upload_folder + f'/{one_file}'

    wf = md.read_nc_emodnet(file_path)

    platform_code = wf.metadata['platform_code']
    metadata = wf.metadata
    metadata['parameters'] = wf.parameters

    if 'ESTOC' in metadata['platform_code']:
        try:
            del wf.vocabulary['TIME']['QC_indicator']
        except KeyError:
            pass
        try:
            del wf.vocabulary['TIME']['QC_procedure']
        except KeyError:
            pass
        wf.data['TIME_QC'] = 0
        wf.data['DEPTH_QC'] = 0
        wf.metadata[
            'last_latitude_observation'] = wf.metadata['geospatial_lat_max']
        wf.metadata[
            'last_longitude_observation'] = wf.metadata['geospatial_lon_max']

    wf_m = wf.copy()
    wf_m.resample('M')
    if ingestion_m and f'{one_file} M' not in lines:
        # Metadata and vocabulary ingestion, only this time
        post_metadata(platform_code, metadata)
        vocabulary = wf.vocabulary.copy()
        post_vocabulary(platform_code, vocabulary)

        # Data ingestion
        ingestion_wf(wf_m, data_index_m)
        # Write file into log
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} M\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_m_max = wf.copy()
    wf_m_max.resample('M', 'max')
    if ingestion_m_max and f'{one_file} M_MAX' not in lines:
        ingestion_wf(wf_m_max, data_index_m_max)
        # Write file into log
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} M_MAX\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_m_min = wf.copy()
    wf_m_min.resample('M', 'min')
    if ingestion_m_min and f'{one_file} M_MIN' not in lines:
        ingestion_wf(wf_m_min, data_index_m_min)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} M_MIN\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_15d = wf.copy()
    wf_15d.resample('15D')
    if ingestion_15d and f'{one_file} 15D' not in lines:
        ingestion_wf(wf_15d, data_index_15d)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 15D\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_15d_max = wf.copy()
    wf_15d_max.resample('15D', 'max')
    if ingestion_15d_max and f'{one_file} 15D_MAX' not in lines:
        print('15D MAX -------------------')
        ingestion_wf(wf_15d_max, data_index_15d_max)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 15D_MAX\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_15d_min = wf.copy()
    wf_15d_min.resample('15D', 'min')
    if ingestion_15d_min and f'{one_file} 15D_MIN' not in lines:
        print('15D MIN -------------------')
        ingestion_wf(wf_15d_min, data_index_15d_min)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 15D_MIN\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_10d = wf.copy()
    wf_10d.resample('10D')
    if ingestion_10d and f'{one_file} 10D' not in lines:
        print('10D -------------------')
        ingestion_wf(wf_10d, data_index_10d)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 10D\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_10d_max = wf.copy()
    wf_10d_max.resample('10D', 'max')
    if ingestion_10d_max and f'{one_file} 10D_MAX' not in lines:
        print('10D MAX -------------------')
        ingestion_wf(wf_10d_max, data_index_10d_max)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 10D_MAX\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_10d_min = wf.copy()
    wf_10d_min.resample('10D', 'min')
    if ingestion_10d_min and f'{one_file} 10D_MIN' not in lines:
        print('10D MIN -------------------')
        ingestion_wf(wf_10d_min, data_index_10d_min)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 10D_MIN\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_6d = wf.copy()
    wf_6d.resample('6D')
    if ingestion_6d and f'{one_file} 6D' not in lines:
        print('6D -------------------')
        ingestion_wf(wf_6d, data_index_6d)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 6D\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_6d_max = wf.copy()
    wf_6d_max.resample('6D', 'max')
    if ingestion_6d_max and f'{one_file} 6D_MAX' not in lines:
        print('6D MAX -------------------')
        ingestion_wf(wf_6d_max, data_index_6d_max)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 6D_MAX\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_6d_min = wf.copy()
    wf_6d_min.resample('6D', 'min')
    if ingestion_6d_min and f'{one_file} 6D_MIN' not in lines:
        print('6D MIN -------------------')
        ingestion_wf(wf_6d_min, data_index_6d_min)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 6D_MIN\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_5d = wf.copy()
    wf_5d.resample('5D')
    if ingestion_5d and f'{one_file} 5D' not in lines:
        print('5D -------------------')
        ingestion_wf(wf_5d, data_index_5d)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 5D\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_5d_max = wf.copy()
    wf_5d_max.resample('5D', 'max')
    if ingestion_5d_max and f'{one_file} 5D_MAX' not in lines:
        print('5D MAX -------------------')
        ingestion_wf(wf_5d_max, data_index_5d_max)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 5D_MAX\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_5d_min = wf.copy()
    wf_5d_min.resample('5D', 'min')
    if ingestion_5d_min and f'{one_file} 5D_MIN' not in lines:
        print('5D MIN -------------------')
        ingestion_wf(wf_5d_min, data_index_5d_min)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 5D_MIN\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_4d = wf.copy()
    wf_4d.resample('4D')
    if ingestion_4d and f'{one_file} 4D' not in lines:
        print('4D -------------------')
        ingestion_wf(wf_4d, data_index_4d)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 4D\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_4d_max = wf.copy()
    wf_4d_max.resample('4D', 'max')
    if ingestion_4d_max and f'{one_file} 4D_MAX' not in lines:
        print('4D MAX -------------------')
        ingestion_wf(wf_4d_max, data_index_4d_max)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 4D_MAX\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_4d_min = wf.copy()
    wf_4d_min.resample('4D', 'min')
    if ingestion_4d_min and f'{one_file} 4D_MIN' not in lines:
        print('4D MIN -------------------')
        ingestion_wf(wf_4d_min, data_index_4d_min)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 4D_MIN\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_3d = wf.copy()
    wf_3d.resample('3D')
    if ingestion_3d and f'{one_file} 3D' not in lines:
        print('3D -------------------')
        ingestion_wf(wf_3d, data_index_3d)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 3D\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_3d_max = wf.copy()
    wf_3d_max.resample('3D', 'max')
    if ingestion_3d_max and f'{one_file} 3D_MAX' not in lines:
        print('3D MAX -------------------')
        ingestion_wf(wf_3d_max, data_index_3d_max)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 3D_MAX\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_3d_min = wf.copy()
    wf_3d_min.resample('3D', 'min')
    if ingestion_3d_min and f'{one_file} 3D_MIN' not in lines:
        print('3D MIN -------------------')
        ingestion_wf(wf_3d_min, data_index_3d_min)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 3D_MIN\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_2d = wf.copy()
    wf_2d.resample('2D')
    if ingestion_2d and f'{one_file} 2D' not in lines:
        print('2D -------------------')
        ingestion_wf(wf_2d, data_index_2d)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 2D\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_2d_max = wf.copy()
    wf_2d_max.resample('2D', 'max')
    if ingestion_2d_max and f'{one_file} 2D_MAX' not in lines:
        print('2D MAX -------------------')
        ingestion_wf(wf_2d_max, data_index_2d_max)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 2D_MAX\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_2d_min = wf.copy()
    wf_2d_min.resample('2D', 'min')
    if ingestion_2d_min and f'{one_file} 2D_MIN' not in lines:
        print('2D MIN -------------------')
        ingestion_wf(wf_2d_min, data_index_2d_min)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 2D_MIN\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_d = wf.copy()
    wf_d.resample('D')
    if ingestion_d and f'{one_file} D' not in lines:
        print('D -------------------')
        ingestion_wf(wf_d, data_index_d)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} D\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_d_max = wf.copy()
    wf_d_max.resample('D', 'max')
    if ingestion_d_max and f'{one_file} D_MAX' not in lines:
        print('D MAX -------------------')
        ingestion_wf(wf_d_max, data_index_d_max)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} D_MAX\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_d_min = wf.copy()
    wf_d_min.resample('D', 'min')
    if ingestion_d_min and f'{one_file} D_MIN' not in lines:
        print('D MIN -------------------')
        ingestion_wf(wf_d_min, data_index_d_min)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} D_MIN\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_12h = wf.copy()
    wf_12h.resample('12H')
    if ingestion_12h and f'{one_file} 12H' not in lines:
        print('12H -------------------')
        ingestion_wf(wf_12h, data_index_12h)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 12H\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_12h_max = wf.copy()
    wf_12h_max.resample('12H', 'max')
    if ingestion_12h_max and f'{one_file} 12H_MAX' not in lines:
        print('12H MAX -------------------')
        ingestion_wf(wf_12h_max, data_index_12h_max)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 12H_MAX\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_12h_min = wf.copy()
    wf_12h_min.resample('12H', 'min')
    if ingestion_12h_min and f'{one_file} 12H_MIN' not in lines:
        print('12H MIN -------------------')
        ingestion_wf(wf_12h_min, data_index_12h_min)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 12H_MIN\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_8h = wf.copy()
    wf_8h.resample('8H')
    if ingestion_8h and f'{one_file} 8H' not in lines:
        print('8H -------------------')
        ingestion_wf(wf_8h, data_index_8h)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 8H\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_8h_max = wf.copy()
    wf_8h_max.resample('8H', 'max')
    if ingestion_8h_max and f'{one_file} 8H_MAX' not in lines:
        print('8H MAX -------------------')
        ingestion_wf(wf_8h_max, data_index_8h_max)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 8H_MAX\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_8h_min = wf.copy()
    wf_8h_min.resample('8H', 'min')
    if ingestion_8h_min and f'{one_file} 8H_MIN' not in lines:
        print('8H MIN -------------------')
        ingestion_wf(wf_8h_min, data_index_8h_min)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 8H_MIN\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_6h = wf.copy()
    wf_6h.resample('6H')
    if ingestion_6h and f'{one_file} 6H' not in lines:
        print('6H -------------------')
        ingestion_wf(wf_6h, data_index_6h)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 6H\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_6h_max = wf.copy()
    wf_6h_max.resample('6H', 'max')
    if ingestion_6h_max and f'{one_file} 6H_MAX' not in lines:
        print('6H MAX -------------------')
        ingestion_wf(wf_6h_max, data_index_6h_max)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 6H_MAX\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_6h_min = wf.copy()
    wf_6h_min.resample('6H', 'min')
    if ingestion_6h_min and f'{one_file} 6H_MIN' not in lines:
        print('6H MIN -------------------')
        ingestion_wf(wf_6h_min, data_index_6h_min)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 6H_MIN\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_3h = wf.copy()
    wf_3h.resample('3H')
    if ingestion_3h and f'{one_file} 3H' not in lines:
        print('3H -------------------')
        ingestion_wf(wf_3h, data_index_3h)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 3H\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_3h_max = wf.copy()
    wf_3h_max.resample('3H', 'max')
    if ingestion_3h_max and f'{one_file} 3H_MAX' not in lines:
        print('3H MAX -------------------')
        ingestion_wf(wf_3h_max, data_index_3h_max)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 3H_MAX\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_3h_min = wf.copy()
    wf_3h_min.resample('3H', 'min')
    if ingestion_3h_min and f'{one_file} 3H_MIN' not in lines:
        print('3H MIN -------------------')
        ingestion_wf(wf_3h_min, data_index_3h_min)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 3H_MIN\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_2h = wf.copy()
    wf_2h.resample('2H')
    if ingestion_2h and f'{one_file} 2H' not in lines:
        print('2H -------------------')
        ingestion_wf(wf_2h, data_index_2h)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 2H\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_2h_max = wf.copy()
    wf_2h_max.resample('2H', 'max')
    if ingestion_2h_max and f'{one_file} 2H_MAX' not in lines:
        print('2H MAX -------------------')
        ingestion_wf(wf_2h_max, data_index_2h_max)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 2H_MAX\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_2h_min = wf.copy()
    wf_2h_min.resample('2H', 'min')
    if ingestion_2h_min and f'{one_file} 2H_MIN' not in lines:
        print('2H MIN -------------------')
        ingestion_wf(wf_2h_min, data_index_2h_min)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} 2H_MIN\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_h = wf.copy()
    wf_h.resample('H')
    if ingestion_h and f'{one_file} H' not in lines:
        print('H -------------------')
        ingestion_wf(wf_h, data_index_h)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} H\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_h_max = wf.copy()
    wf_h_max.resample('H', 'max')
    if ingestion_h_max and f'{one_file} H_MAX' not in lines:
        print('H MAX -------------------')
        ingestion_wf(wf_h_max, data_index_h_max)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} H_MAX\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    wf_h_min = wf.copy()
    wf_h_min.resample('H', 'min')
    if ingestion_h_min and f'{one_file} H_MIN' not in lines:
        print('H MIN -------------------')
        ingestion_wf(wf_h_min, data_index_h_min)
        with open("ingestion_log.txt", "a") as file:
            file.write(f"{one_file} H_MIN\n")
            # now = datetime.datetime.now()
            # file.write(f"{str(now)}\n")

    if ingestion_r and f'{one_file} R' not in lines:
        print('R -------------------')
        try:
            ingestion_wf(wf, data_index_r)
            with open("ingestion_log.txt", "a") as file:
                file.write(f"{one_file} R\n")
                # now = datetime.datetime.now()
                # file.write(f"{str(now)}\n")
        except:
            print('ERROR')

    # Delete figures
    delete_scatter(platform_code)
    delete_platform_pie()
    delete_platform_availability(platform_code)
    delete_map()
    delete_parameter_pie()