c                                                                               
c     routine to plot response function       
c     jh February 1997
c
c  changes
c
c     
c     26 mar 99 bmt : include winplot.inc
c     june 99 jh    :  ------------  version 7.0 -----------------------
c     11 may 00 lo  : include waveform.inc and use read_resp routine
c      16 may 01 jh : fix common block
c     16 june 05 jh : plt to eps
c
c
c                                

      include 'winplot1.inc'   
c      implicit none
      include 'seiplot.inc'
      include 'seidim.inc'
      include 'waveform.inc'
      real ff ! frequency
c-- number of points in spectrum
      integer nspec
c-- if several data sets, number of points in each
      integer nnspec(2)
c-- counters and help variables                                               
      integer i,j				
c-- response value
      complex respons
      character*80 text
      character*30 xtext,ytext      ! axis text
c--returns from spectral plot
      character*1 c_spec(20)
      real x0                       ! left corner of spectrum
c-- Arguments passed
      character*80     arg(10)
      real cx(20),cy(20)
      integer nc,nars
      real x(1000),y(1000)

      logical gse_resp          ! true if gse format
      logical sei_resp          ! true if seisan format
      integer nfa               ! number of frequencies and amplitude

c   NUMBER OF FILTERS,  POLES,AND FREQUENCIES
      INTEGER NFILT,POLE(10)
      real xpole(10)
      REAL FFILT(10)
      complex pol(max_resp_value),zero(max_resp_value)  ! complex PAZ
      integer npol,nzero        ! number of poles and zeros
      real norm                 ! normalization constant for poles and zeros
C   AMPLIFIER GAIN (DB), SEISMOMETER GENERATOR CONSTANT (LOADED V/M/S)
      REAL GAIN,GENCON
C   SEISMOMETER OR ACCELEROMETER DAMPING RATIO AND SEISMOMETER PERIOD
      REAL DAMPIN,PERIOD
C   RECORDING MEDIA GAIN (COUNTS/V OR M/VOLT)
      REAL REGAIN
c   sensor type 2: seismometer, 3: accelerometer
      integer sentyp
      real g1hz  ! gain at 1 hz
c   individual response values
      real freq_val(max_resp_value),gain_val(max_resp_value),
     *phas_val(max_resp_value)
      logical ftab                      ! if true, force tabulated values

c
c response common block
c
      common /response/nfilt,pole,ffilt,gain,gencon,dampin,period,ftab,
     *                 regain,g1hz,freq_val,gain_val,phas_val,sentyp,
     *                 npol,nzero,pol,zero,norm,gse_resp,sei_resp,nfa
      equivalence (xpole,pole)

      include 'version.inc'
      include 'winplot2.inc'
 
c
c print version
c
      out_version_date='July 23, 2001'
      if (version_new) out_version_date=version_date
      call print_ver

      nars = 0
c 
c   get argument
c
      call get_arguments(nars,arg)    ! get arguments 
      if(nars .eq. 1) then
           wav_resp_file=arg(1)
      elseif(nars.eq.0) then
 10       continue
          text=' File name, number, ? to get list'
          call filename(text,wav_resp_file)      
          if(wav_resp_file.eq.'EOF') goto 999
      else
          write(6,*)' Enter one argument with file name'
          stop
      endif
 
c
c  check display type, can be x (sun) or Microsoft (pc)
c
      call get_display(display_type)
c
c-- x-window size in % screen
c
      wsize=65
c
c  graphic display not yet open
c
      disp_open=0

c
c read response file
c
         call read_resp
         plotoption=1
         hctype=1
c
c  open plot file
c
         open(2,file='presp.eps',status='unknown')
         plotunit=2
c
c   open plotter
c
         call open_display 
c
c set some postscipt scalings
c
         write(plotunit,*) ' 1.4 0.85 scale'
c
c  the amplitude response
c
      j=0
      do i = -260,190,1    ! log frequencies
         j=j+1
         ff=10**(i/100.0)
         call calc_resp(ff,respons)
         y(j)=log10(cabs(respons))
         x(j) = log10(ff)          
 1243    continue
      enddo
      nspec=j
c
c   plot spectrum
c
      xtext='      Log frequency (Hz)     '
      ytext='Log amplitude                '
      write(text,'(a)')'Amplitude Response, count/nm '
      nnspec(1)=nspec
      x0=100.0                
      call xy_plot
     *(1,nnspec,x,y,text,xtext,ytext,590.0,250.0,
     *x0,420.0,2,1,30.0
     *,0,c_spec,nc,cx,cy)
c
c  the phase response
c
      j=0
      do i = -260,190,1    ! log frequencies
         j=j+1
         ff=10**(i/100.0)
         call calc_resp(ff,respons)
         y(j)=180.0*atan2(imag(respons),real(respons))/3.141592654
         x(j) = log10(ff)          
      enddo
      nspec=j
c
c   plot spectrum
c
      ytext='Phase                '
      write(text,'(a)')'Phase response '
      nnspec(1)=nspec
      call tchars('q to quit',9,750.0,700.0)
      call tchars(wav_resp_file(1:seiclen(wav_resp_file)),80,10.0,750.0)
      call pchars(wav_resp_file(1:seiclen(wav_resp_file)),80,10.0,750.0)
      call xy_plot
     *(1,nnspec,x,y,text,xtext,ytext,590.0,250.0,
     *x0,70.0,2,1,30.0
     *,1,c_spec,nc,cx,cy)
c
c   close postscript
c
      call close_post
c
c   close output plot file
c 
      close(2)
c
c   back to alpha
c
      call clear_to_alpha
c
c   if filenr.lis list is used, try another
c   
      if(nars.eq.0) goto 10
c
 999  continue
      write(6,*)' Plot file in presp.eps'
      stop
      end                                                                       
c
c      include 'pcplot.inc'   ! pc plotting routines
