The R community is great. If you want to learn R the help lists are an invaluable source for getting solutions quickly. I’ll suggest you following the posting guides to get the most out of it. That means reading the manuals first, reading the help archives and when you do post a question, POST CODE. post a working ‘sample problem’
I use the list on a daily basis. Although I haven’t always followed the rules, I found that when you do, you will get help. The quality of the helpers is great. Anyway, i’ve posted some noob questions about raster and as a result the package has got a few more features today. They havent been released to Cran yet, but you can pick them up from R Forge. If you are new to R I will suggest that you wait, as you have to install packages by hand. It’s straight forward, so at your own risk you can do this:
load the library in the standard fashion [library(“raster”)] and you are good to go. ( see ‘bricktest.R’ )
The new capabilities on the brick, are a “cellStats” feature, an ‘as.logical’ feature and some other otions for taking the area, which will get us weights.
Lets recall what we want to do. For every layer we want to take the temps, apply a weight. the weight is simply the contribution that a cell’s area makes to the entire area sampled weighted by the percentage of Ocean in that cell. So Imagine you have just one temperature report in a month. 10C. Imagine that cell is 50% water. Imagine that this cell has an area of 100sqKm. the calculation goes like this:
Areaweight= (Area_of_ cell*percent_of_Ocean_in_Cell)/Total Sum of area of all cells with temperatures
Final temp= temp*Areaweight.
That’s pretty simple and our goal is to do that all with brick math. So conceptually I have this:
BrickOfcellWeights ( the combination you get by multiplying a weighted Area Brick by the ocean percent layer)
The answer is the product of these two. And then you SUM the result per month. cellStats(brick,sum)
From a 3D brick to a vector of temps. To get there, I’ll start with a simple test of the new capability. One feature at time, giving the developers feedback as I go. Ready. What we do is test out the new cellStat()
######### GET the land/ocean Percent of LAND
##### switch it to percent of ocean
# go get the temperatures and read into a brick
# set a zoo type time index (year/month). for plotting You can use ts() is you perfer. I like zoo.
# NOW, in one line calculate the average temperature per Month ( per LAYER, each layer is a month)
# THIS IS NOT AREA WEIGHTED. This is new rasterbrick capability I am testing
That’s it. Now you dont have to loop over layers to pull out the mean temp of each layer in to a vector that is nlayers long. brilliant. Next, I’ll show you how we multiply bricks. In this case I will multiply a brick by a single layer. Later we will have to multiply a brick by a brick. But for now, I’ll just do a simple illustration of brick*layer. I will use the raster that represents the percent of water in each gridcell. This is a constant month to month. The point of this is not to calculate a temperature (yet) but rather just to show that the code works ( the new version of raster). One thing I wanted ( have to read the manual) was a ‘subset’ or ‘window’ like command on the brick. Like so newbrick<-subset(bigbrick, layer=1,nlayers=36). I’ll have to search for that. Anyway, on to the code: get the cell stats(mean) of the (SST*Ocean) and stuff it in a vector that is nlayers long.
At the drop