Scientific Computing

Monitor website status with curl

When a desired website is down or a new website is about to become available, one can use the “curl” program to check the status of the website HTTP response code. Using a periodic check with watch program one can monitor the status for an extended period of time. Keep in mind that the website might have anti-DOS measures that block repeated requests from the same IP address. Set “watch” interval and maximum number of requests to avoid being blocked.

Use a command like the following to check every 60 seconds for up to 60 times (1 hour):

watch -n 60 -q 60 curl -s -w %{response_code} -o /dev/null URL

Curl command reference:

curl -s -w %{response_code} -o /dev/null URL
-o /dev/null
discard the webpage content to avoid screen clutter. On Windows, use -o NUL instead.
-s
silent mode
-w %{response_code}
write the HTTP response code to console
URL
the desired URL to check

C++ attribute specifiers

C++ attribute specifiers add metadata to declarations and definitions. The metadata can be used by the compiler and by the developer to help understand the code intent.

Commonly used C++17 attributes such as [[maybe_unused]] and [[fallthrough]] suppress compiler warnings and signal developer intent for clarity.

C++20 attributes such as [[likely]] and [[unlikely]] may be used by the compiler to optimize compilation, and also provide human code readers insight into the intent of the algorithm.

GCC ≥ 5 feature test macro __has_cpp_attribute() checks if an attribute is supported by the compiler–even if the command line specified standard is older.

maybe_unused attribute

To enable code to fallback to no attribute with older compilers, use [[maybe_unused]] attribute in declaration AND in the definition to be compatible with GCC:

int windows_fun(int x, [[maybe_unused]] int y) {

#ifdef _WIN32
  return y;
#else
  return x;
#endif
}

On non-Windows systems, the compiler would have issued a warning about y being an unused argument. The [[maybe_unused]] attribute suppresses that warning.

fallthrough attribute

The [[fallthrough]] attribute is used to indicate that a fall-through in a switch statement is intentional. This attribute can be used to suppress warnings about missing break statements in a switch block.

In the header file:

#ifndef __has_cpp_attribute
#define __has_cpp_attribute(x) 0
#endif

#if __has_cpp_attribute(likely)
#define LIKELY [[likely]]
#else
#define LIKELY
#endif

switch (x) {
  case 1:
    x++;
    [[fallthrough]];
  case 2:  LIKELY
    x--;
}

cmake_path(GET in PARENT_PATH out) trailing slash

When using cmake_path(GET in PARENT_PATH out) to manipulate paths in CMake, be cautious about trailing slashes. The usual intent is to get the parent directory of a given path. If the trailing slash is not stripped, the same directory is returned, which may be unexpected.

Strip the trailing slash first like:

set(p "/path/to/directory/")

# Strip trailing slash
string(REGEX REPLACE "[\\/]+$" "" p "${p}")

cmake_path(GET p FILENAME n)

GitHub Oauth private repo access

To give secure access to private GitHub repositories on less-trusted systems like CI or HPC or shared workstation, consider GitHub Oauth tokens. The Oauth token can give read-only (or other fine-grained permissions) to all or a specific subset of repositories the GitHub account has access to.

Create a GitHub Oauth token with the desired permissions.

For read-only private GitHub repo access the “repo” permission group is selected.

Copy the text string token and SSH into the remote system where access is desired. Configure the global user Git config to use the Oauth token for the desired GitHub organization or user.

Private GitHub repo of a collaborator

Suppose a coworker with GitHub username “my1friend” has a private GitHub repo “myrepo”. Have “my1friend” add your GitHub username as a collaborator in the “myrepo” settings. Create a GitHub Oauth classic token with the “repo” permission group. Copy and paste that token string into the laptop or workstation you’ll use to access the private repo like:

git config --global url.https://oauth2:OauthToken@github.com/my1friend/.insteadOf https://github.com/my1friend/

A similar syntax is used for GitHub organizations or specific repositories.

The text OauthToken is replaced with the actual Oauth token string copied from GitHub.

Troubleshooting

Use git remote -v in the Git repo or Git submodule to ensure old information in ~/.gitconfig is not overriding the new API token. If a Git submodule won’t clone due to incorrect token credentials, try Git cloning the submodule repository itself in another directory to see if the old credential are slipping in somewhere.


Related: Git pull HTTPS push SSH

CMake per-language option flags

CMake can set per-language compiler flags scoped to directory or target. The directory scope propagates to targets in the directory and subdirectories. Per-language options that are intended for all targets in the project are often set near the beginning of the top-level CMakeLists.txt, before any targets are declared. The same concepts and scope apply to compile definitions and linker flags.

By default, add_compile_options() and target_compile_options() add flags for all target languages. Multi-language targets (for example, a target that has C and C++ source files) would have the flags applied to all languages. In general, this may cause problems if the flags are not appropriate for all languages.

The options are restricted by compiler, language, build configuration, etc. using CMake generator expressions. For example, $<COMPILE_LANGUAGE:Fortran> is used to restrict options to Fortran targets. A stanza like $<AND:$<COMPILE_LANGUAGE:Fortran>,$<CONFIG:Debug,RelWithDebInfo>> sets flags for Fortran targets in the debug and RelWithDebInfo build configurations.

Note: where using FetchContent, the main project may desire -Wall but this may cause megabytes of warnings from a legacy Fetched project. A solution is to put add_compile_options() in each of the parent project directories and/or use target_compile_options() in the parent project.

This example is for a C and Fortran project, where some flags apply to C and Fortran, and other flags are Fortran-specific. It’s often useful to use a convenience function to make the code more readable, say in a file “AddOptions.cmake”:

function(add_compile_lang_options lang)

  set(args "")

  foreach(arg ${ARGN})
    string(APPEND args ${arg}$<SEMICOLON>)
  endforeach()

  add_compile_options($<$<COMPILE_LANGUAGE:${lang}>:${args}>)
endfunction()
project(Demo LANGUAGES C Fortran)

include(AddOptions.cmake)

add_compile_lang_options(Fortran -Wall -fimplicit-none)

# more advanced example to add debug-only, compiler-specific and language-specific options:

add_compile_options(
  $<$<AND:$<COMPILE_LANG_AND_ID:Fortran,GNU>,$<CONFIG:Debug,RelWithDebInfo>>:-Werror=array-bounds>
)

FFmpeg merge files into single multichannel file

FFmpeg can merge several files each containing one audio channel into a single file with multiple channels. For example, a multichannel data collection might save each sensor channel into individual files. For data analysis and for practicality to avoid mixing channels from different experiments, it can be useful to combine the individual channel files into a single multichannel file. The amerge filter can merge multiple files into a single multichannel file. . The amerge filter requires all input files to have the same sample rate and format.

CB Radio President P channels

The mapping between CB radio channel to President P channel and coded squelch is:

President P channel CB channel Center Frequency (MHz) coded squelch
1 1 26.965 94.8 Hz
2 3 26.985 D073N
3 11 27.085 131.8 Hz
4 18 27.175 D624N
5 24 27.235 203.5 Hz

Some newer President CB radios have preprogrammed FM mode with CTCSS squelch on “President P channels”:

Name Model FCC ID
Bill III FCC TXUS201 2AEOCPC210
Blll II+
Johnson III TXPR767
Barry II+ TXPR122
Taylor IV CL TXPR423

Similar to some OEM FRS / GMRS radios that have preprogrammed channels with CTCSS or DCS codes, the P channels are a convenience that immediately allow users with CB radios having FM and CTCSS to interoperate with each other. We welcome preprogrammed P channel-like features with FM and CTCSS as they are likely to increase usage of FM mode and CTCSS on CB radios. This gives the benefits of maximum range with minimum interference. Before these preprogrammed channels, setting CB radio CTCSS coded squelch involved diving through menus.

Using FM mode with coded squelch experiences much less disturbance from adjacent channel splatter versus the AM mode with signal squelch.

Weather radio channel to frequency

While dedicated NOAA NWR weather (WX) radios are available, two-way communications radios such as CB radio and GMRS etc. can also have 162 MHz weather radio receivers. 162 MHz WX radio is a one-way radio service that broadcasts weather information and emergency alerts across much of the USA, Canada, and Mexico. When determining which WX channel to receive on, it can be confusing because there is no standard WX channel number to 162 MHz frequency mapping. Use the NOAA USA search or Canada search to find nearby WX radio transmitter frequencies.

Channel number to frequency mapping: a common practice is to start with the lowest frequency and go up across the 7 WX radio frequencies. This is perhaps the most common channel number to frequency mapping.

WX Channel Frequency (MHz)
1 162.400
2 162.425
3 162.450
4 162.475
5 162.500
6 162.525
7 162.550

Non-sequential channel order is used by some CB radios such as Radioddity / Anytone radios made by Qixiang.

WX Channel Frequency (MHz)
1 162.550
2 162.400
3 162.475
4 162.425
5 162.450
6 162.500
7 162.525

Comparing the reception performance on a known frequency vs. a handheld radio can give a quick indication that an antenna system is working OK–even if like 27 MHz CB radio the designed antenna frequency is far from the 162 MHz weather radio frequency. Comparing WX radio reception on a CB radio with base or mobile antenna versus a handheld radio on the same 162 MHz WX radio frequency indicates that the CB radio antenna coax is perhaps not severely damaged or lossy, and the radio is working OK.

TV unable to receive any ATSC channels

Modern sub-$250 television receivers make compromises in low cost hardware while supporting the best possible picture quality for internet streams. This can lead to compromises in fundamental performance for over-the-air (OTA) reception of digital ATSC TV signals. For example, the ability to monitor signal strength is often not present in such low-cost TVs. When a channel(s) that should be easily received is not available, the fundamental step of checking signal strength might be unavailable on a low-cost TV. A first step is generally to rerun the channel scan with the antenna (if indoor) positioned in a window facing the transmitter direction.

A diagnostic tool and possible solution is an ATSC tuner connected to the TV via HDMI. ATSC 1.0 HDMI tuners are available for under $30. The dedicated tuner may be of higher quality than the TV’s built-in tuner, and may provide a signal strength reading. Check product reviews to ensure the tuner is of good quality and has a signal strength meter. Some tuners can connect to an Android device or laptop via USB to receive broadcasts or monitor signal strength. This can allow a technically-minded user to diagnose reception issues including interference.