Scientific Computing

read large RINEX files in Python with GeoRINEX

Reading RINEX files in Python or other languages historically required compiling or buying complex software. The GeoRINEX Python 3 program and library to be used with RINEX OBS and NAV files, including Hatanaka compressed files or other compression wrappers like ZIP.

GeoRINEX can even read only every N seconds from a RINEX file with option georinex.load(..., interval=N). This greatly speeds up reading where coarser time intervals than the RINEX file provides is needed. GeoRINEX uses performance-oriented techniques to read RINEX files at speeds approaching compiled languages, using Pure Python + Numpy and Xarray for metadata rich results.

If you use GNSS and RINEX in your work, you may like GeoRINEX for Python 3.

Disable Windows Fast Boot

For those dual-booting Windows and Linux, it’s useful to disable Windows Fast startup so that you can access the Windows partition from Linux. Disabling Fast Boot may add a few seconds to boot time. When you shutdown your PC, Fast Boot actually reboots and then hibernates. This locks the Windows partition so that Linux can’t access it.

Disable Fast Boot: +r powercfg.cpl → “Choose what the power button does”. We typically disable hibernate as well to save on SSD wear.

Disable fast boot

BitLocker and dual-booting: If BitLocker is enabled, Linux cannot access the Windows partition.

Fast Boot SSD wear: Fast Boot just like hibernate increases solid state drive (SSD) wear, since you are dumping an image ~ RAM size to disk on each Fast Boot shutdown.

Python pathlib cast to str for subprocess

Python pathlib is part of Python standard library. When using Python subprocess, in general, do str(mypath) where type(mypath) is pathlib.Path.

Example:

#!/usr/bin/env python
from pathlib import Path
import subprocess

P = Path('~').expanduser()

O = subprocess.check_output(['dir', str(P)])

print(O)

if you don’t cast pathlib.Path to str:

TypeError: argument of type ‘WindowsPath’ is not iterable.

Notes

You should rarely if ever need shell=True. On Windows in particular, give the full path to a non-system executable.


Related: Idiomatic pathlib Python

Recursive sed find and replace text

Find and replace text in files recursively on Linux, macOS and Windows Subsystem for Linux with recursive_sed.sh:

#!/usr/bin/env bash
#
# The quoted variables allow using backticks and other "nasty" characters in sed.
#
# usage:
# ./recursive_sed.sh  pathtofiles yuck yay
#
# References:
# https://stackoverflow.com/a/1585810
# https://unix.stackexchange.com/a/128758

set -o errexit
set -o nounset

path=$1
old=$2
new=$3

echo "${old} => ${new}"

find $path -not -path '*/.git*' -type f -execdir sed -i 's/'"${old}"'/'"${new}"'/g' {} +

Pdoc Python quickstart

Pdoc is vastly simpler to use than Sphinx for small projects, with good HTML formatting.

In distinction from Sphinx, pdoc does not parse RST docstrings, but Pdoc does allow the following docstring formats:

At this time, pdoc3 doesn’t consider Python type hinting.

Install

pdoc is a light minimal install.

pip install pdoc3

Usage

We use the pymap3d project with auto-generated docs as an example.

From the top-level pymap3d/ directory, type

pdoc --html pymap3d

This creates several *.html files under html/pymap3d, including index.html.

Check the output by:

firefox html/pymap3d/index.html

If the output seems satisfactory, move the files to the gh-pages branch, or at least to a docs/ folder:

mv html/pymap3d/*.html docs/

Publish the docs to the web by:

  1. In the Github repo Settings → Github Pages → Source: “gh-pages”
  2. git commit and push. It takes a few seconds for Github Pages to deploy HTML.

Set Python version with update-alternatives

Python users typically use a non-system Python distribution such as Miniconda. Python distributions typically allow easy switching of Python version. Where Linux system Python must be used, the default Python version can be switched persistently with update-alternatives.

Update-alternatives without sudo: a one-time setup.

Configure shell to use ~/.local/bin instead of system-wide /usr as follows:

mkdir ~/.local/bin

Add to ~/.profile:

export PATH="$HOME/.local/bin:$PATH"

Enable switching Python default between Python versions with these one-time commands:

update-alternatives --install $HOME/.local/bin/python python /usr/bin/python3 20

After the setup above, at any time select the default system Python version without sudo:

update-alternatives --config python

Installing GNU Radio

GNU Radio is trivially easy to install on Ubuntu, which includes the UHD driver.

apt install gnuradio

DO NOT use GNU Radio from a virtual machine in general with an SDR, as typically many packets will be lost and the data will not be useful. The typical best choice is to use a GNU Radio live USB image. Frequent GNU Radio users often dual boot into Ubuntu for working with SDR.

Build GNU Radio from source

Optionally compile GNU radio yourself

HP Printer setup on Linux

The HP-Setup program sets up HP printers on Linux and is easy to install and use.

Install HP setup program:

apt install hplip

Get the IP address of your networked HP printer from its front panel or the network address e.g. myprinter.local The CLI method is very quick and reliable.

CLI

Add the HP printer to the PC by:

hp-setup -i PrinterAddress

where PrinterAddress is the IP address e.g. 10.0.0.64 or a network name like myprinter.local

HP setup will try to select a PPD file, usually it finds a good one. Perhaps name the printer by the room name or department – it’s arbitrary.

After setup, the printer will show in:

  • system “Printers” window
  • CUPS under https://localhost:631/printers/ → Manage Printers

GUI

As noted above, this method may not work until HP updates their program to use a current Qt version.

Start GUI by typing hp-setup then select

  1. Network
  2. Manual Discovery
  3. type in printer IP address

The Printer should be instantly discovered and you’re ready to print.

Update BIOS via FreeDOS on Linux PC

BIOS update via FreeDOS works across many PCs, including Dell and Gigabyte. The key is that the OEM provides BIOS as a DOS executable.

Setup DOS-bootable USB drive: download the DOS-flashable BIOS firmware from the OEM e.g. support.dell.com Determine USB drive device (e.g. /dev/mmcblk0) by issuing this command before and after USB drive insertion:

lsblk

Format (permanently erase) this USB drive with

umount /dev/mmcblk0

mkdosfs -F 32 /dev/mmcblk0

Download the FreeDOS “USB Full Image” and extract the FD*.img file. Write this image to the USB stick:

dd if=FD*.img of=/dev/mmcblk0 bs=4M

the image write will take a minute or two, depending on the write speed of the USB stick. To be sure it’s done, wait for this command to exit in a second Terminal window:

sync

Update BIOS: extract/copy ALL the BIOS self-extracted files to the root of the FreeDOS flash drive from Linux. Insert the USB flash drive into the PC to be flashed Upon powerup, choose to boot from the USB drive. This might require pressing F12 key. In the BIOS boot device menu, look under Legacy boot. If Legacy boot is not enabled, it may need to be enabled to see the FreeDOS USB drive. Do NOT install FreeDOS, just boot to DOS when prompted by FreeDOS. In FreeDOS prompt, type the name of the .exe or .bat file for the firmware update as per OEM instructions. The flashing process may take several minutes.

If the PC says

operating system not found

on this DOS USB drive, try it in another PC. If it still doesn’t work, try another model of USB flash drive. Some flash drives just are not amenable to being booted from.

If a new computer motherboard seems to be dead out of the box, be sure the power supply is working and connected correctly. It may have a CPU that’s too new for the motherboard BIOS.

Specific example: This happened for Gigabyte GA-Q87M-D2H with version F6 BIOS. Version F7 BIOS was necessary for Haswell Refresh CPUs. I had to put an “old” CPU in the motherboard to flash the motherboard BIOS!

What does Gfortran -std=legacy allow?

The Gfortran flag -std=legacy disables warning for very out of date non-standard Fortran syntax that should not be used anymore. However, where you are using a legacy package where the code isn’t being changed, using -std=legacy can be necessary, particularly for Gfortran ≥ 8.

Some of the syntax enabled by -std=legacy includes pre-Fortran 77 arbitrary length arrays, where A(1) was declared instead of A(*) in Fortran 77 or A(:) in modern Fortran.