From 718876add09965faf97669c7a241be8aaa6e960d Mon Sep 17 00:00:00 2001 From: roe-dl Date: Tue, 4 Mar 2025 15:29:02 +0100 Subject: [PATCH] almanac related units, almanac locale --- src/weewx/almanac.py | 10 ++++++---- src/weewx/cheetahgenerator.py | 4 ++-- src/weewx/defaults.py | 2 ++ src/weewx/units.py | 22 +++++++++++++++++++--- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/weewx/almanac.py b/src/weewx/almanac.py index ab39aa1c..2df5db4f 100644 --- a/src/weewx/almanac.py +++ b/src/weewx/almanac.py @@ -79,7 +79,7 @@ class Almanac: >>> almanac = Almanac(t, 46.0, -122.0, formatter=weewx.units.get_default_formatter()) Test backwards compatibility with attribute 'moon_fullness': - >>> print("Fullness of the moon (rounded) is %.2f%% [%s]" % (almanac._moon_fullness, almanac.moon_phase)) + >>> print("Fullness of the moon (rounded) is %.2f%% [%s]" % (almanac.moon_fullness,almanac.moon_phase)) Fullness of the moon (rounded) is 3.00% [new (totally dark)] Now get a more precise result for fullness of the moon: @@ -207,7 +207,7 @@ class Almanac: temperature=None, pressure=None, horizon=None, - moon_phases=weeutil.Moon.moon_phases, + texts=dict(), formatter=None, converter=None): """Initialize an instance of Almanac @@ -225,7 +225,8 @@ class Almanac: pressure (float|None): Observer's atmospheric pressure in **mBars**. [Optional. Default is 1010] horizon (float|None): Angle of the horizon in degrees [Optional. Default is zero] - moon_phases (list): An array of 8 strings with descriptions of the moon + texts (dict): a dictionary of language dependent texts, basicly an entry + 'moon_phases' which holds an array of 8 strings with descriptions of the moon phase. [optional. If not given, then weeutil.Moon.moon_phases will be used] formatter (weewx.units.Formatter|None): An instance of weewx.units.Formatter with the formatting information to be used. @@ -239,7 +240,8 @@ class Almanac: self.temperature = temperature if temperature is not None else 15.0 self.pressure = pressure if pressure is not None else 1010.0 self.horizon = horizon if horizon is not None else 0.0 - self.moon_phases = moon_phases + self.texts = texts + self.moon_phases = texts.get('moon_phases',weeutil.Moon.moon_phases) self.formatter = formatter or weewx.units.Formatter() self.converter = converter or weewx.units.Converter() # Check to see whether there is a module that provides more than diff --git a/src/weewx/cheetahgenerator.py b/src/weewx/cheetahgenerator.py index fe9afe2d..e85033a3 100644 --- a/src/weewx/cheetahgenerator.py +++ b/src/weewx/cheetahgenerator.py @@ -542,7 +542,7 @@ class Almanac(SearchList): if 'barometer' in rec: pressure_mbar = weewx.units.convert(weewx.units.as_value_tuple(rec, 'barometer'), "mbar")[0] - self.moonphases = generator.skin_dict.get('Almanac', {}).get('moon_phases', weeutil.Moon.moon_phases) + self.texts = generator.skin_dict.get('Almanac', {}) altitude_vt = weewx.units.convert(generator.stn_info.altitude_vt, "meter") @@ -552,7 +552,7 @@ class Almanac(SearchList): altitude=altitude_vt[0], temperature=temperature_C, pressure=pressure_mbar, - moon_phases=self.moonphases, + texts=self.texts, formatter=generator.formatter, converter=generator.converter) diff --git a/src/weewx/defaults.py b/src/weewx/defaults.py index a5d7a287..1b516633 100644 --- a/src/weewx/defaults.py +++ b/src/weewx/defaults.py @@ -76,6 +76,7 @@ log_failure = False [[StringFormats]] amp = %.1f + astronomical_unit = %.2f bit = %.0f boolean = %d byte = %.0f @@ -147,6 +148,7 @@ log_failure = False [[Labels]] amp = " A" + astronomical_unit = " AU" bit = " b" boolean = "" byte = " B" diff --git a/src/weewx/units.py b/src/weewx/units.py index 26461bf9..a245b71a 100644 --- a/src/weewx/units.py +++ b/src/weewx/units.py @@ -268,6 +268,7 @@ USUnits = ListOfDicts({ "group_illuminance" : "lux", "group_interval" : "minute", "group_length" : "inch", + "group_localtime" : "local_djd", "group_moisture" : "centibar", "group_percent" : "percent", "group_power" : "watt", @@ -308,6 +309,7 @@ MetricUnits = ListOfDicts({ "group_illuminance" : "lux", "group_interval" : "minute", "group_length" : "cm", + "group_localtime" : "local_djd", "group_moisture" : "centibar", "group_percent" : "percent", "group_power" : "watt", @@ -344,6 +346,9 @@ std_groups = { # Conversion functions to go from one unit type to another. conversionDict = { + 'astronomical_unit': {'meter' : lambda x : x * 149597870700, + 'km' : lambda x : x * 149597870.7, + 'mile' : lambda x : x * 92955807.23752087}, 'bit' : {'byte' : lambda x : x / 8}, 'byte' : {'bit' : lambda x : x * 8}, 'cm' : {'inch' : lambda x : x / CM_PER_INCH, @@ -404,7 +409,8 @@ conversionDict = { 'watt_second' : lambda x : x * 3.6e6, 'watt_hour' : lambda x : x * 1000.0}, 'km' : {'meter' : lambda x : x * 1000.0, - 'mile' : lambda x : x * 0.621371192}, + 'mile' : lambda x : x * 0.621371192, + 'astronomical_unit': lambda x : x / 149597870.7 }, 'km_per_hour' : {'mile_per_hour' : kph_to_mph, 'knot' : kph_to_knot, 'meter_per_second' : lambda x : x * 0.277777778}, @@ -436,14 +442,16 @@ conversionDict = { 'watt_hour' : lambda x : x * 1000000 / 3600, 'watt_second' : lambda x : x * 1000000}, 'meter' : {'foot' : lambda x : x / METER_PER_FOOT, - 'km' : lambda x : x / 1000.0}, + 'km' : lambda x : x / 1000.0, + 'astronomical_unit': lambda x : x / 149597870700}, 'meter_per_second' : {'mile_per_hour' : mps_to_mph, 'knot' : mps_to_knot, 'km_per_hour' : lambda x : x * 3.6}, 'meter_per_second2': {'mile_per_hour2' : lambda x : x * 2.23693629, 'knot2' : lambda x : x * 1.94384449, 'km_per_hour2' : lambda x : x * 3.6}, - 'mile' : {'km' : lambda x : x * 1.609344}, + 'mile' : {'km' : lambda x : x * 1.609344, + 'astronomical_unit': lambda x : x / 92955807.23752087}, 'mile_per_hour' : {'km_per_hour' : lambda x : x * 1.609344, 'knot' : mph_to_knot, 'meter_per_second' : lambda x : x * 0.44704}, @@ -771,6 +779,14 @@ class Formatter: else: val_str = time.strftime(useThisFormat, time.localtime(t)) addLabel = False + elif val_t[1]=='local_djd': + ti = time.gmtime((val_t[0]-25567.5)*SECS_PER_DAY) + if useThisFormat is None: + val_str = time.strftime(self.time_format_dict.get(context, "%d-%b-%Y %H:%M"), + ti) + else: + val_str = time.strftime(useThisFormat, ti) + addLabel = False else: # It's not a time. It's a regular value. Get a suitable format string: if useThisFormat is None: