ERA5 Data and Graphics Preparation for the Science-on-a-Sphere

Contents

ERA5 Data and Graphics Preparation for the Science-on-a-Sphere#

This notebook will create all the necessary files and directories for a visualization on NOAA’s Science-on-a-Sphere. For this example, we will produce a map of 850 hPa geopotential heights, temperature, and horizontal wind and sea-level pressure, using the ERA5, for the March 13-14 1993 winter storm, aka Superstorm ‘93.

Overview:#

  1. Set up output directories for SoS

  2. Specify the date/time range for your case

  3. Use Cartopy’s add_cyclic_point method to avoid a blank seam at the dateline

  4. Create small and large thumbnail graphics.

  5. Create a standalone colorbar

  6. Create a set of labels for each plot

  7. Create 5 days worth of Science-on-a-Sphere-ready plots

  8. Create an SoS playlist file

Prerequisites#

Concepts

Importance

Notes

Matplotlib

Necessary

Cartopy

Necessary

Xarray

Necessary

Metpy

Necessary

Linux command line / directory structure

Helpful

  • Time to learn: 30 minutes


Imports#

import xarray as xr
import numpy as np
import pandas as pd
import cartopy.feature as cfeature
import cartopy.crs as ccrs
import cartopy.util as cutil
from datetime import datetime as dt
import warnings
import metpy.calc as mpcalc
from metpy.units import units
import matplotlib.pyplot as plt

Do not output warning messages

warnings.simplefilter("ignore")

Set up output directories for SoS#

The software that serves output for the Science-on-a-Sphere expects a directory structure as follows:

  • Top level directory: choose a name that is consistent with your case, e.g.: SS93

  • 2nd level directory: choose a name that is consistent with the graphic, e.g.: SLP_500Z

  • 3rd level directories:

    • 2048: Contains the graphics (resolution: 2048x1024) that this notebook generates

    • labels: Contains one or two files:

      1. (required) A text file, labels.txt, which has as many lines as there are graphics in the 2048 file. Each line functions as the title of each graphic.

      2. (optional) A PNG file, colorbar.png, which colorbar which will get overlaid on your map graphic.

    • media: Contains large and small thumbnails (thumbnail_small.jpg, thumbnail_large.jpg) that serve as icons on the SoS iPad and SoS computer apps

    • playlist: A text file, playlist.sos, which tells the SoS how to display your product

As an example, here is how the directory structure on our SoS looks for the products generated by this notebook. Our SoS computer stores locally-produced content in the /shared/sos/media/site-custom directory (note: the SoS directories are not network-accessible, so you won’t be able to cd into them). The ERA5 visualizations go in the ERA5 subfolder. Your top-level directory sits within the ERA5 folder.

sos@sos1:/shared/sos/media/site-custom/ERA5/cle_superbomb/SLP_500Z$ ls -R
.:
2048  labels  media  playlist

./2048:
ERA5_1978012600-fs8.png  ERA5_1978012606-fs8.png  ERA5_1978012612-fs8.png  ERA5_1978012618-fs8.png
ERA5_1978012601-fs8.png  ERA5_1978012607-fs8.png  ERA5_1978012613-fs8.png  ERA5_1978012619-fs8.png
ERA5_1978012602-fs8.png  ERA5_1978012608-fs8.png  ERA5_1978012614-fs8.png  ERA5_1978012620-fs8.png
ERA5_1978012603-fs8.png  ERA5_1978012609-fs8.png  ERA5_1978012615-fs8.png  ERA5_1978012621-fs8.png
ERA5_1978012604-fs8.png  ERA5_1978012610-fs8.png  ERA5_1978012616-fs8.png  ERA5_1978012622-fs8.png
ERA5_1978012605-fs8.png  ERA5_1978012611-fs8.png  ERA5_1978012617-fs8.png  ERA5_1978012623-fs8.png

./labels:
colorbar.png  labels.txt

./media:
thumbnail_big.jpg  thumbnail_small.jpg

./playlist:
playlist.sos

Define the 1st and 2nd-level directories.

# You define these
caseDir = 'SS93'
prodDir = '850_T_Z_Wind'

The 3rd-level directories follow from the 1st and 2nd.

# These remain as is
graphicsDir = caseDir + '/' + prodDir + '/2048/'
labelsDir = caseDir + '/' + prodDir + '/labels/'
mediaDir = caseDir + '/' + prodDir + '/media/'
playlistDir = caseDir + '/' + prodDir + '/playlist/'

Create these directories via a Linux command

! mkdir -p {graphicsDir} {labelsDir} {mediaDir} {playlistDir}
Note: In a Jupyter notebook, the ! magic indicates that what follows is a Linux command.
The -p option for mkdir will create all subdirectories, and also will do nothing if the directories already exist.
# Remove all PNGs in the graphics directory - COMMENT OUT if you don't want to do this!
! rm -f  {graphicsDir}*.png

1) Specify a starting and ending date/time, and access ERA5 data#

Note: The sphere animation will be more effective with at least a minimum number of frames. Since the ERA5 repositories have a timestep of 6 hours (4x/day), let's set our time range to 5 days ... i.e. 20 frames.
# Date/Time specification
startYear = 1993
startMonth = 3
startDay = 11
startHour = 0
startMinute = 0
startDateTime = dt(startYear,startMonth,startDay, startHour, startMinute)
startDateTimeStr = dt.strftime(startDateTime, format="%Y%m%d")

endYear = 1993
endMonth = 3
endDay = 15
endHour = 18
endMinute = 0
endDateTime = dt(endYear,endMonth,endDay, endHour, endMinute)

# Vertical level specification
pLevel = 850
levStr = f'{pLevel}'

delta_time = endDateTime - startDateTime
time_range_max = 5*86400

if (delta_time.total_seconds() > time_range_max):
    raise RuntimeError("Your time range must not exceed 5 days. Go back and try again.")

if (delta_time.total_seconds() < 0):
    raise RuntimeError("Your end time must not be earlier than your start time! Go back and try again.")
Wb2EndDate = dt(2023,1,10)
if (endDateTime <= Wb2EndDate):
    ds = xr.open_dataset(
     'gs://weatherbench2/datasets/era5/1959-2023_01_10-wb13-6h-1440x721.zarr', 
     chunks={'time': 48},
     consolidated=True,
     engine='zarr'
)
else: 
    import glob, os
    input_directory = '/free/ktyle/era5'
    files = glob.glob(os.path.join(input_directory,'*.nc'))
    varDict = {'valid_time': 'time', 
               'pressure_level': 'level',
               'msl': 'mean_sea_level_pressure',
               'q': 'specific_humidity',
               't': 'temperature',
               'u': 'u_component_of_wind',
               'v': 'v_component_of_wind',
               'w': 'vertical_velocity',
               'z': 'geopotential'}
    dimDict = {'valid_time': 'time',
               'pressure_level': 'level'}
    ds = xr.open_mfdataset(files).rename_dims(dimDict).rename_vars(varDict)

Examine the Dataset

ds
<xarray.Dataset> Size: 47TB
Dimensions:                                           (time: 93544,
                                                       latitude: 721,
                                                       longitude: 1440,
                                                       level: 13)
Coordinates:
  * latitude                                          (latitude) float32 3kB ...
  * level                                             (level) int64 104B 50 ....
  * longitude                                         (longitude) float32 6kB ...
  * time                                              (time) datetime64[ns] 748kB ...
Data variables: (12/50)
    10m_u_component_of_wind                           (time, latitude, longitude) float32 388GB dask.array<chunksize=(48, 721, 1440), meta=np.ndarray>
    10m_v_component_of_wind                           (time, latitude, longitude) float32 388GB dask.array<chunksize=(48, 721, 1440), meta=np.ndarray>
    2m_dewpoint_temperature                           (time, latitude, longitude) float32 388GB dask.array<chunksize=(48, 721, 1440), meta=np.ndarray>
    2m_temperature                                    (time, latitude, longitude) float32 388GB dask.array<chunksize=(48, 721, 1440), meta=np.ndarray>
    angle_of_sub_gridscale_orography                  (latitude, longitude) float32 4MB dask.array<chunksize=(721, 1440), meta=np.ndarray>
    anisotropy_of_sub_gridscale_orography             (latitude, longitude) float32 4MB dask.array<chunksize=(721, 1440), meta=np.ndarray>
    ...                                                ...
    v_component_of_wind                               (time, level, latitude, longitude) float32 5TB dask.array<chunksize=(48, 13, 721, 1440), meta=np.ndarray>
    vertical_velocity                                 (time, level, latitude, longitude) float32 5TB dask.array<chunksize=(48, 13, 721, 1440), meta=np.ndarray>
    volumetric_soil_water_layer_1                     (time, latitude, longitude) float32 388GB dask.array<chunksize=(48, 721, 1440), meta=np.ndarray>
    volumetric_soil_water_layer_2                     (time, latitude, longitude) float32 388GB dask.array<chunksize=(48, 721, 1440), meta=np.ndarray>
    volumetric_soil_water_layer_3                     (time, latitude, longitude) float32 388GB dask.array<chunksize=(48, 721, 1440), meta=np.ndarray>
    volumetric_soil_water_layer_4                     (time, latitude, longitude) float32 388GB dask.array<chunksize=(48, 721, 1440), meta=np.ndarray>

2) Specify a date/time range, and subset the desired Datasets along their dimensions.#

Create a list of date and times based on what we specified for the initial and final times, using Pandas’ date_range function

dateList = pd.date_range(startDateTime, endDateTime,freq="6h")
dateList
DatetimeIndex(['1993-03-11 00:00:00', '1993-03-11 06:00:00',
               '1993-03-11 12:00:00', '1993-03-11 18:00:00',
               '1993-03-12 00:00:00', '1993-03-12 06:00:00',
               '1993-03-12 12:00:00', '1993-03-12 18:00:00',
               '1993-03-13 00:00:00', '1993-03-13 06:00:00',
               '1993-03-13 12:00:00', '1993-03-13 18:00:00',
               '1993-03-14 00:00:00', '1993-03-14 06:00:00',
               '1993-03-14 12:00:00', '1993-03-14 18:00:00',
               '1993-03-15 00:00:00', '1993-03-15 06:00:00',
               '1993-03-15 12:00:00', '1993-03-15 18:00:00'],
              dtype='datetime64[ns]', freq='6h')

Now create objects for our desired DataArrays based on the coordinates we have subsetted.#

Lat-lon subsetting: We will be plotting over the entire globe, so we will not restrict the range of latitude/longitude coordinates. However, instead of loading the full 1/4 degree resolution, we will sample every other point (i.e., 1/2 degree).
lonRange = np.arange(0,360,0.5)
latRange = np.arange(-90,90.1,0.5) # Why 90.1?
To avoid contouring problems across the dateline, create a function so longitudes run from -180 to 180 instead of 0 to 360.
def reorder_longitudes(da):
    """
    This function reorders the longitudes in a global grid from 0 --> 360 to -180 --> 180
    """
    da = da.assign_coords(longitude=(((da.longitude + 180 ) % 360 ) - 180))
    return da.sortby(da['longitude'])

Read in the desired variables; subset dimensions; and re-order longitudes.#

Modify the next cell so you are choosing only those parameters you need!
# Data variable selection-Modify depending on what variables you need

#%time Q = reorder_longitudes(ds['specific_humidity'].sel(time=dateList,level=pLevel,latitude=latRange,longitude=lonRange).compute())
%time U = reorder_longitudes(ds['u_component_of_wind'].sel(time=dateList,level=pLevel,latitude=latRange,longitude=lonRange).compute())
%time V = reorder_longitudes(ds['v_component_of_wind'].sel(time=dateList,level=pLevel, latitude=latRange,longitude=lonRange).compute())
#%time W = reorder_longitudes(ds['vertical_velocity'].sel(time=dateList,level=pLevel, latitude=latRange,longitude=lonRange).compute())
%time T = reorder_longitudes(ds['temperature'].sel(time=dateList,level=pLevel, latitude=latRange,longitude=lonRange).compute())
%time Z = reorder_longitudes(ds['geopotential'].sel(time=dateList,level=pLevel,latitude=latRange,longitude=lonRange).compute())
#%time SLP = reorder_longitudes(ds['mean_sea_level_pressure'].sel(time=dateList,latitude=latRange,longitude=lonRange).compute())
CPU times: user 20.5 s, sys: 10.7 s, total: 31.1 s
Wall time: 27.9 s
CPU times: user 23.6 s, sys: 9.83 s, total: 33.5 s
Wall time: 30.5 s
CPU times: user 22.3 s, sys: 8.35 s, total: 30.6 s
Wall time: 29.5 s
CPU times: user 22.2 s, sys: 8.9 s, total: 31 s
Wall time: 29.8 s
%time magic directive: This Jupyter lab directive will display how long each line beginning with the directive takes to execute.

Set a variable corresponding to the number of times in the desired time range

nTimes = len(dateList)
nTimes
20

Examine one of the data arrays that you have read in#

Z
<xarray.DataArray 'geopotential' (time: 20, latitude: 361, longitude: 720)> Size: 21MB
array([[[11399.346 , 11399.346 , 11399.346 , ..., 11399.346 ,
         11399.346 , 11399.346 ],
        [11353.995 , 11354.22  , 11354.556 , ..., 11352.987 ,
         11353.323 , 11353.548 ],
        [11275.612 , 11276.396 , 11277.292 , ..., 11273.373 ,
         11273.933 , 11274.717 ],
        ...,
        [12417.766 , 12418.886 , 12420.006 , ..., 12414.07  ,
         12415.303 , 12416.422 ],
        [12393.243 , 12393.803 , 12394.363 , ..., 12391.34  ,
         12392.012 , 12392.571 ],
        [12360.099 , 12360.099 , 12360.099 , ..., 12360.099 ,
         12360.099 , 12360.099 ]],

       [[11315.699 , 11315.699 , 11315.699 , ..., 11315.699 ,
         11315.699 , 11315.699 ],
        [11259.488 , 11259.824 , 11260.271 , ..., 11258.592 ,
         11258.928 , 11259.264 ],
        [11162.853 , 11163.748 , 11164.532 , ..., 11161.173 ,
         11161.621 , 11162.293 ],
...
        [11451.695 , 11450.549 , 11449.402 , ..., 11455.029 ,
         11453.883 , 11452.842 ],
        [11434.398 , 11433.772 , 11433.355 , ..., 11436.065 ,
         11435.544 , 11434.919 ],
        [11429.813 , 11429.813 , 11429.813 , ..., 11429.813 ,
         11429.813 , 11429.813 ]],

       [[11743.245 , 11743.245 , 11743.245 , ..., 11743.245 ,
         11743.245 , 11743.245 ],
        [11654.363 , 11654.78  , 11654.988 , ..., 11653.529 ,
         11653.946 , 11654.154 ],
        [11516.403 , 11517.445 , 11518.383 , ..., 11513.902 ,
         11514.632 , 11515.569 ],
        ...,
        [11313.943 , 11312.901 , 11311.755 , ..., 11317.174 ,
         11316.132 , 11314.985 ],
        [11285.602 , 11284.976 , 11284.559 , ..., 11287.164 ,
         11286.643 , 11286.122 ],
        [11267.262 , 11267.262 , 11267.262 , ..., 11267.262 ,
         11267.262 , 11267.262 ]]], dtype=float32)
Coordinates:
  * latitude   (latitude) float32 1kB -90.0 -89.5 -89.0 -88.5 ... 89.0 89.5 90.0
    level      int64 8B 850
  * time       (time) datetime64[ns] 160B 1993-03-11 ... 1993-03-15T18:00:00
  * longitude  (longitude) float32 3kB -180.0 -179.5 -179.0 ... 179.0 179.5
Attributes:
    long_name:      Geopotential
    short_name:     z
    standard_name:  geopotential
    units:          m**2 s**-2

Perform unit conversions: modify depending on what variables you have read in!

Z = mpcalc.geopotential_to_height(Z).metpy.convert_units('dam')
U = U.metpy.convert_units('kts')
V = V.metpy.convert_units('kts')
T = T.metpy.convert_units('degC')

Depending on what variables you have read in, set appropriate contour value ranges. The cells below are for temperature and geopotential height. Modify to fit the context of what you are visualizing!#

Set contour levels for T, after first inspecting its range of values.

T.min(),T.max()
(<xarray.DataArray 'temperature' ()> Size: 4B
 <Quantity(-47.4400634765625, 'degree_Celsius')>
 Coordinates:
     level    int64 8B 850,
 <xarray.DataArray 'temperature' ()> Size: 4B
 <Quantity(31.384674072265625, 'degree_Celsius')>
 Coordinates:
     level    int64 8B 850)
tLevels = np.arange(-45,39,3)

For the purposes of plotting geopotential heights in decameters, choose an appropriate contour interval and range of values … for geopotential heights, a common convention is: from surface up through 700 hPa: 3 dam; above that, 6 dam to 400 and then 9 or 12 dam from 400 and above.

if (pLevel == 1000):
    zLevels= np.arange(-90,63, 3)
elif (pLevel == 850):
    zLevels = np.arange(60, 183, 3)
elif (pLevel == 700):
    zLevels = np.arange(201, 339, 3)
elif (pLevel == 500):
    zLevels = np.arange(468, 606, 6)
elif (pLevel == 300):
    zLevels = np.arange(765, 1008, 9)
elif (pLevel == 200): 
    zLevels = np.arange(999, 1305, 9)

Create objects for the relevant coordinate arrays; in this case, longitude, latitude, and time.#

Modify so you are using a variable you have read in! The cell below assumes we have read in temperature as T.#

lons, lats, times= T.longitude, T.latitude, T.time

Take a peek at a couple of these coordinate arrays.

lons
<xarray.DataArray 'longitude' (longitude: 720)> Size: 3kB
array([-180. , -179.5, -179. , ...,  178.5,  179. ,  179.5], dtype=float32)
Coordinates:
    level      int64 8B 850
  * longitude  (longitude) float32 3kB -180.0 -179.5 -179.0 ... 179.0 179.5
Notice that the longitudinal array extends to 179.5, not 180. We will use Cartopy's add_cyclic_point method to eliminate the resulting seam.
times
<xarray.DataArray 'time' (time: 20)> Size: 160B
array(['1993-03-11T00:00:00.000000000', '1993-03-11T06:00:00.000000000',
       '1993-03-11T12:00:00.000000000', '1993-03-11T18:00:00.000000000',
       '1993-03-12T00:00:00.000000000', '1993-03-12T06:00:00.000000000',
       '1993-03-12T12:00:00.000000000', '1993-03-12T18:00:00.000000000',
       '1993-03-13T00:00:00.000000000', '1993-03-13T06:00:00.000000000',
       '1993-03-13T12:00:00.000000000', '1993-03-13T18:00:00.000000000',
       '1993-03-14T00:00:00.000000000', '1993-03-14T06:00:00.000000000',
       '1993-03-14T12:00:00.000000000', '1993-03-14T18:00:00.000000000',
       '1993-03-15T00:00:00.000000000', '1993-03-15T06:00:00.000000000',
       '1993-03-15T12:00:00.000000000', '1993-03-15T18:00:00.000000000'],
      dtype='datetime64[ns]')
Coordinates:
    level    int64 8B 850
  * time     (time) datetime64[ns] 160B 1993-03-11 ... 1993-03-15T18:00:00

Create your plot.#

In this example, we will create contour lines of geopotential height, contour fills of temperature, and wind barbs.#

Let’s create a plot for a single time, just to ensure all looks good.

Note: The Science on a Sphere treats titles and colorbars as separate layers. Thus, in these next cells we will not generate nor include them in the figure.
Additionally, the sphere expects its graphics to have a resolution of 2048x1024.
Finally, by default, Matplotlib includes a border frame around each Axes. We don't want that included on the sphere-projected graphic either.
timeIdx = 0

Subset the DataArrays for that single time (thus, eliminating the time dimension)#

Once again, modify the next cell depending on what variables you have read in.

Z0 = Z.isel(time=timeIdx)
T0 = T.isel(time=timeIdx)
U0 = U.isel(time=timeIdx)
V0 = V.isel(time=timeIdx)
proj_data = ccrs.PlateCarree() # The dataset's x- and y- coordinates are lon-lat
res = '110m'
dpi = 100
fig = plt.figure(figsize=(2048/dpi, 1024/dpi))
ax = plt.subplot(1,1,1,projection=ccrs.PlateCarree(central_longitude=180), frameon=False)
ax.set_global()
ax.add_feature(cfeature.COASTLINE.with_scale(res))
ax.add_feature(cfeature.BORDERS.with_scale(res))
ax.add_feature(cfeature.STATES.with_scale(res))

# Temperature (T) contour fills

# Note we don't need the transform argument since the map/data projections are the same, but we'll leave it in
CF = ax.contourf(lons,lats,T0,levels=tLevels,cmap=plt.get_cmap('coolwarm'), extend='both', transform=proj_data) 

# Height (Z) contour lines
CL = ax.contour(lons,lats,Z0,zLevels,linewidths=1.25,colors='yellow', transform=proj_data)
ax.clabel(CL, inline_spacing=0.2, fontsize=8, fmt='%.0f')
fig.tight_layout(pad=.01)

# Plotting wind barbs uses the ax.barbs method. Here, you can't pass in the DataArray directly; you can only pass in the array's values.
# Also need to sample (skip) a selected # of points to keep the plot readable.
skip = 8
ax.barbs(lons[::skip],lats[::skip],U0[::skip,::skip].values, V0[::skip,::skip].values, transform=proj_data);
../../_images/22b47da836aa840f9d7ec266833eb8de8f16be667ef194a477a0372a782dda8b.png

Hmmm, notice something? Let’s now try plotting on an orthographic projection, somewhat akin to how it would look on the sphere.

res = '110m'
dpi = 100
fig = plt.figure(figsize=(2048/dpi, 1024/dpi))
ax = plt.subplot(1,1,1,projection=ccrs.Orthographic(central_longitude=180), frameon=False)
ax.set_global()
ax.add_feature(cfeature.COASTLINE.with_scale(res))
ax.add_feature(cfeature.BORDERS.with_scale(res))
ax.add_feature(cfeature.STATES.with_scale(res))

# Temperature (T) contour fills

# Note we don't need the transform argument since the map/data projections are the same, but we'll leave it in
CF = ax.contourf(lons,lats,T0,levels=tLevels,cmap=plt.get_cmap('coolwarm'), extend='both', transform=proj_data) 

# Height (Z) contour lines
CL = ax.contour(lons,lats,Z0,zLevels,linewidths=1.25,colors='yellow', transform=proj_data)
ax.clabel(CL, inline_spacing=0.2, fontsize=8, fmt='%.0f')
fig.tight_layout(pad=.01)

# Plotting wind barbs uses the ax.barbs method. Here, you can't pass in the DataArray directly; you can only pass in the array's values.
# Also need to sample (skip) a selected # of points to keep the plot readable.
skip = 8
ax.barbs(lons[::skip],lats[::skip],U0[::skip,::skip].values, V0[::skip,::skip].values, transform=proj_data);
../../_images/9bfefe7815214816cb5dd7d2459dd687ff6a3347e98c15abfe779c5220894a4a.png

There “seams” to be a problem close to the dateline. This is because the longitude ends at 179.5 ° E … leaving a gap between 179.5 and 180 °. Fortunately, Cartopy includes the add_cyclic_point method in its utilities class to deal with this rather common phenomenon in global gridded datasets.

res = '110m'
dpi = 100
fig = plt.figure(figsize=(2048/dpi, 1024/dpi))
ax = plt.subplot(1,1,1,projection=ccrs.Orthographic(central_longitude=180), frameon=False)
ax.set_global()
ax.add_feature(cfeature.COASTLINE.with_scale(res))
ax.add_feature(cfeature.BORDERS.with_scale(res))
ax.add_feature(cfeature.STATES.with_scale(res))

# add cyclic points to data and longitudes
# latitudes are unchanged in 1-dimension
Zcyc, clons = cutil.add_cyclic_point(Z0, lons)
Tcyc, clons = cutil.add_cyclic_point(T0, lons)
Ucyc, clons = cutil.add_cyclic_point(U0, lons)
Vcyc, clons = cutil.add_cyclic_point(V0, lons)

# Temperature (T) contour fills

# Note we don't need the transform argument since the map/data projections are the same, but we'll leave it in
CF = ax.contourf(clons,lats,Tcyc,levels=tLevels,cmap=plt.get_cmap('coolwarm'), extend='both', transform=proj_data) 

# Height (Z) contour lines
CL = ax.contour(clons,lats,Zcyc,zLevels,linewidths=1.25,colors='yellow', transform=proj_data)
ax.clabel(CL, inline_spacing=0.2, fontsize=8, fmt='%.0f')
fig.tight_layout(pad=.01)

# Plotting wind barbs uses the ax.barbs method. Here, you can't pass in the DataArray directly; you can only pass in the array's values.
# Also need to sample (skip) a selected # of points to keep the plot readable.
skip = 8

# The U and V arrays produced by add_cyclic_point do not have units attached, so we do not 
# extract the values attribute as in the previous cells.

ax.barbs(clons[::skip],lats[::skip],Ucyc[::skip,::skip], Vcyc[::skip,::skip], transform=proj_data);
../../_images/27b17e78c37764d5b207d80227d1b00af315a0440cc058aced443b91c2b10552.png
The seam has disappeared! Now we can go back to a PlateCarree projection, centered at the Greenwich Meridian.
res = '110m'
dpi = 100
fig = plt.figure(figsize=(2048/dpi, 1024/dpi))
ax = plt.subplot(1,1,1,projection=ccrs.PlateCarree(), frameon=False)
ax.set_global()
ax.add_feature(cfeature.COASTLINE.with_scale(res), edgecolor='brown', linewidth=2.5)
ax.add_feature(cfeature.BORDERS.with_scale(res), edgecolor='brown', linewidth=2.5)
ax.add_feature(cfeature.STATES.with_scale(res), edgecolor='brown')

# add cyclic points to data and longitudes
# latitudes are unchanged in 1-dimension
Zcyc, clons = cutil.add_cyclic_point(Z0, lons)
Tcyc, clons = cutil.add_cyclic_point(T0, lons)
Ucyc, clons = cutil.add_cyclic_point(U0, lons)
Vcyc, clons = cutil.add_cyclic_point(V0, lons)

# Temperature (T) contour fills

# Note we don't need the transform argument since the map/data projections are the same, but we'll leave it in
CF = ax.contourf(clons,lats,Tcyc,levels=tLevels,cmap=plt.get_cmap('coolwarm'), extend='both', transform=proj_data) 

# Height (Z) contour lines
CL = ax.contour(clons,lats,Zcyc,zLevels,linewidths=1.25,colors='yellow', transform=proj_data)
ax.clabel(CL, inline_spacing=0.2, fontsize=8, fmt='%.0f')
fig.tight_layout(pad=.01)

# Plotting wind barbs uses the ax.barbs method. Here, you can't pass in the DataArray directly; you can only pass in the array's values.
# Also need to sample (skip) a selected # of points to keep the plot readable.
skip = 8

# The U and V arrays produced by add_cyclic_point do not have units attached, so we do not 
# extract the values attribute as in the previous cells.

# Default length of wind barbs is 7. Shorten them a bit.
ax.barbs(clons[::skip],lats[::skip],Ucyc[::skip,::skip], Vcyc[::skip,::skip], length = 5, transform=proj_data)
# Save this figure to your current directory
fig.savefig('test_ERA5_SOS.png')
../../_images/da7d11e1053f00bad237c6eb0e29c9b8bc578830264219e4af392f93aad75b62.png

If you’d like, go to https://globe-3d.vercel.app/ and view how your graphic might look on the sphere.#

Create small (128x128) and large (800x800) thumbnails. These will serve as icons in the SoS iPad and computer apps that go along with your particular product.#

We’ll use the orthographic projection and omit the contour lines and some of the cartographic features, and add a title string.

Note: This cell may take a while to complete. Be patient!
res = '110m'
dpi = 100
for size in (128, 800):
    if (size == 128):
        sizeStr = 'small'
    else:
        sizeStr ='big'
        
    fig = plt.figure(figsize=(size/dpi, size/dpi))
    ax = plt.subplot(1,1,1,projection=ccrs.Orthographic(central_longitude=-90), frameon=False)
    ax.set_global()
    ax.add_feature(cfeature.COASTLINE.with_scale(res))
    tl1 = caseDir
    tl2 = prodDir
    ax.set_title(f"{tl1}\n{tl2}", color='purple', fontsize=8)

    # Contour fills

    CF = ax.contourf(clons,lats,Tcyc,levels=tLevels,cmap=plt.get_cmap('coolwarm'), extend='both', transform=proj_data) 

    fig.tight_layout(pad=.01)
    fig.savefig(f'{mediaDir}thumbnail_{sizeStr}.jpg')
../../_images/0441c43c94e15877f8c5448b712dfb53a1c67ee99edf57deda215adaae8c1fa5.png ../../_images/a4bbde2f73602cf78ce9f1d517d836afa467744375bb63458a4c787bed1b5a7f.png

Create a standalone colorbar#

Visualizations on the Science-on-a-Sphere consist of a series of image files, layered on top of each other. In this example, instead of having the colorbar associated with 500 hPa heights adjacent to the map, let’s use a technique by which we remove the contour plot, leaving only the colorbar to be exported as an image. We change the colorbar’s orientation to horizontal, and also change its tick label colors so they will be more visible on the sphere’s display.

# draw a new figure and replot the colorbar there
fig,ax = plt.subplots(figsize=(14,2), dpi=125)
# set tick and ticklabel color
tick_color='black'
label_color='orange'
cbar = fig.colorbar(CF, ax=ax, orientation='horizontal')
cbar.ax.xaxis.set_tick_params(color=tick_color, labelcolor=label_color, labelsize=8)
# Remove the Axes object ... essentially the contours and cartographic features ... from the figure.
ax.remove()
# All that remains is the colorbar ... save it to disk. Make the background transparent.
fig.savefig(f'{labelsDir}colorbar.png',transparent=True)
../../_images/4fa2a9cee75d40cf0872c9fa5f48d9da59c3b84aeb1cecf99286ce6ecb511a0b.png

Create a set of labels for each plot#

labelFname = f'{labelsDir}labels.txt'

Define a function to create the plot for each hour.#

The function accepts a time array element as its argument.

def make_sos_map (timeIdx):
    
    res = '110m'
    dpi = 100
    fig = plt.figure(figsize=(2048/dpi, 1024/dpi))
    ax = plt.subplot(1,1,1,projection=ccrs.PlateCarree(), frameon=False)
    ax.set_global()
    ax.add_feature(cfeature.COASTLINE.with_scale(res), edgecolor='brown', linewidth=1.5)
    ax.add_feature(cfeature.BORDERS.with_scale(res), edgecolor='brown', linewidth=1.5)
    ax.add_feature(cfeature.STATES.with_scale(res), edgecolor='brown')

#   add cyclic points to data and longitudes
#   latitudes are unchanged in 1-dimension
    Zcyc, clons= cutil.add_cyclic_point(Z.isel(time=timeIdx), lons)
    Tcyc, clons = cutil.add_cyclic_point(T.isel(time=timeIdx), lons)
    Ucyc, clons = cutil.add_cyclic_point(U.isel(time=timeIdx), lons)
    Vcyc, clons = cutil.add_cyclic_point(V.isel(time=timeIdx), lons)

#   Temperature (T) contour fills

#   Note we don't need the transform argument since the map/data projections are the same, but we'll leave it in
    CF = ax.contourf(clons,lats,Tcyc,levels=tLevels,cmap=plt.get_cmap('coolwarm'), extend='both', transform=proj_data) 

#   Height (Z) contour lines
    CL = ax.contour(clons,lats,Zcyc,zLevels,linewidths=1.25,colors='yellow', transform=proj_data)
    ax.clabel(CL, inline_spacing=0.2, fontsize=8, fmt='%.0f')
    fig.tight_layout(pad=.01)

#   Plotting wind barbs uses the ax.barbs method. Here, you can't pass in the DataArray directly; you can only pass in the array's values.
#   Also need to sample (skip) a selected # of points to keep the plot readable.
    skip = 8
#   Default length of wind barbs is 7. Shorten them a bit.
    ax.barbs(clons[::skip],lats[::skip],Ucyc[::skip,::skip], Vcyc[::skip,::skip], length = 5, transform=proj_data)
    frameNum = f'{timeIdx}'.zfill(2)
    figName = f'{graphicsDir}ERA5_{caseDir}_{prodDir}_{frameNum}.png'
    fig.savefig(figName)
    
    # Reduce the size of the PNG image via the Linux pngquant utility. The -f option overwites the resulting file if it already exists.
    # The output file will end in "-fs8.png"
    ! pngquant -f {figName}
    
    # Remove the original PNG
    ! rm -f {figName}
    
    # Do not show the graphic in the notebook
    plt.close()

Create the graphics and titles#

  • Open a handle to the labels file

  • Define the time dimension index’s start, end, and increment values

  • Loop over the period of interest

    • Perform any necessary unit conversions

    • Create each timestep’s graphic

    • Write the title line to the text file.

  • Close the handle to the labels file

For demonstration purposes, we will use only produce graphics for the first two timesteps ... thus only 2 graphics will be produced. When you are ready, change the nFrames to nTimes so all times are processed.

Create a title string for the SoS labels file#

## Modify the title to fit your case!
SOS_label_title = f'ERA5 {pLevel} hPa Z/T/Wind'
labelFileObject = open(labelFname, 'w')
nFrames = 2
#nFrames = nTimes

for timeIdx in range(0, nFrames, 1):
    
    make_sos_map(timeIdx)
    
    # Construct the title string and write it to the file
    valTime = pd.to_datetime(times[timeIdx].values).strftime("%m/%d/%Y %H UTC")
    tl1 = f'{SOS_label_title} {valTime} \n' # \n is the newline character
    labelFileObject.write(tl1)
    print(tl1)
    
# Close the text file
labelFileObject.close()
print("Finished with SoS graphic loop")
ERA5 850 hPa Z/T/Wind 03/11/1993 00 UTC 
ERA5 850 hPa Z/T/Wind 03/11/1993 06 UTC 

Finished with SoS graphic loop

Create an SoS playlist file#

We follow the guidelines in https://sos.noaa.gov/support/sos/manuals/datasets/playlists/#dataset-playlist.sos-files

playlistFname = f'{playlistDir}playlist.sos'
  • Open a file handle to the playlist file

  • Add each line of the playlist. Modify as needed for your case and product; in general you will only need to modify the creaName value!

  • Close the file handle

plFileObject = open(playlistFname, 'w')
subCat = "ERA5"
cRet = "\n" # New line character code

creaName = "Rovin Lazyle" # Put your name here!

plFileObject.write(f"name = {SOS_label_title}{cRet}")
plFileObject.write(f"description = {SOS_label_title}{cRet}")

plFileObject.write(f"pip = ../labels/colorbar.png{cRet}")
plFileObject.write(f"pipheight = 10{cRet}")
plFileObject.write(f"pipvertical = -35{cRet}")

plFileObject.write(f"label = ../labels/labels.txt{cRet}")
plFileObject.write(f"layer = Grids{cRet}")
plFileObject.write(f"layerdata = ../2048{cRet}")

plFileObject.write(f"firstdwell = 2000{cRet}")
plFileObject.write(f"lastdwell = 3000{cRet}")
plFileObject.write(f"fps = 8{cRet}")

plFileObject.write(f"zrotationenable = 1{cRet}")
plFileObject.write(f"zfps = 30{cRet}")

plFileObject.write(f"subcategory = {subCat}{cRet}")
plFileObject.write(f"Creator = {creaName}{cRet}")
                   
plFileObject.close()

Display the contents of the playlist file

! cat {playlistFname}
name = ERA5 850 hPa Z/T/Wind
description = ERA5 850 hPa Z/T/Wind
pip = ../labels/colorbar.png
pipheight = 10
pipvertical = -35
label = ../labels/labels.txt
layer = Grids
layerdata = ../2048
firstdwell = 2000
lastdwell = 3000
fps = 8
zrotationenable = 1
zfps = 30
subcategory = ERA5
Creator = Rovin Lazyle
The playlist essentially describes the following:
  1. The name and description of the product, which are used in the SoS iPad and computer apps
  2. The three components of what is displayed on the screen:
    1. The colorbar (a picture-in-picture, aka pip), with its height and vertical position
    2. The label, or title, which describes the graphic
    3. The graphic layer itself. Multiple graphic layers, each with its own layer name and directory path, could be included; in this case, there is just one.
  3. The dwell times, in ms, for the first frame, last frame, and all frames in-between.

We’re done! The directory tree you have created can then be copied/synced to the correct directory on the SoS computer (Ross and Kevin will take care of this).#


Summary#

We now have an end-to-end workflow that will create all that is necessary for a custom SoS visualization, using ERA5 reanalysis data.

What’s Next?#

Use this notebook as a template for your own case and its accompanying visualizations.