Global frontal detection - an example¶

Author: Kai Yang

Manuscript: Global trends in ocean fronts: impacts on air-sea CO2 flux and chlorophyll concentrations

Creation date: 2024-May-18

Latest update: 2025-Jun-03

This Jupyter notebook produces Fig. 1 of the manuscript.

In [1]:
# Import libraries
import xarray as xr
import numpy as np
import cartopy.crs as ccrs
import matplotlib.pyplot as plt 
import matplotlib as mpl
import cmocean as cm
import geopandas as gpd
/Users/kyang7/anaconda3/lib/python3.11/site-packages/pandas/core/arrays/masked.py:60: UserWarning: Pandas requires version '1.3.6' or newer of 'bottleneck' (version '1.3.5' currently installed).
  from pandas.core import (
In [2]:
# Import plot utilities
from plot_utils_v2 import (ccrs_land, add_patches)
In [3]:
# Read maps
dataset_MODIS = xr.open_dataset('../datasets/SST_fronts_example/SST_and_fronts_MODIS_example_2015.nc',chunks={'lat':10,'lon':10}) #MODIS
SSTmap = dataset_MODIS.SST
frontmap = dataset_MODIS.front
In [4]:
SSTmap[1:360,:] = np.nan
SSTmap[1800:2160,:] = np.nan

frontmap[1:360,:] = np.nan
frontmap[1800:2160,:] = np.nan
In [5]:
fig = plt.figure(figsize=(10, 10),dpi=600)
ax0 = fig.add_subplot(2, 1, 2, projection = ccrs.Robinson(central_longitude=180))
# ax0.set_extent([-179.99,179.99,-65, 65])
#################################################################################################################
# mapping
# im1 = SSTmap1.plot(ax = ax0,transform = ccrs.PlateCarree(),add_colorbar=False, 
#                   cmap = 'coolwarm', rasterized = True, vmin = 0, vmax = 30) 
# frontmap1.plot(ax = ax0,transform = ccrs.PlateCarree(),add_colorbar=False,
#               cmap = 'Greys', rasterized=True, vmin=-100, vmax=0)

im1 = SSTmap.plot(ax = ax0,transform = ccrs.PlateCarree(),add_colorbar=False, 
                  cmap = 'coolwarm', rasterized = True, vmin = 0, vmax = 30) 
frontmap.plot(ax = ax0,transform = ccrs.PlateCarree(),add_colorbar=False,
              cmap = 'Greys', rasterized=True, vmin=-100, vmax=0)

plt.plot([0, 360], [60.1, 60.1],
         color='black', linewidth=1, linestyle='--', markersize=3,
         # Be explicit about which transform you want:
         transform = ccrs.PlateCarree())
plt.plot([0, 360], [-60.1, -60.1],
         color='black', linewidth=1, linestyle='--', markersize=3,
         # Be explicit about which transform you want:
         transform = ccrs.PlateCarree())

ax0.set_extent([0.1,359.99,-85, 85], crs=ccrs.PlateCarree())
# map settings
ax0.add_feature(ccrs_land)
add_patches(ax0)

ax0.text(-0.07, 0.55, 'Latitude', va='bottom', ha='center',
        rotation='vertical', rotation_mode='anchor',fontweight = 'bold',
        transform=ax0.transAxes, fontname = 'Arial', fontsize = 12)
ax0.text(0.5, -0.15, 'Longitude', va='bottom', ha='center',
        rotation='horizontal', rotation_mode='anchor', fontweight = 'bold',
        transform=ax0.transAxes, fontname = 'Arial', fontsize = 12)

gl = ax0.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,
                  linewidth=0.5, color='gray', alpha=0.0, linestyle='--')
gl.xlabels_top = False
gl.xlabels_bottom = True
gl.ylabels_right = False
gl.ylabels_left = True
gl.xlabel_style = {'size': 12, 'color': 'k','name':'Arial'}
gl.ylabel_style = {'size': 12, 'color': 'k','name':'Arial'}

# colorbar
cbar_ax = fig.add_axes([0.25, 0.39, 0.15,0.01])
cbar=fig.colorbar(im1, cax=cbar_ax,orientation='horizontal',extend='both',ticks=[0, 5,10,15,20,25,30])
cbar.set_label('SST ($^{\circ}C$)', fontsize=10,color = 'black',fontname = 'Arial')
cbar.ax.tick_params(labelsize=8, color = 'black',labelcolor='black')
for l in cbar.ax.xaxis.get_ticklabels():
    l.set_family("Arial")
cbar.outline.set_color('black')
#################################################################################################################
#################################################################################################################
#################################################################################################################
plt.savefig('../JPEG/Fig1_SST_front_example.jpeg',dpi=600,bbox_inches='tight')
plt.show()
/Users/kyang7/anaconda3/lib/python3.11/site-packages/cartopy/mpl/gridliner.py:451: UserWarning: The .xlabels_top attribute is deprecated. Please use .top_labels to toggle visibility instead.
  warnings.warn('The .xlabels_top attribute is deprecated. Please '
/Users/kyang7/anaconda3/lib/python3.11/site-packages/cartopy/mpl/gridliner.py:463: UserWarning: The .xlabels_bottom attribute is deprecated. Please use .bottom_labels to toggle visibility instead.
  warnings.warn('The .xlabels_bottom attribute is deprecated. Please '
/Users/kyang7/anaconda3/lib/python3.11/site-packages/cartopy/mpl/gridliner.py:487: UserWarning: The .ylabels_right attribute is deprecated. Please use .right_labels to toggle visibility instead.
  warnings.warn('The .ylabels_right attribute is deprecated. Please '
/Users/kyang7/anaconda3/lib/python3.11/site-packages/cartopy/mpl/gridliner.py:475: UserWarning: The .ylabels_left attribute is deprecated. Please use .left_labels to toggle visibility instead.
  warnings.warn('The .ylabels_left attribute is deprecated. Please '

Figure 1 Snapshot of observed frontal positions (black patterns) overlapping average sea surface temperature (SST) for December 2015. SST source: National Aeronautics and Space Administration's Moderate Resolution Imaging Spectroradiometer (NASA MODIS). Fronts are detected using the Belkin-O’Reilly algorithm, with an SST gradient threshold of 0.0281°C km-1 (MODIS SST; Supplementary Appendix D). White regions represent areas where no SST data were available (see Methods).

:)

In [24]:
fig = plt.figure(figsize=(10, 10),dpi=600)
ax0 = fig.add_subplot(2, 1, 2, projection = ccrs.Orthographic(central_longitude=180, central_latitude=0.0, globe=None))
# ax0.set_extent([-179.99,179.99,-65, 65])
#################################################################################################################
# mapping
# im1 = SSTmap1.plot(ax = ax0,transform = ccrs.PlateCarree(),add_colorbar=False, 
#                   cmap = 'coolwarm', rasterized = True, vmin = 0, vmax = 30) 
# frontmap1.plot(ax = ax0,transform = ccrs.PlateCarree(),add_colorbar=False,
#               cmap = 'Greys', rasterized=True, vmin=-100, vmax=0)

im1 = SSTmap.plot(ax = ax0,transform = ccrs.PlateCarree(),add_colorbar=False, 
                  cmap = 'coolwarm', rasterized = True, vmin = 0, vmax = 30) 
frontmap.plot(ax = ax0,transform = ccrs.PlateCarree(),add_colorbar=False,
              cmap = 'Greys', rasterized=True, vmin=-100, vmax=0)

# plt.plot([0, 360], [60.1, 60.1],
#          color='black', linewidth=1, linestyle='--', markersize=3,
#          # Be explicit about which transform you want:
#          transform = ccrs.PlateCarree())
# plt.plot([0, 360], [-60.1, -60.1],
#          color='black', linewidth=1, linestyle='--', markersize=3,
#          # Be explicit about which transform you want:
#          transform = ccrs.PlateCarree())

# ax0.set_extent([0.1,359.99,-85, 85], crs=ccrs.PlateCarree())
# map settings
ax0.add_feature(ccrs_land)
add_patches(ax0)

# ax0.text(-0.07, 0.55, 'Latitude', va='bottom', ha='center',
#         rotation='vertical', rotation_mode='anchor',fontweight = 'bold',
#         transform=ax0.transAxes, fontname = 'Arial', fontsize = 12)
# ax0.text(0.5, -0.15, 'Longitude', va='bottom', ha='center',
#         rotation='horizontal', rotation_mode='anchor', fontweight = 'bold',
#         transform=ax0.transAxes, fontname = 'Arial', fontsize = 12)

# gl = ax0.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,
#                   linewidth=0.5, color='gray', alpha=0.0, linestyle='--')
# gl.xlabels_top = False
# gl.xlabels_bottom = False
# gl.ylabels_right = False
# gl.ylabels_left = False
# gl.xlabel_style = {'size': 12, 'color': 'k','name':'Arial'}
# gl.ylabel_style = {'size': 12, 'color': 'k','name':'Arial'}

# colorbar
# cbar_ax = fig.add_axes([0.25, 0.39, 0.15,0.01])
# cbar=fig.colorbar(im1, cax=cbar_ax,orientation='horizontal',extend='both',ticks=[0, 5,10,15,20,25,30])
# cbar.set_label('SST ($^{\circ}C$)', fontsize=10,color = 'black',fontname = 'Arial')
# cbar.ax.tick_params(labelsize=8, color = 'black',labelcolor='black')
for l in cbar.ax.xaxis.get_ticklabels():
    l.set_family("Arial")
cbar.outline.set_color('black')
#################################################################################################################
#################################################################################################################
#################################################################################################################
plt.savefig('../JPEG/Fig1_SST_front_example_1.jpeg',dpi=600,bbox_inches='tight')
plt.show()
In [ ]: