| Title: | Download and Read Brazilian Meteorological Data from INMET |
|---|---|
| Description: | Automates the download and processing of historical weather data from the Brazilian National Institute of Meteorology (INMET). It resolves formatting inconsistencies in raw CSV files across different years, removes structural artifacts, standardizes column names, converts timestamps to local Brazilian time zones, and outputs tidy data frames ready for analysis. Data are retrieved from <https://portal.inmet.gov.br/dadoshistoricos>. |
| Authors: | Rodrigo Fonseca Villa [aut, cre] (ORCID: <https://orcid.org/0009-0005-2938-2270>) |
| Maintainer: | Rodrigo Fonseca Villa <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.1.0 |
| Built: | 2026-05-22 07:44:25 UTC |
| Source: | https://github.com/rodrigosqrt3/rmet |
Deletes downloaded ZIP files from the cache directory.
inmet_cache_clear( years = NULL, dest_dir = tools::R_user_dir("rmet", "cache"), ask = interactive() )inmet_cache_clear( years = NULL, dest_dir = tools::R_user_dir("rmet", "cache"), ask = interactive() )
years |
Integer vector of years to remove, or |
dest_dir |
Character. Cache directory. |
ask |
Logical. If |
Invisible NULL.
Convenience function that scans the cache directory and reports which annual ZIPs are present and valid.
inmet_cache_status(dest_dir = tools::R_user_dir("rmet", "cache"))inmet_cache_status(dest_dir = tools::R_user_dir("rmet", "cache"))
dest_dir |
Character. Cache directory (same as |
A data.frame with columns year (integer), path (character),
size_mb (numeric), and valid (logical, whether the ZIP passes
integrity check).
inmet_cache_status()inmet_cache_status()
Downloads one or more annual ZIP files from INMET's historical data portal. Downloads are resumable: if a partial file is already on disk (e.g., from a previous interrupted session), the function picks up where it left off instead of restarting.
inmet_download( years = as.integer(format(Sys.Date(), "%Y")), dest_dir = tools::R_user_dir("rmet", "cache"), max_tries = 15L, quiet = FALSE, force = FALSE )inmet_download( years = as.integer(format(Sys.Date(), "%Y")), dest_dir = tools::R_user_dir("rmet", "cache"), max_tries = 15L, quiet = FALSE, force = FALSE )
years |
Integer vector of years to download. Available years start from 2000. Defaults to the current year. |
dest_dir |
Character. Directory where ZIP files will be saved.
Created automatically if it does not exist. Defaults to a persistent
cache directory under |
max_tries |
Integer. Maximum number of download attempts per year
before giving up. Defaults to |
quiet |
Logical. If |
force |
Logical. If |
INMET's server sometimes drops connections mid-transfer. The function
handles this by using curl's CURLOPT_RESUME_FROM to append to the
partial file on each retry, then validates the result with
utils::unzip(). A Sys.sleep(5) back-off separates retries.
ZIP files are not extracted by this function. Use inmet_read() to
parse the contents directly from the ZIP, or inmet_extract() to unzip
to a directory.
Invisibly returns a named character vector (class character) of
local file paths to the downloaded ZIP files. Names correspond to the
requested years (e.g., "2023"). Years that failed to download are
excluded. If all downloads fail, an empty named character vector is
returned.
paths <- inmet_download(years = 2000, dest_dir = tempdir())paths <- inmet_download(years = 2000, dest_dir = tempdir())
Unzips one or more downloaded INMET annual ZIPs to a target directory, preserving the original CSV files.
inmet_extract( years, dest_dir = tools::R_user_dir("rmet", "cache"), out_dir = file.path(dest_dir, "csv"), overwrite = FALSE, quiet = FALSE )inmet_extract( years, dest_dir = tools::R_user_dir("rmet", "cache"), out_dir = file.path(dest_dir, "csv"), overwrite = FALSE, quiet = FALSE )
years |
Integer vector of years to extract. |
dest_dir |
Character. Directory of ZIP files (same as |
out_dir |
Character. Directory where CSVs will be written. Defaults to
a subdirectory |
overwrite |
Logical. Overwrite existing CSVs? Defaults to |
quiet |
Logical. Suppress messages. Defaults to |
Invisible character vector of paths to all extracted CSV files.
Parses the CSV files inside INMET annual ZIP archives and returns a single
tidy data.frame with consistent column names, correct data types, and a
proper POSIXct timestamp column.
inmet_read( years, stations = NULL, dest_dir = tools::R_user_dir("rmet", "cache"), tz = "America/Sao_Paulo", variables = NULL, start_date = NULL, end_date = NULL, quiet = FALSE )inmet_read( years, stations = NULL, dest_dir = tools::R_user_dir("rmet", "cache"), tz = "America/Sao_Paulo", variables = NULL, start_date = NULL, end_date = NULL, quiet = FALSE )
years |
Integer vector of years to read. Each year must have a
corresponding ZIP file in |
stations |
Character vector of station codes (e.g., |
dest_dir |
Character. Directory containing the ZIP files. Defaults to
the same cache directory used by |
tz |
Character. Time zone for the |
variables |
Character vector of variable names to keep. If |
start_date |
Character. Optional start date in |
end_date |
Character. Optional end date in |
quiet |
Logical. Suppress progress messages. Defaults to |
A data.frame containing the parsed INMET data.
Columns include station identifiers (station_code, region,
state, latitude, longitude, elevation),
a POSIXct datetime column, and various meteorological
measurements (temperature, precipitation, pressure, humidity, wind, and radiation).
Returns an empty data.frame if no data is found.
You can pass a subset of the output column names to variables to limit
what is returned, e.g.:
variables = c("temp_dry_c", "precip_mm", "humid_rel_pct").
inmet_download(), inmet_stations()
df <- inmet_read( years = 2000, stations = "A801", dest_dir = tempdir() ) head(df)df <- inmet_read( years = 2000, stations = "A801", dest_dir = tempdir() ) head(df)
Returns a data.frame of all INMET automatic weather stations (estações
automáticas) with their metadata. The catalogue is bundled with the package
and reflects the stations as of the package release date.
inmet_stations(region = NULL, state = NULL, search = NULL)inmet_stations(region = NULL, state = NULL, search = NULL)
region |
Character vector. Filter by macro-region code(s):
|
state |
Character vector. Filter by two-letter state abbreviation(s)
(e.g., |
search |
Character. A free-text search string applied to the station
name column (case-insensitive, partial matching). If |
A data.frame with columns:
station_codeCharacter. INMET four-character station identifier.
station_nameCharacter. Station name.
regionCharacter. Macro-region code.
stateCharacter. Two-letter state abbreviation.
latitudeNumeric. Decimal degrees (negative = south).
longitudeNumeric. Decimal degrees (negative = west).
elevationNumeric. Altitude in metres.
start_yearInteger. Year from which data is available.
inmet_download(), inmet_read()
# All stations st <- inmet_stations() nrow(st) # Stations in Rio Grande do Sul st_rs <- inmet_stations(state = "RS") # Search by name inmet_stations(search = "porto alegre")# All stations st <- inmet_stations() nrow(st) # Stations in Rio Grande do Sul st_rs <- inmet_stations(state = "RS") # Search by name inmet_stations(search = "porto alegre")
A dataset containing one year of hourly meteorological observations from the automatic station A801 (Porto Alegre, RS), downloaded from INMET.
rmet_examplermet_example
A data frame with columns as returned by inmet_read().
See ?inmet_read for the full variable reference.
INMET – Instituto Nacional de Meteorologia