Scientific Computing

Low priority Python execution

A data acquisition system typically runs multiple programs simultaneously:

  1. High-priority data collection - a program driving high data bandwidth acquisition
  2. Low-priority monitoring tools - check system status or process data
  3. Low-priority services - web servers displaying processed results

Properly prioritizing these processes ensures the critical data collection isn’t interrupted by secondary tasks.

Suppose a Python script “preview.py” is monitoring a camera’s status and needs to run at low priority.

On Unix-like systems (Linux, macOS, BSD, …) use nice to control process priority.

nice -n 19 python preview.py
nice -n
run task(s) with priority, where bigger positive numbers are lower priority, and more negative numbers are higher priority.

On Windows the start command can control program run priority.

start /low python preview.py
start /low
runs a program and its child processes at low priority.

Repairing broken symbolic link

A symbolic link may become broken due to updating/compiling/installing software, showing up as red text in ls Terminal.

Suppose the broken link is named “foo.so”: Find where link pointed to:

readlink -v foo.so

Point link to new file (perhaps foo2.so)

ln -sfn foo2.so foo.so

Verify new link is blue color

ls foo.so

Linux loopback audio record

Easily record the sounds heard through system speakers on Linux with Pulseaudio and Audacity.

apt install audacity pavucontrol

To record “what you hear” in Audacity: “click to monitor” (right of microphone in main Audacity screen). Start Pulseaudio advanced configuration tool

pavucontrol

Under pavucontrol Recording tab input, select “Monitor of Built-in XXX” where XXX is the playback device you wish to monitor. This loopback audio from pavucontrol allows recording what you hear in Linux using Audacity and PulseAudio.

PulseAudio loopback

List comments/annotations in PDF

On Windows, Adobe Reader can export and view a list of comments through the Comment tab on the right side. On Linux, in Evince PDF viewer, the side pane has a drop-down annotations menu that lists comments. Press F9 key if you don’t see the left-hand panel.

One can extract all comments and annotations from a PDF file by

apt install texlive-latex-extra

pdfannotextractor --install

pdfannotextractor in.pdf

This yields a .pax file without plain text. However, it was not clear how to read this file.

Open same PDF multiple times in Evince

It’s often useful to open the same PDF file multiple times, particularly when reviewing journal article drafts to view figures simultaneously with text describing the figure.

From Evince (popular Linux PDF viewer), Menu select “Open a Copy”. Open as many copies of the same Postscript or PDF document as desired.

Restarting Unity desktop in Ubuntu

This is for Ubuntu < 18.04. Ubuntu ≥ 18.04 no longer use Unity.


If the desktop seems to freeze for more than a few seconds, try

  1. press together the keys: Ctrl Alt F1
  2. login and type
unity & disown

This leaves desktop/apps all as they were while fixing the broken state.

Full desktop reset

Restart the whole Unity desktop (which is like unto logging out and logging in, discarding the current desktop):

service lightdm restart

Jekyll Markdown syntax highlighting

Jekyll uses Markdown or HTML files to statically render complex websites from simple text files. While Jekyll syntax highlighting is possible, more often we might use more generic Markdown commands.

Enable syntax highlighting: Before every code block, simply include the language name, for example

```fortran

this even works for gdb.

The color code syntax highlighting on your webpage looks quite striking, and the number of languages covered is very extensive. Despite the different syntax highlighter modules used by different Jekyll web hosting services, almost every language works with the method above.

Github uses Linguist syntax highlighting for its own service (Issues, README.md, etc.) Github/Gitlab Pages can use Rouge syntax highlighting via Jekyll _config.yml – specify an allowed syntax highlighter.

Read last lines of file with Python deque

Python collections.deque is a fast FIFO that does not require thread locking as the more sophisticated queue.

Use deque to read the last line of files as follows.

from pathlib import Path
from collections import deque

fn = Path('myfile.txt')

with fn.open('r') as f:
    last = deque(f, 1)[0]
last
last line of file. Throws IndexError if file is empty.

By changing 1 the last N lines of the file may be read and indexed.

deque examples

MURS VHF vs. 900 MHz modems

At first glance, the two watt transmit power limit across five VHF channels can make MURS seem appealing for long-range license-free radio systems. In fact, companies such as GoTenna initially chose MURS for their first generation product.

The clear choice for almost all applications is to use 900 MHz modems over MURS 150 MHz modems. Contact us for a more formal analysis for your application.

MURS data bandwidth:

Freq [MHz] raw data rate (kbps)
151.82 9600
151.88 9600
151.94 9600
154.57 19200
154.60 19200

At first glance, MURS seems to have a comparable data rate to the $39 one watt 868/900/920 MHz modems, and similar hardware pricing, with seemingly longer range due to 150 MHz vs. 900 MHz frequency. Interference limits data throughput.

MURS interference

MURS arose in part because of the decades-long abuse of these five VHF itinerant frequencies. Like the delicensing of 27 MHz CB Radio, the license-free MURS channels arose due to chronic unlicensed use. Warehouses, hotels, shopping malls, construction sites, etc. use MURS frequencies.

The critical point is that in general MURS modems are stuck on one frequency until reprogrammed. $14 walkie talkies meanwhile are clogging up the channels. Cable TV leakage is also a problem at VHF.

Many applications of wireless modems have a latency requirement–how long will an MURS channel be blocked by noise and other users? The answer comes in using 900 MHz spread-spectrum radio.

900 MHz advantages

900 MHz is also a heavily used frequency band. The key distinction is this use is spread over as much as 26 MHz (USA). The one watt transmit power allowed in the USA and numerous other countries, and tens of milliwatts in other countries allows multi-kilometer line-of-sight range, just like 150 MHz radios.

Some 900 MHz modems have a channel list where they hop upon interference, while the high power radios typically employ FHSS. Having a clear RF channel via FHSS is key to getting even low data bandwidth transmissions through reliably with low latency.

Power on/off USB port in Terminal

USB host/hubs can use Terminal commands to power on/off USB port remotely.

  • Linux: apt install gcc make libusb-1.0-0-dev
  • macOS: brew install gcc libusb make

The uhubctl program allows controlling power for many USB hub models:

git clone https://github.com/mvp/uhubctl

make

./uhubctl -h

Note: key system devices, even built-in laptop keyboards are connected by USB, and you could accidentally poweroff your own laptop keyboard. So use a little care to identify which hardware port a device is connected to.


Beaglebone Black can power on/off USB ports via devmem2, useful for automatic remote powering on/off of USB peripherals to save energy/heat.

In general devmem2 reads/writes bytes, here the address to control the Beaglebone USB port is known a priori.

apt install devmem2

If apt install devmem2 doesn’t work, you can manually compile:

git clone https://github.com/VCTLabs/devmem2
make
make install

Power on USB port remotely:

devmem2 0x47401c60 b 1

This powered up my Wifi dongle.

I measured 0 V on the USB host port before issuing this command, and measured 5 V after the command.

Reference for USB power on command using devmem2