I am using R.Studio0.99 version installed on my HP Laptop 4th generation (8GB RAM) windows 8 operating system and I know how to use R studio and some of the library to incorporate spatial data (raster, rgdal, rasterVis, sp). I am having problem to write the detailed script skeleton for some of operations to perform on 120 raster (10 years) files in tif format of one variable (say X1) and same numbers for second variables (Say X2). Each raster comprises mean monthly values of corresponding variables. Brief information of variable X1 which I retrieved by writing the following code in R-Studio: code:
library(raster)
x <- list.files("C:/site-download/AIRS_Natural_Neigh/", pattern = "*.tif$", full.names = TRUE)
x1 <- x[1]
x1 <- raster(x1)
x1
and the information of variable X1:
class : RasterLayer
dimensions : 250, 598, 149500 (nrow, ncol, ncell)
resolution : 0.598, 0.598 (x, y)
extent : -180.299, 177.305, -60.299, 89.201 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : C:\site-download\AIRS_Natural_Neigh\20020901.tif
names : X20020901
values : 0.0003562359, 0.0003850496 (min, max)
But I want to write the code which retrieved all the data values from the raster and save into the dataframe object so that I could compute the correlation and serial correlation (Lag-1) to compare the both variable of raster values. If break down the whole skeleton of this script It would be like this: 1. read the entire monthly product of 120 raster into the object. 2. First raster having resolution of 2x2.5 degree and second one X2 have the resolution of 0.05 degree. I want to resample the rasters on the same resolution of having 2x2.5 degree. 3. conversion of all raster to pointdataframe or dataframe object 4. Then computation of regression and correlation of each pair of raster 5. Serial correlation (Lag-1) which is possible after retrieving the values from the all raster into dataframe objects. While searching from the website to deal with nc file formate I came across one of the library ncdf in R which can be used to work with nc file in R. I spent some time to write the nice code for reading the nc files and dump into the csv file.
library(ncdf)
ncfiles <- list.files("C:/site-download/AIRS/", pattern='\\.nc$', full.names = TRUE)
ncfiles
ncfname <- ncfiles[1]
ncfname
dname <- "mole_fraction_of_carbon_dioxide_in_free_troposphere"
ncin <- open.ncdf(ncfname)
print(ncin)
lon <- get.var.ncdf(ncin, "Longitude")
nlon <- dim(lon)
head(lon)
lat <- get.var.ncdf(ncin, "Latitude")
nlat <- dim(lat)
head(lat)
print(c(nlon, nlat))
co2array <- get.var.ncdf(ncin, "mole_fraction_of_carbon_dioxide_in_free_troposphere")
fillvalue <- att.get.ncdf(ncin, "mole_fraction_of_carbon_dioxide_in_free_troposphere", "Missval")
fillvalue
co2array[co2array == fillvalue] <- NA
library(RColorBrewer)
image(co2array, col = rev(brewer.pal(10, "RdBu")))
grid <- expand.grid(lon = lon, lat = lat)
lonlat <- expand.grid(lon, lat)
lonlat
head(lonlat)
m <- 1
co2.vec <- as.vector(co2array)
length(co2.vec)
co2.df01 <- data.frame(cbind(lonlat, co2.vec*1000000))
names(co2.df01) <- c("lon", "lat", paste("co2", as.character(m), sep = "_"))
head(na.omit(co2.df01), 20)
csvfile <- "co20020901.csv"
write.table(na.omit(co2.df01), csvfile, row.names = FALSE, sep = ",")
It was really nice to work with this code and I have accomplished to dump one monthly nc file into csvfile. sample of the csv file is as under:
lon lat co2_1
26 -117.5 89.5 381.8020
50 -57.5 89.5 373.8030
147 -175.0 88.0 382.7285
148 -172.5 88.0 373.6800
152 -162.5 88.0 371.6560
153 -160.0 88.0 373.4450
154 -157.5 88.0 374.3705
But this is for one variable I have the another variable of LST from MODIS product of having 0.05 degree resolution in hdf formate. My next task is to convert the hdf files to .tif file and retrieve the values of the same monthly hdf file after resampling into 2x2.5 degree resolution. The LST variable for the same month from MODIS product should be added in 4th column in the csv file. This process should be iterative to update csv file so that the next serial correlation can be performed accordingly.
ncdfpackage as I'm not sure if this one plays nicely with Windows. Also, here is a walk-through for using MODIS data with R (and latterly GRASS GIS): https://scottishsnow.wordpress.com/2014/08/24/many-rastered-beast/. Finally, have a look at raster stacks for reading all raster files. – MikeRSpencer Jun 07 '16 at 11:26