	program mcread
c
c	reads and converts Microcat files and outputs in CMG format  
c
c	years are 4 digit
c
	logical pflg
	character*3 amon
	character*8 proj
	character*10 keyword(4)
	character*24 adate
	CHARACTER*80 line
	character*40 fnami,fnamo,form
	dimension idate(5),ldate(5),x(5),mdate(5,6),xdate(6),jdate(5)
	data keyword /'SERIAL NO.','interval =','lenumber =',
     1	'start samp'/
	data jdate /0,0,1,1,1987/
c
	CS=4.2914
	nrun=0
100	read(5,*,end=900,err=900) fnami,fnamo,proj,depth,nvar,
     1	nmark,((mdate(j,m),j=1,5),m=1,nmark)
  	nrun=nrun+1
	if(nrun.gt.1) call pej
	call fdate(adate)
	call fopen(1,fnami,'old')
	call fopen(2,fnamo,'new')
	write(6,101) fnami,fnamo,adate
101	format(' Program mcread'/10x,'Input file name:  ',a/
     1	10x,'Output file name: ',a/10x,'Run date: ',a//
     1	10x,'Input Headers'//)
	write(form,103) nvar
103	format('(1x,2i2,1x,2i2,i4,',i1,'f8.3,f8.5,i6)')
c
c	read header info to get s/n, ndata and delt
c
105	read(1,107,end=900) line
107	format(a)
	nb=notbnk(line,80)
	write(6,111) line(1:nb)
111	format(1x,a)
	ix=index(line,keyword(1))
	if(ix.gt.0) read(line(ix+10:),*) isn
	ix=index(line,keyword(2))
	if(ix.gt.0) read(line(ix+10:),*) delt
	ix=index(line,keyword(3))
	if(ix.gt.0) read(line(ix+10:),*) ndata
	ix=index(line,keyword(4))
	if(ix.eq.0) go to 105
	delt=delt/60.
	write(2,113) proj,'  MC',isn,nvar,delt,depth,' mcread'
113	format(a,a,i4,i2,2f6.1,a)
	write(6,114) proj,'  MC',isn,nvar,delt,depth,' mcread'
114	format(/10x,'Output file head: ',a,a,i4,i2,2f6.1,a)
	read(1,107,end=900) line
115	read(1,107,end=900) line
c
c	figure out where date/time are
c
	ic=index(line,',')

	if(ic.eq.0) go to 115
	do n=2,nvar
	ic=ic+index(line(ic+1:),',')
	enddo
	backspace 1
c
	if(nmark.gt.0) then
	xdel=delt/144.
	write (6,117) ((mdate(j,m),j=1,5),m=1,nmark)
117	format(//10x,'Time marks at ',4i3,i5/(24x,4i3,i5))
	write (6,118)
118	format(//)
	do 119 m=1,nmark
119	xdate(m)=delday(jdate,mdate(1,m))
	endif
c
c	read data
c
	
	nrec=0
120	read(1,107,end=200) line
	read(line,*,err=800) (x(n),n=1,nvar)
	read (line(ic+1:),121,err=800) idate(3),amon,idate(5),
     1	idate(1),idate(2),isec
121	format(i3,1x,a3,i5,1x,3(1x,i2))
	idate(4)=jmonth(amon)
	nrec=nrec+1
	pflg=.FALSE.
	if(nrec.le.5.or.ndata-nrec.lt.5) pflg=.TRUE. 
155	if(mark.le.nmark) then
	yday=delday(jdate,idate)
	ydel=yday-xdate(mark)
	if(ydel.gt.xdel) then
	mark=mark+1
	write (6,118)
	go to 155
	else
	if(ydel.ge.-xdel) pflg=.TRUE.
	endif
	endif

c
c	check for missing data
c
	if(nrec.gt.1) then
	delt0=delday(ldate,idate)*1440.
	if(abs(delt-delt0).gt.0.5) then
	write(6,'(a)') ' --- Time jump'
	pflg=.TRUE.
	endif
	endif
	T=x(1)
	C=x(2)
	CR=C/CS
	S=SAL78(CR,T,DEPTH,0)
	x(nvar+1)=c
	x(2)=s
 	write(2,form) idate,(x(i),i=1,nvar+1),nrec
     	if(pflg) write(6,form) idate,(x(i),i=1,nvar+1),nrec
	call sub_date(idate,ldate)
	go to 120
200	close(1)
	close(2)
	write(6,201) nrec
201	format(i6,' records written')
	go to 100
800	write(6,801) 
801	format(/10x,'Input data error, last line output:'/)
 	backspace 2
	read(2,form) ldate,(x(i),i=1,nvar+1),nrec
     	write(6,form) ldate,(x(i),i=1,nvar+1),nrec
	close(2)
900	stop
	end
