#!/usr/bin/env bash # # build.sh: the experiment and the pictures for "The Nebra Sky" # # Fetches the star catalogue and the photo of the disc, lays the 23 single gold dots on the sky of # 1600 BC in every possible way, does the same with random dot patterns, and draws the figures. # # Stars: HYG v4.4 by astronexus, CC BY-SA 4.0, https://codeberg.org/astronexus/hyg # Photo: WFS.Berlin, CC0, https://commons.wikimedia.org/wiki/File:Himmelsscheibe.Nebra.vorne.P1034154.jpg # Needs: curl, rsvg-convert, the Noto fonts, Python 3 with numpy, scipy and Pillow # # Usage: ./build.sh [options] # -f, --force run the experiment again although results exist # -g, --figures-only only draw the figures from the existing results # -l, --layouts random layouts to compare against (default 1000) # -j, --jobs parallel processes (default: all cores) # -n, --dry-run print what would run # -h, --help # # PYTHON=/path/to/python3 selects the interpreter. # # Output: nebra-results.json, nebra-disc.jpg and nebra-*.png next to this file. Downloads are kept # in ${XDG_CACHE_HOME:-~/.cache}/rose-nebra. With 1000 layouts the run takes about an hour on # sixteen cores. set -Eeuo pipefail HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/rose-nebra" PYTHON="${PYTHON:-python3}" FORCE=0 FIGURES=0 DRY=0 LAYOUTS=1000 JOBS="" CATALOGUE_URL="https://codeberg.org/astronexus/hyg/media/branch/main/data/hyg/CURRENT/hyg_v44.csv.gz" CATALOGUE_SHA="00b349893b9a53106dd488d8371e8d2fa586043e500bb3cdb8bff3931682197d" PHOTO_URL="https://upload.wikimedia.org/wikipedia/commons/e/e2/Himmelsscheibe.Nebra.vorne.P1034154.jpg" PHOTO_SHA="cb8aebc43839dfe3984d6d20be286bc48f4b74150b31744e3f80d95851f76683" say() { printf '%s\n' "$*" >&2; } die() { say "error: $*"; exit 2; } usage() { sed -n '/^# Usage:/,/^# PYTHON/p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; } run() { if ((DRY)); then printf '%q ' "$@" printf '\n' else "$@" fi } fetch() { # url file sha256 if [[ ! -f $2 ]] || ! sha256sum --status -c <<<"$3 $2"; then say "fetching ${2##*/}" run curl -fsSL --retry 3 -A "rose-build/1.0" -o "$2" "$1" ((DRY)) || sha256sum --status -c <<<"$3 $2" || die "${2##*/} is not the expected file; the source may have been updated: $1" fi } while (($#)); do case $1 in -f | --force) FORCE=1 ;; -g | --figures-only) FIGURES=1 ;; -l | --layouts) LAYOUTS="${2:?--layouts needs a number}"; shift ;; -j | --jobs) JOBS="${2:?--jobs needs a number}"; shift ;; -n | --dry-run) DRY=1 ;; -h | --help) usage; exit 0 ;; *) die "unknown option $1 (see --help)" ;; esac shift done for tool in curl rsvg-convert "$PYTHON"; do command -v "$tool" >/dev/null || die "$tool is not installed" done "$PYTHON" -c 'import numpy, scipy, PIL' 2>/dev/null || die "$PYTHON lacks numpy, scipy or Pillow (set PYTHON=...)" run mkdir -p "$CACHE" fetch "$CATALOGUE_URL" "$CACHE/hyg_v44.csv.gz" "$CATALOGUE_SHA" fetch "$PHOTO_URL" "$CACHE/disc.jpg" "$PHOTO_SHA" # one thread per process: the search runs many processes side by side export OMP_NUM_THREADS=1 PYTHONDONTWRITEBYTECODE=1 if ((!FIGURES)); then if ((FORCE)) || [[ ! -f $HERE/nebra-results.json ]]; then run "$PYTHON" "$HERE/skymatch.py" --catalogue "$CACHE/hyg_v44.csv.gz" --out "$HERE" \ --layouts "$LAYOUTS" ${JOBS:+--jobs "$JOBS"} else say "keeping nebra-results.json (use --force to run the experiment again)" fi fi run "$PYTHON" "$HERE/figures.py" --catalogue "$CACHE/hyg_v44.csv.gz" --photo "$CACHE/disc.jpg" --out "$HERE"