WRF output interactive visualization using Plotly

An Interactive python based visualization for WRF output files

Photo by Melissa Bradley on Unsplash

Python Plotly

The plotly Python library is an interactive, open-source plotting library that supports over 40 unique chart types covering a wide range of statistical, financial, geographic, scientific, and 3-dimensional use-cases

WRF-ARW

The Weather Research and Forecasting (WRF) Model is a next-generation mesoscale numerical weather prediction system designed for both atmospheric research and operational forecasting applications. It featsures two dynamical cores, a data assimilation system, and a software architecture supporting parallel computation and system extensibility. The model serves a wide range of meteorological applications across scales from tens of meters to thousands of kilometers. The effort to develop WRF began in the latter 1990’s and was a collaborative partnership of the National Center for Atmospheric Research (NCAR), the National Oceanic and Atmospheric Administration (represented by the National Centers for Environmental Prediction (NCEP) and the Earth System Research Laboratory), the U.S. Air Force, the Naval Research Laboratory, the University of Oklahoma, and the Federal Aviation Administration (FAA).

Setting things up

1. Installing Plotly

  • In this article we were focusing on installation and usage plotly without any web based application (jupyter-notebook)
  • The plots are visualized and stored as HTML files which will be beneficial when using large datasets

2. Setting up python and Plotly

Installation On Windows

sudo apt-get update
sudo apt-get install python
  • For installing miniconda and for further instructions go to this page
  • default is base environment can be deactivated using
  • for more details visit this page
conda deactivate
  • virtualenv is more preferred because its fast and installing only through pip can prevent package conflicts
pip install virtualenv
python -m venv insert_name
insert_name\Scripts\activate.bat
source insert_name/bin/activate

3. Installing WRF

WRF can be configured and compiled using official documentation.

4. Running WRF

In this tutorial we are only focusing methods for interactive visualization.

5. Script for Plotly based WRF output vizualisation

In this tutorial I’m covering 3 dimensional interactive plot to visualize the amount of qcloud in WRF simulation output

import wrf
import pandas as pd
import numpy as np
import plotly.figure_factory as FF
import plotly.graph_objects as go
import time
from scipy.spatial import Delaunay
from netCDF4 import Dataset
direc = "wrfoutput_location"
datain = Dataset(direc)
def cloud_find(datain,time):

qclo = wrf.getvar(datain,'QCLOUD',timeidx=time)
temp = wrf.getvar(datain,"tc",timeidx=time)
interp_level = np.arange(0,12,0.01)
qclo = wrf.vinterp(datain,qclo,"ght_msl",interp_levels=interp_level)
temp = wrf.vinterp(datain,temp,"ght_msl",interp_levels=interp_level)
qclo = qclo.to_dataframe()
temp = temp.to_dataframe()
qclo["temp"] = temp["temp"]
scC = qclo[qclo["temp"].lt(0)]
scC = scC[scC["QCLOUD"].gt(0.00001)]
new_dt = scC[scC["XLONG"].between(78.40,78.50,inclusive=True)]
new_dt = new_dt[new_dt["XLAT"].between(30.2,30.6,inclusive=True)]
new_dt.reset_index(inplace=True)
return new_dt
st_time = int(input("Enter start timestep: "))
en_time = int(input("Enter end timestep: "))
date=[]
time_tt = []
for jj in range(int(en_time-st_time)+1):
time_t = str(wrf.extract_times(Dataset(direc),jj+st_time))
date += [time_t[0:10]]
time_tt += [time_t[11:19]]

Terrain map

To plot terrain height the scipy interpolation tools are used to create triangular interpolation of terrain height values from WRF output

computationterrain = wrf.getvar(datain,"ter",timeidx=0)
qwe = terrain.to_dataframe()
qwe.reset_index(inplace=True)
lat_ter = np.array(qwe["XLAT"])
lon_ter = np.array(qwe["XLONG"])
alt_ter = np.array(qwe["terrain"]/1000)
xx=lon_ter.flatten()
yy=lat_ter.flatten()
points2d = np.vstack([xx,yy]).T
tri = Delaunay(points2d)
simplices = tri.simplices
fig = FF.create_trisurf(x=xx, y=yy, z=alt_ter,
simplices=simplices,show_colorbar=False)
#fig['colorbar'] = False
tme = np.arange(st_time,en_time)
#for jj in range(len(tme)):
# dft = cloud_find(datain,int(jj+st_time))
# print(int(jj+st_time))
# fig.add_trace(go.Scatter3d(x=dft["XLONG"],y=dft["XLAT"],z=dft["interp_level"],mode="markers",hovertext=dft["QCLOUD"]*1000,name=str(time_tt[jj]),marker=dict(
# size=1,
# colorscale='viridis',
# color = dft["QCLOUD"],
# colorbar=dict(x=-0.5,y=0.5,thickness=20,showticklabels=None,title="cloud mixing ratio")
# )))
fig.add_trace(go.Scatter3d(x=x,y=y,z=z,mode="markers",name="cloud",
hovertext=["QCLOUD"]*1000,marker=dict(
size=1,
colorscale='viridis',
color = dft["QCLOUD"]
)))
ext = [78.3, 78.8,30.2, 30.8]
tme = np.arange(st_time,en_time)fig.update_layout(
title="Amount of super-cooled liquid water content",genereated
scene = dict(
xaxis_title="Longitude",
yaxis_title='Latitude',
zaxis_title='Altitude (km)',
xaxis = dict(nticks=10, range=[ext[0],ext[1]],),
yaxis = dict(nticks=10, range=[ext[2],ext[3]],),
zaxis = dict(nticks=20, range=[0,12],),),
width=1000)
#margin=dict(r=20, l=10, b=5, t=10))
fig.update_yaxes(automargin=True)
fig.write_html("/home/nma/Desktop/3dtrajwithjhys.html")#uncomment to save
Liquid water content

Future scope

  • This can be used to generate an extreme event analysis for cloud droplets
  • I have used this method for comparing micro-physics schemes in WRF and trajectory visualization with dispersion modelling

Conclusion

This tutorial is just an simple introduction about how to use plotly for visualizing meteorological data . Similar to 3d plots interactive 2d spatial maps can also generated from WRF netcdf files

Meteorological engineer || baby python developer || Data scientist

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store