#!/usr/bin/env bash

# Description: Create Level b1 netcdf4 files and the corresponding quicklooks
SCRIPT_HOME="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SCRIPT_NAME=$(basename $0)
SCRIPT_NAME=${SCRIPT_NAME/.sh/}

# Get environment variables and common functions
source $SCRIPT_HOME/metobs_config.sh

DATE=$1
if [ -z "$DATE" ]; then
   DATE=`date +%Y%m%d`
fi
LOCK="${ENV}/locks/${SCRIPT_NAME}.lock"
logfile="${LOGDIR}/${SCRIPT_NAME}.log"

if [ ! -d $LOGDIR ]; then
    oops "Log directory doesn't exist: $LOGDIR"
    exit 1
fi

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

    if [ ! -d $TOWER_CACHE_DIR ]; then
        oops "Tower cache directory doesn't exist: $TOWER_CACHE_DIR"
    fi

    log_info "$(date +%Y-%m-%dT%H:%M:%S): Running level b1 jobs for ${DATE}" >> $logfile

    tmp_dir=`work_dir "$DATE"`

    ### NetCDF Generation ###
    # properly generating a netcdf file for day X at least requires the data for day X-1 and X
    previous_date=`day_before "$DATE"`
    prev_file=`cache_level_00_file "$previous_date"`
    curr_file=`cache_level_00_file "$DATE"`
    out_file=`cache_level_b1_file "$DATE"`
    out_fn=`basename "$out_file"`
    tmp_out="$tmp_dir/$out_fn"
    log_info "Generating NetCDF files..."
    $ENV/bin/python -m aosstower.level_b1.nc -l "$logfile" -vv -z -i "$prev_file" "$curr_file" -s "${DATE}" -o "$tmp_out" >> $logfile
    nc_status=$?
    if [ $nc_status -ne 0 ]; then
        oops "NetCDF generation failed for $DATE"
    fi

    log_info "Moving NetCDF file from temp directory to cache"
    $ENV/bin/python -m metobscommon.archive.incoming -vv -l $logfile --dates=${DATE} b1 aoss.tower "$tmp_out" || oops "NetCDF archive failed"

    ### Quicklook Generation ###
    # assumes that out_file is what the archive script wrote the file as
    log_info "Generating Quicklook images..."
    $ENV/bin/python -m aosstower.level_b1.quicklook -l "$logfile" -vv --thumbnail -s "$DATE" "$out_file" -o "$tmp_dir/aoss_tower.{plot_name}.{start_time:%Y-%m-%d}.png" -p meteorogram td pressure wind_speed wind_dir accum_precip solar_flux
    quicklook_status=$?
    if [ $quicklook_status -ne 0 ]; then
        oops "Quicklook generation failed for $DATE"
    fi

    log_info "Moving Level B1 quicklooks from temp directory to cache"
    $ENV/bin/python -m metobscommon.archive.incoming -vv -l $logfile --dates=${DATE} b1 aoss.tower "$tmp_dir/aoss_tower.*.png" || oops "Quicklook archive failed"

    # Delete the working directory
    rm -rf $tmp_dir || oops "Could not delete working directory: $tmp_dir"

    log_info "Done"

) 200>$LOCK
