Skip to content

Instantly share code, notes, and snippets.

View scivision's full-sized avatar
💭
I will be slow to respond.

scivision

💭
I will be slow to respond.
View GitHub Profile
@scivision
scivision / CMakeLists.txt
Last active September 4, 2023 01:20
Override CMake-generated compiler flags
cmake_minimum_required(VERSION 3.10)
project(foo LANGUAGES C)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# use add_compile_options() instead of setting CMAKE_C_FLAGS to ensure the user flags come last,
# that is the user flags take precedence
add_compile_options(
"$<$<COMPILE_LANGUAGE:C>:-Ofast;-march=native;-ftree-vectorize>"
@scivision
scivision / CMakeLists.txt
Created August 15, 2023 17:12
CMake CppCheck example
cmake_minimum_required(VERSION 3.19)
project(DemoCppCheck LANGUAGES C CXX)
option(cppcheck "Run CppCheck static code analysis")
if(cppcheck)
find_program(cppcheck_exe NAMES cppcheck REQUIRED)
set(cppcheck_opts --enable=all --inline-suppr --quiet --suppressions-list=${PROJECT_SOURCE_DIR}/cppcheck.supp)
set(CMAKE_C_CPPCHECK ${cppcheck_exe} --std=c11 ${cppcheck_opts})
@scivision
scivision / CMakeLists.txt
Last active August 11, 2023 02:51
CMake print generator version (Make, Ninja, Visual Studio ...)
execute_process(COMMAND ${CMAKE_MAKE_PROGRAM} --version
OUTPUT_VARIABLE gen_ver)
string(REGEX REPLACE "\n" ";" gen_ver "${gen_ver}")
list(GET gen_ver 0 gen_ver)
message(STATUS "${CMAKE_GENERATOR}
${CMAKE_MAKE_PROGRAM}
${gen_ver}")
@scivision
scivision / tee_subprocess.py
Last active August 7, 2023 19:45
Tee Python subprocess live to Terminal and accumulate in variable buffer simultaneously
#!/usr/bin/env python3
"""
tee output to screen and buffer variable simultaneously,
so that user sees terminal output while a long-running process
executes. The full text buffer is retained to check for messages etc.
https://stackoverflow.com/a/25755038
"""
from io import StringIO
@scivision
scivision / pre-commit
Created August 2, 2023 21:45
Strip Jupyter notebook outputs as Git pre-commit hook
#!/usr/bin/env python3
"""
Strip Jupyter notebook outputs as Git pre-commit hook
"""
import subprocess
from pathlib import Path
files_changed = subprocess.check_output(["git", "diff", "--staged", "--name-only"], text=True).split("\n")
@scivision
scivision / CMakeLists.txt
Last active July 21, 2023 23:14
CMake 3.27 Fortran module directory Ninja changes
cmake_minimum_required(VERSION 3.19)
project(FortranInclude LANGUAGES Fortran)
message(STATUS "${CMAKE_SYSTEM_NAME} CMake ${CMAKE_VERSION} ${CMAKE_Fortran_COMPILER_ID} ${CMAKE_Fortran_COMPILER_VERSION}")
execute_process(COMMAND ${CMAKE_MAKE_PROGRAM} --version OUTPUT_VARIABLE gver OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS "${CMAKE_GENERATOR} ${CMAKE_MAKE_PROGRAM} ${gver}")
if(APPLE)
@scivision
scivision / ssize_t.c
Last active August 16, 2023 01:25
Use ssize_t on POSIX systems and with MSVC compilers
#include <stdint.h> /* C23: SIZE_WIDTH, PTRDIFF_WIDTH */
#include <stddef.h>
#include <stdio.h>
#define ssize_t ptrdiff_t
int main(void){
size_t s;
@scivision
scivision / modern_importlib_resources.py
Last active June 29, 2023 14:18
importlib.resources for Python >= 3.9
#!/usr/bin/env python3
"""
Assumes executable "amender.bin" exists in the Python package and you want to resolve its full path and run it.
"""
import os
import subprocess
import importlib.resources as impr
@scivision
scivision / time_zone.f90
Created June 15, 2023 13:59
Get local machine time zone in Fortran
program main
!! prints local machine timezone offset from UTC in hours
!! based on https://github.com/wavebitscientific/datetime-fortran/pull/80/files
use, intrinsic :: iso_fortran_env, only: real64
implicit none
character(len=5) :: zone
integer :: values(8)
@scivision
scivision / read_gnss_los.py
Created June 9, 2023 19:45
read Madrigal GNSS LOS file
"""
read Madrigal GNSS LOS file
based on http://cedar.openmadrigal.org/static/siteSpecific/programming_tips.pdf
"""
import time
import h5py
import numpy
import argparse