create or replace
PROCEDURE showsacpz(
	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;
	db_curtime   DATE;
	db_lat	     channel_data.lat%TYPE;
	db_lon	     channel_data.lon%TYPE;
	db_elev	     channel_data.elev%TYPE;
	db_edepth    channel_data.edepth%TYPE;
	db_azimuth   channel_data.azimuth%TYPE;
	db_dip	     channel_data.dip%TYPE;
	db_samprate  channel_data.samprate%TYPE;
	db_desc      d_abbreviation.description%TYPE;
	db_staname   station_data.staname%TYPE;

	CURSOR cr_channel IS
		SELECT SYSDATE, c.net, c.sta, c.seedchan, c.location, c.ondate, c.offdate, c.lat, c.lon, c.elev, c.edepth,
		c.azimuth, c.dip, c.samprate, a.description, s.staname
                FROM   Channel_Data c, D_Abbreviation a, Station_Data s
                WHERE
		c.net = s.net AND
		c.sta = s.sta AND
		c.ondate BETWEEN s.ondate AND s.offdate AND
		c.inid = a.id AND
                c.net = net_in AND 
                c.sta = stat_in AND 
                c.location like loc AND
                c.seedchan = chan_in AND
                to_date(edate_in,'MM/DD/YYYY HH24:MI:SS') >= c.ondate AND
                to_date(sdate_in,'MM/DD/YYYY HH24:MI:SS') < c.offdate
		ORDER BY c.net, c.sta, c.seedchan, c.location, c.ondate, c.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
		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_curtime, db_network, db_station, db_channel, db_location, db_starttime, db_endtime, db_lat, db_lon, db_elev, db_edepth, db_azimuth, db_dip, db_samprate, db_desc, db_staname;
	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( '* NETWORK   (KNETWK): ' || db_network );
			DBMS_OUTPUT.PUT_LINE( '* STATION    (KSTNM): ' || db_station );
			DBMS_OUTPUT.PUT_LINE( '* LOCATION   (KHOLE): ' || db_location );
			DBMS_OUTPUT.PUT_LINE( '* CHANNEL   (KCMPNM): ' || db_channel );
			DBMS_OUTPUT.PUT_LINE( '* CREATED           : ' || to_char(db_curtime, 'YYYY,DDD,HH24:MI:SS') );
			DBMS_OUTPUT.PUT_LINE( '* START             : ' || to_char(db_starttime, 'YYYY,DDD,HH24:MI:SS') );
			DBMS_OUTPUT.PUT_LINE( '* END               : ' || to_char(db_endtime,   'YYYY,DDD,HH24:MI:SS') );
			DBMS_OUTPUT.PUT_LINE( '* DESCRIPTION       : ' || db_staname );
			DBMS_OUTPUT.PUT_LINE( '* LATITUDE          : ' || db_lat );
			DBMS_OUTPUT.PUT_LINE( '* LONGITUDE         : ' || db_lon );
			DBMS_OUTPUT.PUT_LINE( '* ELEVATION         : ' || db_elev );
			DBMS_OUTPUT.PUT_LINE( '* DEPTH             : ' || db_edepth );
			DBMS_OUTPUT.PUT_LINE( '* DIP               : ' || db_dip );
			DBMS_OUTPUT.PUT_LINE( '* AZIMUTH           : ' || db_azimuth );
			DBMS_OUTPUT.PUT_LINE( '* SAMPLE RATE       : ' || db_samprate );
			DBMS_OUTPUT.PUT_LINE( '* INPUT UNIT        : M' );
			DBMS_OUTPUT.PUT_LINE( '* OUTPUT UNIT       : COUNTS' );
			DBMS_OUTPUT.PUT_LINE( '* INSTTYPE          : ' || db_desc );

			lastnet   := db_network;
			laststat  := db_station;
			lastchan  := db_channel;
			lastloc   := db_location;
			laststart := db_starttime;
			lastend   := db_endtime;
		end if;

		OPEN cr_resp;
		FETCH cr_resp INTO btype, db_stage;
		WHILE cr_resp%FOUND
		LOOP
			if( btype = 53 ) then
				showPZresp(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;


		FETCH cr_channel INTO db_curtime, db_network, db_station, db_channel, db_location, db_starttime, db_endtime, db_lat, db_lon, db_elev, db_edepth, db_azimuth, db_dip, db_samprate, db_desc, db_staname;
	END LOOP;

	CLOSE cr_channel;
END;
