9. Add SAMP Magic

SAMP lets you exchange data between VO clients. Your script is a VO client, too. Let’s make it broadcast some of the found images:

  with vohelper.SAMP_conn() as conn:
    ... (search) ...
    vohelper.send_image_to(conn, None, image.acref)

[See globalsiapsamp.py]

(also, vohelper.py abstracts SAMP here).

For this course, but probably also for convenience in wider usage, we have gathered some helper functions in a module vohelper that you can find on the web page and attached to the PDF. Have a glance at the source code if you want. Otherwise, just dump it next to your scripts so you can import it.

Before running this, start Aladin so the images are displayed.

SAMP-enabling programs may not come quite natural to people who so far have mainly written fairly linear science code, because when doing SAMP you usually want to react to external events. In linear code this is rather uncommon.

In this example we are just sending data, which does not require much reacting to external signals. We still have to manage the connection to the SAMP hub – things get ugly if you don’t properly close the connection –, which is taken care of by a context manager from vohelper.

If you inspect how send_image_to actually is implemented in vohelper, the way arguments are passed between SAMP services may seem a bit funky: We build message dictionaries with odd keys and then use methods on the “conn” object. But think of the mtype as the function name, and passing arguments in dictionaries instead of tuples isn’t that far-fetched, either.

Given we’re doing function calls between different processes written in different languages, I’d argue this kind of code actually is surprisingly compact.

Problems

(1)

Have a look at the implementation of SAMP_conn in vohelper. This is done as a context manager, which is a python construct ensuring what’s called “external invariants” (e.g., the status of a file is closed before and after a piece of code that needs it). They’re used together with the with keyword that you may already know from file handling.

Can you imagine why such a context manager is a good idea here? Try the code creating a connection and run it without a disconnect several times. Look at the SAMP info in TOPCAT meanwhile.

Files


Markus Demleitner, Hendrik Heinl