如何使用R从1D数组创建网格?

时间:2014-04-01 12:25:53

标签: arrays r matlab

我有一个文件,其中包含代表全球土地面积的209091元素1D二进制数组 可以从这里下载: ftp://sidads.colorado.edu/DATASETS/nsidc0451_AMSRE_Land_Parms_v01/AMSRE_flags_2002/ 我想使用提供的辅助行和列文件.globland_r和globland_c从1D数据数组创建一个完整的,可以从这里下载: ftp://sidads.colorado.edu/DATASETS/nsidc0451_AMSRE_Land_Parms_v01/AMSRE_ancil/

为了这个目的,有一个用Matlab编写的代码,我想将这个Matlab代码翻译成R,但我不知道Matlab

     function [gridout, EASE_r, EASE_s] = mkgrid_global(x)
     %MKGRID_GLOBAL(x)  Creates a matrix for mapping   
     % gridout = mkgrid_global(x) uses the 2090887 element array (x) and returns

     %Load ancillary EASE grid row and column data, where <MyDir> is the path to 
     %wherever the globland_r and globland_c files are located on your machine.
    fid = fopen('C:\MyDir\globland_r','r');
    EASE_r = fread(fid, 209091, 'int16');
    fclose(fid);


     fid = fopen('C:\MyDir\globland_c','r');
     EASE_s = fread(fid, 209091, 'int16');
     fclose(fid);



    gridout = NaN.*zeros(586,1383);

    %Loop through the elment array
    for i=1:1:209091     

    %Distribute each element to the appropriate location in the output
    %matrix (but MATLAB is
    %(1,1)

    end

按照@mdsumner:

的解决方案进行编辑

文件MLLATLSBMLLONLSB (4-byte integers)包含纬度和经度(multiply by 1e-5),用于地理定位全局EASE grid matrix (586×1383) 可以从此处下载MLLATLSBMLLONLSBftp://sidads.colorado.edu/DATASETS/nsidc0451_AMSRE_Land_Parms_v01/AMSRE_ancil/

1 个答案:

答案 0 :(得分:0)

## the sparse dims, literally the xcol * yrow indexes
dims <- c(1383, 586)

cfile <- "ftp://sidads.colorado.edu/DATASETS/nsidc0451_AMSRE_Land_Parms_v01/AMSRE_ancil/globland_c"
rfile <- "ftp://sidads.colorado.edu/DATASETS/nsidc0451_AMSRE_Land_Parms_v01/AMSRE_ancil/globland_r"

## be nice, don't abuse this
col <- readBin(cfile, "integer", n = prod(dims), size = 2, signed = FALSE)
row <- readBin(rfile, "integer", n = prod(dims), size = 2, signed = FALSE)

## example data file
fdat <- "ftp://sidads.colorado.edu/DATASETS/nsidc0451_AMSRE_Land_Parms_v01/AMSRE_flags_2002/flags_2002170A.bin"

dat <- readBin(fdat, "integer", n = prod(dims), size = 1, signed = FALSE)


## now get serious
m <- matrix(as.integer(NA), dims[2L], dims[1L])
m[cbind(row + 1L, col + 1L)] <- dat


image(t(m)[,dims[2]:1], col = rainbow(length(unique(m)), alpha = 0.5))

也许我们也可以重建这个地图投影。

flon <- "MLLONLSB"
flat <- "MLLATLSB"
## the key is that these are integers, floats scaled by 1e5 
lon <- readBin(flon, "integer", n = prod(dims), size = 4) * 1e-5
lat <- readBin(flat, "integer", n = prod(dims), size = 4) * 1e-5

## this is all we really need from now on
range(lon)
range(lat)

library(raster)
library(rgdal)  ## need for coordinate transformation
ex <- extent(projectExtent(raster(extent(range(lon), range(lat)), crs = "+proj=longlat"), "+proj=cea"))

grd <- raster(ncols = dims[1L], nrows = dims[2L], xmn = xmin(ex), xmx = xmax(ex), ymn = ymin(ex), ymx = ymax(ex), crs = "+proj=cea")

可能有一个&#34;输出半像素&#34;那里的错误,留作练习。

测试

plot(setValues(grd, m), col = rainbow(max(m, na.rm = TRUE), alpha = 0.5))

Hohum

library(maptools)
data(wrld_simpl)
plot(spTransform(wrld_simpl, CRS(projection(grd))), add = TRUE)

我们现在可以保存有效的单元格数以匹配我们的&#34; grd&#34;模板,然后读取任何特定的dat文件,并使用基于单元格数的值填充模板。此外,似乎有人早些时候走过这条路,但收获的却不多:

How to identify lat and long for a global matrix?