c.....This program reads the tables of the eight principal 
c.....tidal constituents and extracts a specified component
c.....within T/P sampling space
      program readtab
      implicit none
      integer jmin, jmax, imax, ntide, ntide2
      double precision undef
      parameter (jmin=24, jmax=157, imax=360, ntide=10)
      parameter (ntide2=2*ntide)
      parameter (undef=9999.9999d0)
      integer itide, iform
      double precision tab(ntide2), lat, lon, colat, pi
      double precision tide1, tide2
      integer irec, i1, i2, i, j, n

      write(6,*) 'Specify Tidal Component Required (1-10)'
      write(6,*) 'M2 = 1'
      write(6,*) 'S2 = 2'
      write(6,*) 'N2 = 3'
      write(6,*) 'K2 = 4'
      write(6,*) 'K1 = 5'
      write(6,*) 'O1 = 6'
      write(6,*) 'P1 = 7'
      write(6,*) 'Q1 = 8'
      write(6,*) 'Mf = 9'
      write(6,*) 'Mm =10'
      read*,itide
      write(6,*) 'Specify Output Format (1-2)'
      write(6,*) '(Amplitude(cm), Greenwich Phase(deg)) = 1'
      write(6,*) '(In Phase(cm), Quadrature(cm))        = 2'
      read*,iform

      pi=4.0d0*datan(1.0d0)
      open(10,file='otides10_dw3.2.100',form='formatted',
     &access='direct',status='old',recl=180)
      open(20,file='output_dw',form='formatted',access='sequential',
     &status='unknown')

      do 10 j=jmin,jmax
	do 20 i=1,imax
	  irec=(j-jmin)*imax+i
	  read(10,2000,rec=irec) (tab(n),n=1,ntide2)
	  i1=2*itide-1
	  i2=2*itide
	  if (tab(i1) .ne. undef) then
	    colat=dble(j)-0.5d0
	    lat=90.0d0-colat
	    lon=dble(i)-0.5d0
	    if (iform .eq. 1) then
	      tide1=tab(i1)
	      tide2=tab(i2)
            elseif (iform .eq. 2) then
	      tide1=tab(i1)*dcos(tab(i2)*pi/180.0d0)
	      tide2=tab(i1)*dsin(tab(i2)*pi/180.0d0)
            endif
	    write(20,2100) lon,lat,tide1,tide2
          endif
20      continue
10    continue

2000  format(20(f9.4))
2100  format(f5.1,1x,f5.1,1x,f9.4,1x,f9.4)
      stop
      end
