Arrays 如何使用R从一维阵列创建网格?

Arrays 如何使用R从一维阵列创建网格?,arrays,r,matlab,Arrays,R,Matlab,我有一个文件,其中包含一个209091元素1D二进制数组,表示全球陆地面积 可从此处下载: 我想使用提供的辅助行和列文件从1D数据数组创建完整的行和列。Globaland_r和Globaland_c可从此处下载: 有一个用Matlab编写的代码,我想把这个Matlab代码翻译成R,但我不知道Matlab function [gridout, EASE_r, EASE_s] = mkgrid_global(x) %MKGRID_GLOBAL(x) Creates a mat

我有一个文件,其中包含一个209091元素1D二进制数组,表示全球陆地面积 可从此处下载: 我想使用提供的辅助行和列文件从1D数据数组创建完整的行和列。Globaland_r和Globaland_c可从此处下载:

有一个用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
function[gridout,EASE\u r,EASE\u s]=mkgrid\u global(x)
%MKGRID_GLOBAL(x)创建用于映射的矩阵
%gridout=mkgrid_global(x)使用2090887元素数组(x)并返回
%加载辅助网格行和列数据,其中是到的路径
%无论Globaland_r和Globaland_c文件在您的计算机上位于何处。
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.*零(5861383);
%循环遍历elment数组
对于i=1:1:209091
%将每个元素分配到输出中的适当位置
%矩阵(但MATLAB是
%(1,1)
结束
编辑@mdsumner的以下解决方案: 文件
MLLATLSB
MLLONLSB(4字节整数)
包含纬度和经度
(乘以1e-5)
,用于地理定位整个全球
EASE栅格矩阵(586×1383)
MLLATLSB
MLLONLSB
可从此处下载:

霍姆

现在,我们可以保存有效的手机号码以匹配我们的“grd”模板,然后读取任何特定的dat文件,并使用基于手机号码的值填充模板。此外,似乎有人更早地走了这条路,但收获不多:

## 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")
plot(setValues(grd, m), col = rainbow(max(m, na.rm = TRUE), alpha = 0.5))
library(maptools)
data(wrld_simpl)
plot(spTransform(wrld_simpl, CRS(projection(grd))), add = TRUE)