Skip to content

Commit 75741e6

Browse files
Add George recipe info & make fixes for original recipe 19
1 parent 9437776 commit 75741e6

File tree

1 file changed

+16
-44
lines changed

1 file changed

+16
-44
lines changed
Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
"""
2+
Recipe 1: Calculating and Plotting Seasonal Mean Pressure at Mean Sea Level
3+
4+
Objective: Calculate and plot the seasonal mean pressure at mean sea level
5+
from ensemble simulation data for 1941.
6+
"""
7+
18
import cfplot as cfp
29
import cf
310
import numpy as np
@@ -6,65 +13,30 @@
613
import matplotlib.pyplot as plt
714

815
# 1. Load the dataset
9-
file_path = "~/Documents/Datasets/ERA5_monthly_averaged_SST.nc"
10-
f = cf.read(file_path)
16+
f = cf.read("~/recipes_break/ERA5_monthly_averaged_SST.nc")
1117
sst = f[0] # Select the SST variable
1218

1319
# Collapse data by area mean (average over spatial dimensions)
1420
am = sst.collapse("area: mean") # equivalent to "X Y: mean"
1521
am.squeeze(inplace=True)
1622

17-
# Check available coordinates (already found 'dimensioncoordinate0' as the time coordinate)
23+
# Check available coordinates (already found 'dimensioncoordinate0' as the
24+
# time coordinate)
1825
print("Available coordinates:", am.coordinates())
1926

20-
am_data_key = am.coordinate("dimensioncoordinate0", key=True)
21-
am_copy = am.copy().subspace(**{am_data_key: cf.mam()})
22-
print(am_copy)
23-
exit()
24-
# Retrieve the time coordinate using its internal identifier
25-
am_data = am.coordinate("dimensioncoordinate0")
26-
am_data_copy = am_copy.coordinate("dimensioncoordinate0")
27-
# Convert the time data from 'hours since 1900-01-01' to datetime format
28-
#time_units = time_coord.units
29-
#am_time = time_coord.data
30-
#am_time_datetime = am.subspace(T=cf.dt("2022-12-01 00:00:00"))
31-
32-
# Convert datetime to numeric values for plotting (e.g., using matplotlib date format)
33-
#am_time_numeric = pd.to_numeric(pd.to_datetime(am_time_datetime))
34-
35-
# Extract data and replace missing values
36-
#am_data = am.data
37-
#am_data = np.where(am_data == -32767, np.nan, am_data)
38-
39-
# Create the line plot using the full dataset with numeric x-axis
40-
41-
42-
print(am[:10])
43-
print(am_data[:10].data.datetime_as_string)
27+
am_dim_key, am_data = am.coordinate("dimensioncoordinate0", item=True)
28+
am_sub = am.subspace(**{am_dim_key: cf.mam()})
4429

45-
cfp.gopen(file="Recipe3.png") # Start the plot
30+
cfp.gopen(file="global_avg_sst_plot.png")
4631
cfp.lineplot(
47-
x=am_data,
48-
y=am,
32+
am,
4933
color="blue",
5034
title="Global Average Sea Surface Temperature",
5135
ylabel="Temperature (K)",
5236
xlabel="Time"
5337
)
54-
5538
cfp.lineplot(
56-
x=am_data_copy,
57-
y=am_copy,
39+
am_sub,
5840
color="red",
5941
)
60-
cfp.gclose() # Finish the plot
61-
62-
63-
# Save the plot to a file using matplotlib
64-
plt.savefig("global_avg_sst_plot.png")
65-
66-
# Show the plot interactively
67-
plt.show()
68-
69-
print("Plot created and saved successfully.")
70-
42+
cfp.gclose()

0 commit comments

Comments
 (0)