0

I have had a had time working with rts in R. I have a set of rasters and I need to find the mean over a period of time. This is what I have done so far:

fun1G<-function(x){
 attr1<-"HDFEOS/GRIDS/ColumnAmountNO2/Data Fields/ColumnAmountNO2Trop"
 y<-h5read(x,attr1)
 y[y<=0]=NA
 z<-t(y)
 R1<-raster (z, xmn=-180,xmx=180,ymn=-90,ymx=90)
 R2<-flip (R1,direction="y")
 proj4string(R2)<-CRS("+proj=utm +ellps=WGS84")
 myproj = "+proj=utm +north +zone=28, 29, 30,31, 32, 33 +ellps=WGS84" 
 WA= readShapeSpatial("West_Africa.shp", proj4string = CRS(myproj))
  WAN<- as(WA, 'SpatialPolygons')
 frm <- extent(c(-19, 19,2,29))
 pfrm <- as(frm, 'SpatialPolygons')

  #Create Buffer around west africa (WAN) to crop to good extents
  BUFF2<-gBuffer(WAN,width=1)
  WAE<-crop(R2,pfrm)}

 regexp2<-"([[:digit:]]{4})([[:alpha:]]{1})([[:digit:]]{4})"
##To extract the dates
 DatesOMI<-lapply(OMI, function(x)stri_extract_first_regex(x,regexp2))

 ##To remove "m" from the dates
 DateO2<-gsub("m", "", DatesOMI)
 DDO2<-data.frame(DateO2)
 DDO3<-DDO2[["DateO2"]]

 #COnvert straight to dates
 Timex<-as.Date(DDO3, "%Y%m%d")

 #### TO create the rts(raster time series) and subset
  RNOM2<-stack(CM2)
  NOrts<-rts(RNOM2,Timex)
  RN0411<-NOrts[["2004-10-30/2011-07-10"]]

  ##TO get the monthly data
  RNMM<-apply.monthly(RN0411,mean,na.rm=T)

I am having a hard time subsetting the means for December to february and then generating a mean raster. This is whatI have been able to do to subset for each of these months:

 ###Subset for december, January and february
 Decsub<-subset(RNMM, seq(from=3, to=82, by=12))
 Jansub<-subset(RNMM, seq(from=4, to=82, by=12))
 Febsub<-subset(RNMM, seq(from=5, to=82, by=12))

THis looks like a long way around ans isnt getting me to the final results I need. What are the easiest options to do this, please?

Joke O.
  • 515
  • 6
  • 29

1 Answers1

0

An approach I finally used was:

MMD<-period.apply(Decsub,FUN=mean,na.rm=T,INDEX=...)
MMJ<-period.apply(Jansub,FUN=mean,na.rm=T,INDEX=...)

Where INDEX is the total number of rasters in each subset. Write to file

write.rts(MMD,"Decsub",overwrite=T)
write.rts(CMMW,"Jansub",overwrite=T)
write.rts(CMMW,"Febsub",overwrite=T)

R writes these into individual folders of 3 files so you would need to copy these files into your main working folder. Bring back in as individual rasters

D<-raster("Deccub.gri")
J<-raster("Jansub.gri")
F<-raster("Febsub.gri")

Stack the rasters

DJF<-stack(D,J,F)
DJFmean <- calc(DJF,mean)

Unfortunately, the rts package in R is not so robust to do certain things yet.

Joke O.
  • 515
  • 6
  • 29