Hello everyone, R team,
Sorry in advance for this long message. Your help will be invaluable.
For a few days now i?have been blocked to execute a task on R.?I will
try to synthesize my problem.
I have several raster.?I have an ASCII file for each day of a year
with a single band. For 30 years, and for three climatic variables on
grid 10km/10km (T_min, T_max, Precipitation). So?i?have a total around
of 32 940?raster files (366days*30years*3variables).
Also, i have a layer of aroud 1000 points.
I tried to use the Stack function and then make the intersection for
each raster files with my 1000 points.
I cannot create an independent matrix for all my files where i?applied
the "extract" function, to then concatenate all my matrices in order
to have a single table.
I tried this, exemple for 10 years et only T_Max (my files are
organized the same for my two other variables)? :
*#Datapoints*
Datapoints<-readOGR(dsn="H:/Inventaire/R/final",
? ? ? ? ? ? ? ?layer="Centroid_champs")
Datapoints<- spTransform (Datapoints, CRS ("+init=epsg:4326") ) # 1022
points in the data
st_crs(Datapoints)
*#Rasters files*
folders = list(
? file.path('N:','Data','Climate data','Climate-10km','Max_T','1961'),
#Each year includes daily data, the names?of my several raster is
"max1961_1", "max1961_10", "max1961_100", etc...
? file.path('N:','Data','Climate data','Climate-10km','Max_T','1962'),
? file.path('N:','Data','Climate data','Climate-10km','Max_T','1963'),
? file.path('N:','Data','Climate data','Climate-10km','Max_T','1964'),
? file.path('N:','Data','Climate data','Climate-10km','Max_T','1965'),
? file.path('N:','Data','Climate data','Climate-10km','Max_T','1966'),
? file.path('N:','Data','Climate data','Climate-10km','Max_T','1967'),
? file.path('N:','Data','Climate data','Climate-10km','Max_T','1968'),
? file.path('N:','Data','Climate data','Climate-10km','Max_T','1969'),
? file.path('N:','Data','Climate data','Climate-10km','Max_T','1970')
)
files = unlist(sapply(folders, function(folder) {
? list.files(folder, full.names=TRUE)
}))
files
MET <- lapply(files, raster)
s <- raster::stack(MET)
output <- list()
for(i in 1:length(MET)){
? output[[i]] <- extract(s, Datapoints)
? names(output)[[i]] <- paste("Ann?e", MET[i], sep = "_")
}
Also, i tried that :
p1 <- 1022 (ID of my DataPoints)?; p2 <- 1 (column where there are the
values ??extracted from my raster)?; p3 <- 3660? ? ? # 3660matrix (366
day* 10 years)
matlist <- list(array(NA,c(p1,p2,p3)))? # doing a list of independant
matrix
for(i in seq_along(MET)){
? matlist[[i]] <- extract(s, Datapoints)
}
But, nothing works...
I would like my script to perform these actions :
- For each Raster in my Rasterstack, extract the climatic data values
??and link them to my "Datapoints",
- Take the name of my file, take the first three characters of the
name to get a column of my weather variable, here, "T_Max" (column
with my raster values) ; Take the following four characters then
report this information in a new?column "Year",?and finally, take the
last characters of the file name to create a new column "Day".
- Concatenate all the independent output matrices corresponding to
each intersection made with my different raster files
In the end, I would have a huge table, but one that will allow me to
do my analysis :
Table with 9 attributes (6 attributs of my points +?Year + Day +
T_Max) like this :
ID Datapoint Year Day T_Max
1 1960 1
2 1960 1
?... 1960 1
1022 1960 1
1 1960 2
2 1960 2
?... 1960 2
1022 1960 2
?.. ?.. ?..
1 1970 1
2 1970 1
?... 1970 1
1022 1970 1
1 1970 2
2 1970 2
?... 1970 2
1022 1970 2
?.. ?.. ?..
Could a loop do this task ?
I'm sorry, i?am gradually learning to manipulate R, but this exercise
is more difficult than expected...Please feel free to tell me if my
question is inappropriate.
Thank you very much in advance for your answers. Your help or your
comments will be really appreciated.
Have a good day.
Ga?tan Martinelli
Water and Agriculture research professional in Quebec.