domingo, 4 de diciembre de 2016

rWind R package released!

Hi there! 

 Let me introduce you rWind, an R package with several tools for downloading, editing and converting wind data from Global Forecast System (https://www.ncdc.noaa.gov/data-access/model-data/model-datasets/global-forcast-system-gfs) in other formats as raster for GIS! Wind data is a powerful source of information that could be used for many purposes in biology and other sciences: from the design of air pathways for airplanes to the study of the dispersion routes of plants or bird migrations. Making more accessible this kind of data to scientist and other users is the objective of ERDDAP (http://coastwatch.pfeg.noaa.gov/erddap/index.html), a web service to dive into a lot of weather and oceanographic data-bases and download it easily. 

 I was using specifically one of the ERDDAP data-bases to get wind direction and speed from satellite data, the NOAA/NCEP Global Forecast System (GFS) Atmospheric Model (http://oos.soest.hawaii.edu/erddap/info/NCEP_Global_Best/index.html). At first, I was following this wonderful post from Conor Delaney (http://www.digital-geography.com/cloud-gis-getting-weather-data/#.WERgamd1DCL) to download and fix the data to be used as a GIS layer. However, I needed soon to download and modify a lot of wind data, so I started to write some R functions to automate the different tasks. Finally, I decided to put all together into an R package and upload it to CRAN repository to make it available for other users that could be interested in this kind of data. Here I give you a reference manual and an R code with a brief tutorial to get familiar with the utilities of the rWind package!

If you have any doubt or you want to report a bug or make any suggestion, please, comment the post or write me: jflopez@rjb.csic.es or jflopez.bio@gmail.com. 

 Enjoy it!



Javier Fernández-López (2016). rWind: Download, Edit and Transform Wind Data from GFS. R package version 0.1.3. https://CRAN.R-project.org/package=rWind


   
 # Download and install "rWind" package from CRAN:  
 install.packages("rWind")  
 # You should install also "raster" package if you do not have it   
   
 library(rWind)  
 library(raster)  
   
 packageDescription("rWind")  
 help(package="rWind")  
   
 # "rWind" is a package with several tools for downloading, editing and transforming wind data from Global Forecast   
 # System (GFS, see <https://www.ncdc.noaa.gov/data-access/model-data/model-datasets/global-forcast-system-gfs>) of the USA's  
 # National Weather Service (NWS, see <http://www.weather.gov/>).  
   
 citation("rWind")  
   
 # > Javier Fernández-López (2016). rWind: Download, Edit and Transform Wind Data from GFS. R package version 0.1.3.  
 # > https://CRAN.R-project.org/package=rWind  
   
 # First, we can download a wind dataset of a specified date from GFS using wind.dl function  
 # help(wind.dl)  
   
 # Download wind for Spain region at 2015, February 12, 00:00  
 # help(wind.dl)  
   
 wind.dl(2015,2,12,0,-10,5,35,45)  
   
 # By default, this function generates an R object with downloaded data. You can store it...  
   
 wind_data<-wind.dl(2015,2,12,0,-10,5,35,45)  
   
 head(wind_data)  
   
 # or download a CVS file into your work directory with the data using type="csv" argument:  
   
 getwd()  
 wind.dl(2015,2,12,0,-10,5,35,45, type="csv")  
   
 # If you inspect inside wind_data object, you can see that data are organized in a weird way, with  
 # to rows as headers, a column with date and time, longitude data expressed in 0/360 notation and wind  
 # data defined by the two vector components U and V. You can transform these data in a much more nice format
 # using "wind.fit" function:  
 #help(wind.fit)  
   
 wind_data<-wind.fit(wind_data)  
   
 head(wind_data)  
   
 # Now, data are organized by latitude, with -180/180 and U and V vector components are transformed  
 # into direction and speed. You can export the data.frame as an CVS file to be used with a GIS software  
   
 write.csv(wind_data, "wind_data.csv")  
   
 # Once you have data organized by latitude and you have direction and speed information fields,  
 # you can use it to create a raster layer with wind2raster function to be used by GIS software or to be plotted   
 # in R, for example.  
 # As raster layer can only store one information field, you should choose between direction (type="dir")  
 # or speed (type="speed").  
   
 r_dir <- wind2raster(wind_data, type="dir")  
 r_speed <- wind2raster(wind_data, type="speed")   
   
 # Now, you can use rworldmap package to plot countries contours with your direction and speed data!  
   
 #install.packages("rworldmap")  
 library(rworldmap)   
 newmap <- getMap(resolution = "low")  
   
 par(mfrow=c(1,2))  
   
 plot(r_dir, main="direction")  
 lines(newmap, lwd=4)  
   
 plot(r_speed, main="speed")  
 lines(newmap, lwd=4)  
   
   
 # Additionally, you can use arrowDir and Arrowhead (from "shape" package) functions to plot wind direction  
 # over a raster graph:  
   
 #install.packages("shape")  
 library(shape)   
   
 dev.off()  
 alpha<- arrowDir(wind_data)  
 plot(r_speed, main="wind direction (arrows) and speed (colours)")  
 lines(newmap, lwd=4)  
 Arrowhead(wind_data$lon, wind_data$lat, angle=alpha, arr.length = 0.12, arr.type="curved")  
   
 # If you want a time series of wind data, you can download it by using a for-in loop:  
 # First, you should create an empty list where you will store all the data  
   
 wind_serie<- list()  
   
 # Then, you can use a wind.dl inside a for-in loop to download and store wind data of   
 # the first 5 days of February 2015 at 00:00 in Europe region. It could take a while...  
    
 for (d in 1:5){  
  w<-wind.dl(2015,2,d,0,-10,30,35,70)  
  wind_serie[[d]]<-w  
 }  
   
 wind_serie  
   
 # Finally, you can use wind.mean function to calculate wind average   
   
 wind_average<-wind.mean(wind_serie)  
 wind_average<-wind.fit(wind_average)  
 r_average_dir<-wind2raster(wind_average, type="dir")  
 r_average_speed<-wind2raster(wind_average, type="speed")  
    
  par(mfrow=c(1,2))  
   
 plot(r_average_dir, main="direction average")  
 lines(newmap, lwd=1)  
   
 plot(r_average_speed, main="speed average")  
 lines(newmap, lwd=1)  

20 comentarios:

  1. Hi.
    Very Interesting!
    So Can I aplicate this for México????
    I´ve never work with GFS data
    But i interesting in aplicate some about wind to one investigation

    ResponderEliminar
    Respuestas
    1. Hi Susana!

      Yes, as Conor says, the model is global, you should only select your area of interest as longitude-latitude

      Eliminar
  2. hi¡¡

    I have problems wiht the rwind package. when i use the command wind.dl i have and ERROR message.


    data2014<-wind.dl(2014,1,1,21,-10,3,44,36,type = "read-data")

    ERROR: database not found. Please, check server connection, date or geographical ranges

    I have been investigating about the problem but I can't solve it.

    Thank you very much

    Óscar

    ResponderEliminar
  3. Hi Óscar!

    the last two values should been in this order: southern latitude, northern latitude. You are placing first the northern value.

    Try this!

    data2014<-wind.dl(2014,1,1,21,-10,3,36,44,type = "read-data")

    Cheers!

    ResponderEliminar
  4. Hi! Do you know why I could get this meessage?
    Error in wind.fit(wind_data) : could not find function "wind.fit"

    Thanks

    ResponderEliminar
    Respuestas
    1. Hi! There is a new version of rWind package and wind.fit function is now included in wind.dl or wind.dl_2. Please, check help(wind.dl) for more information.

      Eliminar
  5. Este comentario ha sido eliminado por el autor.

    ResponderEliminar
  6. Javier, this package is super useful! thanks for all the efforts!!

    Instead of using wind.fit I could get the same results using:

    wind_data<-wind_data[c("lat", "lon", "dir", "speed")]

    The rest of the code will work just fine. Except the wind series. The loop does not give a rWind_series anymore:

    wind.mean(wind_serie) : x needs to be of class rWind_series

    Don't know how to solve it yet.

    ResponderEliminar
    Respuestas
    1. Hi Alexander! Thanks for your feedback!
      Yes, you are right. In this post I was using the first version or rWind, and it worked a little bit different. Now wind.dl includes wind.fit, so wind.fit function doesen't exist anymore. wind.mean also works different. Now you can use wind.dl_2 to download series of wind data, and then use wind.mean. Recently we published a paper introducing rWind package, and you can find in the support material full examples using wind.mean and wind.dl_2. Please, check:

      https://onlinelibrary.wiley.com/doi/abs/10.1111/ecog.03730

      Supp. material: https://onlinelibrary.wiley.com/action/downloadSupplement?doi=10.1111%2Fecog.03730&file=ecog12378-Supplemental_Information.zip

      If you have more questions, please, send me an e-mail: jflopez.bio@gmail.com

      Eliminar
    2. Check also this post about the new wind.dl_2

      http://allthiswasfield.blogspot.com/2018/07/new-rwind-v102-release-on-cran.html

      Eliminar
  7. Salut,
    comment faire pour trouver les données d'un pays spécifique? c'est à dire si je veux trouver les données du Maroc.
    Merci

    ResponderEliminar
  8. Hi!
    To find winds for Maroc, you have to provide the geographic coordinates for the entire region as:

    wind.dl(yyyy, mm, dd, tt, lon1, lon2, lat1, lat2)

    being:

    yyyy Selected year.
    mm Selected month.
    dd Selected day.
    tt Selected time. There are currently several options at the GFS database: 00:00 - 03:00 - 06:00 - 09:00 - 12:00 - 15:00 - 18:00 - 21:00 (UTC).
    lon1 Western longitude
    lon2 Eastern longitude
    lat1 Southern latitude
    lat2 Northern latitude

    Here would be an example:

    wind.dl(2015,2,12,0,-14,0,25,36)

    ResponderEliminar
  9. Hi Javi,

    Do you know if it is possible to extract historical forecasts for a selected region ?
    Thanks a lot :)
    Edouard

    ResponderEliminar
    Respuestas
    1. Hi Edouard!

      rWind is based in a database that covers wind data since May 2011... What do you mean with " historical forecasts"...?
      Anyway, there are some people that is using rWind to explain old patterns of ships movements, for instance:

      https://www.tandfonline.com/doi/full/10.1080/15230406.2017.1403376?src=recsys

      Cheers!

      Eliminar
  10. Hi Javi, thanks for your prompt reply! By forecasts, i mean that I am not interested in the actual historical wind but interested in the evolution of the forecasts of wind made by GFS. They publish every 6 hours a forecast of wind and I'd like to access it but I am not able to find it anywhere...

    Have a good day,
    Edouard

    ResponderEliminar
  11. Bonjour :
    Comment faire pour tracer les lignes,
    Par exemple:
    library(rWind)
    library(raster)
    w <- wind.dl(2015, 2, 12, 12, -5.55, -1.55, 35.20, 40.20)
    dt <- wind2raster(w)
    plot(dt)
    mais comment faire pour tracer les lignes ?
    library(rworldmap)
    newmap <- getMap(resolution = "low")

    par(mfrow=c(1,2))

    plot(r_dir, main="direction")
    lines(newmap, lwd=4)

    plot(r_speed, main="speed")
    lines(newmap, lwd=4), Comment ça marche ?????

    ResponderEliminar
  12. Howdy,

    You need to split the r_data into direction and speed first.

    Do the following before the last set of plot.


    r_data <- wind2raster(wind_data)
    r_dir <- r_data$wind.direction
    r_speed <- r_data$wind.speed

    ResponderEliminar
  13. Hi Javi,

    rWind currently downloads GFS with 0.5 degree resolution.

    From the NCEP archive, I've seen GFS data with resolution of 0.25. However, there are only 10 data entries at a time. Is it possible to download them with rWind as well?

    Many thanks,
    Lucas

    ResponderEliminar