import carla
import random
import os
import time
from queue import Queue

def sensor_callback(sensor_data, sensor_queue, sensor_name):
    if 'lidar1' in sensor_name:
        sensor_data.save_to_disk(os.path.join('outputs_cline/lidar1', '%06d.ply' % sensor_data.frame))
    if 'lidar2' in sensor_name:
        sensor_data.save_to_disk(os.path.join('outputs_cline/lidar2', '%06d.ply' % sensor_data.frame))
    if 'camera' in sensor_name:
        sensor_data.save_to_disk(os.path.join('outputs_cline/camera', '%06d.png' % sensor_data.frame))
    sensor_queue.put((sensor_data.frame, sensor_name))

def main():
    actor_list = []
    sensor_list = []

    try:
        # First of all, we need to create the client that will send the requests, assume port is 2000
        client = carla.Client('localhost', 2000)
        client.set_timeout(2.0)

        # Retrieve the world that is currently running
        world = client.get_world()
        # world = client.load_world('Town02') # you can also retrive another world by specifically defining
        blueprint_library = world.get_blueprint_library()

        sensor_queue = Queue()
        ego_vehicle_bp = blueprint_library.find('vehicle.mercedes.coupe_2020')
        # black color
        ego_vehicle_bp.set_attribute('color', '0, 0, 0')
        # get a random valid occupation in the world                                                    # y   z   x
        transform = carla.Transform(carla.Location(x=5.989376, y=207.505676, z=0.275307),
                                    carla.Rotation(pitch=0.000000, yaw=-0.142975, roll=0.000000))
        # transform = random.choice(world.get_map().get_spawn_points()) 598.937622  13.181384
        # spawn the vehilce
        ego_vehicle = world.spawn_actor(ego_vehicle_bp, transform)
        # set the vehicle autopilot mode
        ego_vehicle.set_autopilot(True)
        # collect all actors to destroy when we quit the script
        actor_list.append(ego_vehicle)
        # vehicle = blueprint_library.filter('vehicle.*.*')
        # is_bike = [vehicle.get_attribute('number_of_wheels') == 2]
        # if (is_bike):
        #     vehicle.set_attribute('color', '255,0,0')
        output_path = 'outputs_cline/lidar1'
        if not os.path.exists(output_path):
            os.makedirs(output_path)
        output_path = 'outputs_cline/lidar2'
        if not os.path.exists(output_path):
            os.makedirs(output_path)

        lidar_bp1 = blueprint_library.find('sensor.lidar.ray_cast')
        lidar_bp2 = blueprint_library.find('sensor.lidar.ray_cast')
        lidar_bp1.set_attribute('channels', str(64))
        lidar_bp1.set_attribute('points_per_second', str(180000))
        lidar_bp1.set_attribute('rotation_frequency', str(60))
        lidar_bp1.set_attribute('range', str(20))
        lidar_bp2.set_attribute('channels', str(64))
        lidar_bp2.set_attribute('points_per_second', str(180000))
        lidar_bp2.set_attribute('rotation_frequency', str(60))
        lidar_bp2.set_attribute('range', str(20))

        lidar_location = carla.Location(0, 0, 3)
        lidar_rotation = carla.Rotation(0, 0, 0)
        lidar_transform1 = carla.Transform(lidar_location, lidar_rotation)
        lidar_transform2 = carla.Transform(carla.Location(3, 0, 3), carla.Rotation(0, 0, 0))
        # spawn the lidar
        lidar1 = world.spawn_actor(lidar_bp1, lidar_transform1, attach_to=ego_vehicle)
        lidar2 = world.spawn_actor(lidar_bp1, lidar_transform2, attach_to=ego_vehicle)
        lidar1.listen(
            lambda point_cloud: sensor_callback(point_cloud, sensor_queue, "lidar1"))
        lidar2.listen(
            lambda point_cloud: sensor_callback(point_cloud, sensor_queue, "lidar2"))
        sensor_list.append(lidar1)
        sensor_list.append(lidar2)

        while True:
            # set the sectator to follow the ego vehicle
            spectator = world.get_spectator()
            transform = ego_vehicle.get_transform()
            spectator.set_transform(carla.Transform(transform.location + carla.Location(z=20),
                                                    carla.Rotation(pitch=-90)))

    finally:
        print('destroying actors')
        client.apply_batch([carla.command.DestroyActor(x) for x in actor_list])
        for sensor in sensor_list:
            sensor.destroy()
        print('done.')

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        print(' - Exited by user.')


from marshmallow.fields import Field
from marshmallow.schema import Schema, SchemaMeta, SchemaOpts
import sqlalchemy as sa
from sqlalchemy.ext.declarative import DeclarativeMeta

from .convert import ModelConverter
from .exceptions import IncorrectSchemaTypeError
from .load_instance_mixin import LoadInstanceMixin

# This isn't really a field; it's a placeholder for the metaclass.
# This should be considered private API.
class SQLAlchemyAutoField(Field):
    def __init__(self, *, column_name=None, model=None, table=None, field_kwargs):
        super().__init__()

        if model and table:
            raise ValueError("Cannot pass both `model` and `table` options.")

        self.column_name = column_name
        self.model = model
        self.table = table
        self.field_kwargs = field_kwargs

    def create_field(self, schema_opts, column_name, converter):
        model = self.model or schema_opts.model
        if model:
            return converter.field_for(model, column_name, **self.field_kwargs)
        else:
            table = self.table if self.table is not None else schema_opts.table
            column = getattr(table.columns, column_name)
            return converter.column2field(column, **self.field_kwargs)

    # This field should never be bound to a schema.
    # If this method is called, it's probably because the schema is not a SQLAlchemySchema.
    def _bind_to_schema(self, field_name, schema):
        raise IncorrectSchemaTypeError(
            f"Cannot bind SQLAlchemyAutoField. Make sure that {schema} is a SQLAlchemySchema or SQLAlchemyAutoSchema."
        )

class SQLAlchemySchemaOpts(LoadInstanceMixin.Opts, SchemaOpts):
    """Options class for `SQLAlchemySchema`.
    Adds the following options:

    - ``model``: The SQLAlchemy model to generate the `Schema` from (mutually exclusive with ``table``).
    - ``table``: The SQLAlchemy table to generate the `Schema` from (mutually exclusive with ``model``).
    - ``load_instance``: Whether to load model instances.
    - ``sqla_session``: SQLAlchemy session to be used for deserialization.
        This is only needed when ``load_instance`` is `True`. You can also pass a session to the Schema's `load` method.
    - ``transient``: Whether to load model instances in a transient state (effectively ignoring the session).
        Only relevant when ``load_instance`` is `True`.
    - ``model_converter``: `ModelConverter` class to use for converting the SQLAlchemy model to marshmallow fields.
    """

    def __init__(self, meta, *args, **kwargs):
        super().__init__(meta, *args, **kwargs)

        self.model = getattr(meta, "model", None)
        self.table = getattr(meta, "table", None)
        if self.model is not None and self.table is not None:
            raise ValueError("Cannot set both `model` and `table` options.")
        self.model_converter = getattr(meta, "model_converter", ModelConverter)

class SQLAlchemyAutoSchemaOpts(SQLAlchemySchemaOpts):
    """Options class for `SQLAlchemyAutoSchema`.
    Has the same options as `SQLAlchemySchemaOpts`, with the addition of:

    - ``include_fk``: Whether to include foreign fields; defaults to `False`.
    - ``include_relationships``: Whether to include relationships; defaults to `False`.
    """

    def __init__(self, meta, *args, **kwargs):
        super().__init__(meta, *args, **kwargs)
        self.include_fk = getattr(meta, "include_fk", False)
        self.include_relationships = getattr(meta, "include_relationships", False)
        if self.table is not None and self.include_relationships:
            raise ValueError("Cannot set `table` and `include_relationships = True`.")

class SQLAlchemySchemaMeta(SchemaMeta):
    @classmethod
    def get_declared_fields(mcs, klass, cls_fields, inherited_fields, dict_cls):
        opts = klass.opts
        Converter = opts.model_converter
        converter = Converter(schema_cls=klass)
        fields = super().get_declared_fields(
            klass, cls_fields, inherited_fields, dict_cls
        )
        fields.update(mcs.get_declared_sqla_fields(fields, converter, opts, dict_cls))
        fields.update(mcs.get_auto_fields(fields, converter, opts, dict_cls))
        return fields

    @classmethod
    def get_declared_sqla_fields(mcs, base_fields, converter, opts, dict_cls):
        return {}

    @classmethod
    def get_auto_fields(mcs, fields, converter, opts, dict_cls):
        return dict_cls(
            {
                field_name: field.create_field(
                    opts, field.column_name or field_name, converter
                )
                for field_name, field in fields.items()
                if isinstance(field, SQLAlchemyAutoField)
                and field_name not in opts.exclude
            }
        )

class SQLAlchemyAutoSchemaMeta(SQLAlchemySchemaMeta):
    @classmethod
    def get_declared_sqla_fields(cls, base_fields, converter, opts, dict_cls):
        fields = dict_cls()
        if opts.table is not None:
            fields.update(
                converter.fields_for_table(
                    opts.table,
                    fields=opts.fields,
                    exclude=opts.exclude,
                    include_fk=opts.include_fk,
                    base_fields=base_fields,
                    dict_cls=dict_cls,
                )
            )
        elif opts.model is not None:
            fields.update(
                converter.fields_for_model(
                    opts.model,
                    fields=opts.fields,
                    exclude=opts.exclude,
                    include_fk=opts.include_fk,
                    include_relationships=opts.include_relationships,
                    base_fields=base_fields,
                    dict_cls=dict_cls,
                )
            )
        return fields

class SQLAlchemySchema(
    LoadInstanceMixin.Schema, Schema, metaclass=SQLAlchemySchemaMeta
):
    """Schema for a SQLAlchemy model or table.
    Use together with `auto_field` to generate fields from columns.

    Example: ::

        from marshmallow_sqlalchemy import SQLAlchemySchema, auto_field

        from mymodels import User

        class UserSchema(SQLAlchemySchema):
            class Meta:
                model = User

            id = auto_field()
            created_at = auto_field(dump_only=True)
            name = auto_field()
    """

    OPTIONS_CLASS = SQLAlchemySchemaOpts

class SQLAlchemyAutoSchema(SQLAlchemySchema, metaclass=SQLAlchemyAutoSchemaMeta):
    """Schema that automatically generates fields from the columns of
     a SQLAlchemy model or table.

    Example: ::

        from marshmallow_sqlalchemy import SQLAlchemyAutoSchema, auto_field

        from mymodels import User

        class UserSchema(SQLAlchemyAutoSchema):
            class Meta:
                model = User
                # OR
                # table = User.__table__

            created_at = auto_field(dump_only=True)
    """

    OPTIONS_CLASS = SQLAlchemyAutoSchemaOpts

def auto_field(
    column_name: str = None,
    *,
    model: DeclarativeMeta = None,
    table: sa.Table = None,
    **kwargs,
):
    """Mark a field to autogenerate from a model or table.

    :param column_name: Name of the column to generate the field from.
        If ``None``, matches the field name. If ``attribute`` is unspecified,
        ``attribute`` will be set to the same value as ``column_name``.
    :param model: Model to generate the field from.
        If ``None``, uses ``model`` specified on ``class Meta``.
    :param table: Table to generate the field from.
        If ``None``, uses ``table`` specified on ``class Meta``.
    :param kwargs: Field argument overrides.
    """
    if column_name is not None:
        kwargs.setdefault("attribute", column_name)
    return SQLAlchemyAutoField(
        column_name=column_name, model=model, table=table, field_kwargs=kwargs
    )


import serpent
from pyethereum import transactions, blocks, processblock, utils
from display import *

def new_user(brain_pass):
    key = utils.sha3(brain_pass)
    addr = utils.privtoaddr(key)
    return key, addr

def make_population(N):
    users = []
    keys = []
    for i in xrange(N):
        key, addr = new_user('dsdmn%dvdscu7w2y%d%d'%(i, i*91, i+31))
        users.append(addr)
        keys.append(key)
    return users, keys

def genesis_block(users, coins_each=10**18):
    dd = dict(zip(users, [coins_each]*len(users)))
    gen = blocks.Block.genesis(dd)
    return gen

def write_owner(root_hash, filename):
    f = open(filename)
    d = f.readlines()
    f.close()
    if not d[0] == 'owner = 0x%s\n'%root_hash:
        d[0] = 'owner = 0x%s\n'%root_hash
        f = open(filename, 'w')
        f.writelines(d)
        f.close()

def init_system(genesis, key):
    code = serpent.compile(open('serp/root.se').read())
    tx_make_root = transactions.Transaction.contract(0,0,10**12, 10000, code).sign(key)
    root_contract = processblock.apply_tx(genesis, tx_make_root)

    root_hash = root_contract.encode('hex')

    f = lambda x: write_owner(root_hash, x)
    map(f, ['serp/data.se', 'serp/tag.se', 'serp/users.se', 'serp/currency.se'])

    code = serpent.compile(open('serp/data.se').read())
    tx_make_data = transactions.Transaction.contract(1,0,10**12, 10000, code).sign(key)
    code = serpent.compile(open('serp/tag.se').read())
    tx_make_tag = transactions.Transaction.contract(2,0,10**12, 10000, code).sign(key)
    code = serpent.compile(open('serp/users.se').read())
    tx_make_users = transactions.Transaction.contract(3,0,10**12, 10000, code).sign(key)
    code = serpent.compile(open('serp/currency.se').read())
    tx_make_currency = transactions.Transaction.contract(4,0,10**12, 10000, code).sign(key)

    data_contract = processblock.apply_tx(genesis, tx_make_data)
    tag_contract = processblock.apply_tx(genesis, tx_make_tag)
    users_contract = processblock.apply_tx(genesis, tx_make_users)
    currency_contract = processblock.apply_tx(genesis, tx_make_currency)

    d_contract = data_contract.encode('hex')
    t_contract = tag_contract.encode('hex')
    u_contract = users_contract.encode('hex')
    c_contract = currency_contract.encode('hex')

    #init root
    tx_init_root = transactions.Transaction(5, 0, 10**12, 10000, root_contract, serpent.encode_datalist([d_contract, t_contract, u_contract, c_contract])).sign(key)
    ans = processblock.apply_tx(genesis, tx_init_root)

    adresses = {root_hash:'root', d_contract:'data', t_contract:'tags', u_contract:'users', utils.privtoaddr(key):'me', c_contract:'currency'}

    return adresses

def send_money(to, amount, genesis, root_contract, usr):
    key, addr = usr
    nonce = get_nonce(genesis, addr)
    tx_money = transactions.Transaction(nonce, 0, 10**12, 10000, root_contract, serpent.encode_datalist([to, amount])).sign(key)
    ans = processblock.apply_tx(genesis, tx_money)

def push_content(content, title, genesis, root_contract, usr):
    key, addr = usr
    nonce = get_nonce(genesis, addr)
    content_hash = utils.sha3(content)
    # push a transaction with a title.  recover title from blockchain
    tx_push = transactions.Transaction(nonce, 0, 10**12, 10000, root_contract, serpent.encode_datalist([1, content_hash, title])).sign(key)
    ans = processblock.apply_tx(genesis, tx_push)

    f = open('data/%s'%content_hash.encode('hex'), 'w')
    f.write(content)
    f.close()

    return content_hash

def register_name(name, genesis, root_contract, usr):
    key, addr = usr
    nonce = get_nonce(genesis, addr)
    # register eth-address to a name.  recover name from blockchain.  names are not unique. but names + first 4 digits of address probably are....
    tx_register_name = transactions.Transaction(nonce, 0, 10**12, 10000, root_contract, serpent.encode_datalist([5, name])).sign(key)
    ans = processblock.apply_tx(genesis, tx_register_name)

def tag_content(content_hash, tag, genesis, root_contract, usr):
    key, addr = usr
    nonce = get_nonce(genesis, addr)
    tx_tag = transactions.Transaction(nonce, 0, 10**12, 10000, root_contract, serpent.encode_datalist([2, content_hash, tag])).sign(key)
    ans = processblock.apply_tx(genesis, tx_tag)

def vote_tag(content_hash, tag, vote, genesis, root_contract, usr):
    key, addr = usr
    nonce = get_nonce(genesis, addr)
    #vote on a tag.
    tx_vote = transactions.Transaction(nonce, 0, 10**12, 10000, root_contract, serpent.encode_datalist([3, content_hash, tag, vote])).sign(key)
    ans = processblock.apply_tx(genesis, tx_vote)

def get_all_content(genesis, root_contract, usr):
    key, addr = usr
    nonce = get_nonce(genesis, addr)
    tx_v = transactions.Transaction(nonce, 0, 10**12, 10000, root_contract, serpent.encode_datalist([7, 'kjsdhg'])).sign(key)
    ans = processblock.apply_tx(genesis, tx_v)
    decoded = serpent.decode_datalist(ans)
    hexd = map(hex, decoded)
    f = lambda x : x[2:-1]
    hexd = map(f, hexd)
    f = lambda x: x.decode('hex')
    hexd = map(f, hexd)
    return hexd

def get_content(content_hash):
    f = open('data/%s'%content_hash.encode('hex'))
    content = f.read()
    f.close()
    return content

def get_content_title(content_hash, data_contract, genesis):
    #a = int(content_hash.encode('hex'),16) + 1 # index of title
    a = int(content_hash.encode('hex'),16) + 1 # index of title
    jj = genesis.get_storage_data(data_contract, a)
    return hex(jj)[2:-1].decode('hex')

def get_name(user_addr, users_contract, genesis):
    jj = genesis.get_storage_data(users_contract, user_addr)
    return hex(jj)[2:-1].decode('hex')

def get_tags(content_hash, tag_contract, genesis):
    ntags = genesis.get_storage_data(tag_contract,  content_hash)
    tags = []
    for i in xrange(ntags):
        #a = int(content_hash.encode('hex'),16) + 3*(i+1) # index of ith tag
        a = int(content_hash.encode('hex'), 16) + 3*(i+1) # index of ith tag
        jj = genesis.get_storage_data(tag_contract, a)
        tag = hex(jj)[2:-1].decode('hex')
        tags.append(tag)
    return tags

def get_votes(content_hash, tag, tag_contract, genesis):
    ntags = genesis.get_storage_data(tag_contract,  content_hash)
    found = False
    for i in xrange(ntags):
        a = int(content_hash.encode('hex'), 16) + 3*(i+1) # index of ith tag
        jj = genesis.get_storage_data(tag_contract, a)
        t = hex(jj)[2:-1].decode('hex')
        if tag == t:
            tagnum = i
            found = True
            break
    if found  == True:
        a = int(content_hash.encode('hex'), 16) + 3*tagnum + 1 # index of upvotes
        uv = genesis.get_storage_data(tag_contract, a)
        dv = genesis.get_storage_data(tag_contract, a+1)
        return int(uv), int(dv)
    else:
        return None


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np

from siamban.core.config import cfg
from siamban.models.iou_loss import linear_iou

def get_cls_loss(pred, label, select):  # 通过这个来选出正负样本， label:(17500,) pred:(17500,2), select:(242,)
    if len(select.size()) == 0 or \
            select.size() == torch.Size([0]):
        return 0
    pred = torch.index_select(pred, 0, select)  # (242,2)
    label = torch.index_select(label, 0, select)  # (242,)
    return F.nll_loss(pred, label)

def select_cross_entropy_loss(pred, label):  # label:(28,25,25), pred:(28,25,25,2)
    pred = pred.view(-1, 2)  # (17500,2)
    label = label.view(-1)  # (17500,) 它的作用就是用来得到 下面的 pos 和 neg 的
    pos = label.data.eq(1).nonzero().squeeze().cuda()  # 正样本位置的索引
    neg = label.data.eq(0).nonzero().squeeze().cuda()  # 负样本位置的索引
    loss_pos = get_cls_loss(pred, label, pos)  #
    loss_neg = get_cls_loss(pred, label, neg)
    return loss_pos * 0.5 + loss_neg * 0.5

def weight_l1_loss(pred_loc, label_loc, loss_weight):
    if cfg.BAN.BAN:
        diff = (pred_loc - label_loc).abs()
        diff = diff.sum(dim=1)
    else:
        diff = None
    loss = diff * loss_weight
    return loss.sum().div(pred_loc.size()[0])

def select_iou_loss(pred_loc, label_loc, label_cls):
    label_cls = label_cls.reshape(-1)  # 17500
    pos = label_cls.data.eq(1).nonzero().squeeze().cuda()

    # pred_loc = pred_loc.permute(0, 2, 3, 1).reshape(-1, 4)  # (17500,4)
    pred_loc = torch.index_select(pred_loc, 0, pos)  # (242,4)

    # label_loc = label_loc.permute(0, 2, 3, 1).reshape(-1, 4)  # (17500,4)
    label_loc = torch.index_select(label_loc, 0, pos)  # (242,4)

    return linear_iou(pred_loc, label_loc)

def focal_loss(
    probs,
    targets,
    alpha: float = -1,
    gamma: float = 2,
    reduction: str = "none",
):
    ce_loss = F.binary_cross_entropy(
        probs, targets, reduction="none"
    )
    p_t = probs * targets + (1 - probs) * (1 - targets)
    loss = ce_loss * ((1 - p_t) ** gamma)

    if alpha >= 0:
        alpha_t = alpha * targets + (1 - alpha) * (1 - targets)
        loss = alpha_t * loss

    if reduction == "mean":
        loss = loss.mean()
    elif reduction == "sum":
        loss = loss.sum()

    return loss


from os import stat
import numpy as np
import pandas as pd
import datetime as dt
from contextlib import closing
from urllib.request import urlopen
import json

# J'importe et je modifie le df de train pour en faire le df de prédiction de base avec les colonnes dans le bonnes ordres.
def make_df_init():

    df = pd.read_csv('./data/train.csv')
    df.drop('Unnamed: 0', axis=1, inplace = True)
    df.drop('datetime', axis=1, inplace=True)
    df.drop(columns=['registered', 'casual'], index=1, inplace=True)
    df = pd.DataFrame(columns=df.drop('count', axis=1).keys(), index=np.arange(0,40,1))

    return df

def stat_api_3h(df, lat, lon):
    date = []
    temp = []
    atemp = []
    humidity = []
    windspeed = []
    weather = []
    with closing(urlopen(f'http://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&appid=6bda77c15824913b0353424425dfeb64')) as f:
        cityEph = json.loads(f.read())
        for i in range(0, len(cityEph['list'])):
            temp.append(cityEph['list'][i]['main']['temp'])
            atemp.append(cityEph['list'][i]['main']['feels_like'])
            humidity.append(cityEph['list'][i]['main']['humidity'])
            windspeed.append(cityEph['list'][i]['wind']['speed'])
            weather.append(cityEph['list'][i]['weather'][0]['main'])
            date.append(cityEph['list'][i]['dt_txt'])

    df['day'] = [ i for i in date]
    df['hour']  = pd.to_datetime(df['day']).dt.hour
    df['day']  = pd.to_datetime(df['day']).dt.dayofyear
    df['temp'] = [ i for i in temp]
    df['atemp'] = [ i for i in atemp]
    df['humidity'] = [ i for i in humidity]
    df['windspeed'] = [ i for i in windspeed]
    df['weather'] = [ str(i) for i in weather]

    df['temp'] = df['temp'] - 273.15
    df['atemp'] = df['atemp'] - 273.15

def make_season_holiday_dc(df):

    # Season
    df['season'][(df['day'] >= 78 ) & (df['day'] < 141 )] = 1
    df['season'][(df['day'] >= 141) & (df['day'] < 365 ) ] = 2
    df['season'][(df['day'] >= 365) & (df['day'] < 356 ) ] = 3
    df['season'][(df['day'] >= 356) & (df['day'] < 78 ) ] = 4

    # Holliday For Washington
    df['holiday'] = 0
    df['holiday'][(df['day'] >= 357) & (df['day'] <= 2 ) ] = 1
    df['holiday'][(df['day'] >= 52) & (df['day'] <= 56 ) ] = 1
    df['holiday'][(df['day'] >= 69) & (df['day'] <= 70 ) ] = 1
    df['holiday'][(df['day'] >= 101) & (df['day'] <= 108 ) ] = 1
    df['holiday'][(df['day'] >= 185) & (df['day'] <= 240 ) ] = 1
    df['holiday'][(df['day'] >= 279) & (df['day'] <= 283 ) ] = 1
    df['holiday'][(df['day'] >= 315) & (df['day'] <= 329 ) ] = 1

    # Férié for Washington
    df['holiday'][df['day'] == 17 ] = 1
    df['holiday'][df['day'] == 26 ] = 1
    df['holiday'][df['day'] == 150 ] = 1
    df['holiday'][df['day'] == 129 ] = 1
    df['holiday'][df['day'] == 305 ] = 1
    df['holiday'][df['day'] == 308 ] = 1

    df['workingday'] = 0
    for i in np.arange(1,365,7):
        df['workingday'][(df['day'] == i) | (df['day'] == i + 1) ] = 1

    df.rename(columns = {'weather' : 'weather2'}, inplace = True)
    df.insert(4, 'weather', "")

    df['weather'][df['weather2'].str.lower().isin(['clear'])] = 1
    df['weather'][df['weather2'].str.lower().isin(['clouds','drizzle','mist'])] = 2
    df['weather'][df['weather2'].str.lower().isin(['snow','rain'])] = 3
    df['weather'][df['weather2'].str.lower().isin(['heavy','extreme'])] = 4

    df.drop('weather2', axis=1, inplace=True)

    df['weather'] = df['weather'].astype(int)
    df['season'] = df['season'].astype(int)

def make_season_holiday_fr(df):

    # just need columns day to run this functions and

    # Season
    df['season'][(df['day'] >= 78 ) & (df['day'] < 141 )] = 1
    df['season'][(df['day'] >= 141) & (df['day'] < 365 ) ] = 2
    df['season'][(df['day'] >= 365) & (df['day'] < 356 ) ] = 3
    df['season'][(df['day'] >= 356) & (df['day'] < 78 ) ] = 4

    # Holliday France - Lille
    df['holiday'] = 0
    df['holiday'][(df['day'] >= 352) & (df['day'] < 3 ) ] = 1
    df['holiday'][(df['day'] >= 36) & (df['day'] < 52 ) ] = 1
    df['holiday'][(df['day'] >= 99) & (df['day'] < 115 ) ] = 1
    df['holiday'][(df['day'] >= 145) & (df['day'] < 150 ) ] = 1
    df['holiday'][(df['day'] >= 188) & (df['day'] < 244 ) ] = 1
    df['holiday'][(df['day'] >= 296) & (df['day'] < 312 ) ] = 1

    # Férié
    df['holiday'][df['day'] == 17 ] = 1
    df['holiday'][df['day'] == 26 ] = 1
    df['holiday'][df['day'] == 150 ] = 1
    df['holiday'][df['day'] == 129 ] = 1
    df['holiday'][df['day'] == 305 ] = 1
    df['holiday'][df['day'] == 308 ] = 1

    df['workingday'] = 0
    for i in np.arange(1,365,7):
        df['workingday'][(df['day'] == i) | (df['day'] == i + 1) ] = 1

    df.rename(columns = {'weather' : 'weather2'}, inplace = True)
    df.insert(4, 'weather', 0)

    df['weather'][df['weather2'].str.lower().isin(['clear'])] = 1
    df['weather'][df['weather2'].str.lower().isin(['clouds','drizzle','mist'])] = 2
    df['weather'][df['weather2'].str.lower().isin(['snow','rain'])] = 3
    df['weather'][df['weather2'].str.lower().isin(['heavy','extreme'])] = 4
    df.drop('weather2', axis=1, inplace=True)

    df['weather'] = df['weather'].astype(int)
    df['season'] = df['season'].astype(int)

# faire graph count vélo par jour des 20 jours précédents la requêtes + 10 des jours suivants


"""Example how to make a Cube analysis from a 2D background model.
"""

from __future__ import absolute_import, division, print_function, unicode_literals
import numpy as np
from numpy.testing import assert_allclose
from astropy.tests.helper import assert_quantity_allclose
from astropy.coordinates import SkyCoord, Angle
from astropy.units import Quantity
from gammapy.extern.pathlib import Path
from ...data import DataStore
from ...utils.testing import requires_dependency, requires_data, pytest
from ...image import SkyMask
from .. import StackedObsCubeMaker
from ...background import OffDataBackgroundMaker
from .. import SkyCube
from ...utils.energy import Energy

def make_empty_cube(image_size, energy, center, data_unit=None):
    """
    Make an empty `SkyCube` from a given `SkyMask` and an energy binning.

    Parameters
    ----------
    image_size:int, Total number of pixel of the 2D map
    energy: Tuple for the energy axis: (Emin,Emax,nbins)
    center: SkyCoord of the source
    data_unit : str, Data unit.
    """
    def_image = dict()
    def_image["nxpix"] = image_size
    def_image["nypix"] = image_size
    def_image["binsz"] = 0.02
    def_image["xref"] = center.galactic.l.deg
    def_image["yref"] = center.galactic.b.deg
    def_image["proj"] = 'TAN'
    def_image["coordsys"] = 'GAL'
    def_image["unit"] = data_unit
    e_min, e_max, nbins = energy
    empty_cube = SkyCube.empty(emin=e_min.value, emax=e_max.value, enumbins=nbins, eunit=e_min.unit, mode='edges',
                               **def_image)
    return empty_cube

# Temp xfail for this: https://github.com/gammapy/gammapy/pull/899#issuecomment-281001655
@pytest.mark.xfail
@requires_dependency('reproject')
@requires_data('gammapy-extra')
def test_cube_pipe(tmpdir):
    tmpdir = str(tmpdir)
    outdir = tmpdir
    outdir2 = outdir + '/background'
    Path(outdir2).mkdir()

    ds = DataStore.from_dir("$GAMMAPY_EXTRA/datasets/hess-crab4-hd-hap-prod2")
    ds.copy_obs(ds.obs_table, tmpdir)
    data_store = DataStore.from_dir(tmpdir)

    # Create the background model from the 4 Crab observations
    bgmaker = OffDataBackgroundMaker(data_store, outdir=outdir2)

    bgmaker.select_observations(selection='all')
    bgmaker.group_observations()
    bgmaker.make_model("2D")
    bgmaker.save_models("2D")

    fn = outdir2 + '/group-def.fits'

    # New hdu table that contains the link to the background model
    hdu_index_table = bgmaker.make_total_index_table(
        data_store=data_store,
        modeltype='2D',
        out_dir_background_model=outdir2,
        filename_obs_group_table=fn
    )

    fn = outdir + '/hdu-index.fits.gz'
    hdu_index_table.write(fn, overwrite=True)

    center = SkyCoord(83.63, 22.01, unit='deg').galactic
    offset_band = Angle([0, 2.49], 'deg')

    ref_cube_images = make_empty_cube(image_size=250, energy=[Energy(0.5, "TeV"), Energy(100, "TeV"), 5], center=center)
    ref_cube_exposure = make_empty_cube(image_size=250, energy=[Energy(0.1, "TeV"), Energy(120, "TeV"), 80],
                                        center=center, data_unit="m2 s")
    ref_cube_skymask = make_empty_cube(image_size=250, energy=[Energy(0.5, "TeV"), Energy(100, "TeV"), 5],
                                       center=center)

    data_store = DataStore.from_dir(tmpdir)

    refheader = ref_cube_images.sky_image_ref.to_image_hdu().header
    exclusion_mask = SkyMask.read('$GAMMAPY_EXTRA/datasets/exclusion_masks/tevcat_exclusion.fits')
    exclusion_mask = exclusion_mask.reproject(reference=refheader)
    ref_cube_skymask.data = np.tile(exclusion_mask.data, (5, 1, 1))
    # Pb with the load psftable for one of the run that is not implemented yet...
    data_store.hdu_table.remove_row(14)

    # Cube Analysis
    cube_maker = StackedObsCubeMaker(empty_cube_images=ref_cube_images, empty_exposure_cube=ref_cube_exposure,
                                     offset_band=offset_band, data_store=data_store, obs_table=data_store.obs_table,
                                     exclusion_mask=ref_cube_skymask, save_bkg_scale=True)
    cube_maker.make_cubes(make_background_image=True, radius=10.)

    assert_allclose(cube_maker.counts_cube.data.sum(), 4898.0, atol=3)
    assert_allclose(cube_maker.bkg_cube.data.sum(), 4260.120595293951, atol=3)
    cube_maker.significance_cube.data[np.where(np.isinf(cube_maker.significance_cube.data))] = 0
    assert_allclose(np.nansum(cube_maker.significance_cube.data), 67613.24519908393, atol=3)
    assert_allclose(cube_maker.excess_cube.data.sum(), 637.8794047060486, atol=3)
    assert_quantity_allclose(np.nansum(cube_maker.exposure_cube.data), Quantity(4891844242766714.0, "m2 s"),
                             atol=Quantity(3, "m2 s"))
    assert_allclose(cube_maker.table_bkg_scale[0]["bkg_scale"], 0.8956177614218819)

    assert len(cube_maker.counts_cube.energies()) == 5
    assert len(cube_maker.bkg_cube.energies()) == 5
    assert len(cube_maker.significance_cube.energies()) == 5
    assert len(cube_maker.excess_cube.energies()) == 5
    assert len(cube_maker.exposure_cube.energies()) == 80


import sys
from PyQt6.QtWidgets import QApplication, QMainWindow
from PyQt6.QtGui import QAction, QMouseEvent
from src.executor import Executor
from src.task import get_jd_task

class AppMain(QMainWindow):
    def __init__(self):
        super().__init__()
        self._initUI()

    def _initUI(self):
        exitAct = QAction('&Exit', self)
        exitAct.setShortcut('Ctrl+Q')
        exitAct.setStatusTip('关闭应用')
        exitAct.triggered.connect(QApplication.instance().quit)

        self.statusBar()

        menuBar = self.menuBar()
        fileMenu = menuBar.addMenu('新增菜单')
        fileMenu.addAction(exitAct)

        self.setGeometry(300, 300, 800, 700)
        self.setWindowTitle('电商秒单')

        self.setMouseTracking(True)

        self.show()

    def mouseMoveEvent(self, e: QMouseEvent):
        x = int(e.position().x())
        y = int(e.position().y())
        print(x, y)
        return super().mouseMoveEvent(e)

def init_executor():
    jd_task = get_jd_task("jd_account", "jd_password", qr_login=False)
    executor = Executor(key=jd_task.key(), task=jd_task)
    executor.execute()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = AppMain()

    init_executor()

    sys.exit(app.exec())


import pytest
from pathlib import Path

from tests.prep_tests import (SSTestFile,
                              TEST_DATA,
                              MetaTestFile,
                              TEST_METADATA)
from gwas_sumstats_tools.format import Formatter

@pytest.fixture()
def sumstats_file():
    sumstats = SSTestFile()
    sumstats.to_file()
    yield sumstats.filepath
    sumstats.remove()

@pytest.fixture()
def meta_file():
    metafile = MetaTestFile()
    metafile.to_file()
    yield metafile.filepath
    metafile.remove()

class TestFormatter:
    def test_set_data_outfile_name_when_formatting(self, sumstats_file):
        f = Formatter(sumstats_file, format_data=True)
        assert isinstance(f.data_infile, Path)
        assert isinstance(f.data_outfile, Path)
        assert str(f.data_outfile) == str((Path(sumstats_file))) + "-FORMATTED.tsv.gz"

    def test_set_data_outfile_name_when_not_formatting(self, sumstats_file):
        f = Formatter(sumstats_file, format_data=False)
        assert isinstance(f.data_infile, Path)
        assert isinstance(f.data_outfile, Path)
        assert str(f.data_outfile) == str((Path(sumstats_file)))

    def test_set_data_outfile_name_when_given_custom_outfile_name(self, sumstats_file):
        f = Formatter(sumstats_file, data_outfile="TEST_OUT", format_data=True)
        assert isinstance(f.data_outfile, Path)
        assert str(f.data_outfile) == "TEST_OUT"
        f = Formatter(sumstats_file, data_outfile="TEST_OUT", format_data=False)
        assert isinstance(f.data_outfile, Path)
        assert str(f.data_outfile) == "TEST_OUT"

    def test_set_metadata_outfile_name(self, sumstats_file):
        f = Formatter(sumstats_file)
        assert isinstance(f.metadata_outfile, Path)
        assert str(f.metadata_outfile) == str((Path(f.data_outfile))) + "-meta.yaml"
        f = Formatter(sumstats_file, metadata_outfile="TEST_OUT")
        assert isinstance(f.metadata_outfile, Path)
        assert str(f.metadata_outfile) == "TEST_OUT"


from profiles.utils import (
    create_unique_id,
    send_activation_email,
)
from utils.tests.creations import (
    Athlete,
    Referee,
    activate_user,
)
from utils.tests.helper import (
    TestHelper,
    User,
)

class ProfileUtil(TestHelper):
    def test_activate_user(self):
        print('activate_user')
        athlete = Athlete.objects.create_athlete(**self.data)
        self.assertFalse(athlete.user.is_active)
        self.assertNotEqual(athlete.user.key, '')
        activate_user(athlete.user)
        self.assertTrue(athlete.user.is_active)
        self.assertFalse(athlete.user.key)

    def test_create_unique_id(self):
        print('create_unique_id')
        athlete = Athlete.objects.create_athlete(**self.data)
        self.assertNotEqual(
            create_unique_id(athlete),
            create_unique_id(athlete)
        )
        self.assertNotEqual(
            create_unique_id(athlete),
            athlete.id
        )
        if 'dob' in self.data2:
            del self.data2['dob']
        referee = Referee.objects.create_referee(**self.data2)
        self.assertNotEqual(
            create_unique_id(referee),
            referee.id
        )
        self.assertNotEqual(
            referee.id,
            athlete.id,
        )

    def test_send_activation_email(self):
        print('send_activation_email')
        self.delete_user_instance(self.email)
        user = User.objects.create_user(**self.data['user'])
        self.assertFalse(user.is_active and user.key)
        self.assertEqual(
            send_activation_email(user),
            'localhost:8000/activate/' + user.key + '/'
        )


import cv2
import numpy as np
capture = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
while True:
    ret,frame = capture.read()
    width = int(capture.get(3))
    height = int(capture.get(4))
    image = np.zeros(frame.shape,np.uint8)
    smaller_frame = cv2.resize(frame,(0,0),fx=0.5,fy=0.5)
    image[:height//2,:width//2] = cv2.rotate(smaller_frame,cv2.cv2.ROTATE_180)
    image[height//2:,:width//2] = smaller_frame
    image[:height//2,width//2:] = cv2.rotate(smaller_frame,cv2.cv2.ROTATE_180)
    image[height//2:,width//2:] = smaller_frame

    hsv = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)
    lower_blue = np.array([7,59,7])
    upper_blue = np.array([112,255,255])
    mask = cv2.inRange(hsv,lower_blue,upper_blue)
    result = cv2.bitwise_and(image,image,mask=mask)

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 5, 0), 5)
        roi_gray = gray[y:y+w, x:x+w]
        roi_color = frame[y:y+h, x:x+w]
        eyes = eye_cascade.detectMultiScale(roi_gray, 1.3, 5)
        for (ex, ey, ew, eh) in eyes:
            cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 87, 155), 5)

    cv2.imshow('frame', frame)
    cv2.imshow('mask',mask)
    cv2.imshow('frames',result)
    cv2.imshow('cross',frame)
    cv2.imshow('tiles',image)
    if cv2.waitKey(1)==ord('q'):
       break
capture.release()
cv2.destroyAllWindows


import argparse
import numpy as np
import pickle
from keras.wrappers.scikit_learn import KerasClassifier
from keras.callbacks import ModelCheckpoint, TensorBoard
from keras.models import load_model
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from phase_utils import print_cm
from phase_features_loader import PhaseFeaturesLoader
from phase_model_simple import model_simple
from phase_model_resnet import model_resnet
from phase_model_xgboost import model_xgboost
from imblearn.metrics import classification_report_imbalanced
from imblearn.combine import SMOTETomek, SMOTEENN

if __name__ == "__main__":
    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("-a", "--action", choices=["train", "test"], default="train",
                        help="set the action, either training or test the dataset")
    parser.add_argument("--train_dataset", default="data/phase/ml_features_train.csv",
                        help="set the path to the training dataset")
    parser.add_argument("--test_dataset", default="data/phase/ml_features_test.csv",
                        help="set the path to the test dataset")
    parser.add_argument("-m", "--model", default=None,
                        help="set the path to the pre-trained model/weights")
    parser.add_argument("--cv", type=bool, default=False,
                        help="enable / disable a full cross validation with n_splits=10")
    parser.add_argument("-b", "--batch_size", type=int, default=256,
                        help="set the batch size)")
    parser.add_argument("-e", "--epochs", type=int, default=2000,
                        help="set the epochs number)")
    parser.add_argument("-l", "--layers", default="128 128 64 48 48 32 32 48 32 16",
                        help="set the hidden layers)")
    parser.add_argument("-d", "--dropout", type=float, default=0.1,
                        help="set the dropout)")
    parser.add_argument("-s", "--stations", default="URZ",
                        help="set the station name, it supports currently only LPAZ and URZ")
    parser.add_argument("-v", "--verbose", type=int, default=0,
                        help="set the verbosity)")
    parser.add_argument("-p", "--phase_length", default="URZ 6840 6840 6840 20520",
                        help="set the number of entries of phases per stations to be read from the dataset.\n" +
                             "The default is for the training, for the test use 'URZ 2280 2280 2280 6840, " +
                             "LPAZ 160 160 160 480'")

    args = parser.parse_args()

    # fix random seed for reproducibility
    seed = 7
    np.random.seed(seed)

    epochs = args.epochs
    train_dataset = args.train_dataset
    test_dataset = args.test_dataset
    phase_length = {}
    try:
        for p in args.phase_length.split(","):
            s = p.strip().split(" ")
            phase_length.update({s[0]:{"regP": int(s[1]), "regS": int(s[2]), "tele": int(s[3]), "N": int(s[4])}})
    except ValueError:
        print("It should be a list of a station name followed by four numbers.")
        exit(1)
    stations_lower = [station.lower() for station in sorted(phase_length.keys())]
    layers = []
    try:
        layers = [int(units) for units in args.layers.split(" ")]
    except ValueError:
        print("The layers should be a list of integer, delimited by a whitespace")
        exit(1)

    dropout = args.dropout
    batch_size = args.batch_size
    validation_split = 0.1
    if args.model is None:
        model_file_path = "results/phase_weights_best_s_{}_l_{}_d_{}.hdf5".\
            format("_".join(stations_lower), "_".join([str(layer) for layer in layers]), dropout)
    else:
        model_file_path = args.model

    model = model_xgboost

    if args.action == "train":
        # load train dataset
        pd = PhaseFeaturesLoader(filename=train_dataset, validation_split=validation_split,
                                 phase_length=phase_length, batch_size=batch_size)
        tensorboard = TensorBoard(log_dir='graph', histogram_freq=0, write_graph=True, write_images=True)
        checkpoint = ModelCheckpoint(model_file_path, monitor='acc', verbose=args.verbose,
                                     save_best_only=True, mode='max')
        if args.cv:
            train_x, train_y = pd.get_dataset()
            kfold = KFold(n_splits=10, shuffle=True, random_state=seed)
            estimator = KerasClassifier(build_fn=model, layers=layers, dropout=dropout,
                                        epochs=epochs, batch_size=500, verbose=args.verbose)
            results = cross_val_score(estimator, train_x, train_y, cv=kfold,
                                      fit_params={'callbacks':[checkpoint, tensorboard]})

            print("Baseline: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))
        else:
            model = model(layers=layers, dropout=dropout, layer_number=10)
            print(model)
            train_x, train_y = pd.get_dataset(expand_dim=False, y_onehot=False)
            sme = SMOTEENN(random_state=42)
            train_x_res, train_y_res = sme.fit_sample(train_x, train_y)
            #class_weight = {0:1, 1:1, 2:1, 3:1}
            # train_x = np.reshape(train_x, (train_x.shape[0], train_x[2]))
            # train_y = np.reshape(train_y, (train_y.shape[0], train_y[2]))
            model.fit(train_x_res, train_y_res)

            # save model to file
            pickle.dump(model, open("bst_grid-phase_smoteenn.pickle.dat", "wb"))

            print("Best accuracy obtained: {0}".format(model.best_score_))
            print("Parameters:")
            for key, value in model.best_params_.items():
                print("\t{}: {}".format(key, value))

            # load test dataset
            pd = PhaseFeaturesLoader(filename=test_dataset, phase_length=phase_length, batch_size=batch_size)
            test_x, test_y = pd.get_dataset(expand_dim=False, y_onehot=False)

            y_pred = model.predict(test_x)
            predictions = [np.round(value) for value in y_pred]
            # evaluate predictions
            accuracy = accuracy_score(test_y, predictions)
            print("Accuracy: %.2f%%" % (accuracy * 100.0))
    else:
        # load test dataset
        pd = PhaseFeaturesLoader(filename=test_dataset, phase_length=phase_length, batch_size=batch_size)
        test_x, test_y = pd.get_dataset(expand_dim=False, y_onehot=False)

        # load model & weight
        loaded_model = pickle.load(open("bst_grid-phase_smoteenn.pickle.dat", "rb"))
        print("Loaded model from disk with best params: {}".format(loaded_model.best_params_))

        y_pred = loaded_model.predict(test_x)

        predictions = [np.round(value) for value in y_pred]
        # evaluate predictions
        accuracy = accuracy_score(test_y, predictions)
        print("Accuracy: %.2f%%" % (accuracy * 100.0))
        class_name = ["regP", "regS", "tele", "N"]
        print(classification_report_imbalanced(test_y, predictions, target_names=class_name))


from typing import Dict, Tuple
from unittest import TestCase

from google.protobuf.json_format import ParseDict

from cosmpy.common.utils import json_encode
from cosmpy.protos.cosmos.base.tendermint.v1beta1.query_pb2 import (
    GetBlockByHeightRequest,
    GetBlockByHeightResponse,
    GetLatestBlockRequest,
    GetLatestBlockResponse,
    GetLatestValidatorSetRequest,
    GetLatestValidatorSetResponse,
    GetNodeInfoRequest,
    GetNodeInfoResponse,
    GetSyncingRequest,
    GetSyncingResponse,
    GetValidatorSetByHeightRequest,
    GetValidatorSetByHeightResponse,
)
from cosmpy.tendermint.rest_client import CosmosBaseTendermintRestClient

from tests.helpers import MockRestClient

TYPE = {
    "@type": "type.googleapis.com/google.protobuf.Int32Value",
    "value": "42",
}

class CosmosBaseTendermintRestClientTestCase(TestCase):
    """Test case for CosmosBaseTendermintRestClient class."""

    REST_CLIENT = CosmosBaseTendermintRestClient

    def make_clients(
        self, response_content: Dict
    ) -> Tuple[MockRestClient, CosmosBaseTendermintRestClient]:
        """
        Make  mock client and rest client api for specific content.

        :param response_content: dict
        :return: rest client instance
        """
        mock_client = MockRestClient(json_encode(response_content).encode("utf-8"))
        rest_client = self.REST_CLIENT(mock_client)
        return mock_client, rest_client

    def test_GetNodeInfo(self):
        """Test GetNodeInfo method."""
        content = {
            "default_node_info": {
                "protocol_version": {
                    "p2p": "12",
                    "block": "12",
                    "app": "12",
                },
                "default_node_id": "string",
                "listen_addr": "string",
                "network": "string",
                "version": "string",
                "channels": "string",
                "moniker": "string",
                "other": {"tx_index": "string", "rpc_address": "string"},
            },
            "application_version": {
                "name": "string",
                "app_name": "string",
                "version": "string",
                "git_commit": "string",
                "build_tags": "string",
                "go_version": "string",
                "build_deps": [{"path": "string", "version": "string", "sum": "12"}],
            },
        }
        mock_client, rest_client = self.make_clients(content)
        expected_response = ParseDict(content, GetNodeInfoResponse())

        assert rest_client.GetNodeInfo(GetNodeInfoRequest()) == expected_response
        assert mock_client.last_base_url == "/cosmos/base/tendermint/v1beta1/node_info"

    def test_GetSyncing(self):
        """Test GetSyncing method."""
        content = {"syncing": True}
        mock_client, rest_client = self.make_clients(content)
        expected_response = ParseDict(content, GetSyncingResponse())

        assert rest_client.GetSyncing(GetSyncingRequest()) == expected_response
        assert mock_client.last_base_url == "/cosmos/base/tendermint/v1beta1/syncing"

    def test_GetLatestBlock(self):
        """Test GetLatestBlock method."""
        content = {
            "block_id": {
                "hash": "string",
                "part_set_header": {"total": 0, "hash": "string"},
            },
            "block": {
                "header": {
                    "version": {"block": "12", "app": "12"},
                    "chain_id": "string",
                    "height": "12",
                    "time": "2022-03-29T10:21:54.568Z",
                    "last_block_id": {
                        "hash": "string",
                        "part_set_header": {"total": 0, "hash": "string"},
                    },
                    "last_commit_hash": "string",
                    "data_hash": "string",
                    "validators_hash": "string",
                    "next_validators_hash": "string",
                    "consensus_hash": "string",
                    "app_hash": "string",
                    "last_results_hash": "string",
                    "evidence_hash": "string",
                    "proposer_address": "string",
                },
                "data": {"txs": ["string"]},
                "evidence": {"evidence": []},
                "last_commit": {
                    "height": "12",
                    "round": 0,
                    "block_id": {
                        "hash": "string",
                        "part_set_header": {"total": 0, "hash": "string"},
                    },
                    "signatures": [
                        {
                            "block_id_flag": "BLOCK_ID_FLAG_UNKNOWN",
                            "validator_address": "string",
                            "timestamp": "2022-03-29T10:21:54.569Z",
                            "signature": "string",
                        }
                    ],
                },
            },
        }
        mock_client, rest_client = self.make_clients(content)
        expected_response = ParseDict(content, GetLatestBlockResponse())

        assert rest_client.GetLatestBlock(GetLatestBlockRequest()) == expected_response
        assert (
            mock_client.last_base_url == "/cosmos/base/tendermint/v1beta1/blocks/latest"
        )

    def test_GetBlockByHeight(self):
        """Test GetBlockByHeight method."""
        content = {
            "block_id": {
                "hash": "string",
                "part_set_header": {"total": 0, "hash": "string"},
            },
            "block": {
                "header": {
                    "version": {"block": "12", "app": "12"},
                    "chain_id": "string",
                    "height": "12",
                    "time": "2022-03-29T10:27:01.686Z",
                    "last_block_id": {
                        "hash": "string",
                        "part_set_header": {"total": 0, "hash": "string"},
                    },
                    "last_commit_hash": "string",
                    "data_hash": "string",
                    "validators_hash": "string",
                    "next_validators_hash": "string",
                    "consensus_hash": "string",
                    "app_hash": "string",
                    "last_results_hash": "string",
                    "evidence_hash": "string",
                    "proposer_address": "string",
                },
                "data": {"txs": ["string"]},
                "evidence": {"evidence": []},
                "last_commit": {
                    "height": "12",
                    "round": 0,
                    "block_id": {
                        "hash": "string",
                        "part_set_header": {"total": 0, "hash": "string"},
                    },
                    "signatures": [
                        {
                            "block_id_flag": "BLOCK_ID_FLAG_UNKNOWN",
                            "validator_address": "string",
                            "timestamp": "2022-03-29T10:27:01.687Z",
                            "signature": "string",
                        }
                    ],
                },
            },
        }
        mock_client, rest_client = self.make_clients(content)
        expected_response = ParseDict(content, GetBlockByHeightResponse())

        assert (
            rest_client.GetBlockByHeight(GetBlockByHeightRequest(height=123))
            == expected_response
        )
        assert mock_client.last_base_url == "/cosmos/base/tendermint/v1beta1/blocks/123"

    def test_GetLatestValidatorSet(self):
        """Test GetLatestValidatorSet method."""
        content = {
            "block_height": "12",
            "validators": [
                {
                    "address": "string",
                    "pub_key": TYPE,
                    "voting_power": "12",
                    "proposer_priority": "12",
                }
            ],
            "pagination": {"next_key": "string", "total": "12"},
        }
        mock_client, rest_client = self.make_clients(content)
        expected_response = ParseDict(content, GetLatestValidatorSetResponse())

        assert (
            rest_client.GetLatestValidatorSet(GetLatestValidatorSetRequest())
            == expected_response
        )
        assert (
            mock_client.last_base_url
            == "/cosmos/base/tendermint/v1beta1/validatorsets/latest"
        )

    def test_GetValidatorSetByHeight(self):
        """Test GetValidatorSetByHeight method."""
        content = {
            "block_height": "12",
            "validators": [
                {
                    "address": "string",
                    "pub_key": TYPE,
                    "voting_power": "12",
                    "proposer_priority": "12",
                }
            ],
            "pagination": {"next_key": "string", "total": "12"},
        }
        mock_client, rest_client = self.make_clients(content)
        expected_response = ParseDict(content, GetValidatorSetByHeightResponse())

        assert (
            rest_client.GetValidatorSetByHeight(
                GetValidatorSetByHeightRequest(height=123)
            )
            == expected_response
        )
        assert (
            mock_client.last_base_url
            == "/cosmos/base/tendermint/v1beta1/validatorsets/123"
        )


import isce
import xml.etree.ElementTree as ElementTree
from collections import defaultdict
import datetime
import isceobj
from isceobj.Util import Poly1D, Poly2D
from isceobj.Planet.Planet import Planet
from isceobj.Orbit.Orbit import StateVector, Orbit
from isceobj.Orbit.OrbitExtender import OrbitExtender
from isceobj.Planet.AstronomicalHandbook import Const
from iscesys.Component.Component import Component
from iscesys.DateTimeUtil.DateTimeUtil import DateTimeUtil as DTUtil
from .GRDProduct import GRDProduct
import os
import glob
import json
import numpy as np
import shelve

SAFE = Component.Parameter('safe',
            public_name = 'safe',
            default = None,
            type = str,
            doc = 'SAFE folder with the GRD product')

POLARIZATION = Component.Parameter('polarization',
            public_name = 'polarization',
            default = None,
            type = str,
            doc = 'Polarization to unpack')

ORBIT_DIR = Component.Parameter('orbitDir',
            public_name = 'orbit directory',
            default = None,
            type = str,
            doc = 'Directory to search for orbit data')

ORBIT_FILE = Component.Parameter('orbitFile',
            public_name = 'orbit file',
            default = None,
            type = str,
            doc = 'External orbit file with state vectors')

OUTPUT = Component.Parameter('output',
            public_name = 'output directory',
            default = None,
            type = str,
            doc = 'Directory where the data gets unpacked')

SLANT_RANGE_FILE = Component.Parameter('slantRangeFile',
            public_name = 'slant range file',
            default = None,
            type = str,
            doc = 'Slant range file at full resolution')

####List of facilities
PRODUCT = Component.Facility('product',
            public_name='product',
            module = 'isceobj.Sensor.GRD',
            factory = 'createGRDProduct',
            args = (),
            mandatory = True,
            doc = 'GRD Product populated by the reader')

class Sentinel1(Component):
    """
        A Class representing Sentinel1 data
    """

    family = 's1grd'
    logging = 'isce.sensor.grd.s1'

    parameter_list = (SAFE,
                      POLARIZATION,
                      ORBIT_DIR,
                      ORBIT_FILE,
                      OUTPUT,
                      SLANT_RANGE_FILE)

    facility_list = (PRODUCT,)

    def __init__(self):
        Component.__init__(self)
        self.xml = None
        self.tiff = None
        self.calibrationXml = None
        self.noiseXml = None

        self.manifest = None
        self.noiseCorrectionApplied = False

        self.betaLUT = None
        self.gr2srLUT = None
        self.noiseRangeLUT = None
        self.noiseAzimuthLUT = None

        self._xml_root=None

    def validateUserInputs(self):
        '''
        Validate inputs provided by user.
        Populat tiff and xml list using SAFE folder names.
        '''

        import zipfile
        import fnmatch

        if self.safe is None:
            raise Exception('SAFE directory is not provided')

        if self.polarization in ['',None]:
            raise Exception('Polarization is not provided')

        ###Check if zip file / unpacked directory is provided.
        iszipfile = False
        if self.safe.endswith('.zip'):
            iszipfile = True
            with zipfile.ZipFile(self.safe, 'r') as zf:
                namelist = zf.namelist()

        ####First find VV annotation file
        swathid = 's1?-iw-grd-' + self.polarization.lower()

        if iszipfile:
            #Find XML file
            patt = os.path.join('*.SAFE', 'annotation', swathid+'*')
            match = fnmatch.filter(namelist, patt)

            if len(match) == 0 :
                raise Exception('Annotation file for {0} not found in {1}'.format(self.polarization, self.safe))

            self.xml = os.path.join('/vsizip', self.safe, match[0])

            #Find TIFF file
            patt = os.path.join('*.SAFE', 'measurement', swathid+'*')
            match = fnmatch.filter(namelist, patt)

            if len(match) == 0 :
                raise Exception('Annotation file found for {0} but no measurement in {1}'.format(self.polarization, self.safe))

            self.tiff = os.path.join('/vsizip', self.safe, match[0])

            #Find Calibration file
            patt = os.path.join('*.SAFE', 'annotation', 'calibration', 'calibration-'+swathid+'*')
            match = fnmatch.filter(namelist, patt)

            if len(match) == 0 :
                raise Exception('Annotation file found for {0} but no calibration in {1}'.format(self.polarization, self.safe))

            self.calibrationXml = os.path.join('/vsizip', self.safe, match[0])

            #Find Noise file
            patt = os.path.join('*.SAFE', 'annotation', 'calibration', 'noise-'+swathid+'*')
            match = fnmatch.filter(namelist, patt)

            if len(match) == 0 :
                raise Exception('Annotation file found for {0} but no noise in {1}'.format(self.polarization, self.safe))

            self.noiseXml = os.path.join('/vsizip', self.safe, match[0])

            patt = os.path.join('*.SAFE', 'manifest.safe')
            match = fnmatch.filter(namelist, patt)
            if len(match) == 0:
                raise Exception('No manifest file found in {0}'.format(self.safe))
            self.manifest = os.path.join('/vsizip', self.safe, match[0])

        else:

            ####Find annotation file
            patt = os.path.join( self.safe, 'annotation', swathid + '*')
            match = glob.glob(patt)

            if len(match) == 0:
                raise Exception('Annotation file for {0} not found in {1}'.format(self.polarization, self.safe))

            self.xml = match[0]

            ####Find TIFF file
            patt = os.path.join( self.safe, 'measurement', swathid+'*')
            match = glob.glob(patt)

            if len(match) == 0:
                raise Exception('Annotation file found for {0} but not measurement in {1}'.format(self.polarization, self.safe))

            self.tiff= match[0]

            ####Find calibration file
            patt = os.path.join( self.safe, 'annotation', 'calibration', 'calibration-' + swathid + '*')
            match = glob.glob(patt)

            if len(match) == 0 :
                raise Exception('Annotation file found for {0} but not calibration in {1}'.format(self.polarization, self.safe))

            self.calibrationXml= match[0]

            ####Find noise file
            patt = os.path.join( self.safe, 'annotation', 'calibration', 'noise-' + swathid + '*')
            match = glob.glob(patt)

            if len(match) == 0 :
                raise Exception('Annotation file found for {0} but not noise in {1}'.format(self.polarization, self.safe))

            self.noiseXml = match[0]

	    ####Find manifest file
            self.manifest = os.path.join(self.safe, 'manifest.safe')

        print('XML: ', self.xml)
        print('TIFF: ', self.tiff)
        print('CALIB: ', self.calibrationXml)
        print('NOISE: ', self.noiseXml)
        print('MANIFEST: ', self.manifest)

        return

    def parse(self):
        import zipfile

        self.validateUserInputs()

        if '.zip' in self.xml:
            try:
                parts = self.xml.split(os.path.sep)
                zipname = os.path.join('/',*(parts[:-3]))
                fname = os.path.join(*(parts[-3:]))

                with zipfile.ZipFile(zipname, 'r') as zf:
                    xmlstr = zf.read(fname)
            except:
                raise Exception('Could not read xml file {0}'.format(self.xml))
        else:
            try:
                with open(self.xml, 'r') as fid:
                    xmlstr = fid.read()
            except:
                raise Exception('Could not read xml file {0}'.format(self.xml))

        self._xml_root = ElementTree.fromstring(xmlstr)

        self.populateMetadata()
        self.populateBbox()

        ####Try and locate an orbit file
        if self.orbitFile is None:
            if self.orbitDir is not None:
                self.orbitFile = self.findOrbitFile()
                print('Found this orbitfile: %s' %self.orbitFile)

        ####Read in the orbits
        if '_POEORB_' in self.orbitFile:
            orb = self.extractPreciseOrbit()
        elif '_RESORB_' in self.orbitFile:
            orb = self.extractOrbit()

        self.product.orbit.setOrbitSource('Header')
        for sv in orb:
            self.product.orbit.addStateVector(sv)

        self.populateIPFVersion()
        self.extractBetaLUT()
        self.extractNoiseLUT()

    def getxmlattr(self, path, key):
        try:
            res = self._xml_root.find(path).attrib[key]
        except:
            raise Exception('Cannot find attribute %s at %s'%(key, path))

        return res

    def getxmlvalue(self, path):
        try:
            res = self._xml_root.find(path).text
        except:
            raise Exception('Tag= %s not found'%(path))

        if res is None:
            raise Exception('Tag = %s not found'%(path))

        return res

    def getxmlelement(self, path):
        try:
            res = self._xml_root.find(path)
        except:
            raise Exception('Cannot find path %s'%(path))

        if res is None:
            raise Exception('Cannot find path %s'%(path))

        return res

    def convertToDateTime(self, string):
        dt = datetime.datetime.strptime(string,"%Y-%m-%dT%H:%M:%S.%f")
        return dt

    def populateMetadata(self):
        """
            Create metadata objects from the metadata files
        """
        ####Set each parameter one - by - one
        mission = self.getxmlvalue('adsHeader/missionId')
        swath = self.getxmlvalue('adsHeader/swath')
        polarization = self.getxmlvalue('adsHeader/polarisation')
        orbitnumber = int(self.getxmlvalue('adsHeader/absoluteOrbitNumber'))
        frequency = float(self.getxmlvalue('generalAnnotation/productInformation/radarFrequency'))
        passDirection = self.getxmlvalue('generalAnnotation/productInformation/pass')

        groundRangePixelSize = float(self.getxmlvalue('imageAnnotation/imageInformation/rangePixelSpacing'))
        azimuthPixelSize = float(self.getxmlvalue('imageAnnotation/imageInformation/azimuthPixelSpacing'))
        azimuthTimeInterval = float(self.getxmlvalue('imageAnnotation/imageInformation/azimuthTimeInterval'))

        lines = int(self.getxmlvalue('imageAnnotation/imageInformation/numberOfLines'))
        samples = int(self.getxmlvalue('imageAnnotation/imageInformation/numberOfSamples'))

        slantRangeTime = float(self.getxmlvalue('imageAnnotation/imageInformation/slantRangeTime'))
        startingSlantRange = float(self.getxmlvalue('imageAnnotation/imageInformation/slantRangeTime'))*Const.c/2.0
        incidenceAngle = float(self.getxmlvalue('imageAnnotation/imageInformation/incidenceAngleMidSwath'))

        sensingStart = self.convertToDateTime( self.getxmlvalue('imageAnnotation/imageInformation/productFirstLineUtcTime'))
        sensingStop = self.convertToDateTime( self.getxmlvalue('imageAnnotation/imageInformation/productLastLineUtcTime'))

        ####Sentinel is always right looking
        lookSide = -1

        ###Read ascending node for phase calibration
        ascTime = self.convertToDateTime(self.getxmlvalue('imageAnnotation/imageInformation/ascendingNodeTime'))

        ###Noise correction
        correctionApplied = self.getxmlvalue('imageAnnotation/processingInformation/thermalNoiseCorrectionPerformed').upper() == 'TRUE'

        self.product.lookSide = 'RIGHT'
        self.product.numberOfSamples = samples
        self.product.numberOfLines = lines
        self.product.startingGroundRange = 0.0
        self.product.startingSlantRange = startingSlantRange
        self.product.trackNumber = ((orbitnumber-73)%175) + 1
        self.product.orbitNumber = orbitnumber
        self.product.frameNumber = 1
        self.product.polarization = polarization
        self.product.passDirection = passDirection
        self.product.radarWavelength = Const.c / frequency
        self.product.groundRangePixelSize = groundRangePixelSize
        self.product.azimuthPixelSize = azimuthPixelSize
        self.product.azimuthTimeInterval = azimuthTimeInterval
        self.product.ascendingNodeTime = ascTime
        self.product.slantRangeTime = slantRangeTime
        self.product.sensingStart = sensingStart
        self.product.sensingStop = sensingStop
        self.noiseCorrectionApplied = correctionApplied

    def populateBbox(self, margin=0.1):
        '''
        Populate the bounding box from metadata.
        '''

        glist = self.getxmlelement('geolocationGrid/geolocationGridPointList')

        lat = []
        lon = []

        for child in glist:
            lat.append( float(child.find('latitude').text))
            lon.append( float(child.find('longitude').text))

        self.product.bbox = [min(lat) - margin, max(lat) + margin, min(lon) - margin, max(lon) + margin]
        print(self.product.bbox)
        return

    def populateIPFVersion(self):
        '''
        Get IPF version from the manifest file.
        '''

        if self.manifest is None:
            return

        nsp = "{http://www.esa.int/safe/sentinel-1.0}"

        if '.zip' in self.manifest:

            import zipfile
            parts = self.manifest.split(os.path.sep)
            zipname = os.path.join('/',*(parts[:-2]))
            fname = os.path.join(*(parts[-2:]))

            try:
                with zipfile.ZipFile(zipname, 'r') as zf:
                    xmlstr = zf.read(fname)
            except:
                raise Exception('Could not read manifest file : {0}'.format(self.manifest))
        else:
            try:
                with open(self.manifest, 'r') as fid:
                    xmlstr = fid.read()
            except:
                raise Exception('Could not read manifest file: {0}'.format(self.manifest))

        try:
            root = ElementTree.fromstring(xmlstr)

            elem = root.find('.//metadataObject[@ID="processing"]')
            rdict = elem.find('.//xmlData/' + nsp + 'processing/' + nsp + 'facility/' + nsp + 'software').attrib

            self.IPFversion = rdict['version']
            print('Setting IPF version to : ', self.IPFversion)

        except:
            print('Could not read version number successfully from manifest file: ', self.manifest)
            pass
        return

    def findOrbitFile(self):
        '''
        Find correct orbit file in the orbit directory.
        '''

        datefmt = "%Y%m%dT%H%M%S"
        types = ['POEORB', 'RESORB']
        filelist = []
        match = []
        timeStamp = self.product.sensingStart+(self.product.sensingStop - self.product.sensingStart)/2.

        for orbType in types:
            files = glob.glob( os.path.join(self.orbitDir, 'S1?_OPER_AUX_' + orbType + '_OPOD*'))
            filelist.extend(files)
            ###List all orbit files

        for result in filelist:
            fields = result.split('_')
            taft = datetime.datetime.strptime(fields[-1][0:15], datefmt)
            tbef = datetime.datetime.strptime(fields[-2][1:16], datefmt)
            print(taft, tbef)

            #####Get all files that span the acquisition
            if (tbef <= timeStamp) and (taft >= timeStamp):
                tmid = tbef + 0.5 * (taft - tbef)
                match.append((result, abs((timeStamp-tmid).total_seconds())))
            #####Return the file with the image is aligned best to the middle of the file
            if len(match) != 0:
                bestmatch = min(match, key = lambda x: x[1])
                return bestmatch[0]

        if len(match) == 0:
            raise Exception('No suitable orbit file found. If you want to process anyway - unset the orbitdir parameter')

    def extractOrbit(self):
        '''
        Extract orbit information from xml node.
        '''
        node = self._xml_root.find('generalAnnotation/orbitList')

        print('Extracting orbit from annotation XML file')
        frameOrbit = Orbit()
        frameOrbit.configure()

        for child in node:
            timestamp = self.convertToDateTime(child.find('time').text)
            pos = []
            vel = []
            posnode = child.find('position')
            velnode = child.find('velocity')
            for tag in ['x','y','z']:
                pos.append(float(posnode.find(tag).text))

            for tag in ['x','y','z']:
                vel.append(float(velnode.find(tag).text))

            vec = StateVector()
            vec.setTime(timestamp)
            vec.setPosition(pos)
            vec.setVelocity(vel)
            frameOrbit.addStateVector(vec)

        return frameOrbit

    def extractPreciseOrbit(self):
        '''
        Extract precise orbit from given Orbit file.
        '''
        try:
            fp = open(self.orbitFile,'r')
        except IOError as strerr:
            print("IOError: %s" % strerr)
            return

        _xml_root = ElementTree.ElementTree(file=fp).getroot()

        node = _xml_root.find('Data_Block/List_of_OSVs')

        orb = Orbit()
        orb.configure()

        margin = datetime.timedelta(seconds=40.0)
        tstart = self.product.sensingStart - margin
        tend = self.product.sensingStop + margin

        for child in node:
            timestamp = self.convertToDateTime(child.find('UTC').text[4:])

            if (timestamp >= tstart) and (timestamp < tend):

                pos = []
                vel = []

                for tag in ['VX','VY','VZ']:
                    vel.append(float(child.find(tag).text))

                for tag in ['X','Y','Z']:
                    pos.append(float(child.find(tag).text))

                vec = StateVector()
                vec.setTime(timestamp)
                vec.setPosition(pos)
                vec.setVelocity(vel)
#                print(vec)
                orb.addStateVector(vec)

        fp.close()

        return orb

    def extractBetaLUT(self):
        '''
        Extract Beta nought look up table from calibration file.
        '''

        from scipy.interpolate import RectBivariateSpline

        if self.calibrationXml is None:
            raise Exception('No calibration file provided')

        if '.zip' in self.calibrationXml:
            import zipfile
            parts = self.calibrationXml.split(os.path.sep)
            zipname = os.path.join('/',*(parts[:-4]))
            fname = os.path.join(*(parts[-4:]))

            try:
                with zipfile.ZipFile(zipname, 'r') as zf:
                    xmlstr = zf.read(fname)
            except:
                raise Exception('Could not read calibration file: {0}'.format(self.calibrationXml))
        else:
            try:
                with open(self.calibrationXml, 'r') as fid:
                    xmlstr = fid.read()
            except:
                raise Exception('Could not read calibration file: {0}'.format(self.calibrationXml))

        _xml_root = ElementTree.fromstring(xmlstr)

        node = _xml_root.find('calibrationVectorList')
        num = int(node.attrib['count'])

        lines = []
        pixels = []
        data = None

        for ii, child in enumerate(node):
            pixnode = child.find('pixel')
            nump = int(pixnode.attrib['count'])

            if ii==0:
                data = np.zeros((num,nump))
                pixels = [float(x) for x in pixnode.text.split()]

            lines.append( int(child.find('line').text))
            signode = child.find('betaNought')
            data[ii,:] = [float(x) for x in signode.text.split()]

        lines = np.array(lines)
        pixels = np.array(pixels)

        self.betaLUT = RectBivariateSpline(lines,pixels, data, kx=1, ky=1)

        if False:
            import matplotlib.pyplot as plt
            plt.figure()
            plt.imshow(data)
            plt.colorbar()
            plt.show()

        return

    def extractNoiseLUT(self):
        '''
        Extract Noise look up table from calibration file.
        '''

        from scipy.interpolate import interp1d, InterpolatedUnivariateSpline

        if self.noiseXml is None:
            raise Exception('No calibration file provided')

        if '.zip' in self.noiseXml:
            import zipfile
            parts = self.noiseXml.split('.zip/')
            zipname = parts[0] + '.zip'
            fname = parts[1]

            try:
                with zipfile.ZipFile(zipname, 'r') as zf:
                    xmlstr = zf.read(fname)
            except:
                raise Exception('Could not read noise file: {0}'.format(self.calibrationXml))
        else:
            try:
                with open(self.noiseXml, 'r') as fid:
                    xmlstr = fid.read()
            except:
                raise Exception('Could not read noise file: {0}'.format(self.calibrationXml))

        if float(self.IPFversion) < 2.90:
            noise_range_vector_name = "noiseVectorList"
            noise_range_lut_name = "noiseLut"
            has_azimuth_noise_vectors = False
            self.noiseAzimuthLUT = None
        else:
            noise_range_vector_name = "noiseRangeVectorList"
            noise_range_lut_name = "noiseRangeLut"
            has_azimuth_noise_vectors = True

        print("Extracting noise LUT's...")

        _xml_root = ElementTree.fromstring(xmlstr)

        node = _xml_root.find(noise_range_vector_name)
        num_vectors = int(node.attrib['count'])

        print("File contains {} range noise vectors.".format(num_vectors))

        full_samples_range = np.arange(self.product.numberOfSamples)
        noise_range_lut_indices = np.zeros((num_vectors,))
        noise_range_lut_values = np.zeros((num_vectors, self.product.numberOfSamples))

        for ii, child in enumerate(node):
            print("Processing range noise vector {}/{}".format(ii + 1, num_vectors))
            pixnode = child.find('pixel')

            sample_pixels = [float(x) for x in pixnode.text.split()]

            signode = child.find(noise_range_lut_name)
            vector = np.asarray([float(x) for x in signode.text.split()])
            vector_interpolator = InterpolatedUnivariateSpline(sample_pixels, vector, k=1)
            vector_interpolated = vector_interpolator(full_samples_range)

            noise_range_lut_indices[ii] = int(child.find('line').text)
            noise_range_lut_values[ii] = vector_interpolated

        self.noiseRangeLUT = interp1d(noise_range_lut_indices, noise_range_lut_values, kind='linear', axis=0, fill_value="extrapolate")

        if has_azimuth_noise_vectors:

            node = _xml_root.find("noiseAzimuthVectorList")
            num_vectors = int(node.attrib['count'])

            print("File contains {} azimuth noise blocks.".format(num_vectors))

            noise_azimuth_lut_indices = defaultdict(list)
            noise_azimuth_lut_values = defaultdict(list)

            for block_i, child in enumerate(node):
                print("Processing azimuth noise vector {}/{}".format(block_i + 1, num_vectors))
                linenode = child.find('line')
                signode = child.find("noiseAzimuthLut")
                block_range_start = int(child.find('firstRangeSample').text)
                block_range_end = int(child.find('lastRangeSample').text)
                block_azimuth_start = int(child.find('firstAzimuthLine').text)
                block_azimuth_end = int(child.find('lastAzimuthLine').text)
                block_line_index = [float(x) for x in linenode.text.split()]
                block_vector = [float(x) for x in signode.text.split()]
                block_line_range = np.arange(block_azimuth_start, block_azimuth_end + 1)

                if len(block_vector) > 1:
                    block_vector_interpolator = InterpolatedUnivariateSpline(block_line_index, block_vector, k=1)
                    for line in block_line_range:
                        noise_azimuth_lut_indices[line].extend([block_range_start, block_range_end])
                        noise_azimuth_lut_values[line].extend([block_vector_interpolator(line)] * 2)
                else:
                    for line in block_line_range:
                        noise_azimuth_lut_indices[line].extend([block_range_start, block_range_end])
                        noise_azimuth_lut_values[line].extend([block_vector[0]] * 2)

            self.noiseAzimuthLUT = (noise_azimuth_lut_indices, noise_azimuth_lut_values)

        else:
            print("File contains no azimuth noise blocks.")

        if False:
            import matplotlib.pyplot as plt
            plt.figure()
            plt.imshow(data)
            plt.colorbar()
            plt.show()

        return

    def extractImage(self, parse=False, removeNoise=False):
        """
           Use gdal python bindings to extract image
        """
        try:
            from osgeo import gdal
        except ImportError:
            raise Exception('GDAL python bindings not found. Need this for RSAT2/ TandemX / Sentinel1.')

        from scipy.interpolate import interp1d

        if parse:
            self.parse()

        print('Extracting normalized image ....')

        src = gdal.Open('/vsizip//'+self.tiff.strip(), gdal.GA_ReadOnly)
        band = src.GetRasterBand(1)

        if self.product.numberOfSamples != src.RasterXSize:
            raise Exception('Width in metadata and image dont match')

        if self.product.numberOfLines != src.RasterYSize:
            raise Exception('Length in metadata and image dont match')

        noiseFactor = 0.0
        if (not removeNoise) and self.noiseCorrectionApplied:
            print('User asked for data without noise corrections, but product appears to be corrected. Adding back noise from LUT ....')
            noiseFactor = 1.0
        elif removeNoise and (not self.noiseCorrectionApplied):
            print('User asked for data with noise corrections, but the products appears to not be corrected. Applying noise corrections from LUT ....')
            noiseFactor = -1.0
        elif (not removeNoise) and (not self.noiseCorrectionApplied):
            print('User asked for data without noise corrections. The product contains uncorrected data ... unpacking ....')
        else:
            print('User asked for noise corrected data and the product appears to be noise corrected .... unpacking ....')

        ###Write original SLC to file
        fid = open(self.output, 'wb')
        pix = np.arange(self.product.numberOfSamples)

        if self.noiseAzimuthLUT is not None:
            noise_azimuth_lut_indices, noise_azimuth_lut_values = self.noiseAzimuthLUT

        for ii in range(self.product.numberOfLines//100 + 1):
            ymin = int(ii*100)
            ymax = int(np.clip(ymin+100,0, self.product.numberOfLines))

            if ymax == ymin:
                break

            lin = np.arange(ymin,ymax)
            ####Read in one block of data
            data = 1.0 * band.ReadAsArray(0, ymin, self.product.numberOfSamples, ymax-ymin)

            lut = self.betaLUT(lin,pix,grid=True)

            if noiseFactor != 0.0:
                noise = self.noiseRangeLUT(lin)
                if self.noiseAzimuthLUT is not None:
                    block_azimuth_noise = np.zeros_like(noise)
                    for l_i, line in enumerate(lin):
                        interpolator = interp1d(noise_azimuth_lut_indices[line], noise_azimuth_lut_values[line], kind='previous', fill_value="extrapolate")
                        block_azimuth_noise[l_i] = interpolator(np.arange(self.product.numberOfSamples))
                    noise *= block_azimuth_noise
            else:
                noise = 0.0

            #outdata = data
            outdata = np.clip(data*data/(lut*lut) + noiseFactor * noise/(lut*lut), 0, None)
            #outdata = 10 * np.log10(outdata)

            outdata.astype(np.float32).tofile(fid)

        fid.close()

        ####Render ISCE XML
        slcImage = isceobj.createImage()
        slcImage.setByteOrder('l')
        slcImage.dataType = 'FLOAT'
        slcImage.setFilename(self.output)
        slcImage.setAccessMode('read')
        slcImage.setWidth(self.product.numberOfSamples)
        slcImage.setLength(self.product.numberOfLines)
        slcImage.renderHdr()
        slcImage.renderVRT()
        self.product.image = slcImage

        band = None
        src = None

        return

    def extractSlantRange(self, full=True):
        '''
        Extract pixel-by-pixel slant range file for GRD images.
        '''

        print('Extracing slant range ....')
        from scipy.interpolate import interp1d
        from isceobj.Util import Poly1D

        node = self._xml_root.find('coordinateConversion/coordinateConversionList')
        num = int(node.attrib['count'])

        lines = []
        data = []

        for ii, child in enumerate(node):
            t0 = self.convertToDateTime(child.find('azimuthTime').text)

            lines.append( (t0-self.product.sensingStart).total_seconds()/self.product.azimuthTimeInterval)

            pp =  [float(x) for x in child.find('grsrCoefficients').text.split()]

            meangr = float( child.find('gr0').text)
            if meangr !=0 :
                raise Exception('Ground range image does not start at zero coordinate')

            data.append(pp[::-1])

        ###If polynomial starts beyond first line
        if lines[0] > 0:
            lines.insert(0, 0.)
            pp = data[0]
            data.insert(0,pp)

        ####If polynomial ends before last line
        if lines[-1] < (self.product.numberOfLines-1):
            lines.append(self.product.numberOfLines-1.0)
            pp = data[-1]
            data.append(pp)

        lines = np.array(lines)
        data = np.array(data)

        LUT = []

        for ii in range(data.shape[1]):
            col = data[:,ii]
            LUT.append(interp1d(lines, col, bounds_error=False, assume_sorted=True))

        self.gr2srLUT = LUT

        ###Write original SLC to file
        fid = open(self.slantRangeFile, 'wb')
        pix = np.arange(self.product.numberOfSamples) * self.product.groundRangePixelSize
        lin = np.arange(self.product.numberOfLines)

        polys = np.zeros((self.product.numberOfLines, len(self.gr2srLUT)))

        for ii, intp in enumerate(self.gr2srLUT):
            polys[:,ii] = intp(lin)

        minrng = 1e11
        maxrng = -1e11

        for ii in range(self.product.numberOfLines):
            pp = polys[ii,:]
            outdata = np.polyval(pp, pix)
            minrng = min(minrng, outdata[0])   ###Can be made more efficient
            maxrng = max(maxrng, outdata[-1])

            outdata.tofile(fid)

        fid.close()

        self.product.startingSlantRange = minrng
        self.product.endingSlantRange = maxrng

        ####Render ISCE XML
        slcImage = isceobj.createImage()
        slcImage.setByteOrder('l')
        slcImage.dataType = 'DOUBLE'
        slcImage.setFilename(self.slantRangeFile)
        slcImage.setAccessMode('read')
        slcImage.setWidth(self.product.numberOfSamples)
        slcImage.setLength(self.product.numberOfLines)
        slcImage.renderHdr()
        slcImage.renderVRT()
        self.product.slantRangeImage = slcImage


from typing import Union

import coreir
import hwtypes as ht

from magma.array import Array
from magma.bit import Bit
from magma.clock import Enable
from magma.common import ParamDict
from magma.bits import Bits, UInt, SInt
from magma.circuit import coreir_port_mapping, Circuit
from magma.conversions import as_bits, from_bits, bit
from magma.debug import magma_helper_function
from magma.interface import IO
from magma.generator import Generator
from magma.t import Type, Kind, In, Out, Direction
from magma.tuple import Tuple
from magma.clock import (
    AbstractReset, AsyncReset, AsyncResetN, Clock, get_reset_args
)
from magma.clock_io import ClockIO
from magma.primitives.mux import Mux
from magma.wire import wire

class _CoreIRRegister(Generator):
    """
    Internally used generator for CoreIR register primitive
    """
    def __init__(self, width, init=0, has_async_reset=False,
                 has_async_resetn=False):
        self.name = "reg_P"
        self.coreir_configargs = {"init": coreir.type.BitVector[width](init)}
        T = Bits[width]
        self.io = IO(I=In(T), CLK=In(Clock), O=Out(T))

        if has_async_resetn and has_async_reset:
            raise ValueError("Cannot have posedge and negedge asynchronous "
                             "reset")

        if has_async_reset:
            self.io += IO(arst=In(AsyncReset))
            self.name += "R"
            self.coreir_configargs["arst_posedge"] = True

        if has_async_resetn:
            self.io += IO(arst=In(AsyncResetN))
            self.name += "Rn"
            self.coreir_configargs["arst_posedge"] = False

        self.name += f"{width}"

        self.stateful = True
        self.primitive = True
        self.default_kwargs = {"init": coreir.type.BitVector[width](init)}
        self.coreir_genargs = {"width": width}
        self.renamed_ports = coreir_port_mapping

        self.coreir_name = "reg"
        if (has_async_reset or has_async_resetn):
            self.coreir_name += "_arst"

        self.verilog_name = "coreir_" + self.coreir_name
        self.coreir_lib = "coreir"

        def _simulate(self, value_store, state_store):
            cur_clock = value_store.get_value(self.CLK)

            if not state_store:
                state_store['prev_clock'] = cur_clock
                state_store['cur_val'] = ht.BitVector[width](init)

            if has_async_reset or has_async_resetn:
                cur_reset = value_store.get_value(self.arst)

            prev_clock = state_store['prev_clock']
            clock_edge = cur_clock and not prev_clock

            new_val = state_store['cur_val'].as_bool_list()

            if clock_edge:
                new_val = value_store.get_value(self.I)

            if has_async_reset and cur_reset or \
                    has_async_resetn and not cur_reset:
                new_val = ht.BitVector[width](init)

            state_store['prev_clock'] = cur_clock
            state_store['cur_val'] = ht.BitVector[width](new_val)
            value_store.set_value(self.O, new_val)
        self.simulate = _simulate

def _zero_init(T, init):
    if issubclass(T, Bits):
        return T(init)
    elif issubclass(T, Array):
        return T([_zero_init(T.T, init) for _ in range(len(T))])
    elif issubclass(T, Tuple):
        return T(*(_zero_init(t, init) for t in T.types()))
    return T(init)

def _get_T_from_init(init):
    if isinstance(init, (bool, ht.Bit)):
        return Bit
    if isinstance(init, int):
        return Bits[max(init.bit_length(), 1)]
    if isinstance(init, ht.UIntVector):
        return UInt[len(init)]
    if isinstance(init, ht.SIntVector):
        return SInt[len(init)]
    if isinstance(init, ht.BitVector):
        return Bits[len(init)]
    if isinstance(init, Type):
        return type(init)
    raise ValueError(f"Could not infer register type from {init}")

def _check_init_T(init, T):
    init_T = _get_T_from_init(init)
    if isinstance(init, int) and issubclass(T, Bits):
        # Allow int to be extended to width of T
        if len(init_T) > len(T):
            # Don't implicitly truncate
            return False
        return True
    if isinstance(init, int) and issubclass(T, Bit):
        # Allow int for bit
        if len(init_T) > 1:
            return False
        return True
    return init_T.is_wireable(T)

class AbstractRegister(Generator):
    """Abstract base class for all Register circuits. User-defined register
    types should implement this interface.
    """

    def get_enable(self, inst: Circuit) -> Enable:
        raise NotImplementedError()

class Register(AbstractRegister):
    def __init__(self, T: Kind = None,
                 init: Union[Type, int, ht.BitVector] = None,
                 reset_type: AbstractReset = None, has_enable: bool = False,
                 reset_priority: bool = True, name_map=ParamDict(I="I",
                                                                 O="O",
                                                                 CE="CE")):
        """
        T: The type of the value that is stored inside the register (e.g.
           Bits[5])

        init: (optional) A const value (i.e. init.const() == True) of type T or
              an int to be used as the initial value of the register.
              If no value is provided, the register will be initialized with 0

        has_enable: (optional) whether the register has an enable signal

        reset_type: (optional) The type of the reset port (also specifies the
                    semantic behavior of the reset signal)

        reset_priority: (optional) boolean flag choosing whether synchronous
                        reset (RESET or RESETN) has priority over enable

        name_map: (optional) ParamDict mapping default port names to new names
        """
        if T is None:
            if init is None:
                raise ValueError("User must provide type T or init value (from"
                                 " which T will be inferred)")
            T = _get_T_from_init(init)
        else:
            if not isinstance(T, Kind):
                raise TypeError(
                    f"Expected instance of Kind for argument T, not {type(T)}")
            if init is not None and not _check_init_T(init, T):
                raise ValueError(
                    f"Type {_get_T_from_init(init)} of init ({init}) does not "
                    f"match T ({T})"
                )

        (
            has_async_reset, has_async_resetn, has_reset, has_resetn
        ) = get_reset_args(reset_type)

        I_name = name_map.get("I", "I")
        O_name = name_map.get("O", "O")
        self.CE_name = CE_name = name_map.get("CE", "CE")
        self.io = IO(**{I_name: In(T), O_name: Out(T)})
        if has_enable:
            self.io += IO(**{CE_name: In(Enable)})
        self.io += ClockIO(has_async_reset=has_async_reset,
                           has_async_resetn=has_async_resetn,
                           has_reset=has_reset,
                           has_resetn=has_resetn)
        if init is None:
            init = 0

        if isinstance(init, int):
            init = _zero_init(T, init)

        self.init = init
        self.reset_type = reset_type

        coreir_init = init
        if isinstance(coreir_init, Type):
            coreir_init = as_bits(init)
        coreir_init = int(coreir_init)

        reg = _CoreIRRegister(T.flat_length(), init=coreir_init,
                              has_async_reset=has_async_reset,
                              has_async_resetn=has_async_resetn)()
        O = from_bits(T, reg.O)
        wire(O, getattr(self.io, O_name))

        I = getattr(self.io, I_name)
        if has_reset:
            reset_select = bit(self.io.RESET)
        elif has_resetn:
            reset_select = ~bit(self.io.RESETN)
        if has_enable:
            enable = getattr(self.io, CE_name)
        if (has_reset or has_resetn) and has_enable:
            if reset_priority:
                I = Mux(2, T)(name="enable_mux")(O, I, enable)
                I = Mux(2, T)()(I, init, reset_select)
            else:
                I = Mux(2, T)()(I, init, reset_select)
                I = Mux(2, T)(name="enable_mux")(O, I, enable)
        elif has_enable:
            I = Mux(2, T)(name="enable_mux")(O, I, enable)
        elif (has_reset or has_resetn):
            I = Mux(2, T)()(I, init, reset_select)
        reg.I @= as_bits(I)

    def get_enable(self, inst):
        return getattr(inst, self.CE_name, None)

@magma_helper_function
def register(value, **kwargs):
    T = type(value).qualify(Direction.Undirected)
    inst_kwargs = {}
    try:
        name = kwargs.pop("name")
        inst_kwargs["name"] = name
    except KeyError:
        pass
    ckt = Register(T, **kwargs)
    return ckt(**inst_kwargs)(value)


import base64
import datetime
import json
from OpenSSL import crypto
from josepy import JWK

import pem

def cert_to_jwk(certificates: str, public_key: str) -> str:
    """
    coverts a pem public key and a pem certificate chain to a jwk with a x5c element
    :param certificates: the certificates for the x5c as pem key list
    :param public_key: the pem public key as string
    :return: the jwk as string
    """

    # convert public key to jwk
    public_key = crypto.load_publickey(crypto.FILETYPE_PEM, public_key)
    jwk = JWK.load(crypto.dump_publickey(crypto.FILETYPE_PEM, public_key))

    # covert cert chain to base64 encoded ASN1 according to https://tools.ietf.org/html/rfc7517#section-4.7
    x5c = []
    for cert in pem.parse(str.encode(certificates)):
        cert_pem = crypto.load_certificate(crypto.FILETYPE_PEM, cert.as_bytes())
        x5c.append(
            base64.b64encode(
                crypto.dump_certificate(crypto.FILETYPE_ASN1, cert_pem)
            ).decode()
        )

    # dump jwk obj to json and add x5c cer chain
    jwk_j = jwk.to_json()
    jwk_j["x5c"] = x5c
    return json.dumps(jwk_j)

def get_cert_valid_until(certificates: str) -> datetime:
    """
    get the validity of a cert as datetime
    :param certificates: the certificates for the x5c as pem key list
    :return: the min remaining date as datetime
    """

    min_remaining_date = None
    for cert in pem.parse(str.encode(certificates)):
        cert_pem = crypto.load_certificate(crypto.FILETYPE_PEM, cert.as_bytes())
        certificate_remaining_date = datetime.datetime.strptime(
            str(cert_pem.get_notAfter()), "b'%Y%m%d%H%M%SZ'"
        )
        if (
            min_remaining_date is None
            or min_remaining_date > certificate_remaining_date
        ):
            min_remaining_date = certificate_remaining_date

    return min_remaining_date


import numpy as np
import matplotlib.pyplot as plt
xs=np.linspace(0,1,1001)
r=1.8
for x in xs:
    def f(x,r):
        if x<=0.5:
            return r*x
        else:
            return r-r*x

    plt.plot(x,f(x,r),'r.')
    plt.plot(x,f(f(x,r),r),'b.')
    #plt.plot(x,f(f(f(x,r),r),r),'k.')
    plt.plot(x,x,'g.')
plt.xlabel('$x_n$')
plt.ylabel('$x_{n+1}$')
plt.show()


from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtGui import QIcon
from design import Ui_MainWindow
import sys

cnt = 1

class MainWin(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainWin, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.init_UI()
        self.ui.clickB.clicked.connect(self.change_label_txt)
        self.ui.clickB_2.clicked.connect(self.cnt_refresh)

    def init_UI(self):
        self.setWindowTitle('Clicker')
        self.setWindowIcon(QIcon('icon.png'))

    def change_label_txt(self):
        global cnt
        self.ui.count.setText(str(cnt))
        cnt += 1

    def cnt_refresh(self):
        global cnt
        cnt = 1
        self.ui.count.setText('0')

app = QtWidgets.QApplication([])
application = MainWin()
application.show()

sys.exit(app.exec())