sunAngle calculation wrong result

I need to find the hourly sun angle (angle at which the sun strikes the Earth) for a particular position (latitude, longitude) for a given day in Belgium. I used sunAngle() from "oce" package for the 31 July 2020. t is the hourly vector starting a 00:00:00 of that day until 00:00:00 of August 1.

lat = 50.879700
long = 4.700712
t0 <- as.POSIXct(paste("2020-07-31", "00:00:00"), tz="")
t <- seq(from=t0, to=t0+86400, by="60 min")
a <- sunAngle(t, lat, long) 

The result is:

a[["altitude"]]
 [1] -60.225122 -49.174321 -36.305296 -22.665730  -8.661004   5.521092  19.768803
 [8]  33.983602  48.017946  61.503752  72.978661  75.957727  66.875621  53.924237
[15]  40.050031  25.882692  11.628080  -2.604935 -16.717790 -30.567994 -43.875928
[22] -55.964635 -64.970966 -66.929191 -60.402859

However, this output can not be correct as it means that the sun rises at 4 am and sets at 5 pm, which is impossible for a summer day in Belgium.

Any idea of what is wrong?

Hi there,

After taking a closer look, I found two mistakes that when fixed seem to produce the desired results.

  1. Incorrect time zone: Belgium is in central European time, and thus your tz needs to reflect that as sunAngle works from UTC (it will covert it for you)
  2. You made the mistake of flipping longitude and latitude in the sunAngle function (its first long than lat). So your calculations are operating from somewhere in the middle of the Indian Ocean (looked it up :wink: )

Fixing both I get the following

library(oce)
lat = 50.879700
long = 4.700712
t0 <- as.POSIXct(paste("2020-07-31", "00:00:00"), tz="CET")
t <- seq(from=t0, to=t0+86400, by="60 min")
a <- sunAngle(t, long, lat) 
a[["altitude"]]
#>  [1] -16.972638 -20.124305 -20.869145 -19.124371 -15.079994  -9.118646
#>  [7]  -1.689992   6.776607  15.898364  25.319899  34.661770  43.431265
#> [13]  50.879913  55.865336  57.100069  54.164266  47.980165  39.856705
#> [19]  30.764492  21.323888  11.965609   3.050095  -5.060726 -11.964519
#> [25] -17.215318

Created on 2021-12-04 by the reprex package (v2.0.1)

Now the sun come up between 6 - 7h and sets between 21-22h, which does seems more logical :slight_smile:

Small mistakes can generate weird results, but luckily are easy to fix once you find them

Hope this helps,
PJ

1 Like

Thank you very much, this is indeed the results I was looking for!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.