<jupyter_start><jupyter_text>Global primary production and temperature contours<jupyter_code>xh = ds_tracer.xh+120
pp = (ds_rates['pp']*ds_rates['volcello']).mean(dim='time').sum(dim='z_l').roll(xh=-240,roll_coords=False).assign_coords(xh=xh).load() #
temp = (ds_mom6['thetao']).sel(z_l = slice(0,50)).mean(dim=['time','z_l']).roll(xh=-240,roll_coords=False).assign_coords(xh=xh).load() # 
grid_rolled = grid.copy().roll(xh=-240,roll_coords=False).assign_coords(xh=xh) # 
grid_rolled['geolon'] = grid_rolled['geolon'].where(grid_rolled['geolon']>=-180,
                                                    grid_rolled['geolon'].where(grid_rolled['geolon']<-180)+360)

crs_plot = ccrs.Robinson(central_longitude=-160)
crs_transform = ccrs.PlateCarree()

# Transform lat and lon onro 
pos = crs_plot.transform_points(src_crs=crs_transform,
                                x=grid_rolled['geolon'].values, y=grid_rolled['geolat'].values)
X = xr.zeros_like(pp)+pos[:,:,0]
Y = xr.zeros_like(pp)+pos[:,:,1]

pp = add_cyclic_point(pp.values)
temp = add_cyclic_point(temp.values)
X = add_cyclic_point(X.values)
Y = add_cyclic_point(Y.values)

# Define manual positions of labels
contours = [0,5,10,15,20,25]
ll = -160
manual_labels = [(ll,-70),(ll,-60),(ll,-50),(ll,-40),(ll,-30),(ll,-20),
                (ll,20),(ll,30),(ll,40),(ll,50),(ll,60),(ll,70)]
posc = manual_labels.copy()
for c in range(len(posc)):
    posc[c] = crs_plot.transform_point(src_crs=crs_transform,
                                        x=manual_labels[c][0], y=manual_labels[c][1])
fig,ax = plt.subplots(figsize=(12,6),subplot_kw={'projection': crs_plot})
ax.add_feature(cfeature.LAND,facecolor='lightgray',zorder=10)
ax.coastlines(linewidth=2)
ax.set_title('Depth integrated primary production and temperature contours',pad=10)

# Correct issues of plotting contours from curvilinear grid
X,Y,temp = z_masked_overlap(ax, X, Y, temp, source_projection=crs_transform)

im_pp=ax.pcolormesh(X,Y,pp,vmin=0,vmax=2000,cmap='Greens')
# im_phyc=ax.pcolormesh(grid_rolled['geolon'],grid_rolled['geolat'],phyc,transform=ccrs.PlateCarree())
im_temp=ax.contour(X,Y,temp,contours,colors='cornflowerblue',linestyle=':')
# im_temp=ax.contour(grid_rolled['geolon'],grid_rolled['geolat'],temp,transform=crs_transform)
ax.clabel(im_temp, inline=True, fontsize=10, manual = posc, fmt='%1.0f')

cbar = plt.colorbar(im_pp,ax=ax,fraction=0.06,pad=0.04,orientation='horizontal')
cbar.set_label('[$mol s^{-1}$]')

fig.savefig('figures/esm4/pp.png',transparent=True,orientation='landscape',dpi=600)<jupyter_output><empty_output><jupyter_text>Phytoplankton, primary production, respiration as a function of temperature<jupyter_code># Calculate bgc variables as a function of temperature
bins_theta = np.arange(-2,32,1)
variables = ['phyc']
ds_tracer_rebinned = histsum_ds(ds_tracer,ds_mom6['thetao'],bins=bins_theta,dim=['xh','yh','z_l'],verbose=True,hist_vars=variables)
ds_tracer_rebinned.load()
variables = ['pp','remoc']
ds_rates_rebinned = histsum_ds(ds_rates,ds_mom6['thetao'].mean('time'),bins=bins_theta,verbose=True,hist_vars=variables)
ds_rates_rebinned.load()
start = 0
stop = 1
N = 12
colors = [ cm.BrBG(x) for x in np.linspace(start, stop, N) ]

fig, (ax0,ax1) = plt.subplots(figsize = (12,8),nrows=2,gridspec_kw={'height_ratios': [1, 1]})
for t in range(12):
    ax0.plot(ds_tracer_rebinned['thetao_bin'],1E-12*ds_tracer_rebinned['phyc'].isel(time=t),color=colors[t])

ax0.set_title('Layerwise integral of phytoplankton carbon')
ax0.set_ylabel('[$10^{12}$ moles]')
ax0.set_ylim([0,4])
ax0.grid(linestyle=':')

cbaxes = inset_axes(ax0, width="30%", height="3%", loc='upper center')
# fig.add_axes([0.4, 0.8, 0.2, 0.01]) 
# plt.colorbar(cax=cbaxes, ticks=[0.,1], orientation='horizontal')
sm = plt.cm.ScalarMappable(cmap=cm.BrBG, norm=plt.Normalize(vmin=0, vmax=1))
cbar = plt.colorbar(sm,cax=cbaxes,ticks=[0,1],orientation='horizontal')
cbar.ax.set_xticklabels(['January','December'],fontsize=12)

ax1.set_title('Layerwise integral of primary production and remineralisation')
ax1.plot(ds_rates_rebinned['thetao_bin'],1E-7*ds_rates_rebinned['pp'],label='Primary production')
ax1.plot(ds_rates_rebinned['thetao_bin'],1E-7*ds_rates_rebinned['remoc'],label='Remineralisation')
ax1.grid(linestyle=':')
ax1.set_ylabel('[$10^{7}$ mol s$^{-1}$]')
ax1.set_ylim([0,1.2])
ax1.set_xlabel('Temperature [$^\circ$ C]')
ax1.legend(fontsize=12,loc='upper center')

fig.savefig('figures/osm_bgc-on-temp.png',transparent=True,orientation='landscape',dpi=600)<jupyter_output><empty_output><jupyter_text>Oxygen and nitrogen, zonal means<jupyter_code>fig,ax = plt.subplots(figsize=(16,6))
colo = ds_tracer['o2'].isel(xh=slice(100,400)).mean(dim='xh').squeeze().load()
cont = ds_mom6['thetao'].isel(xh=slice(100,400)).mean(dim='xh').squeeze().load() 
im_colo = ax.contourf(colo['yh'],colo['z_l'],colo,cmap='Blues')
cbar = plt.colorbar(im_colo)

contours = [0,2.5,5,10,15]
im_cont = ax.contour(cont['yh'],cont['z_l'],cont,contours,colors='darkorange')
ax.clabel(im_cont, inline=True, fontsize=14, fmt='%1.0f')

ax.invert_yaxis()

ax.set_ylabel('depth [m]')
ax.set_xlabel('latitude [$^\circ N$]')
ax.set_title('oxygen and temperature contours (Pacific zonal mean)',pad=20)
cbar.set_label('[$mol\,m^{-3}$]')

fig.savefig('figures/esm4/o2-temp_pacific',transparent=True,orientation='landscape',dpi=600)
fig,ax = plt.subplots(figsize=(16,6))
colo = ds_tracer['no3'].isel(xh=slice(100,400)).mean(dim='xh').squeeze().load()
cont = ds_mom6['thetao'].isel(xh=slice(100,400)).mean(dim='xh').squeeze().load() 
im_colo = ax.contourf(colo['yh'],colo['z_l'],colo,cmap='Purples')
cbar = plt.colorbar(im_colo)

contours = [0,2.5,5,10,15]
im_cont = ax.contour(cont['yh'],cont['z_l'],cont,contours,colors='darkorange')
ax.clabel(im_cont, inline=True, fontsize=14, fmt='%1.0f')

ax.invert_yaxis()

ax.set_ylabel('depth [m]')
ax.set_xlabel('latitude [$^\circ N$]')
ax.set_title('nitrate and temperature contours (Pacific zonal mean)',pad=20)
cbar.set_label('[$mol\,m^{-3}$]')

fig.savefig('figures/esm4/no3-temp_pacific',transparent=True,orientation='landscape',dpi=600)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19980363279332353, 'fraction_numerical': 0.040909834724267714, 'mean_word_length': 7.988235294117647, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '493714', 'license': 'MIT', 'n_tokens_mistral': 2671, 'n_tokens_neox': 2486, 'n_words': 277, 'path': 'notebooks/draw_figs_esm.ipynb', 'repo_name': 'gmacgilchrist/wmt_bgc', 'size': '6111'}