Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dark bug #530

Merged
merged 5 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 34 additions & 20 deletions scripts/darkCapture.sh
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
#!/bin/bash

# This file is "source"d into another.
cd $ALLSKY_HOME

# If we are in darkframe mode, we only save to the dark file
DARK_MODE=$(jq -r '.darkframe' "$CAMERA_SETTINGS")
USE_NOTIFICATION_IMAGES=$(jq -r '.notificationimages' "$CAMERA_SETTINGS")
# ${TEMPERATURE} is passed to us by saveImage.sh, but may be null.
# If ${TEMPERATURE} is set, use it as the temperature, otherwise read the ${TEMPERATURE_FILE}.
# If the ${TEMPERATURE_FILE} file doesn't exist, set the temperature to "n/a".
if [ "${TEMPERATURE}" = "" ]; then
TEMPERATURE_FILE="${ALLSKY_HOME}/temperature.txt"
if [ -s "${TEMPERATURE_FILE}" ]; then # -s so we don't use an empty file
TEMPERATURE=$(printf "%2.0f" "$(< ${TEMPERATURE_FILE})")
else
TEMPERATURE="n/a"
fi
fi

# If the temperature file doesn't exist, set a default value
TEMP=20
DARK_MODE=$(jq -r '.darkframe' "$CAMERA_SETTINGS")
if [ "$DARK_MODE" = "1" ] ; then
# "${CURRENT_IMAGE}" is the name of the current image we're working on.
CURRENT_IMAGE="dark.${EXTENSION}" # XXXXX in future release this will be set by saveImage.sh

TEMP_FILE="temperature.txt"
if [ -s "$TEMP_FILE" ]; then # -s so we don't use an empty file
TEMP=$(printf "%.0f" "`cat ${TEMP_FILE}`")
DARKS_DIR="${ALLSKY_DARKS}"
mkdir -p "${DARKS_DIR}"
# If the camera doesn't support temperature, we will keep overwriting the file until
# user creates temperature.txt file.
if [ "${TEMPERATURE}" = "n/a" ]; then
cp "${CURRENT_IMAGE}" "${DARKS_DIR}/dark.${EXTENSION}"
else
cp "${CURRENT_IMAGE}" "${DARKS_DIR}/${TEMPERATURE}.${EXTENSION}"
fi

mkdir -p darks
# To avoid having the websites display a dark frame, when in dark mode
# the image file is different.
DARK_FRAME="dark.$EXTENSION"
cp "$DARK_FRAME" "darks/$TEMP.$EXTENSION"

# If the user has notification images on, the current images says we're taking dark frames, so don't overwrite.
if [ "$USE_NOTIFICATION_IMAGES" = "0" ] ; then
# Go ahead and let the web sites see the dark frame. Not very interesting though...
cp "$DARK_FRAME" "${IMG_PREFIX}${FILENAME}.${EXTENSION}"
# If the user has notification images on, the current image says we're taking dark frames,
# so don't overwrite it.
# xxxx It's possible some people will want to see the dark frame even if notification images
# are being used - may need to make it optional to see the dark frame.
USE_NOTIFICATION_IMAGES=$(jq -r '.notificationimages' "${CAMERA_SETTINGS}")
if [ "${USE_NOTIFICATION_IMAGES}" = "0" ] ; then
# Go ahead and let the web sites see the dark frame to see if it's working.
cp "${CURRENT_IMAGE}" "${FULL_FILENAME}"
fi

exit 0 # exit so the calling script exits.
exit 0 # exit so the calling script exits.

else
CURRENT_IMAGE="${FULL_FILENAME}" # Not capturing darks so use standard file name
fi
117 changes: 83 additions & 34 deletions scripts/darkSubtract.sh
Original file line number Diff line number Diff line change
@@ -1,42 +1,91 @@
#!/bin/bash

cd $ALLSKY_HOME
# This file is "source"d into another.
# "${CURRENT_IMAGE}" is the name of the current image we're working on.

ME="$(basename "$BASH_ARGV0")" # Include script name in output so it's easier to find in the log file
ME="$(basename "${BASH_ARGV0}")"

# Subtract dark frame if there is one defined in config.sh
# This has to come after executing darkCapture.sh which sets ${TEMP}.

if [ "$DARK_FRAME_SUBTRACTION" = "true" ]; then
# Find the closest dark frame temperature wise
CLOSEST_TEMP=0
DIFF=100
for file in darks/*
do
if [[ -f $file ]]; then # example file name for 21 degree dark: "darks/21.jpg".
DARK_TEMP=$(echo $file | awk -F[/.] '{print $2}')
DELTA=$(expr $TEMP - $CLOSEST_TEMP)
ABS_DELTA=${DELTA#-}

if [ "$ABS_DELTA" -lt "$DIFF" ]; then
DIFF=$DELTA
CLOSEST_TEMP=$DARK_TEMP
fi
fi
done

PROCESSED_FILE="$FILENAME-processed.$EXTENSION"
DARK="darks/$CLOSEST_TEMP.$EXTENSION"
if [ -f "$DARK" ]; then
convert "$FULL_FILENAME" "$DARK" -compose minus_src -composite -type TrueColor "$PROCESSED_FILE"
RET=$?
if [ $RET -ne 0 ]; then
echo "*** $ME: ERROR: 'convert' of '$DARK' failed with RET=$RET"
exit 1
fi
# This has to come after executing darkCapture.sh which sets ${TEMPERATURE}.

if [ "${DARK_FRAME_SUBTRACTION}" = "true" ]; then
# Make sure the input file exists; if not, something major is wrong so exit.
if [ "${CURRENT_IMAGE}" = "" ]; then
echo "*** ${ME}: ERROR: 'CURRENT_IMAGE' not set; aborting."
exit 1
fi
if [ ! -f "${CURRENT_IMAGE}" ]; then
echo "*** ${ME}: ERROR: '${CURRENT_IMAGE}' does not exist; aborting."
exit 2
fi

# Make sure we know the current temperature.
# If it doesn't exist, warn the user but continue.
if [ "${TEMPERATURE}" = "" ]; then
echo "*** ${ME}: WARNING: 'TEMPERATURE' not set; continuing without dark subtraction."
return
fi
# Some cameras don't have a sensor temp, so don't attempt dark subtraction for them.
[ "${TEMPERATURE}" = "n/a" ] && return

# First check if we have an exact match.
DARKS_DIR="${ALLSKY_DARKS}"
DARK="${DARKS_DIR}/${TEMPERATURE}.${EXTENSION}"
if [ -s "${DARK}" ]; then
CLOSEST_TEMPERATURE="${TEMPERATURE}"
else
echo "*** $ME: ERROR: No dark frame found for $FULL_FILENAME at TEMP $TEMP." >> log.txt
echo "Either take dark frames or turn DARK_FRAME_SUBTRACTION off in config.sh" >> log.txt
cp "$FULL_FILENAME" "$PROCESSED_FILE"
# Find the closest dark frame temperature wise
typeset -i CLOSEST_TEMPERATURE # don't set yet
typeset -i DIFF=100 # any sufficiently high number
typeset -i TEMPERATURE=${TEMPERATURE}
typeset -i OVERDIFF # DIFF when dark file temp > ${TEMPERATURE}
typeset -i DARK_TEMPERATURE

# Sort the files by temperature so once we find a file at a higher temperature
# than ${TEMPERATURE}, stop, then compare it to the previous file to
# determine which is closer to ${TEMPERATURE}.
# Need "--general-numeric-sort" in case any files have a leading "-".
for file in $(find "${DARKS_DIR}" -maxdepth 1 -iname "*.${EXTENSION}" | sed 's;.*/;;' | sort --general-numeric-sort)
do
[ "${ALLSKY_DEBUG_LEVEL}" -ge 5 ] && echo "Looking at ${file}"
# Example file name for 21 degree dark: "21.jpg".
if [ -s "${DARKS_DIR}/${file}" ]; then
file=$(basename "./${file}") # need "./" in case file has "-"
# Get name of file (which is the temp) without extension
DARK_TEMPERATURE=${file%.*}
if [ ${DARK_TEMPERATURE} -gt ${TEMPERATURE} ]; then
let OVERDIFF=${DARK_TEMPERATURE}-${TEMPERATURE}
if [ ${OVERDIFF} -lt ${DIFF} ]; then
CLOSEST_TEMPERATURE=${DARK_TEMPERATURE}
fi
break
fi
CLOSEST_TEMPERATURE=${DARK_TEMPERATURE}
let DIFF=${TEMPERATURE}-${CLOSEST_TEMPERATURE}
else
echo "${ME}: INFORMATION: dark file '${DARKS_DIR}/${file}' is zero-length; deleting."
rm -f "${DARKS_DIR}/${file}"
fi
done

if [ "${CLOSEST_TEMPERATURE}" = "" ]; then
echo "*** ${ME}: ERROR: No dark frame found for ${CURRENT_IMAGE} at TEMPERATURE ${TEMPERATURE}."
echo "Either take dark frames or turn DARK_FRAME_SUBTRACTION off in config.sh"
echo "Continuing without dark subtraction."
return
fi

DARK="${DARKS_DIR}/${CLOSEST_TEMPERATURE}.${EXTENSION}"
fi

if [ "${ALLSKY_DEBUG_LEVEL}" -ge 4 ]; then
echo "${ME}: Subtracting dark frame '${CLOSEST_TEMPERATURE}.${EXTENSION}' from image with TEMPERATURE=${TEMPERATURE}"
fi
# Update the current image - don't rename it.
convert "${CURRENT_IMAGE}" "${DARK}" -compose minus_src -composite -type TrueColor "${CURRENT_IMAGE}"
if [ $? -ne 0 ]; then
# Exit since we don't know the state of ${CURRENT_IMAGE}.
echo "*** ${ME}: ERROR: 'convert' of '${DARK}' failed"
exit 4
fi
fi
12 changes: 0 additions & 12 deletions scripts/saveImageNight.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,6 @@ if [ $RET -ne 0 ] ; then
exit 3
fi

if [ "$DARK_FRAME_SUBTRACTION" = "true" ] ; then
# PROCESSED_FILE should have been created by darkSubtract.sh. If not, it output an error message.
PROCESSED_FILE="$FILENAME-processed.$EXTENSION"
# Check in case the user has subtraction set to "true" but has no dark frames.
if [[ ! -f "$PROCESSED_FILE" ]]; then
echo "${YELLOW}*** $ME: WARNING: Processed image '$PROCESSED_FILE' does not exist; continuing!${NC}"
else
# Want the name of the final file to alway be the same
mv -f "$PROCESSED_FILE" "$IMAGE_TO_USE"
fi
fi

# Resize the image if required
if [[ $IMG_RESIZE == "true" ]]; then
convert "$IMAGE_TO_USE" -resize "$IMG_WIDTH"x"$IMG_HEIGHT" "$IMAGE_TO_USE"
Expand Down