      program calplot
      integer*4 nfilt,ntime,fk,fknum
      parameter (nfilt=3,ntime=300)
      real*4 vis(nfilt,ntime),viserr(nfilt,ntime)
      real*4 time(ntime),y(ntime)
      real*4 vismax(nfilt),vismin(nfilt),lambda(nfilt)
      real*4 errsum(nfilt),vismean(nfilt),vismerr(nfilt),rms(nfilt)
      character data*80,header*80,device*4,fknam*4
      integer done
      integer nerr(nfilt)
      logical fake
      done=0

   10 write(6,*)'Enter name of concatenated .CAL file!'
      read(5,3,end=10)data
    3 format(a80)
      open(1,file=data,err=10,status='old')
      fake=.false.
      write(6,*)'Screen (s) or hardcopy (h) ?'
      read(5,3)device
      if(device.eq.'h'.or.device.eq.'H')then
       device='/ps'
       iy=3
       csize=2.0
      else
c     if(device.eq.'s'.or.device.eq.'S')then
       device='/ega'
       iy=3
       csize=2.5
      endif
   20 write(6,*)'Enter FK number of star (q/0 to stop)!'
      read(5,'(a)',end=114)fknam
      if(index(fknam,'q').ne.0.or.index(fknam,'n').ne.0)goto 114
      read(fknam,*)fknum
      if(fknum.eq.0)goto 114
      call pgbegin(0,device,3,iy)
      call pgsch(csize)
   21 read(1,3,end=113)data
      if(data(1:10).eq.'          ')goto 21

  100 do i=1,nfilt
       vismax(i)=0
       vismin(i)=0
C      errsum(i)=0
C      vismean(i)=0
C      vismerr(i)=0
C      rms(i)=0
C      nerr(i)=0
      enddo
      timmin=6
      timmax=7
      if(done.eq.1)goto 113
      ndat=1
      if(index(data,'!').ne.0)then
       fake=.true.
       goto 100
      endif
      read(data,*)date,bl,lambda(1),lambda(3),lambda(2)
      do 110 n=1,ntime
       read(1,3,end=112)data
       if(data(1:10).eq.'          ')goto 110
       read(data,*)time(ndat)
       if(time(ndat).gt.24)goto 111
       if(.not.fake)then
        read(data,*,end=110)time(ndat),fk,d1,vis(1,ndat),viserr(1,ndat),
     &   d2,vis(3,ndat),viserr(3,ndat),d3,vis(2,ndat),viserr(2,ndat)
       else
        read(data,*,end=110)time(ndat),fk,vis(1,ndat),viserr(1,ndat),
     &   vis(3,ndat),viserr(3,ndat),vis(2,ndat),viserr(2,ndat)
       endif
       if(fk.eq.fknum.and.time(ndat).ne.0)then
        do j=1,nfilt
         if(vis(j,ndat).ne.2)then
          vismax(j)=max(vismax(j),vis(j,ndat))
          vismin(j)=min(vismin(j),vis(j,ndat))
         endif
        enddo
        timmax=max(timmax,time(ndat))
        timmin=min(timmin,time(ndat))
        ndat=ndat+1
       endif
  110 continue

  111 ndat=ndat-1
      if(ndat.eq.0)goto 100
      do j=1,nfilt
       call pgsci(5)
       timr=timmax-timmin
       timmin=timmin-timr/10.
       timmax=timmax+timr/10.
       vr=vismax(j)-vismin(j)
       vismax(j)=vismax(j)+vr/8.
       call pgenv(timmin,timmax,vismin(j),vismax(j),0,1)
c
        if(j.eq.1)then
         write(header,1)fknum,int(lambda(j))
    1    format('FK5 ',i4,3x,i3,'nm')
        elseif(j.eq.2)then
         write(header,2)int(date),int(lambda(j))
    2    format(2x,i6,3x,i3,'nm')
        elseif(j.eq.3)then
         write(header,4)int(bl),int(lambda(j))
    4    format(3x,'Bl ',i2,3x,i3,'nm')
        endif
c
c      write(header,1)fknum,int(date),int(bl),int(lambda(j))
c   1  format('FK',i4,1x,i6,1x,' B.',i2,1x,i3,' nm')
       csize=csize+1
       call pgsch(csize)
       call pglabel('','',header)
       csize=csize-1
       call pgsch(csize)
       if(j.eq.1)call pgsci(2)
       if(j.eq.2)call pgsci(3)
       if(j.eq.3)call pgsci(11)
       do i=1,ndat
        y(i)=vis(j,i)
        if(y(i).ne.2)then
         call pgpoint(1,time(i),y(i),1)
         vislo=vis(j,i)-viserr(j,i)
         vishi=vis(j,i)+viserr(j,i)
         call pgerry(1,time(i),vislo,vishi,1.0)
C        if(viserr(j,i).ne.0)then
C         vismean(j)=vismean(j)+y(i)/viserr(j,i)/viserr(j,i)
C         vismerr(j)=vismerr(j)+1./viserr(j,i)/viserr(j,i)
C        endif
        endif
       enddo
      enddo
      if(done.eq.1)goto 113
      goto 100

  112 done=1
      goto 111

  113 call pgend
C     do j=1,nfilt
C     if(vismerr(j).ne.0)then
C      vismean(j)=vismean(j)/vismerr(j)
C     endif
C     do i=1,ndat
C     if(vis(j,i).ne.2.and.viserr(j,i).ne.0)then
C      errsum(j)=errsum(j)+(vismean(j)-vis(j,i))**2/(viserr(j,i)**2)
C      rms(j)=rms(j)+(vismean(j)-vis(j,i))**2
C      nerr(j)=nerr(j)+1
C     endif
C     enddo
C     if(nerr(j).ne.0)then
C      errsum(j)=errsum(j)/float(nerr(j))
C      rms(j)=sqrt(rms(j)/float(nerr(j)))
C     endif
C     enddo
C     write(6,*)' Last star: chi^2/N = ',(errsum(j),j=1,nfilt)
C     write(6,*)'   rel. to mean vis = ',(vismean(j),j=1,nfilt)
C     if(vismean(j).ne.0)then
C     write(6,*)'              rms % = ',
C    & (rms(j)/vismean(j)*100.,j=1,nfilt)
C     endif
      done=0
      rewind 1
      goto 20
  114 end

