/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ /* Populate_Filter.pc */ /* Load to the NCEDC database the filter info. */ /* Errors can be found on Error.log */ /* */ /* Zuzlewski Stephane @1998 */ /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ #include #include #include EXEC SQL INCLUDE sqlca.h; typedef char asciz[20]; typedef char vc2_arr[11]; EXEC SQL BEGIN DECLARE SECTION; /* User-defined type for null-terminated strings */ EXEC SQL TYPE asciz IS STRING(20) REFERENCE; /* User-defined type for a VARCHAR array element */ EXEC SQL TYPE vc2_arr IS VARCHAR2(11) REFERENCE; asciz username; asciz password; char user_pwd[80]; int i; int j; int k; int idtmp; char sfid[32]; /* Sequence of filters identification */ int nb_stage; /* Number of stages */ float sgain; /* Sequence of filters gain */ float sfreq; /* Sequence of filters frequency */ float gain; /* Filter gain */ float freq; /* Filter frequency */ float insamp; /* Input sample rate */ float outsamp; /* Output sample rate */ int offset; /* Decimation offset */ float delay; /* Estimated delay */ float correction; /* Correction applied */ int nb_line; /* Number of response lines */ char ftype[32]; /* Filter type */ int nb_pole; /* Number of poles */ float corner; /* Corner frequency */ float damping; /* Damping value */ float r_value; /* Real part of a complex pole/zero */ float i_value; /* Imaginary part of a complex pole/zero */ int Flag_PZ; /* Flag for poles & zeros */ char ptype[32]; /* Polynomial type */ float lower; /* Polynomial lower bound */ float upper; /* Polynomial upper bound */ float maxerror; /* Maximum error of polynomial approximation */ int nb_coeff; /* Number of coefficients */ float pncoeff; /* Polynomial coefficients */ int respnb; /* Response number */ char firname[32]; /* FIR filter name */ float firgain; /* FIR filter gain */ int firid; /* FIR filter identifier */ char symmetry[5]; /* FIR filter symmetry */ char symm; /* FIR filter symmetry */ float coeff_value; /* FIR filter coefficients value */ int coeff_nb; /* FIR filter coefficients number */ int unitin; /* Input units */ int unitout; /* Output units */ EXEC SQL END DECLARE SECTION; long SQLCODE; FILE* f_err; /* Error File descriptor */ void sql_error(); /* handles unrecoverable errors */ /*-*-*-*-*-*-*-*-*/ /* Main function */ /*-*-*-*-*-*-*-*-*/ main (argc, argv) int argc; char* argv[]; { FILE* f_filter; /* Filter file descriptor */ char line[1025]; /* Line information */ char header[7]; /* Header */ float pn_coeff[21]; /* Polynomial coefficients */ /* Opening the error file */ if ((f_err = fopen ("Error.log", "w+t")) == NULL) { printf ("\n Error [File (Error.log) could not be opened].\n\n"); exit (0); } /* Opening the filter file */ if ((f_filter = fopen ("filter.tbl", "rt")) == NULL) { printf ("\n Error [File (filter.tbl) could not be opened].\n\n"); exit (0); } /* Connect to ORACLE. */ EXEC SQL WHENEVER SQLERROR DO sql_error(); strcpy (username, "ncedcdba"); strcpy (password, "passwd"); strcpy (user_pwd, "ncedcdba/passwd@ncedc"); EXEC SQL CONNECT :user_pwd; printf ("\n Connected to ORACLE as user: %s\n", username); /* Parsing filter file */ fgets (line, 1024, f_filter); while (!feof (f_filter)) { strcpy (header, ""); sscanf (line, "%s", header); if (!strcmp (header, "FIRFS")) { /* Reading filters chain information */ sscanf (line, "%*s %s %d", sfid, &nb_stage); printf ("\n Filter chain : %s\t%d", sfid, nb_stage); /* Inserting filter chain information into the database */ EXEC SQL INSERT INTO Filter_Sequence (seqfil_id, name, nb_filter, gain, frequency, lddate) VALUES (seqfilseq.NEXTVAL, :sfid, :nb_stage, NULL, NULL, SYSDATE); for (i=1;i<=nb_stage;i++) { Flag_PZ = 0; respnb = 0; fgets (line, 1024, f_filter); /* Reading filter information */ sscanf (line, "%*s %f %f %*s %*s %d", &gain, &freq, &nb_line); printf ("\n\t Filter : %f\t%f\t%d", gain, freq, nb_line); /* Searching units information */ unitin = 0; unitout = 0; EXEC SQL SELECT id INTO :unitout FROM D_Unit WHERE name = 'COUNTS'; if (unitout == 0) { EXEC SQL SELECT uniseq.NEXTVAL INTO :unitout FROM DUAL; EXEC SQL INSERT INTO D_Unit (id, name, description) VALUES (:unitout, 'COUNTS', NULL); } unitin = unitout; for (j=1;j<=nb_line;j++) { fgets (line, 1024, f_filter); /* Reading response type */ strcpy (header, ""); sscanf (line, "%s", header); if (!strcmp (header, "INPUT")) { /* Reading input information */ sscanf (line, "%*s %f %*s %f %d", &insamp, &outsamp, &offset); printf ("\n\t\t Input : %f\t%f\t%d", insamp, outsamp, offset); } else if (!strcmp (header, "DELAY")) { /* Reading delay information */ sscanf (line, "%*s %f %f", &delay, &correction); printf ("\n\t\t Delay : %f\t%f", delay, correction); EXEC SQL INSERT INTO Filter_Sequence_Data (seqfil_id, filter_nb, filter_id) VALUES (seqfilseq.CURRVAL, :i, filterseq.NEXTVAL); EXEC SQL INSERT INTO Filter (filter_id, gain, frequency, in_sp_rate, out_sp_rate, offset, delay, correction, seqresp_id, lddate) VALUES (filterseq.CURRVAL, :gain, :freq, :insamp, :outsamp, :offset, :delay, :correction, respseq.NEXTVAL, SYSDATE); } else if (!strcmp (header, "FIR")) { respnb++; /* Reading FIR filter information */ sscanf (line, "%*s %s %s %f", firname, symmetry, &firgain); symm = symmetry[0]; /* Inserting response information into the database */ firid = 0; EXEC SQL SELECT fir_id INTO firid FROM Filter_FIR WHERE name = :firname; if (firid == 0) /* FIR filter not yet in database */ { FILE *f_coeff; /* FIR filter coefficients file descriptor */ char line_coeff[255]; /* Current line */ char head_coeff[32]; /* Header information */ char name_coeff[32]; /* FIR filter coefficients file name */ char name_coeff2[128]; /* FIR filter coefficients file name */ EXEC SQL INSERT INTO Response (seqresp_id, resp_nb, resp_type, resp_id, unit_in, unit_out, r_type, lddate) VALUES (respseq.CURRVAL, :respnb, 'F', firseq.NEXTVAL, :unitin, :unitout, 'D', SYSDATE); EXEC SQL INSERT INTO Filter_FIR (fir_id, name, symmetry, gain, lddate) VALUES (firseq.CURRVAL, :firname, :symm, :firgain, SYSDATE); /* Populating the database with the FIR coefficients */ strcpy (name_coeff, firname); name_coeff[3] = '_'; sprintf (name_coeff2, "/data/dc2/Interim/blockettes.new/include/%s.b41.src", name_coeff); /* Opening the filter file */ if ((f_coeff = fopen (name_coeff2, "rt")) == NULL) { printf ("\n Error [File (%s) could not be opened].\n\n", name_coeff2); EXEC SQL ROLLBACK WORK RELEASE; exit (0); } /* Parsing FIR filter coefficients file */ fgets (line_coeff, 1024, f_coeff); coeff_nb = 0; while (!feof (f_coeff)) { strcpy (head_coeff, ""); sscanf (line_coeff, "%s", head_coeff); if (!strcmp (head_coeff, "fir_coeff:")) { coeff_nb++; sscanf (line_coeff, "%*s %f", &coeff_value); EXEC SQL INSERT INTO Filter_FIR_Data (fir_id, coeff_nb, type, coefficient, error) VALUES (firseq.CURRVAL, :coeff_nb, 'N', :coeff_value, NULL); printf ("\n\t\t %d\t%f", coeff_nb, coeff_value); } fgets (line_coeff, 1024, f_coeff); } fclose (f_coeff); } else { EXEC SQL INSERT INTO Response (seqresp_id, resp_nb, resp_type, resp_id, unit_in, unit_out, r_type, lddate) VALUES (respseq.CURRVAL, :respnb, 'F', :firid, :unitin, :unitout, 'D', SYSDATE); } printf ("\n\t\t FIR filter : %s\t%s\t%f", firname, symmetry, firgain); } else if (!strcmp (header, "HP")) { respnb++; /* Reading high-pass filter information */ sscanf (line, "%*s %s %d %f %f", ftype, &nb_pole, &corner, &damping); /* Inserting response information into the database */ EXEC SQL INSERT INTO Response (seqresp_id, resp_nb, resp_type, resp_id, unit_in, unit_out, r_type, lddate) VALUES (respseq.CURRVAL, :respnb, 'H', hpseq.NEXTVAL, :unitin, :unitout, 'A', SYSDATE); EXEC SQL INSERT INTO Response_HP (hp_id, filter_type, nb_pole, corner_freq, damping_value, lddate) VALUES (hpseq.CURRVAL, :ftype, :nb_pole, :corner, :damping, SYSDATE); printf ("\n\t\t HP : %s\t%d\t%f\t%f", ftype, nb_pole, corner, damping); } else if (!strcmp (header, "LP")) { respnb++; /* Reading low-pass filter information */ sscanf (line, "%*s %s %d %f %f", ftype, &nb_pole, &corner, &damping); /* Inserting response information into the database */ EXEC SQL INSERT INTO Response (seqresp_id, resp_nb, resp_type, resp_id, unit_in, unit_out, r_type, lddate) VALUES (respseq.CURRVAL, :respnb, 'L', lpseq.NEXTVAL, :unitin, :unitout, 'A', SYSDATE); EXEC SQL INSERT INTO Response_LP (lp_id, filter_type, nb_pole, corner_freq, damping_value, lddate) VALUES (lpseq.CURRVAL, :ftype, :nb_pole, :corner, :damping, SYSDATE); printf ("\n\t\t LP : %s\t%d\t%f\t%f", ftype, nb_pole, corner, damping); } else if (!strcmp (header, "CP")) { /* Reading complex pole information */ sscanf (line, "%*s %f %f", &r_value, &i_value); if (Flag_PZ == 0) { respnb++; EXEC SQL INSERT INTO Response (seqresp_id, resp_nb, resp_type, resp_id, unit_in, unit_out, r_type, lddate) VALUES (respseq.CURRVAL, :respnb, 'Z', pzseq.NEXTVAL, :unitin, :unitout, 'A', SYSDATE); } Flag_PZ++; EXEC SQL INSERT INTO Response_PZ (pz_id, pz_nb, type, r_value, r_error, i_value, i_error, lddate) VALUES (pzseq.CURRVAL, :Flag_PZ, 'P', :r_value, NULL, :i_value, NULL, SYSDATE); printf ("\n\t\t CP : %f\t%f", r_value, i_value); } else if (!strcmp (header, "CZ")) { /* Reading complex zero information */ sscanf (line, "%*s %f %f", &r_value, &i_value); if (Flag_PZ == 0) { respnb++; EXEC SQL INSERT INTO Response (seqresp_id, resp_nb, resp_type, resp_id, unit_in, unit_out, r_type, lddate) VALUES (respseq.CURRVAL, :respnb, 'Z', pzseq.NEXTVAL, :unitin, :unitout, 'A', SYSDATE); } Flag_PZ++; EXEC SQL INSERT INTO Response_PZ (pz_id, pz_nb, type, r_value, r_error, i_value, i_error, lddate) VALUES (pzseq.CURRVAL, :Flag_PZ, 'Z', :r_value, NULL, :i_value, NULL, SYSDATE); printf ("\n\t\t CZ : %f\t%f", r_value, i_value); } else if (!strcmp (header, "PN")) { respnb++; /* Reading polynomial information */ sscanf (line, "%*s %s %f %f %f %d", ptype, &lower, &upper, &maxerror, &nb_coeff); EXEC SQL INSERT INTO Response (seqresp_id, resp_nb, resp_type, resp_id, unit_in, unit_out, r_type, lddate) VALUES (respseq.CURRVAL, :respnb, 'P', pnseq.NEXTVAL, :unitin, :unitout, 'A', SYSDATE); EXEC SQL INSERT INTO Response_PN (pn_id, poly_type, lower_bound, upper_bound, max_error, nb_coeff, lddate) VALUES (pnseq.CURRVAL, :ptype, :lower, :upper, :maxerror, :nb_coeff, SYSDATE); printf ("\n\t\t PN : %s\t%f\t%f\t%f\t%d", ptype, lower, upper, maxerror, nb_coeff); /* Reading polynomial coefficients */ sscanf (line, "%*s %*s %*s %*s %*s %*s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f", &pn_coeff[0], &pn_coeff[1], &pn_coeff[2], &pn_coeff[3], &pn_coeff[4], &pn_coeff[5], &pn_coeff[6], &pn_coeff[7], &pn_coeff[8], &pn_coeff[9], &pn_coeff[10], &pn_coeff[11], &pn_coeff[12], &pn_coeff[13], &pn_coeff[14], &pn_coeff[15], &pn_coeff[16], &pn_coeff[17], &pn_coeff[18], &pn_coeff[19]); printf ("\n\t\t\t Coefficients : "); for (k=0;k