|
| 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 | + |
1 | 8 | import cfplot as cfp
|
2 | 9 | import cf
|
3 | 10 | import numpy as np
|
|
6 | 13 | import matplotlib.pyplot as plt
|
7 | 14 |
|
8 | 15 | # 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") |
11 | 17 | sst = f[0] # Select the SST variable
|
12 | 18 |
|
13 | 19 | # Collapse data by area mean (average over spatial dimensions)
|
14 | 20 | am = sst.collapse("area: mean") # equivalent to "X Y: mean"
|
15 | 21 | am.squeeze(inplace=True)
|
16 | 22 |
|
17 |
| -# Check available coordinates (already found 'dimensioncoordinate0' as the time coordinate) |
| 23 | +# Check available coordinates (already found 'dimensioncoordinate0' as the |
| 24 | +# time coordinate) |
18 | 25 | print("Available coordinates:", am.coordinates())
|
19 | 26 |
|
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()}) |
44 | 29 |
|
45 |
| -cfp.gopen(file="Recipe3.png") # Start the plot |
| 30 | +cfp.gopen(file="global_avg_sst_plot.png") |
46 | 31 | cfp.lineplot(
|
47 |
| - x=am_data, |
48 |
| - y=am, |
| 32 | + am, |
49 | 33 | color="blue",
|
50 | 34 | title="Global Average Sea Surface Temperature",
|
51 | 35 | ylabel="Temperature (K)",
|
52 | 36 | xlabel="Time"
|
53 | 37 | )
|
54 |
| - |
55 | 38 | cfp.lineplot(
|
56 |
| - x=am_data_copy, |
57 |
| - y=am_copy, |
| 39 | + am_sub, |
58 | 40 | color="red",
|
59 | 41 | )
|
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