/***********************************************************************
B P A M E R I C A
PROPRIETARY - TO BE MAINTAINED IN CONFIDENCE
COPYRIGHTED 2006
***********************************************************************
TEMPLATE 1
"C" template demonstrating the use of the base dds routines
for a simple trace-to-trace processing scheme.
Written by Jerry Ehlers November 2006
**********************************************************************/
#define _POSIX_SOURCE 1 /* Check POSIX Standard */
#define ANSI /* Turn on prototyping from cdds.h */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <cdds.h> /* "C" dds include */
#define RCSID "$Id: c_template1.html 1 2009-01-06 00:31:13Z ehlersjw $"
#define TITLE "template1: C template using DDS base routines"
void doit(int, int, int, int, int, int, int, float, float*, float*);
void help();
/***********************************************************************
*
* main
*
**********************************************************************/
int main(int argc, char **argv)
{
BIN_TAG in_bin=-1, in_buf_bin=-1, out_buf_bin=-1, out_bin=-1;
const char *in_dict, *in_buf_dict, *out_buf_dict, *out_dict;
int ier, ns, nout, tag, ndxsmp, nbytes;
float scale, *inbuf, *outbuf;
char *prog;
/***********************************************************************
* initialize
**********************************************************************/
/*
* get the program name
*/
prog = strrchr(argv[0], '/');
if (prog) prog++;
else prog = argv[0];
/*
* pass the command line arguments on to DDS
*/
setargcv(argc, argv);
ier = cdds_openpr(prog, RCSID);
if (ier > 0) help();
/***********************************************************************
* open input data file
**********************************************************************/
in_dict = cdds_in("in", "stdin:", TITLE);
in_bin = cdds_open(in_dict, "in_format", "in_data", "r");
if (in_bin < 0) {
cdds_prterr("Unable to open input data\n");
}
/*
* Get input data parameters
*/
cdds_scanf("size.axis(1)", "%d", &ns);
/***********************************************************************
* open input data buffer
**********************************************************************/
/*
* Define the input buffer Samples as host-dependent float
*/
cdds_dict("override:", "print");
cdds_printf("fmt:*:ibuf.SAMPLE_TYPE", "\n");
cdds_printf(" ", " typedef float SAMPLE_TYPE;\n");
in_buf_dict = cdds_out("ibuf", " ", in_dict);
cdds_printf("$old_format", "format\n");
cdds_printf("ibuf_fmt", "asp\n");
in_buf_bin = cdds_open(in_buf_dict, "ibuf_fmt", " ", "m");
if (in_buf_bin < 0) {
cdds_prterr("Unable to open internal input buffer!\n");
}
cdds_openm(in_buf_bin, 0, in_bin, 0);
/***********************************************************************
* read parameters
**********************************************************************/
cdds_dict("par:", "scan");
/*
* get scale parameter (default 1.0)
*/
scale = 1.0;
cdds_scanf("scale", "%f", &scale);
/*
* get # samples out (default ns)
*/
nout = ns;
cdds_scanf("nout", "%d", &nout);
/***********************************************************************
* print user parameters
**********************************************************************/
cdds_prtmsg("\n*** USER PARAMETERS ***\n\n");
cdds_prtmsg("\tscale = %g\n", scale);
cdds_prtmsg("\tnout = %d\n", nout);
cdds_prtmsg("\n");
/*
* Check parameters
*/
if (scale < 0.0) {
cdds_prtcon("WARNING: \"scale\" is negative!\n");
}
/***********************************************************************
* allocate dynamic arrays
**********************************************************************/
if (cdds_errors()) goto finish;
/*
* Allocate memory for an entire input record & single output trace
*/
nbytes = cdds_prec(in_buf_bin, 0);
inbuf = cdds_malloc(nbytes);
nbytes = cdds_prec(out_buf_bin, 0);
outbuf = cdds_malloc(nbytes);
/*
* get index to samples (allowing for possible trace headers)
* (since we are not modifying any trace headers in this program,
* we can use the same index for both input and output buffers.)
*/
tag = cdds_member(in_buf_bin, 0, "Samples");
ndxsmp = cdds_index(in_buf_bin, tag, DDS_FLOAT);
/***********************************************************************
* open internal output buffer
**********************************************************************/
if (cdds_errors()) goto finish;
cdds_dict("override:", "print");
cdds_printf("fmt:*:obuf.SAMPLE_TYPE", "\n");
cdds_printf(" ", " typedef complex SAMPLE_TYPE;\n");
/*
* open output buffer dictionary
*/
out_buf_dict = cdds_out("obuf", " ", in_buf_dict);
/*
* print any definitions that might have changed from the input
*/
cdds_printf("size.axis(1)", "%d\n", nout);
/*
* open output buffer binary (using "asp" format)
*/
cdds_printf("obuf_fmt", "asp\n");
out_buf_bin = cdds_open(out_buf_dict, "obuf_fmt", " ", "m");
if (out_buf_bin < 0) {
cdds_prterr("Unable to open output buffer!\n");
}
/***********************************************************************
* open output data
**********************************************************************/
/*
* open the output dictionary from the internal output buffer
*/
out_dict = cdds_out("out", "stdout:", out_buf_dict);
/*
* open the output binary
*/
out_bin = cdds_open(out_dict, "out_format", "out_data", "w+");
if (out_bin < 0) {
cdds_prterr("Unable to open output file!\n");
}
/*
* explicitly open the mapping from the internal output buffer to
* the output dataset
*/
cdds_openm(out_bin, 0, out_buf_bin, 0);
/***********************************************************************
* process the data
**********************************************************************/
if (cdds_errors()) goto finish;
doit(in_bin, in_buf_bin, out_buf_bin, out_bin, ns, nout, ndxsmp, scale,
inbuf, outbuf);
/***********************************************************************
* close files, clean-up, & exit
**********************************************************************/
finish:
cdds_close(in_bin);
cdds_close(in_buf_bin);
cdds_close(out_buf_bin);
cdds_close(out_bin);
cdds_closepr();
}
/***********************************************************************
*
* doit
*
**********************************************************************/
void doit(int in_bin, int in_buf_bin, int out_buf_bin, int out_bin,
int ns, int nout, int ndxsmp, float scale, float* inbuf,
float* outbuf)
{
int ier, i;
/*
* loop over each trace
*/
ier = cdds_readm(in_bin, 0, in_buf_bin, 0, inbuf, 1);
while(ier == 1) {
/*
* save trace headers to output buffer
*/
for(i=0;i<ndxsmp-1;i++) outbuf[i] = inbuf[i];
/*
* process the trace Samples
*/
process(ns, nout, scale, &inbuf[ndxsmp], &outbuf[ndxsmp]);
/*
* write the trace
*/
ier = cdds_writem(out_bin, 0, out_buf_bin, 0, outbuf, 1);
if (ier != 1) {
cdds_prterr("writing output\n");
return;
}
/*
* read the next trace
*/
ier = cdds_readm(in_bin, 0, in_buf_bin, 0, inbuf, 1);
}
return;
}
/***********************************************************************
*
* help
*
**********************************************************************/
void help()
{
fprintf(stderr, "Template program demonstrating the use of the base\n");
fprintf(stderr, "DDS routines for a simple trace-to-trace processing\n");
fprintf(stderr, "scheme.\n");
fprintf(stderr, "\n");
fprintf(stderr, "usage:\n");
fprintf(stderr, " f_template [in=dat] [in_data=bin] [in_format=fmt] \\\n");
fprintf(stderr, " [out=dat] [out_data=bin] [out_format=fmt] \\\n");
fprintf(stderr, " [nout=n] [scale=f]\n");
fprintf(stderr, " \n");
fprintf(stderr, "where:\n");
fprintf(stderr, " in= input dataset\n");
fprintf(stderr, " in_data= input binary\n");
fprintf(stderr, " in_format= input format\n");
fprintf(stderr, " out= output dictionary\n");
fprintf(stderr, " out_data= output binary\n");
fprintf(stderr, " out_format=output format\n");
fprintf(stderr, " nout= output samples (dflt=input)\n");
fprintf(stderr, " scale= scale factor (dflt=1.0)\n");
fprintf(stderr, "\n");
exit(0);
}