[R] FW: How to read multiple raster and serial correlation between series of rasters

Waseem Ali w1malik at gmail.com
Thu Jun 2 13:10:08 CEST 2016




Dear members,
With little effort in producing the code to read all nc files for Carbon dioxide variables I have produced the following code:
library(ncdf)ncfiles <- list.files("C:/site-download/AIRS/", pattern='\\.nc$', full.names = TRUE)ncfilesncfname <- ncfiles[1]ncfnamedname <- "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")fillvalueco2array[co2array == fillvalue] <- NAlibrary(RColorBrewer)image(co2array, col = rev(brewer.pal(10, "RdBu")))grid <- expand.grid(lon = lon, lat = lat)lonlat <- expand.grid(lon, lat)lonlathead(lonlat)m <- 1co2.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 = ",")With the help of this code I can dump the values of Carbon dioxide variables in latitude and longitude wise. sample of the csv file is as under:      lon  lat    co2_126  -117.5 89.5 381.802050   -57.5 89.5 373.8030147 -175.0 88.0 382.7285148 -172.5 88.0 373.6800152 -162.5 88.0 371.6560153 -160.0 88.0 373.4450154 -157.5 88.0 374.3705I need help to tune code to do this with iterative code for whole 120 raster to write it into csv file. Column names should be like as 200301, 200302....up to 200312. This starts from the January of 2003 till December of 2003 and so on for the next year.   My next task to do the same for LST variable retrievable from MODIS LST product of 0.05 degree resolution which is in hdf file formate [I am trying to write the code to interpret the variable ]. I want to resample it first to have on same spatial resolution as my first raster then write to the another csv file in the same way before. After writing it into the csv files I want to correlate the both variables and serial correlation. Please guide to do it using R or any other way to deal with the situations I have briefly explained. Also suggest for interpolation for carbon dioxide variable first to make the raster layer. It may have missing some values.
Waseem Ali 

From: w1malik at hotmail.com
To: r-help at r-project.org
Subject: How to real multiple raster and serial correlation between series of rasters
Date: Sun, 29 May 2016 23:39:18 +0500







Hi,
I have 120 raster (10 years) files
in tif format of one variable (say X1) and same numbers for second variables (Say
X2). Each raster consists the mean monthly values of corresponding variables. I
want to write a script in R which operates the following operations:

·        
First reads the one by one
raster from folder and save into the objects. 

·        
Resample/aggregate the both
raster over same spatial resolution 2˚ x 2.5˚.

·        
After resampling the all
raster over same resolution, conversions of all raster to points by using the
rasterToPoint() function of raster library.

·        
After retrieving the same
monthly raster values (like month of January for X1 and X2) into data frame, I
want to compute regression and correlation values for all 120 raster for both
variables (X1 and X2) and save into the data frame.

Is there any way out to deal with
such task.


library(raster)

x <- list.files("C:/site-download/",
pattern = "*.tif", full.names = TRUE)

x1 <- raster(x1)

p <- as(x1, 'SpatialPixels')

plot(x1)

points(p)
Resultant figure has been attached for you for only x1 variable. I have also attached the X1 and X2 variable tif raster for January 2002 for computation purpose. I need to operate it through loop for reading all these rasters and computing the correlation of each pairs. My next step to compute the Lag -1 correlations which is Serial Correlation for both variables.
Waseem Ali 
 		 	   		  
 		 	   		  


More information about the R-help mailing list