
static char acRcs[] =
    "$Header: srace.c,v 1.3 95/04/24 18:09:25 k2mm Exp $";

#include <stdio.h>

#define tPGM		"srace"
#define tLOGDIR		"rlogs"
#define nLINECHR	1000
#define nTESTMIN	120
#define nCALL		20
#define nDELMAX		15
#define nPLOTLINE	(2 * nDELMAX + 1)

typedef char*		TEXT;
typedef FILE*		pFILE;
typedef struct k *	pK;
typedef struct k
{   TEXT		tCall;
    int			anQso[ nTESTMIN + 1];
}   K;

int			iBegTestGmt;
int			iBegTestMin;
int			iBegPlotGmt;
int			iBegPlotMin;
int			iQsoTarg;
int			nErr, nQso, nLog;
int			anTarg[ nTESTMIN + 1];
K			ak[ nCALL];
char			aacPlot[ nPLOTLINE][ nTESTMIN + 1];

main( nArg, atArg)
int			nArg;
TEXT			atArg[];
{   int			iArg, iLog;

    if( nArg < 5)
	Usage();
    if( sscanf( atArg[ 1], "%d", &iBegTestGmt) != 1)
	Usage();
    if( sscanf( atArg[ 2], "%d", &iBegPlotGmt) != 1)
	Usage();
    if( sscanf( atArg[ 3], "%d", &iQsoTarg) != 1)
	Usage();
    Init();
    for( iArg = 4; iArg < nArg; iArg++)
	ReadLog( atArg[ iArg]);
    if( nErr)
	exit( nErr);
    for( iLog = 0; iLog < nLog; iLog++)
	SetPlot( iLog);
    PrintPlot();
}

Usage()
{
    fprintf( stderr,
	"usage: %s BegTestGmt BegPlotGmt QsoTarg Call [ Call ... ]\n", tPGM);
    exit( -1);
}

#define iGMTtoMIN( x)	(60 * ((int)(x) / 100) + ((int)(x) % 100))

Init()
{   double		dQsoPerMin;
    int			iLine, iMin;

    iBegTestMin = iGMTtoMIN( iBegTestGmt);
    iBegPlotMin = iGMTtoMIN( iBegPlotGmt) - iBegTestMin;
    dQsoPerMin = iQsoTarg / (double) nTESTMIN;
    for( iMin = 0; iMin <= nTESTMIN; iMin++)
	anTarg[ iMin] = 0.5 + iMin * dQsoPerMin;
    for( iLine = 0; iLine < nPLOTLINE; iLine++)
	for( iMin = 0; iMin <= nTESTMIN; iMin++)
	    if( iLine == nDELMAX)
		aacPlot[ iLine][ iMin] = iMin % 10 == 0 ? '+' : '-';
	    else
		aacPlot[ iLine][ iMin] = iMin % 10 == 0 ? '|' : ' ';
}

ReadLog( tCall)
TEXT			tCall;
{   pFILE		pfLog;
    int			iBand, iGmt, iMin, iMinLast;
    char		acLog[ nLINECHR], acLine[ nLINECHR];
    pK			pk;

    sprintf( acLog, "%s/%s", tLOGDIR, tCall);
    pfLog = fopen( acLog, "r");
    if( pfLog == 0)
    {   fprintf( stderr, "%s: %s: can't open %s\n", tPGM, tCall, acLog);
	nErr++;
	return;
    }
    pk = ak + nLog++;
    pk->tCall = tCall;
    iMinLast = nQso = 0;
    while( fgets( acLine, nLINECHR, pfLog))
    {   if( strstr( acLine, "-ok-") == 0)
	    continue;
	sscanf( acLine, "%d %d", &iBand, &iGmt);
	iMin = iGMTtoMIN( iGmt) - iBegTestMin;
	if( iMin < 0 || iMin > nTESTMIN)
	    continue;
	while( iMinLast < iMin)
	    pk->anQso[ iMinLast++] = nQso;
	pk->anQso[ iMin] = ++nQso;
    }
    while( iMinLast <= nTESTMIN)
	pk->anQso[ iMinLast++] = nQso;
    fclose( pfLog);
}

SetPlot( iLog)
int			iLog;
{   pK			pk;
    char		cLog, *pc;
    int			iMin, iDel;

    pk = ak + iLog;
    cLog = 'a' + iLog;
    for( iMin = 0; iMin <= nTESTMIN; iMin++)
    {   iDel = pk->anQso[ iMin] - anTarg[ iMin];
	if( iDel > nDELMAX)
	    iDel = nDELMAX;
	if( iDel < -nDELMAX)
	    iDel = -nDELMAX;
	pc = &aacPlot[ nDELMAX + iDel][ iMin];
	if( *pc == ' ' || *pc == '|' || *pc == '-' || *pc == '+')
	    *pc = cLog;
	else if( *pc >= 'a' && *pc <= 'z')
	    *pc = '2';
	else if( *pc >= '2' && *pc <= '8')
	    *pc += 1;
    }
}

PrintPlot()
{   int			iLine, iMin;

    putchar( '\n');
    for( iLine = nPLOTLINE - 1; iLine >= 0; iLine--)
    {   for( iMin = iBegPlotMin; iMin <= nTESTMIN; iMin++)
	    putchar( aacPlot[ iLine][ iMin]);
	putchar( '\n');
    }
}


