Most-Unstable Convective Available Potential Energy (MUCAPE)
Calculate MUCAPE on a grid of netCDF data using MetPy.
## importing all the packages we might need later for our calculation
import metpy.calc as mpcalc
import xarray as xr
import numpy as np
from metpy.calc import cape_cin, surface_based_cape_cin, dewpoint_from_specific_humidity, parcel_profile,relative_humidity_from_specific_humidity,most_unstable_cape_cin, precipitable_water
from metpy.units import units
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
## opening NetCDF file using xarray
ds = xr.open_dataset("NETCDF_FILE.nc", decode_times=True)
ds
<xarray.Dataset> Size: 2MB Dimensions: (time: 1, lon: 71, lat: 41, lev: 23) Coordinates: * time (time) datetime64[ns] 8B 2019-06-08 * lon (lon) float64 568B -130.5 -129.5 -128.5 ... -62.5 -61.5 -60.5 * lat (lat) float64 328B 20.5 21.5 22.5 23.5 24.5 ... 57.5 58.5 59.5 60.5 * lev (lev) float64 184B 1e+03 975.0 950.0 925.0 ... 300.0 250.0 200.0 Data variables: H (time, lev, lat, lon) float32 268kB ... OMEGA (time, lev, lat, lon) float32 268kB ... PS (time, lat, lon) float32 12kB ... QV (time, lev, lat, lon) float32 268kB ... SLP (time, lat, lon) float32 12kB ... T (time, lev, lat, lon) float32 268kB ... U (time, lev, lat, lon) float32 268kB ... V (time, lev, lat, lon) float32 268kB ... Attributes: (12/33) CDI: Climate Data Interface version 1.9.8 (... Conventions: CF-1 History: Original file generated: Tue Jun 18 21... Comment: GMAO filename: d5124_m2_jan10.inst3_3d... Filename: MERRA2_400.inst3_3d_asm_Np.20190608.nc4 Institution: NASA Global Modeling and Assimilation ... ... ... RangeBeginningTime: 00:00:00.000000 RangeEndingDate: 2019-06-08 RangeEndingTime: 21:00:00.000000 history_L34RS: 'Created by L34RS v1.4.3 @ NASA GES DI... CDO: Climate Data Operators version 1.9.8 (... cdo_openmp_thread_number: 12
#### making a function to slice the xarray dataset according to our need.
def slicer (data,lat1, lat2, lon1, lon2, time1,time2) :
sliced_data = data.sel(lat =slice(lat1, lat2), lon = slice(lon1, lon2),time = slice(time1, time2))
return sliced_data
#slicing the data for CONUS only
new_data = slicer(ds,23.5,50.5,-125.5,-66.5, ds.time[0], ds.time[0])
new_data
<xarray.Dataset> Size: 942kB Dimensions: (time: 1, lon: 60, lat: 28, lev: 23) Coordinates: * time (time) datetime64[ns] 8B 2019-06-08 * lon (lon) float64 480B -125.5 -124.5 -123.5 ... -68.5 -67.5 -66.5 * lat (lat) float64 224B 23.5 24.5 25.5 26.5 27.5 ... 47.5 48.5 49.5 50.5 * lev (lev) float64 184B 1e+03 975.0 950.0 925.0 ... 300.0 250.0 200.0 Data variables: H (time, lev, lat, lon) float32 155kB ... OMEGA (time, lev, lat, lon) float32 155kB ... PS (time, lat, lon) float32 7kB ... QV (time, lev, lat, lon) float32 155kB ... SLP (time, lat, lon) float32 7kB ... T (time, lev, lat, lon) float32 155kB ... U (time, lev, lat, lon) float32 155kB ... V (time, lev, lat, lon) float32 155kB ... Attributes: (12/33) CDI: Climate Data Interface version 1.9.8 (... Conventions: CF-1 History: Original file generated: Tue Jun 18 21... Comment: GMAO filename: d5124_m2_jan10.inst3_3d... Filename: MERRA2_400.inst3_3d_asm_Np.20190608.nc4 Institution: NASA Global Modeling and Assimilation ... ... ... RangeBeginningTime: 00:00:00.000000 RangeEndingDate: 2019-06-08 RangeEndingTime: 21:00:00.000000 history_L34RS: 'Created by L34RS v1.4.3 @ NASA GES DI... CDO: Climate Data Operators version 1.9.8 (... cdo_openmp_thread_number: 12
##extracting temperature, pressure and specific humidity from the dataset
p =new_data.lev*units('hPa')
T = new_data.T
sh = new_data.QV*units('dimensionless')
CAPE = np.zeros((1, 28, 60),dtype=float)
###calculating CAPE in multidimension
for h in range (len(new_data.time)):
for i in range (len(new_data.lat)):
for j in range(len(new_data.lon)):
TC = (T[h,:,i,j]-(273.15))*units('degC')
Td = dewpoint_from_specific_humidity(np.array(p)*units('hPa'),TC, sh[h,:,i,j])
#
try:
MUCAPE = most_unstable_cape_cin(np.array(p)*units('hPa'),np.array(TC)*units('degC'), np.array(Td)*units('degC'))
except:
pass
CAPE[h,i,j] = MUCAPE[0].magnitude
dataproj = ccrs. PlateCarree ()
# # Plot projection
# # The look you want for the view.
plotproj = ccrs. PlateCarree ()
fig=plt.figure(1, figsize=(15.,12.))
ax=plt.subplot(111,projection=plotproj)
ax.add_feature(cfeature.COASTLINE, linewidth=0.5)
ax.add_feature(cfeature.STATES, linewidth=0.5)
plt.title("MUCAPE Calculated Using Metpy (J/Kg)",size = 30)
plt.contourf (new_data.lon, new_data. lat, CAPE[0,:,:],levels =np.arange(0,2400,100),cmap = "PuBuGn", transform=dataproj,extend = "max")
plt.colorbar (orientation = "horizontal", pad=0.01).ax.tick_params(labelsize=20)
plt. show ()
/home/runner/miniconda3/envs/cookbook-dev/lib/python3.10/site-packages/cartopy/io/__init__.py:241: DownloadWarning: Downloading: https://naturalearth.s3.amazonaws.com/50m_physical/ne_50m_coastline.zip
warnings.warn(f'Downloading: {url}', DownloadWarning)
/home/runner/miniconda3/envs/cookbook-dev/lib/python3.10/site-packages/cartopy/io/__init__.py:241: DownloadWarning: Downloading: https://naturalearth.s3.amazonaws.com/50m_cultural/ne_50m_admin_1_states_provinces_lakes.zip
warnings.warn(f'Downloading: {url}', DownloadWarning)