-
Notifications
You must be signed in to change notification settings - Fork 55
/
makeplot.py
415 lines (356 loc) · 11.3 KB
/
makeplot.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import glob
import pathlib
import re
import sys
import astropy.units as u
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import vplot
from matplotlib import ticker
import vplanet
# Path hacks
path = pathlib.Path(__file__).parents[0].absolute()
sys.path.insert(1, str(path.parents[0]))
from get_args import get_args
def comp2huybers(plname, xrange=False, show=True):
"""
Creates plots of insolation, temperature, albedo, ice mass,
and bed rock height over the length of the simulation
Parameters
----------
plname : string
The name of the planet with .Climate data
Keyword Arguments
-----------------
xrange : float tuple, list, or numpy array
Range of x-values (time) to restrict plot
(default = False (no restriction))
orbit : bool
Plot orbital data (obliquity, eccentricity, COPP)
(default = False)
show : bool
Show plot in Python (default = True)
"""
fig = plt.figure(figsize=(8, 12))
fig.subplots_adjust(wspace=0.3, top=0.9, hspace=0.2)
# Run vplanet
out = vplanet.run(path / "vpl.in")
ctmp = 0
for p in range(len(out.bodies)):
if out.bodies[p].name == plname:
body = out.bodies[p]
ctmp = 1
else:
if p == len(out.bodies) - 1 and ctmp == 0:
raise Exception("Planet %s not found." % plname)
try:
ecc = body.Eccentricity
except:
ecc = np.zeros_like(body.Time) + getattr(out.log.initial, plname).Eccentricity
try:
inc = body.Inc
except:
inc = np.zeros_like(body.Time)
try:
obl = body.Obliquity
except:
obltmp = getattr(out.log.initial, plname).Obliquity
if obltmp.unit == "rad":
obltmp *= 180 / np.pi
obl = np.zeros_like(body.Time) + obltmp
plname_lower = plname.lower()
f = open(path / f"{plname_lower}.in", "r")
lines = f.readlines()
f.close()
pco2 = 0
for i in range(len(lines)):
if lines[i].split() != []:
if lines[i].split()[0] == "dRotPeriod":
P = -1 * float(lines[i].split()[1])
if lines[i].split()[0] == "dSemi":
semi = float(lines[i].split()[1])
if semi < 0:
semi *= -1
if lines[i].split()[0] == "dpCO2":
pco2 = float(lines[i].split()[1])
try:
longp = (body.ArgP + body.LongA + body.PrecA + 180)
except:
longp = body.PrecA
esinv = ecc * np.sin(longp)
lats = np.unique(body.Latitude)
nlats = len(lats)
ntimes = len(body.Time)
# plot temperature
temp = np.reshape(body.TempLandLat, (ntimes, nlats))
ax1 = plt.subplot(7, 1, 5)
c = plt.contourf(
body.Time / 1e6, lats[lats > 58 * u.deg], temp.T[lats > 58 * u.deg], 20
)
plt.ylabel("Latitude")
plt.ylim(60, 83)
plt.yticks([60, 70, 80])
if xrange == False:
left = 0
else:
left = xrange[0]
if xrange:
plt.xlim(xrange)
plt.xticks(visible=False)
clb = plt.colorbar(
c,
ax=plt.gca(),
ticks=[-17, -15, -13, -11, -9, -7, -5],
)
clb.set_label("Surface Temp.\n($^{\circ}$C)", fontsize=12)
# plot ice height
ice = np.reshape(body.IceHeight + body.BedrockH, (ntimes, nlats))
ax3 = plt.subplot(7, 1, 4)
c = plt.contourf(
body.Time / 1e6, lats[lats > 58 * u.deg], ice.T[lats > 58 * u.deg, :], 20
)
plt.ylabel("Latitude")
plt.ylim(60, 83)
plt.yticks([60, 70, 80])
if xrange:
plt.xlim(xrange)
plt.xticks(visible=False)
clb = plt.colorbar(c, ax=plt.gca())
clb.set_label("Ice sheet\nheight (m)", fontsize=12)
ax4 = plt.subplot(7, 1, 6)
acc = np.reshape(body.IceAccum, (ntimes, nlats))
c = plt.contourf(
body.Time / 1e6, lats[lats > 58 * u.deg], acc.T[lats > 58 * u.deg], 20
)
plt.ylabel("Latitude")
plt.ylim(60, 83)
plt.yticks([60, 70, 80])
plt.xticks(visible=False)
if xrange == False:
left = 0
else:
left = xrange[0]
if xrange:
plt.xlim(xrange)
clb = plt.colorbar(
c,
ax=plt.gca(),
)
tloc = ticker.MaxNLocator(nbins=5)
clb.locator = tloc
clb.update_ticks()
clb.set_label("Ice Accum.\n(m year$^{-1}$)", fontsize=12)
ax5 = plt.subplot(7, 1, 7)
abl = np.reshape(body.IceAblate, (ntimes, nlats))
c = plt.contourf(
body.Time / 1e6, lats[lats > 58 * u.deg], -abl.T[lats > 58 * u.deg], 20
)
plt.ylabel("Latitude")
plt.ylim(60, 83)
plt.yticks([60, 70, 80])
plt.xlabel("Time (Myr)")
if xrange == False:
left = 0
else:
left = xrange[0]
if xrange:
plt.xlim(xrange)
clb = plt.colorbar(
c,
ax=plt.gca(),
)
clb.set_label("Ice Ablation\n(m year$^{-1}$)", fontsize=12)
plt.subplot(7, 1, 1)
plt.plot(
body.Time / 1e6,
obl,
linestyle="solid",
marker="None",
color=vplot.colors.dark_blue,
linewidth=2,
)
plt.ylabel("Obliquity")
plt.xticks(visible=False)
if xrange:
plt.xlim(xrange)
# Hack to make axis align with the others
c = plt.contourf([np.nan, np.nan], [np.nan, np.nan], [[1, 0], [0, 1]], alpha=0)
cbar = plt.colorbar(c, ax=plt.gca())
cbar.ax.set_visible(False)
plt.subplot(7, 1, 2)
plt.plot(
body.Time / 1e6,
ecc,
linestyle="solid",
marker="None",
color=vplot.colors.purple,
linewidth=2,
)
plt.ylabel("Eccenticity")
plt.xticks(visible=False)
if xrange:
plt.xlim(xrange)
# Hack to make axis align with the others
c = plt.contourf([np.nan, np.nan], [np.nan, np.nan], [[1, 0], [0, 1]], alpha=0)
cbar = plt.colorbar(c, ax=plt.gca())
cbar.ax.set_visible(False)
plt.subplot(7, 1, 3)
plt.plot(
body.Time / 1e6,
esinv,
linestyle="solid",
marker="None",
color=vplot.colors.red,
linewidth=2,
)
plt.ylabel("CPP")
plt.xticks(visible=False)
if xrange:
plt.xlim(xrange)
# Hack to make axis align with the others
c = plt.contourf([np.nan, np.nan], [np.nan, np.nan], [[1, 0], [0, 1]], alpha=0)
cbar = plt.colorbar(c, ax=plt.gca())
cbar.ax.set_visible(False)
# Save
ext = get_args().ext
plt.savefig(path / f"EarthClimateMilankovitch.{ext}")
if show:
plt.show()
else:
plt.close()
def seasonal_maps(time, show=True):
"""
Creates plots of insolation, temperature, and ice balance
over the course of an orbit (4 orbits for temp)
Parameters
----------
time : float
The time of the data you want to plot (see SeasonalClimateFiles directory)
Keyword Arguments
-----------------
show : bool
Show plot in Python (default = True)
"""
check = 0
for f in glob.glob(str(path / "SeasonalClimateFiles" / "*.DailyInsol.*")):
f1 = f.split(".")
if len(f1) == 4:
timestamp = f1[3]
elif len(f1) == 5:
timestamp = f1[3] + "." + f1[4]
time0 = float(timestamp)
if time0 == time:
# get system and planet names
sysname = f1[0]
plname = f1[1]
insolf = (
path
/ "SeasonalClimateFiles"
/ f"{sysname}.{plname}.DailyInsol.{timestamp}"
)
tempf = (
path
/ "SeasonalClimateFiles"
/ f"{sysname}.{plname}.SeasonalTemp.{timestamp}"
)
icef = (
path
/ "SeasonalClimateFiles"
/ f"{sysname}.{plname}.SeasonalIceBalance.{timestamp}"
)
planckbf = (
path
/ "SeasonalClimateFiles"
/ f"{sysname}.{plname}.SeasonalFOut.{timestamp}"
)
check = 1
if check == 0:
raise StandardError("Climate data not found for time %f" % time)
insol = np.loadtxt(insolf, unpack=True)
temp = np.loadtxt(tempf, unpack=True)
ice = np.loadtxt(icef, unpack=True)
fluxo = np.loadtxt(planckbf, unpack=True)
output = vplanet.run(path / "vpl.in")
ctmp = 0
for p in range(len(output.bodies)):
if output.bodies[p].name == plname:
body = output.bodies[p]
ctmp = 1
else:
if p == len(output.bodies) - 1 and ctmp == 0:
raise Exception("Planet %s not found." % plname)
lats = np.unique(body.Latitude)
try:
obl = body.Obliquity[np.where(body.Time == time)[0]]
except:
obl = getattr(output.log.initial, plname).Obliquity
if obl.unit == "rad":
obl *= 180 / np.pi
try:
ecc = body.Eccentricity[np.where(body.Time == time)[0]]
except:
ecc = getattr(output.log.initial, plname).Eccentricity
try:
longp = (body.LongP + body.PrecA)[np.where(body.Time == time)[0]]
except:
try:
longp = getattr(output.log.initial, plname).LongP
except:
try:
longp = (
getattr(output.log.initial, plname).LongA
+ getattr(out.log.initial, plname).ArgP
)
if longp.unit == "rad":
longp *= 180 / np.pi
longp = longp % 360
except:
longp = 0
fig = plt.figure(figsize=(8.5, 5))
fig.subplots_adjust(wspace=0.3, hspace=0.2)
ax = plt.subplot(2, 2, 1)
c1 = plt.contourf(np.arange(np.shape(insol)[1]), lats, insol, cmap="plasma")
plt.ylim(lats[0], lats[-1])
plt.ylabel("Latitude ($^{\circ}$)")
plt.yticks([-60, -30, 0, 30, 60], ["60S", "30S", "0", "30N", "60N"])
clb = plt.colorbar(c1, ax=ax)
clb.set_label(r"Insolation (W/m$^2$)", fontsize=12)
scale = 4 * np.shape(insol)[1] / np.shape(temp)[1]
ax = plt.subplot(2, 2, 2)
c2 = plt.contourf(np.arange(np.shape(temp)[1]) * scale, lats, temp, cmap="plasma")
plt.ylim(lats[0], lats[-1])
plt.xlim(0, np.shape(temp)[1] * scale / 4.0)
plt.yticks([-60, -30, 0, 30, 60], ["60S", "30S", "0", "30N", "60N"])
clb = plt.colorbar(c2, ax=ax)
clb.set_label(r"Surface Temp ($^{\circ}$C)", fontsize=12)
scale = np.shape(insol)[1] / np.shape(ice)[1]
ax = plt.subplot(2, 2, 3)
c3 = plt.contourf(
np.arange(np.shape(ice)[1]) * scale, lats, ice * 1e3, cmap="Blues_r"
)
plt.ylim(lats[0], lats[-1])
plt.ylabel("Latitude ($^{\circ}$)")
plt.yticks([-60, -30, 0, 30, 60], ["60S", "30S", "0", "30N", "60N"])
plt.xlabel("Day of Year")
clb = plt.colorbar(c3, ax=ax)
clb.set_label(r"Ice Mass Balance (10$^{-3}$ kg/m$^2$/s)", fontsize=12)
scale = 4 * np.shape(insol)[1] / np.shape(temp)[1]
ax = plt.subplot(2, 2, 4)
c2 = plt.contourf(np.arange(np.shape(fluxo)[1]) * scale, lats, fluxo, cmap="plasma")
plt.ylim(lats[0], lats[-1])
plt.xlim(0, np.shape(temp)[1] * scale / 4.0)
plt.yticks([-60, -30, 0, 30, 60], ["60S", "30S", "0", "30N", "60N"])
plt.xlabel("Day of Year")
clb = plt.colorbar(c2, ax=ax)
clb.set_label(r"OLR (W/m$^{2}$)", fontsize=12)
# Save
ext = get_args().ext
fig.savefig(path / f"EarthClimateSeasons.{ext}", dpi=300)
if show:
plt.show()
else:
plt.close()
# Make plots
comp2huybers("Earth", show=False)
seasonal_maps(0, show=False)