[StdCalibrate][[Corrections]] check if obs_type is present

148 views
Skip to first unread message

michael.k...@gmx.at

unread,
Mar 8, 2024, 5:13:56 PM3/8/24
to weewx-user
I think I remember that this has been asked before, how to check with an expression in Corrections, if an obs_type is present?

Tom Keffer

unread,
Mar 8, 2024, 6:08:47 PM3/8/24
to weewx...@googlegroups.com
You can't check, but if the observation type is not present, the resultant NameError exception is ignored.

On Fri, Mar 8, 2024 at 2:13 PM 'michael.k...@gmx.at' via weewx-user <weewx...@googlegroups.com> wrote:
I think I remember that this has been asked before, how to check with an expression in Corrections, if an obs_type is present?

--
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 weewx-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/weewx-user/ae1bbcf8-23b1-475e-8749-62286df24fb0n%40googlegroups.com.

Graham Eddy

unread,
Mar 8, 2024, 6:31:49 PM3/8/24
to WeeWX User
i think ' if obs_type in globals() then expr else None ' would work
⊣GE⊢

On 9 Mar 2024, at 9:13 am, 'michael.k...@gmx.at' via weewx-user <weewx...@googlegroups.com> wrote:

I think I remember that this has been asked before, how to check with an expression in Corrections, if an obs_type is present?

Graham Eddy

unread,
Mar 8, 2024, 6:33:53 PM3/8/24
to WeeWX User
oops, python syntax would be: expr if ops_tye in globals() else None
⊣GE⊢

Tom Keffer

unread,
Mar 8, 2024, 9:22:24 PM3/8/24
to weewx...@googlegroups.com
The facility uses the Python function eval. A dictionary with key 'math' and value the math module is passed in for globals. The record is passed in for locals. So, you could use the expression

outTemp = 44 if outTemp in locals() else None



michael.k...@gmx.at

unread,
Mar 9, 2024, 2:15:24 AM3/9/24
to weewx-user
Actually

outTemp = 44 if outTemp in locals() else None

yields None when outTemp  is there. 

Since we are looking for the key 'outTemp' and not it's value, our expression needs to be:

outTemp = 44 if 'outTemp' in locals() else None


michael.k...@gmx.at

unread,
Mar 9, 2024, 2:37:01 AM3/9/24
to weewx-user
Anyway, thank you for helping me with this one!

For the record:

I have two pieces of hardware, one is using a classic anemometer with cups, the other one is an ultrasonic device. With very low wind speeds, the classic one shows zero wind, while the ultrasonic one shows low, but very plausible values. The ultrasonic one also doesn't freeze or gets blocked by snow, because is has a heating. On the other hand, the ultrasonic device often misses wind gusts and lacks accuracy in wet conditions.
In short, the closes approach to represent reality is: always use the values from the sensor, which is showing the higher reading. 
Also, the Wind direction of the ultrasonic device has a better resolution and no vane that sometimes doesn't move, even in wind speeds, the classic anemometer is rotating.
So the second thing is: always use windDir from the ultrasonic device. 

Both readings arrive in the same loop packet, their names are "windSpeed" and "ws90_windSpeed" (the latter ultrasonic), together with their gust speed and direction, except for situations, the sensor's data cannot be received by the console. So my first approach

[StdCalibrate]
    [[Corrections]]
        windSpeed = ws90_windSpeed if ws90 if ws90_windSpeed > windSpeed else windSpeed
        windGust = ws90_windGust if ws90_windGust > windGust else windGust
        windDir = ws90_windDir if ws90_windDir is not None else windDir

is working, but only in cases, both of the readings are present. 

Yesterday, windSpeed (from the classic device) was missing, where ws90_windSpeed was there. The result was, no windSpeed was recorded as at all, due to a python NameError, which was silently ignored. 

try:
event.record[obs_type] = eval(self.corrections[obs_type], {'math': math},
                                                  event.record)
    except (TypeError, NameError):
    pass

So, if this should work as intended, we need to check if windSpeed is there, or not.

What I want is to
  • store whatever wind speed/gust is the higher, if both are present
  • store whatever wind speed/gust, when only one is present
  • store the ultrasonic wind direction if present, or else the other one
For now, I use these expressions, and the loop packets look good, and there shouldn't be a need to check for both being present:

[StdCalibrate]
   
    [[Corrections]]
        # For each type, an arbitrary calibration expression can be given.
        # It should be in the units defined in the StdConvert section.
        # Example:
        #foo = foo + 0.2
        windSpeed = ws90_windSpeed if 'windSpeed' not in locals() or ws90_windSpeed > windSpeed else windSpeed
        windGust = ws90_windGust if 'windGust' not in locals() or ws90_windGust > windGust else windGust
        windDir = ws90_windDir if 'ws90_windDir' in locals() and ws90_windDir is not None else windDir

I think this way I think I get my desired results, but after a long week my brain is a bit overloaded. So if anybody with cognitive capacities left could check if my expressions really make that much sense: very much appreciated :) 

Message has been deleted

michael.k...@gmx.at

unread,
Mar 10, 2024, 6:51:08 AM3/10/24
to weewx-user
This one seems way better:
[StdCalibrate]
   
    [[Corrections]]
        windSpeed = ws90_windSpeed if 'windSpeed' not in locals() else windSpeed if 'ws90_windSpeed' not in locals() else ws90_windSpeed if ws90_windSpeed > windSpeed else windSpeed
        windGust = ws90_windGust if 'windGust' not in locals() else windGust if 'ws90_windGust' not in locals() else ws90_windGust if ws90_windGust > windGust else windGust
        windDir = ws90_windDir if 'ws90_windDir' in locals() and ws90_windDir is not None else windDir

Tom Keffer

unread,
Mar 10, 2024, 11:24:35 AM3/10/24
to weewx...@googlegroups.com
Honestly, with an expression that complicated, I would write a simple WeeWX service.

michael.k...@gmx.at

unread,
Mar 10, 2024, 2:14:02 PM3/10/24
to weewx-user
And honestly: I'm really glad you consider the expressions complicated. For me, they were. I needed to write a test to figure them out :D
Reply all
Reply to author
Forward
0 new messages