      program calplot
      integer*4 nfilt,ntime,fk,fknum,icount,ndat
      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)
      character header*80,device*4,fname*80,fknam*4,fknam6*6
      character*80 data,wdata
      integer done
      integer nerr(nfilt)
      logical first
      done=0
      open(1,file='e:\\cal\\allcal.bin',err=10,status='old',
     & form='unformatted',recl=80,access='direct')
      goto 123
   10 write(6,*)' Enter name of concatenated .CAL file!'
      write(6,*)' Include .CAL or .BIN extension;'
      write(6,*)' .CAL file will be transformed to binary'
      write(6,*)' .BIN file for faster reading!'
      read(5,3,end=10)fname
    3 format(a80)
      indx=0
      indx=max(index(fname,'.CAL'),index(fname,'.cal'))
      if(indx.ne.0)then
       open(2,file=fname,err=10,status='old')
       open(1,file=fname(1:indx-1)//'.bin',status='new',err=444,
     & form='unformatted',recl=80,access='direct')
       write(6,*)' reading .cal file...'
       n=1
       do i=1,100000
        read(2,3,end=123)data
        if(data(1:10).ne.'          ')then
         write(1,rec=n)data
         n=n+1
        endif
       enddo
      else
       open(1,file=fname,err=10,status='old',form='unformatted',
     & recl=80,access='direct')
      endif
  123 write(6,*)' Screen (s) or hardcopy (h) ?'
      read(5,3)device
      if(device.eq.'h'.or.device.eq.'H')then
       device='/lpm'
       iy=4
       csize=3.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 (n/0 to stop)!'
      read(5,'(a)',end=114)fknam
      if(index(fknam,'n').ne.0)goto 114
      read(fknam,*)fknum
      if(fknum.eq.0)goto 114
      fknam6='      '
      write(fknam6(2:5),'(i4)')fknum
      open(3,file=fknam//'.cal')
      call pgbegin(0,device,3,iy)
      call pgsch(csize)
      icount=1
  100 do i=1,nfilt
      vismax(i)=0
      vismin(i)=0
      errsum(i)=0
      vismean(i)=0
      vismerr(i)=0
      nerr(i)=0
      enddo
      timmin=6
      timmax=7
      if(done.eq.1)goto 113
      ndat=1
      read(1,rec=icount)data
      wdata=data
      first=.true.
      icount=icount+1
      read(data,*)date,bl,lambda(1),lambda(3),lambda(2)
      do 110 n=1,ntime
      read(1,rec=icount,end=112)data
      icount=icount+1
      read(data,*)time(ndat)
      if(time(ndat).gt.24)then
       icount=icount-1
       goto 111
      endif
      IF(INDEX(DATA,FKNAM).NE.0)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)
      if(fk.eq.fknum.and.time(ndat).ne.0)then
      if(first)then
       write(3,2)wdata
       first=.false.
      endif
    2 format(a80)
      write(3,2)data
      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
      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)
      write(header,1)fknum,int(date),int(bl),int(lambda(j))
    1 format('FK ',i4,1x,i6,1x,' B.l. ',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(4)
      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)
      if(viserr(j,i).ne.0)then
       vismean(j)=vismean(j)+y(i)/viserr(j,i)/viserr(j,i)
       vismerr(j)=vismerr(j)+1./viserr(j,i)/viserr(j,i)
      endif
      endif
      enddo
      enddo
      if(done.eq.1)goto 113
      goto 100
  112 done=1
      goto 111
  113 call pgend
      close(3)
      do j=1,nfilt
      if(vismerr(j).ne.0)then
       vismean(j)=vismean(j)/vismerr(j)
      endif
      do i=1,ndat
      if(vis(j,i).ne.2.and.viserr(j,i).ne.0)then
       errsum(j)=errsum(j)+(vismean(j)-vis(j,i))**2/(viserr(j,i)**2)
       nerr(j)=nerr(j)+1
      endif
      enddo
      if(nerr(j).ne.0)then
       errsum(j)=errsum(j)/float(nerr(j))
      endif
      enddo
      write(6,*)' Last star: chi^2/N = ',(errsum(j),j=1,nfilt)
      write(6,*)'   rel. to mean vis = ',(vismean(j),j=1,nfilt)
      done=0
      icount=1
      goto 20
  444 write(6,*)' .BIN file exists! Delete it and restart!'
  114 end

