MagicForrest / DGVMTools

R package for processing, analysing and visualising ouput from Dynamic Global Vegetation Models (DGVMs)
GNU General Public License v3.0
29 stars 22 forks source link

How to convert monthly mgpp to yearly? #52

Closed halima1993 closed 4 years ago

halima1993 commented 4 years ago

Hey Matthew, hope you are well and safe. I want to ask you that I have mgpp out file, and I would like to convert it into yearly mgpp.

my code is

mpi.nee85ccsm4 <- getField(source =cfluxnee1ccsm4, 
                           first =2000, last =2010,  var = "mgpp", verbose = TRUE, spatial.extent = mountains, spatial.extent.id = "SEA") #mainfile

years15<-selectYears(x = mpi.nee85ccsm4, first =2000, last = 2010)
aggregatempi15<-aggregateYears(years15, method="mean")
print(aggregatempi15)
Data: 
       Lon Lat Month        mgpp
    1:  61  25     1 0.067586958
    2:  61  25     2 0.006947563
    3:  61  25     3 0.012223445
    4:  61  25     4 0.011243234
    5:  61  25     5 0.002363636
   ---                          
42776: 105  39     8 0.067545455
42777: 105  39     9 0.056909091
42778: 105  39    10 0.033335656
42779: 105  39    11 0.076677777
42780: 105  39    12 0.000000000

I would like to aggregate months by mean and show a raster file showing Yearly mgpp from 2000-2010. How can I achieve this in DGVMTools in R?

MagicForrest commented 4 years ago

Hi Halima,

Yeah, I am fine. I hope you are also keeping well.

You are looking for the function aggregateSubannual(). It works very similarly for the aggregateYears() function that you used above. In particular is also takes a "method" argument, I guess you will want "sum" in this case although "mean" is also an option. It also takes a "target" argument, which defaults to "Year", so obviously since this is what you want you don't need to specify it.

Does that answer your question?

halima1993 commented 4 years ago

Hey yes thank you so much. I would like to know if I am using the aggregateSubannual() properly in my code. My current code is:

field1<- getField(source =cfluxnee, 
                           first =2000, last =2010,  var = "mgpp", verbose = TRUE, spatial.extent = mountains, spatial.extent.id = "SEA") #mainfile

years15<-selectYears(x = field1, first =2000, last = 2010)
aggregate1<-aggregateSubannual(years15, method = "sum", target = "Year",
                       verbose = FALSE)

rastermpi15<-promoteToRaster(aggregate1, layers = "all", tolerance = 0.01,
                             grid.topology = NULL)

After aggregateSubannual, I convert it to raster, is the usage of aggregateSubannual() done properly in my code above? The output raster shows a raster showing gpp from 2000-2010 right?

MagicForrest commented 4 years ago

Yeah sure. It sums the 12 monthly values of mgpp and gives one value per year. But in the code above you are not aggregating the years, just the months. Your resulting raster will have 11 layers, total gpp for each year from 2000-2010.

If you want to average the years, you need to put back in the aggregateYears() call that you had in your first example:

aggregatempi15<-aggregateYears(years15, method="mean")

I think the point is that you can call aggregateYears() or aggregateSubannual() or both. I think you want to call both.

halima1993 commented 4 years ago

Alright, so is this code fine :

aggregatempi15<-aggregateYears(years15,method="sum")
aggregate1<-aggregateSubannual(years15, method = "sum", target = "Year",
                               verbose = FALSE)
rastermpi15<-promoteToRaster(aggregate1, layers = "all", tolerance = 0.01,
                             grid.topology = NULL)

(Actually I am comparing satellite data with LPJ-DGVM. The satellite data raster (2000-2010) is ranging from 0-6 kgC m-2, however the LPJ data is ranging from 0-2 kgC m-2), hence through this above code does my aggregation code for my LPJ data is correct for the comparison? shall I insert "mean" in both aggregateYears and aggregateSubannual?

MagicForrest commented 4 years ago

Well not quite. But you need to think through what you are doing.

From I can tell you want the mean year of the annual sum. So you need to call aggregateYears() with "mean" and aggregateSubannual() with "sum". Something like

field1<- getField(source =cfluxnee, 
                           first =2000, last =2010,  var = "mgpp", verbose = TRUE, spatial.extent = mountains, spatial.extent.id = "SEA") #mainfile

years15<-selectYears(x = field1, first =2000, last = 2010)

years15.meanyear <- aggregateYears(years15, "mean")

years15.meanyear.annualsum <- aggregateSubannualyears15.meanyear5, "sum")

rastermpi15<- promoteToRaster(years15.meanyear.annualsum, layers = "all", tolerance = 0.01,
                             grid.topology = NULL)

But maybe you actually want annual values for each year from 2000 to 2010? I am not sure... It depends if you added or averaged your years of satellite data.

halima1993 commented 4 years ago

ok thank you so much! Yes I did get a lot of clarification through your answer. You may close this issue. Stay safe and take care!

MagicForrest commented 4 years ago

A pleasure. You take care too!