循环中的SolaR包

时间:2014-08-01 11:56:37

标签: r

我正在尝试使用R包solaR来计算水平面上的全局,漫射和光束辐照度,使用气象站的全球水平数据作为输入。我想计算一个小时读数的84个气象站的结果。这涉及在循环中运行calcG0,但是我在理解错误消息时遇到了问题。

我的数据位于csv文件中,如下例所示:

  

日期,纬度,长,G0

     

23/07/2013 12:00,54.02441365,-8.110721855,565.452

     

23/07/2013 12:00,54.87162166,-8.238676542,289.398

     

23/07/2013 12:00,53.79503931,-8.077173903,240.192

我已经改编了以下代码来源:

solaR timestamp for radiation on a tilted surface

http://www.r-bloggers.com/maps-of-solar-radiation/

如下:

sun <-  read.csv("D:/R_Data_Test/solaR/12noon23July13.csv")

# This takes the data and assigns the timestamp to a certain format and timezone
idx <- as.POSIXct(sun$Date, tz="Europe/London", format='%d/%m/%Y %H:%M')

#This pads the time stamps with an "epsilon" (1.0e-7) increment to make them unique
#make.index.unique(idx)

# Creates a zoo object needed to make the Meteo file for input
z <- zoo(sun[,c('Lat', 'Long','G0')], make.index.unique(idx))

N=nrow(sun)
for (i in 1:N){
lat = as.numeric(sun[i,2])
sol = zoo(z[i,1],as.numeric(z[i,2:4]))
g0 <- calcG0(lat = lat, modeRad = 'bdI', dataRad = sol, keep.night=TRUE, sunGeometry='spencer', corr ="EKDh")
print(i)
print(lat)
print(sol)
print(g0)
}

我收到以下错误消息“rval中的错误[i,j,drop = drop。,...]:下标超出范围”。这似乎表明我的循环不够大但我已经获得了行数。我已经为我的辐照度数据尝试了各种列表和数据帧格式,但这并没有解决问题。任何建议都会非常感激。

2 个答案:

答案 0 :(得分:2)

calcG0fSolDfSolIfCompI的包装,设计 时间序列。只有一行它无法正常工作。该 解决方案是自己计算每个步骤。在这种情况下它非常 很容易,因为你只需要水平的辐射组件 平面上。

library(solaR)


vals <- read.csv(text = "Date, Lat, Long, G0
23/07/2013 12:00, 54.02441365, -8.110721855, 565.452
23/07/2013 12:00, 54.87162166, -8.238676542, 289.398
23/07/2013 12:00, 53.79503931, -8.077173903, 240.192")

lat <- vals$Lat
lon <- vals$Long
G0 <- vals$G0

## Correct time using longitude and time zone information
idxLocal <- as.POSIXct(vals$Date, tz="Europe/London", format='%d/%m/%Y %H:%M')
idxSun <- local2Solar(idxLocal, lon)

comp <- lapply(seq_len(nrow(vals)),
               FUN = function(i){
                   ## Sun geometry
                   sol <- calcSol(lat[i], BTi = idxSun[i],
                                  method = 'spencer')
                   ## Radiation components (horizontal plane)
                   compI <- fCompI(sol, G0I = G0[i],
                                   corr = "EKDh")
                   ## Join results
                   res <- cbind(compI, as.zooI(sol),
                                cbind(lat = lat[i], lon = lon[i]))
               })
comp <- do.call(rbind, comp)

这是结果,索引(zoo)排序indexSun

> comp
                           kt        fd      G0       D0         B0          w
2013-07-23 10:27:02 0.2828014 0.9592704 289.398 277.6109  11.787074 -0.4323576
2013-07-23 10:27:33 0.5470501 0.5574753 565.452 315.2255 250.226467 -0.4301032
2013-07-23 10:27:41 0.2317589 0.9780899 240.192 234.9294   5.262642 -0.4295214
                    aman   cosThzS       AlS        AzS      Bo0         rd
2013-07-23 10:27:02    1 0.7732174 0.8838992 -0.6686494 1023.326 0.09543889
2013-07-23 10:27:33    1 0.7810095 0.8962807 -0.6769279 1033.638 0.09609447
2013-07-23 10:27:41    1 0.7830865 0.8996133 -0.6792742 1036.387 0.09626831
                           rg      lat       lon
2013-07-23 10:27:02 0.1020801 54.87162 -8.238677
2013-07-23 10:27:33 0.1027433 54.02441 -8.110722
2013-07-23 10:27:41 0.1029189 53.79504 -8.077174

答案 1 :(得分:1)

您似乎将z定义为动物园对象,其中包含3列c(&#39; Lat&#39;,&#39; Long&#39;&#39; G0&#39;)但是然后尝试引用sol = zoo(z [i,1],as.numeric(z [i,2:4]))中的第4列。在zoo对象中,索引不是列,但在您的情况下将被引用为index(z)或time(z)。