#!/bin/bash
#
# To cron NOAA-20 EARS files we can assume a maximum timeliness of 1800 seconds.
# This is just to check whether EUMETSAT actually manages to do what they claim.
# Oase, 2022/12/06                                                   E. Lobsiger
#

# File Pattern : SVMC_{platform_shortname}_d{start_time:%Y%m%d_t%H%M%S%f}_e{end_time:%H%M%S%f}_b{orbit:5d}_c{creation_time:%Y%m%d%H%M%S%f}_eum_ops.h5
# Example name : SVMC_j01_d20190411_t0224358_e0226003_b07218_c20190411023355000121_eum_ops.h5
# Hugo's ruler : 012345678901234567890123456789012345678901234567890123456789012345678901234567890
# copied EMCV  : 0         1         2         3         4         5         6         7         8
# File Pattern : SVDNBC_{platform_shortname}_d{start_time:%Y%m%d_t%H%M%S%f}_e{end_time:%H%M%S%f}_b{orbit:5d}_c{creation_time:%Y%m%d%H%M%S%f}_eum_ops.h5
# Example name : SVDNBC_j01_d20190411_t0424054_e0425281_b07219_c20190411043053000049_eum_ops.h5
# Hugo's ruler : 012345678901234567890123456789012345678901234567890123456789012345678901234567890
# copied EMCV  : 0         1         2         3         4         5         6         7         8

maxt=0
mint=100000
minp=0
maxp=30
sat="NOAA-20"
chann="E1B-RDS-2"
color="violet"
here=`pwd`
dat1=`date -u -d "-5 days" +%Y/%m/%d`
dat2=`date -u -d "-1 days" +%Y/%m/%d`
rm -f $here/timeliness.txt
# On BASIC Service channel E1B-RDS-2
for n in -5 -4 -3 -2 -1
do
    thisday=`date -u -d "$n days" +%Y/%m/%d`
    cd "/srv/$chann/$thisday"
    # This is for satellite NOAA-20 (M-Bands only)
    for name in `ls -rt SVMC_j01_d*.h5`
    do
        yea=${name:10:4}
        mon=${name:14:2}
        day=${name:16:2}
        hou=${name:20:2}
        min=${name:22:2}
        start=`date -d "$yea-$mon-$day $hou:$min" "+%s" `
        trans=`stat -c "%Y" $name`
        # You should run your receiver/processor under UTC!
        # trans=`date -u -d "1970-01-01 $trans seconds" "+%s"`
        timeliness=$(((trans - start)/60))
        if (( timeliness > maxt )); then maxt=$timeliness; fi
        if (( timeliness < mint )); then mint=$timeliness; fi
        echo $timeliness >> $here/timeliness.txt
    done
done
cd $here
gnuplot -e "sat='$sat'; dat1='$dat1'; dat2='$dat2'; mint=$mint; maxt=$maxt;\
            minp=$minp; maxp=$maxp; chann='$chann'; color='$color'" LEO-SATS.plt

