Package Exports
- weather-formulas
- weather-formulas/PET
- weather-formulas/UTCI
- weather-formulas/airDensity
- weather-formulas/altitude
- weather-formulas/beaufort
- weather-formulas/diurnalRythm
- weather-formulas/fog
- weather-formulas/heatIndex
- weather-formulas/humidex
- weather-formulas/humidity
- weather-formulas/pressure
- weather-formulas/saffirSimpson
- weather-formulas/temperature
- weather-formulas/uvindex
- weather-formulas/wind
Readme
weather-formulas
A library of atmospheric and weather-related formulas.
import { temperature } from 'weather-formulas';
const dewPoint = temperature.dewPointMagnusFormula(298.15, 60);
console.log(`Dew Point: ${dewPoint} K`);- Test code for all formulas.
- Supports provided and custom valuation sets, and configurable parameters where applicable.
- Supports both ES Module (ESM) and CommonJS (CJS).
Table of Contents
Features
Air Density
| Formula | Description |
|---|---|
| Dry Air Density | Calculate air density for dry air. π |
| Moist Air Density | Calculate air density for moist air, considering humidity. π |
| Air Density at Altitude | Calculate air density at a given altitude. π |
| Decay Constant | Calculate decay constant for air density with altitude. π |
Altitude
| Formula | Description |
|---|---|
| Freezing Level Altitude | Estimate the altitude where temperature drops below freezing. π |
Humidity
| Formula | Description |
|---|---|
| Relative Humidity | Calculate the ratio of the current absolute humidity to the maximum possible humidity. π |
| Specific Humidity | Calculate the mass of water vapor per unit mass of air. π |
| Mixing Ratio | Calculate the ratio of water vapor to dry air. π |
| Vapor Pressure | Calculate the partial pressure of water vapor in the air. π |
| Actual Vapor Pressure | Calculate the partial pressure of water vapor actually present in the air, based on saturation vapor pressure and relative humidity. π |
| Saturation Vapor Pressure | Calculate the maximum vapor pressure at a given temperature. π |
| Dew Point Depression | Calculate the difference between the air temperature and the dew point temperature, indicating how close the air is to saturation. π |
| Lifting Condensation Level | Estimate the altitude at which an air parcel becomes saturated when lifted and condensation begins (cloud base height). π |
Pressure
| Formula | Description |
|---|---|
| Pressure Altitude | Calculate the altitude based on observed pressure. π |
| Density Altitude | Calculate the altitude adjusted for temperature and air density. π |
| Barometric Formula | Calculate pressure at a given altitude using the barometric formula. π |
| Adjust Pressure To Sea Level | Adjust pressure to sea level using various methods: |
| Simple formula | A quick approximation for standard conditions. π |
| Advanced formula | A more accurate calculation using the barometric formula. π |
| By dynamic lapse rate | Adjust pressure using a dynamically calculated lapse rate. π |
| By historical data | Adjust pressure using historical readings. π |
Temperature
| Formula | Description |
|---|---|
| Dew Point | Calculate the dew point using the Magnus formula or Arden Buck equation. π |
| Wind Chill | Estimate the perceived temperature based on wind speed and air temperature. π |
| (Australian) Apparent Temperature | Calculate the apparent temperature considering humidity and wind. π |
| Wet-bulb Temperature | Calculate the wet-bulb temperature. π |
| Equivalent Temperature | Calculate the temperature an air parcel would have if all water vapor were condensed and the latent heat released. π |
| Potential Temperature | Calculate the temperature an air parcel would have if brought to a standard pressure. π |
| Virtual Temperature | Calculate the temperature accounting for water vapor in the air. π |
| Lapse Rate | Calculate the rate of temperature change with altitude. π |
| Dynamic lapse rate | Calculate the lapse rate dynamically based on readings. π |
| Weighted Average Temperature | Calculate the weighted average temperature based on altitude differences. π |
| Adjust Temperature by Lapse Rate | Adjust temperature based on a fixed lapse rate. π |
Wind
| Formula | Description |
|---|---|
| Wind Direction | Convert wind direction in degrees to compass direction. π |
| Wind Power Density | Calculate wind power density in watts per square meter. π |
| Wind Force | Calculate wind force in kilograms per square meter. π |
| Adjust Wind Speed for Altitude | Adjust wind speed between different altitudes based on air density. π |
| Apparent Wind | Calculate the wind speed and direction experienced by a moving observer (e.g., vessel or vehicle). π |
Phenomena
| Formula | Description |
|---|---|
| Fog Visibility | Estimate visibility in fog using Koschmiederβs Law. π |
| Fog Point Temperature | Calculate the fog point temperature (dew point at ground level). π |
| Fog Probability | Predict the probability of fog formation using meteorological factors. |
Scales
| Scale | Description |
|---|---|
| Beaufort Scale | Classify wind speed according to the Beaufort scale. π |
| Saffir-Simpson Scale | Classify hurricane wind speed according to the Saffir-Simpson scale. π |
| UV Index | Classify ultraviolet index. π |
| Heat Index | Measure the perceived temperature based on air temperature and humidity. π |
| Humidex | Calculate the humidex, a Canadian measure of perceived temperature. π |
Install
Install the library using npm:
$ npm install weather-formulasUsage
Note: All temperatures must be provided and are returned in Kelvin (the SI unit for temperature).
ES Modules (ESM)
If your project is configured to use ES Modules (e.g., "type": "module" in package.json), you can use the import syntax to load the package:
import { temperature, humidity, pressure, wind, airDensity } from 'weather-formulas';
// Example usage
const RH = humidity.relativeHumidity(298.15, 293.15); // 25Β°C and 20Β°C in Kelvin
console.log(`Relative Humidity: ${RH}%`);CommonJS (CJS)
If your project uses CommonJS (e.g., no "type": "module" in package.json), you can use the require syntax to load the package:
const { temperature, humidity, pressure, wind, airDensity } = require('weather-formulas');
// Example usage
const RH = humidity.relativeHumidity(298.15, 293.15); // 25Β°C and 20Β°C in Kelvin
console.log(`Relative Humidity: ${RH}%`);TypeScript
import { temperature } from 'weather-formulas';
const dewPoint: number = temperature.dewPointMagnusFormula(298.15, 60); // 25Β°C and 60% humidity
console.log(`Dew Point: ${dewPoint} K`);Advanced Examples
Use a provided valuation set:
const valuationSet = temperature.DEW_POINT_VALUATIONS.DAVID_BOLTON;
const actual = temperature.dewPointMagnusFormula(298.15, 60, valuationSet); // 25Β°C and 60% humidity
console.log(`Dew Point: ${actual} K`);Use a custom valuation set:
const valuationSet = { a: 6, b: 17, c: 250, d: 234.5 };
const actual = temperature.dewPointArdenBuckEquation(298.15, 60, valuationSet); // 25Β°C and 60% humidity
console.log(`Dew Point: ${actual} K`);Inspect code/tests for all possibilities.
Testing
All formulas are covered by automated tests.
Run tests with:
npm run testCompatibility
- Requires Node.js 18+.
- Fully typed with TypeScript (v5+).
- Supports both ES Modules (ESM) and CommonJS (CJS).
Contribute
Please feel free to contribute by creating a Pull Request including test code, or by suggesting other formulas.
License
This project is licensed under the GPL v3 License.
Support
For support, please open an issue in the GitHub repository.
Disclaimer
While the formulas are implemented with care, results may vary due to floating point errors, edge cases, or incorrect usage. Please verify results and report any issues or inaccuracies.