Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 计算纬度余弦作为网格数据权重_R_Geospatial - Fatal编程技术网

R 计算纬度余弦作为网格数据权重

R 计算纬度余弦作为网格数据权重,r,geospatial,R,Geospatial,我目前正在尝试使用对象“RCP1pctCO2Mean”(一种光栅砖)计算降水量的“加权”空间年全球值。这是因为相对于较低纬度地区,该区域的大小在两极呈现不同。我需要为我拥有的138年(即138层)全球降水数据中的每一年做这项工作。其想法是通过使用纬度的余弦(这意味着赤道处的网格单元的权重为1(即0度的余弦为1),而两极的权重为1(余弦为90度为1))。然后,在对所有新导出的网格单元降水量值进行每年的平均后,从第1年到第138年,将这些值制作一个时间序列,有效地创建138(加权)平均值) 对象“R

我目前正在尝试使用对象“RCP1pctCO2Mean”(一种光栅砖)计算降水量的“加权”空间年全球值。这是因为相对于较低纬度地区,该区域的大小在两极呈现不同。我需要为我拥有的138年(即138层)全球降水数据中的每一年做这项工作。其想法是通过使用纬度的余弦(这意味着赤道处的网格单元的权重为1(即0度的余弦为1),而两极的权重为1(余弦为90度为1))。然后,在对所有新导出的网格单元降水量值进行每年的平均后,从第1年到第138年,将这些值制作一个时间序列,有效地创建138(加权)平均值)

对象“RCP1pctCO2Mean”如下所示:

class       : RasterStack 
dimensions  : 64, 128, 8192, 138  (nrow, ncol, ncell, nlayers)
resolution  : 2.8125, 2.789327  (x, y)
extent      : -181.4062, 178.5938, -89.25846, 89.25846  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
names       :    layer.1,    layer.2,    layer.3,    layer.4,    layer.5,    layer.6,    layer.7,        layer.8,    layer.9,   layer.10,   layer.11,   layer.12,   layer.13,   layer.14,   layer.15, ... 
min values  : 0.42964514, 0.43375653, 0.51749371, 0.50838983, 0.45366730, 0.53099146, 0.49757186,  0.45697752, 0.41382199, 0.46082401, 0.45516687, 0.51857087, 0.41005131, 0.45956529, 0.47497867, ...  
max values  :   96.30350,  104.08584,   88.92751,   97.49373,   89.57201,   90.58570,   86.67651,    88.33519,   96.94720,  101.58247,   96.07792,   93.21948,   99.59785,   94.26218,   90.62138, ... 
以下是我迄今为止所做的工作:

newraster <- rasterToPoints(RCP1pctCO2Mean) #This creates a dataframe of the raster stack, but I am not sure if this is necessary?

newraster代码中最大的错误是,应用
rasterToPoints
后,返回的对象是矩阵而不是新光栅。从矩阵中提取信息的方式不同于光栅:

##I am calling this prec_ts, it is a matrix not a new raster
prec_ts <- rasterToPoints(RCP1pctCO2Mean)

##calculate weighting based on latitude
weight <- cos(prec_ts[,"y"]*(pi/180))

##plot weight to see that it is changing as expected, i.e. 1 at equator, near 0 at poles, cosine.
plot(prec_ts[,"y"],weight)

非常感谢你们——事实上,这对我的数据非常有效,你们在每一步的评论都帮助我理解了发生的事情。你帮了大忙-非常感谢!@琼斯博士,我想到了最后一个问题——如果我想将权重应用于RCP1pctCO2Mean,那么光栅堆栈中的原始值将相应地转换,该怎么办?我尝试了以下方法:>data(“wrld_siml”)>b landCO2weightCO2如果您有更多问题,那么您应该发布一个新问题,但这是因为您试图将weigjing应用于堆栈,上次您将其应用于数据帧时。您需要首先使用
光栅点
weight <- getWeights(cos(newraster))

head(weight)

[1] 0.9998492 0.9998492 0.9998492 0.9998492 0.9998492 0.9998492
##I am calling this prec_ts, it is a matrix not a new raster
prec_ts <- rasterToPoints(RCP1pctCO2Mean)

##calculate weighting based on latitude
weight <- cos(prec_ts[,"y"]*(pi/180))

##plot weight to see that it is changing as expected, i.e. 1 at equator, near 0 at poles, cosine.
plot(prec_ts[,"y"],weight)
##now apply weighting to the precipitation columns
prec_ts[,3:ncol(prec_ts)] = apply(prec_ts[,3:ncol(prec_ts)], 2, function(x) x * weight)

##calculate averages
averages = colSums(prec_ts[,3:ncol(prec_ts)])/sum(weight)