create or replace
PROCEDURE showPZresp(
	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;
	nbz        NUMBER;
	nbp        NUMBER;
	addz	   NUMBER;
	inst_gain  Sensitivity.sensitivity%TYPE;
	sens       Sensitivity.sensitivity%TYPE;

	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_inst_gain IS
		SELECT sensitivity
		FROM Sensitivity
		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 = 1;

        CURSOR cr_sens IS
                SELECT sensitivity
                FROM Sensitivity
                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 = 0;

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_inst_gain;
	FETCH cr_inst_gain INTO inst_gain;
	CLOSE cr_inst_gain;

	OPEN cr_sens;
	FETCH cr_sens INTO sens;
	CLOSE cr_sens;

	nbz := 0;
	nbp := 0;
	addz := 0;

	if (ua1_name = 'M/S') then
		addz := 1;
	elsif (ua1_name = 'M/S**2') then
		addz := 2;
	end if;

	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( '* INSTGAIN          : ' || ltrim(to_char(inst_gain,'0.999999EEEE')) || ' (' || ua1_name || ')' );
	DBMS_OUTPUT.PUT_LINE( '* COMMENT           : N/A' );
	DBMS_OUTPUT.PUT_LINE( '* SENSITIVITY       : ' || ltrim(to_char(sens,'0.999999EEEE')) || ' (' || ua1_name || ')' );
	DBMS_OUTPUT.PUT_LINE( '* A0                : ' || ltrim(to_char(AO,'0.999999EEEE')) );
	DBMS_OUTPUT.PUT_LINE( '* **********************************' );

	if( nbz != 0 or nbp != 0 ) then
		showPZrep( pz_key, nbz, nbp, addz );
	end if;

	close cr_poles_zeros;

	DBMS_OUTPUT.PUT_LINE( 'CONSTANT        ' || ltrim(to_char(AO*sens,'S0.999999EEEE')) );
END;
