Created
September 24, 2020 18:35
-
-
Save AdrianoPereira/bfda6a66e8b93c8adb254b8a4dc8b596 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
from matplotlib.colors import Normalize | |
from metpy.plots import colortables as ct | |
import os | |
import numpy as np | |
import cartopy.crs as ccrs | |
import cartopy.feature as cfeature | |
from matplotlib.colorbar import make_axes | |
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER | |
import gzip as gz | |
from netCDF4 import Dataset | |
import xarray as xr | |
def load_file(filename: str, level): | |
with gz.open(filename, 'r') as file_gz: | |
nc_bytes = file_gz.read() | |
nc = Dataset("in-mem-file", mode='r', memory=nc_bytes) | |
nc = xr.open_dataset(xr.backends.NetCDF4DataStore(nc)) | |
dbz = nc['DBZc'].data[0] | |
lon = nc['lon0'].data | |
lat = nc['lat0'].data | |
bbox = (lon.ravel().min(), lon.ravel().max(), | |
lat.ravel().min(), lat.ravel().max()) | |
cmap = ct.get_colortable('NWSReflectivity') | |
norm = Normalize(0, 65) | |
projection = ccrs.PlateCarree() | |
title = filename.split(os.sep)[-1] | |
title = title.split('_') | |
title = "%s %sZ" % (title[2], title[3][:4]) | |
title = "Manaus S-Band %s" % (title) | |
title_font = dict(fontsize=16, fontweight=500) | |
fig, ax = plt.subplots(figsize=(20, 9), | |
subplot_kw=dict(projection=projection)) | |
ax.set_extent(bbox, crs=ccrs.PlateCarree()) | |
ax.add_feature(cfeature.LAND) | |
ax.add_feature(cfeature.COASTLINE) | |
ax.add_feature(cfeature.RIVERS, alpha=.5) | |
ax.set_title(title, pad=20, fontdict=title_font) | |
plot = ax.pcolor(lon, lat, dbz[level], cmap=cmap, | |
norm=norm) | |
cax, kw = make_axes(ax, location='bottom', pad=0.075, shrink=.375) | |
out = fig.colorbar(plot, cax=cax, extend='both', **kw) | |
out.set_label('Reflectivity in dBZ', size=10) | |
gl = ax.gridlines(crs=projection, draw_labels=True, linewidth=1, | |
color='gray', alpha=.5, linestyle='--') | |
gl.xlabels_top = False | |
gl.ylabels_right = False | |
gl.xformatter = LONGITUDE_FORMATTER | |
gl.yformatter = LATITUDE_FORMATTER | |
ax.text(bbox[1] - .525, bbox[3], '%skm' % (.5 + (level * .5)), | |
fontsize=14) | |
plt.savefig('imgame_radar.png', dpi=72, bbox_inches='tight', | |
transparent=False, pad_inches=0.1) | |
plt.show() | |
if __name__ == "__main__": | |
filename = "/home/adriano/thunderstorm/data-cappi/02/sbmn_cappi_20140212_1000.nc.gz" | |
load_file(filename=filename, level=5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment