<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 APS - new snow
Imports
Step1:  Parameters, categories and scores
Main control factors
Step2:  Weighting
Weights are added if they are independent of the value of the core factor or multiplied if they are related to the core factor.
Step3:  The new_snow_24_72h_scores are used to weight the new_snow_24h_scores prior to multiplying it with wind_speed_score.
In order to achive a smooth fit within the range of interest I added some control points just right outside the normal range for the higher order polynomials.
The temperature evolution during a snowfall can be fitted to a curve which can then be compared to predefined curves/scenarios. The scenario with the best correlation is chosen to define the category.
The type_new_snow_cat can be infered from evolution_temperature and wind_speed.
In the first place the categories can be set manually.
Score functions
New snow 24 h
Step4:  New snow 24-72 h
Step5:  Wind speed
Step6:  New snow vs. wind speed
Step7:  ToDo
retrieve relevant data from a weather station / grid point (e.g. Hallingdal/Hemsedal Holtø in mid-late January)
calculate new_snow_score for some weeks
compare to chosen AP in regional forecast
maybe extent to a larger grid
...continue with hemsedal_jan2016.py in Test
Random scripting testing
Step8:  the dict is not sorted and the comparison less than is random...
<ASSISTANT_TASK:>
Python Code:
# -*- coding: utf-8 -*-
%matplotlib inline
from __future__ import print_function
import pylab as plt
import datetime
import numpy as np
plt.rcParams['figure.figsize'] = (14, 6)
Explanation: APS - new snow
Imports
End of explanation
# New snow amount last 24 h 0-60 cm [10 cm intervals]
new_snow_24h_cat = np.array([0, 10, 20, 30, 40, 50, 60])
new_snow_24h_score = np.array([0.1, 0.5, 1.1, 1.3, 1.4, 1.8, 2.0])
# Wind speed 0-100 km/h [0,10,20,30,40,50,60,80,100]
wind_speed_km_cat = np.array([-5, 0, 10, 20, 30, 40, 50, 60, 80, 100, 150])
wind_speed_cat = wind_speed_km_cat / 3.6 # m/s
wind_speed_score = np.array([0.0, 1.8, 2.8, 3.3, 2.6, 1.2, 0.6, 0.3, 0.15, 0.07, 0.0])
Explanation: Parameters, categories and scores
Main control factors
End of explanation
# New snow amount last 24-72h 0-100 cm [0,10,20,30,40,50,60,80,100]
new_snow_24_72h_cat = np.array([0, 10, 20, 30, 40, 50, 60, 80, 100])
new_snow_24_72h_score = np.array([0.8, 1.0, 1.1, 1.2, 1.4, 1.6, 1.8, 2.1, 2.5]) # a weight for new_snow_24h
# Evolution of temperature
evolution_temperature_cat = ["constant very cold", "constant cold", "constant warm", "rise towards 0 deg after snowfall", "substantial cooling after snowfall"]
# Bonding to existing snowpack 
bonding_existing_snowpack_cat = ["favorable", "moderate", "poor"]
# Type of new snow 
type_new_snow_cat = ["loose-powder", "soft", "packed", "packed and moist"]
Explanation: Weighting
Weights are added if they are independent of the value of the core factor or multiplied if they are related to the core factor.
End of explanation
new_snow_24h_fit = np.polyfit(new_snow_24h_cat, new_snow_24h_score, 2)
score_new_snow_24h = np.poly1d(new_snow_24h_fit)
x = np.arange(0, 60.0)
res = score_new_snow_24h(x)
plt.scatter(new_snow_24h_cat, new_snow_24h_score)
plt.plot(x, res)
Explanation: The new_snow_24_72h_scores are used to weight the new_snow_24h_scores prior to multiplying it with wind_speed_score.
In order to achive a smooth fit within the range of interest I added some control points just right outside the normal range for the higher order polynomials.
The temperature evolution during a snowfall can be fitted to a curve which can then be compared to predefined curves/scenarios. The scenario with the best correlation is chosen to define the category.
The type_new_snow_cat can be infered from evolution_temperature and wind_speed.
In the first place the categories can be set manually.
Score functions
New snow 24 h
End of explanation
new_snow_24_72h_fit = np.polyfit(new_snow_24_72h_cat, new_snow_24_72h_score, 1)
score_new_snow_24_72h = np.poly1d(new_snow_24_72h_fit)
x = np.arange(0, 100.0)
res = score_new_snow_24_72h(x)
plt.scatter(new_snow_24_72h_cat, new_snow_24_72h_score)
plt.plot(x, res)
Explanation: New snow 24-72 h
End of explanation
wind_speed_fit = np.polyfit(wind_speed_cat, wind_speed_score, 5)
score_wind_speed = np.poly1d(wind_speed_fit)
x = np.arange(-5, 150.0 / 3.6)
res = score_wind_speed(x)
plt.scatter(wind_speed_cat, wind_speed_score)
plt.plot(x, res)
Explanation: Wind speed
End of explanation
new_snow = np.matrix(np.arange(0, 60.0))
sns = score_new_snow_24h(new_snow)
# weighted by new snow amount of the previous two days
new_snow_72 = 40
ns_weight = score_new_snow_24_72h(new_snow_72)
sns *= ns_weight
wind_speed = np.matrix(np.arange(0, 100.0 / 3.6))
swp = score_wind_speed(wind_speed)
M = np.multiply(sns, swp.T)
#print(M)
plt.contourf(M)#np.flipud(M.T))
print("Min {0}; Max {1}".format(np.amin(M), np.amax(M)))
plt.colorbar()
plt.xlabel("New snow last 24h [cm]")
plt.ylabel("Wind speed [m/s]")
Explanation: New snow vs. wind speed
End of explanation
new_snow_cat = ["0-5", "5-10", "10-15", "15-20"]
new_snow_thres = {(0, 5): 0.2, (5, 10): 0.5, (10, 15): 1, (15, 20): 3}
wind_cat = ["0-3", "4-7", "8-10", "10-15", "16-30"]
wind_thres = {(0, 3): 0.2, (3, 7): 1, (7, 10): 2, (10, 15): 0.2, (15, 30): 0.01}
new_snow_region = np.array([[0, 4, 6, 18],
                             [0, 4, 6, 18],
                             [0, 4, 6, 18]])
wind_region = np.array([[0, 4, 12, 18],
                         [4, 0, 18, 6],
                         [18, 12, 6, 0]])
def get_score(a, score_dict):
    for key, value in score_dict.items():
        if key[0] <= a < key[1]:
    #    if a < key:
            return value
            break
    return None
Explanation: ToDo
retrieve relevant data from a weather station / grid point (e.g. Hallingdal/Hemsedal Holtø in mid-late January)
calculate new_snow_score for some weeks
compare to chosen AP in regional forecast
maybe extent to a larger grid
...continue with hemsedal_jan2016.py in Test
Random scripting testing
End of explanation
new_snow_region_score = [get_score(a, new_snow_thres) for a in new_snow_region.flatten()]
new_snow_region_score = np.array(new_snow_region_score).reshape(new_snow_region.shape)
print(new_snow_region_score)
wind_region_score = [get_score(a, wind_thres) for a in wind_region.flatten()]
wind_region_score = np.array(wind_region_score).reshape(wind_region.shape)
print(wind_region_score)
print(wind_region_score * new_snow_region_score)
X = np.matrix(np.arange(0, 11.0))
Y = np.matrix(np.arange(10.0, 21.0))
Z = np.multiply(X, Y.T)
print(X)
print(Y.T)
print(Z)
plt.imshow(Z)
print("Min {0}; Max {1}".format(np.amin(Z), np.amax(Z)))
plt.colorbar()
Explanation: the dict is not sorted and the comparison less than is random...
End of explanation
<END_TASK>