create or replace
PROCEDURE showresp(
	net_in   IN channel_data.net%TYPE,
	stat_in  IN channel_data.sta%TYPE,
	loc_in   IN channel_data.location%TYPE,
	chan_in  IN channel_data.seedchan%TYPE,
	sdate_in IN varchar2,
	edate_in IN varchar2
 )
IS
	loc          channel_data.location%TYPE;
	lastnet      string(2);
	laststat     string(5);
	lastchan     string(3);
	lastloc      string(2);
	laststart    channel_data.ondate%TYPE;
	lastend      channel_data.offdate%TYPE;
	btype        string(3);
        db_stage     number;
	db_network   channel_data.net%TYPE;
	db_station   channel_data.sta%TYPE;
	db_channel   channel_data.seedchan%TYPE;
	db_location  channel_data.location%TYPE;
	db_starttime channel_data.ondate%TYPE;
	db_endtime   channel_data.offdate%TYPE;
	stage0_flag  number;
	stage0_poly  number;

	CURSOR cr_channel IS
		SELECT net, sta, seedchan, location, ondate, offdate
                FROM   Channel_Data
                WHERE
                net = net_in AND 
                sta = stat_in AND 
                location like loc AND
                seedchan = chan_in AND
                to_date(edate_in,'MM/DD/YYYY HH24:MI:SS') >= ondate AND
                to_date(sdate_in,'MM/DD/YYYY HH24:MI:SS') < offdate
		ORDER BY net, sta, seedchan, location, ondate, offdate;

	CURSOR cr_resp IS
		(SELECT '53', stage_seq
		FROM Poles_Zeros
		WHERE net = db_network AND
       		sta = db_station AND
		location = db_location AND
		seedchan = db_channel AND
		ondate = db_starttime
		UNION
		SELECT '54', stage_seq
		FROM Coefficients
		WHERE net = db_network AND
       		sta = db_station AND
		location = db_location AND
		seedchan = db_channel AND
		ondate = db_starttime
                UNION
		SELECT '57', stage_seq
		FROM Decimation
		WHERE net = db_network AND
       		sta = db_station AND
		location = db_location AND
		seedchan = db_channel AND
		ondate = db_starttime
                UNION
		SELECT '58', stage_seq
		FROM Sensitivity
		WHERE net = db_network AND
       		sta = db_station AND
		location = db_location AND
		seedchan = db_channel AND
		ondate = db_starttime
                UNION
		SELECT '62', stage_seq
		FROM Polynomial
		WHERE net = db_network AND
                sta = db_station AND
                location = db_location AND
                seedchan = db_channel AND
                ondate = db_starttime)
		ORDER BY stage_seq;

BEGIN
	DBMS_OUTPUT.enable(1000000);
	lastnet   := '  ';
	laststat  := '  ';
	lastchan  := '  ';
	lastloc   := '  ';
	laststart := to_date('01/01/1700 00:00:00','MM/DD/YYYY HH24:MI:SS');
	lastend   := to_date('01/01/2500 00:00:00','MM/DD/YYYY HH24:MI:SS');

	loc := loc_in;
	IF loc != '%' THEN
		loc := rpad( loc, 2, ' ' );
		loc := replace(loc,'-',' ');
	END IF;
	-- DBMS_OUTPUT.PUT_LINE( 'loc: >' || loc ||'<' );


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

	FETCH cr_channel INTO db_network, db_station, db_channel, db_location, db_starttime, db_endtime;
	WHILE cr_channel%FOUND
	LOOP
		if( (lastnet != db_network) or (laststat != db_station) or (lastloc != db_location) or
		  (lastchan != db_channel) or (laststart != db_starttime) or (lastend != db_endtime) ) then
			DBMS_OUTPUT.PUT_LINE( '#' );
			DBMS_OUTPUT.PUT_LINE( '###################################################################################' );
			DBMS_OUTPUT.PUT_LINE( '#' );
			DBMS_OUTPUT.PUT_LINE( 'B050F03     Station:     ' || db_station );
			DBMS_OUTPUT.PUT_LINE( 'B050F16     Network:     ' || db_network );
			if( db_location = '  ' ) then
				DBMS_OUTPUT.PUT_LINE( 'B052F03     Location:    ??' );
			else
				DBMS_OUTPUT.PUT_LINE( 'B052F03     Location:    ' || db_location );
			end if;
			DBMS_OUTPUT.PUT_LINE( 'B052F04     Channel:     ' || db_channel );
			DBMS_OUTPUT.PUT_LINE( 'B052F22     Start date:  ' || to_char(db_starttime, 'YYYY,DDD,HH24:MI:SS') );
			DBMS_OUTPUT.PUT_LINE( 'B052F23     End date:    ' || to_char(db_endtime,   'YYYY,DDD,HH24:MI:SS') );
			lastnet   := db_network;
			laststat  := db_station;
			lastchan  := db_channel;
			lastloc   := db_location;
			laststart := db_starttime;
			lastend   := db_endtime;
		end if;

		stage0_flag := 0;
		stage0_poly := 0;
		OPEN cr_resp;
		FETCH cr_resp INTO btype, db_stage;
		WHILE cr_resp%FOUND
		LOOP
			if (db_stage = 0) then
				stage0_flag := 1;
				if ( btype = 62 ) then
					stage0_poly := 1;
				end if;
			elsif( btype = 53 ) then
				showb53resp(db_network, db_station, db_location, db_channel, to_char(db_starttime,'MM/DD/YYYY HH24:MI:SS'), to_char(db_endtime,'MM/DD/YYYY HH24:MI:SS'), db_stage);
			elsif( btype = 54 ) then
				showb54resp(db_network, db_station, db_location, db_channel, to_char(db_starttime,'MM/DD/YYYY HH24:MI:SS'), to_char(db_endtime,'MM/DD/YYYY HH24:MI:SS'), db_stage);
			elsif( btype = 57 ) then
				showb57resp(db_network, db_station, db_location, db_channel, to_char(db_starttime,'MM/DD/YYYY HH24:MI:SS'), to_char(db_endtime,'MM/DD/YYYY HH24:MI:SS'), db_stage);
			elsif( btype = 58  ) then
				showb58resp(db_network, db_station, db_location, db_channel, to_char(db_starttime,'MM/DD/YYYY HH24:MI:SS'), to_char(db_endtime,'MM/DD/YYYY HH24:MI:SS'), db_stage);
			elsif( btype = 62 ) then
				showb62resp(db_network, db_station, db_location, db_channel, to_char(db_starttime,'MM/DD/YYYY HH24:MI:SS'), to_char(db_endtime,'MM/DD/YYYY HH24:MI:SS'), db_stage);
			end if;
			FETCH cr_resp INTO btype, db_stage;
		END LOOP;
		CLOSE cr_resp;

		if (stage0_flag = 1) then
			if (stage0_poly = 0) then
				showb58resp(db_network, db_station, db_location, db_channel, to_char(db_starttime,'MM/DD/YYYY HH24:MI:SS'), to_char(db_endtime,'MM/DD/YYYY HH24:MI:SS'), 0);
			else
				showb62resp(db_network, db_station, db_location, db_channel, to_char(db_starttime,'MM/DD/YYYY HH24:MI:SS'), to_char(db_endtime,'MM/DD/YYYY HH24:MI:SS'), 0);
			end if;
		end if;

		FETCH cr_channel INTO db_network, db_station, db_channel, db_location, db_starttime, db_endtime;
	END LOOP;

	CLOSE cr_channel;
END;
