#!/bin/bash
#
# To cron Suomi-NPP 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_npp_d20190326_t0147221_e0148463_b38388_c20190326015546000177_eum_ops.h5
# Hugo's ruler : 0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012
# copied EMCV  : 0         1         2         3         4         5         6         7         8         9         10

mint=100000
maxt=0
minp=0
maxp=30
sat="Suomi-NPP"
chann="E1H-RDS-1"
color="dark-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 HVS-1 Service channel E1H-RDS-1
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 Suomi-NPP (M-Bands only)
    for name in `ls -rt SVMC_npp_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
