	subroutine ctd_read(ctd,npmax,nv,cast_id,pmin,pmax)
c
c	Reads CTD data from EPIC program CTDLST output.
c
c	nv = total number of columns to read
c	icol = column numbers
c	ipcol = pressure column (does not have to be in icol)
c	npmax = maximum scans expected
c	pmin, pmax = pressure limits
c	cast_id = cast name in header record
	
	COMMON/matrix/ icol(9),ipcol,Xdum
        CHARACTER*120 aline
       CHARACTER*40 cast_id
       CHARACTER*3 imon
       common/check/nchk,xbad
	dimension ctd(npmax,nv),jcol(9),idate(5),xx(20)
	equivalence (icol,idate)
	ny=1
	do iv=1,nv
	jcol(iv)=icol(iv)
	enddo
	ncmx=jcol(nv)
c
c	read headers
c
	nr=0
c
c	check for Hakuho format
c
	if(index(cast_id,'/').gt.0) then 
     	call hakuho_head(cast_id,idate)
	go to 125
	endif
	ncm=notbnk(cast_id,40)
100	read(1,101,end=900) aline
101	format(a)
	ic=index(aline,'CAST')
	if(ic.eq.0) go to 100
	if(index(aline(ic+4:),cast_id(1:ncm)).eq.0) go to 100
	call init_array(ctd,npmax,ny,nv,xbad)
	id=index(aline,'DATE')+5
	read(aline(id:),102) idate(3),imon,idate(5)
102	format(i2,1x,a,1x,i2)
	idate(4)=jmonth(imon)
	id=index(aline,'TIME')+5
	read(aline(id:),103) idate(1),idate(2)
103	format(2i2)
	nb=notbnk(aline,120)
	write(6,111) aline(1:nb)
111	format(1x,a)
120	read(1,101,end=900) aline
	nb=notbnk(aline,120)
	write(6,111) aline(1:nb)
	if(index(aline,'SCAN').eq.0) go to 120
c
c	read data 
c
125	nr=0
	write (6,128) (jcol(iv),iv=1,nv)
128	format(/10x,'Reading from columns ',9i2)
	write(6,129)
129	format(/)
	read(1,101,end=900) aline
130	read(1,101,end=900) aline
	nb=notbnk(aline,120)
	if(nb.lt.10) go to 130
	read(aline,*,err=900) (xx(i),i=1,ncmx)
	if(xx(ipcol).lt.pmin) go to 130
	if(xx(ipcol).gt.pmax) go to 900
	nr=nr+1
	do i=1,nv
	ctd(nr,i)=xx(jcol(i))
	enddo
	if(nr.le.2) write(6,139) (ctd(nr,i),i=1,nv)
139	format(1x,9f8.3)
	if(nr.lt.npmax) go to 130
900	write(6,139) (ctd(nr,i),i=1,nv)
	write(6,901) nr
901	format(/,i6,' scans entered')
	return
	end
	
    	subroutine hakuho_head(cast_id,idate)
       CHARACTER*40 cast_id,fnam,aline
	dimension idate(5)
c
c	read Hakuho header info, then open data file
c
	fnam=cast_id
100	read(1,101,end=200) aline
101	format(a)
	nb=notbnk(aline,40)
	write(6,111) aline(1:nb)
111	format(1x,a)
	if(index(aline,'Cruise').gt.0) then
	mb=lblnk(aline,nb)+1
	cast_id=aline(mb:nb)
	nc=notbnk(cast_id,40)+2
	endif
	if(index(aline,'Station').gt.0) then
	mb=lblnk(aline,nb)+1
	cast_id(nc:)=aline(mb:nb)
	endif
	if(index(aline,'Date').gt.0) then
	read(aline(nb-7:nb),'(i2,1x,i2,1x,i2)') 
     1	idate(5),idate(4),idate(3)
	endif
	if(index(aline,'Time(G').gt.0) then
	read(aline(nb-4:nb),'(i2,1x,i2)') 
     1	idate(1),idate(2)
	endif
	go to 100
200	close(1)
	call fopen(1,fnam,'old')
	return
	end
