0

I am trying to use the R package solaR to calculate global, diffuse and beam irradiance on the horizontal plane, using weather stations’ global horizontal data as input. I would like to calculate results for 84 weather stations for a one-hour reading. This involves running calcG0 in a loop, but I’m having problems understanding the error messages.

My data is in a csv file like the following sample:

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

I’ve adapted the following sources of code:

solaR timestamp for radiation on a tilted surface

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

As follows:

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)
}

I get the following error message “Error in rval[i, j, drop = drop., ...] : subscript out of bounds”. This seems to suggest my loop is not big enough but I’ve obtained the number of rows. I have tried various list and dataframe formats for my irradiance data but this does not solve the problem. Any suggestions would be much appreciated.

Community
  • 1
  • 1
Diane Palmer
  • 49
  • 1
  • 6

2 Answers2

2

calcG0 is a wrapper around fSolD, fSolI, and fCompI, designed for time series. It doesn't work correctly with only one row. The solution is to compute each step on your own. In this case it is very easy because you only need the radiation components on the horizontal plane.

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)

This is the result, a zoo ordered by the index (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
Oscar Perpiñán
  • 4,491
  • 17
  • 28
1

It looks like you're defining z as a zoo object with 3 columns c('Lat', 'Long','G0') but then trying to reference a 4th column in sol = zoo(z[i,1],as.numeric(z[i,2:4])) . In a zoo object, the index is not a column but would be referenced, in your case, as index(z) or time(z).

WaltS
  • 5,410
  • 2
  • 18
  • 24
  • I've modified my code as follows: N=nrow(sun) for (i in 1:N){ lat = as.numeric(sun[i,2]) sol = merge(index(z[i]),coredata(z[i])) g0 <- calcG0(lat = lat, modeRad = 'bdI', dataRad = sol, keep.night=TRUE, sunGeometry='spencer', corr ="EKDh") print(i) print(lat) print(sol) print(g0) } – Diane Palmer Aug 04 '14 at 11:26
  • I've mofidified my code as shown below, but am getting: Error in calcG0(lat = lat, modeRad = "bdI", dataRad = sol, keep.night = TRUE, : dataRad$file should be a character, a data.frame or a zoo. Thank you for any suggestions. N=nrow(sun) for (i in 1:N){ lat = as.numeric(sun[i,2]) sol = merge(index(z[i]),coredata(z[i])) g0 <- calcG0(lat = lat, modeRad = 'bdI', dataRad = sol, keep.night=TRUE, + sunGeometry='spencer', corr ="EKDh") print(i) print(lat) print(sol) print(g0) } – Diane Palmer Aug 04 '14 at 11:31