[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/metar 0f998f5 06/16: [metar] defcustom for metar-units
From: |
Stefan Monnier |
Subject: |
[elpa] externals/metar 0f998f5 06/16: [metar] defcustom for metar-units |
Date: |
Sat, 28 Nov 2020 23:16:36 -0500 (EST) |
branch: externals/metar
commit 0f998f5be19c628e2ac13bc23f960dca6d7aa926
Author: Mario Lang <mlang@delysid.org>
Commit: Mario Lang <mlang@delysid.org>
[metar] defcustom for metar-units
---
metar.el | 96 +++++++++++++++++++++++++++++++++++++++++++---------------------
1 file changed, 65 insertions(+), 31 deletions(-)
diff --git a/metar.el b/metar.el
index 60bc129..4909f00 100644
--- a/metar.el
+++ b/metar.el
@@ -43,9 +43,37 @@
(require 'solar)
(require 'url)
-(defvar metar-units '((length . m)
- (pressure . bar)
- (speed . kph)))
+(defcustom metar-units '((length . m)
+ (pressure . hPa)
+ (speed . kph)
+ (temperature . degC))
+ "Default measurement units to use when reporting weather information."
+ :type '(list (cons :format "%v"
+ (const :tag "Length: " length)
+ (choice (const :tag "Meter" m)
+ (const :tag "Inch" in)
+ (const :tag "Foot" ft)
+ (const :tag "Yard" yd)
+ (const :tag "Mile" mi)))
+ (cons :format "%v"
+ (const :tag "Pressure:" pressure)
+ (choice (const :tag "Pascal" Pa)
+ (const :tag "Hecto pascal" hPa)
+ (const :tag "Bar" bar)
+ (const :tag "Inch of mercury" inHg)
+ (const :tag "Standard atmosphere" atm)
+ (const :tag "Meter of mercury" mHg)
+ (const :tag "Punds per square inch" psi)))
+ (cons :format "%v"
+ (const :tag "Speed:" speed)
+ (choice (const :tag "Kilometers per hour" kph)
+ (const :tag "Miles per hour" mph)
+ (const :tag "Knot" knot)))
+ (cons :format "%v"
+ (const :tag "Temperature:" temperature)
+ (choice (const :tag "Degree Celsius" degC)
+ (const :tag "Degree Kelvin" degK)
+ (const :tag "Degree Fahrenheit" degF)))))
(defvar metar-stations-info-url "http://weather.noaa.gov/data/nsd_bbsss.txt"
"URL to use for retrieving station meta information.")
@@ -193,13 +221,34 @@ If no match if found, nil is returned."
(when station-code
(cons station-code (round best-distance))))))
-(defun metar-temp-to-number (string)
- "Convert a METAR temperature to a number."
- (if (= (aref string 0) ?M)
- (- (string-to-number (substring string 1)))
- (string-to-number string)))
+(defun metar-convert-unit (value new-unit)
+ "Convert VALUE to NEW-UNIT.
+VALUE is a string with the value followed by the unit, like \"5 knot\"
+and NEW-UNIT should be a unit name like \"kph\" or similar."
+ (cl-check-type value string)
+ (cl-check-type new-unit (or string symbol))
+ (cl-multiple-value-bind (value unit)
+ (split-string
+ (math-format-value
+ (math-convert-units (math-simplify (math-read-expr value))
+ (math-read-expr
+ (cl-etypecase new-unit
+ (string new-unit)
+ (symbol (symbol-name new-unit))))))
+ " ")
+ (cons (string-to-number value) (intern unit))))
-(defvar metar-url
"http://weather.noaa.gov/pub/data/observations/metar/stations/%s.TXT"
+(defun metar-convert-temperature (string)
+ "Convert a METAR temperature."
+ (metar-convert-unit
+ (concat (if (= (aref string 0) ?M)
+ (concat "-" (substring string 1))
+ string)
+ "degC")
+ (cdr (assq 'temperature metar-units))))
+
+(defvar metar-url
+ "http://weather.noaa.gov/pub/data/observations/metar/stations/%s.TXT"
"URL used to fetch station specific information.
%s is replaced with the 4 letter station code.")
@@ -354,18 +403,19 @@ If no record was found for STATION, nil is returned."
(defun metar-temperature (info)
(when (string-match metar-temperature-and-dewpoint-regexp info)
- (cons (metar-temp-to-number (match-string 1 info)) 'celsius)))
+ (metar-convert-temperature (match-string 1 info))))
(defun metar-dewpoint (info)
(when (string-match metar-temperature-and-dewpoint-regexp info)
- (cons (metar-temp-to-number (match-string 3 info)) 'celsius)))
+ (metar-convert-temperature (match-string 3 info))))
(defun metar-humidity (info)
(when (string-match metar-temperature-and-dewpoint-regexp info)
(cons (round
(metar-magnus-formula-humidity-from-dewpoint
- (metar-temp-to-number (match-string 1 info))
- (metar-temp-to-number (match-string 3 info)))) 'percent)))
+ (save-match-data (car (metar-convert-temperature (match-string 1
info))))
+ (car (metar-convert-temperature (match-string 3 info)))))
+ 'percent)))
(defconst metar-pressure-regexp
(rx symbol-start (group (char ?Q ?A)) (group (1+ digit)) symbol-end)
@@ -453,7 +503,7 @@ Otherwise, determine the best station via
latitude/longitude."
nil t))))
(let ((info (metar-decode (metar-get-record station))))
(if info
- (message "%d minutes ago at %s: %d�C, %d%% relative humidity%s"
+ (message "%d minutes ago at %s: %d°C, %d%% relative humidity%s"
(/ (truncate (float-time (time-since (cdr (assoc 'timestamp
info))))) 60)
(or (metar-stations-get (cdr (assoc 'station info)) 'name)
(cdr (assoc 'station info)))
@@ -507,28 +557,12 @@ Otherwise, determine the best station via
latitude/longitude."
(message "Average temperature in %s is %s"
country
(if (> count 0)
- (format "%.1f�C (%d stations)"
+ (format "%.1f°C (%d stations)"
(/ (float temp-sum) count)
count)
"unknown"))
(when (> count 0)
(/ (float temp-sum) count)))))
-(defun metar-convert-unit (value new-unit)
- "Convert VALUE to NEW-UNIT.
-VALUE is a string with the value followed by the unit, like \"5 knot\"
-and NEW-UNIT should be a unit name like \"kph\" or similar."
- (cl-check-type value string)
- (cl-check-type new-unit (or string symbol))
- (cl-multiple-value-bind (value unit)
- (split-string
- (math-format-value
- (math-convert-units (math-simplify (math-read-expr value))
- (math-read-expr (cl-etypecase new-unit
- (string new-unit)
- (symbol (symbol-name
new-unit))))))
- " ")
- (cons (string-to-number value) (intern unit))))
-
(provide 'metar)
;;; metar.el ends here
- [elpa] externals/metar 88ae024 04/16: Use calc-units to convert units to a user-defined setting (WIP), (continued)
- [elpa] externals/metar 88ae024 04/16: Use calc-units to convert units to a user-defined setting (WIP), Stefan Monnier, 2020/11/28
- [elpa] externals/metar ab9604e 07/16: [metar] Fix humidity calculation when Fahrenheit is used, Stefan Monnier, 2020/11/28
- [elpa] externals/metar 7307c42 01/16: Add metar.el., Stefan Monnier, 2020/11/28
- [elpa] externals/metar 258272f 13/16: * packages/metar/metar.el: Bump version for last two changes., Stefan Monnier, 2020/11/28
- [elpa] externals/metar e0b58d1 12/16: Add a work-around for bug#19582, present before Emacs 24.5., Stefan Monnier, 2020/11/28
- [elpa] externals/metar 5b8b9b7 14/16: Adjust for NOAA server changes., Stefan Monnier, 2020/11/28
- [elpa] externals/metar 3b05cb3 16/16: * .gitignore: New file, Stefan Monnier, 2020/11/28
- [elpa] externals/metar 8c6596d 03/16: * packages/metar/metar.el (metar-latitude-longitude-distance-haversine):, Stefan Monnier, 2020/11/28
- [elpa] externals/metar e1a8e1c 08/16: [metar] Fix metar-convert-temperature to actually DTRT, Stefan Monnier, 2020/11/28
- [elpa] externals/metar e891727 09/16: [metar] Unify station lat/lon conversion., Stefan Monnier, 2020/11/28
- [elpa] externals/metar 0f998f5 06/16: [metar] defcustom for metar-units,
Stefan Monnier <=
- [elpa] externals/metar 6820f4e 10/16: Release 0.1., Stefan Monnier, 2020/11/28
- [elpa] externals/metar a4c5807 11/16: Fix the case where the metar record contains M01 (bug#19401)., Stefan Monnier, 2020/11/28
- [elpa] externals/metar 623639e 15/16: * packages/metar/metar.el: Bump version., Stefan Monnier, 2020/11/28