Project

General

Profile

« Previous | Next » 

Revision d6935666

Added by Adam Wilson almost 9 years ago

  • ID d69356664be7053d785a153b9dfc288dda4ed93c
  • Parent acd9dd79

add timeseries extraction

View differences:

climate/research/timeseries.R
1
library(mapdata)
2
library(maptools)
3
library(raster)
4
library(sp)
5
library(dplyr)
6
library(rgeos)
7
library(foreach)
8

  
9

  
10
## Get all available tiles
11
tiles=list.files("/nobackupp6/aguzman4/climateLayers/out/series_reg5",recursive=T,pattern="gam_CAI_dailyTmax",full=T)
12

  
13
## reconstruct tile/date metadata
14
tdata=data.frame(
15
  path=tiles)
16

  
17
tdata[,c("form","type","var","pred","model","x1","x2","date","x3","lat","lon")]=
18
  do.call(rbind.data.frame,strsplit(sub("[.]tif","",basename(tiles)),split="_"))
19

  
20
tdata$tile=paste0(tdata$lat,"_",tdata$lon)
21
tdata$year=as.numeric(substr(tdata$date,1,4))
22

  
23
## Define domain
24
#data(worldMapEnv)
25
#wld=map('world' , fill = T,plot=F,xlim=c(-20,55),ylim=c(-35,40))
26
#wlds=map2SpatialPolygons(wld,IDs= sapply(strsplit(wld$names, ":"), function(x) x[1]))
27
#projection(wlds)="+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
28

  
29
# Get domain of tiles
30
utile=unique(tdata$tile)
31
stile=do.call(rbind,lapply(1:length(utile),function(u) tdata[tdata$tile==utile[u],][1,]))
32
t1=as(extent(raster(as.character(stile$path[1]))), 'SpatialPolygons')
33
t2=as(extent(raster(as.character(stile$path[2]))), 'SpatialPolygons')
34

  
35
domain=gUnion(t1,t2)
36

  
37

  
38
## Select locations
39
points=spsample(domain,100,type="random")
40

  
41
write.csv(as.data.frame(points),"~/interp/samplePoints.csv")
42

  
43

  
44
## Extract timeseries across tifs
45
#library(doParallel)
46
#registerDoParallel(2)
47

  
48
ytiles=unique(data.frame(year=tdata$year,tile=tdata$tile))
49

  
50
foreach(i=1:nrow(ytiles)) %do% {
51
  tyear=ytiles$year[i]
52
  ttile=ytiles$tile[i]
53

  
54
   outfile=paste0("~/interp/pdata_",ttile,"_",tyear,".csv")
55
    if(!file.exists(outfile)) {
56
    rs=stack(as.character(tdata$path[tdata$year==tyear&tdata$tile==ttile]))
57
    names(rs)=tdata$date[tdata$year==tyear&tdata$tile==ttile]
58
    pdata=data.frame(tile=ttile,year=tyear,as.data.frame(points),extract(rs,points))
59
    write.csv(pdata,outfile)
60
    print(paste(ttile," ",tyear))
61
}
62
}
63

  
64

  
65

  
66
## Make animation
67
foreach(i=1:nrow(ytiles)) %do% {
68
  tyear=ytiles$year[i]
69
  ttile=ytiles$tile[i]
70

  
71
   outfile=paste0("~/interp/pdata_",ttile,"_",tyear,".png")
72
    if(!file.exists(outfile)) {
73
    rs=stack(as.character(tdata$path[tdata$year==tyear&tdata$tile==ttile]))
74
    names(rs)=tdata$date[tdata$year==tyear&tdata$tile==ttile]
75
    rs@z=list(as.Date(substr(names(rs),2,9),"%Y%m%d"))
76
    pdata=data.frame(tile=ttile,year=tyear,as.data.frame(points),extract(rs,points))
77
    write.csv(pdata,outfile)
78
    print(paste(ttile," ",tyear))
79
}
80
}
81

  
82

  
83

  
84

  
85
#animate(rs)
86

  

Also available in: Unified diff