create or replace
PROCEDURE showb53resp(
	in_network   IN poles_zeros.net%TYPE,
	in_station   IN poles_zeros.sta%TYPE,
	in_location  IN poles_zeros.location%TYPE,
	in_channel   IN poles_zeros.seedchan%TYPE,
	starttime    IN varchar2,
	endtime      IN varchar2,
	in_stage     IN poles_zeros.stage_seq%TYPE
 )
IS
	pz_key     poles_zeros.pz_key%TYPE;
	tf_type    poles_zeros.tf_type%TYPE;
	unit_in    poles_zeros.unit_in%TYPE;
	unit_out   poles_zeros.unit_out%TYPE;
        AO         poles_zeros.AO%TYPE;
        AF         poles_zeros.AF%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;
	nbz        NUMBER;
	nbp        NUMBER;

	CURSOR cr_poles_zeros IS
		SELECT pz_key, tf_type, unit_in, unit_out, AO, AF
		FROM   Poles_Zeros
		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_poles_zeros%ISOPEN THEN
		OPEN cr_poles_zeros;
	END IF;

	FETCH cr_poles_zeros into pz_key, tf_type, unit_in, unit_out, AO, AF;

	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;

	nbz := 0;
	nbp := 0;

	SELECT COUNT(*)
	INTO nbz
	FROM   PZ_Data
	WHERE type = 'Z' AND
	key = pz_key;

	SELECT COUNT(*) 
        INTO nbp
        FROM   PZ_Data
        WHERE type = 'P' AND
        key = pz_key;

	DBMS_OUTPUT.PUT_LINE('#');
	DBMS_OUTPUT.PUT_LINE('#                  +-----------------------------------+');
	DBMS_OUTPUT.PUT_LINE('#                  |    Response (Poles and Zeros)     |' );
	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( 'B053F03     Transfer function type:                ' || tf_type );
	DBMS_OUTPUT.PUT_LINE( 'B053F04     Stage sequence number:                 ' || in_stage );
	DBMS_OUTPUT.PUT_LINE( 'B053F05     Response in units lookup:              ' || ua1_name || ' - ' || ua1_text);
	DBMS_OUTPUT.PUT_LINE( 'B053F06     Response out units lookup:             ' || ua2_name || ' - ' || ua2_text);
	DBMS_OUTPUT.PUT_LINE( 'B053F07     A0 normalization factor:              ' || to_char(AO,'S0.99999EEEE') );
	DBMS_OUTPUT.PUT_LINE( 'B053F08     Normalization frequency:              ' || to_char(AF,'S0.99999EEEE') );
	DBMS_OUTPUT.PUT_LINE( 'B053F09     Number of zeroes:                      ' || nbz );
	DBMS_OUTPUT.PUT_LINE( 'B053F14     Number of poles:                       ' || nbp );

	if( nbz != 0 or nbp != 0 ) then
		showb53rep( pz_key );
	end if;

	close cr_poles_zeros;
END;
