{"word_count": 660, "character_count": 4951}
Mid november, a nice workshop on big data and environment will be organized, in Argentina,We will talk a lot about climate models, and I wanted to play a little bit with those data, stored on http://dods.ipsl.jussieu.fr/mc2ipsl/.Since Ewen (aka @3wen) has been working on those datasets recently, he kindly told me how to read those datasets (in some ncdf format). He did show me interesting functions, and did share with me some lines of codes. I will mention some of them in a brief post, because I've been impressed to see how memory issues were not a big deal, here... But first of all, we need half a dozen packages&#62; library(ncdf)&#62; library(lubridate)&#62; library(dplyr)&#62; library(stringr)&#62; library(tidyr)&#62; library(ggplot2)Then i will download one of those files, from January 2000 till December 2012.&#62; download.file("http://dods.ipsl.jussieu.fr/mc2ipsl/CMIP5/output1/IPSL/IPSL-CM5A-MR/historicalNat/day/atmos/day/r1i1p1/latest/tas/tas_day_IPSL-CM5A-MR_historicalNat_r1i1p1_20000101-20121231.nc",destfile="temp.nc")Those file are not huge. But big.&#62; file.size("temp.nc")[1] 390965452That's almost a 400Mo file. For one single file (and there are hundreds on those servers). Now, if we 'read' that file, in R, we have a rather small R object,&#62; nc &#60;- open.ncdf("temp.nc")&#62; object.size(nc)155344 bytesThat's only 150Ko... One out of 2516 compared with the file online. Somehow, we only extract partial information here. We will scan the file completely later on... We can already create some variables that will be used later on, such as the date, the latitude, the longitude, etc&#62; lon &#60;- get.var.ncdf(nc,"lon")&#62; (nlon &#60;- dim(lon))[1] 144&#62;  lat &#60;- get.var.ncdf(nc,"lat")&#62; nlat &#60;- dim(lat)&#62; time &#60;- get.var.ncdf(nc,"time")&#62; tunits &#60;- att.get.ncdf(nc, "time", "units")&#62; orig &#60;- as.Date(ymd_hms(str_replace(  tunits$value, "days since ", "")))&#62; dates &#60;- orig+time&#62; ntime &#60;- length(dates)Here we have the range of latitude and longitude in the dataset, as well as the date. Now it is time to read the file. Based on the starting date, and asking only for temperature&#62; commencement &#60;- 1&#62; dname &#60;- "tas"we can red the file&#62; tab_val &#60;- get.var.ncdf(nc, dname, start=c(1,1,commencement))&#62; dim(tab_val)[1]  144  143 4745&#62; object.size(tab_val)781672528 bytesThe file is big now, a bit less that 800Mo.&#62; fill_value &#60;- att.get.ncdf(nc, dname, "_FillValue")&#62; tab_val[tab_val == fill_value$value] &#60;- NALet us now summarize all the information in nice data frames, that will be used to generate graphs&#62; tab_val_vect &#60;- as.vector(tab_val)&#62; tab_val_mat &#60;- matrix(tab_val_vect, nrow =   nlon * nlat, ncol = ntime)&#62; lon_lat &#60;- expand.grid(lon, lat)&#62; lon_lat &#60;- lon_lat %&#62;% mutate(Var1 =   as.vector(Var1), Var2 = as.vector(Var2))Note that the file is still big (the same size as the previous file, here we just reshape the dataset)&#62; res &#60;- data.frame(cbind(lon_lat,   tab_val_mat))&#62; object.size(res)782496048 bytesHere is the dimension of our dataset&#62; dim(res)[1] 20592  4747We can give understandable names to variables&#62; noms &#60;- str_c(year(dates),+ month.abb[month(dates)] %&#62;%  tolower, sprintf("%02s", day(dates)),  sep = "_")  &#62; names(res) &#60;- c("lon", "lat", noms)Now, let us focus on the ploting part. If we focus on Europe, we need to filter the dataset&#62; res &#60;- res %&#62;%  mutate(lon = ifelse(lon &#62;= 180, yes = lon -   360, no = lon)) %&#62;%  filter(lon &#60; 40, lon &#62; -20, lat &#60; 75,   lat &#62; 30) %&#62;%   gather(key, value, -lon, -lat) %&#62;%  separate(key, into = c("year", "month",   "day"), sep="_")Let use filter, with only years from 1986 till 2005&#62; temp_moy_hist &#60;- res %&#62;%+ filter(year &#62;= 1986, year &#60;= 2005) %&#62;%+ group_by(lon, lat, month) %&#62;%+ summarise(temp = mean(value, na.rm = TRUE)) %&#62;%+ ungroupNow we can plot it. With the temperature in Europe, per month.&#62; ggplot(data = temp_moy_hist,  aes(x = lon, y = lat, fill = temp)) +   geom_raster() + coord_quickmap() +  facet_wrap(~month)Actually, it is possible to add contour lines of European countries,&#62; download.file("http://freakonometrics.free.fr/carte_europe_df.rda",destfile="carte_europe_df.rda")&#62; load("carte_europe_df.rda")If we aggregate all the month, and get only one graph, we have&#62; ggplot() +  geom_raster(data = temp_moy_hist,  aes(x = lon, y = lat, fill = temp)) +  geom_polygon(data = carte_pays_europe,   aes(x=long, y=lat, group=group), fill = NA,   col = "black") +  coord_quickmap(xlim = c(-20, 40),   ylim = c(30, 75))I will spend some time now to analyze the maps, but I have been really impressed by those functions, that can partially read a 'big' dataset, to extract some information, and then we can read the file to create a dataset that can be used to get a graph.