Creating Windrose Plots in R

Recently I had to plot some wind data and found a couple of packages to produce windrose plots. My first choice was Windrose in the circular package. Windrose was easy to use, and I really liked the appearance of the plots.  The only drawback was some difficulty getting the axis labels formatted the way I wanted. So, I decided to try Rosavent.  Rosavent was perfect because the axis labels were exactly what I needed, and it produced a nice legend on the plot.  

Using rosavent is just a little tricky if you’re starting with a table of wind speed and direction because it takes a data frame with the frequency of wind speed for each direction class (Windrose does this work for you). This means you need to create class ranges for both wind direction and wind speed and calculate the number of times each wind speed class occurs in each direction class.

If you have a data frame with columns for wind speed and direction, there are just a few easy steps needed to get your data in the right format for Rosavent. To start we load Climatol, Reshape, and our data.

library(climatol)
library(reshape)
winds <- read.table("/input_dir/winds.csv", sep=",",  header=TRUE)

Next we need to convert our data into a frequency table with columns for our wind direction classes and rows for our wind speed classes. In the first seq argument below I have divided 360 degrees into 16 22.5 degree classes.  In the second I have divided wind speed into 10 classes each representing 1 m/sec.

freq_speed_table <- t(table(cut(winds$wind_direction, seq(0, to=360,
by=22.5)), cut(winds$wind_speed, seq(0, to=10, by=1))))

The data is now organized the way we need it, but rosavent takes a data frame – not a table, so we convert our data into a data frame.

freq_speed_df = data.frame(freq_speed_table, row.names = NULL,
check.rows = FALSE, check.names = TRUE, stringsAsFactors =
default.stringsAsFactors())

To get our dataframe back into the structure of our table, we use cast from the reshape package:

freq_speed_df_cast = cast(freq_speed_df, Var1 ~ Var2)

Finally, we can plot our data with rosavent:

png('/output_dir/example_windrose_plot.png')
rosavent(freq_speed_df_cast, 3, 5, ang=-2*pi/16, flab = 2, main="Example Plot", key = TRUE, margen = c(0, 0, 2, 0), uni = "Wind Speed m/s")
dev.off()

..and this produces a nice windrose plot:

gdal reclassify

A few weeks ago I saw somebody was looking on gis.stackexchange for a command line tool to reclassify the ESA’s Globcover land cover dataset (http://gis.stackexchange.com/a/27382/4701).  I often need to reclassify raster data, but in most cases I need to do it as a step in a larger python script, or I use the QGIS raster calculator. Inspired by the gis.stackexchange post, I decided a standalone tool might be handy for writing bash scripts.

Last weekend I posted the first version on github: https://github.com/chiatt/gdal_reclassify. It still needs more work – the only supported output format is .tif.  It’s also a wee bit slow.  Reclassifying a Landsat band is okay, taking only about 5-10 seconds on my laptop, but a really large raster like Globcover (300m pixel size for the globe = 129600 x 55800) takes just under 45 minutes (ugh!). The good news is that it can handle a file a large as Globcover.  It also gives you some control over the output datatype based on your output class values.  If your output classes are 8-bit unsigned, your output file will be 8-bit unsigned.  Likewise, if your output classes are 32-bit float, your output file will be 32-bit float – regardless of the datatype of your input raster.  Another useful feature is that the user can set a default value for pixels that do not meet any reclass conditions, and the default value can be set as the nodata value.

 

Resuscitating MapRabbit

A few years ago I installed WordPress, but never got around to posting anything. That was a very busy time for me, and after a couple of years of neglecting to keep WordPress updated, the site got hacked. The hacker had replaced my blog with an interesting image of a flaming skull.  It was the most interesting thing that had ever been posted to the site, so I decided that was a sign I didn’t have enough time to bother with blogging and removed WordPress.

Recently, however, a friend of mine suggested I write about one of my projects, and he was surprised to learn that I didn’t maintain a blog.  Having completed my graduate degree last May, I have a bit more free time, so I’ve decided to give MapRabbit another try.  There are many, many GIS blogs to read with more interesting and useful information than anything I expect to post here, but at the very least MapRabbit will serve as a place to keep a few notes about what I’m working on.