-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_full_impacts.py
executable file
·118 lines (98 loc) · 4.34 KB
/
run_full_impacts.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
#!/home/jmframe/programs/anaconda3/bin/python3
import numpy as np
import pandas as pd
import pickle as pkl
from matplotlib import pyplot as plt
import random
from math import sin, cos, sqrt, atan2, radians
from ease_grid import EASE2_grid
grid_size = 36000
egrid = EASE2_grid(grid_size)
assert egrid.shape == (406, 964)
import impacts
# Set the size bins
max_diameter=330
diam_bins = [5, 10, 50, 100]
diam_labs = ['005', '005-009', '010-050', '050-100', '100+']
diam_range = {'005':[1,5],'005-009':[5,9],'010-050':[10,50],'050-100':[50,100],'100+':[100,330]}
lambda_start = {'005':1,'005-009':1.1,'010-050':1.2,'050-100':1.4,'100+':1.8}
lambda_end = {'005':2,'005-009':4,'010-050':8,'050-100':16,'100+':32}
# Make dictionary with size bins and frequency
with open('sfd.csv', 'r') as f:
freqs = pd.read_csv(f).groupby('D').sum()
los_dict = {i:0 for i in diam_labs}
his_dict = {i:0 for i in diam_labs}
for i in freqs.index.values:
for j in range(len(diam_bins)):
if i < diam_bins[j]:
los_dict[diam_labs[j]] += freqs.loc[i,'low']
his_dict[diam_labs[j]] += freqs.loc[i,'high']
break
elif i >= diam_bins[-1]:
los_dict[diam_labs[-1]] += freqs.loc[i,'low']
his_dict[diam_labs[-1]] += freqs.loc[i,'high']
break
df_freq = pd.DataFrame.from_dict({'high':his_dict, 'low':los_dict,
'lambda_start':lambda_start, 'lambda_end':lambda_end})
df_freq['frequency_factor'] = [0.1,0.2,0.3,0.6,0.8]
print("impact frequency")
with pd.option_context('display.max_rows', None, 'display.max_columns', None): # more options can be specified also
print(df_freq)
t_total=500
fivehundredmillion = 500000000
freq_factor = fivehundredmillion/t_total
not_converged=True
while not_converged:
pp = {x:np.zeros(t_total) for x in diam_labs}
l = {x:np.linspace(y,z,t_total) for x,y,z in zip(diam_labs,df_freq['lambda_start'],df_freq['lambda_end'])}
for D in diam_labs:
pp[D] = l[D]*np.exp(-l[D])
df = pd.DataFrame(data=pp)
hits = {d:np.zeros(t_total) for d in diam_labs}
# Main loop through time. Calculate the total number of impacts of each diameter at each time step
for t in range(0,t_total):
for D in diam_labs:
hits[D][t] = np.floor(np.random.poisson(pp[D][t] / df_freq.loc[D, 'frequency_factor']))
total_sum = np.sum([hits[d] for d in diam_labs])
sums = {d:np.sum(hits[d]) for d in diam_labs}
frac = {d:np.round(np.sum(hits[d])/total_sum,2) for d in diam_labs}
for d in diam_labs:
df_freq.loc[d,'total']=sums[d]
good_numbers = 0
for d in diam_labs:
if df_freq.loc[d,'total'] < df_freq.loc[d,'low']:
df_freq.loc[d,'frequency_factor'] = df_freq.loc[d,'frequency_factor']*random.random()
elif df_freq.loc[d,'total'] > df_freq.loc[d,'high']:
df_freq.loc[d,'frequency_factor'] = df_freq.loc[d,'frequency_factor']*(1+random.random())
else:
good_numbers+=1
if good_numbers == df_freq.shape[0]:
not_converged = False
print("impact frequency")
with pd.option_context('display.max_rows', None, 'display.max_columns', None): # more options can be specified also
print(df_freq)
impact_time = np.linspace(0,fivehundredmillion,t_total+1)[1:]
df = pd.DataFrame(data=hits, index=impact_time)
ensemble_member=1
I = impacts.IMPAaCS(egrid, ensemble=ensemble_member, verbose=True)
# Loop through impacts, the df has them stored by time and diameter bn
for it, t in enumerate(df.index.values):
print('time', it)
for d in diam_labs[2:]:
for i in range(int(df.loc[t,d])):
# locate the the impacts on earth
impact_lat = random.randrange(-90,90)
impact_lon = random.randrange(-180,180)
if np.abs(impact_lat) > 45:
continue
impact_loc = [impact_lat, impact_lon]
impactor_diameter = random.randrange(diam_range[d][0],diam_range[d][1])
##### DO THE DYANMICS #############################
I.update(impact_loc, impactor_diameter, t)
# make a map of the results at this time
# I.plot_map()
with open('impact_states/impacts_10min_45lat_t{}.pkl'.format(it), 'wb') as f:
pkl.dump(I.grid_cell_state, f, pkl.HIGHEST_PROTOCOL)
print(I.test_time)
print(I.average_test_target_list)
print(I.top_layer_at_test_cell)