create or replace
PROCEDURE showb62resp(
	in_network   IN polynomial.net%TYPE,
	in_station   IN polynomial.sta%TYPE,
	in_location  IN polynomial.location%TYPE,
	in_channel   IN polynomial.seedchan%TYPE,
	starttime    IN varchar2,
	endtime      IN varchar2,
	in_stage     IN polynomial.stage_seq%TYPE
 )
IS
	pn_key     polynomial.pn_key%TYPE;
        tf_type    polynomial.tf_type%TYPE;
        unit_in    polynomial.unit_in%TYPE;
        unit_out   polynomial.unit_out%TYPE;
        poly_type  pn.poly_type%TYPE;
	ua1_name   d_unit.name%TYPE;
	ua1_text   d_unit.description%TYPE;
	ua2_name   d_unit.name%TYPE;
	ua2_text   d_unit.description%TYPE;
	nbc        NUMBER;

	CURSOR cr_polynomial IS
		SELECT a.pn_key, a.tf_type, a.unit_in, a.unit_out, b.poly_type
		FROM   Polynomial a, PN b
		WHERE
		a.pn_key = b.key AND
		a.net = in_network AND 
		a.sta = in_station AND 
		a.location = in_location AND
		a.seedchan = in_channel AND
		to_date(starttime,'MM/DD/YYYY HH24:MI:SS') >= a.ondate AND
		to_date(starttime,'MM/DD/YYYY HH24:MI:SS') < a.offdate AND
		a.stage_seq = in_stage;

	CURSOR cr_ua1 IS
		SELECT name, description
		FROM D_Unit
		WHERE id = unit_in;

	CURSOR cr_ua2 IS
		SELECT name, description
		FROM D_Unit
		WHERE id = unit_out;

BEGIN
	DBMS_OUTPUT.enable(1000000);

	IF NOT cr_polynomial%ISOPEN THEN
		OPEN cr_polynomial;
	END IF;

	FETCH cr_polynomial into pn_key, tf_type, unit_in, unit_out, poly_type;

	OPEN cr_ua1;
	FETCH cr_ua1 INTO ua1_name, ua1_text;
	CLOSE cr_ua1;
	OPEN cr_ua2;
	FETCH cr_ua2 INTO ua2_name, ua2_text;
	CLOSE cr_ua2;

	nbc := 0;

        SELECT COUNT(*)
        INTO nbc
        FROM  PN_Data
        WHERE key = pn_key;

	DBMS_OUTPUT.PUT_LINE('#');
	DBMS_OUTPUT.PUT_LINE('#                  +-----------------------------------+');
	DBMS_OUTPUT.PUT_LINE('#                  |    Response (Polynomial)          |' );
	DBMS_OUTPUT.PUT_LINE('#                  |        ' || rpad(in_network,2,' ')
					|| '  ' || rpad(in_station,5,' ')
					|| '  ' || rpad(in_location,2,' ')
					|| '  ' || in_channel || '         |' );
	DBMS_OUTPUT.PUT_LINE('#                  |     ' || to_char(to_date(starttime,'MM/DD/YYYY HH24:MI:SS'), 'MM/DD/YYYY') || ' to '
								|| to_char(to_date(endtime,'MM/DD/YYYY HH24:MI:SS'),'MM/DD/YYYY') || '      |' );
	DBMS_OUTPUT.PUT_LINE('#                  +-----------------------------------+');
	DBMS_OUTPUT.PUT_LINE('#');
	DBMS_OUTPUT.PUT_LINE( 'B062F03     Transfer function type:                ' || tf_type );
	DBMS_OUTPUT.PUT_LINE( 'B062F04     Stage sequence number:                 ' || in_stage );
	DBMS_OUTPUT.PUT_LINE( 'B062F05     Response in units lookup:              ' || ua1_name || ' - ' || ua1_text);
	DBMS_OUTPUT.PUT_LINE( 'B062F06     Response out units lookup:             ' || ua2_name || ' - ' || ua2_text);
	DBMS_OUTPUT.PUT_LINE( 'B062F07     Polynomial Approximation Type:         ' || poly_type );

	showb62_1st_line( pn_key );
	if( nbc != 0 ) then
		DBMS_OUTPUT.PUT_LINE('B062F14     Number of coefficients:                '||nbc);
		showb62rep( pn_key );
	end if;

	close cr_polynomial;
END;
