view autocal/txcalconf.c @ 131:8e26cb57c7f8

doc/Test-system-interface: additions for Tx ramp testing
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 20 Dec 2021 03:29:23 +0000
parents 4c3f4231a021
children
line wrap: on
line source

/*
 * The code that reads and parses Tx calibration profiles lives here.
 */

#include <sys/param.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <rvinterf/exitcodes.h>
#include "txvout.h"
#include "txband.h"

char txlevels_directory[] = "/opt/freecalypso/rfcal/txlevels";

extern struct txcal_band *txcal_band;
extern char *txlevels_profile;

extern struct tx_basis_point tx_basis[MAX_BASIS_POINTS];
extern unsigned num_basis_points;

extern struct tx_level tx_levels[MAX_TX_LEVELS];

static basis_set, targets_set;

static void
do_basis_line(cp, filename_for_errs, lineno)
	char *cp, *filename_for_errs;
{
	unsigned n;

	for (n = 0; ; n++) {
		while (isspace(*cp))
			cp++;
		if (*cp == '\0' || *cp == '#')
			break;
		if (!isdigit(*cp)) {
			fprintf(stderr,
				"%s line %d: non-numeric content not allowed\n",
				filename_for_errs, lineno);
			exit(ERROR_USAGE);
		}
		if (n >= MAX_BASIS_POINTS) {
			fprintf(stderr,
				"%s line %d: MAX_BASIS_POINTS exceeded\n",
				filename_for_errs, lineno);
			exit(ERROR_USAGE);
		}
		tx_basis[n].apc = atoi(cp);
		if (n && tx_basis[n].apc <= tx_basis[n-1].apc) {
			fprintf(stderr,
				"%s line %d: basis points must be increasing\n",
				filename_for_errs, lineno);
			exit(ERROR_USAGE);
		}
		while (isdigit(*cp))
			cp++;
	}
	if (n < 2) {
		fprintf(stderr,
		"%s line %d: a minimum of 2 basis points must be given\n",
			filename_for_errs, lineno);
		exit(ERROR_USAGE);
	}
	num_basis_points = n;
	basis_set = 1;
}

static void
do_targets_line(cp, filename_for_errs, lineno)
	char *cp, *filename_for_errs;
{
	unsigned expect_num, n;

	expect_num = txcal_band->end_plnum - txcal_band->start_plnum + 1;
	for (n = 0; ; n++) {
		while (isspace(*cp))
			cp++;
		if (*cp == '\0' || *cp == '#')
			break;
		if (!isdigit(*cp)) {
			fprintf(stderr,
				"%s line %d: non-numeric content not allowed\n",
				filename_for_errs, lineno);
			exit(ERROR_USAGE);
		}
		if (n >= expect_num) {
wrong_num:		fprintf(stderr,
			"%s line %d: exactly %u target numbers expected\n",
				filename_for_errs, lineno, expect_num);
			exit(ERROR_USAGE);
		}
		tx_levels[n].target = atof(cp);
		while (isdigit(*cp) || *cp == '.')
			cp++;
	}
	if (n != expect_num)
		goto wrong_num;
	targets_set = 1;
}

static void
process_line(linebuf, filename_for_errs, lineno)
	char *linebuf, *filename_for_errs;
{
	char *cp, *np;

	for (cp = linebuf; isspace(*cp); cp++)
		;
	if (*cp == '\0' || *cp == '#')
		return;
	for (np = cp; *cp && !isspace(*cp); cp++)
		;
	if (*cp)
		*cp++ = '\0';
	if (!strcmp(np, "basis"))
		do_basis_line(cp, filename_for_errs, lineno);
	else if (!strcmp(np, "targets"))
		do_targets_line(cp, filename_for_errs, lineno);
	else {
		fprintf(stderr, "%s line %d: unknown keyword \"%s\"\n",
			filename_for_errs, lineno, np);
		exit(ERROR_USAGE);
	}
}

read_tx_cal_profile()
{
	char pathname[MAXPATHLEN];
	FILE *inf;
	int lineno;
	char linebuf[512];

	sprintf(pathname, "%s/%s-%s", txlevels_directory, txlevels_profile,
		txcal_band->name);
	inf = fopen(pathname, "r");
	if (!inf) {
		perror(pathname);
		exit(ERROR_USAGE);
	}
	for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++)
		process_line(linebuf, pathname, lineno);
	fclose(inf);
	if (!basis_set) {
		fprintf(stderr, "error: basis not set in %s\n", pathname);
		exit(ERROR_USAGE);
	}
	if (!targets_set) {
		fprintf(stderr, "error: targets not set in %s\n", pathname);
		exit(ERROR_USAGE);
	}
}