-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
124 lines (106 loc) · 3.76 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import numpy as np
import json
import matplotlib.pyplot as plt
import itertools as it
import pandas as pd
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import cartopy.io.shapereader as shpreader
def thetavcalc(
probe):
# Temp and dewpoint in C
Td_C = (probe['td'] - 32.) * (5/9)
T_C = (probe['t'] - 32.) * (5/9)
# Potential temp
theta = T_C * (1000.0 / probe['p1']) ** (287.0 / 1004.0)
# Vapor pressure
# vap_P = 6.11 * np.exp((2500000.0 / 461.0) * (273.15 ** -1 - Td_K ** -1.0))
vap_P = 0.6112 * np.exp((17.67 * Td_C) / (Td_C + 243.5))
# Mixing ratio
MR = .622 * vap_P / (probe['p1'] - vap_P)
theta_V = theta * (1.0 + .61 * MR)
return theta_V
# Reads .json ASOS/RAWS data from Synoptic
def readJSON(file):
with open(file, 'r', encoding='utf-8', newline='') as f:
dataset = json.load(f)
return dataset
def drawStations(lons, lats, times, data):
markers = it.cycle(["." , "," , "o" , "v" , "^" , "<", ">"])
fig, ax = plt.subplots()
ax.scatter(lons, lats, c=data, marker=next(markers))
return None
def getLats(df):
return df.loc[0, 'LATITUDE'].astype(float)
def getLons(df):
return df.loc[0, 'LONGITUDE'].astype(float)
def getTimes(df):
return df.loc[0, 'OBSERVATIONS.date_time'][0]
def getData(df):
return df.loc[0, 'OBSERVATIONS.air_temp_set_1'][0]
def toDF(time, data):
frame = pd.DataFrame(time, columns=['datetime'])
frame['data'] = data
return frame.set_index('datetime')
# process time series data for plotting
def processTS(df, variable, filt='60T', mnet=None):
# acquire stations by their MNET ID
if mnet:
df = df.iloc[df.groupby('MNET_ID').groups[mnet]]
# acquire times and format them
time = [n for n in df['OBSERVATIONS.date_time'] if isinstance(n, list)]
timeForm = [pd.to_datetime(l, infer_datetime_format=True) for l in time]
# acquire data from DF
data = [n for n in df[variable] if isinstance(n, list)]
# resample time and data with median filter
dfs = [toDF(timeForm[i], data[i]).resample(filt).median() for i in range(len(timeForm))]
return dfs
# convert temperature in deg C to potential temperature with pres in Pa
def theta(temp, pres):
return temp*((100000.0/pres)**(2/7))
# calculate RMSE
# find the mean of ASOS data
def meansDF(dfs):
concat = pd.concat(dfs)
by_row_idx = concat.groupby(concat.index)
means = by_row_idx.mean()
return means
def rmse(predictions, targets):
return np.sqrt(((predictions - targets) ** 2).mean())
def cartopyMap(path):
ax = plt.axes(projection=ccrs.PlateCarree())
# counties
reader = shpreader.Reader(path)
counties = list(reader.geometries())
COUNTIES = cfeature.ShapelyFeature(counties, ccrs.PlateCarree())
# add the features
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.LAKES)
ax.add_feature(cfeature.RIVERS)
ax.add_feature(cfeature.BORDERS)
ax.add_feature(cfeature.STATES)
ax.add_feature(COUNTIES, facecolor='none', edgecolor='gray')
ax.coastlines()
return ax
#potential temperature calculation
def thetaCalc(temp, pres): #temp in C, pres in Pa
return (temp + 273)*((100000.0/pres)**(2/7)) - 273 #returns theta in C
#pressure estimation from elevation and temp, hypsometric equation
def hypsometric(temp, elev):
temp = temp + 273
elev = elev * 0.3048
H = 287 * temp / 9.81
return 100000. * np.exp(-elev / H)
#dry adiabatic lapse rate
def dryALR(height):
gamma = -9.8 #degrees C per km
gamma = gamma / 3280.84 #km to feet
return 12.75 + gamma*height - 700*gamma
#getting time series data at a specific time from a DataFrame
def getAtTime(df, stamp, col):
try:
data = df.loc[stamp][col]
return data
except:
data = np.nan
return data