This example is the source code for the included eaarlio_yaml program, which outputs EAARL raster data in YAML format. It provides a more comprehensive and real-world example of how to use the library than the simpler examples.
#include <assert.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "argtable3.h"
void yaml_wf(FILE *out,
unsigned char *wf,
uint16_t wf_len)
{
uint16_t i = 0;
if(!wf || wf_len < 1) {
fprintf(out, "[]");
return;
}
fprintf(out, "[");
for(i = 0; i < wf_len; i++) {
if(i % 12 == 0)
fprintf(out, "\n ");
fprintf(out, " %3d", wf[i]);
if(i + 1 < wf_len)
fprintf(out, ",");
}
fprintf(out, "\n ]\n");
}
void yaml_pulse(FILE *out,
uint16_t pulse_number,
int include_waveforms)
{
assert(out);
assert(raster);
int i = 0;
fprintf(out, " - pulse_number: %" PRIu16 "\n", pulse_number);
fprintf(out, " time: %.6f\n",
fprintf(
fprintf(out,
" time_offset: %" PRIu32
"\n", pulse->
time_offset);
fprintf(out, " scan_angle_counts: %" PRIi16 "\n",
fprintf(out,
" range: %" PRIu16
"\n", pulse->
range);
fprintf(out,
" rx_count: %" PRIu8
"\n", pulse->
rx_count);
fprintf(out,
" thresh_tx: %" PRIu8
"\n", pulse->
thresh_tx);
fprintf(out,
" thresh_rx: %" PRIu8
"\n", pulse->
thresh_rx);
fprintf(out,
" bias_tx: %" PRIu8
"\n", pulse->
bias_tx);
fprintf(out,
" bias_rx: [%" PRIu8 ", %" PRIu8 ", %" PRIu8 ", %" PRIu8 "]\n",
if(!include_waveforms)
return;
fprintf(out, " tx: ");
fprintf(out, " rx: []\n");
} else {
fprintf(out, " rx:\n");
for(i = 0; i < channels; i++) {
fprintf(out, " - ");
yaml_wf(out, pulse->
rx[i], pulse->
rx_len[i]);
}
}
}
void yaml_raster(FILE *out,
int raster_number,
int include_pulses,
int include_waveforms)
{
assert(out);
assert(raster);
uint16_t pulse_number;
fprintf(out, "- raster_number: %d\n", raster_number);
fprintf(out,
" edb_time_offset: %" PRIi32
"\n",
time_offset);
fprintf(out,
" time_seconds: %" PRIu32
"\n", raster->
time_seconds);
fprintf(out,
" time_fraction: %" PRIu32
"\n", raster->
time_fraction);
fprintf(out,
" digitizer: %" PRIu8
"\n", raster->
digitizer);
fprintf(out,
" pulse_count: %" PRIu16
"\n", raster->
pulse_count);
if(!include_pulses)
return;
fprintf(out, " pulses: []\n");
} else {
fprintf(out, " pulses:\n");
for(pulse_number = 1; pulse_number <= raster->
pulse_count;
pulse_number++) {
if(pulse_number > 1)
fprintf(out, "\n");
yaml_pulse(out, raster, pulse_number, include_waveforms);
}
}
}
int *raster_numbers,
int raster_numbers_count)
{
assert(edb);
assert(raster_numbers);
int src = 0;
int dst = 0;
int raster_number;
int file_index;
for(src = 0; src < raster_numbers_count; src++) {
raster_number = raster_numbers[src];
if(raster_number < 1) {
fprintf(stderr, "WARNING: rasters must be >= 1, skipping: %d\n",
raster_number);
continue;
}
fprintf(stderr,
"WARNING: raster number beyond end of EDB, skipping: %d\n",
raster_number);
continue;
}
if(file_index < 1) {
fprintf(stderr,
"WARNING: EDB for raster %d references negative file_index of "
"%d\n",
raster_number, file_index);
continue;
}
fprintf(stderr,
"WARNING: EDB for raster %d references file_index of %d, max "
"is %d\n",
continue;
}
raster_numbers[dst] = raster_number;
dst++;
}
return dst;
}
int eaarlio_yaml(
const char *out_file,
const char *edb_file,
const char *tld_path,
int *raster_numbers,
int raster_numbers_count,
int include_pulses,
int include_waveforms)
{
assert(edb_file);
assert(raster_numbers);
int failed = 0;
int32_t time_offset = 0;
int raster_number = 0;
int i = 0;
FILE *out = stdout;
if(out_file) {
out = fopen(out_file, "w");
if(!out) {
fprintf(
stderr, "ERROR: Unable to open output file: %s\n", out_file);
failed = 1;
goto exit;
}
}
if(failed)
goto exit;
raster_numbers_count =
precheck_rasters(&flight.
edb, raster_numbers, raster_numbers_count);
if(raster_numbers_count < 1) {
fprintf(stderr, "WARNING: no valid rasters were detected\n");
goto exit;
}
fprintf(out, "---\n");
for(i = 0; i < raster_numbers_count; i++) {
if(i > 0)
fprintf(out, "\n");
raster_number = raster_numbers[i];
raster_number, include_pulses, include_waveforms);
err, "ERROR: Problem reading raster %d", raster_number);
if(failed)
goto exit;
yaml_raster(out, raster_number, &raster, time_offset, include_pulses,
include_waveforms);
err, "ERROR: Problem releasing memory for raster");
if(failed)
goto exit;
}
fprintf(out, "...\n");
if(out_file) {
failed = fclose(out);
out = NULL;
if(failed) {
fprintf(
stderr, "ERROR: Unable to close output file: %s\n", out_file);
goto exit;
}
}
err, "ERROR: Problem releasing resouces for the flight");
if(failed)
goto exit;
exit:
if(out && out_file) {
fclose(out);
}
return failed;
}
int main(int argc, char *argv[])
{
int failed = 0, nerrors = 0;
char progname[] = "eaarlio_yaml";
char *tld_path = NULL;
int tld_path_free = 0;
struct arg_lit *help, *version, *nopulse, *nowf;
struct arg_file *outfile, *edb, *tld;
struct arg_int *rasters;
struct arg_end *end;
void *argtable[] = {
help = arg_litn("h", "help", 0, 1, "display this help and exit"),
version =
arg_litn("V", "version", 0, 1, "display library version and exit"),
outfile = arg_filen("o", "output", "<output.yaml>", 0, 1,
"output file to create instead of writing to stdout"),
nopulse = arg_litn(
"P", "no-pulses", 0, 1, "do not include pulse data in the output"),
nowf = arg_litn("W", "no-waveforms", 0, 1,
"do not include waveform data in the output"),
tld = arg_file0("t", "tld", "<tld path>", "path to the TLD files"),
edb = arg_filen(NULL, NULL, "<edb file>", 1, 1, "EDB file for dataset"),
rasters = arg_intn(
NULL, NULL, "<raster number>", 1, 1000, "raster numbers to export"),
end = arg_end(20),
};
if(arg_nullcheck(argtable) != 0) {
printf("error: insufficient memory\n");
failed = 1;
goto exit;
}
outfile->filename[0] = NULL;
nerrors = arg_parse(argc, argv, argtable);
if(version->count > 0) {
failed = 0;
goto exit;
}
if(help->count > 0) {
printf("Usage: %s", progname);
arg_print_syntax(stdout, argtable, "\n");
printf("Export rasters in YAML format.\n\n");
arg_print_glossary(stdout, argtable, " %-25s %s\n");
printf(
"\n"
"Exports the rasters in YAML format as requested...\n");
failed = 0;
goto exit;
}
if(nerrors > 0) {
arg_print_errors(stderr, end, progname);
fprintf(stderr, "Try '%s --help' for more information.\n", progname);
failed = 1;
goto exit;
}
assert(strlen(edb->filename[0]) > 0);
if(tld->count > 0) {
tld_path = (char *)tld->filename[0];
} else if(strlen(edb->basename[0]) < strlen(edb->filename[0])) {
tld_path_free = 1;
tld_path = calloc(strlen(edb->filename[0] + 1), sizeof(char));
if(!tld_path) {
fprintf(stderr, "Memory allocation failure\n");
failed = 1;
goto exit;
}
strcpy(tld_path, edb->filename[0]);
tld_path[strlen(edb->filename[0]) - strlen(edb->basename[0])] = 0;
} else {
tld_path = ".";
}
failed = eaarlio_yaml(outfile->filename[0], edb->filename[0], tld_path,
rasters->ival, rasters->count, nopulse->count == 0, nowf->count == 0);
exit:
arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
if(tld_path_free && tld_path)
free(tld_path);
return failed;
}