Friday, 10 July 2015

Python for WRF

I am using NCL at the moment to generate the images for UCD MCC GFS/WRF forecast:

http://mathsci.ucd.ie/met/mcc-forecast.html

It all works fine... so I want to change it! It would be nice to run the forecast on two domains - a higher resolution over Ireland and the UK. I'd also like to have meteograms for a number of locations, which would show forecasts and (historical) observations.

Can I generate these images using Python? I've had a quick Google around, and a promising place to start seems to be with the UK Met Office SciTools packages:

http://scitools.org.uk/

I've installed it on my mac using conda:

conda install -c scitools iris

Then I had a look at the user guide (http://scitools.org.uk/iris/docs/latest/userguide/index.html) and saw this:


If you are reading this user guide for the first time it is strongly recommended that you read the user guide fully before experimenting with your own data files.

Hmmm. I'm usually too impatient. Let's see how far I get...
- Iris data structures are "cubes", which contain data and metadata
- To load a single file into a list of Iris cubes the iris.load() function is used:

import iris
filename = '/Users/conor/DATA/2015071000/wrfout_d01_2015-07-12_02:00:00'
cubes = iris.load(filename)
print cubes
t2m = cubes[165]
print t2m

Wow! That worked! Loading multiple files, and specific fields, is easy too:

filenames = '/Users/conor/DATA/2015071000/wrfout_d01_2015-07-12*'
t2ms = iris.load(filenames, 'T2')
print t2ms

- The iris.save() function saves one or more cubes to a file.
- Iris is able to save to CF NetCDF (1.5) and GRIB (edition 2).

So, can I plot something?

No. Problem. WRF is not CF-compliant, so it doesn't store data in the format that iris expects. NCL works with WRF because it was specifically coded to handle the WRF format. Possibly useful links if I want to continue with this:

https://groups.google.com/forum/#!topic/scitools-iris/O7IRIhMomWc


I tried using wrfncxnj-0.1_r2183. It did produce a netcdf output file, but when I read that in, I still couldn't plot it in iris.

I did, however, get Scott Hosking's python code (link above) to work, after one change due to a .join error:

timestr = ''.join(times[time])

Now I can plot WRF using Python!


A more comprehensive set of functions are available on this page:

http://www.atmos.washington.edu/~lmadaus/pyscripts.html

I wget-ed these files:

coltbls.py
plot_wrf_maps.py

And ran this:
./plot_wrf_maps.py -e -f ../../../DATA/TESTWRF/wrfout_d01_2000-01-24_12\:00\:00 

This produced lots of plots! Here's one:



This may be a good starting point for MCC plots.