Rather than simply give you the answer I will step through all of the
considerations when adding a new unit to WeeWX, hopefully this will wrap it
all up in one place rather than in several piecemeal posts. Almost all of
the following is covered to some extent in the Customizing the database
<http://weewx.com/docs/customizing.htm#archive_database> and Customizing
units and unit groups
<http://weewx.com/docs/customizing.htm#customizing_units> sections of the
Customization Guide.
There are a number of key points to consider when adding a new unit to
WeeWX, depending on the unit some of these points may require extension of
some aspect of WeeWX or no action may be required. The points to be
considered are:
1. unit groups
2. unit conversion functions
3. default labels and formats
Unit groups. Does your new unit fit into an existing WeeWX unit group
(refer bin/weewx/units.py class USUnits, MetricUnits and MetricWXUnits). If
your unit does fit into an existing unit group (eg Kelvin would fit in
group_temperature) you can skip this step. If your unit does not fit within
an existing unit group then you will need to define a new unit group. This
can be easily done by adding new entries to each of class USUnits,
MetricUnits and MetricWXUnits. For example, the new unit group,
group_resistance, would be added as follows:
import weewx.units
weewx.units.MetricUnits['group_resistance'] = 'ohm'
weewx.units.MetricWXUnits['group_resistance'] = 'ohm'
weewx.units.USUnits['group_resistance'] = 'ohm'
Conversion functions. Having defined the unit group group_resistance, we
can now consider any unit conversion functions. We might want to handle
resistance in ohms, kilohms and megohms. In effect we are treating ohms,
kilohms and megohms as individual units even though they are all really
just scaled version of the same unit ohms. Consider the differences to
temperature where we have C, F and Kelvin, it's the same principle but in
the case of ohms (and in your case the amps/milliamps) the conversion
functions are simple scalings rather than something more complex. So we
need to define conversion functions for converting between our three
resistance units (if we were adding to an existing unit group we would need
to define conversion functions between all units (new and existing) in the
unit group). In this case we will use ohm, kohm and Mohm as the 'units', so
we add to the weewx.units.conversionDict:
weewx.units.conversionDict['ohm'] = {'kohm': lambda x : x / 1000.0,
'Mohm': lambda x : x / 1000000.0}
weewx.units.conversionDict['kohm'] = {'ohm': lambda x : x * 1000.0,
'Mohm': lambda x : x / 1000.0}
weewx.units.conversionDict['Mohm'] = {'ohm': lambda x : x * 1000000.0,
'kohm': lambda x : x * 1000.0}
WeeWX now knows how to convert between the different members of
group_resistance.
Default labels and formats. WeeWX reports may use default labels and number
formats for obs of particular units. So you need to define default number
formats and unit labels for any new units, in this case we need unit labels
and number formats for ohm, kohm and Mohm. To do this we augment
weewx.units.default_unit_label_dict and weewx.units.default_unit_format_dict
respectively. Something like:
weewx.units.default_unit_format_dict['ohm'] = '%.1f'
weewx.units.default_unit_label_dict['ohm'] = ' ohm'
weewx.units.default_unit_format_dict['kohm'] = '%.1f'
weewx.units.default_unit_label_dict['kohm'] = ' ohm'
weewx.units.default_unit_format_dict['Mohm'] = '%.3f'
weewx.units.default_unit_label_dict['Mohm'] = ' Mohm'
That's it. WeeWX now knows about obs that measure resistance in ohms; it
knows how to convert between the three 'units' as well as being able to
properly format tags using resistance obs in a report.
None of the above actually results in anything being stored in the
database. Once you decide you want to add a field to your archive that
records resistance you need to do two things; you need to modify your
database schema by adding a new database field and you need to assign a
suitable WeeWX unit group to the new database field. In the case of
resistance if we want to add a new field isoResistance to the archive we
would modify the schema to include the new field (refer to Adding a new
type to the database
<http://weewx.com/docs/customizing.htm#add_archive_type>), so something
like:
import schemas.wview
schema_with_resistance = schemas.wview.schema + [('isoResistance', 'REAL')]
Of course there also needs to be a change to weewx.conf [DataBindings] to
ensure the relevant binding uses the new schema and if modifying an
existing database the database needs to be updated to include the new field.
We now need to assign a WeeWX unit group to isoResistance. This is done my
augmenting weewx.units.obs_group_dict, something like:
weewx.units.obs_group_dict['isoResistance'] = 'group_resistance'
Provided your archive records contain a field isoResistance WeeWX will now
store isoResistance data in the database, you can use isoResistance in
report tags just as you can any other database field and you can convert
between/display resistance values in ohms, kohms or Mohms.
All of the above code can be placed in bin/user/extensions.py, or if, as
with the Electricity example in the Customization Guide, you have a
separate file containing a custom WeeWX service you can place the code in
that file (this may be useful for keeping all of the related code in the
one place).
Hopefully this helps.
Gary
On Friday, 26 April 2019 04:27:12 UTC+10, wysiwyg wrote:
>
> I assume I can define the label and ratio this way:
>
> weewx.units.default_unit_label_dict['millamp'] = ' mA'
> weewx.units.conversionDict['amp'] = {'milliamp': lambda x : x * 1000}
> weewx.units.conversionDict['milliamp'] = {'amp': lambda x : x * 0.001}
>
>
>
> But I dont figure out how to "declare" milliamp 1st ?
>
>
--
You received this message because you are subscribed to the Google Groups
"weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.