      subroutine readmod
c
c Reads a binafit model format file.
c
c     C. A. Hummel,  4 September 1991
c 
      include 'constnts.inc'
      include 'data.inc'
      include 'model.inc'

      real*8 ul(mxmod,mxfilt),ldud(mxmod,mxfilt)
      real*8 x1,x2,x3,y1,y2,y3

      integer mxlines          ! Max. number of model file lines
      parameter (mxlines=3000)
      character data*80
      logical errors

      errors=.false.

   10 write(outc,*)'Please enter model file name '
      write(outc,'(a,$)')'  or enter <cal> for calibrator: '
      read(inc,1)data
    1 format(a80)
      if(index(data,'cal ').ne.0)then
       calib=.true.
       return
      else
       calib=.false.
      endif
      open(2,file=data,status='old',err=10)
      write(outdat,'(a,a)')'! Input model file name: ',data(1:60)
c
      do i=1,mxlines              ! Look for star's FK5 number
       read(2,1,end=30)data
       j=index(data,'FK5')
       if(j.ne.0)then
        read(data(j+4:j+7),*,err=30,end=30)istar
        if(starno.ne.0.and.starno.ne.istar)then
         write(outc,*)'*** ERROR: selected stars in data and model ',
     &                'different!'
         close(2)
         goto 10
        elseif(starno.eq.0)then
         starno=istar
        endif
       endif
      enddo
   30 rewind 2
c
      do i=1,mxlines              ! First line w/out ! is 1st parameter line
       read(2,1,end=12)data
       if(index(data,'!').eq.0)then
        read(data,*,end=12,err=12)diam(1,1),diam(2,1),
     &              dratio(1,2),dratio(1,3),
     &              dratio(2,2),dratio(2,3),
     &              delmag(2,1),delmag(2,2),delmag(2,3),
     &              mfilter(1),mfilter(2),mfilter(3)
c
        delmag(1,3)=10.0
c       Comment next 2 lines for standard version
c       delmag(1,3)=delmag(2,3)
c       write(outc,*)'WARNING: Constant B-V BINAFIT version!'
c
        ncomp=2
        nfilt=3
        do ic=1,ncomp
         do ig=1,nfilt
          lcoeff(ic,ig)=0.D0
         enddo
        enddo
c       Request limb darkening information if diameters are large
        if(diam(1,1).gt.2.5.or.diam(2,1).gt.2.5)then
         write(outc,*)'Please enter limb darkening coefficients!'
         do ic=1,ncomp
          do ig=1,nfilt
           ul(ic,ig)=0.D0
           write(outc,'(a,i1,a,f4.0,a,$)')' Component ',ic,
     &                        ' Filter ',mfilter(ig),': [0] '
           read(inc,*)ul(ic,ig)
           ldud(ic,ig)=sqrt((1.D0-ul(ic,ig)/3.D0)
     &                      /(1.D0-7.D0*ul(ic,ig)/15.D0))
          enddo
          y1=ul(ic,1)
          y2=ul(ic,2)  
          y3=ul(ic,3)  
          x1=mfilter(1)
          x2=mfilter(2)
          x3=mfilter(3)
          lcoeff(ic,3)=((y3-y2)*(x2-x1)-(y2-y1)*(x3-x2))/
     &                   ((x3-x2)*(x2-x1)*(x3-x1))

          lcoeff(ic,2)=((y3-y2)*(x2*x2-x1*x1)-(y2-y1)*(x3*x3-x2*x2))/
     &                   ((x3-x2)*(x2-x1)*(x1-x3))

          lcoeff(ic,1)=y1-(lcoeff(ic,3)*x1*x1+lcoeff(ic,2)*x1)
         enddo
        endif
c
        do j=1,mxlines            ! Look for errors in previously read parms
         read(2,1,end=12)data
         if(index(data,'!').eq.0)goto 20  ! We did not find errors, but
c                                         ! the line with orbital parameters
c
         if(index(data,'! +/-').ne.0)then ! Here we found error values
          read(2,1,end=12)data
          read(data(2:80),*,end=12)parmerr(1),parmerr(2),parmerr(3),
     &                             parmerr(4),parmerr(5),parmerr(6),
     &                             parmerr(7),parmerr(8),parmerr(9)

          errors=.true.
          goto 20
         endif
        enddo
        goto 20     ! Could not find errors in MXLINES lines
       endif
      enddo
      goto 12       ! We looked through MXLINES lines but did not find parms

   20 do i=1,mxlines                 ! Look for second parameter line
       if(index(data,'!').eq.0)then
        read(data,*,end=12,err=12)a_true,e_true,period,epoch,inclin,
     &                            asc_node,arg_peri
        epoch=epoch-2440000.D0
        do j=1,mxlines
         read(2,1,end=112)data
         if(index(data,'! +/-').ne.0)then
          read(2,1,end=12)data
          read(data(2:80),*,end=12)parmerr(10),parmerr(11),parmerr(12),
     &                             parmerr(13),parmerr(14),parmerr(15),
     &                             parmerr(16)

          goto 111
         endif
        enddo
  112   if(errors)goto 12  ! You found 1st erros but not 2nd ones
        goto 111
       endif
       read(2,1,end=12)data
      enddo
c
   12 write(outc,*)' *** Something is wrong with the model file !***' 
      stop
  111 close(2)
      call cadjmod
      write(outdat,'(a,3(f13.9))')'! Coeff. of delta m fit: ',
     & ccoeff(1)+delmag(2,1),ccoeff(2),ccoeff(3)
      do ic=1,ncomp
       write(outdat,'(a,3(f13.9))')'! Coeff. of diameter fit: ',
     &  dcoeff(ic,1),dcoeff(ic,2),dcoeff(ic,3)
      enddo
      do ic=1,ncomp
       write(outdat,'(a,3(f13.9))')'! Coeff. of limb fit: ',
     &  lcoeff(ic,1),lcoeff(ic,2),lcoeff(ic,3)
      enddo
      do ic=1,ncomp
       write(outdat,'(a,3(f13.9))')'! d_ld/d_ud : ',
     &  ldud(ic,1),ldud(ic,2),ldud(ic,3)
      enddo
      return
      end

