/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ /* Populate_Filter.pc */ /* Load to the NCEDC database the filter info. */ /* Errors can be found in Error.log */ /* */ /* Zuzlewski Stephane @1998-2002 */ /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ #include #include #include #define CONNECT_STRING "user/passwd@db" 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 */ double sgain; /* Sequence of filters gain */ double sfreq; /* Sequence of filters frequency */ double gain; /* Filter gain */ double freq; /* Filter frequency */ double insamp; /* Input sample rate */ double outsamp; /* Output sample rate */ int offset; /* Decimation offset */ double delay; /* Estimated delay */ double correction; /* Correction applied */ int nb_line; /* Number of response lines */ char ftype[32]; /* Filter type */ int nb_pole; /* Number of poles */ double corner; /* Corner frequency */ double damping; /* Damping value */ double r_value; /* Real part of a complex pole/zero */ double i_value; /* Imaginary part of a complex pole/zero */ int Flag_PZ; /* Flag for poles & zeros */ char ptype[32]; /* Polynomial type */ double lower; /* Polynomial lower bound */ double upper; /* Polynomial upper bound */ double maxerror; /* Maximum error of polynomial approximation */ int nb_coeff; /* Number of coefficients */ double pncoeff; /* Polynomial coefficients */ int respnb; /* Response number */ char firname[32]; /* FIR filter name */ double firgain; /* FIR filter gain */ int firid; /* FIR filter identifier */ char symmetry[5]; /* FIR filter symmetry */ char symm; /* FIR filter symmetry */ double 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 (int argc, char* argv[]) { FILE* f_filter; /* Filter file descriptor */ char line[1025]; /* Line information */ char header[7]; /* Header */ double pn_coeff[21]; /* Polynomial coefficients */ if (argc != 2) { printf ("\n %s \n\n", argv[0]); exit (0); } /* Opening the filter file */ if ((f_filter = fopen (argv[1], "rt")) == NULL) { printf ("\n Error [File (%s) could not be opened].\n\n", argv[1]); exit (0); } /* 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); } /* Connect to ORACLE. */ EXEC SQL WHENEVER SQLERROR DO sql_error(); strcpy (user_pwd, CONNECT_STRING); EXEC SQL CONNECT :user_pwd; /* 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 %lf %lf %*s %*s %d", &gain, &freq, &nb_line); printf ("\n\t Filter : %lf\t%lf\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 (sqlca.sqlcode == (-2112)) { fprintf (f_err, "\nError: Multiple entries for unit 'name=COUNTS' in table D_Unit.\n\n"); } 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 %lf %*s %lf %d", &insamp, &outsamp, &offset); printf ("\n\t\t Input : %lf\t%lf\t%d", insamp, outsamp, offset); } else if (!strcmp (header, "DELAY")) { /* Reading delay information */ sscanf (line, "%*s %lf %lf", &delay, &correction); printf ("\n\t\t Delay : %lf\t%lf", 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 %lf", 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 (sqlca.sqlcode == (-2112)) { fprintf (f_err, "\nError: Multiple entries for FIR filter 'name=%s' in table Filter_FIR.\n\n", 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 %lf", &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%lf", 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%lf", firname, symmetry, firgain); } else if (!strcmp (header, "HP")) { respnb++; /* Reading high-pass filter information */ sscanf (line, "%*s %s %d %lf %lf", 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, 'D', 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%lf\t%lf", ftype, nb_pole, corner, damping); } else if (!strcmp (header, "LP")) { respnb++; /* Reading low-pass filter information */ sscanf (line, "%*s %s %d %lf %lf", 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, 'D', 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%lf\t%lf", ftype, nb_pole, corner, damping); } else if (!strcmp (header, "CP")) { /* Reading complex pole information */ sscanf (line, "%*s %lf %lf", &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, 'D', 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 : %lf\t%lf", r_value, i_value); } else if (!strcmp (header, "CZ")) { /* Reading complex zero information */ sscanf (line, "%*s %lf %lf", &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, 'D', 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 : %lf\t%lf", r_value, i_value); } else if (!strcmp (header, "PN")) { respnb++; /* Reading polynomial information */ sscanf (line, "%*s %s %lf %lf %lf %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, 'D', 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%lf\t%lf\t%lf\t%d", ptype, lower, upper, maxerror, nb_coeff); /* Reading polynomial coefficients */ sscanf (line, "%*s %*s %*s %*s %*s %*s %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf", &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