caproto icon indicating copy to clipboard operation
caproto copied to clipboard

Documentation: precision, units, doc and other attributes threading client PV

Open vstadnytskyi opened this issue 5 years ago • 3 comments

Hi All,

I would like to be able to get other information besides data from a PV. For example, if we look at the example here https://nsls-ii.github.io/caproto/_modules/caproto/ioc_examples/records.html#RecordMockingIOC

    # Define three records, an analog input (ai) record:
    A = pvproperty(value=1.0, record='ai')
    # And an analog output (ao) record:
    B = pvproperty(value=2.0, record='ao',
                   precision=3)
    # and an ai with all the bells and whistles
    C = pvproperty(value=0.0,
                   record='ai',
                   upper_alarm_limit=2.0,
                   lower_alarm_limit=-2.0,
                   upper_warning_limit=1.0,
                   lower_warning_limit=-1.0,
                   upper_ctrl_limit=3.0,
                   lower_ctrl_limit=-3.0,
                   units="mm",
                   precision=3,
                   doc='The C pvproperty')

Three PVProperties : A,B,C have much of other attributes

  • record
  • units
  • precision
  • doc
  • etc...

How to get precision, units, doc and other attributes from PVpropery with let's say threading client (https://nsls-ii.github.io/caproto/threading-client.html)

I have tried:

In [70]: pvs[0]                                                                                                                                                                                             │
Out[70]: <PV name='MacProBoxSL:MATH0' priority=0 address=('127.0.0.1', 64322), circuit_state=States.CONNECTED, channel_state=States.CONNECTED>                                                              │
                                                                                                                                                                                                            │
In [71]: result = pvs[0].read()                                                                                                                                                                             │
                                                                                                                                                                                                            │
In [72]: result                                                                                                                                                                                             │
Out[72]: ReadNotifyResponse(data=array([22.93492656]), data_type=<ChannelType.DOUBLE: 6>, data_count=1, status=CAStatusCode(name='ECA_NORMAL', code=0, code_with_severity=1, severity=<CASeverity.SUCCESS: 1│
>, success=1, defunct=False, description='Normal successful completion'), ioid=13, metadata=None)  

In [73]: result.data                                                                                                                                                                                        │
Out[73]: array([22.93492656])                                                                                                                                                                               │
                                                                                                                                                                                                            │
In [74]: result.metadata                                                                                                                                                                                    │
                                                                                                                                                                                                            │
In [75]: result.metadata.timestamp                                                                                                                                                                          │
---------------------------------------------------------------------------                                                                                                                                 │
AttributeError                            Traceback (most recent call last)                                                                                                                                 │
<ipython-input-75-a04531170bd0> in <module>                                                                                                                                                                 │
----> 1 result.metadata.timestamp                                                                                                                                                                           │
                                                                                                                                                                                                            │
AttributeError: 'NoneType' object has no attribute 'timestamp' 

I would expect metadata to have those attributes I am seeking. But for some reasons it is empty. I do know that the PV is getting updated because I can see new values in '.data'

                                                                                                                                                                                                        │
In [76]: result = pvs[0].read()                                                                                                                                                                             │
                                                                                                                                                                                                            │
In [77]: result.data                                                                                                                                                                                        │
Out[77]: array([22.97497969])                                                                                                                                                                               │
                                                                                                                                                                                                                                                                                                                                                                                                                  │
In [79]: result = pvs[0].read()                                                                                                                                                                             │
                                                                                                                                                                                                            │
In [80]: result.data                                                                                                                                                                                        │
Out[80]: array([22.98527906])  

vstadnytskyi avatar Apr 29 '21 13:04 vstadnytskyi

I hope the following answers your questions in verbose code. If you have to ask "why is it so complicated?!", well, the finger-pointing should be at Channel Access itself rather than caproto (in my opinion).

import caproto
import caproto.threading.client

ctx = caproto.threading.client.Context()

pvname = "mock:C"
pv, pv_desc, pv_rtyp = ctx.get_pvs(pvname, f"{pvname}.DESC", f"{pvname}.RTYP")

print(f"""
PV={pvname}

The .DESC field matches up with `doc=`:
    .DESC={pv_desc.read().data[0]}

The RTYP field holds the record type:
    .RTYP={pv_rtyp.read().data[0]}
""")

cls_data = pv.read(data_type=caproto.ChannelType.CLASS_NAME)
reported_record_type = cls_data.metadata.value

print(f"""
A little-known CA feature is that we can read the record type through
a special ChannelType 'CLASS_NAME':
    record_type={reported_record_type}
""")

control_data = pv.read(data_type="control")
print(f"""
The full control data is as follows:
    {control_data}
""")

control_md = control_data.metadata
print(f"""
The control metadata is as follows:
    {control_md}
""")

print(f"""
The control metadata includes precision, units, control limits, and alarm info.
    precision={control_md.precision}
    units={control_md.units}
    status={control_md.status}
""")

time_md = pv.read(data_type="time").metadata
print(f"""
The time metadata is as follows:
    {time_md}

But we can more conveniently grab a datetime from its structure:
    {time_md.stamp.as_datetime()}
""")

Output:

$ python example_metadata.py

PV=mock:C

The .DESC field matches up with `doc=`:
    .DESC=b'The C pvproperty'

The RTYP field holds the record type:
    .RTYP=b'ai'


A little-known CA feature is that we can read the record type through
a special ChannelType 'CLASS_NAME':
    record_type=b'ai'


The full control data is as follows:
    ReadNotifyResponse(data=array([0.]), data_type=<ChannelType.CTRL_DOUBLE: 34>, data_count=1, status=CAStatusCode(name='ECA_NORMAL', code=0, code_with_severity=1, severity=<CASeverity.SUCCESS: 1>, success=1, defunct=False, description='Normal successful completion'), ioid=3, metadata=DBR_CTRL_DOUBLE(status=<AlarmStatus.NO_ALARM: 0>, severity=<AlarmSeverity.NO_ALARM: 0>, upper_disp_limit=0.0, lower_disp_limit=0.0, upper_alarm_limit=2.0, upper_warning_limit=1.0, lower_warning_limit=-1.0, lower_alarm_limit=-2.0, upper_ctrl_limit=3.0, lower_ctrl_limit=-3.0, precision=3, units=b'mm'))


The control metadata is as follows:
    DBR_CTRL_DOUBLE(status=<AlarmStatus.NO_ALARM: 0>, severity=<AlarmSeverity.NO_ALARM: 0>, upper_disp_limit=0.0, lower_disp_limit=0.0, upper_alarm_limit=2.0, upper_warning_limit=1.0, lower_warning_limit=-1.0, lower_alarm_limit=-2.0, upper_ctrl_limit=3.0, lower_ctrl_limit=-3.0, precision=3, units=b'mm')


The control metadata includes precision, units, control limits, and alarm info.
    precision=3
    units=b'mm'
    status=0


The time metadata is as follows:
    DBR_TIME_DOUBLE(status=<AlarmStatus.NO_ALARM: 0>, severity=<AlarmSeverity.NO_ALARM: 0>, timestamp=1619713291.658403)

But we can more conveniently grab a datetime from its structure:
    2021-04-29 16:21:31.658403

Apologies for any typos/points I've missed from your question, as usual I'm reading/responding to issues during meetings.

klauer avatar Apr 29 '21 16:04 klauer

Anyone want to summarize the above and add it to caproto docs? Marking this 'help wanted'

klauer avatar May 18 '23 21:05 klauer

Hello I have a related question.

From reading the code and the example above, I get it is not possible monitor metadata and timestamps at the same time (probably due to how CA works). Is that correct?

What I would like is a way of getting all data, include what would come with data_type='control' AND data_type='time'.

The only way I could think of doing that is when receiving a subscription request, do a simple read with data_type='control', store the metadata, and then keep the subscription running with data_type='time' only. I would then combine the stored information with the timestamps/alarms on the callback function and send them to the client.

This would prevent me to getting updates on metadata though (even if that is not expected to happen, it might, for example a eventual change of limits). Maybe I can keep both monitors running?

Do you have any example applications of where something similar is done? I thought of using the pyepics compatible client (in which I can access everything using **kwargs), but is there a better approach here?

AndreFavotto avatar Nov 10 '25 11:11 AndreFavotto