[R] Problems with bilinear interpolation of a grid

David Winsemius dwinsemius at comcast.net
Sun Jun 21 20:19:58 CEST 2009


Both from the code and the error message it appears that you are  
creating character matrices and unrealistically expecting a function  
designed for numeric input to accept those as arguments.

?sprintf

It appears you are hoping that sprintf gets you something you desire,  
but to my eyes it looks entirely troublesome in this application. You  
should be able to report which line of code is provoking the error.  
I'm guessing it is:
>> loc <- make.surface.grid(list(x=a.mat, y=b.mat))

What happens when you just use numeric arguments?


-- 
David.


On Jun 21, 2009, at 1:33 PM, Andrew Aldersley wrote:

>
> Dear all,
>
> I'm having trouble interpolating a number of gridded datasets that I  
> have. I'm quite new to R so any help/advice that can be offered  
> would be much appreciated!
>
> Firstly I'll describe my dataset. The data is a grid of the planet  
> at 1 degree spatial resolution, with each grid cell containing a  
> value describing a particular variable (e.g. population density) for  
> coordinate pairs. The dimensions of the grid are 360col x 180row.  
> The x-coordinates of the values within the grid are longitudes (from  
> -179.5(W) to 179.5(E)) and the y-coordinates of the grid values are  
> latitudes (from 89.5(N) to -89.5(S)).
>
> What I want to do is convert this 1 degree dataset to a finer  
> resolution of 0.5 degrees, thereby quadrupling the number of cells  
> within the frame (i.e. convert to 720 x 360 grid). I wish to have  
> longitudinal values ranging from -179.75 to 179.75 and latitudinal  
> values from 89.75 to -89.75. I selected the 'interp.surface'  
> function in the 'fields' package to do this as it provides bilinear  
> interpolation from one grid to another. I have written the following  
> code to try and carry out my interpolation...
>
>> library(fields)
>
>> x.mat <- sprintf("%.2f", seq(from = -179.5, to = 179.5,  
>> length=360))  # longitude grid values
>> y.mat <- sprintf("%.2f", seq(from = 89.5, to = -89.5, length=180))   
>> # latitude grid values
>> z.mat <- read.table("BF_200501.txt", colClasses="numeric")  # dataset
>> z.mat <- data.matrix(z.mat)
>> obj <- list(x=x.mat, y=y.mat, z=z.mat)
>
> #Setting up new grid
>> a.mat <- sprintf("%.2f", seq(from = -179.75, to = 179.75, length =  
>> 720))  # longitude grid values
>> b.mat <- sprintf("%.2f", seq(from = 89.75, to = -89.75, length =  
>> 360))  # latitude grid values
>> loc <- make.surface.grid(list(x=a.mat, y=b.mat))
>
> #Interpolation
>> new <- interp.surface(obj, loc)
>
> However I get the following error message:
> Error in x.new - min(x) : non-numeric argument to binary operator
>
> Can anyone explain what this error message means and why I am  
> getting it? Or are there other more effective ways to perform the  
> interpolation on this sort of dataset? Like I said I'm new to R so  
> any advice will be gratefully received.
>
> Many thanks,
>
> Andy
>

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list