Title: | Iberian Actuarial Climate Index Calculations for R |
---|---|
Description: | Provides functions to calculate the Iberian Climate Index (IACI) and its components, including temperature, precipitation, wind power, and sea level data. Designed to support climate change analysis and risk assessment. |
Authors: | Nan Zhou [aut, cre] (<https://orcid.org/0009-0002-5166-6753>, With gratitude to Jose Luis Vilar-Zanon, Jose Garrido, and Antonio Jose Heras Martinez for their essential support.) |
Maintainer: | Nan Zhou <[email protected]> |
License: | GPL-3 |
Version: | 1.0.0 |
Built: | 2025-03-18 06:12:44 UTC |
Source: | https://github.com/nan-z-byte/riaci |
Calculates the maximum length of consecutive dry days.
cdd(ci, spells_can_span_years = TRUE, monthly = TRUE)
cdd(ci, spells_can_span_years = TRUE, monthly = TRUE)
ci |
List. Climate input object created by |
spells_can_span_years |
Logical. Whether spells can span across years (default is TRUE). |
monthly |
Logical. Whether to interpolate annual data to monthly data (default is TRUE). |
Data frame with dates and calculated CDD values.
## Not run: # Calculate annual CDD index cdd_values <- cdd(ci, monthly = FALSE) ## End(Not run)
## Not run: # Calculate annual CDD index cdd_values <- cdd(ci, monthly = FALSE) ## End(Not run)
Calculates the standardized consecutive dry days index.
cdd_std(ci, freq = c("monthly", "seasonal"))
cdd_std(ci, freq = c("monthly", "seasonal"))
ci |
List. Climate input object. |
freq |
Character. Frequency of calculation, either "monthly" or "seasonal". |
Data frame with dates and standardized CDD values.
## Not run: # Calculate standardized CDD index on a monthly basis cdd_std_values <- cdd_std(ci, freq = "monthly") ## End(Not run)
## Not run: # Calculate standardized CDD index on a monthly basis cdd_std_values <- cdd_std(ci, freq = "monthly") ## End(Not run)
Processes climate data and calculates necessary statistics for climate index calculations.
climate_input( tmax = NULL, tmin = NULL, prec = NULL, wind = NULL, dates = NULL, base.range = c(1961, 1990), n = 5, quantiles = NULL, temp.qtiles = c(0.1, 0.9), wind.qtile = 0.9, max.missing.days = c(annual = 15, monthly = 3), min.base.data.fraction.present = 0.1 )
climate_input( tmax = NULL, tmin = NULL, prec = NULL, wind = NULL, dates = NULL, base.range = c(1961, 1990), n = 5, quantiles = NULL, temp.qtiles = c(0.1, 0.9), wind.qtile = 0.9, max.missing.days = c(annual = 15, monthly = 3), min.base.data.fraction.present = 0.1 )
tmax |
Numeric vector. Maximum temperature data. |
tmin |
Numeric vector. Minimum temperature data. |
prec |
Numeric vector. Precipitation data. |
wind |
Numeric vector. Wind speed data. |
dates |
Date vector. Dates corresponding to the data. |
base.range |
Numeric vector of length 2. Base range years for calculations (default is c(1961, 1990)). |
n |
Integer. Window size for running averages (default is 5). |
quantiles |
List. Pre-calculated quantiles (optional). |
temp.qtiles |
Numeric vector. Temperature quantiles to calculate (default is c(0.10, 0.90)). |
wind.qtile |
Numeric. Wind quantile to calculate (default is 0.90). |
max.missing.days |
Named numeric vector. Maximum allowed missing days for annual and monthly data (default is c(annual = 15, monthly = 3)). |
min.base.data.fraction.present |
Numeric. Minimum fraction of data required in base range (default is 0.1). |
A list containing processed data and related information for climate index calculations.
## Not run: # Create climate input object ci <- climate_input( tmax = tmax, tmin = tmin, prec = prec, wind = wind, dates = dates ) ## End(Not run)
## Not run: # Create climate input object ci <- climate_input( tmax = tmax, tmin = tmin, prec = prec, wind = wind, dates = dates ) ## End(Not run)
Merges CSV files in a specified directory into a single NetCDF file, completing the grid by filling missing values.
csv_to_netcdf(csv_dir, output_file, freq)
csv_to_netcdf(csv_dir, output_file, freq)
csv_dir |
Character. Directory containing CSV files, each file representing a single latitude-longitude point. The filename format should be 'lat_lon.csv'. |
output_file |
Character. Path to the output NetCDF file. |
freq |
Character. Frequency of the data, either ''monthly'‘ or '’seasonal''. - ''monthly'‘ data uses date format '’YYYY-MM''. - ''seasonal'‘ data uses date format like '’YYYY-SSS'‘ (e.g., '’1961-DJF''). |
None. The NetCDF file is saved to the specified location.
## Not run: # Example usage of csv_to_netcdf csv_directory <- "/path/to/csv_files" output_netcdf_file <- "/path/to/output_file.nc" csv_to_netcdf(csv_dir = csv_directory, output_file = output_netcdf_file, freq = "monthly") ## End(Not run)
## Not run: # Example usage of csv_to_netcdf csv_directory <- "/path/to/csv_files" output_netcdf_file <- "/path/to/output_file.nc" csv_to_netcdf(csv_dir = csv_directory, output_file = output_netcdf_file, freq = "monthly") ## End(Not run)
Downloads ERA5-Land data from the ECMWF Climate Data Store for the specified time range and variables. Implements a retry mechanism to handle transient errors during data download.
download_data( start_year, end_year, start_month = 1, end_month = 12, variables = c("10m_u_component_of_wind", "10m_v_component_of_wind", "2m_temperature", "total_precipitation"), dataset = "reanalysis-era5-land", area = c(-90, -180, 90, 180), output_dir = "cds_data", user_id, user_key, max_retries = 3, retry_delay = 5, timeout = 7200 )
download_data( start_year, end_year, start_month = 1, end_month = 12, variables = c("10m_u_component_of_wind", "10m_v_component_of_wind", "2m_temperature", "total_precipitation"), dataset = "reanalysis-era5-land", area = c(-90, -180, 90, 180), output_dir = "cds_data", user_id, user_key, max_retries = 3, retry_delay = 5, timeout = 7200 )
start_year |
Integer. The starting year for data download. |
end_year |
Integer. The ending year for data download. |
start_month |
Integer. The starting month (default is 1). |
end_month |
Integer. The ending month (default is 12). |
variables |
Character vector. Variables to download.
Default includes common variables: |
dataset |
Character. Dataset short name (default is "reanalysis-era5-land"). |
area |
Numeric vector. Geographical area specified as |
output_dir |
Character. Directory to save the downloaded data (default is "cds_data"). |
user_id |
Character. Your ECMWF user ID. |
user_key |
Character. Your ECMWF API key. |
max_retries |
Integer. Maximum number of retry attempts in case of download failure (default is 3). |
retry_delay |
Numeric. Delay between retry attempts in seconds (default is 5). |
timeout |
Numeric. Timeout duration for each request in seconds (default is 7200, i.e., 2 hours). |
None. Data is downloaded to the specified output directory.
## Not run: # Set your ECMWF user ID and key user_id <- "your_user_id" user_key <- "your_api_key" # Define the geographical area (North, West, South, East) area <- c(90, -180, -90, 180) # Global # Download data for 2020 download_data( start_year = 2020, end_year = 2020, variables = c("2m_temperature", "total_precipitation"), area = area, user_id = user_id, user_key = user_key ) ## End(Not run)
## Not run: # Set your ECMWF user ID and key user_id <- "your_user_id" user_key <- "your_api_key" # Define the geographical area (North, West, South, East) area <- c(90, -180, -90, 180) # Global # Download data for 2020 download_data( start_year = 2020, end_year = 2020, variables = c("2m_temperature", "total_precipitation"), area = area, user_id = user_id, user_key = user_key ) ## End(Not run)
Exports data from a NetCDF file to CSV files, one for each latitude and longitude point, including only points where data is present. This function utilizes a Python script to perform the data processing.
export_data_to_csv(nc_file, output_dir)
export_data_to_csv(nc_file, output_dir)
nc_file |
Character. Path to the NetCDF file. |
output_dir |
Character. Output directory where CSV files will be saved. |
The function calls a Python script using the 'reticulate' package to process the NetCDF file. The Python script 'data_processing.py' should be located in the 'python' directory of the 'rIACI' package. Only grid points with available data are exported to CSV files. Each CSV file corresponds to a specific latitude and longitude point.
None. CSV files are saved to the specified output directory.
## Not run: # Example usage of export_data_to_csv netcdf_file <- "/path/to/processed_data.nc" csv_output_directory <- "/path/to/csv_output" export_data_to_csv(nc_file = netcdf_file, output_dir = csv_output_directory) ## End(Not run)
## Not run: # Example usage of export_data_to_csv netcdf_file <- "/path/to/processed_data.nc" csv_output_directory <- "/path/to/csv_output" export_data_to_csv(nc_file = netcdf_file, output_dir = csv_output_directory) ## End(Not run)
Integrates various standardized indices to compute the IACI.
iaci_output(ci, si, freq = c("monthly", "seasonal"))
iaci_output(ci, si, freq = c("monthly", "seasonal"))
ci |
List. Climate input object. |
si |
Data frame. Sea level input data. |
freq |
Character. Frequency of calculation, either "monthly" or "seasonal". |
Data frame with dates and IACI values.
## Not run: # Assume we have a climate input object 'ci' and sea level data 'si' # ci should be created using 'climate_input' function (not provided here) ci <- list(base_range = c(1980, 1990)) si <- sea_input( Date = c("1980-01", "1980-02", "1980-03"), Value = c(1.2, 1.3, 1.4) ) # Calculate the IACI with monthly frequency result <- iaci_output(ci, si, freq = "monthly") ## End(Not run)
## Not run: # Assume we have a climate input object 'ci' and sea level data 'si' # ci should be created using 'climate_input' function (not provided here) ci <- list(base_range = c(1980, 1990)) si <- sea_input( Date = c("1980-01", "1980-02", "1980-03"), Value = c(1.2, 1.3, 1.4) ) # Calculate the IACI with monthly frequency result <- iaci_output(ci, si, freq = "monthly") ## End(Not run)
Aggregates monthly data into seasonal averages.
monthly_to_seasonal(data)
monthly_to_seasonal(data)
data |
Data frame. Monthly data with Date and Value columns. |
Data frame with seasonal data.
## Not run: # Assuming you have monthly data in a data frame 'monthly_data' # with columns 'Date' (in 'YYYY-MM' format) and 'Value' seasonal_data <- monthly_to_seasonal(monthly_data) ## End(Not run)
## Not run: # Assuming you have monthly data in a data frame 'monthly_data' # with columns 'Date' (in 'YYYY-MM' format) and 'Value' seasonal_data <- monthly_to_seasonal(monthly_data) ## End(Not run)
Processes all CSV files in the input directory and outputs the results to the output directory.
output_all( si, input_dir, output_dir, freq = c("monthly", "seasonal"), base.range = c(1961, 1990), time.span = c(1961, 2022) )
output_all( si, input_dir, output_dir, freq = c("monthly", "seasonal"), base.range = c(1961, 1990), time.span = c(1961, 2022) )
si |
Data frame. Sea level input data. |
input_dir |
Character. Directory containing input CSV files. |
output_dir |
Character. Directory to save output files. |
freq |
Character. Frequency of calculation, either "monthly" or "seasonal". |
base.range |
Numeric vector. Base range years (default is c(1961, 1990)). |
time.span |
Numeric vector. Time span for output data (default is c(1961, 2022)). |
None. Results are saved to the output directory.
## Not run: # Assume we have sea level data 'si' and input/output directories si <- sea_input( Date = c("1980-01", "1980-02", "1980-03"), Value = c(1.2, 1.3, 1.4) ) input_dir <- "path/to/input/csv/files" output_dir <- "path/to/save/output/files" # Run the output_all function with monthly frequency output_all(si, input_dir, output_dir, freq = "monthly") ## End(Not run)
## Not run: # Assume we have sea level data 'si' and input/output directories si <- sea_input( Date = c("1980-01", "1980-02", "1980-03"), Value = c(1.2, 1.3, 1.4) ) input_dir <- "path/to/input/csv/files" output_dir <- "path/to/save/output/files" # Run the output_all function with monthly frequency output_all(si, input_dir, output_dir, freq = "monthly") ## End(Not run)
Processes NetCDF files in the input directory and saves merged and processed data to the output directory.
process_data(input_dir, output_dir, save_merged = FALSE)
process_data(input_dir, output_dir, save_merged = FALSE)
input_dir |
Character. Directory containing input NetCDF files. |
output_dir |
Character. Directory to save output files. |
save_merged |
Logical. If TRUE, saves the merged NetCDF file. Default is FALSE. |
None. Outputs are saved to the specified directory.
## Not run: # Example usage of process_data input_directory <- "/path/to/input/netcdf_files" output_directory <- "/path/to/output" process_data(input_dir = input_directory, output_dir = output_directory, save_merged = TRUE) ## End(Not run)
## Not run: # Example usage of process_data input_directory <- "/path/to/input/netcdf_files" output_directory <- "/path/to/output" process_data(input_dir = input_directory, output_dir = output_directory, save_merged = TRUE) ## End(Not run)
Calculates the maximum consecutive 5-day precipitation amount.
rx5day(ci, freq = c("monthly", "annual"), center_mean_on_last_day = FALSE)
rx5day(ci, freq = c("monthly", "annual"), center_mean_on_last_day = FALSE)
ci |
List. Climate input object created by |
freq |
Character. Frequency of calculation, either "monthly" or "annual". |
center_mean_on_last_day |
Logical. Whether to center the mean on the last day (default is FALSE). |
Data frame with dates and calculated Rx5day values.
## Not run: # Calculate monthly Rx5day index rx5day_values <- rx5day(ci, freq = "monthly") ## End(Not run)
## Not run: # Calculate monthly Rx5day index rx5day_values <- rx5day(ci, freq = "monthly") ## End(Not run)
Calculates the standardized Rx5day index.
rx5day_std(ci, freq = c("monthly", "seasonal"))
rx5day_std(ci, freq = c("monthly", "seasonal"))
ci |
List. Climate input object. |
freq |
Character. Frequency of calculation, either "monthly" or "seasonal". |
Data frame with dates and standardized Rx5day values.
## Not run: # Calculate standardized Rx5day index on a monthly basis rx5day_std_values <- rx5day_std(ci, freq = "monthly") ## End(Not run)
## Not run: # Calculate standardized Rx5day index on a monthly basis rx5day_std_values <- rx5day_std(ci, freq = "monthly") ## End(Not run)
Creates a data frame for sea level data input.
sea_input(Date = levels(ci$date_factors$monthly), Value = NA)
sea_input(Date = levels(ci$date_factors$monthly), Value = NA)
Date |
Character vector. Dates in "YYYY-MM" format. |
Value |
Numeric vector. Sea level values (default is NA). |
Data frame with Date and Value columns.
## Not run: # Create a sea level input data frame dates <- c("1980-01", "1980-02", "1980-03") values <- c(1.2, 1.3, 1.4) sea_data <- sea_input(Date = dates, Value = values) # If Value is not provided, it defaults to NA sea_data_na <- sea_input(Date = dates) ## End(Not run)
## Not run: # Create a sea level input data frame dates <- c("1980-01", "1980-02", "1980-03") values <- c(1.2, 1.3, 1.4) sea_data <- sea_input(Date = dates, Value = values) # If Value is not provided, it defaults to NA sea_data_na <- sea_input(Date = dates) ## End(Not run)
Calculates the standardized sea level index.
sea_std(si, ci, freq = c("monthly", "seasonal"))
sea_std(si, ci, freq = c("monthly", "seasonal"))
si |
Data frame. Sea level input data created by |
ci |
List. Climate input object containing the base range (e.g., created by |
freq |
Character. Frequency of calculation, either "monthly" or "seasonal". |
Data frame with dates and standardized sea level values.
## Not run: # Assume you have a climate input object 'ci' and sea level data 'si' # ci should be created using 'climate_input' function ci <- climate_input(...) si <- sea_input(Date = c("1980-01", "1980-02", "1980-03"), Value = c(1.2, 1.3, 1.4)) # Calculate standardized sea level index with monthly frequency sea_std_values <- sea_std(si, ci, freq = "monthly") ## End(Not run)
## Not run: # Assume you have a climate input object 'ci' and sea level data 'si' # ci should be created using 'climate_input' function ci <- climate_input(...) si <- sea_input(Date = c("1980-01", "1980-02", "1980-03"), Value = c(1.2, 1.3, 1.4)) # Calculate standardized sea level index with monthly frequency sea_std_values <- sea_std(si, ci, freq = "monthly") ## End(Not run)
Calculates the combined percentage of days when temperature is below the 10th percentile.
t10p(ci, freq = c("monthly", "annual"))
t10p(ci, freq = c("monthly", "annual"))
ci |
List. Climate input object. |
freq |
Character. Frequency of calculation, either "monthly" or "annual". |
Data frame with dates and calculated T10p values.
## Not run: # Calculate monthly T10p index t10p_values <- t10p(ci, freq = "monthly") ## End(Not run)
## Not run: # Calculate monthly T10p index t10p_values <- t10p(ci, freq = "monthly") ## End(Not run)
Calculates the standardized T10p index.
t10p_std(ci, freq = c("monthly", "seasonal"))
t10p_std(ci, freq = c("monthly", "seasonal"))
ci |
List. Climate input object. |
freq |
Character. Frequency of calculation, either "monthly" or "seasonal". |
Data frame with dates and standardized T10p values.
## Not run: Calculate standardized T10p index on a monthly basis t10p_std_values <- t10p_std(ci, freq = "monthly") ## End(Not run)
## Not run: Calculate standardized T10p index on a monthly basis t10p_std_values <- t10p_std(ci, freq = "monthly") ## End(Not run)
Calculates the combined percentage of days when temperature is above the 90th percentile.
t90p(ci, freq = c("monthly", "annual"))
t90p(ci, freq = c("monthly", "annual"))
ci |
List. Climate input object. |
freq |
Character. Frequency of calculation, either "monthly" or "annual". |
Data frame with dates and calculated T90p values.
## Not run: # Calculate monthly T90p index t90p_values <- t90p(ci, freq = "monthly") ## End(Not run)
## Not run: # Calculate monthly T90p index t90p_values <- t90p(ci, freq = "monthly") ## End(Not run)
Calculates the standardized T90p index.
t90p_std(ci, freq = c("monthly", "seasonal"))
t90p_std(ci, freq = c("monthly", "seasonal"))
ci |
List. Climate input object. |
freq |
Character. Frequency of calculation, either "monthly" or "seasonal". |
Data frame with dates and standardized T90p values.
## Not run: # Calculate standardized T90p index on a monthly basis t90p_std_values <- t90p_std(ci, freq = "monthly") ## End(Not run)
## Not run: # Calculate standardized T90p index on a monthly basis t90p_std_values <- t90p_std(ci, freq = "monthly") ## End(Not run)
Calculates the percentage of days when minimum temperature is below the 10th percentile.
tn10p(ci, freq = c("monthly", "annual"))
tn10p(ci, freq = c("monthly", "annual"))
ci |
List. Climate input object created by |
freq |
Character. Frequency of calculation, either "monthly" or "annual". |
Data frame with dates and calculated TN10p values.
## Not run: # Calculate monthly TN10p index tn10p_values <- tn10p(ci, freq = "monthly") ## End(Not run)
## Not run: # Calculate monthly TN10p index tn10p_values <- tn10p(ci, freq = "monthly") ## End(Not run)
Calculates the percentage of days when minimum temperature is above the 90th percentile.
tn90p(ci, freq = c("monthly", "annual"))
tn90p(ci, freq = c("monthly", "annual"))
ci |
List. Climate input object created by |
freq |
Character. Frequency of calculation, either "monthly" or "annual". |
Data frame with dates and calculated TN90p values.
## Not run: # Calculate monthly TN90p index tn90p_values <- tn90p(ci, freq = "monthly") ## End(Not run)
## Not run: # Calculate monthly TN90p index tn90p_values <- tn90p(ci, freq = "monthly") ## End(Not run)
Calculates the percentage of days when maximum temperature is below the 10th percentile.
tx10p(ci, freq = c("monthly", "annual"))
tx10p(ci, freq = c("monthly", "annual"))
ci |
List. Climate input object created by |
freq |
Character. Frequency of calculation, either "monthly" or "annual". |
Data frame with dates and calculated TX10p values.
## Not run: # Calculate monthly TX10p index tx10p_values <- tx10p(ci, freq = "monthly") ## End(Not run)
## Not run: # Calculate monthly TX10p index tx10p_values <- tx10p(ci, freq = "monthly") ## End(Not run)
Calculates the percentage of days when maximum temperature is above the 90th percentile.
tx90p(ci, freq = c("monthly", "annual"))
tx90p(ci, freq = c("monthly", "annual"))
ci |
List. Climate input object created by |
freq |
Character. Frequency of calculation, either "monthly" or "annual". |
Data frame with dates and calculated TX90p values.
## Not run: # Calculate monthly TX90p index tx90p_values <- tx90p(ci, freq = "monthly") ## End(Not run)
## Not run: # Calculate monthly TX90p index tx90p_values <- tx90p(ci, freq = "monthly") ## End(Not run)
Calculates the percentage of days when wind speed is above the 90th percentile.
w90p(ci, freq = c("monthly", "annual"))
w90p(ci, freq = c("monthly", "annual"))
ci |
List. Climate input object created by |
freq |
Character. Frequency of calculation, either "monthly" or "annual". |
Data frame with dates and calculated W90p values.
## Not run: # Calculate monthly W90p index w90p_values <- w90p(ci, freq = "monthly") ## End(Not run)
## Not run: # Calculate monthly W90p index w90p_values <- w90p(ci, freq = "monthly") ## End(Not run)
Calculates the standardized W90p index.
w90p_std(ci, freq = c("monthly", "seasonal"))
w90p_std(ci, freq = c("monthly", "seasonal"))
ci |
List. Climate input object. |
freq |
Character. Frequency of calculation, either "monthly", "seasonal". |
Data frame with dates and standardized W90p values.
## Not run: # Calculate standardized W90p index on a monthly basis w90p_std_values <- w90p_std(ci, freq = "monthly") ## End(Not run)
## Not run: # Calculate standardized W90p index on a monthly basis w90p_std_values <- w90p_std(ci, freq = "monthly") ## End(Not run)