oscarperpinan / solar

Solar Radiation and Photovoltaic Systems with R
http://oscarperpinan.github.io/solar/
GNU General Public License v3.0
35 stars 11 forks source link

Problem with 'NA' values in 'Gef' when using 'calcGef' (package version 0.41) #4

Closed StanislawSwierc closed 9 years ago

StanislawSwierc commented 9 years ago

Hi Oscar, Thank you for creating this package! By its description it seems as if it can solve a lot of the problems I will need to deal with in my research on PV systems. I installed your package version 0.41 from CRAN. I wanted to use calcGef to calculate global irradiation on the generator plane from the values of intradaily global and diffuse irradiation on the horizontal plane. When I started using the package with my data I ran into some problems with NA values for Gef.

Could you help me understand if this is a problem in the package itself, or the way I use it?

Please notice that I do not have ambient temperature, but from what I know global and diffuse irradiation should be sufficient for this calculation. That's why I ignored the following warning:

In checkG0Ta(file, maxmin = TRUE) :
  Ambient temperature data is not available. A new column with a constant value
  has been added. 

Here is the code I used:

library("solaR")
library("dplyr")

# Read solar data
channels <- read.csv("channels_solar_30_fixed.csv",
  col.names = c("time", "glob0", "indr0", "glob30", "indr30"))

# Convert radiation from [kW/m2] to [W/m2]
channels_solar[, -1] <- channels_solar[, -1] * 1000

# Make sure global radiation is greater or equal to indirect
channels_solar <- channels_solar %>%
  mutate(
    glob0 = max(glob0, indr0),
    glob30 = max(glob30, indr30))

# Set location for Dublin IE
lat <- 53.347778,
lon <- -6.259722

# Prepare zoo object with the right columns
meteo_zoo <- zoo(
  data.frame(
    G0 = channels_solar$glob0,
    D0 = channels_solar$indr0,
    B0 = channels_solar$glob0 - channels_solar$indr0),
  local2Solar(channels_solar$time, lon=lon))

gef <- calcGef(
  lat,
  dataRad = meteo_zoo,
  modeRad = 'bdI',
  corr = 'none',
  beta = 30,
  alfa = 0)

print(gef)
xyplot(gef)

gef_df <- as.data.frameI(gef)
head(gef_df, 48)

Here is the result for a single day which shows NA values.

                    Gef      Bef Def day month year
2008-12-31 23:34:57   0        0   0 366    12 2008
2009-01-01 00:04:57   0        0   0   1     1 2009
2009-01-01 00:34:57   0        0   0   1     1 2009
2009-01-01 01:04:57   0        0   0   1     1 2009
2009-01-01 01:34:57   0        0   0   1     1 2009
2009-01-01 02:04:57   0        0   0   1     1 2009
2009-01-01 02:34:57   0        0   0   1     1 2009
2009-01-01 03:04:57   0        0   0   1     1 2009
2009-01-01 03:34:57   0        0   0   1     1 2009
2009-01-01 04:04:57   0        0   0   1     1 2009
2009-01-01 04:34:57   0        0   0   1     1 2009
2009-01-01 05:04:57   0        0   0   1     1 2009
2009-01-01 05:34:57   0        0   0   1     1 2009
2009-01-01 06:04:57   0        0   0   1     1 2009
2009-01-01 06:34:57   0        0   0   1     1 2009
2009-01-01 07:04:57   0        0   0   1     1 2009
2009-01-01 07:34:57   0        0   0   1     1 2009
2009-01-01 08:04:57   0        0   0   1     1 2009
2009-01-01 08:34:57  NA 13465037  NA   1     1 2009
2009-01-01 09:04:57  NA  5412793  NA   1     1 2009
2009-01-01 09:34:57  NA  4026320  NA   1     1 2009
2009-01-01 10:04:57  NA  3446681  NA   1     1 2009
2009-01-01 10:34:57  NA  3167176  NA   1     1 2009
2009-01-01 11:04:57  NA  2988023  NA   1     1 2009
2009-01-01 11:34:57  NA  2898145  NA   1     1 2009
2009-01-01 12:04:57  NA  2850976  NA   1     1 2009
2009-01-01 12:34:57  NA  2913485  NA   1     1 2009
2009-01-01 13:04:57  NA  2985269  NA   1     1 2009
2009-01-01 13:34:57  NA  3153930  NA   1     1 2009
2009-01-01 14:04:57  NA  3496556  NA   1     1 2009
2009-01-01 14:34:57  NA  4140284  NA   1     1 2009
2009-01-01 15:04:57  NA  5636209  NA   1     1 2009
2009-01-01 15:34:57  NA 16391656  NA   1     1 2009
2009-01-01 16:04:57   0        0   0   1     1 2009
2009-01-01 16:34:57   0        0   0   1     1 2009
2009-01-01 17:04:57   0        0   0   1     1 2009
2009-01-01 17:34:57   0        0   0   1     1 2009
2009-01-01 18:04:57   0        0   0   1     1 2009
2009-01-01 18:34:57   0        0   0   1     1 2009
2009-01-01 19:04:57   0        0   0   1     1 2009
2009-01-01 19:34:57   0        0   0   1     1 2009
2009-01-01 20:04:57   0        0   0   1     1 2009
2009-01-01 20:34:57   0        0   0   1     1 2009
2009-01-01 21:04:57   0        0   0   1     1 2009
2009-01-01 21:34:57   0        0   0   1     1 2009
2009-01-01 22:04:57   0        0   0   1     1 2009
2009-01-01 22:34:57   0        0   0   1     1 2009
2009-01-01 23:04:57   0        0   0   1     1 2009

Kind regards, Stan

oscarperpinan commented 9 years ago

Hi, I think it's a problem related to your data. Please, paste a link to the channels_solar_30_fixed.csv file so I can play with it. Best.

StanislawSwierc commented 9 years ago

I just published part of the data set and created a gist which loads it. I hope this will make it easy for you to see what is wrong with the data:

https://gist.github.com/StanislawSwierc/2acc1d57eb2bf24e243e

You can also access the data file directly at:

https://gist.githubusercontent.com/StanislawSwierc/4968b804c12fb34b5c01/raw/9445ede6faa8e46c93e2fbfbe2189c009445407e/channels_solar_30_fixed.csv

Kind regards, Stan

oscarperpinan commented 9 years ago

Thanks. The problem is in the mutate section: it sets the variables to the global maximum value, instead of the row wise maximum value. You should use the rowwise() function (although I am not sure because I am not a dplyr user). However, I would use something like:

idx <- with(channels_solar, glob0 < indr0)
channels_solar[idx, -1] <- NA

On the other hand, you can use only the global irradiation measurements without setting the corr argument:

gef <- calcGef(lat, dataRad=meteo_zoo,
               modeRad='bdI',
               beta=30, alfa=0)
StanislawSwierc commented 9 years ago

Yes! That was silly mistake rowwise solves the problem of having max for glob0

channels_solar <- channels_solar %>%
  rowwise() %>%
  mutate(
    glob0 = max(glob0, indr0),
    glob30 = max(glob30, indr30))

However after making the change I'm still seeing some NA values for effective global irradiation.

channels_solar[which(is.na(gef_df$Gef)), ]

It seems this happens for samples where global (glob0) and indirect (indr0) irradiation match or are very close to each other. I will create smaller example with one of these samples tomororw.

                     time        glob0      indr0       glob30       indr30
706   2009-01-15 16:30:00   15.3450000  14.200000 2.180833e+01  14.01666667
754   2009-01-16 16:30:00   11.9150000  11.766667 1.436167e+01  11.48333333
802   2009-01-17 16:30:00   12.6466667  11.620000 1.229833e+01  12.29833333
1074  2009-01-23 08:30:00    3.7916667   3.501667 8.425000e+00   5.93333333
1619  2009-02-03 17:00:00   23.7550000  23.161667 3.785833e+01  37.21333333
2965  2009-03-03 18:00:00   12.3866667  12.161667 1.034667e+01   9.38333333
3013  2009-03-04 18:00:00   14.0083333  14.008333 2.451333e+01  19.45666667
3231  2009-03-09 07:00:00    4.4416667   4.156667 6.146667e+00   4.63000000
3734  2009-03-19 18:30:00   12.6183333  12.286667 1.147833e+01   9.96500000
5320  2009-04-21 19:30:00    6.9716667   6.971667 5.068333e+00   4.77000000
6185  2009-05-09 20:00:00   11.5266667  11.526667 7.051667e+00   7.05166667
6233  2009-05-10 20:00:00   19.6900000  19.690000 1.362500e+01  13.52333333
6281  2009-05-11 20:00:00   16.3633333  16.363333 1.192833e+01  11.92833333
6329  2009-05-12 20:00:00   23.5750000  23.575000 1.386167e+01  13.86166667
6490  2009-05-16 04:30:00    0.9483333   0.700000 9.033333e-01   0.87500000
6538  2009-05-17 04:30:00    0.0000000   0.000000 1.500000e-02   0.01500000
7098  2009-05-28 20:30:00   13.6733333  13.673333 9.076667e+00   9.07666667
7146  2009-05-29 20:30:00    7.6566667   7.656667 6.286667e+00   6.28666667
7335  2009-06-02 19:00:00  290.6466667 290.646667 1.527950e+02 148.52666667
7337  2009-06-02 20:00:00  201.8033333 186.468333 9.824667e+01  88.73333333
7338  2009-06-02 20:30:00   56.8766667  52.658333 4.248167e+01  37.73333333
oscarperpinan commented 9 years ago

The most likely cause is that global irradiance on the horizontal plane is greater than the extra-terrestrial irradiance (here).