Hi,

If I understand you correctly, you wish to backfill your weeWX archive 
table with calculated maxSolarRad, cloudbase and windrun values? If this is 
the case that is a novel piece of code reuse. First up, it appears that you 
have taken the code from weewxwd_config, which only exists in the weewx-WD 
1.2.0_development branch. That code was last touched in late 2015 and was 
never put into use, so it may have some issue. That being said, you appear 
to be only using a small piece of the code that looks like it should have 
no issues.

The code you are using was originally developed to update existing weeWX 
archive fields extraTemp1 and extraTemp2. These fields are part of the 
default archive table schema used by weeWX and the key point here is that 
the code updates data in existing fields. Fields maxSolarRad, cloudbase and 
windrun are not part of the default weeWX archive table schema. Before you 
can set/store any values in these fields you need to add them to you 
archive table schema. Have you done that? The process is described in the 
Customization Guide, in particular the Add a new type to the archive 
database <http://weewx.com/docs/customizing.htm#add_archive_type> section.

Also, a few comments on your code.

if _rec['outTemp'] <> 0 and _rec['outHumidity'] <> 0:
>        cb1 = weewx.wxformulas.cloudbase_Metric(_rec['outTemp'], 
> _rec['outHumidity'], 53)
> else:
>         cb1 = 0
> dbmanager_wx.updateValue(_rec['dateTime'], 'cloudbase', cb1)
>

The cloudbase_Metric() function requires temperature in Celsius, what unit 
system does your database use METRIC, METRICWX? If so your outTemp field 
will be in Celsius but if you are using the US unit system then it will be 
in Fahrenheit. Your code may be returning the correct cloudbase if your 
database is using METRIC or METRICWX but it will not be if your database is 
using US units. Your code could be more robust by using the weeWX unit 
conversion capabilities to always get the temperature in Celsius 
irrespective of the database units in use. I think the check that outTemp 
is not 0 and outHumidity is not 0 can be removed (for example, your code 
will never calculate a cloudbase value when outTemp=0). The 
cloudbase_Metric() function has the necessary protections to handle all 
possible outTemp and outHumidity values. The resulting code could look 
something like this (not tested):

# get the outTemp units and group used in the record
(t, g) = weewx.units.getStandardUnitType(_rec['usUnits'], 'outTemp')
# get _rec outTemp as a ValueTuple 
temp_vt = weewx.units.ValueTuple(_rec['outTemp'], t, g)
# now get temperature in Celsius
temp_c = weewx.units.convert(temp_vt, 'degree_C').value
# calculate cloudbase, the result will be in meters
cb_m = weewx.wxformulas.cloudbase_Metric(temp_c, _rec['outHumidity'], 53)
# get the calculated cloudbase as a ValueTuple, it will now be an altitude 
in m
cb_m_vt = weewx.units.ValueTuple(cb_m, 'meter', 'group_altitude')
# convert the calculated cloudbase to the altitude units used in _rec
cb1 = weewx.units.convertStd(cb_m_vt, _rec['usUnits']).value
# update the archive cloudbase value
dbmanager_wx.updateValue(_rec['dateTime'], 'cloudbase', cb1)

if _rec['windSpeed'] > 0:
>          vt = (_rec['windSpeed'], "mile_per_hour", "group_speed")
>          ws_kts = weewx.units.convert(vt, "knot")[0]
>          be2 = ws_kts * 5.0 / 60.0
> else:
>          be2 = 0.0
> dbmanager_wx.updateValue(_rec['dateTime'], 'windrun', be2)
>

Again I think you may be be taking a risk in terms of units. You imply that 
windSpeed is in miles per hour (that does not align with your cloudbase 
code where you assumed outTemp was in Celsius - Celsius and miles per hour 
do not co-exist in any of the three weeWX unit systems), then covert to 
knots to calculate windrun which is saved back to your archive where you 
have already assumed your speeds are in miles per hour (which implies you 
distance units would be miles). Also you don't need to worry about the 
check for windSpeed > 0, the maths will take care of that, far better to 
check for windSpeed is not None as that condition will certainly cause 
issues for your calculations. You might want to try something like (again 
not tested):

if _rec['windSpeed'] is not None:
    # get the windSpeed units and group used in _rec
    (t, g) = weewx.units.getStandardUnitType(_rec['usUnits'], 'windSpeed')
    # get the _rec windSpeed as a ValueTuple 
    ws_vt = weewx.units.ValueTuple(_rec['windSpeed'], t, g)
    # now convert windSpeed to a known unit, say km per hour
    ws_kmh = weewx.units.convert(ws_vt, 'km_per_hour').value
    # calculate windrun, it will be a result in km
    be2_km = ws_kmh * 5.0/60
    # get the calculated windrun as a ValueTuple, it will now be a distance 
in km
    be2_km_vt = weewx.units.ValueTuple(be2_km, 'km', 'group_distance')
    # convert the calculated windrun to the distance units used in _rec
    be2 = weewx.units.convertStd(be2_km_vt, _rec['usUnits']).value
else:
    # if windSpeed is None then so is windrun, no need for any unit 
conversions
    be2 = None
# update the archive windrun value
dbmanager_wx.updateValue(_rec['dateTime'], 'windrun', be2)

The above code will require you to import weewx.units if you are not 
already doing so ie:

import weewx.units

One of the keys things to remember when manipulating your data is to be 
cognisant of the units of the source data you are manipulating as well as 
the units of your result - this is particularly so if pulling data from the 
archive. You may happen to get some right by chance (for example cloudbase 
if you are using METRIC or METRICWX) but you will eventually get some wrong 
too.

Gary

Reply via email to