# 6. Calculating Solar Geometry with SZAQ

One of the most fundamental steps in computing scientific analysis on solar radiation or PV power output data, is knowing the position of the sun in the sky.  This is why solar geometry calculations in anusolar are the starting point for anyone learning how to use this code package.

## First, let's review the basics about solar geometry!

If we want to express the position of an object in space, we have to first adopt a coordinate system. For the sun, we use spherical coordinates, but we omit the radial distance of the sun from the center of our coordinate system.  We only need to express the movement of the sun across the sky (azimuth) and its height above the horizon (zenith angle)

Thanks NOAA for this graphic: http://www.esrl.noaa.gov/gmd/grad/solcalc/glossary.html

OK, now let's calculate the sun's position according to these variables, using the...

## The SZAQ Function

Once again, let's use the MLB diffuse fraction example code (using the Engerer2 separation model). You can find the example file in /code/examples/MLB.diffuse_fraction.r.  This loads the required macro metadata (see "working with data"), as well as some radiation data from the Melbourne airport.

# Example script for calculating the diffuse fraction
##----####----####----####----####----##
## First Example: using the Engerer2 & Perez models
# using Melbourne data provided by the Bureau of Meteorology
# for 15 January 2013, only data provided by default in repository

pathto = "" # Enter your path to repository here, end with "/"

# load environment & initialise
source(paste(pathto,"code/rpkg/init.R",sep=""))
init(pathto)

# set the local region ('macro')
mlb = rad.meta("MLB")

# load in example radiation data, 15 Jan 2015 in Melbourne
indf = read.rad(mlb,2013,1,15) # macro, year, month, day, resolution (sec)

# generate solar position info for the time series
sza = SZAQ(indf,mlb)

Once we have the sza list object, we have all the required solar geometry variables for calculating the sun's position, as well as some other helpful outputs:

\$ltms, \$gtms timestamps as described elswhere
\$zen, \$zenr the solar zenith angle in decimal degrees, and radiations, respectively
\$azi, \$azir the solar azimuth angle in degrees/radians from North (0 N, W 90, E -90, S 180)
\$nd, integer day of the year (1-365)
\$ha, the reporting interval of the PV data in seconds
\$dec, decimal time
\$ha, local solar noon in decimal time

So, what can we do with this? Well we can plot the zenith angle with time:

plot(sza\$zen~sza\$ltms,typ='l',col='blue')

We could use the zenith angle to filter out the overnight periods from some PV data:

tmp.Egh = indf\$Egh # get the global radiation values
tmp.Egh[which(sza\$zen>0.9)] = NA

Some basic radiation data quality control!

Or we can pass sza directly to the clear sky radiation modelling function (CSMQ - coming up next!).

/*