import smcmodel_localize.model
import numpy as np
import scipy.optimize
import tqdm

class LocalizationModelMultilateration:

    def __init__(
        self,
        num_objects,
        num_anchors,
        anchor_positions,
        initial_position_guesses,
        num_moving_object_dimensions = 3,
        num_fixed_object_dimensions = 0,
        fixed_object_positions = None,
        measurement_value_name = 'range'
    ):
        num_object_dimensions = num_moving_object_dimensions + num_fixed_object_dimensions
        anchor_positions = np.asarray(anchor_positions)
        initial_position_guesses = np.asarray(initial_position_guesses)
        if anchor_positions.shape[0] != num_anchors:
            raise ValueError('Number of anchors specified as {} but positions specified for {} anchors'.format(
                num_anchors,
                anchor_positions.shape[0]
            ))
        if anchor_positions.shape[1] != num_object_dimensions:
            raise ValueError('Specified anchor positions have {} spatial dimensions but model is specified to have {} + {} = {} object dimensions'.format(
                anchor_positions.shape[1],
                num_moving_object_dimensions,
                num_fixed_object_dimensions,
                num_object_dimensions
            ))
        if initial_position_guesses.shape[0] != num_objects:
            raise ValueError('Number of objects specified as {} but initial position guesses specified for {} objects'.format(
                num_objects,
                initial_position_guesses.shape[0]
            ))
        if initial_position_guesses.shape[1] != num_moving_object_dimensions:
            raise ValueError('Number of moving object dimensions specified as {} but initial position guesses have {} spatial dimensions'.format(
                num_moving_object_dimensions,
                initial_position_guesses.shape[1]
            ))
        if num_fixed_object_dimensions != 0 and fixed_object_positions is None:
            raise ValueError('Number of fixed object dimensions specified as {} but no fixed object positions specified')
        if fixed_object_positions is not None:
            fixed_object_positions = np.asarray(fixed_object_positions)
            if fixed_object_positions.shape[0] != num_objects:
                raise ValueError('Number of objects specified as {} but fixed object object positions specified for {} objects'.format(
                    num_objects,
                    fixed_object_positions.shape[0]
                ))
            if fixed_object_positions.shape[1] != num_fixed_object_dimensions:
                raise ValueError('Number of fixed object dimensions specified as {} but specified fixed object positions have {} spatial dimensions'.format(
                    num_fixed_object_dimensions,
                    fixed_object_positions.shape[1]
                ))
        self.num_objects = num_objects
        self.num_anchors = num_anchors
        self.anchor_positions = anchor_positions
        self.initial_position_guesses = initial_position_guesses
        self.num_moving_object_dimensions = num_moving_object_dimensions
        self.num_fixed_object_dimensions = num_fixed_object_dimensions
        self.num_object_dimensions = num_object_dimensions
        self.fixed_object_positions = fixed_object_positions
        self.measurement_value_name = measurement_value_name
        self.observation_structure = smcmodel_localize.model.observation_structure_generator(
            self.num_anchors,
            self.num_objects,
            self.measurement_value_name
        )
        self.state_summary_structure = state_summary_structure_generator_multilateration(
            self.num_objects,
            self.num_moving_object_dimensions
        )

    def estimate_state_time_series_multilateration(
        self,
        observation_data_queue,
        state_summary_database,
        progress_bar = False,
        num_observations = None
    ):
        position_guesses = self.initial_position_guesses
        if progress_bar:
            if num_observations is not None:
                remaining_iterations = num_observations
            else:
                remaining_iterations = None
            t = tqdm.tqdm(total = remaining_iterations)
        for timestamp, observation_arrays in observation_data_queue:
            observation = observation_arrays[self.measurement_value_name]
            if observation.shape[1] != self.num_anchors:
                raise ValueError('Number of anchors specified as {} but observation data contains ranges for {} anchors'.format(
                    self.num_anchors,
                    observation.shape[1],
                ))
            if observation.shape[2] != self.num_objects:
                raise ValueError('Number of objects specified as {} but observation data contains ranges for {} objects'.format(
                    self.num_objects,
                    observation.shape[2],
                ))
            if position_guesses.shape[0] != self.num_objects:
                raise ValueError('Number of objects specified as {} but initial position guesses specified for {} objects'.format(
                    self.num_objects,
                    position_guesses.shape[0]
                ))
            if position_guesses.shape[1] != self.num_moving_object_dimensions:
                raise ValueError('Number of moving object dimensions specified as {} but initial position guesses have {} spatial dimensions'.format(
                    self.num_moving_object_dimensions,
                    position_guesses.shape[1]
                ))
            if self.fixed_object_positions is not None:
                initial_guesses = np.concatenate(
                    (position_guesses, self.fixed_object_positions),
                    axis = -1
                )
            else:
                initial_guesses = position_guesses
            moving_object_positions = np.empty((self.num_objects, self.num_object_dimensions))
            for object_index in range(self.num_objects):
                moving_object_positions[object_index] = self.estimated_position(
                    observation[0, :, object_index],
                    position_guesses[object_index]
                )
            moving_object_positions_expanded = np.expand_dims(moving_object_positions, axis = 0)
            state_summary = {
                'moving_object_positions_multilateration': moving_object_positions_expanded,
            }
            state_summary_database.write_data(
                timestamp, state_summary)
            position_guesses = moving_object_positions[:, :self.num_moving_object_dimensions]
            if progress_bar:
                t.update()
        if progress_bar:
            t.close()

    def estimated_position(self,measured_ranges, initial_guess):
        solution = scipy.optimize.minimize(
            fun = self.mean_squared_error,
            x0 = initial_guess,
            args = (measured_ranges))
        return solution.x

    def mean_squared_error(self, object_position, measured_ranges):
        num_measured_ranges = np.sum(np.logical_not(np.isnan(measured_ranges)))
        calculated_ranges = self.ranges(object_position)
        errors = measured_ranges - calculated_ranges
        sum_squared_errors = np.nansum(np.square(errors))
        mean_squared_error = sum_squared_errors/num_measured_ranges
        return mean_squared_error

    def ranges(self, object_position):
        object_position_expanded = np.expand_dims(object_position, 0)
        return np.linalg.norm(object_position_expanded - self.anchor_positions, axis = -1)

def state_summary_structure_generator_multilateration(
    num_objects,
    num_moving_object_dimensions
):
    state_summary_structure = {
        'moving_object_positions_multilateration': {
            'shape': [num_objects, num_moving_object_dimensions],
            'type': 'float32'
        }
    }
    return state_summary_structure


from django.conf.urls.defaults import *
from django.contrib import admin
from isobar.urls import urlpatterns
from isobar.apps.transurlvania.defaults import *
from django.views.generic.simple import direct_to_template
from isobar.apps.work.models import *
from isobar.apps.contact.models import *
from isobar.apps.about.models import *
from isobar.apps.home.models import *
from isobar.apps.people.models import Person as PeoplePerson
from isobar.apps.people.models import Department
from django.contrib.flatpages.models import *

# remove older models in favor of new ones so the old ones don't show up on the admin panel:
# example: work vs workisobar, about vs aboutisobar
admin.site.unregister(About)
admin.site.unregister(CaseStudy)
admin.site.unregister(Person)
admin.site.unregister(Location)
admin.site.unregister(Link)
admin.site.unregister(Map)
admin.site.unregister(Department)
admin.site.unregister(PeoplePerson)
admin.site.unregister(Layout)

admin.site.unregister(FlatPage)

localpatterns = lang_prefixed_patterns('aboutisobar.views',
	url(r'^about/services/$', 'services', name='service_view'),
	url(r'^about/services/ajax/$', 'services_ajax', name='service_ajax'),
	url(r'^about/profiles/$', 'profiles', name='profile_view'),
	url(r'^about/partners/$', 'partners', name='partner_view'),
  url(r'^about/partners/ajax/$', 'partners_ajax', name='partner_ajax'),
  url(r'^about/profiles/ajax/$', 'profiles_ajax', name='profile_ajax'),
	url(r'^about/$', 'profiles', name='profile_view'),
)

localpatterns += lang_prefixed_patterns('workisobar.views',
    url(r'^work/$', 'index', name="work_view"),
    url(r'^work/casestudy/(?P<slug>[a-zA-Z0-9-_]*)/$', 'index', name="specific_work_view"),
    url(r'^work/clients/$', 'clients', name="work_clients_view"),
    url(r'^work/clients/json/$', 'clients_json', name="work_clients_json"),
    url(r'^work/clients/ajax/$', 'clients_ajax', name="work_clients_ajax"),
    url(r'^work/casestudies/ajax/$', 'index_ajax', name="work_ajax"),
)

localpatterns += lang_prefixed_patterns('contactisobar.views',
    #url(r'^contact/$', 'index_proxy_contact', name='index_contact_proxy'),
    #url(r'^contact/(?P<location>[a-zA-Z-_,. ]+)/$', 'index_proxy_contact', name='index_contact_proxy'),
    url(r'^contact/$', 'index', name='index'),
    url(r'^contact/(?P<location>[a-zA-Z-_,. ]+)/$', 'index', name='index'),
    url(r'^contact_test/(?P<location>[a-zA-Z-_,. ]+)/$', 'index', name='index_contact_test'),
	url(r'^contact_data/(?P<location>[a-zA-Z-_,. ]+)/$', 'contact_data', name='contact_data_view'),
    url(r'^common_contact/$', 'common_contact', name='common_contact_view'),
    url(r'^shelf_proxy/$', 'shelf_proxy_contact', name='shelf_contact_proxy'),
    url(r'^shelf_contact/$', 'shelf_contact', name='shelf_contact_view'),
	url(r'^office_contact/(?P<name>[a-zA-Z-_,. ]+)/$', 'office_contact', name='office_contact_view'),
)

localpatterns += lang_prefixed_patterns('labsisobar.views',
    url(r'^nowlab/$', 'index', name='labs_view'),
)

localpatterns += patterns('homeisobar.views',
    url(r'^$', 'index', name="home_view"),
)

localpatterns += lang_prefixed_patterns('homeisobar.views',
	url(r'^$', 'index', name="home_view"),
)

localpatterns += lang_prefixed_patterns('careerisobar.views',
	url(r'^career/jobs/job/(?P<job>[a-zA-Z0-9-_]+)/$', 'job_item', name='job_item'),
	url(r'^career/jobs/(?P<region>[a-zA-Z-_,. ]+)/$', 'job_list_json', name='job_list_json'),

	#url(r'^career/$', 'job_view', name="job_view"),
	url(r'^career/jobs/$', 'job_list', name="job_list"),
)
urlpatterns = localpatterns + urlpatterns


import os
from ast import literal_eval

import kaldi_io
import numpy as np
import pandas as pd

from classifier.classifier_lda_plda import EmbeddingSource

__author__ = "Kailash Karthik S"
__uni__ = "ks3740"
__email__ = "kailashkarthik.s@columbia.edu"
__status__ = "Development"

class SessionArkReactor:
    def __init__(self, ark_files, embeddings_file, embedding_source: EmbeddingSource):
        self.embedding_source = embedding_source
        self.speech_arks = [(os.path.basename(file_), kaldi_io.read_vec_flt_ark(file_)) for file_ in ark_files]
        self.multi_modal_embeddings = pd.read_csv(embeddings_file)
        self.multi_modal_embeddings[embedding_source.value] = self.multi_modal_embeddings[embedding_source.value].apply(
            literal_eval)

    @staticmethod
    def cluster_arks(arks):
        combined_ark = {
            '1': list(),
            '2': list(),
            '3': list(),
            '4': list(),
            '5': list(),
        }
        for ark_file, ark in arks:
            for key, vector in ark:
                combined_ark[SessionArkReactor.get_session_number(key)].append((key, vector))
        return combined_ark

    @staticmethod
    def get_session_number(ark_key):
        # anger/disgust-test-01F-improvisation-01-000-M
        return SessionArkReactor.decompose_ark_key(ark_key)[0]

    @staticmethod
    def decompose_ark_key(key):
        # anger/disgust-test-01F-improvisation-01-000-M
        key_components = key.split('-')
        return key_components[2][1], key_components[2][2], key_components[3], key_components[4], key_components[5], \
               key_components[6]

    def get_modified_file_name(self, session):
        return self.embedding_source.value + '_xvector.' + session + '.ark'

    def get_modified_ark_components(self, speech_arks):
        modified_arks = []
        for session, speech_ark in speech_arks.items():
            ark = []
            for key, array_ in speech_ark:
                session_number, mocap_source, dialogue_type, dialogue_number, utterance_number, speaker = \
                    self.decompose_ark_key(key)
                embeddings_record = self.multi_modal_embeddings[
                    (self.multi_modal_embeddings['Session_Number'] == int(session_number)) & (
                            self.multi_modal_embeddings['Mocap_Source'] == mocap_source) & (
                            self.multi_modal_embeddings['Dialogue_Type'] == dialogue_type) & (
                            self.multi_modal_embeddings['Dialogue_Number'] == dialogue_number) & (
                            self.multi_modal_embeddings['Utterance_Number'] == int(utterance_number)) & (
                            self.multi_modal_embeddings['Speaker'] == speaker)]
                embedding = embeddings_record[self.embedding_source.value].values[0]
                ark.append((key, embedding))
            modified_arks.append((session, ark))
        return modified_arks

    def create_session_wise_arks(self):
        session_wise_speech_arks = self.cluster_arks(self.speech_arks)
        modified_ark_components = self.get_modified_ark_components(session_wise_speech_arks)
        for session, components in modified_ark_components:
            with open('iemocap/data/kaldi/modified/' + self.get_modified_file_name(session), 'wb') as file_:
                for key, vector in components:
                    kaldi_io.write_vec_flt(file_, np.array(vector), key)

if __name__ == '__main__':
    ark_files_ = [
        'iemocap/data/kaldi/xvector.1.ark',
        'iemocap/data/kaldi/xvector.2.ark',
        'iemocap/data/kaldi/xvector.3.ark',
        'iemocap/data/kaldi/xvector.4.ark',
        'iemocap/data/kaldi/xvector.5.ark',
    ]
    embeddings_file_ = 'iemocap/data/dataset_with_multi_modal_embeddings.csv'
    reactor = SessionArkReactor(ark_files_, embeddings_file_, EmbeddingSource.TEXT)
    reactor.create_session_wise_arks()
    reactor = SessionArkReactor(ark_files_, embeddings_file_, EmbeddingSource.SPEECH)
    reactor.create_session_wise_arks()
    reactor = SessionArkReactor(ark_files_, embeddings_file_, EmbeddingSource.MULTIMODAL)
    reactor.create_session_wise_arks()


#!/usr/bin/env python
"""
Tests for geneve tool.
"""

import json
import os.path
import pytest
from eve.io.mongo import Validator
from evegenie import EveGenie

test_data = {
    'user': {
        'name': 'Turtle Man',
        'age': 71,
        'alive': True,
        'title': 'Champion of Sea Dwellers',
        'inventory': ['map', 'apple', 'sword', 'potion'],
        'primary_artifact': 'objectid:artifact',
        'secondary_artifacts': ['objectid: artifact', 'objectid:artifact'],
        'address': {
            'address': '123 Pacific Ocean',
            'city': 'Neptunville',
            'state': 'wet',
        },
        'attack_bonus': '1-10',
        'difficulty': '0.0-1.0',
    },
    'artifact': {
        'name': 'Sword of Speed',
        'cost': 501.01,
        'color': 'red',
        'stats': {
            'weight': 200.01,
            'length': 3.01,
            'powers': {
                'strike': 1,
                'deflect': 1,
                'speed': 3,
            },
        }
    }
}

if not os.path.isfile('test.json'):
    with open('test.json', 'w') as ofile:
        ofile.write(json.dumps(test_data, indent=4, separators=(',',' : ')))

test_data_answer = {
    'user': {
        'name': {
            'type': 'string',
        },
        'age': {
            'type': 'integer',
        },
        'alive': {
            'type': 'boolean'
        },
        'title': {
            'type': 'string'
        },
        'inventory': {
            'type': 'list',
            'schema': {
                'type': 'string'
            }
        },
        'primary_artifact': {
            'type': 'objectid',
            'data_relation': {
                'resource': 'artifact',
                'field': '_id',
                'embeddable': True,
            },
        },
        'secondary_artifacts': {
            'type': 'list',
            'schema': {
                'type': 'objectid',
                'data_relation': {
                    'resource': 'artifact',
                    'field': '_id',
                    'embeddable': True,
                },
            },
        },
        'address': {
            'type': 'dict',
            'schema': {
                'address': {'type': 'string'},
                'city': {'type': 'string'},
                'state': {'type': 'string'}
            },
        },
        'attack_bonus': {
            'max': 10,
            'min': 1,
            'type': 'integer'
        },
        'difficulty': {
            'max': 1.0,
            'min': 0.0,
            'type': 'float'
        }
    },
    'artifact': {
        'name': {
            'type': 'string'
        },
        'cost': {
            'type': 'float'
        },
        'color': {
            'type': 'string'
        },
        'stats': {
            'type': 'dict',
            'schema': {
                'weight': {'type': 'float'},
                'length': {'type': 'float'},
                'powers': {
                    'type': 'dict',
                    'schema': {
                        'strike': {'type': 'integer'},
                        'deflect': {'type': 'integer'},
                        'speed': {'type': 'integer'},
                    }
                },
            }
        }
    }
}

simple_test_data = {
    'user': {
        'name': 'Turtle Man',
        'age': 71,
        'alive': True,
        'title': 'Champion of Sea Dwellers',
    },
}

test_data_answer_string = json.dumps(test_data_answer)

def test_input_string():
    """
    Make sure data loaded from string is parsed as expected.

    :return:
    """
    eg = EveGenie(data=json.dumps(test_data))
    assert(dict(eg) == test_data_answer)

def test_input_dict():
    """
    Make sure data loaded as a dict is parsed as expected.

    :return:
    """
    eg = EveGenie(data=test_data)
    assert(dict(eg) == test_data_answer)

def test_input_file():
    """
    Make sure data loaded from file is parsed as expected.

    :return:
    """
    eg = EveGenie(filename='test.json')
    assert(dict(eg) == test_data_answer)

def test_input_both_inputs():
    """
    Make sure when both data types are passed the data is still parsed as expected.

    :return:
    """
    eg = EveGenie(data=test_data, filename='test.json')
    assert(dict(eg) == test_data_answer)

def test_simple_endpoint_validation():
    """
    Test that the endpoint schema generated will validate when used in Eve.

    :return:
    """
    eg = EveGenie(data=simple_test_data)
    data = dict(eg)
    for endpoint in data:
        v = Validator(data[endpoint])
        assert(v.validate(simple_test_data[endpoint]))

if __name__ == '__main__':
    pytest.main('egtest.py')


class Car:
    def mcolor(self,color):
         self.color=color
         print(self.color)
class Bmw(Car):
    def topspeed(self,speed):
        self.speed=speed
        print(self.speed)

b=Bmw()
c=Car()
b.mcolor("RED")
b.topspeed(120)
c.mcolor("RED")
#c.topspeed(120)


"""
1840. Matrix restoration
"""
class Solution:
    """
    @param n: the row of the matrix
    @param m: the column of the matrix
    @param after: the matrix
    @return: restore the matrix
    """
    def matrixRestoration(self, n, m, after):
        # write your code here
        if not after or not after[0]:
            return after

        before_matrix = [[0] * m for _ in range(n)]

        for i in range(n):
            for j in range(m):
                before_matrix[i][j] = after[i][j]
                if j > 0:
                    before_matrix[i][j] -= after[i][j - 1]
                if i > 0:
                    before_matrix[i][j] -= after[i - 1][j]
                if i > 0 and j > 0:
                    before_matrix[i][j] += after[i - 1][j - 1]

        return before_matrix


#!/usr/bin/env python3
# -*- encoding=utf-8 -*-

from bs4 import BeautifulSoup
import requests
import time

html = open('classinfo.htm','r',encoding='utf-8')
html_txt = html.read()

soup = BeautifulSoup(html_txt)

all_people_list=[]

for i in range(1,97):
    #one_info = {'QQnum':None,'QQname':None,'QQrealname':None,'QQpicurl':None}
    one_info = {}
    one_QQnum = soup.find_all('tr')[i].find_all('td')[4].string
    one_QQnum = one_QQnum.strip()
    #one_info['QQnum'] = one_QQnum

    one_QQname = soup.find_all('tr')[i].find_all('td')[2].contents[5].string
    one_QQname = one_QQname.strip()

    #one_info['QQname']=one_QQname

    one_QQrealname = soup.find_all('tr')[i].find_all('td')[3].contents[1].string
    #print(one_QQrealname)
    #print(type(one_QQrealname))
    one_QQrealname = str(one_QQrealname)
    one_QQrealname = one_QQrealname.strip()

    #one_QQrealname = one_QQrealname.strip()
    #one_info['QQrealname'] = one_QQrealname

    one_QQpicurl = 'https://q4.qlogo.cn/g?b=qq&nk='+one_QQnum+'&s=140'

    one_info = {'QQnum':one_QQnum,'QQname':one_QQname,'QQrealname':one_QQrealname,'QQpicurl':one_QQpicurl}
    all_people_list.append(one_info)

#print(all_people_list)

for i in range(len(all_people_list)):
    the_QQnum = all_people_list[i]['QQnum']
    the_QQname = all_people_list[i]['QQname']
    the_QQrealname = all_people_list[i]['QQrealname']
    pic_name = the_QQnum + '-' + the_QQname + '-' + the_QQrealname

    ir = requests.get(all_people_list[i]['QQpicurl'],headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'})
    if ir.status_code==200:
        open(picname,'wb').write(ir.content)

    time.sleep(1)
    print('one file success!')


# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.3'
#       jupytext_version: 0.8.6
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import os, sys, subprocess
import warnings

import pandas as pd
import numpy as np

import scipy.stats
import scipy.optimize

import numba
import bebi103
import tidy_data

data_path = "../data/"
rg = np.random.default_rng()
# -

def log_like_iid_gamma(params, n):
    """
    Calculates log likelihood for iid gamma measurements.

    Inputs:
        params : tuple or list
            Must have 2 elements. In order, mean of alpha, beta
        n : ndarray of observed catastrophe times
    Outputs:
        log-likelihood.
    """
    for param in params:
        if param <= 0:
            return -np.inf
    alpha, beta = params

    return np.sum(scipy.stats.gamma.logpdf(n, alpha, loc=0, scale=1 / beta))

# +
def mle_iid_gamma(n, x0):
    """
    Calculates the MLE for a gamma distribution.

    Inputs:
        n: ndarray of observations / data that we are modeling with the parameters
        x0 : 1x2 ndarray
            Initial estimates for the optimization.
            Must have 2 elements. In order, alpha, beta.
    Returns:
        res.x : list
            MLE for each parameter, in same order as x0.
    """
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")

        res = scipy.optimize.minimize(
            fun=lambda params, n: -log_like_iid_gamma(params, n),
            x0=x0,
            args=(n,),
            method='Powell'
        )

    if res.success:
        return res.x
    else:
        raise RuntimeError('Convergence failed with message', res.message)

#uncomment to run
#initial param estimates
# alpha = 2
# # find rate of arrivals
# beta = alpha / np.mean(times_to_catastrophe)
# init_params = [alpha, beta]
# # test to check that we get reasonable MLEs
# mle = mle_iid_gamma(times_to_catastrophe, init_params)
# output
#array([2.40754885, 0.00546287])

# -

def gen_gamma(params, size, rg):
    """
    generating function for gamma distribution
    Inputs:
        params (ndarray) alpha, beta
        size : number of points to draw
        rg : random number generator (np.random.default_rng)
    Outputs:
        values from gamma distribution, parametrized by params
    """
    alpha, beta = params
    return rg.gamma(alpha, 1 / beta, size=size)

# +
def get_conf_intrvl_gamma(array, init_param, size):
    """
    draw conf ints for alpha and beta of a gamma distribution
    using bebi103.bootstrap.draw_bs_reps_mle
    Inputs:
        array : observed data
        init_param : tuple of initial estimates
        size : bootstrap rep size
    Outputs:
        95% conf int
    """
    mle_bs_reps = bebi103.bootstrap.draw_bs_reps_mle(
                    mle_iid_gamma, #func with scipy.optimize
                    gen_gamma, #generating function, after parameter estimates
                    array,
                    mle_args=(init_param,), #additional args to pass into mle_iid_gamma
                    size = size,
                    progress_bar = False,
                    n_jobs = 1)

    return np.transpose(np.percentile(mle_bs_reps, [2.5, 97.5], axis=0))

#uncomment to run
# conf_intrvls = get_conf_intrvl_gamma(times_to_catastrophe, init_params, 1001)
# conf_intrvls
# #outputs
# alpha conf int: [1.99190, 2.90202]
# beta conf int : [0.00454, 0.00676]
# -

def log_like_iid_two_events(params, n, original_data):
    """
    Calculates log likelihood for iid gamma measurements.

    Inputs:
        params : tuple or list
            Must have 2 elements. In order, beta_1, delta_beta
        n : the data that we have
        y : n x 1 numpy array

    Outputs:
        log-likelihood.
    """
    #beta1 and delta_beta can't be negative.
    #Return -inf since we want to maximize positive values only
    for param in params:
        if param < 0:
            return -np.inf

    beta_1, delta_beta = params
    if beta_1 > 2 / np.mean(original_data):
        return -np.inf

    #if deltabeta approches 0, the model collapses to a gamma distribution
    # gamma distribution with alpha = 2 and beta = beta1 (or beta2)
    if np.isclose(delta_beta, 0):
        return np.sum(scipy.stats.gamma.logpdf(n, 2, loc=0, scale=1 / beta_1))

    #if not close, we'll return the actual log likelihood of the two-process story
    length = len(n)
    tmp1 = length * np.log(beta_1) + length * np.log(beta_1 + delta_beta) - length * np.log(delta_beta)
    tmp2 = -np.sum(beta_1 * n) + np.sum(np.log(1 - np.exp(-delta_beta * n)))
    return tmp1 + tmp2

def mle_iid_two_events(n, x0, original_data):
    """
    Calculates the MLE for a gamma distribution.

    Inputs:
        n  : ndarray of observations of data
        x0 : 1x2 ndarray
            Initial estimates for the optimization.
            Must have 2 elements. In order, beta_1, delta_beta
    Returns:
        res.x : list
            MLE for each parameter, in same order as x0.
    """
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")

        res = scipy.optimize.minimize(
            fun=lambda params, n, original_data: -log_like_iid_two_events(params, n, original_data),
            x0=x0,
            args=(n, original_data),
            method='Powell',
            tol=1e-8,
        )

    if res.success:
        return res.x
    else:
        raise RuntimeError('Convergence failed with message', res.message)

# +
# #uncomment to test out
# #set initial parameters for two arrival story
# beta1 = 2 / np.sum(times_to_catastrophe) * len(times_to_catastrophe) - 0.00000001
# init_params_two_events = [beta1, .00000001]

# # first do beta_1 and delta_beta
# mle_two_events = mle_iid_two_events(times_to_catastrophe, init_params_two_events, times_to_catastrophe)

# # test to check that we get reasonable MLEs
# mle_two_events = np.array([
#     mle_two_events[0],
#     mle_two_events[0] + mle_two_events[1]
# ])

# mle_two_events

# # # outputs
# # # beta1 mle: 0.0045381223
# # # delta beta mle: 0.0000000062

# # # outputs
# # # beta1 mle: 0.0045381223
# # # beta2 mle: 0.0045381285
# -

def gen_two_events(params, n, size, rg):
    """
    Inputs:
        params: ndarray, beta_1, delta_beta = params
        size : number of bootstrap samples

    """
    beta_1, delta_beta = params
    beta_2 = beta_1 + delta_beta
    return rg.exponential(1/beta_1, size = size) + rg.exponential(1/beta_2, size = size)

# +
def get_conf_intrvl_two_events(array, init_param, original_data, size):
    """
    draw conf ints for beta1 and beta2 of two story distribution
    using bebi103.bootstrap.draw_bs_reps_mle
    Inputs:
        array : observed data
        init_param : tuple of initial estimates
        size : bootstrap rep size
    Outputs:
        95% conf int
    """
    mle_bs_reps = bebi103.bootstrap.draw_bs_reps_mle(
                    mle_iid_two_events,
                    gen_two_events,
                    array,
                    mle_args=(init_param, original_data,),
                    gen_args = (array, ),
                    size = size,
                    progress_bar = False,
                    n_jobs = 1)
    #work with individual arrays containing all estimates of a single parameter
    mle_bs_reps = np.transpose(mle_bs_reps)

    beta_1s = mle_bs_reps[0]
    beta_2s = beta_1s + mle_bs_reps[1]

    return np.percentile(np.array(
        [beta_1s, beta_2s]
        ), [2.5, 97.5], axis=1)

# #uncomment to run
# conf_intrvls_two_events = get_conf_intrvl_two_events(times_to_catastrophe, init_params_two_events, times_to_catastrophe, 1001)
# conf_intrvls_two_events = np.transpose(conf_intrvls_two_events)

# #output:
# # beta1 conf int: [0.003227, 0.004538]
# # beta2 conf int : [0.004132, 0.008896]

# +
def main():
    df = tidy_data.tidy_dic()

    #Labelled data
    labeled_data = df.loc[df['labeled'] == True, 'time to catastrophe (s)']

    #convert to ndarray. Will use in part b as well.
    times_to_catastrophe = labeled_data.to_numpy()

    ###### gamma:
    alpha = 2
    # find rate of arrivals
    beta = alpha / np.mean(times_to_catastrophe)
    init_params = [alpha, beta]
    # test to check that we get reasonable MLEs
    mle = mle_iid_gamma(times_to_catastrophe, init_params)
    #confints
    conf_intrvls = get_conf_intrvl_gamma(times_to_catastrophe, init_params, 1001)

    ####### two events:
    #set initial parameters for two arrival story
    beta1 = 2 / np.sum(times_to_catastrophe) * len(times_to_catastrophe) - 0.00000001
    init_params_two_events = [beta1, .00000001]

    # first do beta_1 and delta_beta
    mle_two_events = mle_iid_two_events(times_to_catastrophe, init_params_two_events, times_to_catastrophe)

    # test to check that we get reasonable MLEs
    mle_two_events = np.array([
        mle_two_events[0],
        mle_two_events[0] + mle_two_events[1]
    ])
    #confints
    conf_intrvls_two_events = get_conf_intrvl_two_events(times_to_catastrophe, init_params_two_events, times_to_catastrophe, 1001)
    conf_intrvls_two_events = np.transpose(conf_intrvls_two_events)

    ######### print statements

    #gamma first
    print("""initial parameters for \nalpha: {0} \nbeta: {1:.6f}""".format(*init_params))

    print(
    """
    alpha conf int: [{0:.5f}, {1:.5f}] \n
    beta conf int : [{2:.5f}, {3:.5f}]
    """.format(
    *conf_intrvls.flatten()
    )
    )

    #two story
    print(
    """
    beta1 mle: {0:.10f}\n
    beta2 mle: {1:.10f}
    """.format(
    mle_two_events[0],
    mle_two_events[1],
    )
    )

    print(
        """
        beta1 conf int: [{0:.6f}, {1:.6f}] \n
        beta2 conf int : [{2:.6f}, {3:.6f}]
        """.format(
        *conf_intrvls_two_events.flatten()
        )
    )
    return True

if __name__ == '__main__': main()
# -

#!jupytext --to python parameter_estimates.ipynb


import unittest

from sqlalchemy import MetaData, Table, Column, Integer, String, select
from sqlalchemy.dialects import postgresql
from sqlalchemy.schema import CreateIndex

from pedsnetdcc.concept_name_transform import ConceptNameTransform
from pedsnetdcc.tests.transform_test_utils import clean

class ConceptNameTest(unittest.TestCase):

    def setUp(self):
        self.metadata = MetaData()

        foo_col = Column('foo_concept_id', Integer)
        bar_col = Column('bar_concept_id', Integer)

        self.table1 = Table('table1', self.metadata,
                            foo_col,
                            bar_col)

        baz_col = Column('baz_concept_id', Integer)

        self.table2 = Table('table2', self.metadata,
                            baz_col)

        # Create and add the `concept` table to the sqlalchemy metadata
        self.concept = Table('concept', self.metadata,
                             Column('concept_id', Integer),
                             Column('concept_name', String(512)))

    def test_modify_select(self):

        select_obj = select([self.table1])
        join_obj = self.table1

        select_obj, join_obj = ConceptNameTransform.modify_select(
            self.metadata,
            'user',
            select_obj,
            join_obj)

        select_obj = select_obj.select_from(join_obj)

        new_sql = str(select_obj.compile(dialect=postgresql.dialect()))

        expected = clean("""
          SELECT table1.foo_concept_id, table1.bar_concept_id,
          concept_1.concept_name AS foo_concept_name, concept_2.concept_name
          AS bar_concept_name
          {NL}FROM table1
          LEFT OUTER JOIN concept AS concept_1
              ON concept_1.concept_id = foo_concept_id
          LEFT OUTER JOIN concept AS concept_2
              ON concept_2.concept_id = bar_concept_id
        """)

        self.maxDiff = None
        self.assertEqual(expected, new_sql)

    def test_modify_metadata(self):

        metadata = ConceptNameTransform.modify_metadata(self.metadata)

        indexes = metadata.tables['table1'].indexes
        self.assertEqual(len(indexes), 2, 'Indexes created')

        for index in indexes:
            index_sql = str(CreateIndex(index).compile(
                dialect=postgresql.dialect()))
            if index.name == 'tab_fcn_81387a6132eed7f69b5_ix':
                expected = clean("""
                  CREATE INDEX tab_fcn_81387a6132eed7f69b5_ix
                    ON table1 (foo_concept_name)
                """)
                self.assertEqual(index_sql, expected)
            elif index.name == 'tab_bcn_7baa5e16ad1f8129b90_ix':
                expected = clean("""
                  CREATE INDEX tab_bcn_7baa5e16ad1f8129b90_ix
                    ON table1 (bar_concept_name)
                """)
                self.assertEqual(index_sql, expected)
            else:
                self.fail(
                    'Unexpected index encountered: {}'.format(index.name))


__author__ = 'cromo'

from csvutils import SimpleCSVGrid
from ofx import export as ofxexport
from qif import export as qifexport
from mappings import all_mappings

class Converter():
    def OpenFile(self, mapping_name, csv_file):
        """
            Takes a csv file and loads it's contents into the data table.
        """

        mapping = all_mappings[mapping_name]

        try:
            delimiter = mapping['_params']['delimiter']
        except:
            delimiter = ','
        try:
            skip_last = mapping['_params']['skip_last']
        except:
            skip_last = 0
        try:
            has_header = mapping['_params']['has_header']
        except:
            has_header = True
        self.grid_table = SimpleCSVGrid(csv_file, delimiter, skip_last, has_header)

    def ExportFiles(self, mapping_name, output_format, output_file):

        mapping = all_mappings[mapping_name][output_format]

        grid = self.grid_table

        if output_format == 'OFX':
            csv2ofx_export = ofxexport
        elif output_format == 'QIF':
            csv2ofx_export = qifexport
        else:
            raise Exception("Unhandled export format: %s" % format)

        csv2ofx_export(output_file, mapping, grid)


from .settings import *

env_file = str(PROJECT_ROOT.path('security/environ_local.env'))
environ.Env.read_env(str(env_file))

#DEBUG = env.bool('DEBUG')
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = ['localhost', '127.0.0.1', '[::1]', '*']
TEMPLATES[0]['OPTIONS']['debug'] = DEBUG

SECRET_KEY = SECRET_FILE

#Database
DATABASES = {
    'default': env.db("SQLITE_URL"),
}

'''DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_entropy',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
        'OPTIONS': {
            'sql_mode': 'traditional',
        }
    }
}'''

# CACHES = {
#     'default': env.cache('REDIS_URL')
# }

DJANGO_APPS = (

)
THIRD_PARTY_APPS = (
    # 'debug_toolbar',
    'django_extensions',
)
LOCAL_APPS = (

)
INSTALLED_APPS += DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS

MIDDLEWARE += (
    # 'debug_toolbar.middleware.DebugToolbarMiddleware',
)
DEBUG_TOOLBAR_PANELS = [
    # 'debug_toolbar.panels.versions.VersionsPanel',
    # 'debug_toolbar.panels.timer.TimerPanel',
    # 'debug_toolbar.panels.settings.SettingsPanel',
    # 'debug_toolbar.panels.headers.HeadersPanel',
    # 'debug_toolbar.panels.request.RequestPanel',
    # 'debug_toolbar.panels.sql.SQLPanel',
    # 'debug_toolbar.panels.staticfiles.StaticFilesPanel',
    # 'debug_toolbar.panels.templates.TemplatesPanel',
    # 'debug_toolbar.panels.cache.CachePanel',
    # 'debug_toolbar.panels.signals.SignalsPanel',
    # 'debug_toolbar.panels.logging.LoggingPanel',
    # 'debug_toolbar.panels.redirects.RedirectsPanel',
]
CONFIG_DEFAULTS = {
    'RESULTS_CACHE_SIZE': 3,
    'SHOW_COLLAPSED': True,
    'SQL_WARNING_THRESHOLD': 100,
}
INTERNAL_IPS = ['localhost', '127.0.0.1', '[::1]']
DEBUG_TOOLBAR_CONFIG = {
    'INTERCEPT_REDIRECTS': False,
}
DEBUG_TOOLBAR_PATCH_SETTINGS = True

# https://www.google.com/settings/security/lesssecureapps
EMAIL_BACKEND = env('DJANGO_EMAIL_BACKEND')
EMAIL_CONFIG = env.email_url('EMAIL_URL')
vars().update(EMAIL_CONFIG)

ADMIN_URL = env('ADMIN_URL')

SESSION_COOKIE_SECURE = False
SESSION_COOKIE_HTTPONLY = False
CSRF_COOKIE_SECURE = False

LOGGING['loggers'].update({
    '{{ project_name|lower }}': {
        'handlers': ['console'],
        'level': 'DEBUG',
        'propagate': True,
    },
    'django': {
        'handlers': ['console'],
        'level': 'DEBUG',
        'propagate': True,
    },
    'django.db.backends': {
        'handlers': ['django'],
        'level': 'DEBUG',
        'propagate': False,
    },
    'performance': {
        'handlers': ['console'],
        'level': 'INFO',
        'propagate': True,
    },
})


from fitness_output_retrieve import OutputDataRetriever
from simulator_info_retriever import InfoRetriever
from simulate import Simulate

class FitnessEvaluator:

    @classmethod
    def run(cls):
        detectorIdList= InfoRetriever.getDetectorList("road_map/e1.add.xml")
        return cls.evaluateFitness("road_map/e1output.xml", detectorIdList, "flow")

    @classmethod
    def evaluateFitness(cls, inputFileName, detectorIdList, targetAttrName):
        fitnessListForEachDetector = []
        summation = 0.0
        for detectorId in detectorIdList:
            fitnessListForEachDetector.append(OutputDataRetriever.getFlowFromOneDetector(inputFileName, detectorId, targetAttrName))
        for i in xrange(len(fitnessListForEachDetector)):
            summation += fitnessListForEachDetector[i]
        return summation

    @classmethod
    def getEvaluationResult(cls, inputTrafficLights):
        ind = Simulate()
        return ind


# -*- coding: utf-8 -*-
#
# Created on 9/1/15 by maersu
from core.utils import SYSTEMS, XLIFF, LOCALES, LOCALE_EN
from django.db import models
from django_extensions.db.models import TimeStampedModel
from django.db.models.signals import post_save
from django.dispatch import receiver
from openpyxl import Workbook, load_workbook
from openpyxl.writer.excel import save_virtual_workbook
from translations.models import Translation
from django.core.files.base import ContentFile
import logging
import json

logger = logging.getLogger(__name__)

class InputFile(TimeStampedModel):
    system = models.CharField(max_length=30, choices=SYSTEMS, default=XLIFF)
    locale = models.CharField(max_length=3, choices=LOCALES, default=LOCALE_EN)

    input_file = models.FileField(upload_to='uploads')

    input_processed = models.BooleanField(default=False)
    output_processed = models.BooleanField(default=False)

    output_de = models.FileField(upload_to='downloads', blank=True, null=True)
    output_en = models.FileField(upload_to='downloads', blank=True, null=True)
    output_fr = models.FileField(upload_to='downloads', blank=True, null=True)

    class Meta:
        ordering = ['-created']

    def __unicode__(self):
        return '%s' % self.input_file

class TranslationExport(TimeStampedModel):
    download_processed = models.BooleanField(default=False)
    upload_processed = models.BooleanField(default=False)

    download = models.FileField(upload_to='downloads', blank=True, null=True)
    upload = models.FileField(upload_to='uploads', blank=True, null=True)

    details = models.TextField(blank=True, null=True)

    class Meta:
        ordering = ['-created']

DEFAULT_LANGUAGE = 'de'
LANGUAGES = ['de', 'en'] # , 'fr'

# method for updating
@receiver(post_save, sender=TranslationExport, dispatch_uid="create_download")
def create_download(sender, instance, **kwargs):
    if kwargs.get('created'):

        wb = Workbook()
        ws = wb.active
        ws.append(['id'] + LANGUAGES)

        ids = []

        details = {}
        for l in LANGUAGES:
            details[l] = {'WordCount': 0, 'FuzzyCount': 0}

        for l in LANGUAGES:
            word_count = 0
            for t in Translation.objects.filter(**{'%s_fuzzy' % l: True}).order_by('id'):
                if t.id not in ids:
                    word_count += len(getattr(t, l).split())
                    ids.append(t.id)
                    cols = [t.id]
                    for l2 in LANGUAGES:
                        text = getattr(t, l2)
                        cols.append(text)
                        details[l2]['WordCount'] += len(text.split())
                        if getattr(t, '%s_fuzzy' % l2):
                            details[l2]['FuzzyCount'] += 1

                    ws.append(cols)

        instance.download.save('translations_%s_%s.xlsx' % ('-'.join(LANGUAGES), instance.id),
                               ContentFile(save_virtual_workbook(wb)))

        instance.details = json.dumps(
            details, sort_keys=True,
            indent=4, separators=(',', ': ')
        )
        instance.download_processed = True
        instance.save()

        logger.info('%s Translation exported to TranslationExport' % len(ids))

    elif not instance.upload_processed and instance.upload:

        ms = load_workbook(instance.upload)
        ws = ms.active

        first = True
        count = 0

        for row in ws.iter_rows():
            if first:
                first = False
                continue

            if row[0]:

                if row[1].value and row[2].value:
                    updated = Translation.objects.filter(id=row[0].value).update(
                        de=row[1].value,
                        de_fuzzy=False,
                        en=row[2].value,
                        en_fuzzy=False
                    )

                    if not updated:
                        logger.error('Translation with %s not found (%s)' % (row[0].value, row[1].value))

                    count += updated

                else:
                    logger.error('Translation for %s is missing' % row[0].value)

        instance.upload_processed = True
        instance.save()
        logger.info('%s Translation updated from TranslationExport' % updated)


from Generic_TriggerSource import *
import telnetlib
from pkg_resources import require

require("numpy")
import numpy as np
import warnings

class Agilent33220A_trigsrc(Generic_TrigSource):
    """Rigol3030DSG RFSigGen, Child of Generic_RFSigGen.

    This class is for communicating with the Agilent33220A over telnet. The specific API calls abstract the SCPI
    commands needed to speak with the instrument. Each of these calls is an override of the Generic_RFSigGen.

    Attributes:
        *Inherited from parent.
    """

    # Private Methods
    def _telnet_query(self, message):
        """Private method that will send a message over telnet to the Rigol3030 and return the reply

        Args:
            message (str): SCPI message to be sent to the Agilent33220A

        Returns:
            str: Reply message from the Rigol3030
        """
        self._telnet_write(message)
        return self._telnet_read()

    def _telnet_write(self, message):
        """Private method that will send a message over telnet to the Agilent33220A

        Args:
            message (str): SCPI message to be sent to the Agilent33220A

        Returns:

        """
        # Checks that the telnet message is a string
        if type(message) != str:
            raise TypeError

        self.tn.write(message + "\r\n")  # Writes a telnet message with termination characters

    def _telnet_read(self):
        """Private method that will read a telnet reply from the Agilent33220A

        Args:

        Returns:
            str: Reply message from the Agilent33220A
        """
        return self.tn.read_until("\n", self.timeout).rstrip('\n')  # Telnet reply, with termination chars removed

    # Constructor and Deconstructor

    def __init__(self, ipaddress, port=5024, timeout=1):
        """Initialises and opens the connection to the Agilent33220A over telnet and informs the user

        Args:
            ipaddress (str): The IP address of the Agilent33220A
            port (int/str): The port number for the messages to be sent on (default 5555)
            timeout (float): The timeout for telnet commands in seconds (default 1)

        Returns:

        """
        self.timeout = timeout  # timeout for the telnet comms
        self.tn = telnetlib.Telnet(ipaddress, port, self.timeout)  # connects to the telnet device
        self.DeviceID = self.get_device_id()  # gets the device of the telnet device, makes sure its the right one
        self.turn_off_RF()  # turn off the RF output

        print("Opened connection to Trigger source" + self.DeviceID)  # tells the user the device has been connected to

    def __del__(self):
        """Closes the telnet connection to the Agilent33220A
        """
        self.turn_off_RF()  # make sure the RF output is off
        self.tn.close()  # close the telnet link
        print("Closed connection to " + self.DeviceID)  # tell the user the telnet link has closed

    # API Calls
    def get_device_id(self):
        """Override method that will return the device ID.

        Uses the SCPI command "*IDN?" to get the device ID.

        Args:

        Returns:
            str: The device_id of the SigGen.
        """
        device_id = self._telnet_query("*IDN?")  # gets the device information
        test_string = "Welcome to Agilent's 33220A Waveform Generator"
        if device_id[0:len(test_string)] != test_string:  # checks it's the right device
            raise ValueError("Wrong hardware device connected")
        return device_id

    def set_up_trigger_pulse(self, freq):
        """Override method that will set up a pulse suitable for a trigger signal.

                Args:
                    freq (float)
                """
        if type(freq) != float and type(freq) != int \
                and np.float64 != np.dtype(freq) and np.int64 != np.dtype(freq):
            raise TypeError
        self._telnet_write("FUNC:PULS:DCYC 50")
        self._telnet_write(''.join(("APPL:PULS ", str(freq), ', 1, 0')))
        self.turn_on_RF()

    def turn_on_RF(self):
        """Override method that will turn on the RF device output.

        Uses SCPI command "OUTP ON" to turn on the device.

        Args:

        Returns:
            bool: Returns True if the output is enabled, False if it is not.
        """
        self._telnet_write("OUTP ON")  # turns on the output
        return self.get_output_state()

    def turn_off_RF(self):
        """Override method that will turn off the RF device output.

        Uses SCPI command "OUTP OFF" to turn off the device.

        Args:

        Returns:
            bool: Returns True if the output is enabled, False if it is not.
        """
        self._telnet_write("OUTP OFF")  # turns off the output
        return self.get_output_state()

    def get_output_state(self):
        """Override method that will get the current output state.

        This method send the SCPI command "OUTP?" to request the output state from
        the Rigol3030.

        Args:

        Returns:
            bool: Returns True if the output is enabled, False if it is not.
        """
        # checks output state
        if self._telnet_query("OUTP?") == "1":
            self.Output_State = True  # output must be on
            return True
        else:
            self.Output_State = False  # output must be off
            return False


# encoding: utf-8

__all__ = ["SevenIO", "fsv"]

import re
from py7zlib import Archive7z
from io import TextIOWrapper, BufferedReader, BytesIO, FileIO
from mmap import mmap, MAP_PRIVATE, PROT_READ

def fsv(fmt, line):
    pos = 0
    for item in fmt:
        if item > 0:
            yield line[pos:pos+item]
            pos += item
        else:
            pos -= item # trik, jest ujemne a przesuwamy się o jego abs() do przodu

# Wrapper na StringIO+plik .7z z pojedynczym plikiem w środku
class SevenIO(object):
    def __init__(self, path_or_file, archive=True):
        if archive:
            if hasattr(path_or_file, 'fileno'):
                self.map = mmap(path_or_file.fileno(), 0, MAP_PRIVATE, PROT_READ)
            else:
                fp = open(path_or_file, 'r')
                self.map = mmap(fp.fileno(), 0, MAP_PRIVATE, PROT_READ)
            self.archive = Archive7z(self.map)
            raw = self.archive.files[0].read()
            self.file = TextIOWrapper(BufferedReader(BytesIO(raw)),
                                      encoding='CP1250', newline='\r\n')
        else:
            if hasattr(path_or_file, 'fileno'):
                self.fp = path_or_file
            else:
                self.fp = open(path_or_file, 'r')
            fd = self.fp.fileno()
            self.file = TextIOWrapper(BufferedReader(FileIO(fd)),
                                      encoding='CP1250', newline='\r\n')
        self._index_sections()

    def readline(self):
        return self.file.next()

    # zwróc zawartość danej sekcji
    # niestety jako string, py7zlib nie umie streamować archivefile
    def read_section(self, section):
        start, end = self.sections[section]
        self.file.seek(start)
        return self.file.read(end - start)

    # Nawiguj do danego "adresu" w pliku
    # Adres to minimum jeden argument ze stringiem do zmatchowania
    # pierwszy musi być zawsze nazwą sekcji,
    # kolejne będą wyszukiwane z rosnącym wcięciem, według
    # formatu pliku
    # Zwróć całą linię która dopasowała ostatni adres.
    def seek_to_section(self, *sections):
        main, rest = sections[0], sections[1:]
        start, _end = self.sections[main]
        self.file.seek(start)
        if rest:
            for level, sub in enumerate(rest, 1):
                pat = ''.join(['   ' * level, sub])
                line = self.skip_until(pat)

            return line
        else:
            # wada: jeśli seekujesz tylko do toplevelowej sekcji,
            # nie dostaniesz jej pierwszej linii z powrotem.
            return None

    # pomijaj linie w pliku aż wczytana będzie zaczynać się od bol
    # zostaw kursor w pliku na kolejnej.
    def skip_until(self, bol):
        while self.file.readable():
            try:
                line = self.file.next()
            except StopIteration:
                raise RuntimeError('end of file reached')
            if line.startswith(bol):
                return line

    def _index_sections(self):
        self.sections = {}
        self.file.seek(0)
        chunksize = 1<<20
        rx_start = re.compile(r'^\*([A-Z]{2})\s+(\d+)\r\n', re.M)
        rx_end = re.compile(r'^#([A-Z]{2})\r\n', re.M)
        blob = self.file.read()
        for match in rx_start.finditer(blob):
            self.sections[match.group(1)] = match.end(0)

        for match in rx_end.finditer(blob):
            self.sections[match.group(1)] = self.sections[match.group(1)], match.start(0)

if __name__ == "__main__":
    import sys
    from IPython import embed
    s = SevenIO(sys.argv[1])
    embed()


from django.shortcuts import render
from django.http import HttpResponse

import urllib2
import urllib
import base64
import json
import unicodedata

def _decode_list(data):
    rv = []
    for item in data:
        if isinstance(item, unicode):
            item = item.encode('utf-8')
        elif isinstance(item, list):
            item = _decode_list(item)
        elif isinstance(item, dict):
            item = _decode_dict(item)
        rv.append(item)
    return rv

def _decode_dict(data):
    rv = {}
    for key, value in data.iteritems():
        if isinstance(key, unicode):
            key = key.encode('utf-8')
        if isinstance(value, unicode):
            value = value.encode('utf-8')
        elif isinstance(value, list):
            value = _decode_list(value)
        elif isinstance(value, dict):
            value = _decode_dict(value)
        rv[key] = value
    return rv

#def index(request):
#    context = {'xyz': '123'}
#    return render(request, 'trip/index.html', context)

def index2(request):
    if request.method == 'GET':
        if 'a' in request.GET :
            context = {'xyz': request.GET['a']}
        else :
            context = {'xyz': 'no input'}
        return render(request, 'trip/index.html', context)
def gmap(request):
    return render(request, 'trip/gmap.html')
def hello_id(request, question_id):
    return HttpResponse("You're looking at id : %s." % question_id)

def index(request):
    if (('src' in request.GET) & ('dst' in request.GET)):

        isValid=checkValidAirportCode(request.GET['src'],request.GET['dst'])

        if(False==isValid):
            context = {'src_airport': request.GET['src'], 'dst_airport': request.GET['dst'] }
            return render(request, 'trip/notSupported.html', context)

        x=trip_details(request)

        ss = x.content
        context = {'lowest': ss}
        return render(request, 'trip/lowest.html', context)

    else:
        context = {'src_airport': 'empty', 'dst_airport': 'empty' }
        return render(request, 'trip/notSupported.html', context)

def detail(request, question_id):
    return HttpResponse("You're looking at question %s." % question_id)

def trip_details(request):
    if request.method == 'GET':
        a=request.GET['src']
        src=a.encode('utf-8')
        x=str(type(src))
        b=request.GET['dst']
        dst=b.encode('utf-8')
    token="Shared/IDL:IceSess\/SessMgr:1\.0.IDL/Common/!ICESMS\/ACPCRTD!ICESMSLB\/CRT.LB!-0123456789012345678!123456!0!ABCDEFGHIJKLM!E2E-1"
    lstay="7"

    query_args = { 'origin':src, 'destination':dst,'lengthofstay': lstay,'pointofsalecountry':'US'} # you have to pass in a dictionary
    encoded_args = urllib.urlencode(query_args)

    #print 'Encoded:', encoded_args
    l = []
    l.append('https://api.test.sabre.com/v1/shop/flights/fares?')
    l.append(encoded_args)
    url=''.join(l)
    request = urllib2.Request(url)
    request.add_header('Authorization', 'Bearer %s' % token)
    request.add_header("Accept", "application/json")

    response=urllib2.urlopen(request).read()
    result=json.loads(response, object_hook=_decode_dict)
    c1=result["FareInfo"][10]["LowestFare"]
    c2=result["FareInfo"][10]["LowestNonStopFare"]
    return HttpResponse(c1)

def trip_options(request):
    return

#Check if the airport code is valid
def checkValidAirportCode(src,dst):
    valid_aiports= ['FLL', 'SFO' ,'LAX','JFK','LGA','ORD']
    src_is_in_list=src in valid_aiports
    if(False==src_is_in_list):
        return False
    dst_is_in_list=dst in valid_aiports
    if(False==dst_is_in_list):
        return False
    return True


from django.conf.urls import url
from django.views.generic import TemplateView
from views import templates_list,templates_change,templates_preview,essential_tags,templates_delete

urlpatterns = [
    url(r'templates/$', templates_list, name="templates_list"),
    url(r'^templates/essential-tags/$',essential_tags, name="essential-tags"),
    url(r'templates/add/$', templates_change, name="templates_change"),
    url(r'templates/change/(?P<slug>.+)/$', templates_change, name="templates_change"),
    url(r'templates/preview/(?P<slug>.+)/$', templates_preview, name="templates_preview"),
    url(r'templates/delete/(?P<slug>.+)/$', templates_delete, name="templates_delete"),

]


# using a for loop, make a program that will multiply every number
# in the my_list variable by 3 and print the result
# Note: you cannot change the variable my_list. You can do
# everything else though

my_list = [ 2, 4, 6, 8]

for k in my_list:
    print(k*3)


import CoolProp.CoolProp as CP
import CoolProp
import textwrap
import os
from parse_bib import BibTeXerClass

def PropertyConsistency(Fluid):

    Tmax = CP.Props(Fluid,"Tcrit")-1 if Fluid == 'SES36' else CP.Props(Fluid,"Tcrit")-0.01

    return textwrap.dedent(
    """
    Check of p,h and p,s as inputs (X: Failure .: Success)
    =================================================================
    .. plot::

        from CoolProp.Plots.Plots import Ph,Ps
        from CoolProp.CoolProp import Props
        from matplotlib import pyplot as plt
        import numpy as np

        Ref = "{Fluid:s}"
        fig = plt.figure(figsize=(10,5))
        ax1 = fig.add_subplot(121)
        ax2 = fig.add_subplot(122)

        Tmin = Props(Ref,'Tmin')+3
        pmin = Props('P','T',Tmin,'Q',0,Ref)
        pmax = Props(Ref,'pcrit')*2
        hmin = Props('H','T',Tmin,'Q',0,Ref)
        hmax = 2*Props('H','T',Props(Ref,'Tcrit')-1,'Q',1,Ref)-hmin
        smin = Props('S','T',Tmin,'Q',0,Ref)
        smax = 2*Props('S','T',Props(Ref,'Tcrit')-1,'Q',1,Ref)-smin

        Ph(Ref, axis = ax1, Tmin = Tmin, Tmax = {Tmax:f})
        Ps(Ref, axis = ax2, Tmin = Tmin, Tmax = {Tmax:f})

        for p in np.linspace(pmin,pmax,10):
            for h in np.linspace(hmin,hmax):
                _bad = False
                try:
                    T = Props('T','H',h,'P',p,Ref)
                    rho = Props('D','H',h,'P',p,Ref)
                    hEOS = Props('H','T',T,'D',rho,Ref)
                except ValueError:
                    _bad = True
                if _bad or abs(hEOS/h-1)>1e-6:
                    ax1.plot(h,p,'x',ms = 10)
                else:
                    ax1.plot(h,p,'k.', ms = 1)

        for p in np.linspace(pmin,pmax,10):
            for s in np.linspace(smin,smax):
                _bad = False
                try:
                    T = Props('T','S',s,'P',p,Ref)
                    rho = Props('D','S',s,'P',p,Ref)
                    sEOS = Props('S','T',T,'D',rho,Ref)
                except ValueError:
                    _bad = True
                if _bad or abs(sEOS/s-1)>1e-6:
                    ax2.plot(s,p,'x',ms = 10)
                else:
                    ax2.plot(s,p,'k.', ms = 1)

        plt.tight_layout()
    """.format(Fluid=Fluid,Tmax = Tmax))

def CriticalIsotherm(Fluid):

    return textwrap.dedent(
    """
    Along the critical isotherm where T=T\ :sub:`c`
    ================================================
    .. plot::

        Fluid = "{Fluid:s}"
        RPFluid = "{RPFluid:s}"

        #Critical isotherm
        from CoolProp.CoolProp import Props
        from numpy import linspace,array,abs
        import matplotlib.pyplot as plt

        Tc = Props(Fluid,'Tcrit')
        rhoc = Props(Fluid,'rhocrit')
        rhov = linspace(1e-12,2*rhoc)

        #All the CoolProp calculations
        p = array([Props('P','T',Tc,'D',D,Fluid) for D in rhov])
        rho = array([Props('D','T',Tc,'D',D,Fluid) for D in rhov])
        cp = array([Props('C','T',Tc,'D',D,Fluid) for D in rhov])
        cv = array([Props('O','T',Tc,'D',D,Fluid) for D in rhov])
        h0 = Props('H','T',0.95*Tc,'Q',1,Fluid)
        h = array([Props('H','T',Tc,'D',D,Fluid)-h0 for D in rhov])
        s0 = Props('S','T',0.95*Tc,'Q',1,Fluid)
        s = array([Props('S','T',Tc,'D',D,Fluid)-s0 for D in rhov])
        visc = array([Props('V','T',Tc,'D',D,Fluid) for D in rhov])
        cond = array([Props('L','T',Tc,'D',D,Fluid) for D in rhov])

        Rp = array([Props('P','T',Tc,'D',D,RPFluid) for D in rhov])
        Rrho = array([Props('D','T',Tc,'D',D,RPFluid) for D in rhov])
        Rcp = array([Props('C','T',Tc,'D',D,RPFluid) for D in rhov])
        Rcv = array([Props('O','T',Tc,'D',D,RPFluid) for D in rhov])
        Rh0 = Props('H','T',0.95*Tc,'Q',1,RPFluid)
        Rh = array([Props('H','T',Tc,'D',D,RPFluid)-Rh0 for D in rhov])
        Rs0 = Props('S','T',0.95*Tc,'Q',1,RPFluid)
        Rs = array([Props('S','T',Tc,'D',D,RPFluid)-Rs0 for D in rhov])
        Rvisc = array([Props('V','T',Tc,'D',D,RPFluid) for D in rhov])
        Rcond = array([Props('L','T',Tc,'D',D,RPFluid) for D in rhov])

        fig = plt.figure()

        ax = fig.add_axes((0.15,0.15,0.8,0.8))
        ax.semilogy(rhov/rhoc,abs(p/Rp-1)*100,'o',label='Pressure')
        ax.semilogy(rhov/rhoc,abs(cp/Rcp-1)*100,'o',label='Specific heat (cp)')
        ax.semilogy(rhov/rhoc,abs(cv/Rcv-1)*100,'o',label='Specific heat (cv)')
        ax.semilogy(rhov/rhoc,abs(h/Rh-1)*100,'o',label='Enthalpy')
        ax.semilogy(rhov/rhoc,abs(s/Rs-1)*100,'o',label='Entropy')
        ax.semilogy(rhov/rhoc,abs(visc/Rvisc-1)*100,'^',label='Viscosity')
        ax.semilogy(rhov/rhoc,abs(cond/Rcond-1)*100,'s',label='Conductivity')
        ax.set_ylim(1e-16,100)
        ax.set_title('Critical isotherm Deviations from REFPROP 9.1')
        ax.set_xlabel(r'Reduced density $\\rho/\\rho_c$')
        ax.set_ylabel('Absolute deviation [%]')
        ax.legend(numpoints=1,loc='best')
        plt.show()
    """.format(Fluid=Fluid,RPFluid = 'REFPROP-'+CP.get_REFPROPname(Fluid)))

def SatVaporParity(Fluid):

    return textwrap.dedent(
    """
    Saturated Vapor Deviations
    ==========================

    .. plot::

        Fluid = "{Fluid:s}"
        RPFluid = "{RPFluid:s}"

        #Saturated Vapor
        from CoolProp.CoolProp import Props
        from numpy import linspace,array,abs
        import matplotlib.pyplot as plt

        Tt = Props(Fluid,'Tmin')
        Tc = Props(Fluid,'Tcrit')
        Tv = linspace(Tt+0.01,0.95*Tc,20)

        #All the CoolProp calculations
        p = array([Props('P','T',T,'Q',1,Fluid) for T in Tv])
        rho = array([Props('D','T',T,'Q',1,Fluid) for T in Tv])
        cp = array([Props('C','T',T,'Q',1,Fluid) for T in Tv])
        cv = array([Props('O','T',T,'Q',1,Fluid) for T in Tv])
        h0 = Props('H','T',(Tt+Tc)/2.0,'Q',1,Fluid)
        h = array([Props('H','T',T,'Q',1,Fluid)-h0 for T in Tv])
        s0 = Props('S','T',(Tt+Tc)/2.0,'Q',1,Fluid)
        s = array([Props('S','T',T,'Q',1,Fluid)-s0 for T in Tv])
        sigma = array([Props('I','T',T,'Q',1,Fluid) for T in Tv])
        visc = array([Props('V','T',T,'Q',1,Fluid) for T in Tv])
        cond = array([Props('L','T',T,'Q',1,Fluid) for T in Tv])

        Rp = array([Props('P','T',T,'Q',1,RPFluid) for T in Tv])
        Rrho = array([Props('D','T',T,'Q',1,RPFluid) for T in Tv])
        Rcp = array([Props('C','T',T,'Q',1,RPFluid) for T in Tv])
        Rcv = array([Props('O','T',T,'Q',1,RPFluid) for T in Tv])
        Rh0 = Props('H','T',(Tt+Tc)/2.0,'Q',1,RPFluid)
        Rh = array([Props('H','T',T,'Q',1,RPFluid)-Rh0 for T in Tv])
        Rs0 = Props('S','T',(Tt+Tc)/2.0,'Q',1,RPFluid)
        Rs = array([Props('S','T',T,'Q',1,RPFluid)-Rs0 for T in Tv])
        Rsigma = array([Props('I','T',T,'Q',1,RPFluid) for T in Tv])
        Rvisc = array([Props('V','T',T,'Q',1,RPFluid) for T in Tv])
        Rcond = array([Props('L','T',T,'Q',1,RPFluid) for T in Tv])

        fig = plt.figure()

        ax = fig.add_axes((0.15,0.15,0.8,0.8))
        ax.semilogy(Tv/Tc,abs(p/Rp-1)*100,'o',label='Pressure')
        ax.semilogy(Tv/Tc,abs(rho/Rrho-1)*100,'o',label='Density')
        ax.semilogy(Tv/Tc,abs(cp/Rcp-1)*100,'o',label='Specific heat (cp)')
        ax.semilogy(Tv/Tc,abs(cv/Rcv-1)*100,'o',label='Specific heat (cv)')
        ax.semilogy(Tv/Tc,abs(h/Rh-1)*100,'o',label='Enthalpy')
        ax.semilogy(Tv/Tc,abs(s/Rs-1)*100,'o',label='Entropy')
        ax.semilogy(Tv/Tc,abs(visc/Rvisc-1)*100,'^',label='Viscosity')
        ax.semilogy(Tv/Tc,abs(cond/Rcond-1)*100,'s',label='Conductivity')
        ax.semilogy(Tv/Tc,abs(sigma/Rsigma-1)*100,'p',label='Surface tension')
        ax.set_ylim(1e-16,100)
        ax.set_title('Saturated Vapor Deviations from REFPROP 9.1')
        ax.set_xlabel('Reduced temperature T/Tc')
        ax.set_ylabel('Absolute deviation [%]')
        ax.legend(numpoints=1,loc='best')
        plt.show()
    """.format(Fluid=Fluid,RPFluid = 'REFPROP-'+CP.get_REFPROPname(Fluid)))

def SatLiquidParity(Fluid):

    return textwrap.dedent(
    """
    Saturated Liquid Deviations
    ===========================

    .. plot::

        Fluid = "{Fluid:s}"
        RPFluid = "{RPFluid:s}"

        #Saturated Liquid
        from CoolProp.CoolProp import Props
        from numpy import linspace,array,abs
        import matplotlib.pyplot as plt

        Tt = Props(Fluid,'Tmin')
        Tc = Props(Fluid,'Tcrit')
        Tv = linspace(Tt+0.01,0.95*Tc,20)

        #All the CoolProp calculations
        p = array([Props('P','T',T,'Q',0,Fluid) for T in Tv])
        rho = array([Props('D','T',T,'Q',0,Fluid) for T in Tv])
        cp = array([Props('C','T',T,'Q',0,Fluid) for T in Tv])
        cv = array([Props('O','T',T,'Q',0,Fluid) for T in Tv])
        h0 = Props('H','T',(Tt+Tc)/2.0,'Q',0,Fluid)
        h = array([Props('H','T',T,'Q',0,Fluid)-h0 for T in Tv])
        s0 = Props('S','T',(Tt+Tc)/2.0,'Q',0,Fluid)
        s = array([Props('S','T',T,'Q',0,Fluid)-s0 for T in Tv])
        visc = array([Props('V','T',T,'Q',0,Fluid) for T in Tv])
        cond = array([Props('L','T',T,'Q',0,Fluid) for T in Tv])
        sigma = array([Props('I','T',T,'Q',0,Fluid) for T in Tv])

        Rp = array([Props('P','T',T,'Q',0,RPFluid) for T in Tv])
        Rrho = array([Props('D','T',T,'Q',0,RPFluid) for T in Tv])
        Rcp = array([Props('C','T',T,'Q',0,RPFluid) for T in Tv])
        Rcv = array([Props('O','T',T,'Q',0,RPFluid) for T in Tv])
        Rh0 = Props('H','T',(Tt+Tc)/2.0,'Q',0,RPFluid)
        Rh = array([Props('H','T',T,'Q',0,RPFluid)-Rh0 for T in Tv])
        Rs0 = Props('S','T',(Tt+Tc)/2.0,'Q',0,RPFluid)
        Rs = array([Props('S','T',T,'Q',0,RPFluid)-Rs0 for T in Tv])
        Rvisc = array([Props('V','T',T,'Q',0,RPFluid) for T in Tv])
        Rcond = array([Props('L','T',T,'Q',0,RPFluid) for T in Tv])
        Rsigma = array([Props('I','T',T,'Q',0,RPFluid) for T in Tv])

        fig = plt.figure()

        ax = fig.add_axes((0.15,0.15,0.8,0.8))
        ax.semilogy(Tv/Tc,abs(p/Rp-1)*100,'o',label='Pressure')
        ax.semilogy(Tv/Tc,abs(rho/Rrho-1)*100,'o',label='Density')
        ax.semilogy(Tv/Tc,abs(cp/Rcp-1)*100,'o',label='Specific heat (cp)')
        ax.semilogy(Tv/Tc,abs(cv/Rcv-1)*100,'o',label='Specific heat (cv)')
        ax.semilogy(Tv/Tc,abs(h/Rh-1)*100,'o',label='Enthalpy')
        ax.semilogy(Tv/Tc,abs(s/Rs-1)*100,'o',label='Entropy')
        ax.semilogy(Tv/Tc,abs(visc/Rvisc-1)*100,'^',label='Viscosity')
        ax.semilogy(Tv/Tc,abs(cond/Rcond-1)*100,'s',label='Conductivity')
        ax.semilogy(Tv/Tc,abs(sigma/Rsigma-1)*100,'p',label='Surface tension')
        ax.set_ylim(1e-16,100)
        ax.set_title('Saturated Liquid Deviations from REFPROP 9.1')
        ax.set_xlabel('Reduced temperature T/Tc')
        ax.set_ylabel('Absolute deviation [%]')
        ax.legend(numpoints=1,loc='best')
        plt.show()
    """.format(Fluid=Fluid,RPFluid = 'REFPROP-'+CP.get_REFPROPname(Fluid)))

def params_table(Fluid):
    params = dict(mm = CP.Props(Fluid,'molemass'),
                  Tt = CP.Props(Fluid,'Ttriple'),
                  pt = CP.Props(Fluid,'ptriple'),
                  Tmin = CP.Props(Fluid,'Tmin'),
                  CAS = CP.get_CAS_code(Fluid),
                  ASHRAE = CP.get_ASHRAE34(Fluid)
                  )

    return textwrap.dedent(
    """
    Fluid Data
    ==========

    Fluid Parameters

    =========================  ==============================
    Mole Mass [kg/kmol]        {mm:0.5f}
    Triple Point Temp. [K]     {Tt:0.3f}
    Triple Point Press. [kPa]  {pt:0.10g}
    Minimum temperature [K]    {Tmin:0.3f}
    CAS number                 {CAS:s}
    ASHRAE classification      {ASHRAE:s}
    =========================  ==============================
    """.format(**params))

def critical_table(Fluid):
    params = dict(Tc = CP.Props(Fluid,'Tcrit'),
                  rhoc = CP.Props(Fluid,'rhocrit'),
                  pc = CP.Props(Fluid,'pcrit'),
                  )

    return textwrap.dedent(
    """
    Critical Parameters

    ==============================  ==============================
    Temperature [K]                 {Tc:0.3f}
    Density [kg/m\ :sup:`3`\ ]      {rhoc:0.6f}
    Pressure [kPa]                  {pc:0.5f}
    ==============================  ==============================

    """.format(**params))

def buildrst(Fluid):
    pass

def index_file(Fluids):
    Fluids=[Fluid+'.rst' for Fluid in Fluids]
    FluidList = '\n    '.join(Fluids)
    return textwrap.dedent(
"""
.. toctree::
    :maxdepth: 1

    {:s}
""".format(FluidList))

def fluid_header(Fluid):
    aliases = CP.get_aliases(str(Fluid))
    aliases = ', '.join(['``'+a.strip()+'``' for a in aliases])

    BTC = BibTeXerClass()

    EOSkey = CP.get_BibTeXKey(Fluid, "EOS")
    CP0key = CP.get_BibTeXKey(Fluid, "CP0")
    SURFACE_TENSIONkey = CP.get_BibTeXKey(Fluid, "SURFACE_TENSION")
    VISCOSITYkey = CP.get_BibTeXKey(Fluid, "VISCOSITY")
    CONDUCTIVITYkey = CP.get_BibTeXKey(Fluid, "CONDUCTIVITY")
    ECS_LENNARD_JONESkey = CP.get_BibTeXKey(Fluid, "ECS_LENNARD_JONES")
    ECS_FITSkey = CP.get_BibTeXKey(Fluid, "ECS_FITS")

    BibInfo = ''
    if EOSkey:
        BibInfo += '**Equation of State**: ' + BTC.entry2rst(EOSkey) + '\n\n'
    if CP0key:
        BibInfo += '**Ideal-Gas Specific Heat**: ' + BTC.entry2rst(CP0key) + '\n\n'
    if SURFACE_TENSIONkey:
        BibInfo += '**Surface Tension**: ' + BTC.entry2rst(SURFACE_TENSIONkey) + '\n\n'
    if VISCOSITYkey:
        BibInfo += '**Viscosity**: ' + BTC.entry2rst(VISCOSITYkey) + '\n\n'
    if CONDUCTIVITYkey:
        BibInfo += '**Conductivity**: ' + BTC.entry2rst(CONDUCTIVITYkey) + '\n\n'
    if ECS_LENNARD_JONESkey:
        BibInfo += '**Lennard-Jones Parameters for ECS**: ' + BTC.entry2rst(ECS_LENNARD_JONESkey) + '\n\n'
    if ECS_FITSkey:
        BibInfo += '**ECS Correction Fit**: ' + BTC.entry2rst(ECS_FITSkey) + '\n\n'

    return textwrap.dedent(
"""
********************
{Fluid:s}
********************

Aliases
================================================================================
{Aliases:s}

Bibliographic Information
=========================
{Reference:s}
""".format(Fluid=Fluid,
           Aliases = aliases,
           Reference = BibInfo,
           )
           )

fp=open(os.path.join('Fluids','FluidInformation.rst'),'w')
fp.write('###########################\nFluid Information\n###########################\n')
fp.write(
"""
CoolProp Fluid Information
==========================
.. toctree::
    :maxdepth: 1

    PseudoPureFluids.rst
    PureFluids.rst

There are 4 basic classes of fluids that can be used in CoolProp, an example is provided for each one.

Pure Fluids, Pseudo-Pure Fluids
-------------------------------
All the fluids listed in Pure-Fluids and Pseudo-Pure-Fluids sections above can be used.  To use one of these fluids, do something like

.. ipython::

    In [1]: from CoolProp.CoolProp import Props

    #Density of dry air at 1 atm. and 25C
    In [1]: Props('D','T',298,'P',101.325,'Air')

You can also use any of the aliases of the fluids that are the listed on the fluid page.  For instance, R717 is the refrigerant number for ammonia

.. ipython::

    In [1]: from CoolProp.CoolProp import Props

    #Density of saturated ammonia vapor at 1 atm.
    In [1]: Props('D','Q',1,'P',101.325,'R717')

    #Density of saturated ammonia vapor at 1 atm.
    In [1]: Props('D','Q',1,'P',101.325,'Ammonia')

REFPROP Fluids and Mixtures
---------------------------
If you are on Windows and have REFPROP installed, you can use it with CoolProp.  REFPROP needs to be installed in c:\\\\Program Files\\\\REFPROP.  If it is somewhere else, just copy it to this location.

It is also possible to use REFPROP on Linux.  Please follow the instructions from https://github.com/jowr/librefprop.so to install the library from Fortran sources.  Additionally, you also need to copy the fluid and mixture files to /opt/refprop.

All the pure fluids in REFPROP are used just like the CoolProp fluids except that "REFPROP-" is added at the beginning of the fluid name.  You can use any fluid that is included in REFPROP, but you must use the REFPROP fluid file name.  For CoolProp Fluids, you can use the ``get_REFPROPName()`` function to get the REFPROP name for the fluid.

.. ipython::

    In [1]: from CoolProp.CoolProp import Props

    #Saturated isobutane vapor density at 1 atmosphere
    In [1]: Props('D','Q',1,'P',101.325,'REFPROP-ISOBUTAN')

You can also use mixtures in REFPROP, there is a special format for the fluid name.  The fluid name is set up like this: ``"REFPROP-MIX:R32[0.697615]&R125[0.302385]"`` -  this is R410A.  The numbers within the brackets are the mole fractions of the components.  They must add up to 1.0

.. ipython::

    In [1]: from CoolProp.CoolProp import Props

    #Saturated R410 vapor density at 1 atmosphere using the mixture properties
    In [1]: Props('D','Q',1,'P',101.325,'REFPROP-MIX:R32[0.697615]&R125[0.302385]')

Incompressible Liquids
----------------------
There is also a selection of incompressible liquids implemented.  These only allow for calls with
temperature and pressure as input and provide only a subset of thermophysical properties, namely:
density, heat capacity, internal energy, enthalpy, entropy, viscosity and thermal conductivity.
Hence, the available output keys for the ``Props`` function are: "D", "C", "U", "H", "S", "V", "L",
"Tmin", Tmax" and "Psat". An internal iteration allows us to use enthalpy and pressure as inputs,
but be aware of the reduced computational efficiency.

.. ipython::

    In [1]: from CoolProp.CoolProp import Props

    #Density of HFE-7100 at 300 K and 1 atm.
    In [1]: Props('D','T',300,'P',101.325,'HFE')

For refrigeration applications, 8 fluids were implemented from Aake Melinder "Properties of
Secondary Working Fluids for Indirect Systems" published in 2010 by IIR and coefficients are
obtained from a fit between -80 and +100 degrees Celsius.

==========================   ===================================================
Fluid Name                   Description
==========================   ===================================================
``DEB``                      Diethyl Benzene
``HCM``                      Hydrocarbon Mixture (Therminol D12 Solutia)
``HFE``                      Hydrofluoroether HFE-7100
``PMS1``                     Polydimethylsiloxan 1.
``PMS2``                     Polydimethylsiloxan 2.
``SAB``                      Synthetic alkyl benzene
``HCB``                      Hydrocarbon blend (Dynalene MV)
``TCO``                      Terpene from citrus oils
==========================   ===================================================

Some additional secondary cooling fluids are based on data compiled by Morten Juel Skovrup in
his `SecCool software <http://en.ipu.dk/Indhold/refrigeration-and-energy-technology/seccool.aspx>`_
provided by his employer `IPU <http://en.ipu.dk>`_. Fits have been made according to the manufacturer
data stored in the sodtware. The Aspen Temper fluids are a blend of potassium formate and sodiumpropionate
and the Zitrec S group consists mainly of potassium acetate and potassium formate.

==========================   ===================================================
Fluid Name                   Description
==========================   ===================================================
``AS10``                     Aspen Temper -10 (-10 to +27.5 C)
``AS20``                     Aspen Temper -20 (-20 to +27.5 C)
``AS30``                     Aspen Temper -30 (-30 to +27.5 C)
``AS40``                     Aspen Temper -40 (-40 to +27.5 C)
``AS55``                     Aspen Temper -55 (-55 to +27.5 C)
``ZS10``                     Zitrec S -10 (-10 to +85 C)
``ZS25``                     Zitrec S -25 (-25 to +85 C)
``ZS40``                     Zitrec S -40 (-40 to +85 C)
``ZS45``                     Zitrec S -45 (-45 to +85 C)
``ZS55``                     Zitrec S -55 (-55 to +85 C)
==========================   ===================================================

There are also a few high temperature heat transfer fluids with individual temperature ranges. Please
refer to the file IncompLiquid.h for a complete overview. For these fluids, information from commercial
data sheets was used to obtain coefficients.

==========================   ===================================================
Fluid Name                   Description
==========================   ===================================================
``TD12``                     Therminol D12 (-85 to +230 C)
``TVP1``                     Therminol VP-1 (+12 to +397 C)
``T72``                      Therminol 72 (-10 to +380 C)
``T66``                      Therminol 66 (0 to +345 C)
``DowJ``                     Dowtherm J (-80 to +345 C)
``DowQ``                     Dowtherm Q (-35 to +360 C)
``TX22``                     Texatherm 22 (0 to +350 C)
``NaK``                      Nitrate Salt Blend (+300 to +600 C)
``XLT``                      Syltherm XLT (-100 to +260 C)
``HC10``                     Dynalene HC-10 (-10 to +218 C)
``HC20``                     Dynalene HC-20 (-20 to +210 C)
``HC30``                     Dynalene HC-30 (-30 to +210 C)
``HC40``                     Dynalene HC-40 (-40 to +200 C)
``HC50``                     Dynalene HC-50 (-50 to +210 C)
==========================   ===================================================

All fluids are implemented with polynomials for density and heat capacity with typically 4 coefficients
and hence a third order polynomial. Thermal conductivity is a second order polynomial and viscosity and
vapour pressure are exponential functions.

.. math::

    \\rho    &= \\sum_{i=0}^n C_{\\rho}[i] \\cdot T^i \\\\
    c        &= \\sum_{i=0}^n C_{c}[i] \\cdot T^i \\\\
    u        &= \\int_{0}^{1} c\\left( T \\right) dT
              = \\sum_{i=0}^n \\frac{1}{i+1} \\cdot C_{c}[i]
                \\cdot \\left( T_1^{i+1} - T_0^{i+1} \\right) \\\\
    s        &= \\int_{0}^{1} \\frac{c\\left( T \\right)}{T} dT
              = C_{c}[0] \\cdot \\ln\\left(\\frac{T_1}{T_0}\\right)
                + \\sum_{i=0}^{n-1} \\frac{1}{i+1} \\cdot C_{c}[i+1]
                \\cdot \\left( T_1^{i+1} - T_0^{i+1} \\right) \\\\
    \\lambda &= \\sum_{i=0}^n C_{\\lambda}[i] \\cdot T^i \\\\
    \\mu     &= \\exp\\left( \\frac{C_{\\mu}[0]}{T+C_{\\mu}[1]} - C_{\\mu}[2] \\right) \\\\
    p_{sat}  &= \\exp\\left( \\frac{C_{sat}[0]}{T+C_{sat}[1]} - C_{sat}[2] \\right) \\\\

Brines and Solutions
--------------------
All the brines and solutions can be accessed through the Props function. To use them, the fluid name
is something like ``"MEG-20%"`` which is a 20% by mass ethylene glycol solution. Note that these fluids
have an arbitrary reference state: Be careful with enthalpy and entropy calculations. Again, only
temperature and pressure inputs are supported directly to calculate the same subset of thermophysical
properties as above , namely: density, heat capacity, internal energy, enthalpy, entropy, viscosity
and thermal conductivity. Hence, the available output keys for the ``Props`` function are: "D", "C",
"U", "H", "S", "V", "L", "Tmin", Tmax" and "Tfreeze". An internal iteration allows us to use enthalpy
and pressure as inputs, but be aware of the reduced computational efficiency.

.. ipython::

    In [1]: from CoolProp.CoolProp import Props

    #Specific heat 20% mass ethylene glycol solution at 300 K and 1 atm.
    In [1]: Props('C','T',300,'P',101.325,'MEG-20%')

For Lithium-Bromide, the publication by Patek and Klomfar from 2005 was implemented based on the
source code provided by the authors. The `paper <http://dx.doi.org/10.1016/j.ijrefrig.2005.10.007>`_
covering the equations can be found in the
`International Journal of Refrigeration <http://dx.doi.org/10.1016/j.ijrefrig.2005.10.007>`_. Data is
available for temperatures from 0 C to 225 C and for the full composition range. Use ``LiBr`` to acccess
the functions.

A number of aqueous solutions are implemented using the coefficients from Aake Melinder "Properties of
Secondary Working Fluids for Indirect Systems" published in 2010 by IIR.  According to the book, 2D
polynomials are given in a form that satisfies :math:`0 \\leq i \\leq 5`, :math:`0 \\leq j \\leq 3`
and :math:`i + j \\leq 5` yielding a triangular matrix of coefficients. It is only for the freezing
temperature calculation that the implemented procedures differ from what is presented in Melinder's
book the dependency on the current temperature is removed. In CoolProp, :math:`T_{freeze}` only depends
on concentration.

==========================   ===================================================   =================   =================
Melinder Fluids              Description                                           max. T              max. x
==========================   ===================================================   =================   =================
``MEG``                      Ethylene Glycol (C2H6O2)                              +100 C              60 %
``MPG``                      Propylene Glycol (C3H8O2)                             +100 C              60 %
``MEA``                      Ethyl Alcohol, Ethanol (C2H6O)                        +40 C               60 %
``MMA``                      Methyl Alcohol, Methanol (CH4O)                       +40 C               60 %
``MGL``                      Glycerol (C3H8O3)                                     +40 C               60 %
``MAM``                      Ammonia (NH3)                                         +30 C               30 %
``MKC``                      Potassium Carbonate (K2CO3)                           +40 C               40 %
``MCA``                      Calcium Chloride (CaCl2)                              +40 C               30 %
``MMG``                      Magnesium Chloride (MgCl2)                            +40 C               30 %
``MNA``                      Sodium Chloride (NaCl)                                +40 C               23 %
``MKA``                      Potassium Acetate (CH3CO2K)                           +40 C               45 %
``MKF``                      Potassium Formate (CHKO2)                             +40 C               48 %
``MLI``                      Lithium Chloride (LiCl)                               +40 C               24 %
==========================   ===================================================   =================   =================

Furthermore, there is a number of other secondary fluids that can be accessed in the same way. Most
information is based on the data compiled by Morten Juel Skovrup in his `SecCool software <http://en.ipu.dk/Indhold/refrigeration-and-energy-technology/seccool.aspx>`_
provided by his employer `IPU <http://en.ipu.dk>`_. The coefficient matrix of the SecCool-based fluids
has the same structure as mentioned above.

For slurry ice, the concentration :math:`x` refers to the solid content and the heat capacity includes the heat of fusion.
It might be necessary to adjust the solid content during heat transfer. The implementation is based on the data available
in SecCool, which was originally recorded at the `Danish Technological Institute (DTI) <http://www.dti.dk/>`_.

==========================   ===================================================   =================   =================
SecCool Fluids               Description                                           max. T              max. x
==========================   ===================================================   =================   =================
``ZiAC``                     ZitrecAC (corrosion inhibitor)                        +100 C              50 %
``IceEA``                    Ethanol-water mixture with slurry ice                 -10 C               35 %
``IcePG``                    Propylene glycol-water mixture with slurry ice        -10 C               35 %
``IceNA``                    Sodium chloride-water mixture with slurry ice         -5 C                35 %
``PK2000``                   Pekasol 2000 (Potassium acetate and formate)          +100 C              100 %
==========================   ===================================================   =================   =================

In both of the above cases, :math:`i` is the exponent for the concentration :math:`x` and :math:`j`
is used with the temperature :math:`T`. Properties are modelled with the following polynomials:

.. math::

    \\rho      &= \\sum_{i=0}^n x^i  \\cdot \\sum_{j=0}^m C_{\\rho}[i,j] \\cdot T^j \\\\
    c          &= \\sum_{i=0}^n x^i  \\cdot \\sum_{j=0}^m C_{c}[i,j] \\cdot T^j \\\\
    u          &= \\int_{0}^{1} c\\left( x,T \\right) dT
                = \\sum_{i=0}^n x^i \\cdot \\sum_{j=0}^m \\frac{1}{j+1} \\cdot C_{c}[i,j]
                  \\cdot \\left( T_1^{j+1} - T_0^{j+1} \\right) \\\\
    s          &= \\int_{0}^{1} \\frac{c\\left( x,T \\right)}{T} dT
                = \\sum_{i=0}^n x^i \\cdot \\left(
                  C_{c}[i,0] \\cdot \\ln\\left(\\frac{T_1}{T_0}\\right)
                  + \\sum_{j=0}^{m-1} \\frac{1}{j+1} \\cdot C_{c}[i,j+1] \\cdot \\left( T_1^{j+1} - T_0^{j+1} \\right)
                  \\right) \\\\
    \\lambda   &= \\sum_{i=0}^n x^i  \\cdot \\sum_{j=0}^m C_{\\lambda}[i,j] \\cdot T^j \\\\
    \\mu       &= \\exp \\left( \\sum_{i=0}^n x^i  \\cdot \\sum_{j=0}^m C_{\\mu}[i,j] \\cdot T^j \\right) \\\\
    T_{freeze} &= \\sum_{i=0}^n C_{freeze}[i] \\cdot x^i \\\\

Using a centered approach for the independent variables,
the fit quality can be enhanced. Therefore, all solutions have a reference temperature and concentration
in the original work by Melinder and Skovrup as well as in CoolProp: :math:`x = x_{real} - x_{ref}`
and :math:`T = T_{real} - T_{ref}`, this technique does not affect the calculation
of the derived quantity internal energy since the formula contains temperature differences.
However, integrating :math:`c(x,T)T^{-1}dT` for the entropy requires some changes due to
the logarithm. To structure the problem, we introduce a variable :math:`d(j,T_{real})`, which will be expressed by a third sum.
As a first step for simplification, one has to expand the the binomial :math:`(T_{real}-T_{ref})^n` to a series.
Only containing :math:`j` and :math:`T_{real}`, :math:`d` is independent from :math:`x` and can be
computed outside the loop for enhanced computational efficiency. An integration of the expanded binomial
then yields the final factor :math:`D` to be multiplied with the other coefficients and the concentration.

.. math::

    s          &= \\int_{0}^{1} \\frac{c\\left( x,T \\right)}{T} dT = \\sum_{i=0}^n x^i \\cdot \\sum_{j=0}^m C_{c}[i,j] \\cdot D(j,T_0,T_1) \\\\
    D          &= (-1)^j \\cdot \\ln \\left( \\frac{T_1}{T_0} \\right) \\cdot T_{ref}^j + \\sum_{k=0}^{j-1} \\binom{j}{k} \\cdot \\frac{(-1)^k}{j-k} \\cdot \\left( T_1^{j-k} - T_0^{j-k} \\right) \\cdot T_{ref}^k

"""
)
fp.close()

pseudo_pure_fluids = ['Air','R404A','R410A','R407C','R507A','SES36','R407F']
with open(os.path.join('Fluids','PseudoPureFluids.rst'),'w') as fp:
    fp.write('#######################\nPseudo-Pure Fluids\n#######################\n')
    fp.write(index_file(pseudo_pure_fluids))

for Fluid in pseudo_pure_fluids:
    fp=open(os.path.join('Fluids',Fluid+'.rst'),'w')
    s = fluid_header(Fluid) + params_table(Fluid) + critical_table(Fluid) + SatVaporParity(Fluid) + SatLiquidParity(Fluid) + CriticalIsotherm(Fluid) + PropertyConsistency(Fluid)
    fp.write(s)
    fp.close()

pure_fluids = sorted([fluid for fluid in CoolProp.__fluids__ if fluid not in pseudo_pure_fluids])
with open(os.path.join('Fluids','PureFluids.rst'),'w') as fp:
    fp.write('#######################\nPure Fluids\n#######################\n')
    fp.write(index_file(sorted(pure_fluids)))

for Fluid in sorted(pure_fluids):
    fp=open(os.path.join('Fluids',Fluid+'.rst'),'w')
    s = fluid_header(Fluid) + params_table(Fluid) + critical_table(Fluid) + SatVaporParity(Fluid) + SatLiquidParity(Fluid) + CriticalIsotherm(Fluid) + PropertyConsistency(Fluid)
    fp.write(s)
    fp.close()


import pygame
from settings import Settings
from pygame.sprite import Sprite, Group

settings = Settings()

class Map():
    def __init__(self, screen):
        self.screen = screen
        self.objects = []
        self.objects_view = []
        self.objects_blocking = Group()

        self.grass_image = pygame.image.load('img/grass.png')

    def blit_obj(self):
        for i in range(19):
            for j in range(19):
                self.screen.blit(self.grass_image, (i * settings.block_size , j * settings.block_size))
        for obj in self.objects_view:
            obj.blit_obj()
        for obj in self.objects_blocking:
            obj.blit_obj()

    def create_test_map(self):
        for i in range(14):
            self.objects_view.append(Road(self.screen, 5, 5 + i, '13'))
            self.objects_view.append(Road(self.screen, 5 + i, 5, '02'))
        self.objects_view.append(Road(self.screen, 5, 5, '23'))

        self.objects_blocking.add(Mountain(self.screen, 1, 4))
        self.objects_blocking.add(Mountain(self.screen, 2, 4))
        self.objects_blocking.add(Mountain(self.screen, 3, 4))
        self.objects_blocking.add(Mountain(self.screen, 4, 5))

class Road():
    def __init__(self, screen, x, y, direction):
        self.screen = screen
        self.x = x * settings.block_size
        self.y = y * settings.block_size
        self.image = pygame.image.load('img/road_' + direction + '.png')

    def blit_obj(self):
        self.screen.blit(self.image, (self.x, self.y))

class Mountain(Sprite):
    def __init__(self, screen, x, y):
        super().__init__()

        self.screen = screen

        self.image = pygame.image.load('img/mountain.png')

        self.rect = self.image.get_rect()
        self.rect.centerx = x * settings.block_size - settings.block_size // 2
        self.rect.centery = y * settings.block_size - settings.block_size // 2

    def blit_obj(self):
        self.screen.blit(self.image, self.rect)


from collections import deque
k=int(input())
m, n=map(int, input().split())
list_x=[]
d_1=[[2, 1], [2, -1], [-2, 1], [-2, -1], [1, 2], [1, -2], [-1, 2], [-1, -2]]
d_2=[[1, 0], [0, 1], [-1, 0], [0, -1]]
visit=[]
for i in range(k+1):
    visit.append([[0]*m for j in range(n)])
for i in range(n):
    list_x.append(list(map(int, input().split())))
q=deque()
q.append([0, 0, 0])
visit[0][0][0]=1
while q:
    q_pop=q.popleft()
    now_i=q_pop[0]
    now_j=q_pop[1]
    cost=q_pop[2]
    if now_i==n-1 and now_j==m-1:
        break
    if cost<k:
        for x in d_1:
            i=now_i+x[0]
            j=now_j+x[1]
            if 0<=i<n and 0<=j<m:
                if list_x[i][j]==0 and visit[cost+1][i][j]==0:
                    q.append([i, j, cost+1])
                    visit[cost+1][i][j]=visit[cost][now_i][now_j]+1
    for x in d_2:
        i=now_i+x[0]
        j=now_j+x[1]
        if 0<=i<n and 0<=j<m:
            if list_x[i][j]==0 and visit[cost][i][j]==0:
                q.append([i, j, cost])
                visit[cost][i][j]=1+visit[cost][now_i][now_j]
print(visit[cost][n-1][m-1]-1)


def secondCell(rotation):
    translator = {
        1: 2,
        2: 1,
        3: 1,
        4: 2,
        5: 4,
        6: 3,
        7: 3,
        8: 4
    }
    return translator.get(rotation, 0);

def firstCell(rotation):
    translator = {
        1: 1,
        2: 2,
        3: 2,
        4: 1,
        5: 3,
        6: 4,
        7: 4,
        8: 3
    }
    return translator.get(rotation, 0);

# convert letters to numbers
letterToNumb = {
    "A": 1,
    "B": 2,
    "C": 3,
    "D": 4,
    "E": 5,
    "F": 6,
    "G": 7,
    "H": 8

}
# convert numbers to letters
numbToLetter = dict([[v, k] for k, v in letterToNumb.items()])

class Nonvalidatedplacer:
    def __init__(self):
        pass

    # give what is in the first half of the card according to rotation if not found return 0
    def place(self, move, validator, gameMap):
        if move.type == 0:
            i = move.targetCoordinateLet - 1
            j = int(move.targetCoordinateNum) - 1
            rotation = int(move.rotation)
            validator.coordinateToRotation[numbToLetter.get(i + 1) + str(j + 1)] = rotation;
            gameMap[j][i] = firstCell(rotation)
            if rotation % 2 != 0 and rotation != 0:
                gameMap[j][i + 1] = secondCell(rotation)
            else:
                gameMap[j + 1][i] = secondCell(rotation)
            return True
        else:
            if validator.recycleValidator(move, gameMap):
                i1 = move.sourceCoordinate1Let - 1
                j1 = move.sourceCoordinate1Num - 1

                i2 = move.sourceCoordinate2Let - 1
                j2 = move.sourceCoordinate2Num - 1

                old_val1 = gameMap[j1][i1]
                old_val2 = gameMap[j2][i2]

                gameMap[j1][i1] = 0
                gameMap[j2][i2] = 0

                i = move.targetCoordinateLet - 1
                j = int(move.targetCoordinateNum) - 1
                rotation = int(move.rotation)
                if validator.placeValidator(move, gameMap):
                    # remove the value from dictionary of moves
                    if validator.coordinateToRotation.pop(numbToLetter.get(i1 + 1) + str(j1 + 1), 0) == 0:
                        validator.coordinateToRotation.pop(numbToLetter.get(i2 + 1) + str(j2 + 1), 0)

                    validator.coordinateToRotation[numbToLetter.get(i + 1) + str(j + 1)] = rotation
                    gameMap[j][i] = firstCell(rotation)
                    if rotation % 2 != 0:
                        gameMap[j][i + 1] = secondCell(rotation)
                    else:
                        gameMap[j + 1][i] = secondCell(rotation)

                    # if game_map[j1][i1] == old_val1 and game_map[j2][i2] == old_val2:
                    #     return False
                    return True
                else:
                    gameMap[j1][i1] = old_val1
                    gameMap[j2][i2] = old_val2
                    return False