Jump to content

  •  

CNers have asked about a donation box for Cloudy Nights over the years, so here you go. Donation is not required by any means, so please enjoy your stay.

Photo

MacOS script to search for fits images by RA/Dec and copy them

Software
  • Please log in to reply
3 replies to this topic

#1 etotman

etotman

    Lift Off

  • *****
  • topic starter
  • Posts: 10
  • Joined: 01 Mar 2021

Posted 13 November 2024 - 06:42 PM

I wrote this script to search for fits images by RA/Dec because I found that searching for object names is inconsistent, in my case.  Also I don't save all of my images to object name folders, which I suppose I could do too.  It does require the Observatory app from the App Store, which isn't free, but this app has a lot of features that I find useful.  For example adding fits header data to spotlight metadata, which makes searching very fast using the mdfind command.

 

The script copies matching images so I can process them all at once.

 

ra_dec_search_copy.sh [options]
Options:
  --ra VALUE          Right Ascension target value (required)
  --dec VALUE        Declination target value (required)
  --output DIR         Output directory (default: ./grouped_images)
  --search VALUE   Search distance (default: 0.5)
  --help                   Show this help message

#!/bin/bash

# Default values
SEARCH_DISTANCE=0.5
OUTPUT_DIR="./grouped_images"
RA=""
DEC=""

# Function to display usage
usage() {
    echo "Usage: $0 [options]"
    echo "Options:"
    echo "  --ra VALUE       Right Ascension target value (required)"
    echo "  --dec VALUE      Declination target value (required)"
    echo "  --output DIR     Output directory (default: ./grouped_images)"
    echo "  --search VALUE   Search distance (default: 0.5)"
    echo "  --help          Show this help message"
    echo
    echo "Example:"
    echo "  $0 --ra 128.2 --dec 45.5 --output /path/to/output --search 0.3"
    exit 1
}

# Parse command line arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        --ra)
            RA="$2"
            shift 2
            ;;
        --dec)
            DEC="$2"
            shift 2
            ;;
        --output)
            OUTPUT_DIR="$2"
            shift 2
            ;;
        --search)
            SEARCH_DISTANCE="$2"
            shift 2
            ;;
        --help)
            usage
            ;;
        *)
            echo "Unknown option: $1"
            usage
            ;;
    esac
done

# Check required parameters
if [ -z "$RA" ] || [ -z "$DEC" ]; then
    echo "Error: RA and DEC values are required"
    usage
fi

# Create output directory if it doesn't exist
mkdir -p "$OUTPUT_DIR"

# Display initial settings
echo "Settings:"
echo "  Right Ascension: $RA"
echo "  Declination: $DEC"
echo "  Search Distance: $SEARCH_DISTANCE"
echo "  Output Directory: $OUTPUT_DIR"
echo

# Function to check if a number is within specified distance of target
check_within_range() {
    local value=$1
    local target=$2
    local distance=$3
    awk -v val="$value" -v target="$target" -v dist="$distance" 'BEGIN {
        diff = val - target
        if (diff < 0) diff = -diff
        if (diff <= dist) exit 0
        exit 1
    }'
}

# Count total number of .fit files
echo "Counting files..."
total_files=$(find . -name "*.fit" | wc -l)
echo "Found $total_files .fit files to process"

# Create a temporary file to store copy count
temp_count_file=$(mktemp)
echo "0" > "$temp_count_file"

# Process files and maintain accurate count
find . -name "*.fit" | while read -r file; do
    # Update and display progress
    printf "Processing: %s\r" "$(basename "$file")"
    
    # Get both RA and DEC with a single mdls call
    metadata=$(mdls -n "com_codeobsession_RightAscension" -n "com_codeobsession_Declination" "$file")
    file_ra=$(echo "$metadata" | grep "RightAscension" | awk '{print $3}')
    file_dec=$(echo "$metadata" | grep "Declination" | awk '{print $3}')
    
    # Check if both values are within range
    if check_within_range "$file_ra" "$RA" "$SEARCH_DISTANCE" && \
       check_within_range "$file_dec" "$DEC" "$SEARCH_DISTANCE"; then
        echo -e "\nMatched: $file"
        echo "RA: $file_ra (target: $RA)"
        echo "DEC: $file_dec (target: $DEC)"
        cp "$file" "$OUTPUT_DIR/"
        echo "Copied to $OUTPUT_DIR"
        echo "----------------------------------------"
        # Increment copy counter in temporary file
        curr_count=$(<"$temp_count_file")
        echo "$((curr_count + 1))" > "$temp_count_file"
    fi
done

# Get final copy count
copied=$(<"$temp_count_file")
rm "$temp_count_file"

echo -e "\nProcessing complete!"
echo "Processed $total_files files"
echo "Copied $copied matching files to $OUTPUT_DIR"
echo "Search distance used: $SEARCH_DISTANCE"

  • pfile likes this

#2 pfile

pfile

    Fly Me to the Moon

  • -----
  • Posts: 6,506
  • Joined: 14 Jun 2009

Posted 14 November 2024 - 12:12 AM

thanks for the script, i agree Observatory is indispensible, if only for its mdimporter plugin.


Edited by pfile, 14 November 2024 - 12:12 AM.


#3 lambermo

lambermo

    Apollo

  • -----
  • Posts: 1,338
  • Joined: 16 Jul 2007
  • Loc: .nl

Posted 14 November 2024 - 12:28 PM

what is mdls ? (which software package / github repo ?)



#4 pfile

pfile

    Fly Me to the Moon

  • -----
  • Posts: 6,506
  • Joined: 14 Jun 2009

Posted 14 November 2024 - 01:16 PM

what is mdls ? (which software package / github repo ?)

that's built into the os. it means "metadata list" in the way that "ls" on unix means "list"

 

it shows you the apple spotlight metadata attached to the file. in this case when the observatory pro importer scans a file (should happen when any file is created), it adds the RA and Dec found in the FITS header to the apple spotlight metadata. thus you can get it with mdls. of course if the file is not fits or xisf then the observatory pro importer won't be able to get the center coordinates. i don't know if observatory pro itself  can platesolve images and update the headers or apple metadata, maybe it can.

 

rob


Edited by pfile, 14 November 2024 - 01:19 PM.



CNers have asked about a donation box for Cloudy Nights over the years, so here you go. Donation is not required by any means, so please enjoy your stay.


Recent Topics





Also tagged with one or more of these keywords: Software



Cloudy Nights LLC
Cloudy Nights Sponsor: Astronomics