import winModel
import tensorflow as tf
import sys, getopt, csv, random
import team_stats
import matplotlib.pyplot as plt
import turtle

# Get arguments for input model or output
argv = sys.argv[1:]
inputmodel = None
try:
    opts, args = getopt.getopt(argv,"hi:",["ifile="])
except getopt.GetoptError:
    print('play.py -i <inputmodel>')
    sys.exit(2)
for opt, arg in opts:
    if opt == '-h':
        print('play.py -i <inputmodel>')
        sys.exit()
    elif opt in ("-i", "--ifile"):
        inputmodel = ".\\" + arg + ".ckpt"

# Start session
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())

# Load Model
if inputmodel is not None:
    saver = tf.train.Saver()
    saver.restore(sess, inputmodel) # Restore if model exists

teams = {}
# First run through full season to get current data
for i in range(1,5410):
    if i == 3286: # Turns out this game got postponed due to a leaky roof and now nobody has the data except for the final score
        continue
    with open('C:\\Users\\Douglas\\Documents\\Visual Studio 2012\\Projects\\NCAABasketball\\NCAABasketball\\2015-2016\game' + str(i) + '.txt', 'r') as csvfile:
        content = csv.reader(csvfile)
        gameStat = list(content)
        team1 = gameStat[0]
        team2 = gameStat[1]
        if team1[0] in teams and team2[0] in teams:
            # Process Game
            teams[team1[0]].updateStats(team1)
            teams[team2[0]].updateStats(team2)
            if team1[22] <= team2[22]:
                teams[team1[0]].lose()
                teams[team2[0]].win()
            else:
                teams[team1[0]].win()
                teams[team2[0]].lose()
        else:
            # First add team1 to dictionary
            if team1[0] in teams:
                teams[team1[0]].updateStats(team1)
            else:
                teams[team1[0]] = team_stats.team_stats(team1)
            # Now add team2 to dictionary
            if team2[0] in teams:
                teams[team2[0]].updateStats(team2)
            else:
                teams[team2[0]] = team_stats.team_stats(team2)

            if team1[22] <= team2[22]:
                teams[team1[0]].lose()
                teams[team2[0]].win()
            else:
                teams[team1[0]].win()
                teams[team2[0]].lose()

# ----------BRACKET GAME CLASS----------
class bracket_game:
    def __init__(self, x, y, team1=None, team2=None, backBracket1=None, backBracket2=None):
        self.t1 = team1
        self.t2 = team2
        self.bb1 = backBracket1
        self.bb2 = backBracket2
        self.x = x
        self.y = y

    def predict(self, teams):
        self.getPredictions(teams)
        if (self.t1 is None or self.t2 is None):
            print('Missing one or more teams: ', self.t1, self.t2)
            return
        # Get stats of both teams
        team1stats = teams[self.t1].getStatVect()
        team2stats = teams[self.t2].getStatVect()
        diff1 = []
        diff2 = []
        for j in range(len(team1stats)):
            diff1 += [team1stats[j] - team2stats[j]]
            diff2 += [team2stats[j] - team1stats[j]]

        # Predict score for first team
        x_data = [team1stats + team2stats + diff1]
        team1ScorePrediction = winModel.y.eval(feed_dict={winModel.x: x_data, winModel.keep_prob: 1.0})[0] # Get output vector from model
        # Predict score for second team
        x_data = [team2stats + team1stats + diff2]
        team2ScorePrediction = winModel.y.eval(feed_dict={winModel.x: x_data, winModel.keep_prob: 1.0})[0] # Get output vector from model

        t1WinPct = team1ScorePrediction[0] / (abs(team1ScorePrediction[0]) + abs(team2ScorePrediction[0]))
        t2WinPct = team2ScorePrediction[0] / (abs(team1ScorePrediction[0]) + abs(team2ScorePrediction[0]))
        if t1WinPct <= 0 and t1WinPct == min(t1WinPct, t2WinPct):
            t1WinPct = 0.0
            t2WinPct = 1.0
        if t2WinPct <= 0:
            t2WinPct = 0.0
            t1WinPct = 1.0

        print(self.t1, t1WinPct, self.t2, t2WinPct)# Probably want to display games differently later! Could initialize brackets with coordinate locations for plotting.
        draw_game(self.x, self.y, self.t1, t1WinPct, self.t2, t2WinPct)

        # Return tuple of team1score, team2score, and winner
        if team1ScorePrediction[0] >= team2ScorePrediction[0]:
            return (team1ScorePrediction[0], team2ScorePrediction[0], self.t1)
        else:
            return (team1ScorePrediction[0], team2ScorePrediction[0], self.t2)

    def getPredictions(self, teams):
        if self.t1 is None:
            self.t1 = self.bb1.predict(teams)[2]
        if self.t2 is None:
            self.t2 = self.bb2.predict(teams)[2]

# Set some initial things up
turtle.setup (width=1900, height=1000, startx=0, starty=0)
turtle.speed(0)

def draw_game(xin, yin, t1n, t1s, t2n, t2s):
    turtle.penup()
    turtle.setpos(xin,yin)
    turtle.write(t1n + " " + str(t1s), font=("Arial", 10, "normal"))
    turtle.setpos(xin,yin-2) # Negative y is down unlike normal
    turtle.pd()
    turtle.forward(200) # Turtle starts out moving right
    turtle.hideturtle()

    yin = yin - 22
    turtle.penup()
    turtle.setpos(xin,yin)
    turtle.write(t2n + " " + str(t2s), font=("Arial", 10, "normal"))
    turtle.setpos(x,y-4) # Negative y is down unlike normal
    turtle.pd()
    turtle.forward(200) # Turtle starts out moving right
    turtle.hideturtle()
    yin = yin - 32

    # Every game should transition -54 down

# Initialize all brackets! SWEM
'''
x = -600
y = 480
PIS = bracket_game(x, y, team1='Kansas State', team2='Wake Forest')
x += 300
PIE1 = bracket_game(x, y, team1="Mount St. Mary's", team2='New Orleans')
x += 300
PIM = bracket_game(x, y, team1="North Carolina Central", team2='UC-Davis')
x += 300
PIE2 = bracket_game(x, y, team1='Providence', team2='Southern California')
'''

x = -940
y = 400
FIRSTE1 = bracket_game(x, y, team1='Villanova', team2="Mount St. Mary's")
y -= 54
FIRSTE2 = bracket_game(x, y, team1='Wisconsin', team2='Virginia Tech')
y -= 54
FIRSTE3 = bracket_game(x, y, team1='Virginia', team2='North Carolina-Wilmington')
y -= 54
FIRSTE4 = bracket_game(x, y, team1='Florida', team2='East Tennessee State')
y -= 54
FIRSTE5 = bracket_game(x, y, team1='Southern Mississippi', team2='Southern California')
y -= 54
FIRSTE6 = bracket_game(x, y, team1='Baylor', team2='New Mexico State')
y -= 54
FIRSTE7 = bracket_game(x, y, team1='South Carolina', team2='Marquette')
y -= 54
FIRSTE8 = bracket_game(x, y, team1='Duke', team2='Troy')

y -= 74
FIRSTW1 = bracket_game(x, y, team1='Gonzaga', team2='South Dakota State')
y -= 54
FIRSTW2 = bracket_game(x, y, team1='Northwestern', team2='Vanderbilt')
y -= 54
FIRSTW3 = bracket_game(x, y, team1='Notre Dame', team2='Princeton')
y -= 54
FIRSTW4 = bracket_game(x, y, team1='West Virginia', team2='Bucknell')
y -= 54
FIRSTW5 = bracket_game(x, y, team1='Maryland', team2='Xavier')
y -= 54
FIRSTW6 = bracket_game(x, y, team1='Florida State', team2='Florida Gulf Coast')
y -= 54
FIRSTW7 = bracket_game(x, y, team1="Saint Mary's (CA)", team2='Virginia Commonwealth')
y -= 54
FIRSTW8 = bracket_game(x, y, team1='Arizona', team2='North Dakota')

y = 400
x = 700
FIRSTM1 = bracket_game(x, y, team1='Kansas', team2='UC-Davis')
y -= 54
FIRSTM2 = bracket_game(x, y, team1='Miami (FL)', team2='Michigan State')
y -= 54
FIRSTM3 = bracket_game(x, y, team1='Iowa State', team2='Nevada')
y -= 54
FIRSTM4 = bracket_game(x, y, team1='Purdue', team2='Vermont')
y -= 54
FIRSTM5 = bracket_game(x, y, team1='Creighton', team2='Rhode Island')
y -= 54
FIRSTM6 = bracket_game(x, y, team1='Oregon', team2='Iona')
y -= 54
FIRSTM7 = bracket_game(x, y, team1='Michigan', team2='Oklahoma State')
y -= 54
FIRSTM8 = bracket_game(x, y, team1='Louisville', team2='Jacksonville State')

y -= 74
FIRSTS1 = bracket_game(x, y, team1='North Carolina', team2='Texas Southern')
y -= 54
FIRSTS2 = bracket_game(x, y, team1='Arkansas', team2='Seton Hall')
y -= 54
FIRSTS3 = bracket_game(x, y, team1='Minnesota', team2='Middle Tennessee')
y -= 54
FIRSTS4 = bracket_game(x, y, team1='Butler', team2='Winthrop')
y -= 54
FIRSTS5 = bracket_game(x, y, team1="Cincinnati", team2='Kansas State')
y -= 54
FIRSTS6 = bracket_game(x, y, team1='UCLA', team2='Kent State')
y -= 54
FIRSTS7 = bracket_game(x, y, team1='Dayton', team2='Wichita State')
y -= 54
FIRSTS8 = bracket_game(x, y, team1='Kentucky', team2='Northern Kentucky')

x = -720
y = 300
SECONDE1 = bracket_game(x, y, backBracket1=FIRSTE1, backBracket2=FIRSTE2) # 32 Teams in second round
y -= 54
SECONDE2 = bracket_game(x, y, backBracket1=FIRSTE3, backBracket2=FIRSTE4)
y -= 54
SECONDE3 = bracket_game(x, y, backBracket1=FIRSTE5, backBracket2=FIRSTE6)
y -= 54
SECONDE4 = bracket_game(x, y, backBracket1=FIRSTE7, backBracket2=FIRSTE8)

y = -160
SECONDW1 = bracket_game(x, y, backBracket1=FIRSTW1, backBracket2=FIRSTW2)
y -= 54
SECONDW2 = bracket_game(x, y, backBracket1=FIRSTW3, backBracket2=FIRSTW4)
y -= 54
SECONDW3 = bracket_game(x, y, backBracket1=FIRSTW5, backBracket2=FIRSTW6)
y -= 54
SECONDW4 = bracket_game(x, y, backBracket1=FIRSTW7, backBracket2=FIRSTW8)

x = 480
y = 300
SECONDM1 = bracket_game(x, y, backBracket1=FIRSTM1, backBracket2=FIRSTM2)
y -= 54
SECONDM2 = bracket_game(x, y, backBracket1=FIRSTM3, backBracket2=FIRSTM4)
y -= 54
SECONDM3 = bracket_game(x, y, backBracket1=FIRSTM5, backBracket2=FIRSTM6)
y -= 54
SECONDM4 = bracket_game(x, y, backBracket1=FIRSTM7, backBracket2=FIRSTM8)

y = -160
SECONDS1 = bracket_game(x, y, backBracket1=FIRSTS1, backBracket2=FIRSTS2)
y -= 54
SECONDS2 = bracket_game(x, y, backBracket1=FIRSTS3, backBracket2=FIRSTS4)
y -= 54
SECONDS3 = bracket_game(x, y, backBracket1=FIRSTS5, backBracket2=FIRSTS6)
y -= 54
SECONDS4 = bracket_game(x, y, backBracket1=FIRSTS7, backBracket2=FIRSTS8)

x = -500
y = 240
THIRDE1 = bracket_game(x, y, backBracket1=SECONDE1, backBracket2=SECONDE2) # Sweet Sixteen
y -= 54
THIRDE2 = bracket_game(x, y, backBracket1=SECONDE3, backBracket2=SECONDE4)

y = -220
THIRDW1 = bracket_game(x, y, backBracket1=SECONDW1, backBracket2=SECONDW2)
y -= 54
THIRDW2 = bracket_game(x, y, backBracket1=SECONDW3, backBracket2=SECONDW4)

x = 260
y = 240
THIRDM1 = bracket_game(x, y, backBracket1=SECONDM1, backBracket2=SECONDM2)
y -= 54
THIRDM2 = bracket_game(x, y, backBracket1=SECONDM3, backBracket2=SECONDM4)

y = -220
THIRDS1 = bracket_game(x, y, backBracket1=SECONDS1, backBracket2=SECONDS2)
y -= 54
THIRDS2 = bracket_game(x, y, backBracket1=SECONDS3, backBracket2=SECONDS4)

x = -280
y = 220
FOURTHE1 = bracket_game(x, y, backBracket1=THIRDE1, backBracket2=THIRDE2) # Elite Eight
x = -280
y = -240
FOURTHW1 = bracket_game(x, y, backBracket1=THIRDW1, backBracket2=THIRDW2)
x = 40
y = 220
FOURTHM1 = bracket_game(x, y, backBracket1=THIRDM1, backBracket2=THIRDM2)
x = 40
y = -240
FOURTHS1 = bracket_game(x, y, backBracket1=THIRDS1, backBracket2=THIRDS2)

x = -400
y = 0
EASTWEST = bracket_game(x, y, backBracket1=FOURTHE1, backBracket2=FOURTHW1) # Final Four
x = 160
SOUTHMIDWEST = bracket_game(x, y, backBracket1=FOURTHS1, backBracket2=FOURTHM1)
x = -120
FINAL = bracket_game(x, y, backBracket1=EASTWEST, backBracket2=SOUTHMIDWEST) # CHAMIONSHIP!!!!!!!

# RUN THE BRACKET GENERATOR!
FINAL.predict(teams)
turtle.done()


# Two candidates are running for mayor in a city with three voting regions.
# Write a program that simulates the election 10,000 times and prints the
# percentage where Candiate A wins.  A candiate wins when they win two
# of the three regions.

import random

def check_a_region(region_chances):
    if random.random() <= region_chances:
        return 1
    else:
        return 0

region_one_chances = int(input("What percentage does your candiate have in Region One? ")) * .01
region_two_chances = int(input("What percentage does your candidate have in Region Two? ")) * .01
region_three_chances = int(input("What percentage does your candidate have in Region Three? ")) * .01

wins = 0

for elections in range(10_000):
    if check_a_region(region_one_chances) + check_a_region(region_two_chances) + check_a_region(region_three_chances) >= 2:
        wins += 1

win_percentage = (wins / elections) * 100

print(f"Our candidate won {win_percentage:.2f}% of the simulations.")

Ch 08 - Conditional Logic and Control Flow/08.9-an-election-alt.py


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import datasets as ds
from sklearn import tree, svm
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split, learning_curve, GridSearchCV, validation_curve
from sklearn.metrics import accuracy_score
from sklearn.ensemble import AdaBoostClassifier
plt.rcParams["figure.figsize"] = (3.5,2.5)

random_seed = 17

ds = ds.make_classification(n_samples=10000, n_features=10, n_informative=5, n_repeated=2,
                            n_clusters_per_class=5, flip_y=0.025, class_sep = 0.5, random_state=random_seed)

X, y = ds
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=random_seed)
X_train = StandardScaler().fit_transform(X_train)
X_test = StandardScaler().fit_transform(X_test)

# First gridsearch
param_dict = {
    'kernel' : ['linear', 'poly', 'rbf', 'sigmoid'],
    'C' : [10**c for c in np.arange(-5,5, dtype=float)]
  }

clf = svm.SVC()
grid_object = GridSearchCV(estimator = clf, param_grid = param_dict, scoring = 'accuracy', cv = 10, n_jobs = -1)
grid_object.fit(X_train, y_train)
best_params = grid_object.best_params_
print(best_params)

opt_clf = svm.SVC(**best_params, random_state=random_seed)

# Learning Curve Plots
train_sizes, train_scores, validation_scores = learning_curve(opt_clf, X_train, y_train, train_sizes=np.linspace(0.1, 1.0, 100), n_jobs=-1)
av_train_scores = np.mean(train_scores, axis=1)
av_validation_scores = np.mean(validation_scores,axis=1)
# LC Plot
plt.plot(train_sizes, av_train_scores, label='train scores')
plt.plot(train_sizes, av_validation_scores, label ='validation scores')
plt.title("Learning Curve")
plt.xlabel("Training Examples")
plt.ylabel("Scores")
plt.ylim([0.60, 1.02])
plt.legend()
plt.show()

# Validation Curves
param_range = np.arange(1,1001,50)
train_scores, validation_scores = validation_curve(opt_clf, X_train, y_train, 'C', param_range, cv=10, n_jobs=-1)

av_train_scores = np.mean(train_scores, axis=1)
av_validation_scores = np.mean(validation_scores,axis=1)
# VC Plot
plt.plot(param_range, av_train_scores, label='train scores')
plt.plot(param_range, av_validation_scores, label='validation scores')
plt.title("Validation Curve")
plt.xlabel("C")
plt.ylabel("Scores")
plt.ylim([0.60, 1.02])
plt.legend()
plt.show()

# Table values
param_dict = {
    'C' : [10**c for c in np.arange(-5,5, dtype=float)]
}

clf = svm.SVC(kernel='linear', random_state=random_seed)
grid_object = GridSearchCV(estimator = clf, param_grid = param_dict, scoring = 'accuracy', cv = 10, n_jobs = -1)
grid_object.fit(X_train, y_train)
best_params = grid_object.best_params_
print(best_params)
opt_clf = svm.SVC(**best_params,random_state=random_seed)
opt_clf.fit(X_train, y_train)
print(accuracy_score(y_train, opt_clf.predict(X_train)))

opt_clf = svm.SVC(kernel='rbf', C=100, random_state=random_seed)
opt_clf.fit(X_train, y_train)
print(accuracy_score(y_train, opt_clf.predict(X_train)))


# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# http://www.sphinx-doc.org/en/master/config

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.

import os
import pathlib
import sys

import determined_ai_sphinx_theme

sys.path.append(os.path.abspath(os.path.dirname(__file__)))

# -- Project information -----------------------------------------------------

project = "Determined"
html_title = "Determined AI Documentation"
copyright = "2020, Determined AI"
author = "hello@determined.ai"

# The version info for the project you"re documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = pathlib.Path(__file__).parents[1].joinpath("VERSION").read_text()

# The full version, including alpha/beta/rc tags.
release = version

# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    "sphinx_ext_downloads",
    "sphinx.ext.autodoc",
    "sphinx.ext.extlinks",
    "sphinx.ext.intersphinx",
    "sphinx.ext.mathjax",
    "sphinx.ext.napoleon",
    "sphinxarg.ext",
    "sphinx_gallery.gen_gallery",
    "sphinx_copybutton",
    "sphinx_sitemap",
]

autosummary_generate = True
autoclass_content = "class"

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also effect to html_static_path and html_extra_path
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", "examples", "requirements.txt"]

# The suffix of source filenames.
source_suffix = {".rst": "restructuredtext", ".txt": "restructuredtext"}

highlight_language = "none"

# Base URL for sitemap.
html_baseurl = "https://docs.determined.ai"

# The URLs generated by sphinx-sitemap include both the version number and the
# language by default. We don't use language in the published URL, and we also
# want to encourage the latest version of the docs to be indexed, so only
# include that variant in the sitemap.
sitemap_url_scheme = "latest/{link}"

# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
#

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named 'default.css' will overwrite the builtin 'default.css'.
# html_static_path = ["_static"]

# Our custom sphinx extension uses this value to decide where to look for
# downloadable files.
dai_downloads_root = os.path.join("site", "downloads")

# -- HTML theme settings ------------------------------------------------

html_show_sourcelink = False
html_show_sphinx = False
html_last_updated_fmt = None
html_sidebars = {"**": ["logo-text.html", "globaltoc.html", "localtoc.html", "searchbox.html"]}

html_theme_path = [determined_ai_sphinx_theme.get_html_theme_path()]
html_theme = "determined_ai_sphinx_theme"
html_logo = "assets/images/logo.png"
html_favicon = "assets/images/favicon.ico"

html_theme_options = {
    "analytics_id": "UA-110089850-1",
    "kickfire_id": "258260",
    "collapse_navigation": False,
    "display_version": True,
    "logo_only": False,
}

language = "en"

todo_include_todos = True

html_use_index = True
html_domain_indices = True

# -- Sphinx Gallery settings -------------------------------------------

class ExplicitOrder:
    """
    sphinx_gallery.sorting.ExplicitOrder doesn't work with
    within_subsection_order. Define a custom class for ordering examples within
    subsections with a static ordering.
    """

    ORDERING = {
        "native-tf-keras": [
            "tf_keras_native.py",
            "tf_keras_native_hparam_search.py",
            "tf_keras_native_dtrain.py",
        ]
    }

    def __init__(self, src_dir):
        self.gallery = pathlib.Path(src_dir).name
        if self.gallery not in ExplicitOrder.ORDERING:
            raise Exception("Ordering for gallery {} not found".format(self.gallery))

        self.ordering = ExplicitOrder.ORDERING[pathlib.Path(src_dir).name]

    def __call__(self, item):
        if item in self.ordering:
            return self.ordering.index(item)
        else:
            raise Exception(
                "Item '{}' not found in ordering for gallery {}".format(item, self.gallery)
            )

sphinx_gallery_conf = {
    "examples_dirs": "../examples/tutorials/native-tf-keras",
    "gallery_dirs": "tutorials/native-tf-keras",
    # Subsections are sorted by number of code lines per example. Override this
    # to sort via the explicit ordering.
    # "within_subsection_order": CustomOrdering,
    "within_subsection_order": ExplicitOrder,
    "download_all_examples": True,
    "plot_gallery": False,
    "min_reported_time": float("inf"),
}


import pandas as pd

from sm.engine.util import logger

THEOR_PEAKS_TARGET_ADD_SEL = (
    'SELECT sf_id, adduct, centr_mzs[1:%s], centr_ints[1:%s] '
    'FROM theor_peaks p '
    'JOIN formula_db db ON db.id = p.db_id '
    'WHERE db.id = %s AND adduct = ANY(%s) AND ROUND(sigma::numeric, 6) = %s AND pts_per_mz = %s '
    'AND charge = %s '
    'ORDER BY sf_id, adduct')

THEOR_PEAKS_DECOY_ADD_SEL = (
    'SELECT DISTINCT p.sf_id, decoy_add as adduct, centr_mzs[1:%s], centr_ints[1:%s] '
    'FROM theor_peaks p '
    'JOIN formula_db db ON db.id = p.db_id '
    'JOIN target_decoy_add td on td.job_id = %s '
    'AND td.db_id = p.db_id AND td.sf_id = p.sf_id AND td.decoy_add = p.adduct '
    'WHERE db.id = %s AND ROUND(sigma::numeric, 6) = %s AND pts_per_mz = %s AND charge = %s '
    'ORDER BY sf_id, adduct')

class FormulasSegm(object):
    ISOTOPIC_PEAK_N = 4

    def __init__(self, job_id, db_id, ds_config, db):
        self.job_id = job_id
        self.db_id = db_id
        self.ppm = ds_config['image_generation']['ppm']
        iso_gen_conf = ds_config['isotope_generation']
        charge = '{}{}'.format(iso_gen_conf['charge']['polarity'], iso_gen_conf['charge']['n_charges'])
        target_sf_peaks_rs = db.select(THEOR_PEAKS_TARGET_ADD_SEL, self.ISOTOPIC_PEAK_N, self.ISOTOPIC_PEAK_N,
                                       self.db_id,
                                       iso_gen_conf['adducts'], iso_gen_conf['isocalc_sigma'],
                                       iso_gen_conf['isocalc_pts_per_mz'], charge)
        assert target_sf_peaks_rs, 'No formulas matching the criteria were found in theor_peaks! (target)'

        decoy_sf_peaks_rs = db.select(THEOR_PEAKS_DECOY_ADD_SEL, self.ISOTOPIC_PEAK_N, self.ISOTOPIC_PEAK_N,
                                      self.job_id, self.db_id,
                                      iso_gen_conf['isocalc_sigma'], iso_gen_conf['isocalc_pts_per_mz'], charge)
        assert decoy_sf_peaks_rs, 'No formulas matching the criteria were found in theor_peaks! (decoy)'

        sf_peak_rs = target_sf_peaks_rs + decoy_sf_peaks_rs
        self.sf_df = (pd.DataFrame(sf_peak_rs, columns=['sf_id', 'adduct', 'centr_mzs', 'centr_ints'])
                      .sort_values(['sf_id', 'adduct']))
        self.check_formula_uniqueness(self.sf_df)

        logger.info('Loaded %s sum formula, adduct combinations from the DB', self.sf_df.shape[0])

    @staticmethod
    def check_formula_uniqueness(sf_df):
        uniq_sf_adducts = pd.unique(sf_df[['sf_id', 'adduct']].values).shape[0]
        assert uniq_sf_adducts == sf_df.shape[0],\
            'Not unique formula-adduct combinations {} != {}'.format(uniq_sf_adducts, sf_df.shape[0])

    @staticmethod
    def sf_peak_gen(sf_df):
        for sf_id, adduct, mzs, _ in sf_df.values:
            for pi, mz in enumerate(mzs):
                yield sf_id, adduct, pi, mz

    def get_sf_peak_df(self):
        return pd.DataFrame(self.sf_peak_gen(self.sf_df),
                            columns=['sf_id', 'adduct', 'peak_i', 'mz']).sort_values(by='mz')

    def get_sf_adduct_sorted_df(self):
        return self.sf_df[['sf_id', 'adduct']].copy().set_index(['sf_id', 'adduct']).sort_index()

    def get_sf_peak_ints(self):
        return dict(zip(zip(self.sf_df.sf_id, self.sf_df.adduct), self.sf_df.centr_ints))

    def get_sf_adduct_peaksn(self):
        """
        Returns
        -------
        : list
            An array of triples (formula id, adduct, number of theoretical peaks)
        """
        # return zip(self.sf_ids, self.adducts, map(len, self.sf_theor_peaks))
        return zip(self.sf_df.sf_id, self.sf_df.adduct, self.sf_df.centr_mzs.map(len))


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
file_name= 'PAPER_INITIAL_10.csv'
df=pd.read_csv(file_name,sep=';')
new_data=np.zeros((15,55,5))

for j in range(15):
    for k in range(55):
        for l in range(5):
            part_1='BPR:'
            part_2=str(j+1)
            part_3=','+str(k+1)
            part_4=','+str(l+1)
            layer_name=part_1+ part_2+ part_3+part_4
            data = df[layer_name]
            value = data.to_numpy()[-1]
            new_data[j,k,l] = value

plt.imshow(new_data[:,:,1],cmap='jet',vmin=4120,vmax=np.max(new_data[:,:,1])*1.)
plt.colorbar()
plt.show()

x1=open('X-pressure.csv','w')

for j in range(15):
	for k in range(55):
		for l in range(5):
			x1.write(str(new_data[j,k,l])+',')
x1.write('\n')

x1.close()


#!/usr/bin/env python
# Name: Puja Chandrikasingh
# Student number: 11059842
"""
This script converts data in a csv file to a json file.
"""

import csv
import json

def convertToJson(file):
    """
    This function converts the data in a csv file of the KNMI to a json file.
    """

    with open(file, 'r') as csv_file:
        csv_reader = csv.DictReader(csv_file, delimiter= ",")
        title = csv_reader.fieldnames

        # make title layout nice (first letter upercase, rest lowercase)
        for i in range(len(title)):
            title[i] = title[i].strip().lower().capitalize()

        data_for_json = []

        for row in csv_reader:
            # first find the right year and extract then the data
            if row[title[1]] == '1997':
                for i in range(2, len(title) - 1):
                    data_for_json.extend([{"Date":title[i].strip(),
                                           "Temp" : row[title[i]].strip()}])

        # save the data as json
        with open("data.json", "w") as j:
            j.write(json.dumps(data_for_json, indent = 4))

if __name__ == "__main__":
    # get the csv file and call the convert function
    convertToJson("D:/Econometrie/Jaar 3/Semester 2/Data Processing/data2.csv")


from django.shortcuts import render
from django import forms
from django.forms import fields, widgets
from app01 import models

class DetailedForm(forms.Form):
    """
    required：是否必填
    error_messages：錯誤提示(要透過{{ obj.errors.user.0 }}調用)
    widget：訂製html插件(重要)
    label：訂製標籤名(要透過{{ obj.user.label }}調用)
    initial：默認值
    validators：自訂製驗證規則
    disabled：是否禁止編輯
    """
    user = fields.CharField(
        required=True,
        max_length=12,  # 最大長度
        min_length=3,  # 最小長度
        error_messages={},
        widget=widgets.TextInput(attrs={'n': 123}),  # 參數為自定義屬性
        label="用戶名",
        initial='請輸入用戶名',
        disabled=True,
    )
    age = fields.IntegerField(
        label="年齡",
        max_value=1000,  # 最大值
        min_value=0,  # 最小值
        error_messages={
            'max_value': '太大了',
        }
    )
    email = fields.EmailField(
        label="信箱",
    )
    smath = fields.DecimalField()  # 小數
    file = fields.FileField()
    city = fields.ChoiceField(
        choices=[(1, "上海"), (2, "北京"), (3, "南京")],  # (value, 顯示文字)
        initial=3,  # 默認value=3
    )
    hobby = fields.MultipleChoiceField(
        choices=[(1, "籃球"), (2, "電動"), (3, "小說")],  # (value, 顯示文字)
        initial=[1, 2],
    )
    # date = fields.DateField()  # 格式：2015-09-01
    # datetime = fields.DateTimeField()  # 格式：2015-09-01 11:12
    radio = fields.ChoiceField(
        choices=[(1, 'man'), (2, 'woman')],
        widget=widgets.RadioSelect(),
    )
    check = fields.MultipleChoiceField(
        choices=[(1, 'man'), (2, 'woman')],
        widget=widgets.CheckboxSelectMultiple(),
    )
    from django.core.validators import RegexValidator
    my = fields.CharField(
        # 自定義正則一
        # validators可設置多個RegexValidator
        # RegexValidator(正則, 錯誤信息)
        validators=[RegexValidator(r"^[0-9]+$", "請輸入數字"), RegexValidator(r"^87[0-9]+$", "請以87開頭")],
    )
    # 自定義正則二
    # fields.RegexField(正則)，只可設置單個正則
    my2 = fields.RegexField(
        r"^[0-9]+$",
        error_messages={
            "invalid": "請輸入數字",
        }
    )

def detailed_form(request):
    if request.method == "GET":
        obj = DetailedForm()
        return render(request, 'detailed_form.html', locals())
    else:
        # 文件存在request.FILES
        obj = DetailedForm(request.POST, request.FILES)
        obj.is_valid()
        print(obj.cleaned_data)
        return render(request, 'detailed_form.html', locals())

class DynamicForm(forms.Form):
    price = fields.IntegerField()
    # 因為是靜態字段，所以在程序啟動時就會給一塊內存去加載，除非重啟，否則不會動態更改
    user_id = fields.IntegerField(
        # widget=widgets.Select(choices=[(0, 'jamie'), (1, 'joker'), (2, 'jj'), ])
        # widget=widgets.Select(choices=models.UserInfo.objects.values_list('id', 'username'))
        widget=widgets.Select()  # 因為init會加載，所以不用再這邊查models
    )

    def __init__(self, *args, **kwargs):
        # super一定要寫在上面，用來拷貝所有的靜態字段複製給self.fields
        super(DynamicForm, self).__init__(*args, **kwargs)
        # 每次調用obj時就會執行一次init，所以在此更新choices，實現動態更新
        self.fields['user_id'].widget.choices = models.UserInfo.objects.values_list('id', 'username')

def dynamic_form(request):
    obj = DynamicForm()
    return render(request, 'dynamic_form.html', locals())


import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
from netCDF4 import num2date
import numpy as np
import xarray as xr
from datetime import datetime
import datetime as dt
from xarray.backends import NetCDF4DataStore
import cartopy
from mpl_toolkits.axes_grid1 import make_axes_locatable
from scipy.ndimage import gaussian_filter
import metpy.calc as mpcalc
import numpy.ma as ma
from metpy.units import units
import scipy.ndimage as ndimage
from metpy.plots import USCOUNTIES
import matplotlib.patches as mpatches
import supplementary_tools as spt
import matplotlib.colors as col
from metpy.plots import ctables
from matplotlib.colors import ListedColormap, LinearSegmentedColormap, Normalize
import soundingmaps as smap

mdate = spt.get_init_time('HRRR')[0]
init_hour = spt.get_init_time('HRRR')[1]
url = 'http://nomads.ncep.noaa.gov:80/dods/hrrr/hrrr'+mdate+'/hrrr_sfc.t'+init_hour+'z'
#url='http://nomads.ncep.noaa.gov:80/dods/hrrr/hrrr20201231/hrrr_sfc.t00z'
print(url)

# Create new directory
output_dir = mdate+'_'+init_hour+'00'
#output_dir = '20201231_0000'
spt.mkdir_p(output_dir)
spt.mkdir_p(output_dir+'/HRRR_ex')
#Parse data using MetPy
ds = xr.open_dataset(url)
times = ds['tmp2m'].metpy.time
init_time = ds['time'][0]

lats = np.arange(20,55,0.25)
lons = np.arange(240,300,0.25)
total_precip=ds['apcpsfc'].isel(time=0).squeeze()*.0393700787402

t2mi = ds['tmp2m'].isel(time=1).squeeze()-273.15
td2mi = ds['tmp2m'].isel(time=1).squeeze()-273.15

u10 = ds['ugrd10m'].isel(time=1).squeeze()*1.94384449
v10 = ds['vgrd10m'].isel(time=1).squeeze()*1.94384449
ws10 = ((u10**2)+(v10**2))**.5
acc_fram = spt.fram(acc_ice,spt.wet_bulb(t2mi,td2mi),ws10)
print("INITIALIZATION SUCCESSFUL")

for i in range(6,49):

    data = ds.metpy.parse_cf()
    data = data.isel(time=i)
    print(data['hgt700mb'])
    #Rename variables to useful things
    data = data.rename({
        'tcdcclm':'tcc',
        'tmpprs': 'temperature',
        'ugrd10m': 'u',
        'vgrd10m': 'v',
        'mslmamsl':'mslp',
        'tmp2m':'sfc_temp',
        'dpt2m':'sfc_td',
        'refcclm':'radar',
        'apcpsfc':'qpf',
        'capesfc':'cape',
        'gustsfc':'sfcgust',
        'hcdchcll':'high_cloud',
        'mcdcmcll':'mid_cloud',
        'lcdclcll':'low_cloud',
        'vissfc':'sfcvis',
        'hgt263_k':'hgt_m10c',
        'hgt253_k':'hgt_m20c',
        'ltngclm':'lightning',
        'sbt124toa':'simsat',
        'hgt0c':'0chgt',
        'hgt500mb':'height5',
        'hgt700mb':'height7',
        'pwatclm':'pwat',
        'dptprs':'dpt',
        'ugrdprs':'uprs',
        'vgrdprs':'vprs'
    })

    print(data['temperature'])
    catrain = data['catrain'].squeeze()
    catsnow = data['catsnow'].squeeze()
    catsleet = data['catsleet'].squeeze()
    catice = data['catice'].squeeze()
    pwat = data['pwat'].squeeze()*0.0393700787402
    zH5 = data['temperature'].squeeze()
    zH5_crs = zH5.metpy.cartopy_crs

    vertical, = data['temperature'].metpy.coordinates('vertical')
    time = data['temperature'].metpy.time
    x, y = data['temperature'].metpy.coordinates('x', 'y')
    lat, lon = xr.broadcast(y, x)

    t2m = data['sfc_temp'].squeeze()
    t2mc = t2m-273.15
    t2m = ((t2m - 273.15)*(9./5.))+32.

    td2m = data['sfc_td'].squeeze()
    td2mc = td2m-273.15
    td2m = ((td2m - 273.15)*(9./5.))+32.
    td2ms = ndimage.gaussian_filter(td2m,sigma=5,order=0)
    wb2mc = spt.wet_bulb(t2mc,td2mc)
    #wb2mc = (wb2m-32.)*(5./9.)

    t500 = data['temperature'].sel(lev=500).squeeze()
    t700 = data['temperature'].sel(lev=700).squeeze()
    h500 = data['height5'].squeeze()
    h700 = data['height7'].squeeze()

    delt = t700-t500
    delp = h700-h500

    mllapse = (delt/delp)*1000
    mllapse = ndimage.gaussian_filter(mllapse,sigma=1,order=0)
    swg = data['sfcgust'].squeeze()
    cloudcover = data['tcc'].squeeze()
    high_cloud = data['high_cloud'].squeeze()
    mid_cloud = data['mid_cloud'].squeeze()
    low_cloud = data['low_cloud'].squeeze()
    vis = data['sfcvis'].squeeze()*0.000621371
    reflectivity = data['radar'].squeeze()
    cape = data['cape'].squeeze()
    lightning=data['lightning'].squeeze()
    dgz_depth = data['hgt_m20c'].squeeze()-data['hgt_m10c'].squeeze()
    simsat = data['simsat'].squeeze()
    hgt0c = data['0chgt'].squeeze()*3.28084
    hrly_precip = data['qpf'].squeeze()*0.0393700787402
    total_precip = total_precip+hrly_precip

    mslpc = data['mslp'].squeeze()/100
    mslpc=ndimage.gaussian_filter(mslpc,sigma=3,order=0)
    wind_slice_zo = slice(50,-50,50)
    wind_slice = slice(36,-36,36)
    wind_slice_ne = slice(18,-18,18)
    wind_slice_me = slice(9,-9,9)

    u_10m = data['u'].squeeze()
    v_10m = data['v'].squeeze()
    u_10m = u_10m*1.94384449
    v_10m = v_10m*1.94384449
    wspd = ((u_10m**2)+(v_10m**2))**.5

    u5 = data['uprs'].sel(lev=500).squeeze()*1.94384449
    v5 = data['vprs'].sel(lev=500).squeeze()*1.94384449

    u8 = data['uprs'].sel(lev=850).squeeze()*1.94384449
    v8 = data['vprs'].sel(lev=850).squeeze()*1.94384449

    ws5 = ((u5**2)+(v5**2))**.5
    ws8 = ((u8**2)+(v8**2))**.5

    u5m = np.ma.masked_where(ws5<35,u5)
    v5m = np.ma.masked_where(ws5<35,v5)

    u8m = np.ma.masked_where(ws8<25,u8)
    v8m = np.ma.masked_where(ws8<25,v8)

    ###GRAB LOCAL DATA###
    stations=['PWM','AUG','RKD','SFM','IZG','LEW','EEN','MHT','BML','CVA','1P1','FAR','BGR','GNR','CAR','MAC',
            'RUT','BTV','CDA','ORH','TAN','BAF','BOS','ALB','SYR','ROC','ITH','ART','MSV','HFD','BDL','OKX',
            'PVD','YSC']
    coords=[[43.644940, -70.309360],[44.317911, -69.796462],[44.062355, -69.095368],[43.398890, -70.711034],
            [43.989267, -70.946733],[44.047577, -70.284566],[42.901405, -72.269613],[42.929656, -71.434166],
            [44.577748, -71.177514],[45.084798, -70.217251],[43.778606, -71.753213],[44.670005, -70.151436],
            [44.807750, -68.818231],[45.463921, -69.553345],[45.463921, -69.553345],[44.727026, -67.473533],
            [43.527006, -72.949843],[44.466761, -73.141541],[44.570582, -72.017706],[44.466761, -73.141541],
            [42.266704, -71.873740],[41.873395, -71.015191],[42.159667, -72.715977],[42.358814, -71.057869],
            [42.749352, -73.804664],[43.112376, -76.110738],[43.122772, -77.672485],[42.490250, -76.457773],
            [43.992712, -76.022970],[41.702975, -74.798589],[41.737535, -72.649142],[41.478649, -73.135379],
            [41.724628, -71.427676],[45.438474, -71.691100]]

    pwlevs = []
    for j in range(10,25):
        lev = 0.1*j
        pwlevs.append(lev)

    '''
    station_qpf = []
    station_temp = []
    for i in range(len(stations)):
        station = stations[i]
        lat = coords[i][0]
        lon = coords[i][1]
        qpf = total_precip.interp(lat=lat,lon=lon).values
        temp = t2m.interp(lat=lat,lon=lon).values
        station_qpf.append(np.round(qpf,1))
        station_temp.append(np.round(temp,1))
    '''
    ########## SET UP FIGURE ##################################################
    fig = plt.figure(figsize=(44,15))

    gs = fig.add_gridspec(ncols=3,nrows= 2, width_ratios=[1,2,1])
    gs.update(hspace=0.01,wspace=0.01)
    ax1 = fig.add_subplot(gs[:, 1], projection = zH5_crs)
    ax2 = fig.add_subplot(gs[0, 0], projection = zH5_crs)
    ax3 = fig.add_subplot(gs[1, 0], projection = zH5_crs)
    ax4 = fig.add_subplot(gs[0, 2], projection = zH5_crs)
    ax5 = fig.add_subplot(gs[1, 2], projection = zH5_crs)

    ax1.coastlines(resolution='10m')
    ax1.add_feature(cfeature.BORDERS.with_scale('10m'))
    ax1.add_feature(cfeature.STATES.with_scale('10m'))

    ax2.coastlines(resolution='10m')
    ax2.add_feature(cfeature.BORDERS.with_scale('10m'))
    ax2.add_feature(cfeature.STATES.with_scale('10m'))

    ax3.coastlines(resolution='10m')
    ax3.add_feature(cfeature.BORDERS.with_scale('10m'))
    ax3.add_feature(cfeature.STATES.with_scale('10m'))

    ax4.coastlines(resolution='10m')
    ax4.add_feature(cfeature.BORDERS.with_scale('10m'))
    ax4.add_feature(cfeature.STATES.with_scale('10m'))

    ax5.coastlines(resolution='10m')
    ax5.add_feature(cfeature.BORDERS.with_scale('10m'))
    ax5.add_feature(cfeature.STATES.with_scale('10m'))

    #fig.suptitle("HRRR Forecast valid at " + time[0].dt.strftime('%Y-%m-%d %H:%MZ').item(),fontsize=36)

    dtfs = str(time.dt.strftime('%Y-%m-%d_%H%MZ').item())
    print(dtfs)

    norm_ref, cmap_ref = ctables.registry.get_with_steps('NWSStormClearReflectivity', -20., .5)
    newcmap = ListedColormap(cmap_ref(range(40, 194)))
    newnorm = Normalize(0,77)

    ########## PLOTTING #######################################################
    tmp_2m = ax5.contourf(x,y,t2m,cmap='RdYlBu_r', alpha = 0.8, levels = range(-20,100,5),transform=zH5_crs)
    tmp_2m32 = ax5.contour(x,y,t2m,colors='b', alpha = 0.8, levels = [32])
    cbr = fig.colorbar(tmp_2m, orientation = 'vertical', aspect = 80, ax = ax5, pad = 0.01,
                        extendrect=False, ticks = range(-20,100,5), shrink=0.7)
    cbr.set_label('2m Temperature (F)', fontsize = 14)

    h_contour = ax1.contour(x, y, mslpc, colors='lightgray', levels=range(940,1040,4),linewidths=2)
    h_contour.clabel(fontsize=14, colors='lightgray', inline=True, fmt='%i mb', rightside_up=True, use_clabeltext=True)

    ref_levs = range(5,75,1)

    mlc = ax1.contourf(x,y,mllapse,colors=['lightcoral','orangered','orange'],levels=[-10,-8,-7],alpha=0.25)
    mlcc = ax1.contour(x,y,mllapse,colors=['lightcoral','orangered','orange'],levels=[-10,-8,-7],linewidths=1.5)

    tdc = ax1.contour(x,y,td2m,colors='lawngreen',levels=[60],linewidths=1.5)
    tdf = ax1.contourf(x,y,td2m,colors=['lawngreen','green'],levels=[60,100],alpha=0.2)
    cape_summary = ax1.contourf(x,y,cape,levels=[500,1000,1500,2000,2500,3000,3500,4000],alpha=0.5,colors=['plum','violet','orchid','palevioletred','hotpink','deeppink','mediumvioletred','magenta'])

    refc = ax1.contourf(x,y,reflectivity,norm=newnorm,cmap=newcmap,levels=range(5,75,1))

    ax1.set_facecolor('dimgray')

    cbr = fig.colorbar(refc, orientation = 'horizontal', aspect = 80, ax = ax1, pad = 0.01,
                        extendrect=False, ticks = range(-20,100,5), shrink=0.7)
    cbr.set_label('Reflectivity', fontsize = 14)

    #cax1 = fig.add_axes([.35,.5,.02,.25])
    #cax2 = fig.add_axes([.35,.75,.02,.25])

    refp = ax4.contourf(x,y,reflectivity, levels=[20, 25, 30, 35, 40, 45, 50, 55, 60, 65], alpha = 0.7, cmap = 'Greens',transform=zH5_crs) #colors=['#0099ff00', '#4D8080ff', '#666666ff', '#804d4dff','#993333ff','#B33333ff','#CC1a1aff','#E60000ff','#0000e6','#0000cc','#0000b3','#2d00b3','#5900b3','#8600b3','#b300b3','#b30086'])
    capep = ax4.contourf(x, y, cape, levels=[100, 250, 500, 750, 1000, 1250, 1500, 1750, 2000, 2500, 3000], extend='max', alpha = 0.7, cmap='RdPu')#['#0099ff00', '#4066ffb3', '#8066ff8c', '#BF66ff66','#8cff66','#b3ff66','#d9ff66','#ffff66','#ffd966','#ffcc66','#ffb366','#ff8c66','#ff6666','#ff668c','#ff66b3','#ff66d9','#ff66ff'])
    lgt = ax4.contour(x,y,lightning,levels=[0.5,1,1.5,2,2.5,3,3.5,4,4.5,5])
    ax4.barbs(x[wind_slice_zo],y[wind_slice_zo],u5m[wind_slice_zo,wind_slice_zo],v5m[wind_slice_zo,wind_slice_zo],length=6,color='blue')
    ax4.barbs(x[wind_slice_zo],y[wind_slice_zo],u8m[wind_slice_zo,wind_slice_zo],v8m[wind_slice_zo,wind_slice_zo],length=6,color='yellow')

    cb = fig.colorbar(capep, orientation='vertical', pad = 0.01, aspect = 50, ax = ax4, extendrect=False, ticks=[100, 250, 500, 750, 1000, 1250, 1500, 1750, 2000, 2500, 3000])
    cb.set_label('SBCAPE (J/kg)', size='large')
    #smap.plot_soundings(fig,ax4,data['temperature'],data['rh'],31,99.5,'regional',cape=True)

    #ax4.barbs(x[ws2],y[ws2],s5u[ws2,ws2],s5v[ws2,ws2], length = 7)
    white = mpatches.Patch(color='#6ec6fd', label='Snow')
    orange = mpatches.Patch(color='orange', label='7-8 C/km')
    pink = mpatches.Patch(color='orangered', label='>8 C/km')
    green = mpatches.Patch(color='lawngreen', label='Td >60F')

    leg = ax1.legend(handles=[pink,orange,green],loc=4,framealpha=1)
    leg.set_zorder(100)

    ax1.barbs(x[wind_slice],y[wind_slice],u_10m[wind_slice,wind_slice],v_10m[wind_slice,wind_slice], length=6)
    ax1.set_title('Reflectivity (dBZ), 700-500mb Lapse Rates (C/km), 10m Winds (kts), and MSLP (mb)',fontsize=14)
    ax1.set_title('\n Valid: '+time.dt.strftime('%a %b %d %H:%MZ').item(),fontsize=11,loc='right')
    ax1.set_title('\n HRRR Init: '+init_time.dt.strftime('%Y-%m-%d %H:%MZ').item(),fontsize=11,loc='left')

    cloud_levs = [40,45,50,55,60,65,70,75,80,85,90,95]
    #high_cols = ['#33c7f4f4','#40b6f0f1','#4da5edee','#5990e8ea','#6678ebed','#7359e6e8','#8030e5e8','#8c18d5d8','#9916c2c5','#9914b5b8','#9912a5a5','#99119579','#990e7a7c','#990d7173','#990c686a','#990b6365']
    #mid_cols = ['#4dbcfac1','#59aef9b4','#66a0f8a7','#7391f79a','#8083f68d','#8c75f57f','#9966f472','#a658f365','#b34af258','#b33bf14b','#b32df03d','#b325e935','#b31ce32d','#b31ad52a','#b319c827','#b317ba25']
    #low_cols = ['#66ea99f4','#73e78cf3','#80e47ef1','#8ce170f0','#99db54ed','#a6d846ec','#b3d539ea','#bfd32be9','#ccd01de7','#ccc617de','#ccba16d0','#ccae14c2','#cca113b4','#cc9511a6','#cc881098','#cc7c0f8a']

    high_cols = ['#c7f4f4','#b6f0f1','#a5edee','#90e8ea','#78ebed','#59e6e8','#30e5e8','#18d5d8','#16c2c5','#14b5b8','#12a5a5','#119579']
    mid_cols = ['#bcfac1','#aef9b4','#a0f8a7','#91f79a','#83f68d','#75f57f','#66f472','#58f365','#4af258','#3bf14b','#2df03d','#25e935']
    low_cols = ['#ea99f4','#e78cf3','#e47ef1','#e170f0','#db54ed','#d846ec','#d539ea','#d32be9','#d01de7','#c617de','#ba16d0','#ae14c2']

    blue = mpatches.Patch(color='#119579', label='High Clouds')
    green = mpatches.Patch(color='#25e935', label='Mid-Level Clouds')
    purple = mpatches.Patch(color='#ae14c2',label='Low-Level Clouds')
    leg = ax2.legend(handles=[blue,green,purple],loc=4,framealpha=1)
    leg.set_zorder(100)
    tccp = ax2.contourf(x,y,cloudcover,cmap='Greys',levels=cloud_levs,alpha=0,extend='max')
    lccp = ax2.contourf(x,y,low_cloud, colors=low_cols,levels=cloud_levs,alpha = 0.35,extend='max')
    mccp = ax2.contourf(x,y,mid_cloud, colors=mid_cols,levels=cloud_levs,alpha = 0.25,extend='max')
    hccp = ax2.contourf(x,y,high_cloud, colors=high_cols,levels=cloud_levs,alpha = 0.15,extend='max')#colors=['dimgray','gray','darkgray','slategrey','silver','lightgray'])
    cbar2 = fig.colorbar(tccp,orientation='vertical',pad=0.01,ax=ax2,aspect=50,extendrect=False, ticks=np.arange(10,100,10))
    cbar2.set_label('Cloud Cover (%)',fontsize=14)

    tprecip = ax3.contourf(x,y,total_precip, alpha = 0.7, cmap = 'cool',transform=zH5_crs, levels=[0.01,0.1,0.25,0.5,0.75,1.0,1.25,1.5,2.0,2.5,3,3.5,4,4.5,5])
    tcprecip = ax3.contour(x,y,total_precip,colors=['b','darkblue','darkviolet'],levels=[0.5,1,1.5],linewidths=1.5)
    cbar3 = fig.colorbar(tprecip,orientation='vertical',pad=0.01,ax=ax3,aspect=50,extendrect=False,ticks=[0.01,0.1,0.25,0.5,0.75,1.0,1.25,1.5,2.0,2.5,3,3.5,4,4.5,5])
    cbar3.set_label('Total Precipitation (inches)',fontsize=14)
    pwc = ax3.contourf(x,y,pwat,cmap='Greens',levels=pwlevs,alpha=0.5)
    ax3.set_facecolor('dimgray')
    ax2.set_facecolor('dimgray')
    ax4.set_facecolor('dimgray')

    #refp3 = ax4.contourf(x,y,reflectivity, levels=[20, 25, 30, 35, 40, 45, 50, 55, 60, 65], alpha = 0.7, cmap = 'Greens',transform=zH5_crs)
    #cbar4 = fig.colorbar(tccp,orientation='horizontal',pad=0.01,ax=ax4,aspect=50,extendrect=False)

    #ax1.set_extent((255, 290, 25, 45))#, crs = zH5_crs)    # Set a title and show the plot
    #ax2.set_extent((255, 290, 25, 45))#, crs = zH5_crs)    # Set a title and show the plot
    #ax3.set_extent((255, 290, 25, 45))#, crs = zH5_crs)    # Set a title and show the plot
    #ax4.set_extent((255, 290, 25, 45))#, crs = zH5_crs)    # Set a title and show the plot
    #ax5.set_extent((255, 290, 25, 45))#, crs = zH5_crs)    # Set a title and show the plot
    #north=50, south=15, east=-70, west=-115

    ## set bounds from center point. 15 lon, 12 lat.

    ax1.set_extent((242, 288, 24, 50))#, crs = zH5_crs)    # Set a title and show the plot
    ax2.set_extent((250, 288, 24, 49))#, crs = zH5_crs)    # Set a title and show the plot
    ax3.set_extent((250, 288, 24, 49))#, crs = zH5_crs)    # Set a title and show the plot
    ax4.set_extent((250, 288, 24, 49))#, crs = zH5_crs)    # Set a title and show the plot
    ax5.set_extent((250, 288, 24, 49))#, crs = zH5_crs)    # Set a title and show the plot
    #fig.canvas.draw()
    fig.tight_layout()
    plt.savefig(output_dir+'/HRRR_ex/CONUS_svr_dash_v4_'+dtfs+'_.png',bbox_inches='tight',pad_inches=0.1)
    ax1.barbs(x[wind_slice_ne],y[wind_slice_ne],u_10m[wind_slice_ne,wind_slice_ne],v_10m[wind_slice_ne,wind_slice_ne], length=6)
    '''
    ax1.set_extent((281, 295, 39, 49))#, crs = zH5_crs)    # Set a title and show the plot
    ax2.set_extent((283, 295, 39, 49))#, crs = zH5_crs)    # Set a title and show the plot
    ax3.set_extent((283, 295, 39, 49))#, crs = zH5_crs)    # Set a title and show the plot
    ax4.set_extent((283, 295, 39, 49))#, crs = zH5_crs)    # Set a title and show the plot
    ax5.set_extent((283, 295, 39, 49))#, crs = zH5_crs)    # Set a title and show the plot
    for i in range(len(stations)):
        station = stations[i]
        lat = coords[i][0]
        lon = coords[i][1]
        ax3.text(360+lon,lat,str(station_qpf[i]),ha='center',transform=zH5_crs)
        ax1.text(360+lon,lat,str(station_temp[i]),ha='center',transform=zH5_crs)
    plt.savefig(output_dir+'/HRRR_ex/NE_fivepanelwinter9_'+dtfs+'_.png',bbox_inches='tight',pad_inches=0.1)
    plt.clf()
    '''
    plt.clf()
    '''
    ### SECOND PLOT ###
    fig2 = plt.figure(figsize=(15,15))
    ax6 = fig2.add_subplot(111, projection = zH5_crs)

    ax6.coastlines(resolution='10m')
    ax6.add_feature(cfeature.BORDERS.with_scale('10m'))
    ax6.add_feature(cfeature.STATES.with_scale('10m'))

    tmp_2m = ax6.contourf(x,y,t2m,cmap='RdYlBu_r', alpha = 0.8, levels = range(-20,100,5),transform=zH5_crs)
    tmp_2m32 = ax6.contour(x,y,t2m,colors='b', alpha = 0.8, levels = [32])
    cbr6 = fig2.colorbar(tmp_2m, orientation = 'horizontal', aspect = 80, ax = ax6, pad = 0.01,
                        extendrect=False, ticks = range(-20,100,5))
    cbr6.set_label('2m Temperature (F)', fontsize = 14)

    h_contour = ax6.contour(x, y, mslpc, colors='dimgray', levels=range(940,1040,4),linewidths=2)
    h_contour.clabel(fontsize=14, colors='dimgray', inline=1, inline_spacing=4, fmt='%i mb', rightside_up=True, use_clabeltext=True)

    ref_levs = [1,5,10,15,20, 25, 30, 35, 40, 45, 50, 55, 60, 65]

    try:
        ra = ax6.contourf(x,y,rain,cmap='Greens',levels=ref_levs,alpha=0.7)
    except:
        print('no rain')
    try:
        sn = ax6.contourf(x,y,snow,cmap='cool',levels=ref_levs,alpha=0.7)
    except:
        print('no snow')
    try:
        ip = ax6.contourf(x,y,sleet,cmap='autumn',levels=ref_levs,alpha=0.7)
    except:
        print('no sleet')
    try:
        zr = ax6.contourf(x,y,ice, cmap='RdPu',levels=ref_levs,alpha=0.7)
    except:
        print('no ice')

    ax6.barbs(x[wind_slice],y[wind_slice],u_10m[wind_slice,wind_slice],v_10m[wind_slice,wind_slice], length=6)
    ax6.set_extent((sub_w-1, sub_e-1, sub_s, sub_n))#, crs = zH5_crs)    # Set a title and show the plot
    ax6.set_title('HRRR Composite Forecast valid at ' + time.dt.strftime('%Y-%m-%d %H:%MZ').item(),fontsize=24)
    plt.savefig(output_dir+'/HRRR_ex/EC_ptype_composite_'+dtfs+'_.png',bbox_inches='tight')
    ax6.set_extent((283, 295, 39, 49))#, crs = zH5_crs)    # Set a title and show the plot
    ax6.barbs(x[wind_slice_ne],y[wind_slice_ne],u_10m[wind_slice_ne,wind_slice_ne],v_10m[wind_slice_ne,wind_slice_ne], length=6)
    plt.savefig(output_dir+'/HRRR_ex/NE_ptype_composite_'+dtfs+'_.png',bbox_inches='tight',pad_inches=0.1)
    ax6.add_feature(USCOUNTIES.with_scale('5m'), edgecolor='dimgray')
    wsl = slice(5,-5,5)
    ax6.barbs(x[wsl],y[wsl],u_10m[wsl,wsl],v_10m[wsl,wsl], length=6)
    ax6.set_extent((289,291,43,45))
    plt.savefig(output_dir+'/HRRR_ex/local_ptype_composite_'+dtfs+'_.png',bbox_inches='tight',pad_inches=0.1)
    plt.close()
    plt.clf()
    plt.close()
    plt.clf()
    ### END SECOND PLOT ###

    ### THIRD PLOT ###
    fig3 = plt.figure(figsize=(15,15))
    ax7 = fig3.add_subplot(111,projection=zH5_crs)

    ax7.coastlines(resolution='10m')
    ax7.add_feature(cfeature.BORDERS.with_scale('10m'))
    ax7.add_feature(cfeature.STATES.with_scale('10m'))

    tprecip = ax7.contourf(x,y,total_precip, alpha = 0.7, cmap = 'cool',transform=zH5_crs, levels=[0.01,0.1,0.25,0.5,0.75,1.0,1.25,1.5,2.0,2.5,3,3.5,4,4.5,5])
    tcprecip = ax7.contour(x,y,total_precip,colors=['b','darkblue','darkviolet'],levels=[0.5,1,1.5],linewidths=2)
    cbar3 = fig3.colorbar(tprecip,orientation='vertical',pad=0.01,shrink=.6,ax=ax7,aspect=50,extendrect=False,ticks=[0.01,0.1,0.25,0.5,0.75,1.0,1.25,1.5,2.0,2.5,3,3.5,4,4.5,5])
    cbar3.set_label('Total Precipitation (inches)',fontsize=14)

    ax7.set_extent((sub_w-1, sub_e-1, sub_s, sub_n))#, crs = zH5_crs)    # Set a title and show the plot
    ax7.set_title('HRRR Precipitation Forecast Valid Through ' + time.dt.strftime('%Y-%m-%d %H:%MZ').item(),fontsize=24)
    plt.savefig(output_dir+'/HRRR_ex/EC_total_precip_'+dtfs+'_.png',bbox_inches='tight',pad_inches=0.1)
    ax7.set_extent((281, 295, 39, 49))
    plt.savefig(output_dir+'/HRRR_ex/NE_total_precip_'+dtfs+'_.png',bbox_inches='tight',pad_inches=0.1)
    ax7.set_extent((289,291,43,45))
    ax7.add_feature(USCOUNTIES.with_scale('5m'), edgecolor='dimgray')
    plt.savefig(output_dir+'/HRRR_ex/local_total_precip_'+dtfs+'_.png',bbox_inches='tight',pad_inches=0.1)
    plt.close()
    plt.clf()
    ### FOURTH PLOT ###
    fig4 = plt.figure(figsize=(15,15))
    ax8 = fig4.add_subplot(111,projection=zH5_crs)

    ax8.coastlines(resolution='10m')
    ax8.add_feature(cfeature.BORDERS.with_scale('10m'))
    ax8.add_feature(cfeature.STATES.with_scale('10m'))

    sfcwinds = ax8.contourf(x,y,wspd, cmap='PuRd', levels=range(5,60,5), alpha=0.75)
    cbr = fig4.colorbar(sfcwinds, orientation = 'vertical', pad = 0.01, aspect = 25,
                        panchor = (0.999,0.5), ax = ax8, extendrect=False, ticks = range(5,75,5), shrink = 0.80)
    cbr.set_label('10m Wind Speed (kts)')
    ax8.barbs(x[wind_slice],y[wind_slice],u_10m[wind_slice,wind_slice],v_10m[wind_slice,wind_slice], length=6)
    ax8.set_title('HRRR 10m Wind Forecast Valid at ' + time.dt.strftime('%Y-%m-%d %H:%MZ').item(),fontsize=24)
    ax8.set_extent((sub_w-1, sub_e-1, sub_s, sub_n))#, crs = zH5_crs)    # Set a title and show the plot

    h_contouqr = ax8.contour(x, y, mslpc, colors='dimgray', levels=range(940,1040,4),linewidths=2)
    h_contouqr.clabel(fontsize=14, colors='dimgray', inline=1, inline_spacing=4, fmt='%i mb', rightside_up=True, use_clabeltext=True)

    plt.savefig(output_dir+'/HRRR_ex/EC_wind_'+dtfs+'_.png',bbox_inches='tight',pad_inches=0.1)
    ax8.set_extent((281, 295, 39, 49))
    plt.savefig(output_dir+'/HRRR_ex/NE_wind_'+dtfs+'_.png',bbox_inches='tight',pad_inches=0.1)
    ax8.set_extent((289,291,43,45))
    ax8.barbs(x[wsl],y[wsl],u_10m[wsl,wsl],v_10m[wsl,wsl], length=6)
    ax8.add_feature(USCOUNTIES.with_scale('5m'), edgecolor='dimgray')
    plt.savefig(output_dir+'/HRRR_ex/local_wind_'+dtfs+'_.png',bbox_inches='tight',pad_inches=0.1)
    plt.close()
    plt.clf()
    plt.close()
    plt.clf()

    ### FIFTH PLOT ###
    fig5 = plt.figure(figsize=(15,15))
    ax9 = fig5.add_subplot(111,projection=zH5_crs)

    ax9.coastlines(resolution='10m')
    ax9.add_feature(cfeature.BORDERS.with_scale('10m'))
    ax9.add_feature(cfeature.STATES.with_scale('10m'))
    ax9.set_title('HRRR Cloud Cover Forecast Valid at ' + time.dt.strftime('%Y-%m-%d %H:%MZ').item(),fontsize=24)

    blue = mpatches.Patch(color='#119579', label='High Clouds')
    green = mpatches.Patch(color='#25e935', label='Mid-Level Clouds')
    purple = mpatches.Patch(color='#ae14c2',label='Low-Level Clouds')
    leg = ax9.legend(handles=[blue,green,purple],loc=3,framealpha=1)
    leg.set_zorder(100)
    tccp = ax9.contourf(x,y,cloudcover,cmap='Greys',levels=cloud_levs,alpha=0,extend='max')
    lccp = ax9.contourf(x,y,low_cloud, colors=low_cols,levels=cloud_levs,alpha = 0.35,extend='max')
    mccp = ax9.contourf(x,y,mid_cloud, colors=mid_cols,levels=cloud_levs,alpha = 0.25,extend='max')
    hccp = ax9.contourf(x,y,high_cloud, colors=high_cols,levels=cloud_levs,alpha = 0.15,extend='max')#colors=['dimgray','gray','darkgray','slategrey','silver','lightgray'])
    cbar2 = fig.colorbar(tccp,orientation='vertical',pad=0.01,ax=ax9,shrink=.8,aspect=50,extendrect=False, ticks=np.arange(10,100,10))
    cbar2.set_label('Cloud Cover (%)',fontsize=14)
    ax9.set_extent((sub_w-1, sub_e-1, sub_s, sub_n))#, crs = zH5_crs)    # Set a title and show the plot
    plt.savefig(output_dir+'/HRRR_ex/EC_clouds_'+dtfs+'_.png',bbox_inches='tight',pad_inches=0.1)
    ax9.set_extent((281, 295, 39, 49))#, crs = zH5_crs)    # Set a title and show the plot
    plt.savefig(output_dir+'/HRRR_ex/NE_clouds_'+dtfs+'_.png',bbox_inches='tight',pad_inches=0.1)
    ax9.set_extent((289,291,43,45))
    ax9.add_feature(USCOUNTIES.with_scale('5m'), edgecolor='dimgray')
    plt.savefig(output_dir+'/HRRR_ex/local_clouds_'+dtfs+'_.png',bbox_inches='tight',pad_inches=0.1)
    plt.clf()
    plt.close()
    plt.clf()
    plt.close()

    ###PLOT ACCUMS#####
    ref_levs = [1,5,10,15,20, 25, 30, 35, 40, 45, 50, 55, 60, 65]
    q_levs = [0.01,0.05,0.1,0.25,0.5,0.75,1,1.25]
    qarlevs = [0.01,0.05,0.1,0.25,0.5,0.75,1,1.25,1.5,1.75,2,2.5,3,3.5,4,4.5,5,6,7,8,9,10]
    qaslevs = [0.01,0.05,0.1,0.25,0.5,0.75,1,1.25,1.5,1.75,2,2.5,3,3.5,4,4.5]
    qazlevs = [0.01,0.05,0.1,0.25,0.5,0.75,1,1.25,1.5,1.75,2,2.5,3]
    qr_cols = ['#cfffbf','#a7ff8a','#85ff5c','#60ff2b','#40ff00','#ffff00','#e6e600','#cccc00','#e4cc00']
    qs_cols = ['#b8ffff','#82ffff','#00ffff','#00cccc','#00adad','#007575','#0087f5','#0039f5','#1d00f5']
    qi_cols = ['#eeccff','#dd99ff','#cc66ff','#bb33ff','#aa00ff','#8800cc','#660099','#440066','#6600cc']
    qz_cols = ['#ff0066','#ff0080','#ff33cc','#ff00bf','#cc0099','#990073','#66004d','#b30000','#ff3333']
    qra_cols = ['#cfffbf','#a7ff8a','#85ff5c','#60ff2b','#40ff00','#40ff00','#ffff00','#e6e600','#cccc00','#e4cc00','#ffcc00','#ff9500','#ff4800','#ff2900','#ff1200','#ff0000','#cc0000','#990000','#990033','#b3003b','#ff3333','#ff6666','#ffffff']
    qrs_cols = ['#b8ffff','#82ffff','#00ffff','#00cccc','#00adad','#007575','#0087f5','#0039f5','#1d00f5','#4f01f6','#7a00f5','#9e00f5','#b833ff','#d280ff','#cc00f1','#ad00cc','#820099','#4700b3']
    qzr_cols = ['#ff0066','#ff33cc','#ff00bf','#cc0099','#990073','#66004d','#b30000','#ff3333','#ff6666','#ff9999','#ffcccc','#ffffff']
    qip_cols = ['#eeccff','#dd99ff','#cc66ff','#bb33ff','#aa00ff','#8800cc','#660099','#440066','#6600cc','#9933ff','#bf80ff','#e6ccff','#ffffff']
    q_levs_r = [0.01,0.05,0.1,0.25,0.5,0.75,1,1.25,1.5,1.75,2,2.5,3]

    fig6 = plt.figure(figsize=(25,15))
    ax10 = fig6.add_subplot(221,projection=zH5_crs)
    ax11 = fig6.add_subplot(222,projection=zH5_crs)
    ax12 = fig6.add_subplot(223,projection=zH5_crs)
    ax13 = fig6.add_subplot(224,projection=zH5_crs)

    ax10.coastlines(resolution='10m')
    ax10.add_feature(cfeature.BORDERS.with_scale('10m'))
    ax10.add_feature(cfeature.STATES.with_scale('10m'))

    ax11.coastlines(resolution='10m')
    ax11.add_feature(cfeature.BORDERS.with_scale('10m'))
    ax11.add_feature(cfeature.STATES.with_scale('10m'))

    ax12.coastlines(resolution='10m')
    ax12.add_feature(cfeature.BORDERS.with_scale('10m'))
    ax12.add_feature(cfeature.STATES.with_scale('10m'))

    ax13.coastlines(resolution='10m')
    ax13.add_feature(cfeature.BORDERS.with_scale('10m'))
    ax13.add_feature(cfeature.STATES.with_scale('10m'))

    try:
        ra = ax13.contourf(x,y,acc_rain,colors=qra_cols,levels=qarlevs,alpha=0.7)
        rac = fig6.colorbar(ra,orientation = 'vertical', aspect = 20, ax = ax13, pad = 0.01,
                            extendrect=False, ticks = qarlevs,shrink=0.7)
    except:
        print('no rain')
    try:
        sn = ax10.contourf(x,y,acc_snow,colors=qrs_cols,levels=qaslevs,alpha=0.7)
        snc = fig6.colorbar(sn,orientation = 'vertical', aspect = 20, ax = ax10, pad = 0.01,
                            extendrect=False, ticks = qaslevs,shrink=0.7)
    except:
        print('no snow')
    try:
        ip = ax11.contourf(x,y,acc_sleet,colors=qip_cols,levels=qazlevs,alpha=0.7)
        ipc = fig6.colorbar(ip,orientation = 'vertical', aspect = 20, ax = ax11, pad = 0.01,
                            extendrect=False, ticks = qazlevs,shrink=0.7)
    except:
        print('no sleet')
    try:
        zr = ax12.contourf(x,y,acc_ice, colors=qzr_cols,levels=qazlevs,alpha=0.7)
        zrc = fig6.colorbar(zr,orientation = 'vertical', aspect = 20, ax = ax12, pad = 0.01,
                            extendrect=False, ticks = qazlevs,shrink=0.7)
    except:
        print('no ice')

    ax10.set_title('Accumulated Liquid Equivalent Snow (in)')
    ax10.set_title('HRRR Init: '+init_time.dt.strftime('%m-%d %H:%MZ').item(),fontsize=14,loc='left')
    ax11.set_title('Accumulated Liquid Equivalent Sleet (in)')
    ax11.set_title('Valid: '+time.dt.strftime('%a %b %d %H:%MZ').item(),fontsize=14,loc='right')
    ax12.set_title('Accumulated Liquid Equivalent Freezing Rain (in)')
    ax13.set_title('Accumulated Rain (in)')
    ax10.set_extent((260, 295, 25, 50))#, crs = zH5_crs)    # Set a title and show the plot
    ax11.set_extent((260, 295, 25, 50))#, crs = zH5_crs)    # Set a title and show the plot
    ax12.set_extent((260, 295, 25, 50))#, crs = zH5_crs)    # Set a title and show the plot
    ax13.set_extent((260, 295, 25, 50))#, crs = zH5_crs)    # Set a title and show the plot
    fig6.tight_layout()
    plt.savefig(output_dir+'/HRRR_ex/ec_accum_ptype_'+dtfs+'.png',bbox_inches='tight',pad_inches=0.1)
    ax10.set_extent((281,295,39,49))#, crs = zH5_crs)    # Set a title and show the plot
    ax11.set_extent((281,295,39,49))#, crs = zH5_crs)    # Set a title and show the plot
    ax12.set_extent((281,295,39,49))#, crs = zH5_crs)    # Set a title and show the plot
    ax13.set_extent((281,295,39,49))#, crs = zH5_crs)    # Set a title and show the plot
    plt.savefig(output_dir+'/HRRR_ex/ne_accum_ptype_'+dtfs+'.png',bbox_inches='tight',pad_inches=0.1)
    ax10.set_extent((289,291,43,45))
    ax10.add_feature(USCOUNTIES.with_scale('5m'), edgecolor='dimgray')
    ax11.set_extent((289,291,43,45))
    ax11.add_feature(USCOUNTIES.with_scale('5m'), edgecolor='dimgray')
    ax12.set_extent((289,291,43,45))
    ax12.add_feature(USCOUNTIES.with_scale('5m'), edgecolor='dimgray')
    ax13.set_extent((289,291,43,45))
    ax13.add_feature(USCOUNTIES.with_scale('5m'), edgecolor='dimgray')
    plt.savefig(output_dir+'/HRRR_ex/local_accum_ptype_'+dtfs+'.png',bbox_inches='tight',pad_inches=0.1)
    plt.close()
    plt.clf()

    fig7 = plt.figure(figsize=(15,15))
    ax14 = fig7.add_subplot(111,projection=zH5_crs)

    ax14.coastlines(resolution='10m')
    ax14.add_feature(cfeature.BORDERS.with_scale('10m'))
    ax14.add_feature(cfeature.STATES.with_scale('10m'))

    try:
        zr = ax14.contourf(x,y,acc_fram,colors=qzr_cols,levels=qazlevs,alpha=0.7,extend='max')
    except:
        print('no ice')
    ax14.set_title('Total Ice Accretion (FRAM)')
    ax14.set_title('HRRR Init: '+init_time.dt.strftime('%m-%d %H:%MZ').item(),fontsize=14,loc='left')
    ax14.set_title('Valid: '+time.dt.strftime('%a %b %d %H:%MZ').item(),fontsize=14,loc='right')

    ax14.set_extent((260, 295, 25, 50))
    plt.savefig(output_dir+'/HRRR_ex/ec_accum_ice_'+dtfs+'.png',bbox_inches='tight',pad_inches=0.1)
    ax14.set_extent((281,295,39,49))
    plt.savefig(output_dir+'/HRRR_ex/ne_accum_ice_'+dtfs+'.png',bbox_inches='tight',pad_inches=0.1)
    ax14.set_extent((289,291,43,45))
    ax14.add_feature(USCOUNTIES.with_scale('5m'), edgecolor='dimgray')
    plt.savefig(output_dir+'/HRRR_ex/local_accum_ice_'+dtfs+'.png',bbox_inches='tight',pad_inches=0.1)
    plt.close()
    plt.clf()

    fig8 = plt.figure(figsize=(15,15))
    ax15 = fig8.add_subplot(111,projection=zH5_crs)

    ax15.coastlines(resolution='10m')
    ax15.add_feature(cfeature.BORDERS.with_scale('10m'))
    ax15.add_feature(cfeature.STATES.with_scale('10m'))

    visc1 = ax15.contourf(x,y,vis,levels=[0.00625,0.125,0.25,0.5,0.75,1,2,3,4,5],extend='min',colors=['#ffb3ff','magenta','deeppink','hotpink','mediumvioletred','crimson','orangered','darkorange','orange','gold'],alpha=0.7)
    cbr = fig.colorbar(visc1,orientation = 'horizontal', aspect = 80, ax = ax15, pad = 0.01,
                        extendrect=False, ticks = [0.00625,0.125,0.25,0.5,0.75,1,2,3,4,5], shrink=0.7)
    cbr.set_label('Surface Visibility (mi)')
    ax15.set_title('Surface Visibility')
    ax15.set_title('HRRR Init: '+init_time.dt.strftime('%m-%d %H:%MZ').item(),fontsize=14,loc='left')
    ax15.set_title('Valid: '+time.dt.strftime('%a %b %d %H:%MZ').item(),fontsize=14,loc='right')

    ax15.set_extent((260, 295, 25, 50))
    plt.savefig(output_dir+'/HRRR_ex/ec_vis_'+dtfs+'.png',bbox_inches='tight',pad_inches=0.1)
    ax15.set_extent((281,295,39,49))
    plt.savefig(output_dir+'/HRRR_ex/ne_vis_'+dtfs+'.png',bbox_inches='tight',pad_inches=0.1)
    ax15.set_extent((289,291,43,45))
    ax15.add_feature(USCOUNTIES.with_scale('5m'), edgecolor='dimgray')
    plt.savefig(output_dir+'/HRRR_ex/local_vis_'+dtfs+'.png',bbox_inches='tight',pad_inches=0.1)
    plt.close()
    plt.clf()

    fig9 = plt.figure(figsize=(15,15))
    ax16 = fig9.add_subplot(111,projection=zH5_crs)

    ax16.coastlines(resolution='10m')
    ax16.add_feature(cfeature.BORDERS.with_scale('10m'))
    ax16.add_feature(cfeature.STATES.with_scale('10m'))

    hgt_frz = ax16.contourf(x,y,hgt0c,cmap='rainbow',levels=range(0,10000,500),extend='max')
    cbar2 = fig9.colorbar(hgt_frz,orientation='vertical', pad = 0.01, aspect = 50, ax = ax16, extendrect=False)
    cbr.set_label('Freezing Level Height (ft)')

    ax16.set_title('Freezing Level Height')
    ax16.set_title('HRRR Init: '+init_time.dt.strftime('%m-%d %H:%MZ').item(),fontsize=14,loc='left')
    ax16.set_title('Valid: '+time.dt.strftime('%a %b %d %H:%MZ').item(),fontsize=14,loc='right')

    ax16.set_extent((260, 295, 25, 50))
    plt.savefig(output_dir+'/HRRR_ex/ec_frz_height_'+dtfs+'.png',bbox_inches='tight',pad_inches=0.1)
    ax16.set_extent((281,295,39,49))
    plt.savefig(output_dir+'/HRRR_ex/ne_frz_height_'+dtfs+'.png',bbox_inches='tight',pad_inches=0.1)
    ax16.set_extent((289,291,43,45))
    ax16.add_feature(USCOUNTIES.with_scale('5m'), edgecolor='dimgray')
    plt.savefig(output_dir+'/HRRR_ex/local_frz_height_'+dtfs+'.png',bbox_inches='tight',pad_inches=0.1)
    plt.close()
    plt.clf()

    fig10 = plt.figure(figsize=(15,15))
    ax17 = fig10.add_subplot(111,projection=zH5_crs)
    ax17.coastlines(resolution='10m')
    ax17.add_feature(cfeature.BORDERS.with_scale('10m'))
    ax17.add_feature(cfeature.STATES.with_scale('10m'))

    blizc = ax17.contourf(x,y,blizzard,colors=['orange','magenta'],levels=[2,3])
    ax17.barbs(x[wind_slice],y[wind_slice],u_10m[wind_slice,wind_slice],v_10m[wind_slice,wind_slice], length=6)
    h_contour = ax17.contour(x, y, mslpc, colors='dimgray', levels=range(940,1040,4),linewidths=2)
    h_contour.clabel(fontsize=14, colors='dimgray', inline=True, fmt='%i mb', rightside_up=True, use_clabeltext=True)

    orange = mpatches.Patch(color='orange', label='Almost')
    green = mpatches.Patch(color='white', label='Nope')
    purple = mpatches.Patch(color='magenta',label='Yep!')
    leg = ax17.legend(handles=[purple,orange,green],loc=4,framealpha=1)

    ax17.set_title('Blizzard?',fontsize=16)
    ax17.set_title('HRRR Init: '+init_time.dt.strftime('%m-%d %H:%MZ').item(),fontsize=14,loc='left')
    ax17.set_title('Valid: '+time.dt.strftime('%a %b %d %H:%MZ').item(),fontsize=14,loc='right')

    ax17.set_extent((260, 295, 25, 50))
    plt.savefig(output_dir+'/HRRR_ex/ec_bliz_2_'+dtfs+'.png',bbox_inches='tight',pad_inches=0.1)
    ax17.set_extent((281,295,39,49))
    ax17.barbs(x[wind_slice_ne],y[wind_slice_ne],u_10m[wind_slice_ne,wind_slice_ne],v_10m[wind_slice_ne,wind_slice_ne], length=6)
    plt.savefig(output_dir+'/HRRR_ex/ne_bliz_2_'+dtfs+'.png',bbox_inches='tight',pad_inches=0.1)
    ax17.set_extent((289,291,43,45))
    ax17.barbs(x[wsl],y[wsl],u_10m[wsl,wsl],v_10m[wsl,wsl], length=6)
    ax17.add_feature(USCOUNTIES.with_scale('5m'), edgecolor='dimgray')
    plt.savefig(output_dir+'/HRRR_ex/local_bliz_2_'+dtfs+'.png',bbox_inches='tight',pad_inches=0.1)
    plt.close()
    plt.clf()

    fig11 = plt.figure(figsize=(15,15))
    ax18 = fig11.add_subplot(111,projection=zH5_crs)
    ax18.coastlines(resolution='10m')
    ax18.add_feature(cfeature.BORDERS.with_scale('10m'))
    ax18.add_feature(cfeature.STATES.with_scale('10m'))

    ax18.set_title('Blizzard Ingredients',fontsize=16)
    ax18.set_title('HRRR Init: '+init_time.dt.strftime('%m-%d %H:%MZ').item(),fontsize=14,loc='left')
    ax18.set_title('Valid: '+time.dt.strftime('%a %b %d %H:%MZ').item(),fontsize=14,loc='right')

    swgc = ax18.contourf(x,y,swg, levels=range(0,60,5), cmap = 'hot')
    sw3g = ax18.contour(x,y,swg,levels=[30],colors=['lime'],linewidths=2)
    visc = ax18.contour(x,y,vis,levels=[0.25],colors=['violet'],linewidths=2)

    try:
        sn = ax18.contour(x,y,smoothedsnow,cmap='cool',levels=[15],linewidths=2)
    except:
        print('no snow')
    cbar4 = fig.colorbar(swgc, orientation='horizontal',pad=0.01,ax=ax18, aspect = 50, extendrect=False, ticks = np.arange(0,70,10))
    blue = mpatches.Patch(color='aqua',label='Snow')
    lime = mpatches.Patch(color='lime', label='35 mph Gusts')
    pink = mpatches.Patch(color='violet', label='1/4mi Visibility')
    leg = ax18.legend(handles=[blue,lime,pink],loc=4,title='Blizzard Ingredients',framealpha=1)
    cbar4.set_label('Surface Wind Gust (kts)')

    ax18.set_extent((260, 295, 25, 50))
    plt.savefig(output_dir+'/HRRR_ex/ec_bliz_ing_'+dtfs+'.png',bbox_inches='tight',pad_inches=0.1)
    ax18.set_extent((281,295,39,49))
    ax18.barbs(x[wind_slice_ne],y[wind_slice_ne],u_10m[wind_slice_ne,wind_slice_ne],v_10m[wind_slice_ne,wind_slice_ne], length=6)
    plt.savefig(output_dir+'/HRRR_ex/ne_bliz_ing_'+dtfs+'.png',bbox_inches='tight',pad_inches=0.1)
    ax18.set_extent((289,291,43,45))
    ax18.barbs(x[wsl],y[wsl],u_10m[wsl,wsl],v_10m[wsl,wsl], length=6)
    ax18.add_feature(USCOUNTIES.with_scale('5m'), edgecolor='dimgray')
    plt.savefig(output_dir+'/HRRR_ex/local_bliz_ing_'+dtfs+'.png',bbox_inches='tight',pad_inches=0.1)
    plt.close()
    plt.clf()
    '''
    print(str(i)+'_Done!')


# (The MIT License)
#
# Copyright (c) 2016 Kura
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""Provides control functionality, including socket wrappers."""

import grp
import logging
import os
import pwd
import socket
try:
    import ssl
except ImportError:
    ssl = None

from .config import Config
from .exceptions import BlackholeRuntimeException

__all__ = ('pid_permissions', 'server', 'setgid', 'setuid')

logger = logging.getLogger('blackhole.control')
ciphers = ['ECDHE-ECDSA-AES256-GCM-SHA384', 'ECDHE-RSA-AES256-GCM-SHA384',
           'ECDHE-ECDSA-CHACHA20-POLY1305', 'ECDHE-RSA-CHACHA20-POLY1305',
           'ECDHE-ECDSA-AES128-GCM-SHA256', 'ECDHE-RSA-AES128-GCM-SHA256',
           'ECDHE-ECDSA-AES256-SHA384', 'ECDHE-RSA-AES256-SHA384',
           'ECDHE-ECDSA-AES128-SHA256', 'ECDHE-RSA-AES128-SHA256']

def _context(use_tls=False):
    """
    Create a TLS context using the certificate, key and dhparams file.

    :param use_tls: Whether to create a TLS context or not.
    :type use_tls: :any:`bool`
    :returns: A TLS context or none.
    :rtype: :any:`ssl.SSLContext` or :any:`None`.

    .. note::

       Created with:

       - :any:`ssl.OP_NO_SSLv2`
       - :any:`ssl.OP_NO_SSLv3`
       - :any:`ssl.OP_NO_COMPRESSION`
       - :any:`ssl.OP_CIPHER_SERVER_PREFERENCE`

       Also responsible for loading Diffie Hellman ephemeral parameters if
       they're provided -- :any:`ssl.SSLContext.load_dh_params`

       If the ``-ls`` or ``--less-secure`` option is provided,
       :any:`ssl.OP_SINGLE_DH_USE` and :any:`ssl.OP_SINGLE_ECDH_USE` will be
       omitted from the context. --
       https://blackhole.io/command-line-options.html#command-line-options --
       added in :ref:`2.0.13`
    """
    if use_tls is False:
        return None
    config = Config()
    ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
    ctx.load_cert_chain(config.tls_cert, config.tls_key)
    ctx.options |= ssl.OP_NO_SSLv2
    ctx.options |= ssl.OP_NO_SSLv3
    ctx.options |= ssl.OP_NO_COMPRESSION
    ctx.options |= ssl.OP_CIPHER_SERVER_PREFERENCE
    if not config.args.less_secure:
        ctx.options |= ssl.OP_SINGLE_DH_USE
        ctx.options |= ssl.OP_SINGLE_ECDH_USE
    ctx.set_ciphers(':'.join(ciphers))
    if config.tls_dhparams:
        ctx.load_dh_params(config.tls_dhparams)
    return ctx

def _socket(addr, port, family):
    """
    Create a socket, bind and listen.

    :param addr: The address to use.
    :type addr: :any:`str`
    :param port: The port to use.
    :type port: :any:`int`
    :param family: The type of socket to use.
    :type family: :any:`socket.AF_INET` or :any:`socket.AF_INET6`.
    :returns: A bound socket.
    :rtype: :any:`socket.socket`
    :raises: :any:`blackhole.exceptions.BlackholeRuntimeException`
    """
    sock = socket.socket(family, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    try:
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
    except (AttributeError, OSError):
        pass
    if family == socket.AF_INET6:
        sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
    try:
        sock.bind((addr, port))
    except OSError:
        msg = 'Cannot bind to {}:{}.'.format(addr, port)
        logger.fatal(msg)
        sock.close()
        raise BlackholeRuntimeException(msg)
    os.set_inheritable(sock.fileno(), True)
    sock.listen(1024)
    sock.setblocking(False)
    return sock

def server(addr, port, family, flags={}, use_tls=False):
    """
    A socket and possibly a TLS context.

    Create an instance of :any:`socket.socket`, bind it and return a dictionary
    containing the socket object and a TLS context if configured.

    :param addr: The address to use.
    :type addr: :any:`str`
    :param port: The port to use.
    :type port: :any:`int`
    :param family: The type of socket to use.
    :type family: :any:`socket.AF_INET` or :any:`socket.AF_INET6`.
    :param flags: Flags to use.
    :type flags: :any:`dict`. Default: {}
    :param use_tls: Whether to create a TLS context or not.
    :type use_tls: :any:`bool`
    :returns: A bound socket, a TLS context if configured and any configured
              flags.
    :rtype: :any:`dict`
    """
    sock = _socket(addr, port, family)
    ctx = _context(use_tls=use_tls)
    return {'sock': sock, 'ssl': ctx, 'flags': flags}

def pid_permissions():
    """
    Change the pid file ownership.

    Called before :any:`blackhole.control.setgid` and
    :any:`blackhole.control.setuid` are called to stop
    :any:`blackhole.daemon.Daemon` losing permissions to modify the pid file.

    :raises: :any:`SystemExit` -- :any:`os.EX_USAGE`
    """
    config = Config()
    try:
        user = pwd.getpwnam(config.user)
        group = grp.getgrnam(config.group)
        os.chown(config.pidfile, user.pw_uid, group.gr_gid)
    except (KeyError, PermissionError):
        logger.error('Unable to change pidfile ownership permissions.')
        raise SystemExit(os.EX_USAGE)

def setgid():
    """
    Change group.

    Change to a less privileged group. Unless you're using it incorrectly --
    in which case, don't use it.
    :raises: :any:`SystemExit` -- :any:`os.EX_USAGE` or :any:`os.EX_NOPERM`

    .. note::

       MUST be called BEFORE setuid, not after.
    """
    config = Config()
    try:
        gid = grp.getgrnam(config.group).gr_gid
        os.setgid(gid)
    except KeyError:
        logger.error('Group \'%s\' does not exist.', config.group)
        raise SystemExit(os.EX_USAGE)
    except PermissionError:
        logger.error('You do not have permission to switch to group \'%s\'.',
                     config.group)
        raise SystemExit(os.EX_NOPERM)

def setuid():
    """
    Change user.

    Change to a less privileged user.Unless you're using it incorrectly --
    inwhich case, don't use it.
    :raises: :any:`SystemExit` -- :any:`os.EX_USAGE` or :any:`os.EX_NOPERM`

    .. note::

       MUST be called AFTER setgid, not before.
    """
    config = Config()
    try:
        uid = pwd.getpwnam(config.user).pw_uid
        os.setuid(uid)
    except KeyError:
        logger.error('User \'%s\' does not exist.', config.user)
        raise SystemExit(os.EX_USAGE)
    except PermissionError:
        logger.error('You do not have permission to switch to user \'%s\'.',
                     config.user)
        raise SystemExit(os.EX_NOPERM)


def main():
    from mkinit import static_mkinit
    # from os.path import exists, join, isdir
    import textwrap
    import os
    import six
    import argparse
    description = textwrap.dedent(
        '''
        discover and run doctests within a python package
        ''').strip('\n')

    parser = argparse.ArgumentParser(prog='python -m mkinit', description=description)
    parser.add_argument('modname_or_path', nargs='?', help='module or path to generate __init__.py for', default='.')
    parser.add_argument('--dry', action='store_true', default=False)

    parser.add_argument('--noattrs', action='store_true', default=False,
                        help='Do not generate attribute from imports')
    parser.add_argument('--nomods', action='store_true', default=False,
                        help='Do not generate modules imports')
    parser.add_argument('--noall', action='store_true', default=False,
                        help='Do not generate an __all__ variable')

    parser.add_argument('--relative', action='store_true', default=False,
                        help='Use relative . imports instead of <modname>')

    parser.add_argument('--ignore_all', action='store_true', default=False,
                        help='Ignores __all__ variables when parsing')

    args, unknown = parser.parse_known_args()
    ns = args.__dict__.copy()

    # modpath_or_name = sys.argv[1]
    print('ns = {!r}'.format(ns))
    modname_or_path = ns['modname_or_path']

    def touch(fpath):
        if six.PY2:  # nocover
            with open(fpath, 'a'):
                os.utime(fpath, None)
        else:
            flags = os.O_CREAT | os.O_APPEND
            with os.fdopen(os.open(fpath, flags=flags, mode=0o666)) as f:
                os.utime(f.fileno() if os.utime in os.supports_fd else fpath)

    # Hack in the ability to handle the case where __init__ does not exist yet
    # if isdir(modname_or_path) and not exists(join(modname_or_path, '__init__.py')):
    #     touch(join(modname_or_path, '__init__.py'))

    use_all = not ns['ignore_all']

    # Formatting options
    options = {
        'with_attrs': not ns['noattrs'],
        'with_mods': not ns['nomods'],
        'with_all': not ns['noall'],
        'relative': ns['relative'],
    }
    static_mkinit.autogen_init(modname_or_path, use_all=use_all,
                               options=options, dry=ns['dry'])

if __name__ == '__main__':
    main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 20 16:42:12 2019

@author: mitja
"""

import numpy as np
import matplotlib.pyplot as plt

input_data=np.loadtxt('gaia_tri_03_results.csv',dtype='str',skiprows=1,delimiter=',',usecols=(0,3,8,9,10,11,12,13,14,15,16,17,19))

name,sourceid,m1,m2,m3,m4,m5,m6,m7,m8,m9,ot,publish_name=input_data.T

unique_sourceid=np.unique(sourceid)

m1=m1.astype(float)
m2=m2.astype(float)
m3=m3.astype(float)
m4=m4.astype(float)
m5=m5.astype(float)
m6=m6.astype(float)
m7=m7.astype(float)
m8=m8.astype(float)
m9=m9.astype(float)

ot=ot.astype(float)

mag=np.zeros(len(input_data))

for i in range(len(input_data)):
    mag[i]=np.nanmedian([m1[i],m2[i],m3[i],m4[i],m5[i],m6[i],m7[i],m8[i],m9[i]])

idx=[]
for j in range(len(unique_sourceid)):
    idx.append(np.where(sourceid==unique_sourceid[j])[0])

for i in range(len(unique_sourceid)):
    plt.figure()
    plt.plot(ot[np.min(np.where(sourceid==unique_sourceid[i])[0]):np.max(np.where(sourceid==unique_sourceid[i])[0])+1]-2455000,mag[np.min(np.where(sourceid==unique_sourceid[i])[0]):np.max(np.where(sourceid==unique_sourceid[i])[0])+1],'b.')
    plt.gca().invert_yaxis()
    plt.xlabel('time[JD-2455000]')
    plt.ylabel('mag')
    plt.title('Publish name: %s'%publish_name[np.where(sourceid==unique_sourceid[i])[0][0]])
    plt.show()


import os, time, threading, logging, json, requests
print("=========================================================")
print(" _              ___ _ _                                  ")
print("(_)            / __) (_)                                 ")
print(" _ ____   ____| |__| |_ ____     ____   ____ ____   ____ ")
print("| |    \ / _  |  __) | |  _ \   |    \ / _  )    \ / _  )")
print("| | | | ( ( | | |  | | | | | |  | | | ( (/ /| | | ( (/ / ")
print("|_|_|_|_|\_|| |_|  |_|_| ||_/   |_|_|_|\____)_|_|_|\____)")
print("        (_____|        |_|                               ")
print("=========================================================")
try:
    how_much = int(input("[Default 100]How Much :"))
except ValueError:
    how_much = 100

api_url_imgflip = 'https://api.imgflip.com/get_memes'
datas = ''
format = "%(asctime)s: %(message)s"
logging.basicConfig(format=format, level=logging.INFO,datefmt="%H:%M:%S")

logging.info("[Information] Check if data.json is exists")
if os.path.exists('data.json'):
    logging.info("[Information] Found data.json")
    with open('data.json','r') as load:
        datas = json.load(load)
    logging.info("[Information] Data Loaded")
else:
    try:
        r = requests.get(api_url_imgflip,timeout=30)
        logging.info("[Information] Awaiting Information From API Endpoint")
        datas = json.loads(r.content.decode('utf-8'))
        with open('data.json','w', encoding='utf-8') as save:
            json.dump(datas,save,ensure_ascii=False,indent=4)
        logging.info("[Information] Data Loaded")
    except Exception:
        logging.error("[ERROR] Error Parsing Data")

hasil = datas['success']

i = 1

try:
    if hasil == True:
        logging.info("[Information] Getting information from API endpoint")
        for hasil in datas['data']['memes']:
            ID = hasil['id']
            name = hasil['name']
            url = hasil['url']
            width = hasil['width']
            height = hasil['height']
            box_count = hasil['box_count']
            #filename = '{}.jpg'.format(ID)
            filename = '{0} {1} {2}x{3}.jpg'.format(i,name,width,height)
            #logging.info("[Information] Get Image Url from API")
            Picture_request = requests.get(url)
            if Picture_request.status_code == 200:
                #logging.info("[Information] Image Loaded")
                with open(filename,'wb') as savePic:
                    savePic.write(Picture_request.content)
                    logging.warn("[Information] {} Image Downloaded".format(i))
                    i += 1
                    if i-1 >= how_much:
                        break
            else:
                logging.error("[ERROR] Request Failed")
                print("Exiting...")
                time.sleep(1)
                exit()
    else:
        logging.error("[ERROR] Cannot Parse Data from Api")
        #print(datas['success'])
        print("Some hopefully-useful statement about why it failed : ")
        print(datas['error_message'])
except Exception as e:
    print(e)


# Importing Required libraries
import pandas as pd
import numpy as np
import pickle
import cv2
from sklearn.model_selection import train_test_split
import warnings
warnings.filterwarnings("ignore")
from IPython.core.debugger import set_trace

def random_flip(image, steering_angle):
    """
    Function to Flip the images Horizontally randomly
    Flipped images will be assigned with the Negative steering angle mentioning the network in which direction curve associated with.

    """
    if np.random.rand() < 0.5:
        image = cv2.flip(image,1)
        steering_angle = -steering_angle
    return image, steering_angle

def random_shadow(image):
    """
    Function to add shadow in images randomly at random places, Random shadows meant to make the Convolution model learn
    Lanes and lane curvature patterns effectively in dissimilar places.
    """
    if np.random.rand() < 0.5:
        # (x1, y1) and (x2, y2) forms a line
        # xm, ym gives all the locations of the image
        x1, y1 = image.shape[1] * np.random.rand(), 0
        x2, y2 = image.shape[1] * np.random.rand(), image.shape[0]
        xm, ym = np.mgrid[0:image.shape[0], 0:image.shape[1]]

        mask = np.zeros_like(image[:, :, 1])
        mask[(ym - y1) * (x2 - x1) - (y2 - y1) * (xm - x1) > 0] = 1

        # choose which side should have shadow and adjust saturation
        cond = mask == np.random.randint(2)
        s_ratio = np.random.uniform(low=0.2, high=0.5)

        # adjust Saturation in HLS(Hue, Light, Saturation)
        hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS)
        hls[:, :, 1][cond] = hls[:, :, 1][cond] * s_ratio
        return cv2.cvtColor(hls, cv2.COLOR_HLS2RGB)
    else:
        return image

def random_brightness(image):
    """
    Function to add random brightness to random images. Random brightness will allow the model to learn and coverge on lane lines
    and lane curvatures even in low light conditions.

    """
    if np.random.rand() < 0.5:
        # HSV (Hue, Saturation, Value) is also called HSB ('B' for Brightness).
        image = np.array(image, dtype=np.uint8)
        hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
        ratio = 1.0 + 0.4 * (np.random.rand() - 0.5)
        hsv[:,:,2] =  hsv[:,:,2] * ratio
        return cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)
    else:
        return image

def Image_Preprocessing(Drivinglog):
    '''
        Function to read each image in a directory and making them as a generalized array to feed Neural Network.
        Preprocessing of images included with 5 steps
            1. Reading all training images and the driving log csv to create Train features and predicting variable data.
            2. Translating the images with the use Random flipping and assigning a steering angle negatively for each flipped image.
            3. Randomly adding shadow to each image
            4. Concatenating all augmented data
            5. Splitting the data to train, test and valid randomly

    '''
    #1. Reading all training images and the driving log csv to create Train features and predicting variable data.
    CenterImages = np.concatenate([cv2.cvtColor(cv2.imread(load_dir +file), cv2.COLOR_BGR2RGB)[np.newaxis] for file in Drivinglog[Drivinglog.columns[0]].tolist()])
#     a = Drivinglog[Drivinglog.columns[3]].astype(float)
  #   y_data = (a + ((((~a.between(-0.05,0.01))  & (a.gt(0.01))).astype(int) * 0.05) + (((~a.between(-0.05,0.01))  & (a.lt(-0.01))).astype(int) * -0.05))).values[:,None]
    y_data = Drivinglog[Drivinglog.columns[3]].astype(float).values[:,None]

    #FlipedCenterImages = np.concatenate([cv2.flip(image,1)[np.newaxis] for image in CenterImages])
    print('Translating the Image data  ... ')
    # 2. Translating the images with the use Random flipping and assigning a steering angle negatively for each flipped image.
    val = [random_flip(image,steering_angle) for image, steering_angle in zip(CenterImages,y_data)]
    TranslatedImages = np.concatenate([i[0][np.newaxis] for i in val])
    TranslatedSangle = np.array([i[1] for i in val])
    print('Adding shadow to image data. ...')

    # 3. Randomly adding shadow to each image
    RBrightnesImages = np.concatenate([random_brightness(image)[np.newaxis] for image in CenterImages])
    print('Changing Brightness of image data ... ')

    # 4. Concatenating all augmented data
#     X_data = np.concatenate((CenterImages, TranslatedImages, RBrightnesImages))
#     y_data = np.concatenate((y_data, TranslatedSangle, y_data))
    X_data = CenterImages #RBrightnesImages
    y_data = y_data

    # 5. Splitting the data to train, test and valid randomly
    x_train, x_test, y_train, y_test = train_test_split(X_data, y_data, test_size=0.1, random_state=42)
    x_train, x_valid, y_train, y_valid = train_test_split(x_train, y_train, test_size=0.1, random_state=42)

    print('Train data shape ', X_data.shape)

    with open('/opt/ProcessedData.data','wb') as file:
        pickle.dump([x_test,y_test],file)
        pickle.dump([x_valid,y_valid],file)
        pickle.dump([x_train,y_train],file)
        file.close()

load_dir = '../Data/SampleData/data/'
Drivinglog = pd.read_csv(load_dir + 'driving_log.csv')
Drivinglog = pd.concat([Drivinglog[Drivinglog.steering.ne(0)],Drivinglog[Drivinglog.steering.between(-0.01,0.01)][:300]]).reset_index(drop=True)
Image_Preprocessing(Drivinglog)


from sorttest import test
import random
import time

def insert(nums):

    length = len(nums)

    for i in xrange(1, length):
        key = nums[i]
        j = i - 1
        while j >= 0 and nums[j]  > key:
            nums[j + 1] = nums[j]
            j = j - 1

        nums[j+1] = key

    return nums

if __name__ == "__main__":

    res = []
    start = time.time()
    for _ in xrange(1000):
        size = random.randint(1,100)
        nums = [random.randint(-10,20) for _ in xrange(size)]
        #print(nums)
        insert(nums)
        #print(nums)
        res.append(test(nums))

    end = time.time()
    print("%s Pass."%(res.count(True)))
    print("%s Fail."%(res.count(False)))
    print("Time :  %s"%(end-start))


# Copyright 2013 Devsim LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import devsim
import test_common

device='MyDevice'
regions=('MySi1', 'MySi2')
interface='MyInt'
contacts=('top', 'bot')

test_common.CreateSimpleMeshWithInterface(device=device, region0=regions[0], region1=regions[1], interface=interface)

for region in regions:
    test_common.SetupResistorConstants(device, region)
    test_common.SetupInitialResistorSystem(device, region, net_doping=1e16)

for contact in contacts:
    test_common.SetupInitialResistorContact(device, contact=contact)

test_common.SetupContinuousPotentialAtInterface(device, interface)

#####
##### Initial DC Solution
#####
devsim.set_parameter(name="topbias", value=0.0)
devsim.set_parameter(name="botbias", value=0.0)
devsim.solve(type='dc', absolute_error=1.0, relative_error=1e-10, maximum_iterations=30)

for region in regions:
    for name in ("Potential", "IntrinsicElectrons"):
        devsim.print_node_values(device=device, region=region, name=name)

for region in regions:
    test_common.SetupCarrierResistorSystem(device, region)

for contact in contacts:
    test_common.SetupCarrierResistorContact(device, contact=contact)

test_common.SetupContinuousElectronsAtInterface(device, interface)

for v in (0.0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.10):
    devsim.set_parameter(name="topbias", value=v)
    devsim.solve(type="dc", absolute_error=1.0, relative_error=1e-10, maximum_iterations=30)

    test_common.printResistorCurrent(device=device, contact='top')
    test_common.printResistorCurrent(device=device, contact='bot')

for region in regions:
    devsim.print_node_values(device=device, region=region, name="Electrons")
    devsim.print_node_values(device=device, region=region, name="Potential")
    devsim.print_edge_values(device=device, region=region, name="ElectricField")
    devsim.print_edge_values(device=device, region=region, name="ElectronCurrent")

l=1.0
q=1.6e-19
n=1.0e16
u=400
R=l/(q*n*u)
print(0.1/R)


from StringIO import StringIO

from Products.CMFCore.utils import getToolByName

from Products.PluggableAuthService.interfaces.plugins \
    import IGroupsPlugin
from Products.PluggableAuthService.interfaces.plugins \
    import IPropertiesPlugin
from Products.PluggableAuthService.interfaces.plugins \
    import IUserFactoryPlugin

from Products.PlonePAS.Extensions.Install import activatePluginInterfaces

from interfaces import IUserAdder
from config import TOOLNAME
from config import USE_COLLECTIVE_INDEXING

def _doRegisterUserAdderUtility(context, step_name, profile_id,
                                utility_name, utility):
    """ registers utility for adding ExampleMembers """
    portal = context.getSite()

    sm = portal.getSiteManager()
    logger = context.getLogger(step_name)
    if sm.queryUtility(IUserAdder, name=utility_name) is None:
        sm.registerUtility(
            provided=IUserAdder,
            component=utility,
            name=utility_name)
        logger.info("Registered IUserAdder utility: %s" %
                    utility_name)
        mbtool = getToolByName(portal, TOOLNAME)
        if not mbtool.user_adder:
            # we become the default if one isn't already specified
            mbtool.user_adder = utility_name
    else:
        logger.info("IUserAdder utility '%s' already registered" %
                    utility_name)

def _setupPlugins(portal, out):
    """
    Install and prioritize the membrane PAS plug-ins.
    """
    uf = getToolByName(portal, 'acl_users')
    plugins = uf.plugins

    membrane = uf.manage_addProduct['membrane']
    existing = uf.objectIds()

    if 'membrane_users' not in existing:
        membrane.addMembraneUserManager('membrane_users')
        print >> out, "Added User Manager."
        activatePluginInterfaces(portal, 'membrane_users', out)

    if 'membrane_groups' not in existing:
        membrane.addMembraneGroupManager('membrane_groups')
        print >> out, "Added Group Manager."
        activatePluginInterfaces(portal, 'membrane_groups', out)
        plugins.movePluginsUp(IGroupsPlugin, ['membrane_groups'])

    if 'membrane_roles' not in existing:
        membrane.addMembraneRoleManager('membrane_roles')
        print >> out, "Added Role Manager."
        activatePluginInterfaces(portal, 'membrane_roles', out)

    if 'membrane_properties' not in existing:
        membrane.addMembranePropertyManager('membrane_properties')
        print >> out, "Added Property Manager."
        activatePluginInterfaces(portal, 'membrane_properties', out)
        plugins.movePluginsUp(IPropertiesPlugin, ['membrane_properties'])
        plugins.movePluginsUp(IPropertiesPlugin, ['membrane_properties'])

    if 'membrane_user_factory' not in existing:
        membrane.addMembraneUserFactory('membrane_user_factory')
        print >> out, "Added User Factory."
        activatePluginInterfaces(portal, 'membrane_user_factory', out)
        plugins.movePluginsUp(IUserFactoryPlugin, ['membrane_user_factory'])

def setupPlugins(context):
    """ initialize membrane plugins """
    if context.readDataFile('membrane-setup-plugins.txt') is None:
        return

    portal = context.getSite()
    out = StringIO()
    if USE_COLLECTIVE_INDEXING:
        setup_tool = getToolByName(portal, 'portal_setup')
        try:
            setup_tool.runAllImportStepsFromProfile(
                'profile-collective.indexing:default')
        except KeyError:
            # collective.indexing 2.0 has no install and needs no install.
            pass
    _setupPlugins(portal, out)
    logger = context.getLogger("plugins")
    logger.info(out.getvalue())


import math
f = open('file2.txt','r',encoding='utf-8')
m=f.read()
emp = []
tot=0
for i in range(256):
	emp.append(0)
for x in m:
	z=ord(x)
	if z<256:
		tot+=1
		emp[z]+=1
num=0
entropy=0.0
for i in range(256):
	if emp[i]!=0:
		num+=1
		emp[i]=emp[i]/tot
		entropy-=emp[i]*math.log(emp[i],2)
print(num)
print(entropy)

opt=tot*entropy/8
print(opt)


import torch.nn as nn
from torch.nn import ReLU
import math
from dynnet.layers import (
    Input, Linear, BatchNorm, Flatten, CapNorm, Conv2d, Sum)
from dynnet.graph import Graph
from dynnet.filters import SmoothFilter

RESNET_CFGS = {
    50: [3, 4, 6, 3],
    101: [3, 4, 23, 3],
    152: [3, 8, 36, 3]
}

EXPANSION = 4

def block(graph, inp, planes, downsample, stride=1):
    conv1 = graph.add(Conv2d, out_channels=planes, kernel_size=1,
                      bias=False)(inp)
    bn1 = graph.add(BatchNorm)(conv1)
    filt1 = graph.add(SmoothFilter)(bn1)
    conv2 = graph.add(Conv2d, out_channels=planes, kernel_size=3,
                      stride=stride, padding=1, bias=False)(filt1)
    bn2 = graph.add(BatchNorm)(conv2)
    filt2 = graph.add(SmoothFilter)(bn2)
    conv3 = graph.add(Conv2d, out_channels=planes * EXPANSION,
                      kernel_size=1, bias=False)(filt2)
    bn3 = graph.add(BatchNorm)(conv3)
    ss = graph.add(Sum)(bn3, downsample)
    return graph.add(nn.ReLU, inplace=True)(ss)

class ResNet(nn.Module):

    def __init__(self, params):
        super(ResNet, self).__init__()
        num_classes = params['num_classes']
        layers = RESNET_CFGS[int(params['config'])]
        factor = int(params['factor'])
        self.graph = Graph()
        self.inplanes = 64 * factor
        self.inp = self.graph.add(Input, 3, 224, 224)()
        conv1 = self.graph.add(Conv2d, out_channels=self.inplanes,
                               kernel_size=7, stride=2, padding=3,
                               bias=False)(self.inp)
        filt1 = self.graph.add(SmoothFilter)(conv1)
        bn1 = self.graph.add(BatchNorm)(filt1)
        relu = self.graph.add(ReLU, inplace=True)(bn1)
        pool = self.graph.add(nn.MaxPool2d, kernel_size=3,
                              stride=2, padding=1)(relu)
        layer1 = self._make_layer(pool, 64 * factor, layers[0])
        layer2 = self._make_layer(layer1, 128 * factor, layers[1], stride=2)
        layer3 = self._make_layer(layer2, 256 * factor, layers[2], stride=2)
        layer4 = self._make_layer(layer3, 512 * factor, layers[3], stride=2)
        avgpool = self.graph.add(nn.AvgPool2d, 7, stride=1)(layer4)
        flat = self.graph.add(Flatten)(avgpool)
        self.result = self.graph.add(Linear, out_features=num_classes)(flat)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

    def _make_layer(self, inp, planes, blocks, stride=1):
        def down_sampler(x):
            if stride != 1 or self.inplanes != planes * EXPANSION:
                downsample = self.graph.add(Conv2d,
                                            out_channels=planes * EXPANSION,
                                            kernel_size=1, stride=stride,
                                            bias=False)(x)
                return self.graph.add(BatchNorm)(downsample)
            else:
                return x

        inp = block(self.graph, inp, planes, down_sampler(inp), stride)
        self.inplanes = planes * EXPANSION
        for i in range(1, blocks):
            inp = block(self.graph, inp, planes, inp)
        return inp

    def forward(self, x):
        return self.graph({self.inp: x}, self.result)[0]


import csv
import yaml

cutflows={}
with open("nevents.list") as f:
  reader = csv.reader(f, delimiter=":")
  c = list(reader)
f.close()

for cc in c:
  cutflows[cc[0]] = cc[1]

def write_new_weights(sample):
  filename = sample + '.json'
  weights = yaml.load(file(filename))
  for k in weights.keys():
    tag = weights[k].keys()[0]
    try:
      weights[k][tag]['num events'] = cutflows[k]
    except:
      weights[k][tag]['num events'] = 0

  with open(sample+'.yml','w+') as outfile:
    outfile.write(yaml.dump(weights,default_flow_style=False))
  outfile.close()

write_new_weights('ttbar')