| Title: | Analysis the Weather Data |
|---|---|
| Description: | Functions are collected to analyse weather data for agriculture purposes including to read weather records in multiple formats, calculate extreme climate index. Demonstration data are included the SILO daily climate data (licensed under CC BY 4.0, <https://www.longpaddock.qld.gov.au/silo/>). |
| Authors: | Bangyou Zheng [aut, cre] |
| Maintainer: | Bangyou Zheng <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.3.1 |
| Built: | 2026-05-21 08:50:17 UTC |
| Source: | https://github.com/byzheng/weaana |
Getter to access the weather data at a specific position.
## S4 method for signature 'WeaAna' x[i, j, drop]## S4 method for signature 'WeaAna' x[i, j, drop]
x |
A WeaAna object. |
i |
the specific position which will access. |
j |
None use parameter. |
drop |
None use parameter. |
A WeaAnaSite object at the position i.
library(weaana) data( "WeatherRecordsDemo" ) records[1] records[1:2] records[2:2]library(weaana) data( "WeatherRecordsDemo" ) records[1] records[1:2] records[2:2]
This function adds relative humidity data to APSIM weather records based on a standard weather file.
add_rh(apsim, standard, output = NULL, overwrite = FALSE)add_rh(apsim, standard, output = NULL, overwrite = FALSE)
apsim |
A character string specifying the path to the APSIM weather file. |
standard |
A character string specifying the path to the standard weather file containing relative humidity data. |
output |
A character string specifying the path to save the modified APSIM weather file with relative humidity data. If NULL, the function will not save the file. |
overwrite |
A logical value indicating whether to overwrite the output file if it already exists. Default is FALSE. |
A data frame containing the new weather records with relative humidity added.
Change weather records
Change weather records
changeWeatherRecords(object, ...) ## S4 method for signature 'WeaAna' changeWeatherRecords(object, ...)changeWeatherRecords(object, ...) ## S4 method for signature 'WeaAna' changeWeatherRecords(object, ...)
object |
A WeaAna object. |
... |
New weather records |
A new WeaAna object with updated records
Summarise the climate variable by growth stages
climate_by_stages( climates, sowing, emergence, heading = NULL, flowering = NULL, maturity, latitude )climate_by_stages( climates, sowing, emergence, heading = NULL, flowering = NULL, maturity, latitude )
climates |
a data.frame for climate records |
sowing |
date. an vector of sowing date |
emergence |
numeric (days after sowing). an vector of emergence date |
heading |
numeric (days after sowing). an vector of heading date (optional. see details) |
flowering |
numeric (days after sowing). an vector of flowering time (optional. see details) |
maturity |
numeric (days after sowing). an vector of maturity time |
latitude |
latitude |
Define of growth stages
S0: From start of year to emergence
S1: From emergence to flowering time - 300Cd
S2: From flowering time - 300Cd to flowering time + 100Cd
S3: From flowering time + 100 Cd to flowering time + 600Cd
S4: From flowering + 600Cd to maturity
Climate variables
stage: defination of stages
n: Number of days in each stage
avgt: average temperature (C)
sum.tt: total thermal time (Cd) with base temperature 0C
avg.mint: average minimum temperature
avg.maxt: average maximum temperature
sum.rain: total rainfall
avg.evap: average evapration
avg.radn average radiation
hot.days: number of hot days (daily maximum temperature is more than 30C)
very.hot.days: number of very hot days (daily maximum temperature is more than 35C)
frost.days: number of frost days (daily minimum temperature is less than 0C)
hot.sum: total thermal time above 30C of hot days (daily maximum temperature is more than 30C)
very.hot.sum: total thermal time above 35C of very hot days (daily maximum temperature is more than 35C)
frost.sum: total thermal time below 0C of frost days (daily minimum temperature is less than 0C)
vpd: vapour-pressure deficit
te: transpiration efficiency
bio.radn: bio.radn
bio.water: bio.water
bio.tt: bio.tt
ptq: photothermal quotient
avt.diffuse.radn: average diffuse radiation
a data.frame for summarised climate variable by stages. See details for more information.
if (FALSE) { sowing <- rep(as.Date("1981-05-01"), 10) emergence <- rep(10, 10) heading <- NULL flowering <- runif(10) * 20 + 50 maturity <- runif(10) * 20 + 100 latitude <- -27 res <- climate_by_stages(climates = climates, sowing = sowing, emergence = emergence, heading = heading, flowering = flowering, maturity = maturity, latitude = latitude) }if (FALSE) { sowing <- rep(as.Date("1981-05-01"), 10) emergence <- rep(10, 10) heading <- NULL flowering <- runif(10) * 20 + 50 maturity <- runif(10) * 20 + 100 latitude <- -27 res <- climate_by_stages(climates = climates, sowing = sowing, emergence = emergence, heading = heading, flowering = flowering, maturity = maturity, latitude = latitude) }
Convert a data frame to weaana class
convert2Records(infor, records)convert2Records(infor, records)
infor |
A list or data frame of site information |
records |
A data frame will convert to records |
A new WeaAna object
create WeaAna class
createWeaAna(mets)createWeaAna(mets)
mets |
A list contained information of weather records. |
A new WeaAna class
The time elapsed in hours between the specified sun angle from 90 degree in am and pm. +ve above the horizon, -ve below the horizon.
dayLength(doy, lat, angle = -6)dayLength(doy, lat, angle = -6)
doy |
day of year number |
lat |
latitude of site (deg) |
angle |
angle to measure time between, such as twilight (deg). angular distance between 90 deg and end of twilight - altitude of sun. +ve up, -ve down. |
day length in hours
Calculate the diurnal variation in air temperature. Parton WJ, Logan JA (1981) A model for diurnal variation in soil and air temperature. Agricultural Meteorology, 23, 205?216. Codes copied from APSIM Utilities.cpp
diurnalT(maxt, mint, doy, hour, latitude, A = 1.5, B = 4, C = 1)diurnalT(maxt, mint, doy, hour, latitude, A = 1.5, B = 4, C = 1)
maxt |
maximum daily temperature |
mint |
minimum daily temperature |
doy |
day of year |
hour |
hour from 1 to 24 |
latitude |
latitude in radials |
A |
is the time lag in temperature after noon |
B |
is coef that controls temperature decrease at night |
C |
is the time lag for min temperature after sunrise |
A vector with diurnal air temperature
diurnalT(maxt = 20, mint = 10, doy = 1, hour = seq(from = 1, to = 23.99, by = 0.1), latitude = -10, A = 1.5, B = 4, C = 1)diurnalT(maxt = 20, mint = 10, doy = 1, hour = seq(from = 1, to = 23.99, by = 0.1), latitude = -10, A = 1.5, B = 4, C = 1)
Computes the dew point temperature (°C) using the Magnus–Tetens approximation, a widely used empirical relationship between air temperature, relative humidity, and the saturation vapor pressure of water vapor. The method provides a good approximation for typical atmospheric conditions and is suitable for most meteorological and agricultural applications.
dp_mint(mint, minrh)dp_mint(mint, minrh)
mint |
Numeric. Minimum air temperature (°C). |
minrh |
Numeric. Relative humidity (%) corresponding to the minimum temperature. |
The dew point temperature is estimated using the Magnus–Tetens equation:
where is air temperature (°C), is relative humidity (%),
and the empirical constants are and (for water over a
liquid surface). This formulation is based on improvements to the original
Magnus equation proposed by Alduchov and Eskridge (1996).
For air temperatures below 0 °C, slightly different coefficients are sometimes used to account for ice surfaces, but the above constants are generally suitable for most temperature ranges encountered in agricultural or meteorological data.
Numeric. Dew point temperature (°C).
Alduchov, O. A., & Eskridge, R. E. (1996). Improved Magnus Form Approximation of Saturation Vapor Pressure. Journal of Applied Meteorology, 35(4), 601–609. doi:10.1175/1520-0450(1996)035<0601:IMFAOS>2.0.CO;2
# Dew point for 10 °C and 80% RH dp_mint(10, 80) # Vectorized input dp_mint(c(5, 10, 15), c(90, 70, 50))# Dew point for 10 °C and 80% RH dp_mint(10, 80) # Vectorized input dp_mint(c(5, 10, 15), c(90, 70, 50))
Get all weather records by year range
Get all weather records by year range
getWeatherRecords(object, ...) ## S4 method for signature 'WeaAna' getWeatherRecords(object, yrange = NULL, vars = "all", ...)getWeatherRecords(object, ...) ## S4 method for signature 'WeaAna' getWeatherRecords(object, yrange = NULL, vars = "all", ...)
object |
A WeaAna object. |
... |
Other arguments |
yrange |
Year range. |
vars |
Variable |
A data frame with all weather records
library(weaana) data( "WeatherRecordsDemo" ) getWeatherRecords( records, yrange = c( 2008, 2009 ) ) getWeatherRecords( records, yrange = c( 2008, 2009 ), length = 10 )library(weaana) data( "WeatherRecordsDemo" ) getWeatherRecords( records, yrange = c( 2008, 2009 ) ) getWeatherRecords( records, yrange = c( 2008, 2009 ), length = 10 )
Return a y value from a linear interpolation function
interpolationFunction(x, y, values, split = "\\s+")interpolationFunction(x, y, values, split = "\\s+")
x |
x |
y |
y |
values |
values |
split |
split |
The interpolated values
Calculate the moving values
mov(x, k = 10, shift = "centre", fun = "mean")mov(x, k = 10, shift = "centre", fun = "mean")
x |
A vector to calculate moving values |
k |
The moving windows |
shift |
if shift = "centre", then values are shifted to centre. if shift = "begin", then values are at begin of period. if shift = "end", then values are at end of period. The default value (centre) will be used if shift is other value. |
fun |
The method to calculate moving values. Curruntly, only "mean", "max", "min", and "sum" are supported. A NULL will be returned for any other values |
The moving value of vector x at moving windows k. A NULL will be returned for any unsupported fun
Note that for n = odd, can average at central period. If n = even, must average at end of period and then shift values
mov.avg(x, k = 10, shift = "centre")mov.avg(x, k = 10, shift = "centre")
x |
A vector to calculate moving average |
k |
The moving windows |
shift |
if shift = "centre", then values are shifted to centre. if shift = "begin", then values are at begin of period. if shift = "end", then values are at end of period. The default value (centre) will be used if shift is other value. |
The moving average of vector x at moving windows n
Calculate the moving maximum. For compatibility only.
mov.max(x, k, shift = "centre")mov.max(x, k, shift = "centre")
x |
A vector to calculate moving maximum |
k |
The moving windows |
shift |
if shift = "centre", then values are shifted to centre. if shift = "begin", then values are at begin of period. if shift = "end", then values are at end of period. The default value (centre) will be used if shift is other value. |
The moving maximum of vector x at moving windows k
Calculate the moving minimum. For compatibility only.
mov.min(x, k, shift = "centre")mov.min(x, k, shift = "centre")
x |
A vector to calculate moving minimum |
k |
The moving windows |
shift |
if shift = "centre", then values are shifted to centre. if shift = "begin", then values are at begin of period. if shift = "end", then values are at end of period. The default value (centre) will be used if shift is other value. |
The moving minimum of vector x at moving windows k
Calculate the moving sum. For compatibility only.
mov.sum(x, k, shift = "centre")mov.sum(x, k, shift = "centre")
x |
A vector to calculate moving sum |
k |
The moving windows |
shift |
if shift = "centre", then values are shifted to centre. if shift = "begin", then values are at begin of period. if shift = "end", then values are at end of period. The default value (centre) will be used if shift is other value. |
The moving sum of vector x at moving windows k
Read weather records from a file list and/or a folder list
readWeatherRecords( dataFiles = NULL, dataFolders = NULL, dataFormat = "APSIM", dataWeather = NULL, load.later = FALSE, ... )readWeatherRecords( dataFiles = NULL, dataFolders = NULL, dataFormat = "APSIM", dataWeather = NULL, load.later = FALSE, ... )
dataFiles |
A character vector to specify the path of weather data files. |
dataFolders |
A character vector to specify the path of weather data folders. |
dataFormat |
The format of weather data file. |
dataWeather |
A data.frame for existing data. |
load.later |
Whether load weather records now or later. "dataFroamt" should be One of "APSIM" and "RDATA". |
... |
Other arguments |
A WeaAna class which contains all weather data.
Demo weather records
recordsrecords
An object of class WeaAna of length 1.
Define the class for statistics results
nameName of result
typeType of result
Show the name, number, latitude, longitude of all weather stations.
## S4 method for signature 'WeaAna' show(object)## S4 method for signature 'WeaAna' show(object)
object |
WeaAna objects |
library(weaana) data( "WeatherRecordsDemo" ) show( records ) recordslibrary(weaana) data( "WeatherRecordsDemo" ) show( records ) records
Get site information
Get site information
Get site information
siteInfor(object, ...) ## S4 method for signature 'WeaAna' siteInfor(object, load.now = FALSE) ## S4 method for signature 'WeaAnaSite' siteInfor(object, load.now = FALSE)siteInfor(object, ...) ## S4 method for signature 'WeaAna' siteInfor(object, load.now = FALSE) ## S4 method for signature 'WeaAnaSite' siteInfor(object, load.now = FALSE)
object |
A WeaAnaSite object. |
... |
Not used |
load.now |
Whether load site information |
Site information in the WeaAna object
Site information in the WeaAnaSite object
library(weaana) data( "WeatherRecordsDemo" ) siteInfor( records ) siteInfor( records, load.now = TRUE )library(weaana) data( "WeatherRecordsDemo" ) siteInfor( records ) siteInfor( records, load.now = TRUE )
Calculate the sphere distance
sphericalDistance(lat1, lon1, lat2, lon2)sphericalDistance(lat1, lon1, lat2, lon2)
lat1 |
Latitude |
lon1 |
Longitude |
lat2 |
Latitude |
lon2 |
Longitude |
Distance in km
Calculate thermal time using cardinal temperatures
thermalTime(weather, x_temp, y_temp, method = NULL)thermalTime(weather, x_temp, y_temp, method = NULL)
weather |
WeaAna object |
x_temp |
The cardinal temperatures |
y_temp |
The effective thermal time |
method |
The method to calculate thermal time. The default method is ( maxt + mint ) / 2 - base. The three hour temperature methods will be used if method = '3hr' |
A data.frame with three columns: year, day and thermalTime.
met_file <- system.file("extdata/WeatherRecordsDemo1.met", package = "weaana") records <- readWeatherRecords(met_file) x_temp <- c(0, 26, 34) y_temp <- c(0, 26, 0) res <- thermalTime(records, x_temp, y_temp) head(res) res <- thermalTime(records, x_temp, y_temp, method = "3hr") head(res)met_file <- system.file("extdata/WeatherRecordsDemo1.met", package = "weaana") records <- readWeatherRecords(met_file) x_temp <- c(0, 26, 34) y_temp <- c(0, 26, 0) res <- thermalTime(records, x_temp, y_temp) head(res) res <- thermalTime(records, x_temp, y_temp, method = "3hr") head(res)
Calculate thermal time using cardinal temperatures
thermalTimeDaily(mint, maxt, x_temp, y_temp, method = NULL)thermalTimeDaily(mint, maxt, x_temp, y_temp, method = NULL)
mint |
The minimum temperature |
maxt |
The maximum temperature |
x_temp |
The cardinal temperatures |
y_temp |
The effective thermal time |
method |
The method to calculate thermal time. The default method is ( maxt + mint ) / 2 - base. The three hour temperature methods will be usesd if method = '3hr' |
The thermal time.
mint <- c(0, 10) maxt <- c(30, 40) x_temp <- c(0, 20, 35) y_temp <- c(0, 20, 0) thermalTimeDaily(mint, maxt, x_temp, y_temp) thermalTimeDaily(mint, maxt, x_temp, y_temp, method = '3hr')mint <- c(0, 10) maxt <- c(30, 40) x_temp <- c(0, 20, 35) y_temp <- c(0, 20, 0) thermalTimeDaily(mint, maxt, x_temp, y_temp) thermalTimeDaily(mint, maxt, x_temp, y_temp, method = '3hr')
Calculate thermal time using the hourly temperature (non daily temperature)
thermalTimeHourly(timestamp, temperature, x_temp, y_temp)thermalTimeHourly(timestamp, temperature, x_temp, y_temp)
timestamp |
The timestamp of weather records |
temperature |
The temperature |
x_temp |
The cardinal temperatures |
y_temp |
The effective thermal time |
A data frame with daily thermal time
met_file <- system.file("extdata/WeatherHourly.csv", package = "weaana") hourly <- read.csv(met_file, as.is = TRUE) hourly$timestamp <- as.POSIXct(hourly$timestamp, format = "%Y-%m-%dT%H:%M:%SZ") x_temp <- c(0, 20, 35) y_temp <- c(0, 20, 0) thermalTimeHourly(hourly$timestamp, hourly$temperature, x_temp, y_temp)met_file <- system.file("extdata/WeatherHourly.csv", package = "weaana") hourly <- read.csv(met_file, as.is = TRUE) hourly$timestamp <- as.POSIXct(hourly$timestamp, format = "%Y-%m-%dT%H:%M:%SZ") x_temp <- c(0, 20, 35) y_temp <- c(0, 20, 0) thermalTimeHourly(hourly$timestamp, hourly$temperature, x_temp, y_temp)
Computes hourly estimates of air temperature (Ta) from daily Tmin, Tmax, and day length using the method of Goudriaan (1994). The function assumes a sinusoidal temperature variation during daylight hours and an exponential decay after sunset.
tt_hourly_pp(tmin, tmax, tmax_b, tmin_a, lat, doy)tt_hourly_pp(tmin, tmax, tmax_b, tmin_a, lat, doy)
tmin |
Numeric. Minimum temperature of the day (°C). |
tmax |
Numeric. Maximum temperature of the day (°C). |
tmax_b |
Numeric. Maximum temperature from the previous day (°C). |
tmin_a |
Numeric. Minimum temperature of the following day (°C). |
lat |
Numeric. Latitude of the location (degrees, -90 to 90). |
doy |
Integer. Day of the year (1 to 366). |
- **Daytime interpolation**: During sunlight hours, Ta is estimated using a sinusoidal curve fitted between Tmin and Tmax. - **Nighttime interpolation**: After sunset, Ta follows an exponential decline from the sunset temperature to the next morning's Tmin. - **Sunrise and Sunset Calculation**: - Sunrise hour: 'hsrise = 12 - d/2' - Sunset hour: 'hsset = 12 + d/2' - Tmax occurs at hour = 13.5.
If the Controlled Environment module is used for weather data, the function takes hourly values from that instead of computing them.
A numeric vector of length 24 containing the estimated hourly air temperatures (°C).
Goudriaan, J. (1994). Advanced Methods for Calculating Radiation Models. Netherlands Journal of Agricultural Science, 42(4), 315-323.
tmin <- 10 tmax <- 20 tmax_b <- 21 tmin_a <- 11 lat <- -27 doy <- 10 hourly_temps <- tt_hourly_pp(tmin, tmax, tmax_b, tmin_a, lat, doy)tmin <- 10 tmax <- 20 tmax_b <- 21 tmin_a <- 11 lat <- -27 doy <- 10 hourly_temps <- tt_hourly_pp(tmin, tmax, tmax_b, tmin_a, lat, doy)
Method is presented by Santer et al. 2000
ttest_ts(y, slope = NULL)ttest_ts(y, slope = NULL)
y |
A vector of time serial data |
slope |
Whether export slope |
p values of t-test
There are two modes to use wcal, function mode if FUN is not null,
and string formula mode if FUN is NULL.
wcal(object, ...) ## S4 method for signature 'WeaAna' wcal(object, FUN = NULL, ..., var.args = NULL, var.name = NULL)wcal(object, ...) ## S4 method for signature 'WeaAna' wcal(object, FUN = NULL, ..., var.args = NULL, var.name = NULL)
object |
A WeaAna objects. |
... |
Optional arguments to |
FUN |
A function to be used which results should have the same length as original records. |
var.args |
Arguments of weather variable pass to |
var.name |
Variable name is used if |
library(weaana) data( "records" ) # Daily mean temperature wcal( records, avgt2 = "( maxt + mint ) / 2" ) # Moving average temperature wcal( records, FUN = mov.avg, var.args = "avgt", k = 5, shift = "begin", var.name = "mov.avg" )library(weaana) data( "records" ) # Daily mean temperature wcal( records, avgt2 = "( maxt + mint ) / 2" ) # Moving average temperature wcal( records, FUN = mov.avg, var.args = "avgt", k = 5, shift = "begin", var.name = "mov.avg" )
Define the class for multiple sites
numtotal number of weather station
recordsA pointer vector to weather records of each site
resultA pointer for all results name and type.
Define the class of WeaAna
nameName of weather station
numberStation number of weather station
latitudeLatitude of weather station
longitudeLatitude of weather station
tavAnnual average ambient temperature
ampAnnual amplitude in mean monthly temperature
markerThe extra marker for this site
yearA vector of year of weather station
dayA vector of day of weather station
radnA vector of radiation of weather station
maxtA vector of maximum temperature of weather station
mintA vector of minimum temperature of weather station
evapA vector of evaporation of weather station
rainA vector of rainfall of weather station
vpA vector of pressure atmosphere of weather station
codeThe 6 digit code indicates the source of the 6 data columns
extraA list of variables need to store
resAll statistics results store in this slot
figuresA list to store all plotted figures.
file.pathThe file path for this site.
data.formatThe data format for this site.
load.laterWhether are records loaded laterly.
Write weather records into file
Write weather records into file
writeWeatherRecords(object, ...) ## S4 method for signature 'WeaAna' writeWeatherRecords(object, file, cols = NULL)writeWeatherRecords(object, ...) ## S4 method for signature 'WeaAna' writeWeatherRecords(object, file, cols = NULL)
object |
A WeaAna object. |
... |
Not used |
file |
Path of output file. |
cols |
Columns to export. All columns exported if NULL |
No return values