! nob: number of observations
! no_it: number of ideal types
! no_cv: number of context variables
! no_des: number of design variables
      USE MSIMSLSS
      INTEGER    INTCEPT, LDX, NCOEF, NIND, NOBS
C	      
      REAL       B(3), SSE, SST, X(4,3), Y(4)      
	REAL       config_c(10,15),config_d(10,15),xc(3000,15)
	REAL       xd(3000,15),fit(3000),	config_h(3000,15)
!
!	 x(no_cv,no_it), y(no_cv)
!	 B(ncoef)
!
      open (unit=7, file='d:\FitCalculate\Con_hybrid\chtfout.txt')
	  open (unit=8, file='d:\FitCalculate\Con_hybrid\chtfin.txt')
	  read (8,*) nob,no_it,no_cv,no_des
	  INTCEPT=0
	  NIND = no_it
	  NOBS = no_cv
	  LDX =NOBS
	  NCOEF = INTCEPT + NIND  

!
	  do 10 i=1,no_it
	    read(8,*) (config_c(i,j),j=1,no_cv),(config_d(i,j),j=1,no_des)
10 	  continue
!
	  do 7 i =1,no_cv
	    do 8 j = 1,no_it  
	     X(i,j) = config_c(j,i)
8       continue
7     continue
!
      do 20 i=1,nob
	  	 read(8,*) (xc(i,j),j=1,no_cv),(xd(i,j),j=1,no_des)
		 do 21 j1= 1,no_cv
		    Y(j1)=xc(i,j1)
21       continue
!
	CALL RLSE (NOBS, Y, NIND, X, LDX, INTCEPT, B, SST, SSE)
!    	
c      write(*,*) B
c	write(7,777) nobs,ncoef,nind,ldx,(B(jjj),jjj=1,ncoef),sst,sse	
c777   format(1x, 4(i5,x),6(f7.3,1x))				   
c	do 707 ii=1,no_cv
c	   write(7,708) (x(ii,jj),jj=1,no_it),y(ii)
c708      format(1x, 4(f10.4,1x))
c707   continue
      do 35 j1=1,no_des
	     config_h(i,j1)=0.
	     do 36 j2=1,no_it			    
		    config_h(i,j1)=config_h(i,j1)+B(j2)*config_d(j2,j1)
 36      continue
 35   continue
! 
!
         fit(i)=0. 
         do 40 k2=1,no_des
			   fit(i) = fit(i) +  (xd(i,k2)-config_h(i,k2))**2.
40          continue 			 
          fit(i)= -(fit(i)**0.5) 		 	  
20    continue
      do 60 i=1,nob
	    write(7,66) fit(i)
66      format(1x,f10.4)
60    continue
	  end
!
! 

c
      