import csv
# import json
from matplotlib import pyplot as plt
import numpy as np
import os
from sklearn.preprocessing import MinMaxScaler
import json

all = ["Cameroon", "Chad", "Congo", "Djibouti", "Eritrea", "Eswatini", "Ethiopia", "Lesotho", "Malawi", "Mali", "Mauritania",
       "Namibia", "Niger", "Nigeria", "Rwanda", "Senegal", "Somalia", "South Africa", "Sudan", "Tanzania", "Uganda", "Zimbabwe"]

def compile_data(filename):
    data = {e:{} for e in all}
    with open('raw_data/'+filename, 'r') as csvfile:
        csvreader = csv.reader(csvfile)

        next(csvreader)

        for row in csvreader:
            entity = row[0]
            if len(row[3])==0:
                continue
            year = int(row[2])
            im = float(row[3])

            if entity in all:
                data[entity].update({year: im})

    return data

def scale(ydata):
    scaler = MinMaxScaler()
    y_values_scaled = scaler.fit_transform(np.array(ydata).reshape(-1, 1))

    y_values_scaled = y_values_scaled.flatten()
    return y_values_scaled

ys = {"ghi","mort","hdi","conflict-and-terror"}
xs = {"ghg","degraded"}

names = {"ghi":"Hunger Index",
            "mort":"Mortality",
            "infm":"Infant Mortality",
            "hdi":"Human Development Index",
            "hci":"Human Capital Index",
            "health_access":"Healthcare access/quality",
            "conflict-and-terror":"Conflict and terror",
            "deforestation":"Deforestation","cropland-area":"Cropland area","farea":"Forest area",
            "degraded":"Share of Degraded land",
            "ghg":"Greenhouse emissions","gold-prod":"Gold production"}

corrs = {}
for country in all:
    for x in xs:
        env_data = compile_data(x+'.csv')
        for y in ys:
            im_data = compile_data(y+'.csv')
            y_series = im_data[country]
            x_series = env_data[country]

            if len(y_series)<3 or len(x_series)<3:
                continue

            y_data = list(y_series.values())
            x_data = list(x_series.values())

            common_years = set(im_data[country].keys()) & set(env_data[country].keys())
            if len(common_years)>1:
                x_common = scale([env_data[country][year] for year in common_years])
                y_common = scale([im_data[country][year] for year in common_years])

                corr = np.corrcoef(x_common, y_common)[0,1]
                if abs(corr) > 0.5:
                    # print(country+": "+x+"_"+y)
                    # print(corr)
                    corrs[country+"_"+x+"_"+y] = corr

                plt.plot(y_series.keys(), scale(y_data), label=names[y])
                plt.plot(x_series.keys(), scale(x_data), label=names[x])

                plt.title(country)
                plt.ylabel('Normalised Index')
                plt.legend()
                plt.xlabel("Year")
                if not os.path.exists('plots/'+country):
                    os.makedirs('plots/'+country)
                # plt.show()
                plt.savefig("plots/"+country+"/"+y+"_"+x+".png")
                plt.clf()
    # break

# with open("plot_relation.json", "w") as outfile:
#     json.dump(corrs, outfile)

# countries = all[1:9]
# print(countries)
# print(len(countries))
# fig, axes = plt.subplots(len(countries)//3+1, 3, figsize=(10, 6*len(countries)), sharex=True)

# plt.subplots_adjust(hspace=0.5)

# for i, country in enumerate(countries):
#     y_series = im_data[country]
#     x_series = env_data[country]

#     # print("Len ",country,len(y_series),len(x_series))
#     if (len(y_series)>0):
#         axes[i//3,i%3].plot(y_series.keys(), scale(list(y_series.values())), label="Emissions")
#     if (len(x_series)>0):
#         axes[i//3,i%3].plot(x_series.keys(), scale(list(x_series.values())), label="Deforestation")

#     axes[i//3,i%3].set_title(country)
#     axes[i//3,i%3].set_ylabel('National Index')
#     # axes[i].grid(True)
#     axes[i//3,i%3].legend()

# plt.xlabel("Year")
# plt.tight_layout()

# plt.show()


import os
import sys
import requests
import random
import string
from glob import glob
from time import perf_counter
from . import process_question_with_teprolin, \
    remove_diacritics, frequent_verbs

_bing_search_url = 'https://api.bing.microsoft.com/v7.0/search?q=#QUERY#&cc=RO'
_bing_api_key = os.getenv('BING_SEARCH_V7_KEY')
_debug = True

# 1. Check for valid Bing API key
if not _bing_api_key:
    raise RuntimeError(
        "No Bing API key was found in environment variable BING_SEARCH_V7_KEY!")
# end if

# 2. Create multiprocess Bing query caches
_cache_unique_key = ''

while len(_cache_unique_key) < 8:
    _cache_unique_key += random.choice(string.ascii_lowercase + string.digits)
# end while

_bing_cache_file = os.path.join('cache', f'bing-{_cache_unique_key}.txt')
_bing_master_cache_file = os.path.join('cache', f'bing-master.txt')
_bing_cache = {}
_cache_additions = 0

def _add_cache_file(cache_file: str):
    """Adds a Bing cache file to the current cache."""

    with open(cache_file, mode='r', encoding='utf-8') as f:
        line = f.readline().strip()

        while True:
            query = line
            query_hits = []
            line = f.readline().strip()

            while line and line != '#EOR#':
                hit_dict = {}
                hit_dict['name'] = line
                hit_dict['url'] = f.readline().strip()
                hit_dict['snippet'] = f.readline().strip()
                hit_dict['date'] = f.readline().strip()
                hit_dict['rank'] = len(query_hits)
                query_hits.append(hit_dict)
                line = f.readline().strip()
            # end while

            if not line:
                break
            # end if

            _bing_cache[query] = query_hits
            line = f.readline().strip()
        # end for
    # end with
# end def

# 2.1 Load the cache for Bing queries
for f in glob(os.path.join('cache', 'bing-*.txt')):
    _add_cache_file(f)

    if not f.endswith(_bing_master_cache_file):
        print(f'Deleting previous cache file [{f}]', file=sys.stderr, flush=True)
        os.remove(f)
    # end if
# end for

def _save_cache_file(cache_file: str):
    """Saves the `_bing_cache` dict to the `cache_file` file."""

    with open(cache_file, mode='w', encoding='utf-8') as f:
        for query in sorted(list(_bing_cache.keys())):
            print(query, file=f)

            for hit_dict in _bing_cache[query]:
                print(hit_dict['name'], file=f)
                print(hit_dict['url'], file=f)
                print(hit_dict['snippet'], file=f)
                print(hit_dict['date'], file=f)
            # end for

            print('#EOR#', file=f, flush=True)
        # end for
    # end with
# end def

# 2.2 Write master file back
_save_cache_file(_bing_master_cache_file)

def _generate_query_1(tokens: list) -> str:
    """Takes a processed question and generates a web search query from it.
    For now, just choose the nouns in the question."""

    query = []

    for tid, wf, lem, ctag, head, deprel in tokens:
        if ctag == 'NOUN' or ctag == 'PROPN' or ctag == 'ADJ' or \
                ctag == 'NUM' or ctag == 'ADV' or \
                (ctag == 'VERB' and lem not in frequent_verbs):
            query.append(wf)
        # end if
    # end for

    return ' '.join(query)

def bing_search_v7(question: str, given_query: str) -> list:
    """Performs a Bing V7 search for the given question.
    If question is in cache, no search request is sent.
    If `given_query` is not empty, use it instead of generating the query.
    This is useful when testing the QA system with human generated queries."""

    if given_query:
        return _do_the_web_search(given_query)
    # end if

    # 1. Process question
    q_tokens = process_question_with_teprolin(question)

    # 2. Generate query
    q_query = _generate_query_1(q_tokens)
    ndq_query = remove_diacritics(q_query)

    if q_query == ndq_query:
        if q_query in _bing_cache:
            # Spare Bing API querying...
            return _bing_cache[q_query]
        else:
            return _do_the_web_search(q_query)
        # end if
    else:
        results = []

        if q_query in _bing_cache:
            # Spare Bing API querying...
            results.extend(_bing_cache[q_query])
        else:
            results.extend(_do_the_web_search(q_query))
        # end if

        if ndq_query in _bing_cache:
            # Spare Bing API querying...
            results.extend(_bing_cache[ndq_query])
        else:
            results.extend(_do_the_web_search(ndq_query))
        # end if

        return _remove_duplicate_hits(results)
    # end if

def _do_the_web_search(query: str) -> list:
    """Does the Bing search and returns the list of results."""
    global _cache_additions

    # 1. Populate query link
    bing_query_url = _bing_search_url.replace('#QUERY#', query)

    if _debug:
        # 2. Do the query
        print(f'Querying Bing with query [{query}]', file=sys.stderr, flush=True)
        start = perf_counter()
    # end if
    response = requests.get(bing_query_url, headers={
                            "Ocp-Apim-Subscription-Key": _bing_api_key})

    if _debug:
        elapsed = 1000 * (perf_counter() - start)
        print(f' -> took {elapsed:.3f}ms', file=sys.stderr, flush=True)
    # end if

    if response.status_code == 200:
        bing_json = response.json()

        if 'webPages' in bing_json and \
                'value' in bing_json['webPages']:
            results = []
            rank = 0

            for hit in bing_json['webPages']['value']:
                hit_dict = {'rank': rank}
                hit_dict['name'] = hit['name']
                hit_dict['url'] = hit['url']
                hit_dict['snippet'] = hit['snippet']
                hit_dict['date'] = hit['dateLastCrawled']
                results.append(hit_dict)
                rank += 1
            # end for

            _bing_cache[query] = results
            _cache_additions += 1

            if _cache_additions % 10 == 0:
                _save_cache_file(_bing_cache_file)
            # end if

            return results
    else:
        print(
            f'Error querying Bing with [{query}; error code {response.status_code}]',
            file=sys.stderr, flush=True)
        return []

def _remove_duplicate_hits(search_results: list) -> list:
    results = []
    url_set = set()

    for hit in search_results:
        if hit['url'] not in url_set:
            results.append(hit)
            url_set.add(hit['url'])
        # end if
    # end for

    return results


from scipy.misc import factorial as fac
from sys import argv
import time
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
from pyrap.tables import table
import astropy.io.fits as fits
from astropy import wcs
from pyuvdata import UVBeam
import os
import sys
import glob
import argparse
import shutil
import copy
import healpy
import scipy.stats as stats
import pytz
import datetime
import ephem
from scipy import interpolate
from astropy.time import Time
import pylab as plt
from astropy.table import Table
from scipy import interpolate
from scipy.interpolate import interp1d

def beam_interp_func(theta, phi, data):
            # convert to radians
            theta = copy.copy(theta) * np.pi / 180.0
            phi = copy.copy(phi) * np.pi / 180.0
            shape = theta.shape
            beam_interp = [healpy.get_interp_val(m, theta.ravel(), phi.ravel(), lonlat=False).reshape(shape) for m in data]
            return np.array(beam_interp)

def lookupNearest(theta_s,phi_s,theta,phi):

  index = np.where((np.abs(theta-theta_s)==np.min(np.abs(theta-theta_s))) &
                   (np.abs(phi-phi_s)==np.min(np.abs(phi-phi_s))))

  return index

# generate random sky models
RA_inner=[]
DE_inner=[]
while len(RA_inner)<100:
   RA_cen=np.random.randint(-55,55,1)
   DE_cen=np.random.randint(-35,35,1)
   r=np.sqrt(RA_cen**2+DE_cen**2)
   if r<30:
        RA_inner.append(RA_cen)
        DE_inner.append(DE_cen)

RA_outer=[]
DE_outer=[]
while len(RA_outer)<100:
   RA_cen=np.random.randint(-55,55,1)
   DE_cen=np.random.randint(-35,35,1)
   r=np.sqrt(RA_cen**2+DE_cen**2)
   if r>40:
        RA_outer.append(RA_cen)
        DE_outer.append(DE_cen)

plt.plot(RA_inner,DE_inner,'ob')
plt.plot(RA_outer,DE_outer,'or')
RA_inner=np.array(RA_inner)[:,0]
DE_inner=np.array(DE_inner)[:,0]

RA_outer=np.array(RA_outer)[:,0]
DE_outer=np.array(DE_outer)[:,0]

path_to_rand='/vault-ike/ntsikelelo/simulated_ms_files/Artificial_sky_models/20_random_distributed_sources/'
np.save(path_to_rand+'RA_cen.npy',RA_inner)
np.save(path_to_rand+'DE_cen.npy',DE_inner)
np.save(path_to_rand+'RA_cen_outer.npy',RA_outer)
np.save(path_to_rand+'DE_cen_outer.npy',DE_outer)

# pointing direction
ms = table('/home/ntsikelelo/zen.2458115.31193.xx.HH.uvR.ms.split')
fld = table('/home/ntsikelelo/zen.2458115.31193.xx.HH.uvR.ms.split'+'::FIELD')
radec0 = fld.getcol('PHASE_DIR').squeeze().reshape(1,2)
radec0 = np.tile(radec0, (60,1))
fld.close()
point_ra=np.rad2deg(radec0[0,0])
point_dec=np.rad2deg(radec0[0,1])
path_to_rand='/vault-ike/ntsikelelo/simulated_ms_files/Artificial_sky_models/20_random_distributed_sources/'

RA_cen=np.zeros(shape=(200))
DE_cen=np.zeros(RA_cen.shape)
RA_cen[0:100]=np.load(path_to_rand+'RA_cen.npy')
DE_cen[0:100]=np.load(path_to_rand+'DE_cen.npy')
RA_cen[100:200]=np.load(path_to_rand+'RA_cen_outer.npy')
DE_cen[100:200]=np.load(path_to_rand+'DE_cen_outer.npy')
DE=DE_cen+point_dec
RA=RA_cen+point_ra
flux=np.ones(DE.shape)
flux[0:100]=flux[100:200]*1e-2
flux[100:200]=flux[100:200]*1e2
spectral_index=np.ones(DE.shape)*(-0.7)

for o in range (flux.shape[0]):
    sky_model= open(path_to_rand+"model_sky_gains_30_rand_original_full.txt","w+")
    sky_model.write("#format:name ra_d dec_d i q u v emaj_d emin_d pa_d spi freq0"+"\n")
    sources=[]
    for k in range (flux.shape[0]):
        sources.append("SRC"+str(k)+" "+str(RA[k])+" "+str(DE[k])+" "+str(flux[k])+" "+"0"+" "+"0"+" "+"0"+" "+"0"+" "+"0"+" "+"0"+" "+str(-0.7)+" "+"150e6")
    for i in range(len(sources)):
        sky_model.write(sources[i]+"\n")
    sky_model.close()

#  #HERA beam
# print("Now simulating with HERA_beam")
# uvb = UVBeam()
# uvb.read_beamfits('/home/ntsikelelo/NF_HERA_power_beam_healpix128.fits')
# pol_ind = np.where(uvb.polarization_array ==-6)[0][0]
# beam_maps = np.abs(uvb.data_array[0, 0, pol_ind, :, :])
# beam_freqs = uvb.freq_array.squeeze() / 1e6
# Nbeam_freqs = len(beam_freqs)
# beam_nside = healpy.npix2nside(beam_maps.shape[1])

# theta = np.sqrt( (RA - point_ra)**2 + (DE - point_dec)**2 ) # center origin at the array dec and RA
# phi = np.arctan2((DE-point_dec), (RA-point_ra)) + np.pi

# pb = beam_interp_func(theta, phi,beam_maps)

# data_freqs=np.linspace(100,200,1024)
# Ndata_freqs = len(data_freqs)

# beam_values=np.zeros(shape=(1024, DE_cen.shape[0]))
# for source in range (pb.shape[1]):
#     pb_interp = interpolate.interp1d(beam_freqs,pb[:,source], kind='cubic')
#     beam_values[:,source]=pb_interp(data_freqs)

# gains=np.zeros((1,8,1024,DE_cen.shape[0],1), dtype=complex)
# gains[:,:,:,:,0]=np.sqrt(beam_values)
# np.save(path_to_rand+'Gains_HERA_Fagnoni_beam_full.npy', gains)

n_chan=1024
#         mutual coupling beams
beam_name=np.array(['06','22','26'])

for k in range (len(beam_name)):
    print("Now simulating beam "+beam_name[k])
    uvb = UVBeam()
    uvb.read_beamfits('/home/ntsikelelo/HERA-Beams/NF_CrossCouplingBeams/NF_HERA-Dipole_ccbeam_port'+beam_name[k]+'_E-field_phasecorrected.fits')
    uvb.peak_normalize()
    print("beam normalised")

    beam_maps = uvb.data_array[0, 0, 0, :, :, :]
    beam_freqs = uvb.freq_array.squeeze() / 1e6
    Nbeam_freqs = len(beam_freqs)
    phi_grid,theta_grid=np.meshgrid(uvb.axis1_array,uvb.axis2_array) ## phi,theta

    theta = np.sqrt( (RA_cen)**2 + (DE_cen)**2 ) # source origin at the array dec and RA
    phi = np.arctan2((DE_cen), (RA_cen)) + np.pi

    pb=np.zeros((beam_freqs.shape[0],DE_cen.shape[0]),dtype=complex)
    for source in range (len(phi)):
        index=lookupNearest(np.deg2rad(theta[source]),np.deg2rad(phi[source]),theta_grid,phi_grid)
        index=np.array(index)[:,0]
        pb[:,source]=beam_maps[:,index[0],index[1]]

        ##phase at zenith
    theta_0 = np.sqrt( (0)**2 + (0)**2 ) # center origin at the array dec and RA
    phi_0 = np.arctan2((0), (0)) + np.pi
    index_0=lookupNearest(np.deg2rad(theta_0),np.deg2rad(phi_0),theta_grid,phi_grid)
    index_0=np.array(index_0)[:,0]
    pb_0=beam_maps[:,index_0[0],index_0[1]]

    for source in range(DE_cen.shape[0]):
        pb[:,source]=pb[:,source]*(np.conjugate(pb_0)/np.abs(pb_0))

    data_freqs=np.linspace(100,200,n_chan)
    Ndata_freqs = len(data_freqs)
    beam_values=np.zeros(shape=(n_chan, DE_cen.shape[0]),dtype=complex)

    print("interpolating over freq")
    for source in range (pb.shape[1]):
        pb_interp_abs = interpolate.interp1d(beam_freqs,np.abs(pb[:,source]), kind='cubic')
        pb_interp_phase = interpolate.interp1d(beam_freqs,pb[:,source], kind='cubic')
        phase=np.angle(pb_interp_phase(data_freqs))
        amp=pb_interp_abs(data_freqs)
        beam_values[:,source]=amp*np.exp(1j*phase)

    gains=np.zeros((1,1,n_chan,DE_cen.shape[0],1), dtype=complex)
    gains[:,:,:,:,0]=beam_values
    np.save(path_to_rand+'sky_model_mutual_'+beam_name[k]+'_full.npy', gains)

HERA_beam_mutual_06=np.load(path_to_rand+'sky_model_mutual_06_full.npy')
HERA_beam_mutual_22=np.load(path_to_rand+'sky_model_mutual_22_full.npy')
HERA_beam_mutual_26=np.load(path_to_rand+'sky_model_mutual_26_full.npy')

## mutual_coupling_gain
mutual_gains=np.zeros(shape=(1,8,1024,HERA_beam_mutual_06.shape[3],1),dtype=complex)
mutual_gains[:,0,:,:,:]=HERA_beam_mutual_06
mutual_gains[:,1,:,:,:]=HERA_beam_mutual_26
mutual_gains[:,2,:,:,:]=HERA_beam_mutual_26
mutual_gains[:,3,:,:,:]=HERA_beam_mutual_22
mutual_gains[:,4,:,:,:]=HERA_beam_mutual_22
mutual_gains[:,5,:,:,:]=HERA_beam_mutual_22
mutual_gains[:,6,:,:,:]=HERA_beam_mutual_22
mutual_gains[:,7,:,:,:]=HERA_beam_mutual_22

np.save(path_to_rand+'Gains_mutual_coupling_EEC_ECC_CCC_full.npy',mutual_gains)


# -*- coding: utf-8 -*-
from maya import cmds
import os
import json

def maya_export():
    selection = cmds.ls(sl=True)
    long_names = cmds.ls(sl=True, l=True)
    if selection:
        cmds.inViewMessage( amg='<hl>Go Maya</hl> : Export Selected Objects', pos='midCenterTop', fade=True, ta=0.75, a=0.5)
        temp = __name__.split('.')#nameは自分自身のモジュール名。splitでピリオドごとに3分割。
        folderPath = os.path.join(
            os.getenv('MAYA_APP_DIR'),#Mayaのディレクトリ環境変数を取得
            'Scripting_Files',
            temp[-1]
            )
        if not os.path.exists(folderPath):
            os.makedirs(os.path.dirname(folderPath+os.sep))  # 末尾\\が必要なので注意
        print(folderPath)
        files = os.listdir(folderPath)
        sel_dict = dict()
        if files is not None:
            for file in files:
                os.remove(folderPath + os.sep + file)
        for sel, long_name in zip(selection, long_names):
            cmds.select(sel, r=True)
            name = sel.replace('|', '__Pipe__')
            cmds.file(folderPath+os.sep+name+'.ma', force=True, options="v=0", typ="mayaAscii", pr=True, es=True)
            sel_dict[name+'.ma'] = long_name
        cmds.select(selection, r=True)
        #選択ノード名を保存
        fine_name = folderPath+os.sep +'go_maya_selection_node.json'
        with open(fine_name, 'w') as f:
            json.dump(sel_dict, f)

def maya_import():
    temp = __name__.split('.')#nameは自分自身のモジュール名。splitでピリオドごとに3分割。
    folderPath = os.path.join(os.getenv('MAYA_APP_DIR'),'Scripting_Files','go')
    if not os.path.exists(folderPath):
        os.makedirs(os.path.dirname(folderPath+os.sep))  # 末尾\\が必要なので注意
    #print(folderPath)
    files = os.listdir(folderPath)
    if files is not None:
        for file in files:
            print(file)
            nameSpace = file.replace('.ma', '')
            cmds.file(folderPath+os.sep+file, i=True, typ="mayaAscii", iv=True, mnc=False, options="v=0;", pr=True)
            #重複マテリアルにファイル名が頭に付与されてしまうのを修正
            allMat = cmds.ls(mat=True)
            fileName = file.split('.')[0]
            for mat in allMat:
                if mat.startswith(fileName+'_'):
                    cmds.rename(mat, mat.replace(fileName+'_', ''))
        cmds.inViewMessage( amg='<hl>Go Maya</hl> : Imoprt objects', pos='midCenterTop', fade=True, ta=0.75, a=0.5)
    else:
        cmds.inViewMessage( amg='<hl>Go Maya</hl> : There is no exported object', pos='midCenterTop', fade=True, ta=0.75, a=0.5)


def solution(line):
    xlist = []
    ylist = []
    answer = []
    start_points=[]

    for i in range(len(line)):           # 첫번쨰 직선
        a, b, e = line[i]
        for j in range(i, len(line)):    # 두번쨰 직선
            c, d, f = line[j]
            adbc = (a*d-b*c)
            if adbc==0: continue    # 평행 or 일치
            x = (b*f-e*d)/adbc
            y = (e*c-a*f)/adbc
            if (x.is_integer() and y.is_integer()):
                start_points.append((int(x),int(y)))
                xlist.append(int(x))
                ylist.append(int(y))

    # x크기, y크기 구하기
    max_x,min_x = max(xlist),min(xlist)
    max_y,min_y = max(ylist),min(ylist)
    x_size = max_x - min_x + 1
    y_size = max_y - min_y + 1

    arr= [['.'] * x_size for _ in range(y_size)]
    for x,y in start_points:
        arr[max_y-y][x-min_x] = '*'

    return [''.join(s) for s in arr]


from __future__ import absolute_import, division, print_function

import decimal
from decimal import *
import logging
import typing
from builtins import *

import past.builtins
import xlrd
import xlwt

import canmatrix
import canmatrix.formats.xls_common

logger = logging.getLogger(__name__)
default_float_factory = decimal.Decimal

# Font Size : 8pt * 20 = 160
# font = 'font: name Arial Narrow, height 160'
font = 'font: name Verdana, height 160'

if xlwt is not None:
    sty_header = xlwt.easyxf(font + ', bold on; align: vertical center, horizontal center',
                             'pattern: pattern solid, fore-colour rose')
    sty_norm = xlwt.easyxf(font + ', colour black')
    sty_first_frame = xlwt.easyxf(font + ', colour black; borders: top thin')
    sty_white = xlwt.easyxf(font + ', colour white')

    # ECU Matrix-Styles
    sty_green = xlwt.easyxf('pattern: pattern solid, fore-colour light_green')
    sty_green_first_frame = xlwt.easyxf('pattern: pattern solid, fore-colour light_green; borders: top thin')
    sty_sender = xlwt.easyxf('pattern: pattern 0x04, fore-colour gray25')
    sty_sender_first_frame = xlwt.easyxf('pattern: pattern 0x04, fore-colour gray25; borders: top thin')
    sty_sender_green = xlwt.easyxf('pattern: pattern 0x04, fore-colour gray25, back-colour light_green')
    sty_sender_green_first_frame = xlwt.easyxf(
        'pattern: pattern 0x04, fore-colour gray25, back-colour light_green; borders: top thin')

def write_ecu_matrix(ecus, sig, frame, worksheet, row, col, first_frame):

    print("def : format - xls - write_ecu_matrix")

    # type: (typing.Sequence[str], typing.Optional[canmatrix.Signal], canmatrix.Frame, xlwt.Worksheet, int, int, xlwt.XFStyle) -> int
    # first-frame - style with borders:
    if first_frame == sty_first_frame:
        norm = sty_first_frame
        sender = sty_sender_first_frame
        norm_green = sty_green_first_frame
        sender_green = sty_sender_green_first_frame
    # consecutive-frame - style without borders:
    else:
        norm = sty_norm
        sender = sty_sender
        norm_green = sty_green
        sender_green = sty_sender_green

    # iterate over ECUs:
    for ecu_name in ecus:
        # every second ECU with other style
        if col % 2 == 0:
            loc_style = norm
            loc_style_sender = sender
        # every second ECU with other style
        else:
            loc_style = norm_green
            loc_style_sender = sender_green

        # write "s" "r" "r/s" if signal is sent, received or send and received by ECU
        if sig and ecu_name in sig.receivers and ecu_name in frame.transmitters:
            worksheet.write(row, col, label="r/s", style=loc_style_sender)
        elif sig and ecu_name in sig.receivers:
            worksheet.write(row, col, label="r", style=loc_style)
        elif ecu_name in frame.transmitters:
            worksheet.write(row, col, label="s", style=loc_style_sender)
        else:
            worksheet.write(row, col, label="", style=loc_style)
        col += 1
    # loop over ECUs ends here
    return col

def write_excel_line(worksheet, row, col, row_array, style):

    print("def : format - xls - write_excel_line")

    # type: (xlwt.Worksheet, int, int, typing.Sequence, xlwt.XFStyle) -> int
    for item in row_array:
        worksheet.write(row, col, label=item, style=style)
        col += 1
    return col

def dump(db, file, **options):

    print("def : format - xls - dump")

    # type: (canmatrix.CanMatrix, typing.IO, **typing.Any) -> None
    head_top = ['ID', 'Frame Name', 'Cycle Time [ms]', 'Launch Type', 'Launch Parameter', 'Signal Byte No.',
                'Signal Bit No.', 'Signal Name', 'Signal Function', 'Signal Length [Bit]', 'Signal Default',
                ' Signal Not Available', 'Byteorder']
    head_tail = ['Value', 'Name / Phys. Range', 'Function / Increment Unit']

    if len(options.get("additionalSignalAttributes", "")) > 0:
        additional_signal_columns = options.get("additionalSignalAttributes").split(",")  # type: typing.List[str]
    else:
        additional_signal_columns = []  # ["attributes['DisplayDecimalPlaces']"]

    if len(options.get("additionalFrameAttributes", "")) > 0:
        additional_frame_columns = options.get("additionalFrameAttributes").split(",")  # type: typing.List[str]
    else:
        additional_frame_columns = []  # ["attributes['DisplayDecimalPlaces']"]

    motorola_bit_format = options.get("xlsMotorolaBitFormat", "msbreverse")

    workbook = xlwt.Workbook(encoding='utf8')
    #    ws_name = os.path.basename(filename).replace('.xls', '')
    #    worksheet = workbook.add_sheet('K-Matrix ' + ws_name[0:22])
    worksheet = workbook.add_sheet('K-Matrix ')

    row_array = []  # type: typing.List[str]
    col = 0

    # write ECUs in first row:
    ecu_list = [ecu.name for ecu in db.ecus]

    row_array += head_top
    head_start = len(row_array)

    row_array += ecu_list
    for col in range(len(row_array)):
        worksheet.col(col).width = 1111
    tail_start = len(row_array)
    row_array += head_tail

    additional_frame_start = len(row_array)

    for col in range(tail_start, len(row_array)):
        worksheet.col(col).width = 3333

    for additionalCol in additional_frame_columns:
        row_array.append("frame." + additionalCol)
        col += 1

    for additionalCol in additional_signal_columns:
        row_array.append("signal." + additionalCol)
        col += 1

    write_excel_line(worksheet, 0, 0, row_array, sty_header)

    # set width of selected Cols
    worksheet.col(1).width = 5555
    worksheet.col(3).width = 3333
    worksheet.col(7).width = 5555
    worksheet.col(8).width = 7777
    worksheet.col(head_start).width = 1111
    worksheet.col(head_start + 1).width = 5555

    frame_hash = {}
    if db.type == canmatrix.matrix_class.CAN:
        logger.debug("Length of db.frames is %d", len(db.frames))
        for frame in db.frames:
            if frame.is_complex_multiplexed:
                logger.error("export complex multiplexers is not supported - ignoring frame %s", frame.name)
                continue
            frame_hash[int(frame.arbitration_id.id)] = frame
    else:
        frame_hash = {a.name: a for a in db.frames}

    # set row to first Frame (row = 0 is header)
    row = 1

    # iterate over the frames
    for idx in sorted(frame_hash.keys()):

        frame = frame_hash[idx]
        frame_style = sty_first_frame

        # sort signals:
        sig_hash = {"{:02d}{}".format(sig.get_startbit(), sig.name): sig for sig in frame.signals}

        # set style for first line with border
        sig_style = sty_first_frame

        additional_frame_info = [frame.attribute(frameInfo, default="") for frameInfo in additional_frame_columns]

        # iterate over signals
        row_array = []
        if len(sig_hash) == 0:  # Frames without signals
            row_array += canmatrix.formats.xls_common.get_frame_info(db, frame)
            for _ in range(5, head_start):
                row_array.append("")
            temp_col = write_excel_line(worksheet, row, 0, row_array, frame_style)
            temp_col = write_ecu_matrix(ecu_list, None, frame, worksheet, row, temp_col, frame_style)

            row_array = []
            for col in range(temp_col, additional_frame_start):
                row_array.append("")
            row_array += additional_frame_info
            for _ in additional_signal_columns:
                row_array.append("")
            write_excel_line(worksheet, row, temp_col, row_array, frame_style)
            row += 1
            continue

        # iterate over signals
        for sig_idx in sorted(sig_hash.keys()):
            sig = sig_hash[sig_idx]

            # if not first Signal in Frame, set style
            if sig_style != sty_first_frame:
                sig_style = sty_norm

            if sig.values.__len__() > 0:  # signals with value table
                val_style = sig_style
                # iterate over values in value table
                for val in sorted(sig.values.keys()):
                    row_array = canmatrix.formats.xls_common.get_frame_info(db, frame)
                    front_col = write_excel_line(worksheet, row, 0, row_array, frame_style)
                    if frame_style != sty_first_frame:
                        worksheet.row(row).level = 1

                    col = head_start
                    col = write_ecu_matrix(ecu_list, sig, frame, worksheet, row, col, frame_style)

                    # write Value
                    (frontRow, backRow) = canmatrix.formats.xls_common.get_signal(db, frame, sig, motorola_bit_format)
                    write_excel_line(worksheet, row, front_col, frontRow, sig_style)
                    backRow += additional_frame_info
                    for item in additional_signal_columns:
                        temp = getattr(sig, item, "")
                        backRow.append(temp)

                    write_excel_line(worksheet, row, col + 2, backRow, sig_style)
                    write_excel_line(worksheet, row, col, [val, sig.values[val]], val_style)

                    # no min/max here, because min/max has same col as values...
                    # next row
                    row += 1
                    # set style to normal - without border
                    sig_style = sty_white
                    frame_style = sty_white
                    val_style = sty_norm
                # loop over values ends here
            # no value table available
            else:
                row_array = canmatrix.formats.xls_common.get_frame_info(db, frame)
                front_col = write_excel_line(worksheet, row, 0, row_array, frame_style)
                if frame_style != sty_first_frame:
                    worksheet.row(row).level = 1

                col = head_start
                col = write_ecu_matrix(
                    ecu_list, sig, frame, worksheet, row, col, frame_style)
                (frontRow, backRow) = canmatrix.formats.xls_common.get_signal(db, frame, sig, motorola_bit_format)
                write_excel_line(worksheet, row, front_col, frontRow, sig_style)

                if float(sig.min) != 0 or float(sig.max) != 1.0:
                    backRow.insert(0, str("%g..%g" % (sig.min, sig.max)))  # type: ignore
                else:
                    backRow.insert(0, "")
                backRow.insert(0, "")

                backRow += additional_frame_info
                for item in additional_signal_columns:
                    temp = getattr(sig, item, "")
                    backRow.append(temp)

                write_excel_line(worksheet, row, col, backRow, sig_style)

                # next row
                row += 1
                # set style to normal - without border
                sig_style = sty_white
                frame_style = sty_white
        # loop over signals ends here
    # loop over frames ends here

    # frozen headings instead of split panes
    worksheet.set_panes_frozen(True)
    # in general, freeze after last heading row
    worksheet.set_horz_split_pos(1)
    worksheet.set_remove_splits(True)
    # save file
    workbook.save(file)

# ########################### load ###############################

def parse_value_name_column(value_name, value_str, signal_size, float_factory):

    print("def : format - xls - parse_value_name_column")

    # type: (str, str, int, typing.Callable) -> typing.Tuple
    mini = maxi = offset = None  # type: typing.Any
    value_table = dict()
    if ".." in value_name:
        (mini, maxi) = value_name.strip().split("..")
        mini = float_factory(mini)
        maxi = float_factory(maxi)
        offset = mini

    elif len(value_name) > 0:
        if len(value_str.strip()) > 0:
            # Value Table
            value = int(float(value_str))
            value_table[value] = value_name
        maxi = pow(2, signal_size) - 1
        maxi = float_factory(maxi)
        mini = 0
        offset = 0
    return mini, maxi, offset, value_table

def read_additional_signal_attributes(signal, attribute_name, attribute_value):

    print("def : format - xls - read_additional_signal_attributes")

    if not attribute_name.startswith("signal"):
        return
    if attribute_name.replace("signal.", "") in vars(signal):
        command_str = attribute_name + "="
        command_str += str(attribute_value)
        if len(str(attribute_value)) > 0:
            exec(command_str)
    else:
        pass

def load(file, **options):

    print("def : format - xls - load")

    # type: (typing.IO, **typing.Any) -> canmatrix.CanMatrix
    wb = xlrd.open_workbook(file_contents=file.read())
    sh = wb.sheet_by_index(0)
    db = canmatrix.CanMatrix()

    # Defines not imported... add by users
    # add_ecu_defines BA_DEF_ BU_
    #db.add_ecu_defines("NWM-Stationsadresse", 'HEX 0 63')
    #db.add_ecu_defines("NWM-Knoten", 'ENUM  "nein","ja"')

    # add_frame_defines BA_DEF_ BO_
    #db.add_frame_defines("GenMsgDelayTime", 'INT 0 65535')
    #db.add_frame_defines("GenMsgCycleTimeActive", 'INT 0 65535')
    #db.add_frame_defines("GenMsgNrOfRepetitions", 'INT 0 65535')
    #db.add_frame_defines("GenMsgStartValue", 'STRING')

    # add_signal_defines BA_DEF_ SG_
    #db.add_signal_defines("GenSigSNA", 'STRING')

    SignalGroupTemp = []
    FrameTemp = []

    FrameWithSignal = []

    # eval search for correct columns:
    index = {}
    for i in range(sh.ncols):
        value = sh.cell(0, i).value
        if value == "Transmitter":
            index['Transmitter'] = i
        elif "Receiver" in value:
            index['Receiver'] = i
        elif "Signal Name" in value:
            index['SignalName'] = i
        elif "Message Name" in value:
            index['MessageName'] = i
        elif "Message ID" in value:
            index['MessageID'] = i
        elif "Message Type" in value:
            index['MessageType'] = i
        elif "Period [ms]" in value:
            index['Period'] = i
        elif "DLC [Byte]" in value:
            index['DLC'] = i
        elif "MSB" in value:
            index['MSB'] = i
        elif "LSB" in value:
            index['LSB'] = i
        elif "Size [Bit]" in value:
            index['Size'] = i
        elif "Byte Order" in value:
            print("def : format - xls - load - BYTE ORDER INDEX: {} ".format(i))
            index['ByteOrder'] = i
        elif "Datatype" in value:
            index['DataType'] = i
        elif "Default Initialized Value" in value:
            index['DefaultInitializedValue'] = i
        elif "Factor" in value:
            index['Factor'] = i
        elif "Offset" in value:
            index['Offset'] = i
        elif "P-Minimum" in value:
            index['Minimum'] = i
        elif "P-Maximum" in value:
            index['Maximum'] = i
        elif "Unit" == value:
            print("def : format - xls - load - UNIT INDEX: {} ".format(i))
            index['Unit'] = i
        elif "Coding" in value:
            index['Coding'] = i
        elif "Signal Group" in value:
            index['Signal Group'] = i
        elif "Comment" in value:
            index['Comment'] = i
        # elif "Multiplexer" in value:
        #     print("def : format - xls - load - MULTIPLEXER INDEX: {} ".format(i))
        #     index['Multiplexer'] = i

    index['ECUstart'] = index['Comment'] + 1
    index['ECUend'] = sh.ncols

    # ECUs:
    for x in range(index['ECUstart'], index['ECUend']):
        print("def : format - xls - load - ECU : {}".format(sh.cell(0, x).value))
        db.add_ecu(canmatrix.Ecu(sh.cell(0, x).value))

    # initialize:
    frame_id = None
    signal_name = None

    # read xls/xlsx row by row
    for row_num in range(1, sh.nrows):
        # new signal detected
        if sh.cell(row_num, index['SignalName']).value.strip() != signal_name or \
                sh.cell(row_num, index['MessageID']).value.strip() != frame_id:

            # receiver
            receiver = [sh.cell(row_num, index['Receiver']).value]

            # signal name
            signal_name = sh.cell(row_num, index['SignalName']).value.strip()

            # MSB
            msb = int(sh.cell(row_num, index['MSB']).value)

            # LSB
            lsb = int(sh.cell(row_num, index['LSB']).value)

            # size
            signal_length = int(sh.cell(row_num, index['Size']).value)

            # cycle_time
            cycle_time = 0

            # byte order
            if index.get("ByteOrder", False):
                signal_byte_order = sh.cell(row_num, index['ByteOrder']).value

                if 'intel' in signal_byte_order:
                    is_little_endian = True
                else:
                    is_little_endian = False
            else:
                is_little_endian = True  # Default Intel

            # datatype
            is_signed = sh.cell(row_num, index['DataType']).value

            # default initialized value (not use in dbc file)
            signal_default = sh.cell(row_num, index['DefaultInitializedValue']).value

            # factor
            factor = str(sh.cell(row_num, index['Factor']).value)
            factor = decimal.Decimal(factor)

            # offset
            offset = str(sh.cell(row_num, index['Offset']).value)
            offset = decimal.Decimal(offset)

            # p-minimum
            pminimum = str(sh.cell(row_num, index['Minimum']).value)
            pminimum = decimal.Decimal(pminimum)

            # p-maximum
            pmaximum = str(sh.cell(row_num, index['Maximum']).value)
            pmaximum = decimal.Decimal(pmaximum)

            # unit
            unit = sh.cell(row_num, index['Unit']).value
            print("def : format - xls - load - unit : {} - {} - {}".format(row_num,unit,index['Unit']))

            # coding
            coding = sh.cell(row_num, index['Coding']).value
            # eval coding (value table)
            value_table = dict()
            coding = coding.replace('\n', ':').split(':')

            for i in range(len(coding)):
                if coding[i].isdigit():
                    value_table[coding[i]] = coding[i + 1]
                else:
                    pass

            # # message name
            # frame_name = sh.cell(row_num, index['MessageName']).value
            #
            # signal_group = sh.cell(row_num, index['Signal Group']).value
            # signal_group_find = False
            # # Frame Name = 0 , Signal Group Name = 1 , Signal Group ID = 2 , Signals = 3
            # if signal_group != '/':
            #     for each in SignalGroupTemp:
            #         if each[1] == signal_group:
            #             signal_group_find = True
            #             if len(each[3]) == 0:
            #                 each[3] = []
            #                 each[3].append(signal_name)
            #             else:
            #                 each[3].append(signal_name)
            #     if signal_group_find == False:
            #         SignalGroupTemp.append([frame_name,signal_group,1,[signal_name]])

            # comment
            signal_comment = sh.cell(row_num, index['Comment']).value.strip()

            # signal_multiplexer = sh.cell(row_num, index['Multiplexer']).value

            signal_multiplexer = '/'
            if signal_multiplexer == '/':

                # create Canmatrix signal and add to db
                new_signal = canmatrix.Signal(
                    name=signal_name,
                    msb=msb,
                    lsb=lsb,
                    size=int(signal_length),
                    cycle_time=cycle_time,
                    is_little_endian=is_little_endian,
                    is_signed=is_signed,
                    offset=offset,
                    factor=factor,
                    receivers=receiver,
                    unit=unit,
                    min=pminimum,
                    max=pmaximum,
                    values=value_table)

                new_signal.add_comment(signal_comment)

                print("def : format - xls - load - SIGNAL - {} - {}".format(new_signal.name,new_signal.values))
                db.add_signal(new_signal)

            else:

                if signal_multiplexer != "Multiplexor":
                    # create Canmatrix signal and add to db
                    new_signal = canmatrix.Signal(
                        name=signal_name,
                        msb=msb,
                        lsb=lsb,
                        size=int(signal_length),
                        cycle_time=cycle_time,
                        is_little_endian=is_little_endian,
                        is_signed=is_signed,
                        offset=offset,
                        factor=factor,
                        receivers=receiver,
                        unit=unit,
                        min=pminimum,
                        max=pmaximum,
                        multiplex=int(signal_multiplexer),
                        values=value_table)

                    new_signal.is_multiplexer = True
                    new_signal.add_comment(signal_comment)

                    print("def : format - xls - load - SIGNAL - {} - {}".format(new_signal.name, new_signal.values))
                    db.add_signal(new_signal)
                else:
                    # create Canmatrix signal and add to db
                    new_signal = canmatrix.Signal(
                        name=signal_name,
                        msb=msb,
                        lsb=lsb,
                        size=int(signal_length),
                        cycle_time=cycle_time,
                        is_little_endian=is_little_endian,
                        is_signed=is_signed,
                        offset=offset,
                        factor=factor,
                        receivers=receiver,
                        unit=unit,
                        min=pminimum,
                        max=pmaximum,
                        values=value_table)

                    new_signal.is_multiplexer = True
                    new_signal.multiplex = 'Multiplexor'
                    new_signal.add_comment(signal_comment)

                    print("def : format - xls - load - SIGNAL - {} - {}".format(new_signal.name, new_signal.values))
                    db.add_signal(new_signal)

            # Frame information for new signal

            # message name
            frame_name = sh.cell(row_num, index['MessageName']).value

            signal_group = sh.cell(row_num, index['Signal Group']).value
            signal_group_find = False
            # Frame Name = 0 , Signal Group Name = 1 , Signal Group ID = 2 , Signals = 3
            if signal_group != '/':
                for each in SignalGroupTemp:
                    if each[1] == signal_group:
                        signal_group_find = True
                        if len(each[3]) == 0:
                            each[3] = []
                            each[3].append(new_signal)
                        else:
                            each[3].append(new_signal)
                if signal_group_find == False:
                    SignalGroupTemp.append([frame_name,signal_group,1,[new_signal]])

            # message id
            frame_id = sh.cell(row_num, index['MessageID']).value

            # period (follow signal period)
            cycle_time = int(sh.cell(row_num, index['Period']).value)
            # dlc
            dlc = int(sh.cell(row_num, index['DLC']).value)

            # transmitter
            transmitter = [sh.cell(row_num, index['Transmitter']).value]

            # create Canmatrix frame
            new_frame: object = canmatrix.Frame(frame_name, size=dlc)

            new_frame.cycle_time = cycle_time

            # eval transmitter
            new_frame.transmitters = transmitter

            # eval message ID
            if frame_id.endswith("xh"):
                new_frame.arbitration_id = canmatrix.ArbitrationId(int(frame_id[:-2], 16), extended=True)
            else:
                new_frame.arbitration_id = canmatrix.ArbitrationId(int(frame_id[2:], 16), extended=False)

            if sh.cell(row_num, index['MessageType']).value == "EXT":
                #frame.arbitration_id.id = (int(temp.group(1)) - 0x80000000)
                new_frame.arbitration_id.extended = True

            # add frame information to signal
            new_signal.frames = new_frame
            FrameTemp.append(new_frame)

            if signal_name != '/':
                FrameWithSignal.append(new_frame)

    # read rows values loop end

    # add vframeformat
    for signal in db.signals:
        signal.frames.set_fd_type()

    SkipUsedFrame = []
    for each in  SignalGroupTemp:
        #print("def : format - xls - load - SIGNAL GROUP - START - {}".format(each[0]))
        #print("def : format - xls - load - SIGNAL GROUP - START - ",db.signals)
        for each_frame in FrameTemp:
            #print("def : format - xls - load - SIGNAL GROUP - SCAN FRAME - {} - {}".format(each[0],each_frame.name))
            if each_frame.name == each[0] and each_frame.name not in SkipUsedFrame:
                SkipUsedFrame.append(each_frame.name)
                print("def : format - xls - load - SIGNAL GROUP - SCAN FRAME - {} - {}".format(each[1], each[0]))
                each_frame.add_signal_group(each[1],1,each[3])
                print("def : format - xls - load - SIGNAL GROUP - ", each[3])

            #print("def : format - xls - load - SIGNAL GROUP - {}".format(each_frame))
            #each_frame.add_signal_group("A",1,["Hello"])

    #WRITE DEF
    # sh_def = wb.sheet_by_index(1)
    #
    # # eval search for correct columns:
    # index = {}
    # for i in range(sh_def.ncols):
    #     value = sh_def.cell(0, i).value
    #     if value == "DEF TYPE":
    #         index['DEFTYPE'] = i
    #     elif "VALUE" in value:
    #         index['VALUE'] = i
    #     elif "TYPE" in value:
    #         index['TYPE'] = i
    #     elif "MIN" in value:
    #         index['MIN'] = i
    #     elif "MAX" in value:
    #         index['MAX'] = i
    #     elif "DEFAULT" in value:
    #         index['DEFAULT'] = i
    #
    # for row_num in range(1, sh_def.nrows):
    #
    #     #print("DEF ROW : {} - {} - {}".format(sh_def.cell(row_num, index['DEFTYPE']).value,sh_def.cell(row_num, index['VALUE']).value,sh_def.cell(row_num, index['TYPE']).value))
    #
    #     if sh_def.cell(row_num, index['DEFTYPE']).value == "GLO DEF":
    #         #print("DEF ROW - G : {} {} ".format(sh_def.cell(row_num, index['VALUE']).value,sh_def.cell(row_num, index['TYPE']).value))
    #
    #         if sh_def.cell(row_num, index['TYPE']).value == "HEX":
    #
    #             #print("DEF ROW - G : {} {} {}".format(sh_def.cell(row_num, index['VALUE']).value,int(sh_def.cell(row_num, index['MIN']).value),int(sh_def.cell(row_num, index['MAX']).value)))
    #             db.add_global_defines(sh_def.cell(row_num, index['VALUE']).value,"{} {} {}".format(sh_def.cell(row_num, index['TYPE']).value,str(int(sh_def.cell(row_num, index['MIN']).value)),str(int(sh_def.cell(row_num, index['MAX']).value))))
    #
    #         elif sh_def.cell(row_num, index['TYPE']).value == "INT":
    #             #print("DEF ROW - G - INT : {} - {}".format(sh_def.cell(row_num, index['VALUE']).value,int(sh_def.cell(row_num, index['DEFAULT']).value)))
    #             db.add_global_defines(sh_def.cell(row_num, index['VALUE']).value,"{} {} {}".format(sh_def.cell(row_num, index['TYPE']).value,str(int(sh_def.cell(row_num, index['MIN']).value)),str(int(sh_def.cell(row_num, index['MAX']).value))))
    #         elif sh_def.cell(row_num, index['TYPE']).value == "STRING":
    #             print("DEF ROW - G - STRING : {} - {}".format(sh_def.cell(row_num, index['VALUE']).value,sh_def.cell(row_num, index['DEFAULT']).value))
    #             db.add_global_defines(sh_def.cell(row_num, index['VALUE']).value, sh_def.cell(row_num, index['TYPE']).value)
    #
    #         if sh_def.cell(row_num, index['DEFAULT']).value != '/':
    #             print("DEF ROW - TRACE : {} - {} ".format(sh_def.cell(row_num, index['VALUE']).value,sh_def.cell(row_num, index['DEFAULT']).value))
    #             #db.add_define_default(sh_def.cell(row_num, index['VALUE']).value,sh_def.cell(row_num, index['DEFAULT']).value)
    #         else:
    #             db.add_define_default(sh_def.cell(row_num, index['VALUE']).value,"")
    #
    #
    #     elif sh_def.cell(row_num, index['DEFTYPE']).value == "ECU DEF":
    #
    #         if sh_def.cell(row_num, index['TYPE']).value == "INT":
    #             db.add_ecu_defines(sh_def.cell(row_num, index['VALUE']).value,"{} {} {}".format(sh_def.cell(row_num, index['TYPE']).value,int(sh_def.cell(row_num, index['MIN']).value),int(sh_def.cell(row_num, index['MAX']).value)))
    #             # if sh_def.cell(row_num, index['DEFAULT']).value != '/':
    #             #     db.add_define_default(sh_def.cell(row_num, index['VALUE']).value,"0")
    #             # else:
    #             #     db.add_define_default(sh_def.cell(row_num, index['VALUE']).value,"")
    #
    #         elif sh_def.cell(row_num, index['TYPE']).value == "HEX":
    #
    #             db.add_ecu_defines(sh_def.cell(row_num, index['VALUE']).value,"{} {} {}".format(sh_def.cell(row_num, index['TYPE']).value,int(sh_def.cell(row_num, index['MIN']).value),int(sh_def.cell(row_num, index['MAX']).value)))
    #
    #         elif sh_def.cell(row_num, index['TYPE']).value == "ENUM":
    #
    #             db.add_ecu_defines(sh_def.cell(row_num, index['VALUE']).value,"{} {}".format(sh_def.cell(row_num, index['TYPE']).value,sh_def.cell(row_num, index['MIN']).value))
    #
    #         else:
    #             db.add_ecu_defines(sh_def.cell(row_num, index['VALUE']).value,sh_def.cell(row_num, index['TYPE']).value)
    #
    #
    #         if sh_def.cell(row_num, index['DEFAULT']).value != '/':
    #
    #             #print("DEBUG : {}".format(sh_def.cell(row_num, index['DEFAULT']).value))
    #             db.add_define_default(sh_def.cell(row_num, index['VALUE']).value,str(int(sh_def.cell(row_num, index['DEFAULT']).value)))
    #         else:
    #             db.add_define_default(sh_def.cell(row_num, index['VALUE']).value,"")
    #
    #     elif sh_def.cell(row_num, index['DEFTYPE']).value == "ENV DEF":
    #
    #         if sh_def.cell(row_num, index['TYPE']).value == "ENUM":
    #
    #             db.add_env_defines(sh_def.cell(row_num, index['VALUE']).value,"{} {}".format(sh_def.cell(row_num, index['TYPE']).value,sh_def.cell(row_num, index['MIN']).value))
    #
    #         elif sh_def.cell(row_num, index['TYPE']).value == "INT":
    #
    #             db.add_env_defines(sh_def.cell(row_num, index['VALUE']).value,
    #                                "{} {} {}".format(sh_def.cell(row_num, index['TYPE']).value,
    #                                               int(sh_def.cell(row_num, index['MIN']).value),
    #                                                  int(sh_def.cell(row_num, index['MAX']).value)))
    #
    #         elif sh_def.cell(row_num, index['TYPE']).value == "STRING":
    #
    #             db.add_env_defines(sh_def.cell(row_num, index['VALUE']).value,sh_def.cell(row_num, index['TYPE']).value)
    #
    #     elif sh_def.cell(row_num, index['DEFTYPE']).value == "FRM DEF":
    #
    #         if sh_def.cell(row_num, index['TYPE']).value == "INT":
    #
    #             db.add_frame_defines(sh_def.cell(row_num, index['VALUE']).value,"{} {} {}".format(sh_def.cell(row_num, index['TYPE']).value,int(sh_def.cell(row_num, index['MIN']).value),int(sh_def.cell(row_num, index['MAX']).value)))
    #
    #         elif sh_def.cell(row_num, index['TYPE']).value == "HEX":
    #             pass
    #         elif sh_def.cell(row_num, index['TYPE']).value == "STRING":
    #
    #             db.add_frame_defines(sh_def.cell(row_num, index['VALUE']).value,sh_def.cell(row_num, index['TYPE']).value)
    #
    #         elif sh_def.cell(row_num, index['TYPE']).value == "FLOAT":
    #             db.add_frame_defines(sh_def.cell(row_num, index['VALUE']).value,
    #                                  "{} {} {}".format(sh_def.cell(row_num, index['TYPE']).value,
    #                                                    sh_def.cell(row_num, index['MIN']).value,
    #                                                    sh_def.cell(row_num, index['MAX']).value))
    #
    #         elif sh_def.cell(row_num, index['TYPE']).value == "ENUM":
    #
    #             db.add_frame_defines(sh_def.cell(row_num, index['VALUE']).value,"{} {}".format(sh_def.cell(row_num, index['TYPE']).value,sh_def.cell(row_num, index['MIN']).value))
    #
    #     elif sh_def.cell(row_num, index['DEFTYPE']).value == "SIG DEF":
    #
    #         if sh_def.cell(row_num, index['TYPE']).value == "INT":
    #
    #             db.add_signal_defines(sh_def.cell(row_num, index['VALUE']).value,"{} {} {}".format(sh_def.cell(row_num, index['TYPE']).value,int(sh_def.cell(row_num, index['MIN']).value),int(sh_def.cell(row_num, index['MAX']).value)))
    #
    #         elif sh_def.cell(row_num, index['TYPE']).value == "HEX":
    #             pass
    #         elif sh_def.cell(row_num, index['TYPE']).value == "STRING":
    #
    #             db.add_signal_defines(sh_def.cell(row_num, index['VALUE']).value,sh_def.cell(row_num, index['TYPE']).value)
    #
    #         elif sh_def.cell(row_num, index['TYPE']).value == "FLOAT":
    #             db.add_signal_defines(sh_def.cell(row_num, index['VALUE']).value,"{} {} {}".format(sh_def.cell(row_num, index['TYPE']).value,int(sh_def.cell(row_num, index['MIN']).value),int(sh_def.cell(row_num, index['MAX']).value)))
    #
    #         elif sh_def.cell(row_num, index['TYPE']).value == "ENUM":
    #             #print("DEBUG : SIG STRING ENUM : {}".format(sh_def.cell(row_num, index['MIN']).value))
    #             db.add_signal_defines(sh_def.cell(row_num, index['VALUE']).value,"{} {}".format(sh_def.cell(row_num, index['TYPE']).value,sh_def.cell(row_num, index['MIN']).value))
    #
    # #WRITE BA_DEF_DEF_
    #
    # for row_num in range(1, sh_def.nrows):
    #     if sh_def.cell(row_num, index['DEFTYPE']).value == "BA_DEF_DEF_":
    #         print("def : format - xls - load - BA_DEF_DEF_ : {} - {} ".format(sh_def.cell(row_num, index['VALUE']).value,sh_def.cell(row_num, index['DEFAULT']).value))
    #         if sh_def.cell(row_num, index['DEFAULT']).value == "/":
    #             #print("def : format - dbc - load - BA_DEF_DEF_ : {} - {} ".format(sh_def.cell(row_num, index['VALUE']).value,''))
    #             db.add_define_default(sh_def.cell(row_num, index['VALUE']).value,"")
    #         else:
    #             db.add_define_default(sh_def.cell(row_num, index['VALUE']).value,sh_def.cell(row_num, index['DEFAULT']).value)
    #
    # #WRITE ATTR
    # sh_attr = wb.sheet_by_index(2)
    #
    # # eval search for correct columns:
    # index = {}
    # for i in range(sh_attr.ncols):
    #     value = sh_attr.cell(0, i).value
    #     if value == "P1":
    #         index['P1'] = i
    #     elif "P2" in value:
    #         index['P2'] = i
    #     elif "P3" in value:
    #         index['P3'] = i
    #     elif "P4" in value:
    #         index['P4'] = i
    #     elif "P5" in value:
    #         index['P5'] = i
    #
    # for row_num in range(1, sh_attr.nrows):
    #
    #     if sh_attr.cell(row_num, index['P2']).value == "BU_":
    #         db.ecu_by_name(sh_attr.cell(row_num, index['P3']).value).add_attribute(
    #             sh_attr.cell(row_num, index['P1']).value,
    #             sh_attr.cell(row_num, index['P4']).value)
    #
    #     elif sh_attr.cell(row_num, index['P2']).value == "BO_":
    #
    #         #for each in FrameTemp:
    #         #for each in FrameWithSignal:
    #         for each in FrameTemp:
    #             # print("DEBUG : ADD FRAME ATTR - {} - {}".format(each.arbitration_id.id,
    #             #
    #             #
    #             #                                                sh_attr.cell(row_num, index['P3']).value))
    #             if each.arbitration_id.extended == False:
    #                 if str(each.arbitration_id.id) == sh_attr.cell(row_num, index['P3']).value:
    #                     # if sh_attr.cell(row_num, index['P1']).value != "GenMsgILSupport" or sh_attr.cell(row_num, index['P1']).value != "Diag_Response":
    #
    #                     print("DEBUG : ADD STD FRAME ATTR - {} - {} - {}".format(each.name,sh_attr.cell(row_num, index['P1']).value,sh_attr.cell(row_num, index['P4']).value))
    #                     if sh_attr.cell(row_num, index['P4']).value == "/":
    #                         each.add_attribute(sh_attr.cell(row_num, index['P1']).value,"")
    #                     else:
    #                         each.add_attribute(sh_attr.cell(row_num, index['P1']).value,sh_attr.cell(row_num, index['P4']).value)
    #             else:
    #                 if str(each.arbitration_id.id + 0x80000000) == sh_attr.cell(row_num, index['P3']).value:
    #                     # if sh_attr.cell(row_num, index['P1']).value != "GenMsgILSupport" or sh_attr.cell(row_num, index['P1']).value != "Diag_Response":
    #
    #                     print("DEBUG : ADD EXT FRAME ATTR - {} - {} - {}".format(each.name,
    #                                                                          sh_attr.cell(row_num, index['P1']).value,
    #                                                                          sh_attr.cell(row_num, index['P4']).value))
    #                     if sh_attr.cell(row_num, index['P4']).value == "/":
    #                         each.add_attribute(sh_attr.cell(row_num, index['P1']).value, "")
    #                     else:
    #                         each.add_attribute(sh_attr.cell(row_num, index['P1']).value,
    #                                            sh_attr.cell(row_num, index['P4']).value)
    #
    #         # get_frame_by_id(canmatrix.ArbitrationId.from_compound_integer(sh_attr.cell(row_num, index['P3']).value)).add_attribute(
    #         #     sh_attr.cell(row_num, index['P1']).value,
    #         #     sh_attr.cell(row_num, index['P4']).value)
    #
    #     elif sh_attr.cell(row_num, index['P2']).value == "SG_":
    #         FrameIdandSignal = (sh_attr.cell(row_num, index['P3']).value).split(" ")
    #         print("DEBUG : ADD SIGNAL ATTR - BA_ SG_ - {} - {}".format(FrameIdandSignal[0],FrameIdandSignal[1]))
    #         for signal in db.signals:
    #             if signal.name == FrameIdandSignal[1] and signal.frames.arbitration_id.id == int(FrameIdandSignal[0]):
    #                 print("DEBUG : ADD SIGNAL ATTR - BA_ SG_ - {} ; {}".format(sh_attr.cell(row_num, index['P1']).value,sh_attr.cell(row_num, index['P4']).value))
    #                 signal.add_attribute(sh_attr.cell(row_num, index['P1']).value,sh_attr.cell(row_num, index['P4']).value)
    #
    #     elif sh_attr.cell(row_num, index['P2']).value == "GLO":
    #         db.add_attribute(sh_attr.cell(row_num, index['P1']).value, sh_attr.cell(row_num, index['P4']).value)
    #
    #     elif sh_attr.cell(row_num, index['P2']).value == "SIG_VALTYPE_":
    #         for signal in db.signals:
    #             if signal.name == sh_attr.cell(row_num, index['P4']).value and signal.frames.arbitration_id.id == int(sh_attr.cell(row_num, index['P3']).value):
    #                 signal.is_float = True

    # ecu-attributes:
    # for ecu in db.ecus:
    #     for attrib, val in sorted(ecu.attributes.items()):
    #
    #         print("def : format - dbc - dump - ECU ATTRIBUTE : [{} , {}]".format(attrib, val))

            # f.write(
            #     create_attribute_string(attrib, "BU_", ecu.name, val, db.ecu_defines[attrib].type == "STRING").encode(
            #         dbc_export_encoding, ignore_encoding_errors))

    # # global-attributes:
    # for attrib, val in sorted(db.attributes.items()):
    #
    #     print("def : format - dbc - dump - GLOBAL ATTRIBUTE : [{} , {}]".format(attrib, val))
    #
    #     f.write(create_attribute_string(attrib, "", "", val, db.global_defines[attrib].type == "STRING").encode(
    #         dbc_export_encoding, ignore_encoding_errors))
    #
    # # messages-attributes:
    # curr_frame = None
    # for signal in db.signals:
    #     frame = signal.frames
    #     if frame.name != curr_frame:
    #         for attrib, val in sorted(frame.attributes.items()):
    #
    #             print("def : format - dbc - dump - MESSAGE ATTRIBUTE : [{} , {}]".format(attrib,val))
    #
    #             f.write(create_attribute_string(attrib, "BO_", str(frame.arbitration_id.to_compound_integer()), val,
    #                                             db.frame_defines[attrib].type == "STRING").encode(dbc_export_encoding,
    #                                                                                ignore_encoding_errors))
    #         curr_frame = frame.name
    #
    # # signal-attributes:
    # for signal in db.signals:
    #     frame = signal.frames
    #     for attrib, val in sorted(signal.attributes.items()):
    #         name = output_names[frame][signal]
    #         if isinstance(val, float):
    #             val = format_float(val)
    #         if attrib in db.signal_defines:
    #
    #             print("def : format - dbc - dump - SIGNAL ATTRIBUTE : {}".format(attrib))
    #
    #             f.write(create_attribute_string(
    #                 attrib, "SG_", '%d ' % frame.arbitration_id.to_compound_integer() + name, val,
    #                                db.signal_defines[attrib].type == "STRING").encode(dbc_export_encoding,
    #                                                                                ignore_encoding_errors))
    #
    # # environment-attributes:
    # for env_var_name, env_var in db.env_vars.items():
    #     if "attributes" in env_var:
    #         for attribute, value in env_var["attributes"].items():
    #
    #             print("def : format - dbc - dump - ENVIRONMENT ATTRIBUTE : [{} , {}]".format(attrib, val))
    #
    #             f.write(create_attribute_string(attribute, "EV_", "", value,
    #                                             db.env_defines[attribute].type == "STRING")
    #                     .encode(dbc_export_encoding, ignore_encoding_errors))

    return db


import numpy as np

class Model(object):
    '''
    Class for model objects.

    Attributes:
        train_X(ndarray): X training dataset.
        train_Y(ndarray): Y training dataset.
        neural_net(Object): Neural network class to be initialized later.
        train_accuracy(float): Accuracy for the train dataset.
        errors(ndarray): Errors in test dataset.
        test_Y_pred(ndarray): Y predictions for test dataset.
        test_accuracy(float): Accuracy for the test dataset.
        unit_test(dict): Unit test results dictionary.

    '''

    def __init__(self, train_path, test_path, unit_test=False):
        '''

        Args:
            train_path(str): Training data file path.
            test_path(str): Testing data file path.
            unit_test(boolean): Adds a dictionary for unit test results if True. Otherwise None.
        '''

        self.train_X_orig, self.train_Y, self.test_X_orig, self.test_Y, self.classes = self.load_data(train_path,
                                                                                test_path)
        self.train_X, self.test_X = self.transform_data()
        self.neural_net = None
        self.train_accuracy = None
        self.errors = None
        self.test_Y_pred = None
        self.test_accuracy = None
        self.unit_test = {} if unit_test else None

    def load_data(self, train_path, test_path):
        '''
        Loads the data given the paths.

        Args:
            train_path(str): Training data file path.
            test_path(str): Testing data file path.

        Returns:

        '''
        return

    def transform_data(self):
        return

    def show_errors(self):
        '''

        Returns:
            ndarray: all errors where the prediction is different from the label.

        '''
        X = self.test_X_orig
        Y = self.test_Y
        Y_pred = self.test_Y_pred
        classes = self.classes

        wrong = Y != Y_pred
        wrong = np.squeeze(wrong)

        self.errors = wrong

    def train(self, NN, L, activations, lambd, keep_prob, learning_rate, xavier, num_epochs, mini_batch_size, seed,
              gradient_check,
              print_cost=True, **kwargs):
        '''
        Trains the neural network using the training data.

        Args:
            NN(Object): Neural network class object {NN, NNTF}.
            L(list): List of nodes in each of the layers including the input and output lauer.
            activations(list):  List of activations in the different layers {'relu', 'sigmoid', 'leaky-relu'}.
            lambd(float): If not None or 0, you will get L2 regularization with L2 penalty.
            keep_prob(float): If less than 1.0, dropout regularization will be implemented.
            learning_rate(float): Learning rate.
            xavier(boolean): True for Xavier initialization otherwise random initialization.
            num_epochs(int): Number of iterations.
            mini_batch_size(int): Mini-batch size.
            seed(int): Ramdom number generator seed.
            gradient_check(boolean): Switches off dropout to allow checking gradient with a numerical check.
            print_cost(boolean): True to print cost as you train.

        '''
        X = self.train_X
        Y = self.train_Y
        self.neural_net = NN(L, activations)

        J = self.neural_net.fit(X, Y, lambd, keep_prob, learning_rate, xavier=xavier,
                                num_epochs=num_epochs, mini_batch_size=mini_batch_size, seed=seed,
                                gradient_check=gradient_check, print_cost=print_cost, **kwargs)
        if type(self.unit_test) == dict:
            self.unit_test['J'] = J

        return

    def predict_train(self):
        '''
        Calculates the prediction and accuracy from the training data.

        '''
        X = self.train_X
        Y = self.train_Y

        _, self.train_accuracy = self.neural_net.predict(X, Y)

        if type(self.unit_test) == dict:
            self.unit_test['train'] = self.train_accuracy

    def predict_test(self):
        '''
        Calculates the prediction and accuracy from the test data.

        '''
        X = self.test_X
        Y = self.test_Y

        self.test_Y_pred, self.test_accuracy = self.neural_net.predict(X, Y)

        if type(self.unit_test) == dict:
            self.unit_test['test'] = self.test_accuracy

    def gradient_check(self, lambd, print_gradients=False):
        self.unit_test['grad_diff'] = self.neural_net.gradient_check(self.train_X, self.train_Y, lambd,
                                                                     print_gradients=print_gradients)


from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from .forms import VideoFileForm
from .models import Categories
from django.core.files.storage import FileSystemStorage
from django.contrib import messages
from django.conf import settings
import uuid
import io
from moviepy.editor import *
from google.cloud import language_v1, speech
from .words import programming_words

# Create your views here.

# Instantiates a client
client = language_v1.LanguageServiceClient()
speech_client = speech.SpeechClient()

# separates video and audio in video file
def strip_video(video_file):
    filename = os.path.join(settings.MEDIA_ROOT, 'audio') + '/' + str(uuid.uuid4()) + '.wav' # 'audio/' + str(uuid.uuid4()) + '.wav'
    audio = AudioFileClip(video_file)
    audio.write_audiofile(filename, fps=16000, bitrate='256k', nbytes=2, codec='pcm_s16le')
    return filename

# returns transcription of audio file
def transcribe(audio_file):
    with io.open(audio_file, 'rb') as a:
        content = a.read()
        audio = speech.RecognitionAudio(content=content)

    config = speech.RecognitionConfig(
        encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
        sample_rate_hertz=16000,
        language_code='en-US',
        audio_channel_count=2,
    )

    response = speech_client.recognize(config=config, audio=audio)

    transcript = ""

    for result in response.results:
        transcript += result.alternatives[0].transcript
    print(transcript)
    return transcript

# incorporates functions above to analyze key words (wrapper function)
def find_key_words(video_file):
    audio_file = strip_video(video_file)
    text = transcribe(audio_file)

    document = language_v1.Document(content=text, type_=language_v1.Document.Type.PLAIN_TEXT)
    response = client.analyze_entities(document=document, encoding_type='UTF32')

    entity_list = []
    for entity in response.entities:
        for word in programming_words:
            if entity.name.lower() == word.lower():
                entity_list.append(entity.name)
    print(entity_list)
    return entity_list

# Use information to find programming words
# programming words are classified as beginner, intermediate, and advanced
# Also consider level if person asks question about the topic because that means they don't know what it is.

def place_categories(user, words):
    for word in words:
        category = Categories.objects.filter(tag=word, user=user)
        if category.count() == 0:
            c = Categories(tag=word, user=user)
            c.save()

@login_required
def add_category(request):
    form = VideoFileForm()
    if request.method == 'POST':
        form = VideoFileForm(request.POST, request.FILES)
        if form.is_valid():
            # save video file
            file = request.FILES['video']
            file_name = str(uuid.uuid4())
            file_location = os.path.join(settings.MEDIA_ROOT, 'videos')
            fs = FileSystemStorage(location=file_location)
            fs.save(file_name, file)

            # analyze video file
            key_words = find_key_words(os.path.join(file_location, file_name))

            # create categories
            # this will probably be more complicated in the future
            place_categories(request.user, key_words)

            messages.success(request, 'Your file has been uploaded and analyzed. You will see tags shortly.')
    return render(request, 'categories/add_category.html', {'form': form})

@login_required
def show_tags(request):
    tags = Categories.objects.filter(user=request.user)
    return render(request, 'categories/show_categories.html', {'tags': tags})


#!/usr/bin/python

# Python Standard Library Imports
import time

# External Imports
pass

# Custom Imports
from pycomms import PyComms

# ===========================================================================
# Adafruit BMP085 Class (slightly modified)
# ===========================================================================

class BMP085:
    i2c = None

    # Operating Modes
    __BMP085_ULTRALOWPOWER     = 0
    __BMP085_STANDARD          = 1
    __BMP085_HIGHRES           = 2
    __BMP085_ULTRAHIGHRES      = 3

    # BMP085 Registers
    __BMP085_CAL_AC1           = 0xAA  # R   Calibration data (16 bits)
    __BMP085_CAL_AC2           = 0xAC  # R   Calibration data (16 bits)
    __BMP085_CAL_AC3           = 0xAE  # R   Calibration data (16 bits)
    __BMP085_CAL_AC4           = 0xB0  # R   Calibration data (16 bits)
    __BMP085_CAL_AC5           = 0xB2  # R   Calibration data (16 bits)
    __BMP085_CAL_AC6           = 0xB4  # R   Calibration data (16 bits)
    __BMP085_CAL_B1            = 0xB6  # R   Calibration data (16 bits)
    __BMP085_CAL_B2            = 0xB8  # R   Calibration data (16 bits)
    __BMP085_CAL_MB            = 0xBA  # R   Calibration data (16 bits)
    __BMP085_CAL_MC            = 0xBC  # R   Calibration data (16 bits)
    __BMP085_CAL_MD            = 0xBE  # R   Calibration data (16 bits)
    __BMP085_CONTROL           = 0xF4
    __BMP085_TEMPDATA          = 0xF6
    __BMP085_PRESSUREDATA      = 0xF6
    __BMP085_READTEMPCMD       = 0x2E
    __BMP085_READPRESSURECMD   = 0x34

    # Private Fields
    _cal_AC1 = 0
    _cal_AC2 = 0
    _cal_AC3 = 0
    _cal_AC4 = 0
    _cal_AC5 = 0
    _cal_AC6 = 0
    _cal_B1 = 0
    _cal_B2 = 0
    _cal_MB = 0
    _cal_MC = 0
    _cal_MD = 0

    def __init__(self, address = 0x77, mode = 3):
        self.i2c = PyComms(address)
        self.address = address

        # Make sure the specified mode is in the appropriate range
        if ((mode < 0) | (mode > 3)):
            self.mode = self.__BMP085_STANDARD
        else:
            self.mode = mode

        # Read the calibration data
        self.readCalibrationData()

    def readCalibrationData(self):
        # Reads the calibration data from the IC
        self._cal_AC1 = self.i2c.readS16(self.__BMP085_CAL_AC1)   # INT16
        self._cal_AC2 = self.i2c.readS16(self.__BMP085_CAL_AC2)   # INT16
        self._cal_AC3 = self.i2c.readS16(self.__BMP085_CAL_AC3)   # INT16
        self._cal_AC4 = self.i2c.readU16(self.__BMP085_CAL_AC4)   # UINT16
        self._cal_AC5 = self.i2c.readU16(self.__BMP085_CAL_AC5)   # UINT16
        self._cal_AC6 = self.i2c.readU16(self.__BMP085_CAL_AC6)   # UINT16
        self._cal_B1 = self.i2c.readS16(self.__BMP085_CAL_B1)     # INT16
        self._cal_B2 = self.i2c.readS16(self.__BMP085_CAL_B2)     # INT16
        self._cal_MB = self.i2c.readS16(self.__BMP085_CAL_MB)     # INT16
        self._cal_MC = self.i2c.readS16(self.__BMP085_CAL_MC)     # INT16
        self._cal_MD = self.i2c.readS16(self.__BMP085_CAL_MD)     # INT16

    def readRawTemp(self):
        # Reads the raw (uncompensated) temperature from the sensor
        self.i2c.write8(self.__BMP085_CONTROL, self.__BMP085_READTEMPCMD)
        time.sleep(0.005)  # Wait 5ms
        raw = self.i2c.readU16(self.__BMP085_TEMPDATA)

        return raw

    def readRawPressure(self):
        # Reads the raw (uncompensated) pressure level from the sensor
        self.i2c.write8(self.__BMP085_CONTROL, self.__BMP085_READPRESSURECMD + (self.mode << 6))
        if (self.mode == self.__BMP085_ULTRALOWPOWER):
            time.sleep(0.005)
        elif (self.mode == self.__BMP085_HIGHRES):
            time.sleep(0.014)
        elif (self.mode == self.__BMP085_ULTRAHIGHRES):
            time.sleep(0.026)
        else:
          time.sleep(0.008)

        msb = self.i2c.readU8(self.__BMP085_PRESSUREDATA)
        lsb = self.i2c.readU8(self.__BMP085_PRESSUREDATA + 1)
        xlsb = self.i2c.readU8(self.__BMP085_PRESSUREDATA + 2)

        raw = ((msb << 16) + (lsb << 8) + xlsb) >> (8 - self.mode)

        return raw

    def readTemperature(self):
        # Gets the compensated temperature in degrees celcius
        UT = 0
        X1 = 0
        X2 = 0
        B5 = 0
        temp = 0.0

        # Read raw temp before aligning it with the calibration values
        UT = self.readRawTemp()
        X1 = ((UT - self._cal_AC6) * self._cal_AC5) >> 15
        X2 = (self._cal_MC << 11) / (X1 + self._cal_MD)
        B5 = X1 + X2
        temp = ((B5 + 8) >> 4) / 10.0

        return temp

    def readPressure(self):
        # Gets the compensated pressure in pascal
        UT = 0
        UP = 0
        B3 = 0
        B5 = 0
        B6 = 0
        X1 = 0
        X2 = 0
        X3 = 0
        p = 0
        B4 = 0
        B7 = 0

        UT = self.readRawTemp()
        UP = self.readRawPressure()

        # True Temperature Calculations
        X1 = ((UT - self._cal_AC6) * self._cal_AC5) >> 15
        X2 = (self._cal_MC << 11) / (X1 + self._cal_MD)
        B5 = X1 + X2

        # Pressure Calculations
        B6 = B5 - 4000
        X1 = (self._cal_B2 * (B6 * B6) >> 12) >> 11
        X2 = (self._cal_AC2 * B6) >> 11
        X3 = X1 + X2
        B3 = (((self._cal_AC1 * 4 + X3) << self.mode) + 2) / 4

        X1 = (self._cal_AC3 * B6) >> 13
        X2 = (self._cal_B1 * ((B6 * B6) >> 12)) >> 16
        X3 = ((X1 + X2) + 2) >> 2
        B4 = (self._cal_AC4 * (X3 + 32768)) >> 15
        B7 = (UP - B3) * (50000 >> self.mode)

        if (B7 < 0x80000000):
            p = (B7 * 2) / B4
        else:
            p = (B7 / B4) * 2

        X1 = (p >> 8) * (p >> 8)
        X1 = (X1 * 3038) >> 16
        X2 = (-7375 * p) >> 16

        p = p + ((X1 + X2 + 3791) >> 4)

        return p

    def readAltitude(self, seaLevelPressure = 101325):
        # Calculates the altitude in meters
        altitude = 0.0
        pressure = float(self.readPressure())
        altitude = 44330.0 * (1.0 - pow(pressure / seaLevelPressure, 0.1903))

        return altitude
        return 0


import informe

def costo_camion(nombre_archivo):
    suma = 0
    rows = informe.leer_camion(nombre_archivo)
    for n_row, row in enumerate(rows):
        try:
            suma += row.cajones * row.precio
        # Esto atrapa errores en los int() y float() de arriba.
        except ValueError:
            print(f'Fila {n_row}: No pude interpretar: {row}')
    print(suma)

costo_camion('../Data/camion.csv')

# informe.informe_camion('../Data/camion.csv', '../Data/precios.csv')


# lab6afile.txt
inputFile = input("enter the valid file name ")

N= int(input("Enter N value "))

filedata=open(inputFile,'r')
linesLists  = filedata.readlines()
print("the following lines are the first ",N," lines of the text file")

for textline in (linesLists[:N]):
    print(textline)

word = input("Enter a word : ")
cnt = 0

for textline in linesLists:
    cnt += textline.count(word)
print("The word ",word," appears", cnt, "times in the file")

filedata.close()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Use iterative parsing to process the map file and
find out not only what tags are there, but also how many, to get the
feeling on how much of which data is expected within the map.

The output should be a dictionary with the tag name as the key
and number of times this tag can be encountered in the map as value.
"""
import xml.etree.ElementTree as ET
import pprint

#OSMFILE = "../example.osm"
OSMFILE = "../example_sf.osm"
#OSMFILE = "../san-francisco.osm"

def count_tags(filename):
    """Returns dictionary of different tags and corresponding count of how
    many times that tag was seen"""
    xml_dict = {}
    context = ET.iterparse( filename )
    for event, elem in context:
        if elem.tag in xml_dict:
            xml_dict[elem.tag] = xml_dict[elem.tag] + 1
        else:
            xml_dict.update({elem.tag : 1})
    return xml_dict

def test():

    tags = count_tags(OSMFILE)
    pprint.pprint(tags)

if __name__ == "__main__":
    test()


from pyird.image.pattern_model import median_XY_profile
import numpy as np
import pkg_resources
from pyird.utils import irdstream
import pathlib
import matplotlib.pyplot as plt
from pyird.image.channel import image_to_channel_cube, channel_cube_to_image, eopixel_split, eopixel_combine
from pyird.image.bias import bias_subtract_image
from pyird.image.hotpix import identify_hotpix
import astropy.io.fits as pyf

# hotpixel mask
datadir = pathlib.Path('/home/kawahara/pyird/data/dark/')
anadir = pathlib.Path('/home/kawahara/pyird/data/dark/')
dark = irdstream.Stream2D('targets', datadir, anadir)
dark.fitsid = [41018]
for data in dark.rawpath:
    im = pyf.open(str(data))[0].data
im_subbias = bias_subtract_image(im)
hotpix_mask, obj = identify_hotpix(im_subbias)

# Load data
mode = 'faint'
if mode == 'dark':
    datadir = pathlib.Path('/home/kawahara/pyird/data/dark/')
    anadir = pathlib.Path('/home/kawahara/pyird/data/dark/')
    target = irdstream.Stream2D('targets', datadir, anadir)
    target.fitsid = [41018]
elif mode == 'faint':
    datadir = pathlib.Path('/home/kawahara/pyird/data/samples/REACH/')
    anadir = pathlib.Path('/home/kawahara/pyird/data/samples/REACH/')
    target = irdstream.Stream2D('targets', datadir, anadir)
    # target.fitsid=[47103]
    target.fitsid = [47077]
else:
    print('no mode')
    import sys
    sys.exit()

# Load an image
for datapath in target.rawpath:
    im = pyf.open(str(datapath))[0].data

# image for calibration
calim = np.copy(im)

# aperture
if mode == 'faint':
    from pyird.io.iraf_trace import read_trace_file
    pathC = (pkg_resources.resource_filename('pyird', 'data/samples/aprefC'))
    path_c = (pkg_resources.resource_filename('pyird', 'data/samples/apref_c'))
    y0, interp_function, xmin, xmax, coeff = read_trace_file([pathC, path_c])

    from pyird.image.mask import trace
    from pyird.image.trace_function import trace_legendre
    mask = trace(im, trace_legendre, y0, xmin, xmax, coeff)
    calim[mask] = np.nan
    calim[hotpix_mask] = np.nan

#############################################################
# REMOVAL CODE

model_im = median_XY_profile(calim)
corrected_im = im-model_im
print(np.sum(corrected_im))

###########################################
fig = plt.figure(figsize=(8, 4))
ax1 = fig.add_subplot(131)
if mode == 'dark':
    vmax = -8.0
    vmin = -15.0
elif mode == 'faint':
    vmax = 10.0
    vmin = -15.0

cc = ax1.imshow(im, vmin=vmin, vmax=vmax)
plt.colorbar(cc, shrink=0.55)
ax1.set_title('Raw image')

ax2 = fig.add_subplot(132)
cc = ax2.imshow(model_im, vmin=vmin, vmax=vmax)
plt.colorbar(cc, shrink=0.55)
ax2.set_title('Model image')

ax3 = fig.add_subplot(133)
cc = ax3.imshow(corrected_im, vmin=-3.0, vmax=4.0)
plt.colorbar(cc, shrink=0.55)
ax3.set_title('pattern corrected')
plt.savefig('pattern_correction.png')
plt.show()


from django.conf import settings
import django

from dota2Private.settings import DATABASES, INSTALLED_APPS
settings.configure(DATABASES=DATABASES, INSTALLED_APPS=INSTALLED_APPS)
django.setup()

import time
from schedule import Scheduler
import threading
from privateApp.database_fetcher import getGamesDota2Api, getmatchesDetailsStratz
from privateApp.dabase_writer import add_to_database
from nordvpn_switcher import rotate_VPN
from django.db import connection

SECRET_KEY = 'django-insecure-uf0*ru4@iah!v($!i-(fm$4vqjd$x=!p^qeliqm$v5%n6=j4*u'

from privateApp import fetch_and_write
from privateApp.models import *
run = 0

def fetch_and_write():

    global run

    try:
        Match.objects.all().first()
    except Exception:
        connected = False
        print("database connection failed")
        connection.close()
        cursor = connection.cursor()
    else:
        connected = True
    if not run ==429 and connected == True:
        with open(r'privateApp/lastSequence.txt') as f:
            maxid = f.readline
            maxid =maxid()
        allIds=[]
        last_seq = 0
        for i in range(0,9):
            resd2, matchIds, temp_last_seq = getGamesDota2Api(maxid)
            if resd2 ==200:
                allIds+=matchIds
                last_seq = temp_last_seq
                maxid=temp_last_seq
            elif i > 2:
                break
            else:
                time.sleep(2)

        print("Found "+str(len(allIds))+" matches")
        if not len(allIds) == 0:
            print(str(allIds[len(allIds)-1]))
            chunks = [allIds[x:x+100] for x in range(0, len(allIds), 100)]
            success=True
            for chunk in chunks:
                resstratz, data = getmatchesDetailsStratz(chunk,maxid)
                run = resstratz
                if resstratz==200:
                    add_to_database(data)

                else:
                    success=False
                    break

            if success and last_seq!=0:
                try:
                    with open('privateApp/lastSequence.txt', "w") as myfile:
                        myfile.write(last_seq)
                except Exception as e:
                    print('File error main loop '+ str(e))
                    try:
                        with open('privateApp/lastSequence.txt', "w") as myfile:
                            myfile.write(last_seq)
                    except Exception as e:
                        print('File error main loop '+ str(e))
        elif resd2 != 200:
            time.sleep(5)

    else:
        if(run==429):
            rotate_VPN()
        time.sleep(30)
        run = 200

while True:
    fetch_and_write()
    time.sleep(1)


import copy
import os
import shutil

class ProjectConf:
    def __init__(self, **dvargs):
        self.dico = dvargs
        self.original_file = None
        self.backup_file = None

    def set_folder(self, folder_path):
        self.original_file = f"{folder_path}/project-conf.h"
        if os.path.exists(self.original_file):
            self.backup_file = f"{folder_path}/project-conf.h.back"
        else:
            self.backup_file = None

    def update_file(self):
        if self.original_file == None:
            return

        lines = []
        last_endif = -1

        if os.path.exists(self.original_file):
            shutil.copy(self.original_file,self.backup_file)
            with open(self.original_file,'r') as f:
                i = 0
                for line in f:
                    if line != "" and line.endswith("#endif"):
                        last_endif = i
                    lines.append(line.strip())
                    i+=1
        else:
            lines = [
                "#ifndef __PROJECT_CONF_H__",
                "#define __PROJECT_CONF_H__",
                "#endif"]
            last_line = -1

        redefines = []
        for name, value in self.dico.items():
            redefines.append(f"#undef {name}")
            if type(value) == str and not value.isupper():
                value = f"\"{value}\""
            redefines.append(f"#define {name} {value}")

        lines = lines[:last_endif] + redefines + lines[last_endif:]

        try:
            with open(self.original_file, "w") as output:
                for line in lines:
                    output.write(f"{line}\n")
        except Exception as e:
            raise Exception("Problem occured while updating 'project-conf.h' file")

    def __setitem__(self, key, value):
        self.dico[key] = value

    def __getitem__(self, key):
        return self.dico[key]

    def __str__(self):
        return str(self.dico)

    def restore_file(self):
        if(self.backup_file != None):
            shutil.copy(self.backup_file, self.original_file)
            os.remove(self.backup_file)
        else:
            os.remove(self.original_file)


import sys
input = sys.stdin.readline

N, K = map(int, input().split())
arr = [int(input()) for _ in range(N)]

cnt = 0
for i in reversed(arr):
    cnt += K // i
    K %= i
print(cnt)


from django.conf.urls import url, include
from django.contrib import admin

from .views import Home, Base, Team, Landingpage
from videos.views import VideoListView
from videos.views import (
    VideoDetailView ,
    VideoCreateView ,
    VideoUpdateView ,
    VideoDeleteView ,
)

from .views import UsercreateView , UserCreateDoneView

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^accounts/', include('django.contrib.auth.urls')),
    url(r'^accounts/register/$', UsercreateView.as_view(), name='register'),
    url(r'^accounts/register/done', UserCreateDoneView.as_view(), name='register_done' ),
    url(r'^$', Home.as_view()),
    url(r'^base/$', Base.as_view()),
    url(r'^team/$', Team.as_view()),
    url(r'^videos/$', VideoListView.as_view(), name='videos'),
    url(r'^videos/(?P<pk>\d+)/$', VideoDetailView.as_view(), name='video-detail'),
    url(r'^videos/create/$', VideoCreateView.as_view(), name='video-create'),
    url(r'^videos/(?P<pk>\d+)/update/$', VideoUpdateView.as_view(), name='video-update'),
    url(r'^videos/(?P<pk>\d+)/delete/$', VideoDeleteView.as_view(), name='video-delete'),
    url(r'^index/$', Landingpage.as_view(), name='Landingpage'),
    url(r'^photo/', include('photo.urls', namespace='photo')),
]


import os
import subprocess
import collections
import re

# private
def d2nt(dictionary):
    return collections.namedtuple('metadata', dictionary.keys())(**dictionary)

def export_env(key, value):
    with open(os.getenv("GITHUB_ENV"), "a") as f:
        f.write("%s=%s\n" % (key, value))

class Repo(object):
    def __init__(self, name, url, branch_rx=None, extraction_rx=None):
        print("[Repo Object] Initializing repo %s with URL %s…" % (name, url))
        self.name = name
        self.url = url
        self.commit = None
        self.branch_rx = branch_rx
        self.extraction_rx = extraction_rx

        self._latest_commit = None
        self._branches = None
        self._tags = None

    @property
    def latest_commit(self):
        if self._latest_commit is None:
            print("[Repo Object] Fetching latest commit for %s…" % self.name)
            p = subprocess.check_output([
                "git",
                "ls-remote",
                self.url
            ]).decode("utf8")
            for line in p.split("\n"):
                if "HEAD" in line:
                    self._latest_commit = line[:40]
        return self._latest_commit

    @property
    def branches(self):
        if self._branches is None:
            print("[Repo Object] Fetching branches for %s…" % self.name)
            p = subprocess.check_output([
                "git", "ls-remote", "--heads", self.url
            ]).decode("utf8")
            branches = []
            for line in p.split("\n"):
                if line.strip() == "":
                    continue

                match = line.split()

                hash = match[0]
                name = match[1]

                branches.append((hash, name))
            self._branches = branches
        return self._branches

    @property
    def tags(self):
        if self._tags is None:
            print("[Repo Object] Fetching tags for %s…" % self.name)
            p = subprocess.check_output([
                "git", "ls-remote", "--tags", "--sort=v:refname",
                self.url
            ]).decode("utf8")

            tags = []
            for line in p.split("\n"):
                if line.strip() == "":
                    continue

                match = line.split()

                hash = match[0]
                name = match[1].split("/")[2]

                tags.append((hash, name))
            self._tags = tags
        return self._tags

    # Helper Functions
    def match_branch(self, line):
        match = re.match(self.branch_rx, line)
        if match is not None:
            return match[1]

    def match_line(self, line):
        match = re.match(self.extraction_rx, line)
        if match is not None:
            return match[1]

    def out_of_date(self):
        return self.commit != self.latest_commit

origin = os.getenv("REPO_URL")
repo = Repo("Openlane", origin)

# public
gh = d2nt({
    "origin": origin,
    "branch": os.getenv("BRANCH_NAME"),
    "image": os.getenv("IMAGE_NAME"),
    "root": os.getenv("GITHUB_WORKSPACE"),
    "pdk": os.getenv("PDK_ROOT"),
    "tool": os.getenv("TOOL"),
    "event": d2nt({
        "name": os.getenv("GITHUB_EVENT_NAME")
    }),
    "export_env": export_env,
    "Repo": Repo,
    "openlane": repo
})


import os
import re
import math

#REQUIREMENT: Dictionary
#REQUIREMENT: List
#REQUIREMENT: Keyword Search
#REQUIREMENT: Add One (or more) Objects
#REQUIREMENT: One (or more) Modules
#REQIUREMENT: Instance Data Methods & Members (like static fields)

#Tracing input file steps from instructions, note: this is not copying, right? ;3
fin = open( './/base.pov', 'r+' )
sdl = fin.read() #read the entire file into a string
fin.close() #POV source code stored as string sdl

#ANINATION PART I
#2piR/time to create animation length (loop counter)
locationVector = re.compile(r'location\s*<[^>]*>')
count = 0
while count < 10:
    # Camera Math - Radius, Cosin, Sin - R x Sin = Y vaule | R x Cosin = X value
    R = 5
    vX = R * math.cos(10) + count /2
    vY = R * math.sin(10) + count /2
    vZ = count /2
    #REQUIREMENT: Tuple
    new_string = 'location<{0},{1},{2}>'.format(vX, vY, vZ)
    # replace the results for the camera line in the code
    sdl = locationVector.sub(new_string, sdl)
    #output the new file, append numeric incrimentation
    outfile = 'tmp' + str(count) + '.pov'
    fout = open( outfile, 'w' )
    fout.write( sdl )
    fout.close()
    count +=1

#Export to movie maker
print('Encoding Movie')
os.system('ffmpeg -start_number 0 -i tmp%01d.png -c:v libx264 -r 30 -pix_fmt yuv420p movie.avi' )

#ANIMATION PART II
#Duplicate the cone, move its x axis slowly to make the percption of dilation
# coneVector = re.compile(r'cone.*\n.*\n.*')
# count2 = 0
# while count2 < 50:
#     vx1 = vx1 + 0.01
#     vy1 = vy1 + 0.01
#     vz1 = vz1 + 0.01
#     vx2 = vx2 + 0.01
#     vy2 = vy2 + 0.01
#     vz2 = vz2 + 0.01
#     other_string = 'cone <{0}, {1}. {2}>, 0 <{3}, {4}, {5}>, 0.5'.format(vx1, vy1, vz1, vx2, vy2, vz2)
#     sdl = coneVector.sub(other_string, sdl)
#     #output the new file, append numeric incrimentation
#     outfile = 'tmp' + str(count2) + '.pov'
#     fout = open( outfile, 'w' )
#     fout.write( sdl )
#     fout.close()
#     count +=1
#
# while count2 > 50 < 100:
#     vx1 -= 0.01
#     vy1 -= 0.01
#     vz1 -= 0.01
#     vx2 -= 0.01
#     vy2 -= 0.01
#     vz2 -= 0.01
#     other_string = 'cone <{0}, {1}. {2}>, 0 <{3}, {4}, {5}>, 0.5'.format(vx1, vy1, vz1, vx2, vy2, vz2)
#     sdl = coneVector.sub(other_string, sdl)
#     #output the new file, append numeric incrimentation
#     outfile = 'tmp' + str(count2) + '.pov'
#     fout = open( outfile, 'w' )
#     fout.write( sdl )
#     fout.close()
#     count +=1


# leetcode-字谜分组.py
# 给定一个字符串数组，将字母异位词组合在一起。字母异位词指字母相同，但排列不同的字符串。

# 示例:

# 输入: ["eat", "tea", "tan", "ate", "nat", "bat"],
# 输出:
# [
#   ["ate","eat","tea"],
#   ["nat","tan"],
#   ["bat"]
# ]
# 说明：

# 所有输入均为小写字母。
# 不考虑答案输出的顺序。

"""
思路:
长度,~集合~,,count

en ..
想到了排序整个strs
但没有想到排序单个str呢...

看清关键再哪喽..

解答即参考...

"""
from collections import defaultdict
class Solution(object):
    def groupAnagrams(self, strs):
        """
        :type strs: List[str]
        :rtype: List[List[str]]
        """

        # 参考
        sorted_words = defaultdict(list)

        for word in strs:
            sorted_list = [c for c in word]
            sorted_list.sort()
            sorted_word = "".join(sorted_list)

            sorted_words[sorted_word].append(word)

        return list(sorted_words.values())

        # i = 0
        # ans = []

        # while i<len(strs):
        #     tmps = strs[i]
        #     ans.append([strs[i]])
        #     strs.pop(i)

        #     j = i
        #     while j<len(strs):
        #         if len(tmps) == len(strs[j]):
        #             if not any([tmps.count(c) != strs[j].count(c) for c in set(tmps)]):
        #                 ans[-1].append(strs[j])
        #                 strs.pop(j)
        #                 j-=1

        #         j += 1

        # return ans


# 실버 1

import sys
from collections import deque

input = sys.stdin.readline

m, n = map(int, input().split())
dx, dy = [-1, 1, 0, 0, -1, -1, 1, 1], [0, 0, -1, 1, -1, 1, -1, 1]
board = [input().split() for _ in range(m)]
result = 0

for i in range(m):
    for j in range(n):
        if board[i][j] == "1":
            board[i][j] = "0"
            queue = deque([(i, j)])

            while queue:
                x, y = queue.popleft()
                for k in range(8):
                    nx, ny = x + dx[k], y + dy[k]
                    if 0 <= nx < m and 0 <= ny < n and board[nx][ny] == "1":
                        board[nx][ny] = "0"
                        queue.append((nx, ny))

            result += 1

print(result)


# 첫수를 넣으세요 2
# 끝수를 넣으세요 4
# 배수를 넣으세요 3

# 2에서 4까지의 3의 배수의 합은 3입니다.

a = input("첫수를 넣으세요 ")
b = input("끝수를 넣으세요 ")
c = input("배수를 넣으세요 ")

first = int(a)
last = int(b)
mul = int(c)

sum = 0
for i in range(first, last+1):
    # print(i)
    if i % mul == 0:
      sum += i

print("{}에서 {}까지의 {}의 배수의 합은 {}입니다.".format(first, last, mul, sum))