rIACI
is a package designed to calculate the Iberian
Actuarial Climate Index (IACI). This package integrates multiple
standardized climate indices, including temperature, precipitation, wind
power, and sea level data, to support climate change analysis and risk
assessment.
Table of contents - 1. Installation - 2. Dependencies - 2.1. R Dependencies - 2.2. Python Dependencies - 3. Quick Start - 4. Features - 4.1. Data Download - 4.2. Data Processing - 4.3. Climate Index Calculation - 4.4. IACI Calculation - 5. Examples - 6. Notes - 7. Contributing - 8. License
You can install the latest version of the rIACI
package
from GitHub:
R
# Install devtools if not already installed
install.packages("devtools")
# Install rIACI package from GitHub
::install_github("your_username/rIACI") devtools
Please ensure that you have the latest version of R and the necessary dependency packages installed.
The rIACI
package depends on the following R
packages:
Rcpp
ecmwfr
reticulate
lubridate
dplyr
tidyr
magrittr
stats
utils
Some data processing functions in rIACI
rely on Python
scripts. To ensure smooth operation, please install Python (version 3.6
or higher) and the following Python libraries:
xarray
pandas
numpy
dask
Installing Python and Required Libraries: 1. Python Installation:
apt
, yum
) to install Python.2. Installing Libraries via pip
:
Open a terminal or command prompt and run:
bash
pip install xarray pandas numpy
3. Ensuring reticulate
Can Find
Python:
The R package reticulate
needs to interface with Python.
You may need to set the Python path in R:
R
::use_python("/path/to/your/python", required = TRUE) reticulate
Replace "/path/to/your/python"
with the actual path to
your Python executable.
Below are the basic steps to calculate the IACI using the
rIACI
package:
1. Download ERA5-Land Data: Use the
download_data()
function to download necessary
meteorological data from the ECMWF Climate Data Store.
2. Process Data: Use the process_data()
function to process the downloaded NetCDF files and extract required
variables.
3. Export Data to CSV: Use the
export_data_to_csv()
function to export the processed data
to CSV format for further analysis.
4. Calculate Climate Indices: Use the
climate_input()
function to create a climate input object
and then calculate various standardized climate indices.
5. Calculate IACI: Use the
iaci_output()
function to integrate all standardized
indices and calculate the IACI.
6. Output Results: Use the output_all()
function to batch process data for all grid points and save results to a
specified directory.
download_data(
# Starting year
start_year, # Ending year
end_year, # List of variables to download
variables, # Geographical area (North, West, South, East)
area, # Directory to save data
output_dir, # ECMWF user ID
user_id, # ECMWF API key
user_key )
This function downloads ERA5-Land data from the ECMWF Climate Data Store, supporting specific time ranges, variables, and geographical areas.
process_data(
# Input directory containing downloaded NetCDF files
input_dir, # Output directory to save processed data
output_dir, save_merged = FALSE # Whether to save the merged NetCDF file
)
This function processes the downloaded NetCDF files, extracts required variables, and prepares data for further analysis.
climate_input(
# Maximum temperature data
tmax, # Minimum temperature data
tmin, # Precipitation data
prec, # Wind speed data
wind, # Corresponding dates
dates, base.range = c(1961, 1990) # Base year range
)
This function creates a climate input object containing all necessary data and parameters for calculating various climate indices.
You can use the following functions to calculate different climate indices:
tx90p()
: Calculates the percentage of days when maximum
temperature exceeds the 90th percentile.tn90p()
: Calculates the percentage of days when minimum
temperature exceeds the 90th percentile.tx10p()
: Calculates the percentage of days when maximum
temperature is below the 10th percentile.tn10p()
: Calculates the percentage of days when minimum
temperature is below the 10th percentile.rx5day()
: Calculates the maximum consecutive 5-day
precipitation amount.cdd()
: Calculates the maximum length of consecutive dry
days (precipitation less than 1mm).w90p()
: Calculates the percentage of days when wind
power exceeds the 90th percentile.iaci_output(
# Climate input object
ci, # Sea level input data
si, freq = "monthly" # Frequency: "monthly" or "seasonal"
)
This function integrates all standardized climate indices and sea level data to calculate the IACI.
Below is a complete example of calculating the IACI using the
rIACI
package.
1. Download Data
# Set ECMWF user ID and API key
<- "your_user_id" # yourmail@mail.com
user_id <- "your_api_key"
user_key
# Define geographical area (North, West, South, East)
# Example: Iberian Peninsula roughly bounded by 44N, -10W, 35N, 5E
<- c(44, -10, 36, 4)
area
# Download data from the year 1960
download_data(
start_year = 1960,
end_year = 2023,
area = area,
output_dir = "cds_data",
user_id = user_id,
user_key = user_key
)
2. Process Data
# Define input and output directories
<- "path/to/downloaded/cds_data"
input_directory <- "path/to/processed_data"
output_directory
# Process data
process_data(
input_dir = input_directory,
output_dir = output_directory,
save_merged = TRUE
)
3. Export Data to CSV
# Define the processed NetCDF file and output directory
<- "path/to/processed_data.nc"
netcdf_file <- "path/to/csv_output"
csv_output_directory
# Export data to CSV
export_data_to_csv(
nc_file = netcdf_file,
output_dir = csv_output_directory
)
4. Calculate and output IACI for all grids
Integrating Sea Level Data
If you have an existing sea level data file, load it and prepare it for IACI calculations:
# Load sea level data from a CSV file
<- read.csv('path/to/sea_level_data')
sea_data $Date <- format.Date(sea_data$Date,'%Y-%m')
sea_data
# Prepare the sea level input for IACI calculations
<- sea_input(sea_data$Date,sea_data$Sea) si
If you don’t have sea level data, you can generate a blank series for the full time range
# Generate monthly dates from 1960-01 to 2023-12
<- seq(as.Date("1960-01-01"), as.Date("2023-12-01"), by = "month")
sea_dates <- format(sea_dates, "%Y-%m") # Format as "YYYY-MM"
sea_dates
# Generate NA values for each date
<- rep(NA, length(sea_dates))
sea_values
# Combine dates and values into a data frame
<- sea_input(Date = sea_dates, Value = sea_values) si
Calculate IACI for All CSV Files and Output Results
# Define input and output directories
<- "csv_output"
input_dir <- "iaci_results"
output_dir
# Run the output_all function with monthly frequency
output_all(
si = si,
input_dir = input_dir,
output_dir = output_dir,
freq = "monthly",
base.range = c(1961, 1990),
time.span = c(1961, 2023)
)
5. Merge CSVs into NetCDF (optional)
# Define output file
<- "iaci.nc"
merged_netcdf
#Run the CSV to NetCDF Function
csv_to_netcdf(csv_dir = iaci_results_directory, output_file = merged_netcdf)
reticulate
package.Contributions to the rIACI
package are welcome! You can
participate in the following ways: - Submit issues or feature requests -
Submit pull requests to improve the code - Share your experiences and
suggestions
Please read the project’s contribution guidelines before contributing.
The rIACI
package is distributed under the GPL-3
License.
“With gratitude to Jose Luis Vilar-Zanon, Jose Garrido, and Antonio Jose Heras Martinez for their essential support.”
Thank you for using the rIACI package! If you have any questions or suggestions, please feel free to contact us.