将矩阵转换为栅格时输出错误

时间:2019-05-24 12:16:47

标签: r matrix raster netcdf ncdf4

我正在测试一些将矩阵转换为NetCDF文件的代码。但是输出会产生一个奇怪的栅格/矩阵,其顺序完全不同。知道原因吗?

这是矩阵

mx <- matrix(1:162,ncol=18,byrow=T)
mx
       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18]
 [1,]    1    2    3    4    5    6    7    8    9    10    11    12    13    14    15    16    17    18
 [2,]   19   20   21   22   23   24   25   26   27    28    29    30    31    32    33    34    35    36
 [3,]   37   38   39   40   41   42   43   44   45    46    47    48    49    50    51    52    53    54
 [4,]   55   56   57   58   59   60   61   62   63    64    65    66    67    68    69    70    71    72
 [5,]   73   74   75   76   77   78   79   80   81    82    83    84    85    86    87    88    89    90
 [6,]   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108
 [7,]  109  110  111  112  113  114  115  116  117   118   119   120   121   122   123   124   125   126
 [8,]  127  128  129  130  131  132  133  134  135   136   137   138   139   140   141   142   143   144
 [9,]  145  146  147  148  149  150  151  152  153   154   155   156   157   158   159   160   161   162

这是创建netCDF的代码

library(ncdf4)
library(raster)

LondonResult <- mx

# path and file name, set dname
ncpath <- "C:/Users/ww/Desktop/"
ncname <- "LondonNC"  
ncfname <- paste(ncpath, ncname, ".nc", sep="")
dname <- "NO2"  

##################################################################
lon <- as.array(seq(-177.5, 0, 10)) 
lat <- as.array(seq(-87.5, 0, 10))
time <- seq(as.Date("1970/01/01"), by = "day", length.out = 1) 
time <- as.array(as.numeric(time)) # as.Date(16436,origin="1970-01-01")
tunits <- "days since 2015-01-01 00:00:00"
nlon <- length(seq(-177.5, 0, 10))
nlat <- length(seq(-87.5, 0, 10))
nt <- 1

# create and write the netCDF file -- ncdf4 version
# define dimensions
londim <- ncdim_def("lon","degrees_east",as.double(lon)) 
latdim <- ncdim_def("lat","degrees_north",as.double(lat)) 
timedim <- ncdim_def("time",tunits,as.double(time))

# define variables
fillvalue <- 1e32
dlname <- "Predcited_NO2_Daily_London_Surface"
no2_def <- ncvar_def("NO2","ug/m3",list(londim,latdim,timedim),fillvalue,dlname,prec="single")

# create netCDF file and put arrays
ncout <- nc_create(ncfname,no2_def,force_v4=TRUE)

# put variables
ncvar_put(ncout,no2_def,LondonResult,count=c(nlon, nlat, nt))

# put additional attributes into dimension and data variables
ncatt_put(ncout,"lon","axis","X") #,verbose=FALSE) #,definemode=FALSE)
ncatt_put(ncout,"lat","axis","Y")
ncatt_put(ncout,"time","axis","T")

# Get a summary of the created file:
ncout

# Don't forget to close the file
nc_close(ncout)

输出结果没有错误。但是当我使用raster()打开netCDF文件时,看起来矩阵的顺序完全不同

enter image description here

使用as.matrix(outputNetCDf)

将输出显示为矩阵
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18]
 [1,]   17   35   53   71   89  107  125  143  161    18    36    54    72    90   108   126   144   162
 [2,]   15   33   51   69   87  105  123  141  159    16    34    52    70    88   106   124   142   160
 [3,]   13   31   49   67   85  103  121  139  157    14    32    50    68    86   104   122   140   158
 [4,]   11   29   47   65   83  101  119  137  155    12    30    48    66    84   102   120   138   156
 [5,]    9   27   45   63   81   99  117  135  153    10    28    46    64    82   100   118   136   154
 [6,]    7   25   43   61   79   97  115  133  151     8    26    44    62    80    98   116   134   152
 [7,]    5   23   41   59   77   95  113  131  149     6    24    42    60    78    96   114   132   150
 [8,]    3   21   39   57   75   93  111  129  147     4    22    40    58    76    94   112   130   148
 [9,]    1   19   37   55   73   91  109  127  145     2    20    38    56    74    92   110   128   146

我在实际数据集上使用了相同的代码(这确实很大),并且输出栅格是上下颠倒的。代码中缺少什么吗?非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我相信这是因为存在多种存储(或读取)数组的方式。我想你会得到你想要的

mx <- matrix(1:162,ncol=18,byrow=T)
mx <- mx[nrow(mx):1, ]
mx <- t(mx)

一种替代方法是

library(raster)
mx <- matrix(1:162,ncol=18,byrow=T)
r <- raster(mx)
crs(r) <- "+proj=longlat +datum=WGS84"
extent(r) <- c(-177.5, -7.5, -87.5, -7.5)
r <- writeRaster(r, "test.nc")