Scientific Computing

GNSS data abbreviations

Some of the most fundamental GNSS measurements retrieved from GNSS receivers include:

  • CNR: carrier to noise ratio or C/N0. The estimation technique varies between receivers. Typical values in the range 30..50 [dB Hz]
  • PSR: Psuedorange [meters]
  • ADR: accumulated Doppler range–carrier phase measurements [cycles]

Using networks of GNSS receivers along with appropriate post-processing techniques, estimated maps of vertical TEC (integrated electron density) can be derived.

Reference

Numpy / OpenCV image BGR to RGB

Conversion between any/all of BGR, RGB, and GBR may be necessary when working with

  • Matplotlib pyplot.imshow(): M x N x 3 image, where last dimension is RGB.
  • OpenCV imshow(): M x N x 3 image, where last dimension is BGR
  • Scientific Cameras: some output M X N x 3 image, where last dimension is GBR

Note: as in any programming language, operations on memory-contiguous arrays are most efficient. In particular, OpenCV in-place operations require a contiguous array from Python to avoid unexpected results. The safest approach is to always make a copy of the array as in the examples below.

Use .copy() to avoid unexpected results if using OpenCV. If just using Matplotlib, .copy() is not necessary–but performance (speed) may benefit from .copy().


BGR to RGB: OpenCV image to Matplotlib

rgb = bgr[...,::-1].copy()

RGB to BGR: Matplotlib image to OpenCV

bgr = rgb[...,::-1].copy()

RGB to GBR:

gbr = rgb[...,[2,0,1]].copy()

The axis order convention for Python images:

  • 3-D: W x H x 3, where the last axis is color (e.g. RGB)
  • 4-D: W x H x 3 x 1, where the last axis is typically an alpha channel

Further examples:

CMake builds for modern C++

Non-standard language options and incomplete feature support are normal for compilers across virtually all programming languages from BASIC to Fortran and here C++. Modern C++ features typically require using specific compiler flags to enable support. Knowing what compiler flags to set can be confusing for those new to modern C++ features. Setup of C++ compiler flags for modern C++ features is easily and automatically handled by CMake.

add_executable(filesep_cpp filesep.cpp)
target_compile_features(filesep_cpp PRIVATE cxx_std_17)

C++ fstream allows writing files to disk. Some operations need to manage directory slashes (Windows vs. POSIX). C++ std::filesystem::path::preferred_separator manages platform-agnostic path separators. Akin to Python pathlib, use std::filesystem::path. C++ filesystem works on almost all current C++ compilers.

#include <filesystem>
#include <iostream>

int main() {
    std::cout << std::filesystem::path::preferred_separator << "\n";
    return 0;
}

brew-like Scoop for Windows

Note: Winget might be preferred for Windows packages.


Scoop brings easy install like scoop install gcc of developer programs in the package list to Microsoft Windows. Scoop works from a fresh Windows install, for example a free Windows virtual machine image.

Install Scoop, then install Git and SSH via Scoop, so that Scoop can update its recipes:

scoop install git openssh

Commmon development tools:

  • gcc / gfortran: scoop install gcc
  • make / cmake: scoop install make cmake
  • clang / LLVM: scoop install clang
  • GNU Octave: scoop install octave

From time to time scoop update gcc or similar to update individual packages.

Scoop quick start

One-click RDP + SSH Linux to remote Windows PC

Assumes Linux laptop to connect to a remote PC such as:

On the Linux laptop:

apt install freerdp2-x11
Remote PC IP Remote PC SSH port Remote PC RDP port
1.2.3.4 22 (open TCP firewall) 3389 (blocked by remote PC firewall)

Create executable script myrdp.sh on the Linux laptop:

#!/bin/sh

ssh -f -L 4389:localhost:3389 remoteusername@1.2.3.4 sleep 1;

xfreerdp /v:localhost:4389

Running that script on the Linux laptop connects using RDP over SSH to the Windows computer

Notes

  • Advanced freerdp configuration (e.g. limited bandwidth)
  • xfreerdp command line options
  • If freerdp2-wayland on Wayland doesn’t work, try freerdp2-x11.

Related: Windows to Windows SSH / RDP

PNG stack to animated GIF using FFmpeg

FFmpeg converts all PNGs in a directory into an animated GIF by:

ffmpeg -pattern_type glob -i '*.png' out.gif
-pattern_type glob -i '*.png'
collect all .png files in this directory

Windows FFmpeg file globbing

FFmpeg file globbing does NOT work on Windows, even with FFMPEG 4.x. The error on Windows is like:

Pattern type ‘glob’ was selected but globbing is not supported by this libavformat build

Windows FFmpeg users, consider using Windows Subsystem for Linux for file globbing with FFmpeg.

PNG stack to AVI using FFMPEG

FFmpeg losslessly converts all PNGs in a directory into a single lossless AVI video file by:

ffmpeg -framerate 5 -pattern_type glob -i '*.png' -c:v ffv1 out.avi
-framerate 5
show 5 PNG image frames per second.
-pattern_type glob -i '*.png'
collect all .png files in this directory
-c:v ffv1
use the lossless FFV1 codec

If you have problems playing back the .avi file, try omitting the -c:v ffv1 parameter. Don’t go below a framerate of about 3 frames/second because some viewers won’t work (e.g. VLC).

FFmpeg file globbing does NOT work on Windows, even with FFMPEG 4.x. The error on Windows is like:

Pattern type ‘glob’ was selected but globbing is not supported by this libavformat build

Windows FFmpeg users, consider using Windows Subsystem for Linux for file globbing with FFmpeg.

Overloading functions in Matlab and GNU Octave

Matlab and GNU Octave are constantly adding new functionality. However, legacy versions remain in use for years. Overloading a built-in function in Matlab and Octave requires logic to account for Octave providing many functions as m-files rather than builtin as in Matlab.

This example overloads isfile for single files.

Create a file “isfile.m” containing:

function ret = isfile(path)

if exist('isfile', 'builtin') == 5 || exist('isfile', 'file') == 2
  ret = builtin('isfile', path);
else
  ret = exist(path, 'file') == 2;
end

end

Using Ruby Gem install with GitHub Actions

To use Ruby quickly and easily in GitHub Actions, add this YaML snippet in your Job:

    - uses: actions/setup-ruby
      with:
        ruby-version: '2.x'

Example

A complete job (named integration) example where Ruby packages are called from Python is below. Example from Python Linguist.

  integration:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout
    - uses: actions/setup-python
      with:
        python-version: '3.x'
    - uses: actions/setup-ruby
      with:
        ruby-version: '2.x'
    - run: gem install github-linguist --no-document
    - run: pip install -e .[tests]
    - run: pytest

Force apt to IPv4

Sometimes a remote host or ISP is temporarily misconfigured with regard to IPv6. In this case, apt update can simply hang, for example:

Connecting to archive.raspberrypi.org (2a00:1098:0:80:1000:13:0:5)

apt is forced to use IPv4 with “apt” option -o Acquire::ForceIPv4=true.

Example:

apt -o Acquire::ForceIPv4=true update

apt -o Acquire::ForceIPv4=true upgrade