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:

x <- list.files("C:/site-download/AIRS_Natural_Neigh/", pattern = "*.tif$",  full.names = TRUE)
x1 <- x[1]
x1 <- raster(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.

ncfiles <- list.files("C:/site-download/AIRS/", pattern='\\.nc$', full.names = TRUE)
ncfname <- ncfiles[1]
dname <- "mole_fraction_of_carbon_dioxide_in_free_troposphere"
ncin <- open.ncdf(ncfname)
lon <- get.var.ncdf(ncin, "Longitude")
nlon <- dim(lon)
lat <- get.var.ncdf(ncin, "Latitude")
nlat <- dim(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")
co2array[co2array == fillvalue] <- NA
image(co2array, col = rev(brewer.pal(10, "RdBu")))
grid <- expand.grid(lon = lon, lat = lat)
lonlat <- expand.grid(lon, lat)
m <- 1
co2.vec <- as.vector(co2array)
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.

  • 1
    I would highly recommend not using RStudio for spatial analysis. For some reason RStudio does not always play nice with R's spatial classes. I have wasted considerable time troubleshooting confounding issues for students that are entirely attributed to RStudio and not underlying code. May 31, 2016 at 18:23
  • If you're going to make such strong changes of resolution, most of the regression output will be meaningless and your rasters will grow to enormous sizes. That suggests staying in raster format and using matrix-based operations to fit the regression (that's simple "map algebra") and compute the correlations (that's a straightforward convolution).
    – whuber
    May 31, 2016 at 18:55
  • @whuber. Actually, these files were in nc file formate and by using the Make NetCDF feature layer tool function in ArcMAP I have exported all these from point file to raster file by natural neighbourhood interpolation. These files were of 2.5x2.0 degree spatial resolution of carbon dioxide (disc.gsfc.nasa.gov/uui/datasets/AIRX3C2M_V005/summary). I have processed all files with default parameter of 0.59 degree resolution so I need to change it back with actual resolution and do it same for temperature data set of MODIS product also.
    – Waseem Ali
    May 31, 2016 at 20:06
  • @JeffreyEvans. Is there any substitute to do it the same process in other software?
    – Waseem Ali
    Jun 5, 2016 at 10:05
  • Well, there is always R. Jun 5, 2016 at 14:08


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.