# -*- coding:utf-8 -*-
"""
@version: 3.7
@time: 2021/2/19 15:20
@author: James
"""
"""
根据item获取商品（模拟浏览器，比较慢）
"""
import time
from lxml import etree
from pprint import pprint
import urllib3
import re
from concurrent.futures import ThreadPoolExecutor, as_completed

executor = ThreadPoolExecutor(max_workers=10)

urllib3.disable_warnings()
from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(options=chrome_options)

# driver = webdriver.PhantomJS()

def get_item_infos(item):
    return_value = {}
    try:
        # url = "https://www.amazon.com/dp/%s?language=zh_CN" % item
        url = "https://www.amazon.com/dp/%s?language=en_US" % item
        print(url)

        driver.get(url)
        page_source = driver.page_source
        root = etree.HTML(page_source)

        titles = root.xpath("//span[@id='productTitle']//text()")
        solds = root.xpath("//a[@id='sellerProfileTriggerId']//text()")
        star_levels = root.xpath("//span[@class='a-icon-alt']//text()")
        comments = root.xpath("//span[@id='acrCustomerReviewText']//text()")

        # 图片
        img_list = list(eval(re.search("main.*?}", re.search("colorImages.*?},", page_source)[0])[0][6:]).keys())
        return_value["imgs"] = img_list

        # 标题
        title = ""
        if len(titles) > 0:
            title = "".join(titles)
            title = title.strip()
        return_value["title"] = title

        # 价格
        price = ""
        prices1 = root.xpath("//span[@id='price_inside_buybox']//text()")
        prices2 = root.xpath("//span[contains(@id, 'priceblock')]//text()")
        if len(prices1) > 0:
            price = prices1[0].strip().split('$')[-1].replace(",", "").strip()
        if price == "" and len(prices2) > 0:
            price = prices2[0].strip().split('$')[-1].replace(",", "").strip()
        return_value["price"] = price

        # 店铺
        sold = ""
        if len(solds) > 0:
            sold = solds[0].strip()
        return_value["sold"] = sold

        # 星级
        star_level = 0
        if len(star_levels) > 0:
            star_level = star_levels[0].split(" ")[0].strip()
        return_value["star_level"] = star_level

        # 评论
        comment = ""
        if len(comments) > 0:
            comment = comments[0].split(" ")[0].replace(",", "").strip()
        return_value["comment"] = comment

        # 排名
        real_rank = {}
        try:
            for i in range(50):
                rank = "".join(
                    root.xpath("//table[@id='productDetails_detailBullets_sections1']//tr[%s]//td//text()" % i))
                if rank == "":
                    continue
                if rank.strip().find("商品里排第") > -1:
                    for r in rank.split("\n"):
                        if r != "":
                            k = r.split("名")[-1].split("(")[0].strip()
                            v = r.split("第")[-1].split("名")[0].replace(",", "").strip()
                            real_rank[k] = v
                if rank.strip().find("#") > -1 and rank.strip()[0] == "#":
                    for r in rank.split("\n"):
                        if r != "":
                            k = r.split(" in ")[1].split("(")[0].strip()
                            v = r.split("#")[-1].split(" in ")[0].replace(",", "").strip()
                            real_rank[k] = v

        except Exception as e:
            pass

        if real_rank == "":
            try:
                rank = "".join(root.xpath("//li[@id='SalesRank']//text()"))
                if rank.strip().find("商品里排第") > -1:
                    for r in rank.split("\n"):
                        if r != "":
                            k = r.split("名")[-1].split("(")[0].strip()
                            v = r.split("第")[-1].split("名")[0].replace(",", "").strip()
                            real_rank[k] = v
                if rank.strip().find("#") > -1 and rank.strip()[0] == "#":
                    for r in rank.split("\n"):
                        if r != "":
                            k = r.split(" in ")[1].split("(")[0].strip()
                            v = r.split("#")[-1].split(" in ")[0].replace(",", "").strip()
                            real_rank[k] = v
            except Exception as e:
                pass
        return_value["rank"] = real_rank
        return_value["item"] = item
        print("结果:")
        pprint(return_value)

    except Exception as e:
        pass
    finally:
        return return_value

def selenium_item_infos_run():
    t1 = time.time()
    items = ["B07FCMKK5X", "B08R2WG4D4", "B08GYKNCCP"]
    all_task = [executor.submit(get_item_infos, (item)) for item in items]

    for future in as_completed(all_task):
        data = future.result()
        print("data:{}".format(data))

    # for item in items:
    #     get_item_infos(item)
    try:
        driver.close()
    except Exception as e:
        pass

    print(time.time() - t1)

if __name__ == '__main__':
    selenium_item_infos_run()


"The unit tests for the flights app"
from django.test import TestCase
from django.utils import timezone
from .models import Flight
from .views import _search_origin, _search_destination

class FlightsTestCase(TestCase): # pylint: disable=no-member
    "Unit tests for the main functionality"

    def setUp(self):
        "Create Flight objects for testing"
        Flight.objects.create(
            flight_id="123456",
            created_at=timezone.now(),
            updated_at=timezone.now(),
            flight_identifier = "abcede-12345",
            flt_num = "1234",
            scheduled_origin_gate="001",
            scheduled_destination_gate="Any",
            out_gmt = timezone.now(),
            in_gmt = timezone.now(),
            off_gmt = timezone.now(),
            on_gmt = timezone.now(),
            destination = "ATL",
            origin="BNA",
            destination_full_name="Atlanta",
            origin_full_name="Nashville"
        )
        Flight.objects.create(
            flight_id="654321",
            created_at=timezone.now(),
            updated_at=timezone.now(),
            flight_identifier = "fghijk-45678",
            flt_num = "4567",
            scheduled_origin_gate="002",
            scheduled_destination_gate="Any",
            out_gmt = timezone.now(),
            in_gmt = timezone.now(),
            off_gmt = timezone.now(),
            on_gmt = timezone.now(),
            destination = "DEN",
            origin="TPA",
            destination_full_name="Denver",
            origin_full_name="Tampa"
        )

    def test_search_origin(self):
        "Test that search_origin returns flights matching the search"
        bna_flight = Flight.objects.get(flight_id="123456")
        tpa_flight = Flight.objects.get(flight_id="654321")
        self.assertIn(bna_flight, _search_origin("bna"))
        self.assertIn(tpa_flight, _search_origin("tpa"))
        self.assertNotIn(bna_flight, _search_origin("tpa"))
        self.assertNotIn(tpa_flight, _search_origin("bna"))

    def test_search_destination(self):
        "Test that search_origin returns flights matching the search"
        atl_flight = Flight.objects.get(flight_id="123456")
        den_flight = Flight.objects.get(flight_id="654321")
        self.assertIn(atl_flight, _search_destination("atl"))
        self.assertIn(den_flight, _search_destination("den"))
        self.assertNotIn(atl_flight, _search_destination("den"))
        self.assertNotIn(den_flight, _search_destination("atl"))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 31 23:02:00 2018

@author: throop
"""

import pdb
import glob
import math       # We use this to get pi. Documentation says math is 'always available'
                  # but apparently it still must be imported.
from   subprocess import call
import warnings
import pdb
import os.path
import os
import subprocess

import astropy
from   astropy.io import fits
from   astropy.table import Table
import astropy.table   # I need the unique() function here. Why is in in table and not Table??
import matplotlib
import matplotlib.pyplot as plt # pyplot
from   matplotlib.figure import Figure
import numpy as np
import astropy.modeling
from   scipy.optimize import curve_fit
#from   pylab import *  # So I can change plot size.
                       # Pylab defines the 'plot' command
import spiceypy as sp
#from   itertools import izip    # To loop over groups in a table -- see astropy tables docs
from   astropy.wcs import WCS
from   astropy import units as u           # Units library
from   astropy.coordinates import SkyCoord # To define coordinates to use in star search
#from   photutils import datasets
from   scipy.stats import mode
from   scipy.stats import linregress
from   astropy.visualization import wcsaxes
import time
from   scipy.interpolate import griddata
from   importlib import reload            # So I can do reload(module)
import imreg_dft as ird                    # Image translation
import struct

import re # Regexp
import pickle # For load/save

from   datetime import datetime

import scipy

from   matplotlib.figure import Figure
from   get_radial_profile_circular import get_radial_profile_circular

# HBT imports

import hbt

from nh_ort_track4_grid            import nh_ort_track4_grid    # Includes .read, .write, .plot, .flythru, etc.

#from nh_ort_track3_plot_trajectory import nh_ort_track3_plot_trajectory
#from nh_ort_track3_read            import nh_ort_track3_read
#from nh_ort_track3_read            import stretch_hbt, stretch_hbt_invert  # Some stretch routines good for traj's
#from nh_ort_track3_read            import plot_flattened_grids_table

#%%%
# =============================================================================
# Main function to fly s/c through the grids. All of the 4D grids must already
# be generated and saved to disk. This routine loops over them, and creates the
# output files for Doug Mehoke, of particle density vs. time.
#
# To run Track 4:
#
#   - Execute nh_track4_calibrate.py . This reads in all of DPH/DK's individual dust trajectories,
#     and merges them into '4D' dust grids, which are properly calibrated to match a given I/F.
#     Typically this reads in 108 files, and outputs 64 files, named *.grids4d.gz. These grids
#     are essentially just matrices (7, 200, 200, 200) with the dust density as a func of XYZ and grain size.
#
#   - Then execute nh_ort_track4_flyby.py. This reads all of the 64 grids files, and
#     outputs a list of dust densities vs. time, for each one.
#     Output is a table, essentially showing dust density (in # km-3) as a func of grain size, and time.
#     Typically 64 files, *.dust .
#
# This is a regular function, but it calls the class method nh_ort_track4_grid.fly_trajectory().
# =============================================================================

def nh_ort_track4_flyby(dir_in=None, dir_out=None, name_trajectory = 'prime'):

    #%%%

#    dir_in = '/Users/throop/
#    dir_in = '/Users/throop/data/ORT4/throop/ort4_bc3_10cbr2_dph/'

    stretch_percent = 99
    stretch = astropy.visualization.PercentileInterval(stretch_percent)

#    dir_data = os.path.expanduser('~/Data/')

    # dir_in git
    do_compress = False   # Do we use .gzip compression on the Track-4 input grids?
                          # If we used compression on the track4_calibrate routine, we must use it here too.

#    dir_track4 = os.path.join(dir_data, name_ort, 'throop', 'track4')

    if do_compress:
        files = glob.glob(os.path.join(dir_in, '*.grid4d.gz'))

    else:
        files = glob.glob(os.path.join(dir_in, '*.grid4d'))
        files = glob.glob(os.path.join(dir_in, '*.dust.pkl'))

    # Alphabetize file list

    files = sorted(files)

    plt.set_cmap('plasma')

    utc_ca = '2019 1 Jan 05:33:00'
    dt_before = 1*u.hour
    dt_after  = 1*u.hour

#    area_sc = (1*u.m)**2

    frame = '2014_MU69_SUNFLOWER_ROT'

    name_target = 'MU69'
    origin = 'lower'   # Required plotting order for imshow

    name_observer = 'New Horizons'

    hbt.figsize((8,6))
    hbt.set_fontsize(12)

    dt = 1*u.s         # Sampling time through the flyby. Astropy units.

    # Create an output table, Astropy format

    t = Table(names = ['trajectory', 'speed', 'q_dust', 'albedo', 'rho',
                       'tau_max', 'tau_typical', 'iof_max', 'iof_typical'],
              dtype = ['U30', float, float, float, float,
                       float, float, float, float]  )

    # Start up SPICE if needed. Unload old kernels just as a safety precaution.

    sp.unload('kernels_kem_prime.tm')
    sp.unload('kernels_kem_alternate.tm')

    sp.furnsh(f'kernels_kem_{name_trajectory}.tm')

    do_short = False

    if do_short:
        files = files[0:4]

        i=3
        file = files[i]

    num_files = len(files)
    name_run = dir_out.split('/')[-2]

#%%%
    for i,file in enumerate(files):

#%%%
        print(f'Starting file {i}/{len(files)}')

        grid = nh_ort_track4_grid(file)    # Load the grid from disk. Uses gzip, so it is quite slow (10 sec/file)
        print(f'Loading file {file}')

    # Load the trajectory parameters

        et_ca = int( sp.utc2et(utc_ca) )  # Force this to be an integer, just to make output cleaner.

        et_start = et_ca - dt_before.to('s').value
        et_end   = et_ca + dt_after.to('s').value

        grid.frame       = frame
        grid.name_target = name_target
        grid.name_trajectory = name_trajectory

        # And call the method to fly through it!
        # The returned density values etc are available within the instance variables, not returned explicitly.

        grid.fly_trajectory(name_observer, et_start, et_end, dt)

        # If the first time thru loop, make plot of our path through the system

        do_plots_geometry = True

        if (do_plots_geometry and (i==0)):

            grid.plot_trajectory_geometry()

        # Make slice plots thru the grid

        do_plot_slices_xyz = False

        if do_plot_slices_xyz:
            hbt.fontsize(8)
            hbt.figsize((20,5))
            grid.plot(axis_sum=0)
            grid.plot(axis_sum=1)
            grid.plot(axis_sum=2)

            hbt.fontsize(10)

        # Make a plot of optical depth

        do_plot_tau = True
        if do_plot_tau:
            grid.plot_tau()

# =============================================================================
# Make some plots of count rate vs. time!
# =============================================================================

        # Make a plot of the instantaneous count rate

        hbt.figsize((10,15))

        # Make a plot of the actual density that we give to Doug Mehoke

        # Define a list of colors. This is so we can use colors= argument to set
        # a marker to show grain size, rather than let plot() auto-assign.

#        colors = plt.rcParams['axes.prop_cycle'].by_key()['color']  # This is the default color iterator.
        colors = ['antiquewhite',
                  'tomato',
                  'blueviolet',
                  'skyblue',
                  'gold',
                  'darkcyan',
                  'thistle',
                  'olive',
                  'red',
                  'sienna',
                  'deepskyblue',
                  'lightsalmon',
                  'pink',
                  ]
                  # 'aqua']

#                  'antiquewhite4', 'aqua', 'aquamarine4', 'black', 'blue', 'blueviolet',
#                  'brown1', 'chartreuse1', 'darkgreen', 'darkorange1', 'dodgerblue1', 'lightpink', 'magenta']

        # Make a plot of dust number density. This is straight out of the grid, and for comparison with MRS.

        vals_fiducial = [1e-10, 1e-8, 1e-6,1e-4, 1e-2, 1e-0]

        plt.subplot(3,1,1)
        for j,s in enumerate(grid.s):
            plt.plot(grid.delta_et_t, grid.number_t[j],
                     label = 's={:.2f} mm'.format(s),
                     color=colors[j])
        plt.legend()
        plt.title('Dust number density'.format(grid.area_sc))
        plt.xlabel('ET from C/A')
        plt.yscale('log')
        plt.ylim((1e-10, 1e2))  # Match to MRS.
        plt.axvline(0, color='black', alpha=0.05)
        plt.ylabel(r'Dust,, # km$^{-3}$')

        for val in vals_fiducial:
            plt.axhline(val, color='black', alpha=0.05)

        # Make a plot of impact rate. This assumes a s/c area.

        plt.subplot(3,1,2)
        for j,s in enumerate(grid.s):   # 's' is dust size
            plt.plot(grid.delta_et_t, grid.number_sc_t[j],
                     label = f's={s:.2f} mm',
                     color=colors[j])
        plt.title('Impact rate, A={}'.format(grid.area_sc))
        plt.yscale('log')
        plt.xlabel('ET from C/A')
        plt.legend()
        plt.ylabel(r'Dust, # Impacts sec$^{{-1}}$')

        # Make a plot of the cumulative count rate. Mark grain sizes here too.

        plt.subplot(3,1,3)
        for j,s in enumerate(grid.s):                                             # Loop over size
            plt.plot(grid.delta_et_t, grid.number_sc_cum_t[j],                    # Main plot line
                     label = 's={:.2f} mm'.format(s), color=colors[j])
            plt.plot([grid.delta_et_t[-1]], [grid.number_sc_cum_t[j,-1].value],   # Circle to indicate grain size
                     markersize=(7-j)*2, marker = 'o',                            # Use same color as prev line!
                     color=colors[j])

        hbt.figsize(5,5)
        plt.legend()
        plt.title('Number of impacts (cumulative), A={}'.format(grid.area_sc))
        plt.xlabel('ET from C/A')
        plt.yscale('log')
        plt.ylabel('# of Impacts')
        plt.axhline(y = 1, linestyle = '--', alpha = 0.1)

        plt.tight_layout()

        plt.show()

        # Make a plot of size distibution.
        # Make two curves: one for n(r) for the entire grid, and one for n(r) that hits s/c

        # Now add an entry to the table. This is a table that lists all of the results --
        #     e.g., max_tau, count rate etc
        # One line per grid.

        t.add_row(vals=[grid.name_trajectory, grid.speed, grid.q, grid.albedo, grid.rho,
                        grid.tau_max, grid.tau_typ,
                        grid.iof_max, grid.iof_typ])

        # Get size dist along path

        number_path = grid.number_sc_cum_t[:,-1].value

        # Take the full particle grid, and sum along all spatial axes, leaving just the size axis left.

        number_grid = np.sum(np.sum(np.sum(grid.density, axis=1), axis=1), axis=1)

        # Normalize the size dists both
        number_grid = hbt.normalize(number_grid)
        number_path = hbt.normalize(number_path)

        plt.plot(grid.s, number_path, label = 'Along s/c path')
        plt.plot(grid.s, number_grid, label = 'In grid, total')
        plt.yscale('log')
        plt.xscale('log')
        plt.ylim( (hbt.minval(np.array([number_grid, number_path]))/2, 1) )
        plt.xlabel('Radius [mm]')
        plt.ylabel('Particle number [arbitrary]')
        plt.legend(loc = 'lower right')
        plt.show()

        # Output the dust population for this run to a file. This is the file that Doug Mehoke will read.

        grid.output_trajectory(name_run=name_run, do_positions=False, dir_out=dir_out)

        print('---')

#%%%

    # Print the table

    t.pprint(max_width=-1)

    # Save the table as output

    file_out = os.path.join(dir_out, f'nh_{name_trajectory}_track4_table.pkl')

    lun = open(file_out, 'wb')
    pickle.dump(t,lun)
    lun.close()
    print(f'Wrote: {file_out}')

#%%%

    # Now that all files have been created, compress results into an archive (.tar.gz) for Doug Mehoke

    inits_track4    = 'hbt'

#    if 'hamilton' in files[0]:
#        inits_track3 = 'dph'
#    if 'kauf' in files[0]:
#        inits_track3 = 'dk'

    file_out = f'{name_trajectory}_{name_run}_{inits_track4}_n{num_files}.tgz'

    str = f'cd {dir_out}; tar -czf {file_out} *{name_trajectory}*.dust'

    _ = subprocess.Popen(str, shell=True)

    print(str)
    print(f'Wrote: {dir_out}/{file_out}')

# =============================================================================
# Plot some tabulated results.
# One-off function.
# =============================================================================

def plot_table():

    file_in = '/Users/throop/Data/ORT2/throop/track4/nh_ort_track4_table.pkl'

    lun = open(file_in, 'rb')
    t = ge.load(lun)
    lun.close()

    hbt.figsize((10,8))

    xvals = ['albedo', 'speed', 'rho', 'q_dust']
    i = 0
    for xval in xvals:
        plt.subplot(2,2,i+1)
#        plt.plot(t[xval], t['iof_max'], marker = 'o', linestyle='none', label = 'I/F Max')
        plt.plot(t[xval], t['iof_typical'], marker = '+', linestyle='none', label = 'I/F Typical')
        plt.xlabel(xval)
        plt.yscale('log')
        plt.legend()
        i +=1
    plt.tight_layout()
    plt.show()

    t.sort(['speed', 'q_dust', 'albedo', 'rho'])
    t.pprint(max_width=-1, max_lines=-1)

# =============================================================================
# Output table indices for MRS
# =============================================================================

def make_table_grid_positions():

    """
    This is a one-off utility function for MRS.
    In it, I just do a flyby of MU69, and output the X Y Z grid indices (as well as positions and timestamps).
    I don't output density at all -- just the s/c positions.

    I do this for both prime and alternate trajectories.

    This is just because he hasn't integrated SPICE into his grid code.

    This function is stand-alone. It doesn't rely on the grid class.
    It is included in this file because it directly relates to the grids.

    """

#%%%

    name_trajectory = 'alternate'  # ← Set this to 'prime' or 'alternate'
    # name_trajectory = 'prime'  # ← Set this to 'prime' or 'alternate'

    hbt.unload_kernels_all()

    frame = '2014_MU69_SUNFLOWER_ROT'

    name_observer = 'New Horizons'

    name_target = 'MU69'

    sp.furnsh(f'kernels_kem_{name_trajectory}.tm')

    # Get the OD version. This might not work.

    # files = hbt.list_kernels_loaded()
    # for file in files:
        # if

#    file_in = '/Users/throop/Data/ORT2/throop/track4/ort2-ring_v2.2_q2.0_pv0.10_rho0.22.grid4d.gz'
#    file_in = '/Users/throop/Data/ORT5/throop/deliveries/tuna9k/ort5_None_y3.0_q3.5_pv0.70_rho1.00.dust.txt'
    file_in = '/Users/throop/Data/ORT5/kaufmann/deliveries/chr3-tunacan10k/chr3-0003' + \
              '/y3.0/beta1.0e+00/subset00/model.array2'

    # file_in = '/Users/throop/Data/ORT5/throop/deliveries/tuna9k/ort5_None_y3.0_q3.5_pv0.70_rho1.00.dust.pkl' #250km
    # file_in = '/Users/throop/Data/ORT5/throop/deliveries/dph-sunflower10k/ort5_None_y2.2_q2.5_pv0.05_rho0.46.dust.pkl' #500km
    file_in = '/Users/throop/Data/ORT5/throop/deliveries/dph-tunacan3.5kinc55/ort5_None_y2.2_q2.5_pv0.05_rho0.46.dust.pkl' #500km

    grid = nh_ort_track4_grid(file_in)    # Load the grid from disk. Uses gzip, so it is quite slow (10 sec/file)

    resolution_km = int(grid.resolution_km[0])

    utc_ca = '2019 1 Jan 05:33:00'
    dt_before = 1*u.hour
    dt_after  = 1*u.hour
    dt = 1*u.s         # Sampling time through the flyby. Astropy units.

    et_ca = int( sp.utc2et(utc_ca) )  # Force this to be an integer, just to make output cleaner.

    et_start = et_ca - dt_before.to('s').value
    et_end   = et_ca + dt_after.to('s').value

    grid.frame       = frame
    grid.name_target = name_target
    grid.name_trajectory = name_trajectory

    # And call the method to fly through it!
    # The returned density values etc are available within the instance variables, not returned explicitly.

    grid.fly_trajectory(name_observer, et_start, et_end, dt)

    # Make plots

    hbt.figsize((9,9))
    hbt.fontsize(12)

    plt.subplot(3,2,1)
    plt.plot(grid.bin_x_t)
    plt.ylabel('X Bin #')
    plt.title(f'MU69, Trajectory = {name_trajectory}, frame = {frame}')

    plt.subplot(3,2,3)
    plt.plot(grid.bin_y_t)
    plt.ylabel('Y Bin #')

    plt.subplot(3,2,5)
    plt.plot(grid.bin_z_t)
    plt.ylabel('Z Bin #')
    plt.xlabel('Timestep #')

    t_t = grid.et_t - np.mean(grid.et_t)
    bin_t = range(len(t_t))
    plt.subplot(3,2,2)
    plt.axhline(0, color='pink')
    plt.axvline(0, color='pink')
    plt.plot(t_t, grid.x_t)

    plt.ylabel('X [km]')

    plt.xlabel('t [sec]')

    plt.subplot(3,2,4)
    plt.axhline(0, color='pink')
    plt.axvline(0, color='pink')
    plt.plot(t_t, grid.y_t)
    plt.ylabel('Y [km]')

    plt.subplot(3,2,6)
    plt.axhline(0, color='pink')
    plt.axvline(0, color='pink')
    plt.plot(t_t, grid.z_t)
    plt.ylabel('Z [km]')
    plt.xlabel('Time from C/A [sec]')
    plt.tight_layout()

    # Save the plot to a file

    file_out = f'positions_trajectory_{name_trajectory}.png'
    path_out = os.path.join(dir_out, file_out)

    plt.savefig(path_out)
    print(f'Wrote: {path_out}')
    plt.show()

    # Make a table

    arr = {'bin' : bin_t,
           'delta_et' : t_t,
           'X_km' : grid.x_t,
           'Y_km' : grid.y_t,
           'Z_km' : grid.z_t,
           'Bin_X' : grid.bin_x_t,
           'Bin_Y' : grid.bin_y_t,
           'Bin_Z' : grid.bin_z_t}

    t = Table(arr, names=['bin', 'delta_et', 'X_km', 'Y_km', 'Z_km', 'Bin_X', 'Bin_Y', 'Bin_Z'],
              dtype=['int', 'int', 'float', 'float', 'float', 'int', 'int', 'int'])

    # Save the table to a file

    file_out = f'positions_trajectory_{name_trajectory}_res{resolution_km}.txt'
    path_out = os.path.join('/Users/throop/Data/ORT5', file_out)

    t.write(path_out, format = 'ascii.csv', overwrite=True)
    print(f'Wrote: {path_out}')

#%%%

# =============================================================================
# Call the main function when this program as run
# =============================================================================

if (__name__ == '__main__'):

#%%%

    # NB: It would make sense to parallelize this loop below. That way I could run a bunch of these in parallel.

    # name_trajectory = 'prime'
    name_trajectory = 'alternate'  # 'prime' or 'alternate'. For ORT5, use 'prime' on 3.5k, and 'alternate' on 10k.

    # dir_in  = '/Users/throop/data/ORT4/throop/ort4_bc3_10cbr2_dph/'
    # dir_in  = '/Users/throop/data/ORT5/throop/deliveries/chr3-sunflower3.5k/'
    # dir_in  = '/Users/throop/data/ORT5/throop/deliveries/chr3-sunflower10k/'

    # dir_in = '/Users/throop/data/ORT5/throop/deliveries/tuna9k/'

    # dir_in = '/Users/throop/data/ORT5/throop/deliveries/sun10k-DPH/'
    # dir_in = '/Users/throop/data/ORT5/throop/deliveries/dph-tunacan3.5kinc55/'
    dir_in = '/Users/throop/data/ORT5/throop/deliveries/dph-tunacan3.5kinc70v1/'
    dir_in = '/Users/throop/data/ORT5/throop/deliveries/dph-sunflower10k/'
    dir_in = '/Users/throop/data/ORT5/throop/deliveries/dph-sunflower3.5k/'
    dir_in = '/Users/throop/data/ORT5/throop/deliveries/dek-chr3-sunflower3.5k/'
    dir_in = '/Users/throop/data/ORT5/throop/deliveries/dek-chr3-sunflower10k/'

    # dir_in = '/Users/throop/data/ORT5/throop/deliveries/sun10k_a'
    # dir_in = '/Users/throop/data/ORT5/throop/deliveries/sun10k_b'
    # dir_in  = '/Users/throop/data/ORT4/throop/ort4_bc3_10cbr2_dek/'

    dir_out = os.path.join(dir_in, 'for_mehoke')

    if not os.path.exists(dir_out):
        os.makedirs(dir_out)

#%%%

    nh_ort_track4_flyby(dir_in=dir_in, dir_out=dir_out, name_trajectory = name_trajectory)


import tensorflow as tf
import ssl

from tensorflow.examples.tutorials.mnist import input_data
##########################################
# Fix to pass https certficate on download
##########################################
try:
   _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
   # Legacy Python that doesn't verify HTTPS certificates by default
   pass
else:
   # Handle target environment that doesn't support HTTPS verification
   ssl._create_default_https_context = _create_unverified_https_context
#
# ,one_hot=False
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)  # y labels are oh-encoded
#
#
n_train = mnist.train.num_examples  # 55,000
n_validation = mnist.validation.num_examples  # 5000
n_test = mnist.test.num_examples  # 10,000
#
n_input = 784  # input layer (28x28 pixels)
n_hidden1 = 512  # 1st hidden layer
n_hidden2 = 256  # 2nd hidden layer
n_hidden3 = 128  # 3rd hidden layer
n_output = 10  # output layer (0-9 digits)
#
learning_rate = 1e-4
n_iterations = 1000
batch_size = 128
dropout = 0.5
#
X = tensorflow.placeholder("float", [None, n_input])
Y = tf.placeholder("float", [None, n_output])
keep_prob = tf.placeholder(tf.float32)
# KOA
#
weights = {
    'w1': tf.Variable(tf.truncated_normal([n_input, n_hidden1], stddev=0.1)),
    'w2': tf.Variable(tf.truncated_normal([n_hidden1, n_hidden2], stddev=0.1)),
    'w3': tf.Variable(tf.truncated_normal([n_hidden2, n_hidden3], stddev=0.1)),
    'out': tf.Variable(tf.truncated_normal([n_hidden3, n_output], stddev=0.1)),
}
#
biases = {
    'b1': tf.Variable(tf.constant(0.1, shape=[n_hidden1])),
    'b2': tf.Variable(tf.constant(0.1, shape=[n_hidden2])),
    'b3': tf.Variable(tf.constant(0.1, shape=[n_hidden3])),
    'out': tf.Variable(tf.constant(0.1, shape=[n_output]))
}
#
layer_1 = tf.add(tf.matmul(X, weights['w1']), biases['b1'])
layer_2 = tf.add(tf.matmul(layer_1, weights['w2']), biases['b2'])
layer_3 = tf.add(tf.matmul(layer_2, weights['w3']), biases['b3'])
layer_drop = tf.nn.dropout(layer_3, keep_prob)
output_layer = tf.matmul(layer_3, weights['out']) + biases['out']
#output_layer = tf.matmul(layer_3, weights['out']) + biases['out']
#
cross_entropy = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(
        labels=Y, logits=output_layer
        ))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
#
# Training and testing
#
correct_pred = tf.equal(tf.argmax(output_layer, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
#
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
#
# train on mini batches
#
for i in range(n_iterations):
    batch_x, batch_y = mnist.train.next_batch(batch_size)
    sess.run(train_step, feed_dict={
        X: batch_x, Y: batch_y, keep_prob: dropout
        })

    # print loss and accuracy (per minibatch)
    # feed_dict={X: batch_x, Y: batch_y, keep_prob: 1.0}
    if i % 100 == 0:
        minibatch_loss, minibatch_accuracy = sess.run(
            [cross_entropy, accuracy],
            feed_dict={X: batch_x, Y: batch_y, keep_prob: 1.0}
            )
        print(
            "Iteration",
            str(i),
            "\t| Loss =",
            str(minibatch_loss),
            "\t| Accuracy =",
            str(minibatch_accuracy)
            )
#
test_accuracy = sess.run(accuracy, feed_dict={X: mnist.test.images, Y: mnist.test.labels, keep_prob: 1.0})
print("\nAccuracy on test set:", test_accuracy)
#
# Test with a image
#
import numpy as np
from PIL import Image
#
# curl -O https://raw.githubusercontent.com/do-community/tensorflow-digit-recognition/master/test_img.png
#
img = np.invert(Image.open("test_img.png").convert('L')).ravel()
#

print("Here we go...")

prediction = sess.run(tf.argmax(output_layer, 1), feed_dict={X: [img]})
#prediction = sess.run((output_layer), feed_dict={X: [img]})
#prediction1 = sess.run(tf.argmin(output_layer, 1), feed_dict={X: [img]})
print ("Prediction for test image:", np.squeeze(prediction))
#
print ("Antal i array", n_output)
for i in range(n_output-1):
    print("%s - Value: %s" % (i, prediction[:n_output]))

#for i in range(len(Xnew)):
#	print("X=%s, Predicted=%s" % (Xnew[i], ynew[i]))


# -*- coding: utf-8 -*-
# Program ki omogoča ugibanje glavnega mesta evropskih držav

import Tkinter as Tk # dobre informacije za Tkinter so na effbot
import tkMessageBox
from functools import partial
import random

main_window = Tk.Tk() # naredimo glavno okno

def preveri_drzavo(drzave_mesta_dict, ugibana_drzava):
    vpisano = vpis.get()
    if vpisano.lower() == drzave_mesta_dict[ugibana_drzava]:
        sporocilo = "Bravo, uganil si!"
    else:
        sporocilo = "Narobe, več sreče naslednjič."
    tkMessageBox.showinfo("Rezultat", sporocilo)

def izberi_nakljucno_drzavo(drzave_mesta_dict):
    # Izberemo nakljucno drzavo med 0 in dolzino vseh drzav
    # Pazi, randint vrne eno vec kot hocemo, zato damo -1
    cifra_drzave = random.randint(0,len(drzave_mesta_dict)-1)
    # Vrnemo to drzavo
    return drzave_mesta_dict.keys()[cifra_drzave]

drzave_mesta_dict = {
    "Slovenija":"ljubljana",
    "Avstrija":"dunaj",
    "Nemčija":"berlin",
    "Italija":"rim",
    "Madžarska":"budimpesta",
    "Francija":"pariz"
    }

napis1 = Tk.Label(main_window, text = "Pozdravljen v igrici: Glavna mesta držav")

drzava = izberi_nakljucno_drzavo(drzave_mesta_dict)

napis2 = Tk.Label(main_window, text = "Ugani glavno mesto drzave " + drzava + " ")

vpis = Tk.Entry(main_window)

gumb1 = Tk.Button(main_window, text="Preveri", command=partial(preveri_drzavo, drzave_mesta_dict, drzava))

napis1.pack()
napis2.pack()
vpis.pack()
gumb1.pack()

main_window.mainloop()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 13 17:52:12 2018

@author: daniel.grieneisen
"""

import numpy as np
from matplotlib import pyplot as plt

def readFromCSV(filename):
    with open(filename, 'r') as fn:
        result_array = []
        for line in fn.readline():
            split_line = line.split(',')
            try:
                row = []
                for val in split_line:
                    row.append(np.float(split_line[0]))
                result_array.append(row)
            except:
                # Could not covert some data into floating point values
                # Skip this row
                continue
        return np.array(result_array)

def writeToCSV(filename, data):
    with open(filename, 'w') as fn:
        fn.write('x, y\n');
        for sample in data:
            fn.write('{}, {}\n'.format(data[0], data[1]))

def plotData(data):
    fig, ax = plt.subplots()
    ax.plot(data[..., 0], data[..., 1], '*', linewidth=2, color='firebrick')
    plt.title('Data')
    plt.xlabel('x value')
    plt.ylabel('y value')

def generateData(fn, min_x, max_x):
    output = []
    x = np.linspace(min_x, max_x, 100)
    for val in x:
        output.append([val, fn(val)])
    return np.array(output)

def random_val(val):
    return np.random.uniform(-val, val)

def fn1(x):
    return 5 + 1.5 * x - 0.2 * x * x + random_val(0.75)

def fn2(x):
    return 0.25 * np.exp(x) + 1.0 / x + random_val(4)

def fn3(x):
    return 1.4 * np.cos(x) + + 0.5 * x + random_val(2.0)

data1 = generateData(fn1, 0, 5)
data2 = generateData(fn2, 0.1, 5)
data3 = generateData(fn3, -np.pi * 2, np.pi * 2)

plotData(data1)
plotData(data2)
plotData(data3)

writeToCSV('ols_1.csv', data1)
writeToCSV('ols_2.csv', data2)
writeToCSV('ols_3.csv', data3)


import sys
import os
import re
import shutil
from PySide2.QtUiTools import *
from PySide2.QtCore import *
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from ui_mainwindow import Ui_mainWindow
# from ui_form import Ui_Form
from ui_command_info import Ui_FormCommandInfo
from lxml import etree
from htoxml.cmdfinder import CmdFinder
import webgenxml
import copy
from functools import partial
import time

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_mainWindow()
        self.ui.setupUi(self)
        self.ui.pushButtonGAll.clicked.connect(self.fillinput)
        self.ui.pushButtonGAll.clicked.connect(lambda : self.ui.tabWidget.setCurrentIndex(1))

        self.media_path = ''
        self.command_info = []
        self.command_tags = ('DW0_dwlen', 'class', 'def_dwSize', 'index', 'input_dwsize', 'name')
        self.dword_tags = ('NO', 'value', 'class', 'cmdarraysize', 'otherCMD')
        self.command_filter = {'MI_NOOP_CMD', 'MI_NOOP'}
        self.ringinfo_path = ''
        self.output_path = ''
        self.command_xml = ''
        self.test_name = ''
        self.platform = ''
        self.frame_num = 0
        self.row_num = 0

        self.form = FormCommandInfo(self)

        #
        self.last_dir = ''
        self.ui.SelectMediaPath.clicked.connect(partial(self.selectpath,'Media'))
        self.ui.SelectRinginfoPath.clicked.connect(partial(self.selectpath,'Ringinfo'))
        self.ui.SelectDDIInputPath.clicked.connect(partial(self.selectpath,'DDIInput'))

        #self.ui.lineEditMediaPath.setText(r'C:\Users\jiny\gfx\gfx-driver\Source\media')
        #self.ui.lineEditDDIInputPath.setText(r'C:\projects\github\AutoULTGen\Client\command_validator_app\vcstringinfo\HEVC-VDENC-grits-WP-2125\DDI_Input')
        #self.ui.lineEditRinginfoPath.setText(r'C:\projects\github\AutoULTGen\Client\command_validator_app\vcstringinfo\HEVC-VDENC-Grits001-2125\VcsRingInfo')

    @Slot()
    def fillinput(self):
        self.ui.buttonBox.accepted.connect(self.addHeader)
        self.ui.buttonBox.rejected.connect(self.reject)
        self.ui.InputPathText.setText(self.ui.lineEditDDIInputPath.text())
        self.ui.Component_input.setText(self.ui.lineEditComponent.text())
        self.ui.GUID_input.setText('DXVA2_Intel_LowpowerEncode_HEVC_Main')
        self.ui.Width_input.setText('256')
        self.ui.Height_input.setText('192')
        self.ui.RawTileType_input.setText('1')
        self.ui.RawFormat_input.setText('25')
        self.ui.ResTileType_input.setText('4')
        self.ui.ResFormat_input.setText('62')
        self.ui.EncFunc_input.setText('4')
        self.ui.FrameNum_input.setReadOnly(True)
        #self.ui.FrameNum_input.setText('1')
        self.FrameNumdiff = 0

    @Slot()
    def generate_from_bspec(self):
        url = self.ui.lineEditURL.text()
        webgenxml.webgen(url)
        self.ui.logBrowser.append('Get infomation from ' + url + '\n')

    def load_vesc_ring_info(self):
        vesc_ring_info = {}
        pass

    @Slot()
    def show_command_table(self, item, column = 0):
        self.form.current_item = item
        f_frame = False
        if item.data(2, 1):
            idx = item.data(2, 1)
            if idx['cmd_idx'] == 'all':
                self.form.current_frame = self.command_info[idx['frame_idx']]
                self.form.current_command = 'all'
                command_list = self.command_info[idx['frame_idx']]
                f_frame = True
            else:
                self.form.current_frame = self.command_info[idx['frame_idx']]
                self.form.current_command = self.command_info[idx['frame_idx']][idx['cmd_idx']]
                command_list = [self.command_info[idx['frame_idx']][idx['cmd_idx']]]
                if 'dword_idx' in idx:
                    command_item = item.parent()
                else:
                    command_item = item

            table = self.form.ui.tableWidgetCmd
            table.clearContents()
            self.form.row_command_map = []
            table.setRowCount(0)
            i_row = 0
            ##print(f_frame)
            for command_idx, command in enumerate(command_list):
                ##print('command ' + str(command_idx) + '\n')
                QCoreApplication.processEvents()
                if f_frame:
                    command_item = item.child(command_idx)

                if i_row >= table.rowCount():
                    table.insertRow(i_row)
                table.setItem(i_row, 0, QTableWidgetItem(command['name']))
                for dword_idx, dword in enumerate(command['dwords']):
                    ##print('dword ' + str(dword_idx) + '\n')
                    QCoreApplication.processEvents()
                    if 'unmappedstr' in dword and dword['unmappedstr']:
                        continue
                    if i_row >= table.rowCount():
                        table.insertRow(i_row)
                    table.setItem(i_row, 1, QTableWidgetItem('dword' + dword['NO']))
                    if not dword['fields']:
                        if dword['value']:
                            table.setItem(i_row, 3, QTableWidgetItem(dword['value']))
                        self.form.row_command_map.append(
                            {'frame_idx': idx['frame_idx'], 'command_idx': command['index'], 'dword_idx': dword_idx})
                        i_row += 1

                    dword_item = command_item.child(dword_idx)

                    for field in dword['fields']:
                        QCoreApplication.processEvents()
                        if field['field_name'].startswith('Obj'):
                            if i_row >= table.rowCount():
                                table.insertRow(i_row)
                            table.setItem(i_row, 1, QTableWidgetItem('dword' + dword['NO'] + '_' + field['field_name']))
                            for obj_field in field['obj_fields']:
                                if obj_field['obj_field_name'].startswith('Reserved') and self.form.ui.checkBoxReserved.isChecked():
                                    continue
                                if i_row >= table.rowCount():
                                    table.insertRow(i_row)
                                table.setItem(i_row, 2, QTableWidgetItem(obj_field['obj_field_name']))
                                checkBox = QCheckBox()
                                if (not obj_field['obj_field_name'].startswith('Reserved')) and command_item.checkState(0) == Qt.CheckState.Checked and dword_item.checkState(0) == Qt.CheckState.Checked and field['CHECK'] == 'Y':
                                    checkBox.setCheckState(Qt.CheckState.Checked)
                                checkBox.stateChanged.connect(self.check_box_change)
                                table.setCellWidget(i_row, 5, checkBox)
                                if self.form.mode == 'bin':
                                    table.setItem(i_row, 3, QTableWidgetItem(bin(int(obj_field['default_value'], 16))))
                                    table.setItem(i_row, 4, QTableWidgetItem(bin(int(obj_field['value'], 16))))
                                    table.setItem(i_row, 7, QTableWidgetItem(bin(int(obj_field['min_value'], 16))))
                                    table.setItem(i_row, 8, QTableWidgetItem(bin(int(obj_field['max_value'], 16))))
                                elif self.form.mode == 'dec':
                                    table.setItem(i_row, 3, QTableWidgetItem(str(int(obj_field['default_value'], 16))))
                                    table.setItem(i_row, 4, QTableWidgetItem(str(int(obj_field['value'], 16))))
                                    table.setItem(i_row, 7, QTableWidgetItem(str(int(obj_field['min_value'], 16))))
                                    table.setItem(i_row, 8, QTableWidgetItem(str(int(obj_field['max_value'], 16))))
                                else:
                                    table.setItem(i_row, 3, QTableWidgetItem(obj_field['default_value']))
                                    table.setItem(i_row, 4, QTableWidgetItem(obj_field['value']))
                                    table.setItem(i_row, 7, QTableWidgetItem(obj_field['min_value']))
                                    table.setItem(i_row, 8, QTableWidgetItem(obj_field['max_value']))
                                if 'Address' in field:
                                    table.setItem(i_row, 6, QTableWidgetItem(obj_field['Address']))
                                else:
                                    table.setItem(i_row, 6, QTableWidgetItem('N'))
                                table.item(i_row, 3).setFlags(Qt.NoItemFlags)
                                table.setItem(i_row, 9, QTableWidgetItem(obj_field['bitfield_l']))
                                table.setItem(i_row, 10, QTableWidgetItem(obj_field['bitfield_h']))
                                table.item(i_row, 9).setFlags(Qt.NoItemFlags)
                                table.item(i_row, 10).setFlags(Qt.NoItemFlags)
                                self.form.row_command_map.append(
                                    {'frame_idx': idx['frame_idx'], 'command_idx': command['index'],
                                     'dword_idx': dword_idx})
                                i_row += 1
                            continue

                        if field['field_name'].startswith('Reserved') and self.form.ui.checkBoxReserved.isChecked():
                            continue

                        if i_row >= table.rowCount():
                            table.insertRow(i_row)
                        table.setItem(i_row, 2, QTableWidgetItem(field['field_name']))
                        checkBox = QCheckBox()
                        if (not field['field_name'].startswith('Reserved')) and command_item.checkState(0) == Qt.CheckState.Checked and dword_item.checkState(0) == Qt.CheckState.Checked and field['CHECK'] == 'Y':
                            checkBox.setCheckState(Qt.CheckState.Checked)
                        checkBox.stateChanged.connect(self.check_box_change)
                        table.setCellWidget(i_row, 5, checkBox)
                        if self.form.mode == 'bin':
                            table.setItem(i_row, 3, QTableWidgetItem(bin(int(field['default_value'], 16))))
                            table.setItem(i_row, 4, QTableWidgetItem(bin(int(field['value'], 16))))
                            table.setItem(i_row, 7, QTableWidgetItem(bin(int(field['min_value'], 16))))
                            table.setItem(i_row, 8, QTableWidgetItem(bin(int(field['max_value'], 16))))
                        elif self.form.mode == 'dec':
                            table.setItem(i_row, 3, QTableWidgetItem(str(int(field['default_value'], 16))))
                            table.setItem(i_row, 4, QTableWidgetItem(str(int(field['value'], 16))))
                            table.setItem(i_row, 7, QTableWidgetItem(str(int(field['min_value'], 16))))
                            table.setItem(i_row, 8, QTableWidgetItem(str(int(field['max_value'], 16))))
                        else:
                            table.setItem(i_row, 3, QTableWidgetItem(field['default_value']))
                            table.setItem(i_row, 4, QTableWidgetItem(field['value']))
                            table.setItem(i_row, 7, QTableWidgetItem(field['min_value']))
                            table.setItem(i_row, 8, QTableWidgetItem(field['max_value']))
                        table.item(i_row, 3).setFlags(Qt.NoItemFlags)
                        if 'Address' in field:
                            table.setItem(i_row, 6, QTableWidgetItem(field['Address']))
                        else:
                            table.setItem(i_row, 6, QTableWidgetItem('N'))
                        table.setItem(i_row, 9, QTableWidgetItem(field['bitfield_l']))
                        table.item(i_row, 3).setFlags(Qt.NoItemFlags)
                        table.setItem(i_row, 10, QTableWidgetItem(field['bitfield_h']))
                        table.item(i_row, 3).setFlags(Qt.NoItemFlags)
                        self.form.row_command_map.append({'frame_idx': idx['frame_idx'],'command_idx': command['index'], 'dword_idx': dword_idx})
                        i_row += 1

            table.resizeColumnsToContents()
            table.resizeRowsToContents()

    @Slot()
    def show_command_info(self):
        # self.form.ui = Form()
        # self.form.ui.setupUi(self.form)
        self.form.setWindowTitle(self.test_name)
        tree = self.form.ui.treeWidgetCmd
        header = self.form.ui.treeWidgetCmd.header()
        header.setSectionResizeMode(QHeaderView.ResizeToContents)
        header.setStretchLastSection(False)

        tree.itemDoubleClicked.connect(self.form.save)
        tree.itemDoubleClicked.connect(self.show_command_table)
        tree.itemClicked.connect(self.form.save)
        tree.itemClicked.connect(self.show_command_table)
        tree.itemChanged.connect(self.form.update_tree_checkstate)
        # tree.itemChanged.connect(self.show_command_table)
        test = QTreeWidgetItem(tree)
        test.setText(0, self.test_name)
        for frame_idx in range(len(self.command_info)):
            ##print('frame idx:' + str(frame_idx))
            frame = QTreeWidgetItem(test)
            frame.setText(0, 'frame' + str(frame_idx))
            frame.setData(2, 1, {'frame_idx': frame_idx, 'cmd_idx': 'all'})
            for command_idx, command in enumerate(self.command_info[frame_idx]):
                # #print(command_idx)
                ##print('command idx:' + str(command_idx))
                cmd = QTreeWidgetItem(frame)
                cmd.setText(0, command['name'])
                if command['name'] in self.command_filter:
                    cmd.setCheckState(0, Qt.CheckState.Unchecked)
                else:
                    cmd.setCheckState(0, Qt.CheckState.Checked)
                cmd.setData(2, 1, {'frame_idx': frame_idx, 'cmd_idx': command_idx})
                for dword_idx in range(len(command['dwords'])):
                    dword = QTreeWidgetItem(cmd)
                    #print('dword_idx' + str(dword_idx))
                    dword.setText(0, 'dword' + command['dwords'][dword_idx]['NO'])
                    if command['name'] in self.command_filter:
                        dword.setCheckState(0, Qt.CheckState.Unchecked)
                    else:
                        dword.setCheckState(0, Qt.CheckState.Checked)
                    dword.setData(2, 1, {'frame_idx': frame_idx, 'cmd_idx': command_idx, 'dword_idx': dword_idx})
                    for field_obj in command['dwords'][dword_idx]['fields']:
                        if 'field_name' in field_obj:
                            field = QTreeWidgetItem(dword)
                            field.setText(0, field_obj['field_name'])

        # for command in self.command_info:
        #     fields_num = 0
        #     for dword in command['dwords']:
        #         for key in dword:
        #             if key != 'value' and key != 'unmappedstr':
        #                 fields_num += 1
        #     # #print(command)
        #     # #print(fields_num)
        #     # #print(i_row)
        #     if i_row >= self.form.ui.cmdTable.rowCount():
        #         self.form.ui.cmdTable.insertRow(i_row)
        #
        #     table.setItem(i_row, 0, QTableWidgetItem(command['name']))
        #     for dword in command['dwords']:
        #         for key, value in dword.items():
        #             if key == 'value' or key == 'unmappedstr':
        #                 continue
        #             # #print(i_row)
        #             if i_row >= self.form.ui.cmdTable.rowCount():
        #                 self.form.ui.cmdTable.insertRow(i_row)
        #             table.setItem(i_row, 1, QTableWidgetItem(key))
        #             checkBox = QCheckBox()
        #
        #             checkBox.setCheckState(Qt.CheckState.Checked)
        #             checkBox.stateChanged.connect(self.check_box_change)
        #             table.setCellWidget(i_row, 3, checkBox)
        #             table.setItem(i_row, 2, QTableWidgetItem(value['default_value']))
        #             table.setItem(i_row, 4, QTableWidgetItem(value['Address']))
        #             table.setItem(i_row, 5, QTableWidgetItem(value['min_value']))
        #             table.setItem(i_row, 6, QTableWidgetItem(value['max_value']))
        #             i_row += 1
        # table.resizeColumnsToContents()
        # table.resizeRowsToContents()
        self.form.show()

    @Slot(int)
    def check_box_change(self, row_id):
        print(row_id)

    def parse_command_file(self):
        print('begin parse command file')
        self.ui.logBrowser.append('Begin parse vcs ring info\n')
        self.ui.logBrowser.append('It may take about 30 seconds to finish parsing.\n')
        QCoreApplication.processEvents()
        #self.ringinfo_path = self.ringinfo_path.strip()
        #if self.ringinfo_path[-1] != '\\':
        #    self.ringinfo_path = self.ringinfo_path + '\\'
        self.command_xml = os.path.join(self.ringinfo_path, 'mapringinfo.xml')
        #main(self.ringinfo_path, self.command_xml, self.media_path)
        #init
        start = time.clock()
        #self.obj = CmdFinder(self.media_path, 12, self.ringinfo_path)
        Buf = self.obj.h2xml()
        self.obj.extractfull()
        self.obj.writexml()
        elapsed = (time.clock() - start)
        print("Total Time used:",elapsed)
        #
        print('end parse command file')
        self.ui.logBrowser.append('End parse vcs ring info\n')
        self.ui.logBrowser.append('Save xml in '+ self.ringinfo_path + '\n')

    def read_info_from_ui(self):
        if self.ui.lineEditComponent.text():
            self.component = self.ui.lineEditComponent.text()
        else:
            self.component = self.ui.comboBoxComponent.currentText()
        if self.ui.lineEditPlatform.text():
            self.platform = self.ui.lineEditPlatform.text()
        else:
            self.platform = self.ui.comboBoxPlatform.currentText()
        self.test_name = self.ui.lineEditTestName.text()
        self.source_path = self.ui.lineEditMediaPath.text().replace('/', '\\').strip()
        if self.component in ('vp', 'VP'):
            self.output_path = self.ui.lineEditMediaPath.text() + '\\media_embargo\\media_driver_next\\ult\\windows\\vp\\test\\test_data'
        else:
            self.output_path = self.ui.lineEditMediaPath.text() + '\\media_embargo\\media_driver_next\\ult\\windows\\codec\\test\\test_data'
        if not os.path.exists(self.output_path):
            os.makedirs(self.output_path)
        self.media_path = self.ui.lineEditMediaPath.text()
        self.ringinfo_path = self.ui.lineEditRinginfoPath.text()
        self.test_name = self.ui.lineEditTestName.text()
        # build CMDFinder obj
        self.obj = CmdFinder(self.media_path, 12, self.ringinfo_path)

    def read_command_info_from_xml(self):
        #self.read_info_from_ui()
        #self.parse_command_file()
        # if self.ui.lineEditFrame.text():
        #     self.frame_num = int(self.ui.lineEditFrame.text())
        # else:
        #     self.ui.logBrowser.append('Please input frame count\n')
        #     return
        tree = etree.parse(self.command_xml)
        root = tree.getroot()[0]
        frames = []
        for frame in root:
            ##print(frame.tag)
            commands = []
            # self.test_class_name = root.get('name')
            for command in frame:
                info = {'dwords': []}
                for command_tag in self.command_tags:
                    info[command_tag] = command.get(command_tag)
                if info['name'] in self.command_filter:
                    info['check'] = 'N'
                else:
                    info['check'] = 'Y'
                # f_other_cmd = False

                for dword in command:
                    # dword_t = {}
                    # dword_t['value'] = dword.get('value')
                    # dword_t['NO'] = dword.get('NO')
                    # if dword.get('otherCMD'):
                    #     f_other_cmd = True
                    #     other_cmd = {'name': dword.get('otherCMD'), 'class': command.get('class'), 'dwords': []}
                    #     dword_t['NO'] = '0'
                    #     for field in dword:
                    #         dword_t[field.tag] = {}
                    #         if field.tag.startswith('Obj'):
                    #             for obj_field in field:
                    #                 dword_t[field.tag][obj_field.tag] = {}
                    #                 for key, value in obj_field.items():
                    #                     dword_t[field.tag][obj_field.tag][key] = value
                    #         else:
                    #             for key, value in field.items():
                    #                 dword_t[field.tag][key] = value
                    #     other_cmd['dwords'].append(dword_t)
                    #     commands.append(other_cmd)
                    # if not dword_t['value']:
                    #     dword_t['unmappedstr'] = dword.get('unmappedstr')
                    dword_info = {'fields': []}
                    for dword_tag in self.dword_tags:
                        dword_info[dword_tag] = dword.get(dword_tag)

                    if dword_info['NO'] == '0':
                        dword_info['check'] = 'Y'
                    else:
                        dword_info['check'] = 'Y'
                    # if dword_info['otherCMD']:
                    #     f_other_cmd = True

                    for field in dword:
                        field_info = {'field_name': field.tag}
                        if field.tag.startswith('Obj'):
                            field_info = {'value': field.get('value'), 'obj_fields': [], 'field_name': field.tag}
                            for obj_field in field:
                                obj_field_info = {'obj_field_name': obj_field.tag}
                                for key, value in obj_field.items():
                                    obj_field_info[key] = value
                                if 'default_value' in obj_field_info:
                                    obj_field_info['value'] = obj_field_info['default_value']
                                field_info['obj_fields'].append(obj_field_info)
                        else:
                            for key, value in field.items():
                                field_info[key] = value
                            if 'default_value' in field_info:
                                field_info['value'] = field_info['default_value']
                        dword_info['fields'].append(field_info)
                    info['dwords'].append(dword_info)
                # if not f_other_cmd:
                commands.append(info)
            # for command_idx, command in enumerate(commands):
            #     command['index'] = command_idx
            frames.append(commands)
        self.command_info = frames
        self.dw_length_check()
        self.ui.lineEditFrame.setText(str(len(frames)))

        self.ui.logBrowser.append('Read infomation from ' + self.command_xml + '\n')
        self.form.info = self.command_info
        ##print(self.command_info)
        self.show_command_info()
        # self.update_cmd_list()

    def dw_length_check(self):
        s = ''
        for frame_idx, frame in enumerate(self.command_info):
            for command_idx, command in enumerate(frame):
                # DW0_dwlen = item_text_to_dec(command['input_dwsize'])
                input_dwsize = 0
                if 'input_dwsize' in command and command['input_dwsize']:
                    input_dwsize = item_text_to_dec(command['input_dwsize'])
                if 'def_dwsize' in command and command['def_dwSize']:
                    def_dwsize = item_text_to_dec(command['def_dwSize'])
                if command['dwords']:
                    last_dword_no = command['dwords'][-1]['NO']
                    if last_dword_no.find('_') != -1:
                        idx = last_dword_no.rfind('_')
                        last_dword_no = last_dword_no[idx + 1:]
                    last_dword_no = int(last_dword_no)
                    # #print(last_dword_no)
                    # #print(input_dwsize)
                    ##print(command['input_dwsize'])
                    if last_dword_no and input_dwsize and last_dword_no > input_dwsize:
                        s = 'frame ' + str(frame_idx) + ' command ' + str(command_idx) + ' ' + command['name'] + 'wrong dword length. \n'
                        s = s + 'Suggest ' + str(hex(last_dword_no)) + ' intstead\n\n'
        if s:
            self.show_message(s, 'Error')

    def show_message(self, inf, title):
        msgBox = QMessageBox()
        msgBox.setWindowTitle(title)
        msgBox.setInformativeText(inf)
        msgBox.exec_()

    def split_dword(self):
        for frame_idx, frame in enumerate(self.command_info):
            for cmd_idx, cmd in enumerate(frame):
                for dword_idx, dword in enumerate(cmd['dwords']):
                    if dword['NO'].find('_') != -1:
                        ##print(dword_idx)
                        ##print(dword['NO'])
                        idx = dword['NO'].find('_')
                        l = dword['NO'][:idx]
                        r = dword['NO'][idx+1:]
                        cmd['dwords'][dword_idx]['NO'] = l
                        next_dword = copy.deepcopy(dword)
                        next_dword['NO'] = r
                        next_dword['value'] = '0x' + dword['value'][10:]
                        if next_dword['value'] == '0x':
                            next_dword['value'] = '0x0'
                        dword['value'] = dword['value'][:10]
                        split_field_idx = -1
                        for field_idx, field in enumerate(dword['fields']):
                            if int(field['bitfield_h'])> 31 and int(field['bitfield_l']) <= 31:
                                split_field_idx = field_idx
                                break
                        if split_field_idx >= 0:
                            dword['fields'][split_field_idx]['bitfield_h'] = '31'
                            dword['fields'] = dword['fields'][:split_field_idx + 1]
                            dword['fields'][split_field_idx]['have_following'] = 'Y'
                            next_dword['fields'][split_field_idx]['bitfield_l'] = '32'
                            next_dword['fields'] = next_dword['fields'][split_field_idx:]
                            next_dword['fields'][0]['have_precursor'] = 'Y'
                            ##print('have_precursor')
                            for field in next_dword['fields']:
                                field['bitfield_l'] = str(int(field['bitfield_l']) - 32)
                                field['bitfield_h'] = str(int(field['bitfield_h']) - 32)
                            len1 = int(dword['fields'][split_field_idx]['bitfield_h']) - int(dword['fields'][split_field_idx]['bitfield_l'])
                            len1 = len1//4
                            if dword['fields'][split_field_idx]['default_value'] != 0:
                                dword['fields'][split_field_idx]['default_value'] = dword['fields'][split_field_idx]['default_value'][:2+len1]
                                dword['fields'][split_field_idx]['value'] = dword['fields'][split_field_idx]['default_value']
                                dword['fields'][split_field_idx]['min_value'] = dword['fields'][split_field_idx]['default_value']
                                dword['fields'][split_field_idx]['max_value'] = dword['fields'][split_field_idx]['default_value']
                                next_dword['fields'][0]['default_value'] = '0x' + next_dword['fields'][0]['default_value'][2+len1:]
                                next_dword['fields'][0]['value'] = next_dword['fields'][0]['default_value']
                                next_dword['fields'][0]['min_value'] = next_dword['fields'][0]['default_value']
                                next_dword['fields'][0]['max_value'] = next_dword['fields'][0]['default_value']
                        cmd['dwords'].insert(dword_idx+1, next_dword)

    @Slot()
    def generate_xml(self):
        if not self.command_info:
            pass
        self.split_dword()
        lines = ['<?xml version="1.0"?>\n']
        lines.append('<' + self.test_name + '>\n')
        lines.append('  <Platform name="' + self.platform + '">\n')
        for frame_idx, frame in enumerate(self.command_info):
            # #print('frame' + str(frame_idx))
            lines.append('    <Frame NO="' + str(frame_idx + 1) + '">\n')
            for cmd_idx, cmd in enumerate(frame):
                # #print('cmd' + str(cmd_idx))
                s_cmd = '      <CMD'
                for key, value in cmd.items():
                    if key != 'dwords' and value:
                        s_cmd = s_cmd + ' ' + key + '="' + str(value) + '"'
                s_cmd  = s_cmd + '>\n'
                lines.append(s_cmd)
                # lines.append('      <CMD index="' + str(cmd_idx) + '" name="' + cmd['name'] + '" class="' + cmd['class'] + '">\n ')
                for dword_idx, dword in enumerate(cmd['dwords']):
                    s_dword = '        <dword'
                    for key, value in dword.items():
                        if key != 'fields' and value:
                            s_dword = s_dword + ' ' + key + '="' + str(value) + '"'
                    s_dword = s_dword + '>\n'
                    lines.append(s_dword)
                    # if 'unmappedstr' in dword:
                    #     continue
                    # if dword['value']:
                    #     lines.append('        <dword index="' + dword['NO'] + '" value="' + dword['value'] + '">\n')
                    # elif dword['unmappedstr']:
                    #     lines.append('        <dword index="' + dword['NO'] + '" unmappedstr="' + dword['unmappedstr'] + '">\n')
                    for field in dword['fields']:
                        # if 'CHECK' in field and field['CHECK'] == 'Y':
                        if not field['field_name'].startswith('Reserve'):
                            s_field = '          <' + field['field_name']
                            for key, value in field.items():
                             #   if key == 'have_precursor':
                             #        #print('have_precursor')
                                if key != 'field_name':
                                    s_field = s_field + ' ' + key + '="' + str(value) + '"'
                            s_field = s_field + '/>\n'
                            lines.append(s_field)
                    lines.append('        </dword>\n')
                lines.append('      </CMD>\n')
            lines.append('    </Frame>\n')
        lines.append('  </Platform>\n')
        lines.append('</' + self.test_name + '>\n')
        file_name = self.test_name + '.xml'
        with open(self.output_path + '\\' + file_name, 'w') as fout:
            fout.writelines(lines)
        self.ui.logBrowser.append('Generating modified command xml' + self.output_path + '\\' + file_name + '\n')
        self.show_message('Generating modified command xml' + self.output_path + '\\' + file_name + '\n', '')

    #
    @Slot()
    def selectpath(self, name):
        #open file dialog and display directory in the text edit area
        if self.last_dir:
            dir = QFileDialog.getExistingDirectory(self, "Open Directory",
                                           self.last_dir,
                                           QFileDialog.ShowDirsOnly
                                           | QFileDialog.DontResolveSymlinks)
        else:
            dir = QFileDialog.getExistingDirectory(self, "Open Directory",
                                           "/home",
                                           QFileDialog.ShowDirsOnly
                                           | QFileDialog.DontResolveSymlinks)
        self.last_dir = dir
        if name == 'Media':
            self.ui.lineEditMediaPath.setText(dir)
        if name == 'DDIInput':
            self.ui.lineEditDDIInputPath.setText(dir)
            self.ddiinputpath = self.ui.lineEditDDIInputPath.text()
        if name == 'Ringinfo':
            self.ui.lineEditRinginfoPath.setText(dir)

    @Slot()
    def addHeader(self):
        # click OK, generate xml header
        self.read_info_from_ui()
        self.Component = self.ui.Component_input.text()
        self.GUID = self.ui.GUID_input.text()
        self.Width = self.ui.Width_input.text()
        self.Height = self.ui.Height_input.text()
        self.inputpath = self.ui.InputPathText.text()
        self.RawTileType = self.ui.RawTileType_input.text()
        self.RawFormat = self.ui.RawFormat_input.text()
        self.ResTileType = self.ui.ResTileType_input.text()
        self.ResFormat = self.ui.ResFormat_input.text()
        self.EncFunc = self.ui.EncFunc_input.text()
        self.FrameNum = self.ui.FrameNum_input.text()
        # get real Frame Number according to input files

        Frameset = set()
        for f in os.listdir(self.inputpath):
            pattern = re.search('^(\d)-0.*DDIEnc_(.*)Params_._Frame', f)
            if pattern:
                Frameset.add(int(pattern.group(1)))
        self.FrameNumdiff = min(Frameset) - 0
        self.FrameNum = str(len(Frameset))
        self.ui.FrameNum_input.setText(self.FrameNum)
        # combine input files and parameters
        self.combine()
        self.ui.logBrowser.append("The input file has been generated in " + self.output_path +".\n")
        #pop out message box
        #msgBox = QMessageBox()
        #msgBox.setText("The input file has been generated.")
        #msgBox.exec_()

        self.parse_command_file()
        self.read_command_info_from_xml()
        self.ui.tabWidget.setCurrentIndex(0)
        if self.ui.lineEditFrame.text() != self.FrameNum:
            msgBox = QMessageBox()
            msgBox.setText("Inconsistent Frame number!")
            msgBox.exec_()
        self.form.showcmdlist()

    @Slot()
    def reject(self):
        # click cancel, exit
        sys.exit(app.exec_())

    def combine(self):
        #combine ddi_input text files and add header infomation

        with open(os.path.join(self.output_path, 'encodeHevcCQPInput.dat'),'w') as wfd:
            #wfd.write('<Header Component=%s  GUID=%s Width=%s Height=%s OutputFormat=%s>\n' % (self.Component, self.GUID, self.Width, self.Height, self.OutputFormat))
            wfd.write(f'''<Header>
Component = {self.Component}
GUID = DXVA2_Intel_LowpowerEncode_HEVC_Main
Width = {self.Width}
Height = {self.Height}
#MOS_TILE_Y
RawTileType = {self.RawTileType}
#Format_NV12
RawFormat = {self.RawFormat}
#MOS_TILE_LINEAR
ResTileType = {self.ResTileType}
#Format_Buffer
ResFormat = {self.ResFormat}
#ENCODE_ENC_PAK, 4
EncFunc = {self.EncFunc}
FrameNum = {self.FrameNum}
''')
            #wfd.write('</Header>')

            for f in os.listdir(self.inputpath):
                pattern = re.search('^(\d)-0.*DDIEnc_(.*)Params_._Frame', f)
                if pattern:
                    FrameNo = str(int(pattern.group(1))-self.FrameNumdiff)
                    ParaGroup = pattern.group(2)
                    wfd.write('<Frame No=%s  Param=%s >\n' % (FrameNo, ParaGroup))
                    with open(os.path.join(self.inputpath, f), 'r') as file:
                        content = file.readlines()
                        new_content = []
                        for line in content:
                            clean_line = line.replace('\00', '')
                            new_content.append(clean_line)
                    wfd.writelines(new_content)
                    #wfd.write('</Frame>\n')

class FormCommandInfo(QWidget):
    def __init__(self, main_window):
        super(FormCommandInfo, self).__init__()
        self.ui = Ui_FormCommandInfo()
        self.ui.setupUi(self)
        self.info = []
        self.main_window = main_window
        self.ui.pushButtonSave.clicked.connect(self.save)
        self.ui.pushButtonGen.clicked.connect(self.main_window.generate_xml)
        self.current_frame = 0
        self.current_command = ''
        self.mode = 'hex'
        self.first = True
        self.row_command_map = []
        self.ui.checkBoxHex.stateChanged.connect(self.update_data_mode_hex)
        self.ui.checkBoxDec.stateChanged.connect(self.update_data_mode_dec)
        self.ui.checkBoxBinary.stateChanged.connect(self.update_data_mode_bin)
        self.ui.checkBoxReserved.stateChanged.connect(self.update_reserve_show)
        self.current_item = None
        #
        self.ui.stackedWidget.setCurrentIndex(1)   #set the all infomation page as default page
        self.ui.pushButtonSCL.clicked.connect(lambda : self.ui.stackedWidget.setCurrentIndex(1))  #show cmd name list
        self.ui.pushButtonSCL.clicked.connect(self.showcmdlist)  #show cmd name list
        self.ui.pushButtonSA.clicked.connect(lambda : self.ui.stackedWidget.setCurrentIndex(0))  #show cmd name list
        self.ui.pushButtonSU.clicked.connect(self.updateinfo)

    def show_message(self, inf, title):
        msgBox = QMessageBox()
        msgBox.setWindowTitle(title)
        if title == 'Save':
            msgBox.addButton(QMessageBox.Ok)
            msgBox.addButton(QMessageBox.No)
        msgBox.setInformativeText(inf)
        msgBox.exec_()

    def check(self):
        s = ''
        table = self.ui.tableWidgetCmd
        for i in range(table.rowCount()):
            if not table.item(i, 2):
                continue
            if table.cellWidget(i, 5).isChecked():
                command = self.info[int(self.row_command_map[i]['frame_idx'])][int(self.row_command_map[i]['command_idx'])]
                dword = 'dword' + command['dwords'][int(self.row_command_map[i]['dword_idx'])]['NO']
                field = str(table.item(i, 2).text())
                value = str(table.item(i, 4).text())

                min_value = str(table.item(i, 7).text())
                max_value = str(table.item(i, 8).text())
                # value = item_text_to_dec(value)
                # min_value = item_text_to_dec(min_value)
                # max_value = item_text_to_dec(max_value)
                if max_value < min_value:
                    s = s + 'Command ' + command['name'] + ' ' + dword + ' max value smaller than min value\n\n'
                    # s = s + 'Row' + str(i) + ' max value smaller than min value\n\n'
                if value < min_value:
                    s = s + 'Command ' + command['name'] + ' ' + dword + ' value smaller than min value\n\n'
                if value > max_value:
                    s = s + 'Command ' + command['name'] + ' ' + dword + ' value larger than max value\n\n'

        if s:
            self.show_message(s, 'Error')
            return -1
        else:
            return 0

    @Slot()
    def save(self):
        table = self.ui.tableWidgetCmd
        tree = self.ui.treeWidgetCmd
        if not self.first:
            self.show_message('Save information', 'Save')
        else:
            self.first = False
            # for i in range(table.rowCount()):
            #     cmd_name = str(table.item(i, 0))
            #     if cmd_name not in cmds:
            #         cmds.add(cmd_name)
            #         info.append({'name': cmd_name, 'fields': {}})
            #     if table.cellWidget(i, 3).checkState():
            #         field_name = str(table.item(i, 1))
            #         info[-1]['fields'][field_name] = {}
            #         info[-1]['fields']['defalt_value'] = str(table.item(i, 2))
            #         info[-1]['fields']['Address'] = str(table.item(i, 4))
            #         info[-1]['fields']['min_value'] = str(table.item(i, 5))
            #         info[-1]['fields']['max_value'] = str(table.item(i, 6))
        if self.check() != 0:
            return
        for i in range(table.rowCount()):
            dword = self.info[int(self.row_command_map[i]['frame_idx'])][int(self.row_command_map[i]['command_idx'])]['dwords'][int(self.row_command_map[i]['dword_idx'])]
            if dword:
                if not table.item(i, 2):
                    continue
                field_name = str(table.item(i, 2).text())
                for field in dword['fields']:
                    if field['field_name'] == field_name:
                        field['value'] = str(table.item(i, 4).text())
                        if table.cellWidget(i, 5).isChecked():
                            field['CHECK'] = 'Y'
                        else:
                            field['CHECK'] = 'N'
                        if self.mode == 'bin':
                            field['value'] = hex(int(str(table.item(i, 4).text()), 2))
                            field['min_value'] = hex(int(str(table.item(i, 7).text()), 2))
                            field['max_value'] = hex(int(str(table.item(i, 8).text()), 2))
                        elif self.mode == 'dec':
                            field['value'] = hex(int(str(table.item(i, 4).text())))
                            field['min_value'] = hex(int(str(table.item(i, 7).text())))
                            field['max_value'] = hex(int(str(table.item(i, 8).text())))
                        else:
                            field['value'] = str(table.item(i, 4).text())
                            field['min_value'] = str(table.item(i, 7).text())
                            field['max_value'] = str(table.item(i, 8).text())

    @Slot(QTreeWidgetItem, int)
    def update_tree_checkstate(self, item, column):
        for i in range(item.childCount()):
            dword = item.child(i)
            if item.checkState(0) == Qt.CheckState.Checked:
                dword.setCheckState(0, Qt.CheckState.Checked)
            else:
                dword.setCheckState(0, Qt.CheckState.Unchecked)

    @Slot()
    def save(self):
        table = self.ui.tableWidgetCmd
        tree = self.ui.treeWidgetCmd
            # for i in range(table.rowCount()):
            #     cmd_name = str(table.item(i, 0))
            #     if cmd_name not in cmds:
            #         cmds.add(cmd_name)
            #         info.append({'name': cmd_name, 'fields': {}})
            #     if table.cellWidget(i, 3).checkState():
            #         field_name = str(table.item(i, 1))
            #         info[-1]['fields'][field_name] = {}
            #         info[-1]['fields']['defalt_value'] = str(table.item(i, 2))
            #         info[-1]['fields']['Address'] = str(table.item(i, 4))
            #         info[-1]['fields']['min_value'] = str(table.item(i, 5))
            #         info[-1]['fields']['max_value'] = str(table.item(i, 6))
        # self.show_message('Save command info ', 'Save')
        if self.check() != 0:
            return
        for i in range(table.rowCount()):
            dword = self.info[int(self.row_command_map[i]['frame_idx'])][int(self.row_command_map[i]['command_idx'])]['dwords'][int(self.row_command_map[i]['dword_idx'])]
            # #print('find_dword')
            if dword:
                if not table.item(i, 2):
                    continue
                # #print('table.item')
                field_name = str(table.item(i, 2).text())
                for field in dword['fields']:
                    if field['field_name'] == field_name:

                        if table.cellWidget(i, 5).isChecked():
                            field['CHECK'] = 'Y'
                        else:
                            field['CHECK'] = 'N'
                        field['Address'] = str(table.item(i, 6).text())
                        if self.mode == 'bin':
                            field['value'] = hex(int(str(table.item(i, 4).text()), 2))
                            field['min_value'] = hex(int(str(table.item(i, 7).text()), 2))
                            field['max_value'] = hex(int(str(table.item(i, 8).text()), 2))
                        elif self.mode == 'dec':
                            field['value'] = hex(int(str(table.item(i, 4).text())))
                            field['min_value'] = hex(int(str(table.item(i, 7).text())))
                            field['max_value'] = hex(int(str(table.item(i, 8).text())))
                        else:
                            field['value'] = str(table.item(i, 4).text())
                            field['min_value'] = str(table.item(i, 7).text())
                            field['max_value'] = str(table.item(i, 8).text())

        self.main_window.command_info = self.info

    @Slot(QTreeWidgetItem, int)
    def update_tree_checkstate(self, item, column):
        for i in range(item.childCount()):
            dword = item.child(i)
            if item.checkState(0) == Qt.CheckState.Checked:
                dword.setCheckState(0, Qt.CheckState.Checked)
            else:
                dword.setCheckState(0, Qt.CheckState.Unchecked)

    @Slot()
    def update_data_mode_hex(self):
        if self.ui.checkBoxHex.isChecked():
            self.mode = 'hex'
            self.ui.checkBoxDec.setCheckState(Qt.CheckState.Unchecked)
            self.ui.checkBoxBinary.setCheckState(Qt.CheckState.Unchecked)
            if self.current_item:
              self.main_window.show_command_table(self.current_item)

    @Slot()
    def update_data_mode_dec(self):
        if self.ui.checkBoxDec.isChecked():
            self.mode = 'dec'
            self.ui.checkBoxHex.setCheckState(Qt.CheckState.Unchecked)
            self.ui.checkBoxBinary.setCheckState(Qt.CheckState.Unchecked)
            if self.current_item:
              self.main_window.show_command_table(self.current_item)

    @Slot()
    def update_reserve_show(self):
        if self.current_item:
            self.main_window.show_command_table(self.current_item)

    @Slot()
    def update_data_mode_bin(self):
        if self.ui.checkBoxBinary.isChecked():
            self.mode = 'bin'
            self.ui.checkBoxDec.setCheckState(Qt.CheckState.Unchecked)
            self.ui.checkBoxHex.setCheckState(Qt.CheckState.Unchecked)
            if self.current_item:
              self.main_window.show_command_table(self.current_item)

    @Slot()
    def showcmdlist(self):
        #print(self.main_window.obj.size_error_cmd)
        #print(self.main_window.obj.size_error)
        self.ui.tableWidgetCmdlist.clearContents()
        self.ui.tableWidgetCmdlist.setRowCount(0)
        self.table = self.ui.tableWidgetCmdlist
        self.table.cellDoubleClicked.connect(self.modifycmd)
        row = 0
        ##print(self.main_window.obj.ringcmddic)
        for cmd, index in self.main_window.obj.ringcmddic.items():
            self.table.insertRow(row)
            self.table.setItem(row, 0, QTableWidgetItem(cmd))
            self.table.setItem(row, 1, QTableWidgetItem(str(index)))
            if cmd in self.main_window.obj.notfoundset:
                self.table.setItem(row, 2, QTableWidgetItem('Not Found'))
            if self.main_window.obj.size_error_cmd[cmd]:
                warning = 'Size Error in position: '
                for i in self.main_window.obj.size_error_cmd[cmd]:
                    warning += str(i) + ','
                warning = warning.strip(',')
                self.table.setItem(row, 2, QTableWidgetItem(warning))
            row += 1
        self.cmdlistrow = row
        self.table.resizeColumnsToContents()
        self.table.resizeRowsToContents()

    @Slot()
    def modifycmd(self, row, column):
        ##print("Row %d and Column %d was clicked" % (row, column))
        item = self.table.item(row, column)
        cmdname = self.table.item(row, 0).text()
        ##print(item.text())
        minimum_value = 1
        maximum_value = int(self.table.item(row, 1).text())
        index = ''
        new = ''
        #if column == 0:
        #    new, ok = QInputDialog.getText(self.table,  "Modify", "CMD Name", QLineEdit.Normal, item.text())
        #    if ok:
        #        if new != cmdname:
        #            self.table.setItem(row, 0, QTableWidgetItem(new))
        #            self.table.item(row, 0).setTextColor(QColor(255,0,0))
        #            index = 'all'
        #if column == 1:
        new, ok = QInputDialog.getText(self.table,  "Modify", "CMD Name", QLineEdit.Normal, self.table.item(row, 0).text())
        if ok:
            input_index, ok = QInputDialog.getText(self.table,  "Modify", "\tScope(1-%s)\ne.g. 1-4,6" %maximum_value, QLineEdit.Normal, 'Apply to All')
            if input_index != 'Apply to All':
                index = []
                list = input_index.strip().split(',')
                for i in list:
                    if '-' in i:
                        i = i.split('-')
                        index = list(range(int(i[0]), int(i[1])+1))
                    else:
                        index.append(int(i))
                length = len(index)
                if int(maximum_value) > length:
                    self.table.setItem(row, 1, QTableWidgetItem(str(maximum_value-length)))
                    self.table.item(row, 1).setTextColor(QColor(255,0,0))
                    self.table.insertRow(row+1)
                    self.table.setItem(row+1, 1, QTableWidgetItem(str(length)))
                    self.table.item(row+1, 1).setTextColor(QColor(255,0,0))
                    self.table.setItem(row+1, 0, QTableWidgetItem(new))
                    self.table.item(row+1, 0).setTextColor(QColor(255,0,0))
                    self.cmdlistrow += 1
                elif int(maximum_value) == length:
                    index = 'all'
            else:
                index = 'all'
            if new != cmdname and index == 'all':
                self.table.setItem(row, 0, QTableWidgetItem(new))
                self.table.item(row, 0).setTextColor(QColor(255,0,0))
        if new and index:
            #print(self.main_window.obj.ringcmddic)
            self.main_window.obj.modifyringcmd(cmdname, new, index)
            #print(self.main_window.obj.ringcmddic)

    @Slot()
    def updateinfo(self):
        self.main_window.obj.undate_full_ringinfo()
        self.main_window.obj.updatexml()
        self.main_window.ui.logBrowser.append('Update xml\n')
        self.ui.tableWidgetCmd.clearContents()
        self.ui.treeWidgetCmd.clear()
        self.main_window.ui.logBrowser.append('Reload...\n')
        #pop out message box
        msgBox = QMessageBox()
        msgBox.setText("Success!")
        msgBox.exec_()
        self.main_window.read_command_info_from_xml()
        self.showcmdlist()

def item_text_to_dec(s):
    if s.startswith('0x'):
        return int(s, 16)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())


from re import T
import cv2
import numpy as np
from cv2 import aruco

DICCIONARIO = aruco.getPredefinedDictionary(aruco.DICT_5X5_50)
lena = cv2.imread("R.png")
h, w, _ = lena.shape
vlena = np.array([[0,0], [w,0], [w,h], [0,h]])

cap = cv2.VideoCapture(0)

if not cap.isOpened():
    print("No se puede abrir la cámara")
    exit()
else:
    salir = False
while not salir:
    ret, frame = cap.read()

    if not ret:
        print("No he podido leer el frame")
        salir  =True
    else:
        #Aqui procesamos el frame
        gris = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        bboxs, ids, rechazados = aruco.detectMarkers(gris, DICCIONARIO)
        if ids is not None:
            #aruco.drawDetectedMarkers(frame, bboxs, ids)
            vertices = bboxs
            for i in range(len(ids)):
                vertices = bboxs[i][0].astype(int)

                #cv2.line(frame, vertices[0], vertices[2], (0,0,255), 4)
                #cv2.line(frame, vertices[1], vertices[3], (0,0,255), 4)

                #cv2.rectangle(frame, vertices[0], vertices[3], (0,255,255), 5)

                #cv2.circle(frame, vertices[0], 10, (255,0,0), 1)

                #cv2.polylines(frame, [vertices], True, (255,255,0), 2)

                #cv2.fillConvexPoly(frame, vertices, (255,255,255))

                #cv2.putText(frame, str(ids[i]), vertices[1], cv2.FONT_HERSHEY_SIMPLEX, 2, (0,0,0), 1)

                matriz, _ = cv2.findHomography(vlena, vertices)
                warplena = cv2.warpPerspective(lena, matriz, (frame.shape[1], frame.shape[0]))

                cv2.fillConvexPoly(frame, vertices, (0,0,0))

                frame = warplena + frame

        cv2.imshow('WEBCAM', frame )
        if cv2.waitKey(1) == ord(' '):
            salir=True

cap.release()
cv2.destroyWindow('WEBCAM')


from django.contrib import admin
from django.urls import path, include
from dj_rest_auth.views import UserDetailsView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/v1/', include('articles.urls')),
    path('accounts/', include('dj_rest_auth.urls')),
    # path('accounts/user/', UserDetailsView.as_view(), name='user_details'),
    path('accounts/signup/', include('dj_rest_auth.registration.urls')),
    path('movies/', include('movies.urls')),
    path('polls/', include('polls.urls')),
]


import torch as th
import argparse
import os
import numpy as np
import copy
from ema_pytorch import EMA

from .model_and_diffusion_util import *
from .gen_image import get_gen_images

def uniform_sample_timesteps(steps, batch_size):
    indices_np = np.random.choice(steps, size=(batch_size,))
    indices = th.from_numpy(indices_np).long() # .to(device)
    return indices

def update_ema(target_params, source_params, ema_rate=0.99):
    # update target in-place
    for targ, src in zip(target_params, source_params):
        targ.detach().mul_(ema_rate).add_(src, alpha=1 - ema_rate)

def params_to_state_dict(target_params, model):
    state_dict = model.state_dict()
    for i, (name, _value) in enumerate(model.named_parameters()):
        assert name in state_dict
        state_dict[name] = target_params[i]
    return state_dict

def run_loop(model, gd, data, save_desc, lr=1e-3, start_epoch=0, epoch_block=10000, num_its=20, p_uncond=0.0, default_im='im_10.png', ddim_gd=None, latent_orthog=False, ema_rate=0.9999, dataset='clevr', downweight=False, image_size=64, use_dist=False):
    if use_dist: # distributed training
        from .util.dist_util import dev
        device = dev()
    else:
        device = 'cuda' if th.cuda.is_available() else 'cpu'

    # ddim sampling for generating samples per epoch block
    if ddim_gd == None:
        ddim_gd = create_ddim_diffusion(diffusion_defaults())

    # ema_params = copy.deepcopy(list(model.parameters()))
    ema_model = EMA(model, beta=ema_rate, update_every=1)
    ema_model.to('cuda')

    optimizer = th.optim.Adam(model.parameters(),
                                lr=lr)
                                # weight_decay = 1e-8)
    total_epochs = epoch_block * num_its

    save_dir = 'logs_' + save_desc # f'{save_desc}_params/'
    os.makedirs(save_dir, exist_ok=True)
    print(f'Saving model ckpts to {save_dir}')

    free = p_uncond > 0
    for epoch in range(start_epoch, start_epoch + total_epochs):
        batch, cond = next(data)
        batch = batch.to(device)
        model_kwargs = {}

        if not free:
            model_kwargs = dict(latent=None, x_start=batch)
        else:

            b = batch.shape[0]
            rand_values = th.rand(b, 1).to(device)
            keep_mask = rand_values >= p_uncond
            null_emb = th.zeros(b, model.latent_dim_expand).to(device)
            latent_emb = model.encode_latent(batch)
            emb = th.where(
                keep_mask,
                latent_emb,
                null_emb
            )
            model_kwargs = dict(latent=emb)

        t = uniform_sample_timesteps(gd.num_timesteps, len(batch)).to(device)

        loss = gd.training_losses(model, batch, t, model_kwargs=model_kwargs, latent_orthog=latent_orthog, downweight=downweight)
        loss = loss.mean()

        # The gradients are set to zero,
        # the gradient is computed and stored.
        # .step() performs parameter update
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        # update ema params
        # update_ema(ema_params, list(model.parameters()), ema_rate=ema_rate)
        ema_model.update()

        imgs_save_dir = f'gen_imgs_{save_desc}'
        if epoch % epoch_block == 0:
            print(f'img at {epoch} epochs')
            print(f'Saving images to {imgs_save_dir}')
            get_gen_images(model, ddim_gd, im_path=default_im, desc=str(epoch), save_dir=imgs_save_dir, free=free, dataset=dataset, sample_method='ddim', num_images=1, image_size=image_size)

            print('loss:')
            print(loss)

            th.save(model.state_dict(), os.path.join(save_dir, f'model_{epoch}.pt'))
            # save ema params
            # ema_state_dict = params_to_state_dict(ema_params, model)
            th.save(ema_model.state_dict(), os.path.join(save_dir, f'ema_{ema_rate}_{epoch}.pt'))

def create_ema(save_desc, epoch_block=10000, last_epoch=140000):
    save_dir = 'logs_' + save_desc
    defaults = model_defaults()

    parser = argparse.ArgumentParser()
    add_dict_to_argparser(parser, defaults)
    parser.add_argument('--ema_rate', type=float, default=0.99)
    args = parser.parse_args()

    ema_rate = args.ema_rate
    model_kwargs = args_to_dict(args, model_defaults().keys())
    model = create_diffusion_model(**model_kwargs)
    model.eval()
    device = 'cuda'
    model.to(device)
    ema_params = 0

    for epoch in range(0, last_epoch + 1, epoch_block):
        ckpt_path = os.path.join(save_dir, f'model_{epoch}.pt')
        print(f'loading from {ckpt_path}')
        checkpoint = th.load(ckpt_path, map_location='cpu')
        model.load_state_dict(checkpoint)
        if epoch == 0:
            ema_params = copy.deepcopy(list(model.parameters()))
        update_ema(ema_params, list(model.parameters()), ema_rate=ema_rate)
        print(epoch)

        ema_state_dict = params_to_state_dict(ema_params, model)
        th.save(ema_state_dict, os.path.join(save_dir, f'ema_{ema_rate}_{epoch}.pt'))

if __name__=='__main__':
    save_desc = 'unet_model_celebahq_10000_xstart_emb_128'
    create_ema(save_desc)


# -*-coding:utf-8 -*-
import cx_Oracle
import os
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
con = cx_Oracle.connect('scm/develop@10.24.10.224/bdcor5')
cur = con.cursor()
cur.execute('select name from sys_account')
for result in cur:
	print(result[0])

cur.close()
con.close()


# coding:utf-8
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
import settings

from .views import *
urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'aka.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', mainpage),
    url(r'^mainpage/$', mainpage),
    url(r'^mainpageenglish/$', mainpageenglish),
    url(r'simple/$', simple),
    url(r'^simplechinese/$', simpleenglish),
    url(r'^image/(?P<path>.*)$','django.views.static.serve',
        {'document_root': settings.MEDIA_ROOT+'/image', 'show_indexes':True}),

    url(r'^media/(?P<path>.*)$','django.views.static.serve',
        {'document_root': settings.MEDIA_ROOT, 'show_indexes':True}),

    url(r'^jeapmain$', 'jeapmain.views.home'),

    url(r'^android$', 'android.views.mainpage'),

    url(r'^pythonvideo$', 'pythonvideo.views.home', name='home'),
    url(r'^cates$','pythonvideo.views.cates',name='cates'),
    url(r'^courses$','pythonvideo.views.courses',name='courses'),
    url(r'^videos$','pythonvideo.views.videos',name='videos'),
    url(r'^video/(\d+)$','pythonvideo.views.video',name='video'),

    url(r'^game$', 'game.views.home', name='gamehome'),
    url(r'^list$', 'game.views.listpage', name='list'),
    url(r'^teachers$', 'game.views.teachers', name='teachres'),
    url(r'^showpage/(\d+)$', 'game.views.showpage', name='showpage'),
    url(r'^aboutus$', 'game.views.aboutus', name='aboutus'),

    url(r'^apply/eventjoin/(\d+)$', 'apply.views.eventjoin', name='eventjoin'),
    url(r'^apply/joinsave/(\d+)$', 'apply.views.joinsave', name='joinsave'),
    url(r'^apply/eventadmin/(\d+)$', 'apply.views.eventadmin', name='eventadmin'),
    url(r'^apply/confirm/(\d+)/(\d+)$', 'apply.views.confirm', name='confirm'),
    url(r'^apply/delete/(\d+)/(\d+)$', 'apply.views.delete', name='delete'),
    url(r'^apply/listconfirm/(\d+)$', 'apply.views.listuser', name='listuser'),
)


import json
from bs4 import BeautifulSoup
from scraper_api import ScraperAPIClient
import os

def handler(event, context):

    class job:
        def __init__(self, title, company, summary):
            self.title = title
            self.company = company
            self.summary = summary

    search_string = event['queryStringParameters']['search']
    location = event['queryStringParameters']['state']

    client = ScraperAPIClient(os.environ.get('API_KEY'))
    URL = f"https://www.indeed.com/jobs?q={search_string}&l={location}"
    page = client.get(url=URL)
    soup = BeautifulSoup(page.content, 'html.parser')
    results = soup.find(id='resultsCol')
    job_elems = results.find_all('div', class_='jobsearch-SerpJobCard')

    jobs = {
        "jobs" : []
    }
    for job_elem in job_elems:
        title_elem = job_elem.find('h2', class_='title')
        company_elem = job_elem.find('div', class_='sjcl')
        summary_elem = job_elem.find('div', class_='summary')
        jobs['jobs'].append( job(title_elem.text.strip(), company_elem.text.strip(), summary_elem.text.strip()))

    result = json.dumps(jobs, default = lambda x: x.__dict__ )

    return {
        'statusCode': 200,
        'headers': {
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Headers': '*',
            'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
        },
        'body': result.replace("\\n"," ")
    }