Skip to content
Snippets Groups Projects
post_process_aoss_windpro.sh 2.85 KiB
#!/usr/bin/env bash
# Quick hack script to get data from the windpro when its on the roof instead of in the SPARC

RAW_DIR=/mnt/inst-data/aoss-windpro
CACHE_DIR=/mnt/inst-data/cache/aoss/windpro

if [ $# -eq 0 ]; then
    NOW=`date -u +%Y%m%d`
else
    NOW=$1
fi

year=${NOW:0:4}
month=${NOW:4:2}
day=${NOW:6:2}

#echo $NOW
#echo $year $month $day

if [ $USER != "metobs" ]; then
    echo "ERROR: This script should only be run by the 'metobs' user"
    exit 2
fi

source "/opt/metobs/sparchive/scripts/sparchive_env.sh"

ENV_NAME=`basename ${SPARCHIVE_ENV}`

# Let the environment variable set the past days (default 3)
LOCK=${SPARCHIVE_ENV}/lock/$(basename $0).lock
logfile=${SPARCHIVE_LOG_DIR}/$(basename $0).log

log_info() {
    echo "INFO: $*" &>> $logfile
}

oops() {
    echo "ERROR: $*" &>> $logfile
    if [ -d $work_dir ]; then
        echo "INFO: Removing temporary work directory: $work_dir" &>> $logfile
        rm -rf $work_dir
    fi
    exit 1
}

make_dir() {
    dst_dir=$1
    if [ ! -d $dst_dir ]; then
        log_info "Creating data directory: $dst_dir"
        mkdir -p $dst_dir || oops "Couldn't create directory $dst_dir"
        chmod 755 $dst_dir || oops "Couldn't change permissions of directory $dst_dir"
        chown metobs:metobs $dst_dir || oops "Couldn't change ownership of directory $dst_dir"
    fi
}

(
    flock -x -n 200 || log_info "Script is already running, will not run again."

    log_info "Running archive_sparc.sh for experiment ${EXP_NAME} with past days ${SPARCHIVE_PAST_DAYS}"

    raw_dir=$RAW_DIR/$year/$month/$day
    if [ ! -d $raw_dir ]; then
        oops "Input data directory does not exist: $raw_dir"
    fi

    # Make the year
    dst_dir=$CACHE_DIR/profiles
    make_dir $dst_dir
    dst_dir=$dst_dir/$year
    make_dir $dst_dir
    dst_dir=$dst_dir/$month
    make_dir $dst_dir
    dst_dir=$dst_dir/$day
    make_dir $dst_dir

    # Create a working directory so that the davidh user (through sudo) can write some files
    work_dir=`mktemp -d ${SPARCHIVE_TMP_ROOT}/aoss_windpro_profiles_${NOW}_XXXXXX` || oops "Failed to create temporary work directory"
    chmod 777 $work_dir || oops "Failed to change temporary work directory permissions"
    cd $work_dir || oops "Couldn't change to temporary directory"
    log_info "Created temporary working directory: $work_dir"


    log_info "Creating WindPro profile files from data on ${NOW}"
    sudo -u davidh ${SPARCHIVE_SCRIPTS_ROOT}/run_windpro_process_profile.sh ${raw_dir} ${work_dir} &>> $logfile || oops "Could not make windpro profile files"

    log_info "Moving files from work directory to final output directory"
    mv $work_dir/* $dst_dir/ || oops "Failed to move profile results to output directory: $dst_dir"

    log_info "Removing temporary work directory: $work_dir"
    rm -rf ${work_dir} || oops "Failed to remove the work directory: $work_dir"

    log_info "Done for $NOW"
) 200>$LOCK