Module weatherlib

Some weather related routines and solar calculations. Also provides unit conversion routines.

This module derives code from GWeather library which is part of the GNOME project. GWeather is available at http://ftp.gnome.org/pub/GNOME/sources/libgweather/ and it is licensed under the terms of the GNU General Public License Version 2.0.

This module derives code from the NOAA Solar Calculator. Original JavaScript code at the aforementioned site is placed under public domain.

Author:

Tuomas Jormola

Copyright© 2011 Tuomas Jormola tj@solitudo.net http://solitudo.net Licensed under the terms of the GNU General Public License Version 2.0.

Functions

calc_apparent_temperature (temperature, wind_speed, humidity) Calculate wind chill or heat index corrected temperature
calc_heat_index (temperature, humidity) Calculate heat index.
calc_humidity (temperature, dewpoint) Calculate relative humidity.
calc_moon (t) Calculate moon phase and latitude at given time
calc_sunrise (date, latitude, longitude, timezone) Calculate the time of sunrise at the given date and location.
calc_sunset (date, latitude, longitude, timezone) Calculate the time of sunset at the given date and location.
calc_timezone_offset (timezone) Calculate timezone offset from UTC in seconds for the given timezone.
calc_wind_chill (temperature, wind_speed) Calculate wind chill.
convert (conversion_type, from_unit, to_unit, value) Convert a value from a unit to another.
convert_angle (from_unit, to_unit, value) Convert angle from a unit to another.
convert_length (from_unit, to_unit, value) Convert length from a unit to another.
convert_pressure (from_unit, to_unit, value) Convert pressure from a unit to another.
convert_speed (from_unit, to_unit, value) Convert speed from a unit to another.
convert_temperature (from_unit, to_unit, value) Convert temperature from a unit to another.
convert_time (from_unit, to_unit, value) Convert time from a unit to another.
round (value, precisions, precision) Round numeric value to given precisions

Tables

ANGLE_UNITS Angle units table.
CONVERSION_TYPE Conversion type table.
LENGTH_UNITS Length units table.
PRESSURE_UNITS Pressure units table.
SPEED_UNITS Speed units table.
TEMPERATURE_UNITS Temperature units table.
TIME_UNITS Time units table.
conversion_table Conversion table


Functions

calc_apparent_temperature (temperature, wind_speed, humidity)
Calculate wind chill or heat index corrected temperature

Parameters

  • temperature: Temperature in Fahrenheit
  • wind_speed: Wind speed in miles per hour
  • humidity: Relative humidity as a percentage value between 0 and 100

Return value:

Apparent temperature given the weather conditions

See also:

calc_heat_index (temperature, humidity)
Calculate heat index. If it's hot and humidity is high, temperature feels higher than what it actually is. Heat index is the approximation of the human-perceived temperature in hot and moist conditions. Heat index formula from http://www.ukscience.org/_Media/MetEquations.pdf.

Parameters

  • temperature: Temperature in Fahrenheit, must be 80 or more
  • humidity: Relative humidity as a percentage value between 40 and 100

Return value:

Heat index of the given conditions, or nil if invalid input received
calc_humidity (temperature, dewpoint)
Calculate relative humidity. Formula from http://www.gorhamschaffler.com/humidity_formulas.htm.

Parameters

  • temperature: Temperature in Celcius
  • dewpoint: Dewpoint in Celcius

Return value:

Relative humidity as a percentage value between 0 and 100 or nil if invalid arguments
calc_moon (t)
Calculate moon phase and latitude at given time

Parameters

  • t: Seconds since epoch or os.date table, universal time

Return values:

  1. Moon phase in degrees between 0 and 360
  2. Moon latitude
calc_sunrise (date, latitude, longitude, timezone)
Calculate the time of sunrise at the given date and location. Routines from the JavaScript code at http://www.esrl.noaa.gov/gmd/grad/solcalc/.

Parameters

  • date: an os.date table representing the date for which to calculate the time of sunrise
  • latitude: Latitude of the location
  • longitude: Longitude of the location
  • timezone: Timezone as an offset in hours from UTC at the given location

Return value:

os.date table that holds the date and time when sunrise occurs
calc_sunset (date, latitude, longitude, timezone)
Calculate the time of sunset at the given date and location. Routines from the JavaScript code at http://www.esrl.noaa.gov/gmd/grad/solcalc/.

Parameters

  • date: an os.date table representing the date for which to calculate the time of sunset
  • latitude: Latitude of the location
  • longitude: Longitude of the location
  • timezone: Timezone as an offset in hours from UTC at the given location

Return value:

os.date table that holds the date and time when sunset occurs
calc_timezone_offset (timezone)
Calculate timezone offset from UTC in seconds for the given timezone. Uses external command date(1). Takes Daylight Saving Time into account if the local date command supports that. Tested only on Ubuntu Linux 10.04 and Solaris 10 operating systems. May not be very portable.

Parameters

  • timezone: Timezone name, e.g. Europe/Helsinki, Asia/Bangkok, US/Pacific

Usage

  • weatherlib.calc_timezone_offset('Europe/Helsinki') -- 7200
  • weatherlib.calc_timezone_offset('Asia/Bangkok') -- 28800
  • weatherlib.calc_timezone_offset('US/Pacific') -- -28800

Return value:

Timezone offset in seconds from UTC. Positive for timezones ahead of UTC, negative for timezones behind UTC.
calc_wind_chill (temperature, wind_speed)
Calculate wind chill. If temperature is low but it's windy, the temperature feels lower than the actual measured temperature. Wind chill formula from http://www.nws.noaa.gov/om/windchill/.

Parameters

  • temperature: Temperature in Fahrenheit, must be 50 or less
  • wind_speed: Wind speed in miles per hour

Return value:

Wind chill of the given conditions, or nil if invalid input received
convert (conversion_type, from_unit, to_unit, value)
Convert a value from a unit to another. This is just a shortcut to conversion_table[conversion_type][from_unit][to_unit](value).

Parameters

  • conversion_type: CONVERSION_TYPE value indicating the type of the conversion
  • from_unit: Value from the *_UNITS matching the conversion type indicating the unit in which the current value is
  • to_unit: Value from the *_UNITS matching the conversion type indicating the unit to which the current value is to be converted
  • value: Value to convert

Return values:

  1. Converted value if no errors, nil if an error occurred
  2. Error string if an error occurred
convert_angle (from_unit, to_unit, value)
Convert angle from a unit to another. This is just a shortcut to convert(CONVERSION_TYPE.ANGLE, from_unit, to_unit, value). Convert angle from a unit to another

Parameters

  • from_unit: ANGLE_UNITS value indicating the unit in which the current value is
  • to_unit: ANGLE_UNITS value indicating the unit to which the current value is to be converted
  • value:

Return values:

  1. Converted value if no errors, nil if an error occurred
  2. Error string if an error occurred

See also:

convert_length (from_unit, to_unit, value)
Convert length from a unit to another. This is just a shortcut to convert(CONVERSION_TYPE.LENGTH, from_unit, to_unit, value). Convert length from a unit to another

Parameters

  • from_unit: LENGTH_UNITS value indicating the unit in which the current value is
  • to_unit: LENGTH_UNITS value indicating the unit to which the current value is to be converted
  • value:

Return values:

  1. Converted value if no errors, nil if an error occurred
  2. Error string if an error occurred

See also:

convert_pressure (from_unit, to_unit, value)
Convert pressure from a unit to another. This is just a shortcut to convert(CONVERSION_TYPE.PRESSURE, from_unit, to_unit, value). Convert pressure from a unit to another

Parameters

  • from_unit: PRESSURE_UNITS value indicating the unit in which the current value is
  • to_unit: PRESSURE_UNITS value indicating the unit to which the current value is to be converted
  • value:

Return values:

  1. Converted value if no errors, nil if an error occurred
  2. Error string if an error occurred

See also:

convert_speed (from_unit, to_unit, value)
Convert speed from a unit to another. This is just a shortcut to convert(CONVERSION_TYPE.SPEED, from_unit, to_unit, value).

Parameters

  • from_unit: SPEED_UNITS value indicating the unit in which the current value is
  • to_unit: SPEED_UNITS value indicating the unit to which the current value is to be converted
  • value:

Return values:

  1. Converted value if no errors, nil if an error occurred
  2. Error string if an error occurred

See also:

convert_temperature (from_unit, to_unit, value)
Convert temperature from a unit to another. This is just a shortcut to convert(CONVERSION_TYPE.TEMPERATURE, from_unit, to_unit, value). Convert temperature from a unit to another

Parameters

  • from_unit: TEMPERATURE_UNITS value indicating the unit in which the current value is
  • to_unit: TEMPERATURE_UNITS value indicating the unit to which the current value is to be converted
  • value:

Return values:

  1. Converted value if no errors, nil if an error occurred
  2. Error string if an error occurred

See also:

convert_time (from_unit, to_unit, value)
Convert time from a unit to another. This is just a shortcut to convert(CONVERSION_TYPE.TIME, from_unit, to_unit, value). Convert time from a unit to another

Parameters

  • from_unit: TIME_UNITS value indicating the unit in which the current value is
  • to_unit: TIME_UNITS value indicating the unit to which the current value is to be converted
  • value:

Return values:

  1. Converted value if no errors, nil if an error occurred
  2. Error string if an error occurred

See also:

round (value, precisions, precision)
Round numeric value to given precisions

Parameters

  • value: Value to round
  • precisions:
  • precision: How many decimals in the rounded value, 0 by default

Usage

  • mathlib.round(3.673242) -- 4
  • mathlib.round(3.673242, 3) -- 3.673

Return value:

Rounded value

Tables

ANGLE_UNITS
Angle units table. Values from this table can be used as indices to the conversion types in the conversion table. Fields
  • DEG: Index value for degrees
  • RAD: Index value for radians
  • HOUR: Index value for hour
CONVERSION_TYPE
Conversion type table. Values from this table can be used as indices to the conversion table. Fields
  • TEMPERATURE: Index value temperature conversions
  • SPEED: Index value for speed conversions
  • PRESSURE: Index value for pressure conversions
  • LENGTH: Index value for length conversions
  • TIME: Index value for time conversions
  • ANGLE: Index value for angle conversions
LENGTH_UNITS
Length units table. Values from this table can be used as indices to the conversion types in the conversion table. Fields
  • METER: Index value for meter
  • KILOMETER: Index value for kilometer
  • FOOT: Index value for foot
  • YARD: Index value for yard
  • MILE: Index value for mile
PRESSURE_UNITS
Pressure units table. Values from this table can be used as indices to the conversion types in the conversion table. Fields
  • HPA: Index value for hectopascal
  • ATM: Index value for standard atmosphere
  • INHG: Index value for inches of Mercury
SPEED_UNITS
Speed units table. Values from this table can be used as indices to the conversion types in the conversion table. Fields
  • KNOT: Index value for knot
  • MS: Index value for meters per second
  • KMH: Index value for kilometers per hour
  • MPH: Index value for miles per hour
TEMPERATURE_UNITS
Temperature units table. Values from this table can be used as indices to the conversion types in the conversion table. Fields
  • CELCIUS: Index value for Celcius
  • FAHRENHEIT: Index value for Fahrenheit
TIME_UNITS
Time units table. Values from this table can be used as indices to the conversion types in the conversion table. Fields
  • S: Index value for second
  • M: Index value for minute
  • H: Index value for hour
conversion_table
Conversion table Fields
  • temperature: Temperature conversion routines
  • speed: Speed conversion routines
  • pressure: Pressure conversion routines
  • length: Length conversion routines
  • time: Time conversion routines
  • angle: Angle conversion routines

Valid XHTML 1.0!