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

Built on top of the Plotly JavaScript library (plotly.js), plotly enables Python users to create beautiful interactive web-based visualizations that can be displayed in Jupyter notebooks, saved to standalone HTML files, or served as part of pure Python-built web applications using Dash. The plotly Python library is sometimes referred to as "plotly.py" to differentiate it from the JavaScript library.

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

Sometimes your system may come with python installed and you can check by opening a command prompt and do python -V. If you are seeing the error, it means python is not installed on your machine and you can continue with this tutorial

If not installed Let’s got to this page and download the version that you want

Follow the steps in order to install the python 3.x.x in the system

Installing on Mac Os

Installation Mac OS is similar to above. Let’s go to this page and download the macOS 64-bit installer

Installation On Linux

There are so many Linux distributions out there. for Ubuntu:

sudo apt-get update
sudo apt-get install python

Python environment and IDE

  • Using python packages like conda (Miniconda) or virtualenv we can create an isolated python environment for installing python packages separately
  • 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
  • Alternatively, we can create custom environment using virtualenv
  • virtualenv is more preferred because its fast and installing only through pip can prevent package conflicts
pip install virtualenv
  • A new virtual environment can be created using simply:
python -m venv insert_name
  • Once you’ve created a virtual environment, you may activate it.

on windows, run:

insert_name\Scripts\activate.bat

on Unix or MacOS, run:

source insert_name/bin/activate

In this tutorial I have using the Spyder IDE similarly jupyter-notebook also used

Installing spyder

pip install spyder=4.2.2

conda install spyder=4.2.2genereated

Installing plotly

plotly can be installed via conda or pip

conda install -c plotly plotly

pip install plotly

3. Installing WRF

WRF can be configured and compiled using official documentation.

More simplified and detailed installation instructions will be covered in another article

Or you can use my Github page to find the automated BASH code to compile and install WRF automatically

The below automated code is based on the official WRF documentation and some changes have made to automate the installation. And I can’t give any 100% guarantee it will work in every system configuration (tested in ubuntu 20.10)

All the above steps to install the WRF-ARW on Ubuntu (aptitude package manager) . In other distros it is pretty hard to install WRF due to missing packages but might be possible in Arch linux. There will be another tutorial on this

Windows ubuntu subsytem can be used to install WRF in windows but showing serious issues with memory management and simulation speed.

4. Running WRF

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

Also feel free to use BASH based automation sctipt to run WRF (with preconfigured domain,schemes e.t.c) from my github page

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

qcloud is the cloud water mixing ratio (units : kg kg-1)

importing libraries

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)

Functions for extracting variable values

  • NetCDF4 and Pandas are used to provide data
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

Converting WRF time steps to date time

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

For plot:

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

Complete code

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

I have use qcloud for an illustrative purpose only . The qcloud variable is not simulated in every microphysics (i am using Thomson scheme) and the filtering function is adjusted so that it will show some values

The variable extraction function can be changed in order to visualise other variables

An time step based plot can be generated in similar way code for that is available in my github repo

Also you can find the updated and additional scripts in my github-repo

If you find any issues or any suggestions for improving this feel free to contact me

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