#!/bin/bash
# Runs at Midnight CST/CDT
DD=$(date -u +'%d')
MM=$(date -u +'%m')
YYYY=$(date -u +'%Y')

# Need this done so that certain variables are there for DEP
cd summary || exit 1
python compute_daily.py --date="$(date --date '1 day ago' +'%Y-%m-%d')"

cd ../other || exit 1
python update_daily_srad.py --date="$(date --date '1 day ago' +'%Y-%m-%d')"

# Need this done so that IEMRE daily grids are there for DEP
cd ../iemre || exit 1
python daily_analysis.py --date="$(date --date '1 day ago' +'%Y-%m-%d')"
python daily_analysis.py --date="$(date --date '1 day ago' +'%Y-%m-%d')" --domain=sa
# Update some forecasts, also runs from RUN_0Z.sh is doing
python daily_analysis.py --date="$(date --date '2 days' +'%Y-%m-%d')" --forecast --domain=conus
python daily_analysis.py --date="$(date --date '2 days' +'%Y-%m-%d')" --forecast --domain=sa
python daily_analysis.py --date="$(date --date '2 days' +'%Y-%m-%d')" --forecast --domain=europe
python daily_analysis.py --date="$(date --date '3 days' +'%Y-%m-%d')" --forecast --domain=china

cd ../asos || exit 1
python adjust_report_type.py --date="$(date -u --date '1 day ago' +'%Y-%m-%d')"

cd ../smos || exit 1
python plot.py --valid="$(date --date '1 day ago' +'%Y-%m-%d')T12:00:00" --realtime

# Wait a bit before doing this
sleep 600
cd ../qc || exit 1
python check_station_geom.py
python check_vtec_eventids.py
python check_afos.py --date="$(date --date '1 day ago' +'%Y-%m-%d')"

cd ../iemre || exit 1
python grid_rsds.py --date="$(date --date '1 day ago' +'%Y-%m-%d')"

cd ../dbutil || exit 1
python set_attribute_phour.py

cd ../hads || exit 1
python dedup_hml_forecasts.py --date="$(date --date '1 day ago' +'%Y-%m-%d')"
python raw2obs.py --date="$(date --date '1 day ago' +'%Y-%m-%d')"

cd ../mrms || exit 1
python mrms_monthly_plot.py

# Assume we have MERRA data by the 28th each month
if [ "$DD" -eq "28" ]
then
    cd ../dl || exit 1
    python fetch_merra.py
    MM=$(date -u --date '1 month ago' +'%m')
    YYYY=$(date -u --date '1 month ago' +'%Y')
    cd ../climodat || exit 1
    python merra_solarrad.py --year="$YYYY" --month="$MM"
fi
