Learn R Programming

⚠️There's a newer version (0.18.0) of this package.Take me there.

metR

metR packages several functions and utilities that make R better for handling meteorological data in the tidy data paradigm. It started mostly sa a packaging of assorted wrappers and tricks that I wrote for my day to day work as a researcher in atmospheric sciences. Since then, it has grown organically and for my own needs and feedback from users.

Conceptually it’s divided into visualization tools and data tools. The former are geoms, stats and scales that help with plotting using ggplot2, such as stat_contour_fill() or scale_y_level(), while the later are functions for common data processing tools in the atmospheric sciences, such as Derivate() or EOF(); these are implemented to work in the data.table paradigm, but also work with regular data frames.

Currently metR is in development but maturing. Most functions check arguments and there are some tests. However, some functions might change it’s interface, and functionality can be moved to other packages, so please bear that in mind.

Installation

You can install metR from CRAN with:

install.packages("metR")

Or the development version () with:

# install.packages("devtools")
devtools::install_github("eliocamp/metR@dev")

If you need to read netcdf files, you might need to install the netcdf and udunits2 libraries. On Ubuntu and it’s derivatives this can be done by typing

sudo apt install libnetcdf-dev netcdf-bin libudunits2-dev

Citing the package

If you use metR in your research, please consider citing it. You can get citation information with

citation("metR")
#> 
#> To cite metR in publications use:
#> 
#> 
#> 
#> A BibTeX entry for LaTeX users is
#> 
#>   @Manual{,
#>     title = {metR: Tools for Easier Analysis of Meteorological Fields},
#>     author = {Elio Campitelli},
#>     year = {2021},
#>     note = {R package version 0.13.0.9000},
#>     url = {https://github.com/eliocamp/metR},
#>     doi = {10.5281/zenodo.2593516},
#>   }

Examples

In this example we easily perform Principal Components Decomposition (EOF) on monthly geopotential height, then compute the geostrophic wind associated with this field and plot the field with filled contours and the wind with streamlines.

library(metR)
library(data.table)
library(ggplot2)
data(geopotential)
# Use Empirical Orthogonal Functions to compute the Antarctic Oscillation
geopotential <- copy(geopotential)
geopotential[, gh.t.w := Anomaly(gh)*sqrt(cos(lat*pi/180)),
      by = .(lon, lat, month(date))]
aao <- EOF(gh.t.w ~ lat + lon | date, data = geopotential, n = 1)
aao$left[, c("u", "v") := GeostrophicWind(gh.t.w/sqrt(cos(lat*pi/180)), 
                                                    lon, lat)]

# AAO field
binwidth <- 0.01
ggplot(aao$left, aes(lon, lat)) +
    geom_contour_fill(aes(z = gh.t.w/sqrt(cos(lat*pi/180)), 
                          fill = after_stat(level)), binwidth = binwidth,
                      xwrap = c(0, 360)) +
    geom_streamline(aes(dx = dlon(u, lat), dy = dlat(v)),
                    linewidth = 0.4, L = 80, skip = 3, xwrap = c(0, 360)) +
    scale_x_longitude() +
    scale_y_latitude(limits = c(-90, -20)) +
    scale_fill_divergent_discretised(name = "AAO pattern") +
    coord_polar()
#> Warning in .check_wrap_param(list(...)): 'xwrap' and 'ywrap' will be
#> deprecated. Use ggperiodic::periodic insead.

# AAO signal
ggplot(aao$right, aes(date, gh.t.w)) +
    geom_line() +
    geom_smooth(span = 0.4)
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

You can read more in the vignettes: Visualization tools and Working with data.

Copy Link

Version

Install

install.packages('metR')

Monthly Downloads

4,524

Version

0.14.0

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Elio Campitelli

Last Published

March 23rd, 2023

Functions in metR (0.14.0)

ConvertLongitude

Converts between longitude conventions
MaskLand

Mask
Mag

Magnitude and angle of a vector
Percentile

Percentiles
ReadNetCDF

Read NetCDF files.
JumpBy

Skip observations
Interpolate

Bilinear interpolation
ImputeEOF

Impute missing values
Impute2D

Impute missing values by linear or constant interpolation
MakeBreaks

Functions for making breaks
WaveFlux

Calculate wave-activity flux
geom_contour2

2d contours of a 3d surface
Trajectory

Compute trajectories
geom_arrow

Arrows
coriolis

Effects of the Earth's rotation
WrapCircular

Wrap periodic data to any range
as.path

Interpolates between locations
cut.eof

Remove some principal components.
denormalise

Denormalise eof matrices
Smooth2D

Smooths a 2D field
as.discretised_scale

Create discretised versions of continuous scales
geom_contour_fill

Filled 2d contours of a 3d surface
geom_label_contour

Label contours
geom_relief

Relief Shading
geom_streamline

Streamlines
geom_contour_tanaka

Illuminated contours
guide_colourstrip

Discretized continuous colour guide
guide_vector

Reference arrow for magnitude scales
geopotential

Geopotential height
metR

metR: Tools for Easier Analysis of Meteorological Fields
map_labels

Label longitude and latitude
scale_label_colour_continuous

Scales for contour label aesthetics
logic

Extended logical operators
scale_mag

Scale for vector magnitudes
scale_longitude

Helpful scales for maps
scale_stroke.colour_continuous

Scale for stroke.colour
season

Assign seasons to months
is.cross

Cross pattern
label_placers

Functions to place contour labels
stat_na

Filter only NA values.
stat_subset

Subset values
reverselog_trans

Reverse log transform
scale_divergent

Divergent colour scales
waves

Fourier transform
thermodynamics

Thermodynamics
temperature

Air temperature
surface

Surface height
standard_atmosphere

Standard atmosphere
spherical

Transform between spherical coordinates and physical coordinates
Anomaly

Anomalies
GeostrophicWind

Calculate geostrophic winds
FitLm

Fast estimates of linear regression
GetTopography

Get topographic data
Derivate

Derivate a discrete variable using finite differences
DivideTimeseries

Divides long timeseries for better reading
EPflux

Computes Eliassen-Palm fluxes.
GetSMNData

Get Meteorological data
EOF

Empirical Orthogonal Function