-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions.py
303 lines (223 loc) · 10.3 KB
/
Functions.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 5 08:43:09 2023
@author: ricij
"""
import matplotlib.pyplot as plt
import numpy as np
from numba import njit
def read_geography(filepath):
return np.genfromtxt(filepath, dtype=np.int8)
def robinson_projection(nlatitude, nlongitude):
def x_fun(lon, lat):
return lon / np.pi * (0.0379 * lat ** 6 - 0.15 * lat ** 4 - 0.367 * lat ** 2 + 2.666)
def y_fun(_, lat):
return 0.96047 * lat - 0.00857 * np.sign(lat) * np.abs(lat) ** 6.41
# Longitude goes from -pi to pi (not included), latitude from -pi/2 to pi/2.
# Latitude goes backwards because the data starts in the North, which corresponds to a latitude of pi/2.
x_lon = np.linspace(-np.pi, np.pi, nlongitude, endpoint=False)
y_lat = np.linspace(np.pi / 2, -np.pi / 2, nlatitude)
x = np.array([[x_fun(lon, lat) for lon in x_lon] for lat in y_lat])
y = np.array([[y_fun(lon, lat) for lon in x_lon] for lat in y_lat])
return x, y
# Plot data at grid points in Robinson projection. Return the colorbar for customization.
# This will be reused in other milestones.
def plot_robinson_projection(data, title, plot_continents=False, geo_dat=[], **kwargs):
# Get the coordinates for the Robinson projection.
nlatitude, nlongitude = data.shape
x, y = robinson_projection(nlatitude, nlongitude)
# Start plotting.
fig, ax = plt.subplots()
# Create contour plot of geography information against x and y.
im = ax.contourf(x, y, data, **kwargs)
if plot_continents:
ax.contour(x,y,geo_dat,colors='black',linewidths=0.25, linestyles='solid')
plt.title(title)
ax.set_aspect("equal")
# Remove axes and ticks.
plt.xticks([])
plt.yticks([])
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.spines["bottom"].set_visible(False)
ax.spines["left"].set_visible(False)
# Colorbar with the same height as the plot. Code copied from
# https://stackoverflow.com/a/18195921
# create an axes on the right side of ax. The width of cax will be 5%
# of ax and the padding between cax and ax will be fixed at 0.05 inch.
from mpl_toolkits.axes_grid1 import make_axes_locatable
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
cbar = plt.colorbar(im, cax=cax)
return cbar
def read_true_longitude(filepath):
return np.genfromtxt(filepath, dtype=np.float64)
def calc_radiative_cooling_co2(co2_concentration, co2_concentration_base=315.0,
radiative_cooling_base=210.3):
return radiative_cooling_base - 5.35 * np.log(co2_concentration / co2_concentration_base)
def calc_albedo(geo_dat):
def legendre(latitude):
return 0.5 * (3 * np.sin(latitude) ** 2 - 1)
def albedo(surface_type, latitude):
if surface_type == 1:
return 0.3 + 0.12 * legendre(latitude)
elif surface_type == 2:
return 0.6
elif surface_type == 3:
return 0.75
elif surface_type == 5:
return 0.29 + 0.12 * legendre(latitude)
else:
raise ValueError(f"Unknown surface type {surface_type}.")
nlatitude, nlongitude = geo_dat.shape
y_lat = np.linspace(0, np.pi/2, nlatitude)
# Map surface type to albedo.
return np.array([[albedo(geo_dat[i, j], y_lat[i])
for j in range(nlongitude)]
for i in range(nlatitude)])
def insolation(latitude, true_longitude, solar_constant, eccentricity,
obliquity, precession_distance):
# Determine if there is no sunset or no sunrise.
sin_delta = np.sin(obliquity) * np.sin(true_longitude)
cos_delta = np.sqrt(1 - sin_delta ** 2)
tan_delta = sin_delta / cos_delta
# Note that z can be +-infinity.
# This is not a problem, as it is only used for the comparison with +-1.
# We will never enter the `else` case below if z is +-infinity.
z = -np.tan(latitude) * tan_delta
if z >= 1:
# Latitude where there is no sunrise
return 0.0
else:
rho = ((1 - eccentricity * np.cos(true_longitude - precession_distance))
/ (1 - eccentricity ** 2)) ** 2
if z <= -1:
# Latitude where there is no sunset
return solar_constant * rho * np.sin(latitude) * sin_delta
else:
h0 = np.arccos(z)
second_term = h0 * np.sin(latitude) * sin_delta + np.cos(latitude) * cos_delta * np.sin(h0)
return solar_constant * rho / np.pi * second_term
def calc_solar_forcing(y_lat,albedo, true_longitudes, solar_constant=1371.685,
eccentricity=0.0, obliquity=0.409253,
precession_distance=1.783037):
def solar_forcing(theta, true_longitude, albedo_loc):
s = insolation(theta, true_longitude, solar_constant, eccentricity,
obliquity, precession_distance)
# a_c = 1 - albedo_loc
return s * albedo_loc
# Latitude values at the grid points
nlatitude = albedo.size
#y_lat = np.linspace( 0,np.pi/2 , nlatitude)
return np.array([[solar_forcing(y_lat[j], true_longitude, albedo[j])
for true_longitude in true_longitudes]
for j in range(nlatitude)])
def calc_diffusion_operator__(mesh, diffusion_coeff, temperature, phi):
h = mesh.h
n_latitude = mesh.n_latitude
n_longitude = mesh.n_longitude
csc2 = mesh.csc2
cot = mesh.cot
return calc_diffusion_operator_inner(h, n_latitude, n_longitude, csc2, cot, diffusion_coeff, temperature, phi)
def calc_diffusion_operator_inner__(h, n_latitude, n_longitude, csc2, cot, diffusion_coeff, temperature, phi):
result = np.zeros(diffusion_coeff.size)
# North Pole
result[0] = 0
# South Pole
result[-1] = 0
for j in range(1, n_latitude - 1):
factor2 = 1 / h ** 2
term2 = factor2 * (-2 * diffusion_coeff[j] * temperature[j] + (diffusion_coeff[j] - 0.25 *
(diffusion_coeff[j + 1] - diffusion_coeff[j - 1])) *
temperature[j - 1]
+ (diffusion_coeff[j] + 0.25 *
(diffusion_coeff[j + 1] - diffusion_coeff[j - 1])) *
temperature[j + 1])
term3 = cot[j-1] * diffusion_coeff[j] * 0.5 / h * (temperature[j + 1] - temperature[j - 1])
result[j] = term2 + term3
return result
def calc_diffusion_operator(mesh, D, temperature, phi):
n_latitude = mesh.n_latitude
h = mesh.h
RE = mesh.RE
return calc_diffusion_operator_inner(n_latitude, h, RE, D, temperature, phi)
@njit
def calc_diffusion_operator_inner(n_latitude, h, RE, D, temperature, phi):
delta_phi = np.pi/(n_latitude-1)
op = np.zeros(phi.size)
op[0] = RE**2 * 2 * np.pi * D[1] * np.sin(h/2) * (temperature[1]- temperature[0] )/h #0 # Äquator
op[-1]= RE**2 * 2 * np.pi * D[1] * np.sin(h/2) * (temperature[-2]- temperature[-1] )/h #0 #north pole --> zero flux boundary condition
for j in range(1,phi.size-1):
op[j] = D[j] * ((temperature[j+1] - 2 * temperature[j] + temperature[j-1])/(delta_phi**2) - np.tan(phi[j]) * (temperature[j+1] - temperature[j-1])/(2*delta_phi))
return op
def plot_annual_temperature(annual_temperature, average_temperature, title):
fig, ax = plt.subplots()
ntimesteps = len(annual_temperature)
plt.plot(average_temperature * np.ones(ntimesteps), label="average temperature")
plt.plot(annual_temperature, label="annual temperature")
plt.xlim((0, ntimesteps - 1))
labels = ["Südpol", "Sdl. Halbkugel", "Äquator" , "Nrd.Halbkugel", "Nordpol" ]
plt.xticks(np.linspace(0, ntimesteps - 1, 5), labels)
ax.set_ylabel("surface temperature [°C]")
plt.grid()
plt.title(title)
plt.legend(loc="upper right")
plt.tight_layout()
plt.show()
def plot_over_time(annual_temperature, average_temperature, title):
fig, ax = plt.subplots()
ntimesteps = len(annual_temperature)
plt.plot(average_temperature * np.ones(ntimesteps), label="average temperature")
plt.plot(annual_temperature, label="annual temperature")
plt.xlim((0, ntimesteps - 1))
labels = ["March" , "Mai", "July", "Sep.", "Nov." , "Jan." ]
plt.xticks(np.linspace(0, ntimesteps - 1, 6), labels)
ax.set_ylabel("surface temperature [°C]")
plt.grid()
plt.title(title)
plt.legend(loc="upper right")
plt.tight_layout()
plt.show()
def calc_mean_1D(data, area):
nlatitude = data.size
mean_data = area[0] * data[0] + area[-1] * data[-1]
for i in range(1, nlatitude - 1):
mean_data += area[i] * data[i]
return mean_data
def calc_area(n_latitude):
area = np.zeros(n_latitude, dtype=np.float64)
delta_theta = np.pi / (n_latitude - 1)
# Poles
area[0] = area[-1] = 0.5 * (1 - np.cos(0.5 * delta_theta))
# Inner cells
for j in range(1, n_latitude - 1):
area[j] = np.sin(0.5 * delta_theta) * np.sin(delta_theta * j)
return area
def calc_lambda(dt = 1.0 / 48, nt=48, ecc= 0.016740, per = 1.783037):
eccfac = 1.0 - ecc**2
rzero = (2.0*np.pi)/eccfac**1.5
lambda_ = np.zeros(nt)
for n in range(1, nt): #hier plus 2??
nu = lambda_[n-1] - per
t1 = dt*(rzero*(1.0 - ecc * np.cos(nu))**2)
t2 = dt*(rzero*(1.0 - ecc * np.cos(nu+0.5*t1))**2)
t3 = dt*(rzero*(1.0 - ecc * np.cos(nu+0.5*t2))**2)
t4 = dt*(rzero*(1.0 - ecc * np.cos(nu + t3))**2)
lambda_[n] = lambda_[n-1] + (t1 + 2.0*t2 + 2.0*t3 + t4)/6.0
return lambda_
def plot_annual_temperature_vgl(annual_temperature_og, annual_temperature_paper):
fig, ax = plt.subplots()
ntimesteps = len(annual_temperature_og)
plt.plot(annual_temperature_og, label="temperature (total)")
plt.plot(annual_temperature_paper, label="temperature paper")
plt.xlim((0, ntimesteps - 1))
labels = ["March", "June", "September", "December", "March"]
#labels = ["Südpol", "Sdl. Halbkugel", "Äquator" , "Nrd.Halbkugel", "Nordpol" ]
plt.xticks(np.linspace(0, ntimesteps - 1, 5), labels)
ax.set_ylabel("surface temperature [°C]")
plt.grid()
plt.title("Annual temperature ")
plt.legend(loc="upper right")
plt.tight_layout()
plt.show()