create or replace
PROCEDURE showb54resp(
	in_network   IN coefficients.net%TYPE,
        in_station   IN coefficients.sta%TYPE,
        in_location  IN coefficients.location%TYPE,
        in_channel   IN coefficients.seedchan%TYPE,
        starttime    IN varchar2,
        endtime      IN varchar2,
        in_stage     IN coefficients.stage_seq%TYPE
 )
IS
	dc_key     coefficients.dc_key%TYPE;
        tf_type    coefficients.tf_type%TYPE;
        unit_in    coefficients.unit_in%TYPE;
        unit_out   coefficients.unit_out%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;
	nbn        NUMBER;
	nbd        NUMBER;
	symmetry   dc.symmetry%TYPE;

	CURSOR cr_coefficients IS
		SELECT dc_key, tf_type, unit_in, unit_out
		FROM   Coefficients
		WHERE
		net = in_network AND 
		sta = in_station AND 
		location = in_location AND
		seedchan = in_channel AND
		to_date(starttime,'MM/DD/YYYY HH24:MI:SS') >= ondate AND
		to_date(starttime,'MM/DD/YYYY HH24:MI:SS') < offdate AND
		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_coefficients%ISOPEN THEN
		OPEN cr_coefficients;
	END IF;

	FETCH cr_coefficients INTO dc_key, tf_type, unit_in, unit_out;

	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;

	nbn := 0;
        nbd := 0;

        SELECT COUNT(*)
        INTO nbn
        FROM   DC_Data
        WHERE type = 'N' AND
        key = dc_key;

        SELECT COUNT(*) 
        INTO nbd
        FROM   DC_Data
        WHERE type = 'D' AND
        key = dc_key;

	if (nbn > 0) then
		SELECT symmetry
        	INTO symmetry
        	FROM DC
        	WHERE key = dc_key;

		if (symmetry = 'E') then
			nbn := 2*nbn;
		elsif (symmetry = 'O') then
			nbn := 2*nbn - 1;
		end if;
	end if;

	DBMS_OUTPUT.PUT_LINE('#');
	DBMS_OUTPUT.PUT_LINE('#                  +-----------------------------------+');
	DBMS_OUTPUT.PUT_LINE('#                  |      Response (Coefficients)      |' );
	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( 'B054F03     Transfer function type:                ' || tf_type );
	DBMS_OUTPUT.PUT_LINE( 'B054F04     Stage sequence number:                 ' || in_stage );
	if( unit_in is null ) then
		DBMS_OUTPUT.PUT_LINE( 'B054F05     Response in units lookup:              ');
	else
		DBMS_OUTPUT.PUT_LINE( 'B054F05     Response in units lookup:              ' || ua1_name || ' - ' || ua1_text);
	end if;
	if( unit_out is null ) then
		DBMS_OUTPUT.PUT_LINE( 'B054F06     Response out units lookup:             ' );
	else
		DBMS_OUTPUT.PUT_LINE( 'B054F06     Response out units lookup:             ' || ua2_name || ' - ' || ua2_text);
	end if;
	if( nbn != 0 ) then
		DBMS_OUTPUT.PUT_LINE( 'B054F07     Number of numerators:                  ' || nbn );
	else
		DBMS_OUTPUT.PUT_LINE( 'B054F07     Number of numerators:                  0' );
	end if;
	if( nbd != 0 ) then
		DBMS_OUTPUT.PUT_LINE( 'B054F10     Number of denominators:                ' || nbd );
	else
		DBMS_OUTPUT.PUT_LINE( 'B054F10     Number of denominators:                0' );
	end if;

	if( nbn != 0 or nbd != 0 ) then
		showb54rep( dc_key );
	end if;

	close cr_coefficients;
END;
