oml_max#

# Parameters
variable = "oml_max"
stream = "native"
long_name = "Thickness of the surface Ocean Boundary Layer calculated by [CVMix] KPP"
from IPython.display import display, Markdown
# Dynamically generate markdown content
markdown_text = f" This notebook compares area-weighted maps, in some cases, vertical profiles for {variable} in different basins."

# Display the updated markdown content
display(Markdown(markdown_text))

This notebook compares area-weighted maps, in some cases, vertical profiles for oml_max in different basins.

%load_ext autoreload
%autoreload 2
%%capture 
# comment above line to see details about the run(s) displayed
import sys, os
sys.path.append(os.path.abspath(".."))
from misc import *
import glob
print("Last update:", date.today())
%matplotlib inline
months = ['January', 'February', 'March', 'April', 
          'May', 'June', 'July', 'August', 'September', 
          'October', 'November', 'December']
# load data
ds = []
for c, p in zip(casename, climo_path):
  file = glob.glob(p+'{}.{}.{}.??????-??????.nc'.format(c, stream, variable))[0]
  ds.append(xr.open_dataset(file))
def identify_xyz_dims(dims):
    dims = tuple(dims)

    z_options = ['zl', 'z_l', 'zi', 'z_i']
    y_options = ['yh', 'yq']
    x_options = ['xh', 'xq']

    z_dim = next((dim for dim in dims if dim in z_options), None)
    y_dim = next((dim for dim in dims if dim in y_options), None)
    x_dim = next((dim for dim in dims if dim in x_options), None)

    # Set default values for coordinates and area
    x_coord = y_coord = area_var = None

    if y_dim == 'yh' and x_dim == 'xh':
        x_coord = 'geolon'
        y_coord = 'geolat'
        area_var = 'areacello'
    elif y_dim == 'yq' and x_dim == 'xh':
        x_coord = 'geolon_v'
        y_coord = 'geolat_v'
        area_var = 'areacello_cv'
    elif y_dim == 'yh' and x_dim == 'xq':
        x_coord = 'geolon_u'
        y_coord = 'geolat_u'
        area_var = 'areacello_cu'

    return x_dim, y_dim, z_dim, x_coord, y_coord, area_var
dims = identify_xyz_dims(ds[0][variable+'_annual_mean'].dims)
def annual_plot(variable, dims, label):
    area = grd_xr[0][dims[5]].fillna(0)
    x = dims[0]; y = dims[1]; z = dims[2]
    lon = dims[3]; lat = dims[4] 
    model = []
    for i in range(len(label)):
        if z is None:
            model.append(np.ma.masked_invalid(ds[i][variable+'_annual_mean'].values))
        else:
            model.append(np.ma.masked_invalid(ds[i][variable+'_annual_mean'].isel({z: 0}).values))

        if i == 0:
            xyplot(model[i], 
                grd_xr[i].geolon.values, grd_xr[i].geolat.values, area.values,
                title = 'Annual mean '+str(variable)+ ' ('+str(ds[0].units)+')', 
                suptitle= label[i]+', '+ str(start_date) + ' to ' + str(end_date), 
                extend='max')
        else:
            xyplot((model[i]-model[0]), 
                grd_xr[i].geolon.values, grd_xr[i].geolat.values, area.values,
                title = 'Annual mean '+str(variable)+ ' ('+str(ds[0].units)+')', 
                suptitle= label[i]+' - '+label[0]+', '+ str(start_date) + ' to ' + str(end_date), 
                extend='max')
            
    fig, ax = plt.subplots(figsize=(8,4))
    for i in range(len(label)):
        if z is None:
            ds[i][variable+'_annual_mean'].weighted(area).mean(x).plot(y=y, 
                                            ax=ax, label=label[i])
        else:
            ds[i][variable+'_annual_mean'].isel({z: 0}).weighted(area).mean(x).plot(y=y, 
                                            ax=ax, label=label[i])
            
    ax.set_title('Zonally averaged '+str(variable)+' ('+str(ds[0].units)+'), annual mean')
    ax.grid()
    ax.legend();
    return

Annual mean#

annual_plot(variable, dims, label)
../_images/39658a94ddbe5026fc0fb0af6a309777d05d17c21462d7feba78f187559852f8.png ../_images/d0e1ccbaf43638424d9c7289e8d795a6839d17e6ea40d1cf1f9a0f767ace2a27.png

Monthly climatology#

area = grd_xr[0][dims[5]].fillna(0)
x = dims[0]; y = dims[1]; z = dims[2]
lon = dims[3]; lat = dims[4]
model = []
for i in range(len(label)):
    if z is None:
        model.append(ds[i][variable+'_monthly_climatology'])
    else:
        model.append(ds[i][variable+'_monthly_climatology'].isel({z: 0}))
        
    if i == 0:
        g = model[i].plot(x='geolon', y='geolat', col='month', col_wrap=3,
            figsize=(12,12), robust=True,
            cbar_kwargs={"label": variable + ' ({})'.format(str(ds[0].units)),
                        "orientation": "horizontal", 'shrink': 0.8, 'pad': 0.05})
        
        plt.suptitle(label[i]+ ', ' +str(start_date) + ' to ' + str(end_date), y=1.02, fontsize=17)  

    else:
        g = (model[i]-model[0]).plot(x='geolon', y='geolat', col='month', col_wrap=3,
            figsize=(12,12), robust=True,
            cbar_kwargs={"label": variable + ' ({})'.format(str(ds[0].units)),
                        "orientation": "horizontal", 'shrink': 0.8, 'pad': 0.05})
        plt.suptitle(label[i] + ' - ' + label[0]+ ', ' +str(start_date) + ' to ' + str(end_date), 
                     y=1.02, fontsize=17)  
../_images/11397b6e38251b3184c99bb6cc4009a3a18196bd0ad397d1d5184c509e96d1f2.png
def monthly_plot(variable, dims, label, m):
    area = grd_xr[0][dims[5]].fillna(0)
    x = dims[0]; y = dims[1]; z = dims[2]
    lon = dims[3]; lat = dims[4]
          
    fig, ax = plt.subplots(figsize=(8,4))
    for i in range(len(label)):
        if z is None:
            ds[i][variable+'_monthly_climatology'].isel(month=m).weighted(area).mean(x).plot(y=y, 
                                               ax=ax, label=label[i])
        else:
            ds[i][variable+'_monthly_climatology'].isel({z: 0, 'month': m}).weighted(area).mean(x).plot(y=y, 
                                                ax=ax, label=label[i])
    ax.set_title(str(months[m])+', zonally averaged '+str(variable)+' ('+str(ds[0].units)+')')
    ax.grid()
    ax.legend();
    return

January#

m=0
monthly_plot(variable, dims, label, m)
../_images/12b9979a785566e1bcd7871e7af3822896ea93004ccc6476eca6f4284e729bf8.png

February#

m=1
monthly_plot(variable, dims, label, m)
../_images/4ede7420ba014abae83712730d4f983c9ec5a17c686d71c455a8f4a552666479.png

March#

m=2
monthly_plot(variable, dims, label, m)
../_images/9b5cd09960dfe395fb0c539aaac15833878aa2b4fd89993759fc22d63a8c614f.png

April#

m=3
monthly_plot(variable, dims, label, m)
../_images/4ffb57292237b65d75a06f5a0be1be1de8e4622ef2f7ff7a77327e425db30abd.png

May#

m=4
monthly_plot(variable, dims, label, m)
../_images/ae1df93a576bd1e78fc21a0c9119acc632e454956a1ddda63a400dba81d70afa.png

June#

m=5
monthly_plot(variable, dims, label, m)
../_images/1096df5eaf41b4e0f63c1fae8d8c11a68032277178a374b3fd2202ad00fdd1c2.png

July#

m=6
monthly_plot(variable, dims, label, m)
../_images/53f31aa59f083ce204d4bd8eb6f07a44d288377d220706d16e21ae477196e195.png

August#

m=7
monthly_plot(variable, dims, label, m)
../_images/5b3e441fb9b975374376e7e5810cfb0ab18065663863421c86abbf145b43f274.png

September#

m=8
monthly_plot(variable, dims, label, m)
../_images/38936c34998ade4f6751e6268c9241ac04cd38d5cf1663680ca608470985b35d.png

October#

m=9
monthly_plot(variable, dims, label, m)
../_images/77b843f957d1a083c2c6901ee4e88333ff1b84070125c386ecb08ed7f309f689.png

November#

m=10
monthly_plot(variable, dims, label, m)
../_images/e7d81ac046de935b6a72e743b86672b17439b4da5eb196968fe4b86b01ecb681.png

December#

m=11
monthly_plot(variable, dims, label, m)
../_images/84bdeb08f8d2b7950728c347a4400b578a97329a120875ed5eab92f9932f484e.png

By basins#

Monthly climo @ surface#

# GMM, update this
basin_code = xr.open_dataset('/glade/work/gmarques/cesm/tx2_3/basin_masks/basin_masks_tx2_3v2_20250318.nc')['basin_masks']
area = grd_xr[0][dims[5]].fillna(0)
x = dims[0]; y = dims[1]; z = dims[2]
model_mean_wgt = []
    
for i in range(len(label)):
    basin_code_dummy = basin_code.rename({'yh': y, 'xh': x})
    if z is None:
        model = ds[i][variable+'_monthly_climatology']
    else:
        model = ds[i][variable+'_monthly_climatology'].isel({z: 0})
    
    model_mean_wgt.append((model * basin_code_dummy).weighted(area*basin_code_dummy).mean(dim=[y, x]))
        
for i in range(len(label)):
    g = model_mean_wgt[i].plot(x="month", yincrease=False, col="region", col_wrap=5, label=label[i])
    
fig = g.fig  # not g.figure
fig.suptitle(str(variable)+' ('+str(ds[0].units)+')', fontsize=16)
fig.tight_layout()
fig.subplots_adjust(top=0.9)
for ax in g.axes.flat:
    ax.grid(True);
ax.legend()
<matplotlib.legend.Legend at 0x152d1aa214d0>
../_images/67ba0a46dfaf7b482fe99f2480e6976ea6e6511fa05a19d5db1812f585d967d3.png

Vertical profiles#

Averaged over annual means

z_max=1000 # change this to 6000 to see full profile

if stream == 'z' and (z == 'z_l' or z == 'z_i'):

    model_mean_wgt = []
    
    for i in range(len(label)):
        basin_code_dummy = basin_code.rename({'yh': y, 'xh': x})
        model = ds[i][variable+'_annual_mean']
        
        model_mean_wgt.append((model * basin_code_dummy).weighted(area*basin_code_dummy).mean(dim=[y, x]))
            
    for i in range(len(label)):
        g = model_mean_wgt[i].sel(**{z: slice(0., z_max)}).plot(y=z, yincrease=False, col="region", col_wrap=5, label=label[i])
    
    fig = g.fig  # not g.figure
    fig.suptitle(str(variable)+' ('+str(ds[0].units)+')', fontsize=16)
    fig.tight_layout()
    fig.subplots_adjust(top=0.9)
    plt.legend()
    for ax in g.axes.flat:
        ax.grid(True);